Back to index

nux  3.0.0
Events.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 #include "GLResource.h"
00023 #include "Events.h"
00024 
00025 
00026 namespace nux
00027 {
00028   MouseButton GetEventButton(unsigned long button_state)
00029   {
00030     if ((button_state & NUX_EVENT_BUTTON1_DOWN) || (button_state & NUX_EVENT_BUTTON1_UP))
00031       return NUX_MOUSE_BUTTON1;
00032     else if ((button_state & NUX_EVENT_BUTTON2_DOWN) || (button_state & NUX_EVENT_BUTTON2_UP))
00033       return NUX_MOUSE_BUTTON2;
00034     else if ((button_state & NUX_EVENT_BUTTON3_DOWN) || (button_state & NUX_EVENT_BUTTON3_UP))
00035       return NUX_MOUSE_BUTTON3;
00036     else if ((button_state & NUX_EVENT_BUTTON4_DOWN) || (button_state & NUX_EVENT_BUTTON4_UP))
00037       return NUX_MOUSE_BUTTON4;
00038 
00039     return NUX_INVALID_MOUSE_BUTTON;
00040   }
00041   
00042   bool GetButtonState(unsigned long button_state, MouseButton button)
00043   {
00044     if (button == NUX_MOUSE_BUTTON1)
00045       return(button_state & NUX_STATE_BUTTON1_DOWN) ? true : false;
00046     else if (button == NUX_MOUSE_BUTTON2)
00047       return(button_state & NUX_STATE_BUTTON2_DOWN) ? true : false;
00048     else if (button == NUX_MOUSE_BUTTON3)
00049       return(button_state & NUX_STATE_BUTTON3_DOWN) ? true : false;
00050     else if (button == NUX_MOUSE_BUTTON4)
00051       return(button_state & NUX_STATE_BUTTON4_DOWN) ? true : false;
00052 
00053     return false;
00054   }
00055   
00056   bool GetKeyModifierState(unsigned long key_modifiers_states, KeyModifier key_modifier)
00057   {
00058     return((key_modifiers_states & key_modifier) != 0);
00059   }
00060 
00061   Event::Event()
00062   {
00063     Memset(text, 0, sizeof(text));
00064 
00065     for (int i = 0; i < NUX_MAX_VK; i++)
00066     {
00067       VirtualKeycodeState[i] = 0;
00068     }
00069 
00070     ascii_code = 0;
00071     virtual_code = 0;
00072     key_modifiers = 0;
00073     key_repeat_count = 0;
00074     mouse_state = 0;
00075     x = -1;
00076     y = -1;
00077     x_root = 0;
00078     y_root = 0;
00079     dx = 0;
00080     dy = 0;
00081     clicks = 0;
00082     is_click = 0;
00083     wheel_delta = 0;
00084 
00085 #if defined(NUX_OS_WINDOWS)
00086     win32_keycode = 0;
00087     win32_keysym = 0;
00088 #endif
00089 
00090 #if defined(NUX_OS_LINUX)
00091     x11_keycode = 0;
00092     x11_keysym = 0;
00093     x11_timestamp = 0;
00094     x11_window = 0;
00095     x11_key_state = 0;
00096 #endif
00097 
00098     //Application = 0;
00099   }
00100 
00101   void Event::Reset()
00102   {
00103     type = NUX_NO_EVENT;
00104     Memset(text, 0, sizeof(text));
00105 #if defined(NUX_OS_WINDOWS)
00106     win32_keycode = 0;
00107     win32_keysym = 0;
00108 #endif
00109 
00110 #if defined(NUX_OS_LINUX)
00111     x11_keycode = 0;
00112     x11_keysym = 0;
00113     x11_key_state = 0;
00114 #endif
00115 
00116     key_repeat_count = 0;
00117     key_modifiers = 0;
00118     wheel_delta = 0;
00119   }
00120 
00121   int Event::GetX() const
00122   {
00123     return x;
00124   }
00125   int Event::GetY() const
00126   {
00127     return y;
00128   }
00129   int Event::GetRootX() const
00130   {
00131     return x_root;
00132   }
00133   int Event::GetRootY() const
00134   {
00135     return y_root;
00136   }
00137   int Event::GetDeltaX() const
00138   {
00139     return dx;
00140   }
00141   int Event::GetDeltaY() const
00142   {
00143     return dy;
00144   }
00145 
00146   unsigned long Event::GetKeyState()      const
00147   {
00148     return key_modifiers;
00149   }
00150 
00151   unsigned long Event::GetMouseState() const
00152   {
00153     return mouse_state;
00154   }
00155 
00156   MouseButton Event::GetEventButton() const
00157   {
00158     if ((mouse_state & NUX_EVENT_BUTTON1_DOWN) || (mouse_state & NUX_EVENT_BUTTON1_UP))
00159       return NUX_MOUSE_BUTTON1;
00160     else if ((mouse_state & NUX_EVENT_BUTTON2_DOWN) || (mouse_state & NUX_EVENT_BUTTON2_UP))
00161       return NUX_MOUSE_BUTTON2;
00162     else if ((mouse_state & NUX_EVENT_BUTTON3_DOWN) || (mouse_state & NUX_EVENT_BUTTON3_UP))
00163       return NUX_MOUSE_BUTTON3;
00164     else if ((mouse_state & NUX_EVENT_BUTTON4_DOWN) || (mouse_state & NUX_EVENT_BUTTON4_UP))
00165       return NUX_MOUSE_BUTTON4;
00166 
00167     return NUX_INVALID_MOUSE_BUTTON;
00168   }
00169 
00170   bool Event::GetButtonState(MouseButton button) const
00171   {
00172     if (button == 1)
00173       return(mouse_state & NUX_STATE_BUTTON1_DOWN) ? true : false;
00174     else if (button == 2)
00175       return(mouse_state & NUX_STATE_BUTTON2_DOWN) ? true : false;
00176     else if (button == 3)
00177       return(mouse_state & NUX_STATE_BUTTON3_DOWN) ? true : false;
00178     else if (button == 4)
00179       return(mouse_state & NUX_STATE_BUTTON4_DOWN) ? true : false;
00180 
00181     return false;
00182   }
00183 
00184   bool Event::GetKeyModifierState(KeyModifier key_modifier) const
00185   {
00186     return((key_modifiers & key_modifier) != 0);
00187   }
00188 
00190 
00194   unsigned long Event::GetKeySym() const
00195   {
00196 #if defined(NUX_OS_WINDOWS)
00197     return win32_keysym;
00198 #endif
00199 
00200 #if defined(NUX_OS_LINUX)
00201     return x11_keysym;
00202 #endif
00203 
00204     return 0;
00205   }
00206   unsigned short Event::GetKeyRepeatCount() const
00207   {
00208     return key_repeat_count;
00209   }
00210 
00211   const char* Event::GetText() const
00212   {
00213     return text;
00214   }
00215 
00216 
00218 
00223   unsigned long Event::GetVirtualKeyState(unsigned long VirtualKey) const
00224   {
00225     if (VirtualKey >= NUX_MAX_VK)
00226       return 0;
00227 
00228     if (VirtualKey <= 0)
00229       return 0;
00230 
00231     return VirtualKeycodeState[VirtualKey];
00232   }
00233 
00234   bool Event::IsShiftDown() const
00235   {
00236     return (key_modifiers & NUX_STATE_SHIFT) ? true : false;
00237   }
00238 
00239   bool Event::IsControlDown() const
00240   {
00241     return (key_modifiers & NUX_STATE_CTRL) ? true : false;
00242   }
00243 
00244   bool Event::IsCapsLockDown() const
00245   {
00246     return (key_modifiers & NUX_STATE_CAPS_LOCK) ? true : false;
00247   }
00248 
00249   bool Event::IsAltDown() const
00250   {
00251     return (key_modifiers & NUX_STATE_ALT) ? true : false;
00252   }
00253 
00254   bool Event::IsNumLockDown() const
00255   {
00256     return (key_modifiers & NUX_STATE_NUMLOCK) ? true : false;
00257   }
00258 
00259   bool Event::IsScrollLockDown() const
00260   {
00261     return (key_modifiers & NUX_STATE_SCROLLLOCK) ? true : false;
00262   }
00263 
00264   bool Event::IsSuperKeyDown() const
00265   {
00266     return (key_modifiers & NUX_STATE_SCROLLLOCK) ? true : false;
00267   }
00268 
00269 }
00270