Debian Codegraph @

radiant/xyview/tools/CameraMoveTool.h

raw
1 #pragma once
2
3 #include "XYMouseToolEvent.h"
4 #include "i18n.h"
5 #include "imousetool.h"
6 #include "iorthoview.h"
7 #include "icameraview.h"
8 #include "selectionlib.h"
9
10 namespace ui
11 {
12
13 /**
14 * The CameraMoveTool translates the camera to the
15 * world position that is clicked on by the mouse.
16 */
17 class CameraMoveTool :
18 public MouseTool
19 {
20 public:
21 const std::string& getName() override
22 {
23 static std::string name("CameraMoveTool");
24 return name;
25 }
26
27 const std::string& getDisplayName() override
28 {
29 static std::string displayName(_("Drag Camera"));
30 return displayName;
31 }
32
33 Result onMouseDown(Event& ev) override
34 {
35 try
36 {
37 positionCamera(dynamic_cast<XYMouseToolEvent&>(ev));
38
39 return Result::Activated;
40 }
41 catch (std::bad_cast&)
42 {
43 }
44
45 return Result::Ignored; // not handled
46 }
47
48 Result onMouseMove(Event& ev) override
49 {
50 try
51 {
52 positionCamera(dynamic_cast<XYMouseToolEvent&>(ev));
53
54 return Result::Continued;
55 }
56 catch (std::bad_cast&)
57 {
58 }
59
60 return Result::Ignored;
61 }
62
63 Result onMouseUp(Event& ev) override
64 {
65 try
66 {
67 // We only operate on XY view events, so attempt to cast
68 dynamic_cast<XYMouseToolEvent&>(ev).getScale();
69 return Result::Finished;
70 }
71 catch (std::bad_cast&)
72 {
73 }
74
75 return Result::Ignored;
76 }
77
78 unsigned int getRefreshMode() override
79 {
80 return RefreshMode::Force | RefreshMode::AllViews; // update cam view too
81 }
82
83 private:
84 void positionCamera(XYMouseToolEvent& xyEvent)
85 {
86 try
87 {
88 auto& camera = GlobalCameraManager().getActiveView();
89
90 Vector3 origin = xyEvent.getWorldPos();
91
92 // Substitute the third component from the selection work zone
93 auto workzoneOrigin = GlobalSelectionSystem().getWorkZone().bounds.getOrigin();
94
95 switch (xyEvent.getViewType())
96 {
97 case OrthoOrientation::XY:
98 origin[2] = workzoneOrigin[2];
99 break;
100 case OrthoOrientation::YZ:
101 origin[0] = workzoneOrigin[0];
102 break;
103 case OrthoOrientation::XZ:
104 origin[1] = workzoneOrigin[1];
105 break;
106 };
107
108 xyEvent.getView().snapToGrid(origin);
109 camera.setCameraOrigin(origin);
110
111 xyEvent.getView().queueDraw();
112 }
113 catch (const std::runtime_error&)
114 {
115 // no camera present
116 }
117 }
118 };
119
120 }