1 /***********************************************************************
2     created:    Fri Feb 17 2012
3     author:     Paul D Turner <paul@cegui.org.uk>
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2012 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #include "CEGUI/SystemKeys.h"
28 
29 namespace CEGUI
30 {
31 //----------------------------------------------------------------------------//
SystemKeys()32 SystemKeys::SystemKeys() :
33     d_current(0),
34     d_leftShift(false),
35     d_rightShift(false),
36     d_leftCtrl(false),
37     d_rightCtrl(false),
38     d_leftAlt(false),
39     d_rightAlt(false)
40 {
41 }
42 
43 //----------------------------------------------------------------------------//
reset()44 void SystemKeys::reset()
45 {
46     *this = SystemKeys();
47 }
48 
49 //----------------------------------------------------------------------------//
get() const50 uint SystemKeys::get() const
51 {
52     return d_current;
53 }
54 
55 //----------------------------------------------------------------------------//
isPressed(SystemKey key) const56 bool SystemKeys::isPressed(SystemKey key) const
57 {
58     return (d_current & key) || (!key && !d_current);
59 }
60 
61 //----------------------------------------------------------------------------//
keyPressed(Key::Scan key)62 void SystemKeys::keyPressed(Key::Scan key)
63 {
64     updatePressedStateForKey(key, true);
65     updateSystemKeyState(keyCodeToSystemKey(key));
66 }
67 
68 //----------------------------------------------------------------------------//
keyReleased(Key::Scan key)69 void SystemKeys::keyReleased(Key::Scan key)
70 {
71     updatePressedStateForKey(key, false);
72     updateSystemKeyState(keyCodeToSystemKey(key));
73 }
74 
75 //----------------------------------------------------------------------------//
keyCodeToSystemKey(Key::Scan key)76 SystemKeys::SystemKey SystemKeys::keyCodeToSystemKey(Key::Scan key)
77 {
78     switch (key)
79     {
80     case Key::RightShift:
81     case Key::LeftShift:
82         return Shift;
83 
84     case Key::LeftControl:
85     case Key::RightControl:
86         return Control;
87 
88     case Key::LeftAlt:
89     case Key::RightAlt:
90         return Alt;
91 
92     default:
93         return None;
94     }
95 }
96 
97 //----------------------------------------------------------------------------//
updateSystemKeyState(SystemKey syskey)98 void SystemKeys::updateSystemKeyState(SystemKey syskey)
99 {
100     switch (syskey)
101     {
102     case Shift:
103         (d_leftShift || d_rightShift) ? d_current |= Shift : d_current &= ~Shift;
104         break;
105 
106     case Control:
107         (d_leftCtrl || d_rightCtrl) ? d_current |= Control : d_current &= ~Control;
108         break;
109 
110     case Alt:
111         (d_leftAlt || d_rightAlt) ? d_current |= Alt : d_current &= ~Alt;
112         break;
113 
114     default:
115         break;
116     }
117 }
118 
119 //----------------------------------------------------------------------------//
updatePressedStateForKey(Key::Scan key,bool state)120 void SystemKeys::updatePressedStateForKey(Key::Scan key, bool state)
121 {
122     switch (key)
123     {
124     case Key::LeftShift:
125         d_leftShift = state;
126         break;
127 
128     case Key::RightShift:
129         d_rightShift = state;
130         break;
131 
132     case Key::LeftControl:
133         d_leftCtrl = state;
134         break;
135 
136     case Key::RightControl:
137         d_rightCtrl = state;
138         break;
139 
140     case Key::LeftAlt:
141         d_leftAlt = state;
142         break;
143 
144     case Key::RightAlt:
145         d_rightAlt = state;
146         break;
147 
148     default:
149         break;
150     }
151 }
152 
153 //----------------------------------------------------------------------------//
mouseButtonPressed(MouseButton button)154 void SystemKeys::mouseButtonPressed(MouseButton button)
155 {
156     d_current |= mouseButtonToSystemKey(button);
157 }
158 
159 //----------------------------------------------------------------------------//
mouseButtonReleased(MouseButton button)160 void SystemKeys::mouseButtonReleased(MouseButton button)
161 {
162     d_current &= ~mouseButtonToSystemKey(button);
163 }
164 
165 //----------------------------------------------------------------------------//
mouseButtonToSystemKey(MouseButton button)166 SystemKeys::SystemKey SystemKeys::mouseButtonToSystemKey(MouseButton button)
167 {
168 	switch (button)
169 	{
170 	case LeftButton:
171 		return LeftMouse;
172 
173 	case RightButton:
174 		return RightMouse;
175 
176 	case MiddleButton:
177 		return MiddleMouse;
178 
179 	case X1Button:
180 		return X1Mouse;
181 
182 	case X2Button:
183 		return X2Mouse;
184 
185 	default:
186 		return None;
187 	}
188 }
189 //----------------------------------------------------------------------------//
190 }
191 
192