Debian Codegraph @

radiant/xyview/tools/ZoomTool.h

raw
1 #pragma once
2
3 #include "i18n.h"
4 #include "imousetool.h"
5 #include "iorthoview.h"
6
7 namespace ui
8 {
9
10 /**
11 * The ZoomTool implements the drag-to-zoom functionality used in
12 * the orthoviews. The mouse pointer is captured during its active
13 * phase and only the move deltas are processed to zoom in and out.
14 */
15 class ZoomTool :
16 public MouseTool
17 {
18 private:
19 int _dragZoom;
20
21 public:
22 const std::string& getName() override
23 {
24 static std::string name("ZoomTool");
25 return name;
26 }
27
28 const std::string& getDisplayName() override
29 {
30 static std::string displayName(_("Zoom View"));
31 return displayName;
32 }
33
34 unsigned int getPointerMode() override
35 {
36 return PointerMode::Capture | PointerMode::Freeze |
37 PointerMode::Hidden | PointerMode::MotionDeltas;
38 }
39
40 Result onMouseDown(Event& ev) override
41 {
42 try
43 {
44 // Perform a dynamic cast to ensure we're on the correct view
45 dynamic_cast<OrthoViewMouseToolEvent&>(ev).getOrthoView();
46
47 _dragZoom = 0;
48
49 return Result::Activated;
50 }
51 catch (std::bad_cast&)
52 {
53 }
54
55 return Result::Ignored; // not handled
56 }
57
58 Result onMouseMove(Event& ev) override
59 {
60 try
61 {
62 // We use capture mode, so orthoview event will contain the delta only
63 auto& xyEvent = dynamic_cast<OrthoViewMouseToolEvent&>(ev);
64
65 if (xyEvent.getDeviceDelta().y() != 0)
66 {
67 _dragZoom += xyEvent.getDeviceDelta().y();
68
69 while (abs(_dragZoom) > 8)
70 {
71 if (_dragZoom > 0)
72 {
73 xyEvent.getOrthoView().zoomOut();
74 _dragZoom -= 8;
75 }
76 else
77 {
78 xyEvent.getOrthoView().zoomIn();
79 _dragZoom += 8;
80 }
81 }
82 }
83
84 return Result::Continued;
85 }
86 catch (std::bad_cast&)
87 {
88 }
89
90 return Result::Ignored;
91 }
92
93 Result onMouseUp(Event& ev) override
94 {
95 try
96 {
97 // We only operate on orthoview events, so attempt to cast
98 dynamic_cast<OrthoViewMouseToolEvent&>(ev).getOrthoView();
99 return Result::Finished;
100 }
101 catch (std::bad_cast&)
102 {
103 }
104
105 return Result::Ignored;
106 }
107 };
108
109 }