WSScreen.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "WSScreen.h"
  2. #include "WSEventLoop.h"
  3. #include "WSEvent.h"
  4. #include "WSWindowManager.h"
  5. #include <AK/Assertions.h>
  6. static WSScreen* s_the;
  7. void WSScreen::initialize()
  8. {
  9. s_the = nullptr;
  10. }
  11. WSScreen& WSScreen::the()
  12. {
  13. ASSERT(s_the);
  14. return *s_the;
  15. }
  16. WSScreen::WSScreen(unsigned width, unsigned height)
  17. : m_width(width)
  18. , m_height(height)
  19. {
  20. ASSERT(!s_the);
  21. s_the = this;
  22. m_cursor_location = rect().center();
  23. }
  24. WSScreen::~WSScreen()
  25. {
  26. }
  27. void WSScreen::on_receive_mouse_data(int dx, int dy, bool left_button, bool right_button)
  28. {
  29. auto prev_location = m_cursor_location;
  30. m_cursor_location.moveBy(dx, dy);
  31. m_cursor_location.constrain(rect());
  32. if (m_cursor_location.x() >= width())
  33. m_cursor_location.setX(width() - 1);
  34. if (m_cursor_location.y() >= height())
  35. m_cursor_location.setY(height() - 1);
  36. if (m_cursor_location != prev_location) {
  37. auto event = make<MouseEvent>(WSEvent::MouseMove, m_cursor_location.x(), m_cursor_location.y());
  38. WSEventLoop::the().post_event(&WSWindowManager::the(), move(event));
  39. }
  40. bool prev_left_button = m_left_mouse_button_pressed;
  41. bool prev_right_button = m_right_mouse_button_pressed;
  42. m_left_mouse_button_pressed = left_button;
  43. m_right_mouse_button_pressed = right_button;
  44. if (prev_left_button != left_button) {
  45. auto event = make<MouseEvent>(left_button ? WSEvent::MouseDown : WSEvent::MouseUp, m_cursor_location.x(), m_cursor_location.y(), MouseButton::Left);
  46. WSEventLoop::the().post_event(&WSWindowManager::the(), move(event));
  47. }
  48. if (prev_right_button != right_button) {
  49. auto event = make<MouseEvent>(right_button ? WSEvent::MouseDown : WSEvent::MouseUp, m_cursor_location.x(), m_cursor_location.y(), MouseButton::Right);
  50. WSEventLoop::the().post_event(&WSWindowManager::the(), move(event));
  51. }
  52. if (m_cursor_location != prev_location || prev_left_button != left_button)
  53. WSWindowManager::the().draw_cursor();
  54. }
  55. void WSScreen::on_receive_keyboard_data(Keyboard::Key key)
  56. {
  57. auto event = make<KeyEvent>(WSEvent::KeyDown, 0);
  58. int key_code = 0;
  59. switch (key.character) {
  60. case 8: key_code = KeyboardKey::Backspace; break;
  61. case 10: key_code = KeyboardKey::Return; break;
  62. }
  63. event->m_key = key_code;
  64. if (key.character) {
  65. char buf[] = { 0, 0 };
  66. char& ch = buf[0];
  67. ch = key.character;
  68. if (key.shift()) {
  69. if (ch >= 'a' && ch <= 'z') {
  70. ch &= ~0x20;
  71. } else {
  72. switch (ch) {
  73. case '1': ch = '!'; break;
  74. case '2': ch = '@'; break;
  75. case '3': ch = '#'; break;
  76. case '4': ch = '$'; break;
  77. case '5': ch = '%'; break;
  78. case '6': ch = '^'; break;
  79. case '7': ch = '&'; break;
  80. case '8': ch = '*'; break;
  81. case '9': ch = '('; break;
  82. case '0': ch = ')'; break;
  83. case '-': ch = '_'; break;
  84. case '=': ch = '+'; break;
  85. case '`': ch = '~'; break;
  86. case ',': ch = '<'; break;
  87. case '.': ch = '>'; break;
  88. case '/': ch = '?'; break;
  89. case '[': ch = '{'; break;
  90. case ']': ch = '}'; break;
  91. case '\\': ch = '|'; break;
  92. case '\'': ch = '"'; break;
  93. case ';': ch = ':'; break;
  94. }
  95. }
  96. }
  97. event->m_text = buf;
  98. }
  99. event->m_shift = key.shift();
  100. event->m_ctrl = key.ctrl();
  101. event->m_alt = key.alt();
  102. WSEventLoop::the().post_event(&WSWindowManager::the(), move(event));
  103. }