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