SDL13.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. GWEN
  3. Copyright (c) 2011 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #ifndef GWEN_INPUT_SDL13_H
  7. #define GWEN_INPUT_SDL13_H
  8. #include "Gwen/InputHandler.h"
  9. #include "Gwen/Gwen.h"
  10. #include "Gwen/Controls/Canvas.h"
  11. #ifdef _WIN32
  12. #define UCS_STRING "UCS-2"
  13. #else
  14. #define UCS_STRING "UCS-4"
  15. #endif
  16. namespace Gwen
  17. {
  18. namespace Input
  19. {
  20. class SDL13
  21. {
  22. public:
  23. SDL13()
  24. {
  25. m_Canvas = NULL;
  26. }
  27. void Initialize( Gwen::Controls::Canvas* c )
  28. {
  29. m_Canvas = c;
  30. }
  31. bool ProcessEvent( SDL_Event* Event )
  32. {
  33. if (!m_Canvas) return false;
  34. switch(Event->type)
  35. {
  36. case SDL_KEYUP:
  37. case SDL_KEYDOWN:
  38. {
  39. SDL_KeyboardEvent* E = &Event->key;
  40. int iKey = -1;
  41. SDL_scancode scancode = E->keysym.scancode;
  42. switch (scancode) {
  43. case SDL_SCANCODE_RETURN: iKey = Gwen::Key::Return; break;
  44. case SDL_SCANCODE_BACKSPACE: iKey = Gwen::Key::Backspace; break;
  45. case SDL_SCANCODE_DELETE: iKey = Gwen::Key::Delete; break;
  46. case SDL_SCANCODE_LEFT: iKey = Gwen::Key::Left; break;
  47. case SDL_SCANCODE_RIGHT: iKey = Gwen::Key::Right; break;
  48. case SDL_SCANCODE_LSHIFT: iKey = Gwen::Key::Shift; break;
  49. case SDL_SCANCODE_RSHIFT: iKey = Gwen::Key::Shift; break;
  50. case SDL_SCANCODE_TAB: iKey = Gwen::Key::Tab; break;
  51. case SDL_SCANCODE_SPACE: iKey = Gwen::Key::Space; break;
  52. case SDL_SCANCODE_HOME: iKey = Gwen::Key::Home; break;
  53. case SDL_SCANCODE_END: iKey = Gwen::Key::End; break;
  54. case SDL_SCANCODE_LCTRL: iKey = Gwen::Key::Control; break;
  55. case SDL_SCANCODE_RCTRL: iKey = Gwen::Key::Control; break;
  56. case SDL_SCANCODE_UP: iKey = Gwen::Key::Up; break;
  57. case SDL_SCANCODE_DOWN: iKey = Gwen::Key::Down; break;
  58. case SDL_SCANCODE_ESCAPE: iKey = Gwen::Key::Escape; break;
  59. case SDL_SCANCODE_LALT: iKey = Gwen::Key::Alt; break;
  60. case SDL_SCANCODE_RALT: iKey = Gwen::Key::Alt; break;
  61. default: return false;
  62. }
  63. return m_Canvas->InputKey(iKey, E->state);
  64. }
  65. case SDL_TEXTINPUT:
  66. {
  67. SDL_TextInputEvent* E = &Event->text;
  68. wchar_t* widechar = (wchar_t*)SDL_iconv_string(UCS_STRING, "UTF-8", E->text, SDL_strlen(E->text)+1);
  69. bool ret = m_Canvas->InputCharacter(*widechar);
  70. SDL_free(widechar);
  71. return ret;
  72. }
  73. case SDL_MOUSEMOTION:
  74. {
  75. SDL_MouseMotionEvent* E = &Event->motion;
  76. return m_Canvas->InputMouseMoved(E->x, E->y, E->xrel, E->yrel);
  77. }
  78. case SDL_MOUSEBUTTONDOWN:
  79. case SDL_MOUSEBUTTONUP:
  80. {
  81. SDL_MouseButtonEvent* E = &Event->button;
  82. int Button = -1;
  83. switch (E->button) {
  84. case SDL_BUTTON_LEFT: Button = 0; break;
  85. case SDL_BUTTON_MIDDLE: Button = 2; break;
  86. case SDL_BUTTON_RIGHT: Button = 1; break;
  87. default: return false;
  88. }
  89. return m_Canvas->InputMouseButton(Button, E->state);
  90. }
  91. case SDL_MOUSEWHEEL:
  92. {
  93. SDL_MouseWheelEvent* E = &Event->wheel;
  94. return m_Canvas->InputMouseWheel(E->y);
  95. }
  96. default:
  97. {
  98. return false;
  99. }
  100. }
  101. }
  102. protected:
  103. Gwen::Controls::Canvas* m_Canvas;
  104. };
  105. }
  106. }
  107. #endif