WSScreen.cpp 3.8 KB

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