KX_BlenderKeyboardDevice.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file gameengine/BlenderRoutines/KX_BlenderKeyboardDevice.cpp
  28. * \ingroup blroutines
  29. */
  30. #ifdef _MSC_VER
  31. /* annoying warnings about truncated STL debug info */
  32. # pragma warning (disable:4786)
  33. #endif
  34. #include "KX_BlenderKeyboardDevice.h"
  35. #include "KX_KetsjiEngine.h"
  36. KX_BlenderKeyboardDevice::KX_BlenderKeyboardDevice()
  37. : m_hookesc(false)
  38. {
  39. }
  40. KX_BlenderKeyboardDevice::~KX_BlenderKeyboardDevice()
  41. {
  42. }
  43. /**
  44. * IsPressed gives boolean information about keyboard status, true if pressed, false if not
  45. */
  46. bool KX_BlenderKeyboardDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
  47. {
  48. const SCA_InputEvent & inevent = m_eventStatusTables[m_currentTable][inputcode];
  49. bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
  50. inevent.m_status == SCA_InputEvent::KX_ACTIVE);
  51. return pressed;
  52. }
  53. /*const SCA_InputEvent& KX_BlenderKeyboardDevice::GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)
  54. {
  55. return m_eventStatusTables[m_currentTable][inputcode];
  56. }
  57. */
  58. /**
  59. * NextFrame toggles currentTable with previousTable,
  60. * and copy relevant event information from previous to current
  61. * (pressed keys need to be remembered)
  62. */
  63. void KX_BlenderKeyboardDevice::NextFrame()
  64. {
  65. SCA_IInputDevice::NextFrame();
  66. // now convert justpressed keyevents into regular (active) keyevents
  67. int previousTable = 1-m_currentTable;
  68. for (int keyevent= KX_BEGINKEY; keyevent<= KX_ENDKEY;keyevent++)
  69. {
  70. SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][keyevent];
  71. if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
  72. oldevent.m_status == SCA_InputEvent::KX_ACTIVE )
  73. {
  74. m_eventStatusTables[m_currentTable][keyevent] = oldevent;
  75. m_eventStatusTables[m_currentTable][keyevent].m_status = SCA_InputEvent::KX_ACTIVE;
  76. }
  77. }
  78. }
  79. /**
  80. * ConvertBlenderEvent translates blender keyboard events into ketsji kbd events
  81. * extra event information is stored, like ramp-mode (just released/pressed)
  82. */
  83. bool KX_BlenderKeyboardDevice::ConvertBlenderEvent(unsigned short incode, short val, unsigned int unicode)
  84. {
  85. bool result = false;
  86. // convert event
  87. KX_EnumInputs kxevent = this->ToNative(incode);
  88. // only process it, if it's a key
  89. if (kxevent >= KX_BEGINKEY && kxevent <= KX_ENDKEY)
  90. {
  91. int previousTable = 1-m_currentTable;
  92. if (val == KM_PRESS || val == KM_DBL_CLICK)
  93. {
  94. if (kxevent == KX_KetsjiEngine::GetExitKey() && val != 0 && !m_hookesc)
  95. result = true;
  96. if (kxevent == KX_PAUSEKEY && val && (IsPressed(KX_LEFTCTRLKEY) || IsPressed(KX_RIGHTCTRLKEY)))
  97. result = true;
  98. // todo: convert val ??
  99. m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
  100. m_eventStatusTables[m_currentTable][kxevent].m_unicode = unicode;
  101. switch (m_eventStatusTables[previousTable][kxevent].m_status)
  102. {
  103. case SCA_InputEvent::KX_JUSTACTIVATED:
  104. {
  105. m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
  106. break;
  107. }
  108. case SCA_InputEvent::KX_ACTIVE:
  109. {
  110. m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
  111. break;
  112. }
  113. case SCA_InputEvent::KX_NO_INPUTSTATUS:
  114. {
  115. m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
  116. break;
  117. }
  118. default:
  119. {
  120. m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
  121. }
  122. }
  123. } else if (val == KM_RELEASE)
  124. {
  125. // blender eventval == 0
  126. switch (m_eventStatusTables[previousTable][kxevent].m_status)
  127. {
  128. case SCA_InputEvent::KX_JUSTACTIVATED:
  129. {
  130. m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
  131. break;
  132. }
  133. case SCA_InputEvent::KX_ACTIVE:
  134. {
  135. m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
  136. break;
  137. }
  138. default:
  139. {
  140. m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
  141. }
  142. }
  143. }
  144. }
  145. return result;
  146. }
  147. void KX_BlenderKeyboardDevice::HookEscape()
  148. {
  149. m_hookesc = true;
  150. }