File: MouseButton.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (178 lines) | stat: -rw-r--r-- 4,044 bytes parent folder | download | duplicates (6)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#pragma once

#include <wx/event.h>
#include "Modifier.h"

namespace wxutil
{

class MouseButton
{

#define ATTR_BUTTON ("button")

#define BUTTONSTR_LMB "LMB"
#define BUTTONSTR_MMB "MMB"
#define BUTTONSTR_RMB "RMB"
#define BUTTONSTR_AUX1 "AUX1"
#define BUTTONSTR_AUX2 "AUX2"

public:
	enum ButtonFlags
	{
		NONE	        = 0,
		LEFT	        = 1 << 1,
		RIGHT	        = 1 << 2,
		MIDDLE	        = 1 << 3,
		AUX1	        = 1 << 4,
		AUX2	        = 1 << 5,
        ALL_BUTTON_MASK = (LEFT | RIGHT | MIDDLE | AUX1 | AUX2),
		/* Used by wxutil::Modifier
        SHIFT	= 1 << 6,
		CONTROL	= 1 << 7,
		ALT		= 1 << 8
        */
	};

    // Returns a bit mask corresponding to a single mouse button CHANGE event
    // Only one mouse button will be marked in this bit mask since only one
    // button can be changed at a given time. The keyboard modifier state
    // is added to the bit mask in the usual way, the according bit flag will
    // be set if the modifier key is currently held. This also checks the flag
    // if a double-click event is generated by wxWidgets
    static unsigned int GetButtonStateChangeForMouseEvent(wxMouseEvent& ev)
	{
		unsigned int newState = NONE;

		if (ev.LeftDown() || ev.LeftUp() || ev.LeftDClick())
		{
			newState |= LEFT;
		}
		else if (ev.RightDown() || ev.RightUp() || ev.RightDClick())
		{
			newState |= RIGHT;
		}
		else if (ev.MiddleDown() || ev.MiddleUp() || ev.MiddleDClick())
		{
			newState |= MIDDLE;
		}
#if wxCHECK_VERSION(3,0,0)
		else if (ev.Aux1Down() || ev.Aux1Up() || ev.Aux1DClick())
		{
			newState |= AUX1;
		}
		else if (ev.Aux2Down() || ev.Aux2Up() || ev.Aux2DClick())
		{
			newState |= AUX2;
		}
#endif
        // Add the modifier key state as usual
        newState |= Modifier::GetStateForMouseEvent(ev);

		return newState;
	}
    
    // Returns a bit mask representing the current mouse and modifier state
    // Since it represents the current state it's possible to find multiple
    // mouse buttons being held at the same time.
	static unsigned int GetStateForMouseEvent(wxMouseEvent& ev)
	{
		unsigned int newState = NONE;

		if (ev.LeftIsDown())
		{
			newState |= LEFT;
		}
		else
		{
			newState &= ~LEFT;
		}
	
		if (ev.RightIsDown())
		{
			newState |= RIGHT;
		}
		else
		{
			newState &= ~RIGHT;
		}

		if (ev.MiddleIsDown())
		{
			newState |= MIDDLE;
		}
		else
		{
			newState &= ~MIDDLE;
		}

#if wxCHECK_VERSION(3,0,0)
		if (ev.Aux1IsDown())
		{
			newState |= AUX1;
		}
		else
		{
			newState &= ~AUX1;
		}

		if (ev.Aux2IsDown())
		{
			newState |= AUX2;
		}
		else
		{
			newState &= ~AUX2;
		}
#endif
        newState |= Modifier::GetStateForMouseEvent(ev);

		return newState;
	}

    // Parses the node's attributes to the corresponding flag
    static unsigned int LoadFromNode(const xml::Node& node)
    {
        return GetStateFromString(node.getAttributeValue(ATTR_BUTTON));
    }

    // Converts "LMB" to the corresponding flag
    static unsigned int GetStateFromString(const std::string& str)
    {
        if (str == BUTTONSTR_LMB) return LEFT;
        if (str == BUTTONSTR_RMB) return RIGHT;
        if (str == BUTTONSTR_MMB) return MIDDLE;
        if (str == BUTTONSTR_AUX1) return AUX1;
        if (str == BUTTONSTR_AUX2) return AUX2;

        return NONE;
    }

    static std::string GetButtonString(unsigned int state)
    {
        if ((state & LEFT) != 0) return BUTTONSTR_LMB;
        if ((state & RIGHT) != 0) return BUTTONSTR_RMB;
        if ((state & MIDDLE) != 0) return BUTTONSTR_MMB;
        if ((state & AUX1) != 0) return BUTTONSTR_AUX1;
        if ((state & AUX2) != 0) return BUTTONSTR_AUX2;

        return "";
    }

    // Saves the button flags to the given node
    static void SaveToNode(unsigned int state, xml::Node& node)
    {
        node.setAttributeValue(ATTR_BUTTON, GetButtonString(state));
    }

    static void ForeachButton(const std::function<void(unsigned int)>& func)
    {
        func(LEFT);
        func(MIDDLE);
        func(RIGHT);
        func(AUX1);
        func(AUX2);
    }
};

} // namespace