BackendWindow.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "BackendWindow.h"
  24. using namespace dsr;
  25. bool BackendWindow::executeEvents() {
  26. bool executedEvent = false;
  27. this->prefetchEvents();
  28. // Execute any resize first
  29. // This makes sure that following events gets a canvas size synchronized with the window size
  30. if (this->requestingResize) {
  31. executedEvent = true;
  32. this->callback_resizeEvent(this->requestedWidth, this->requestedHeight);
  33. this->requestingResize = false;
  34. }
  35. // Look for events
  36. for (int e = 0; e < this->eventQueue.length(); e++) {
  37. InputEvent* event = this->eventQueue[e];
  38. if (event) {
  39. executedEvent = true;
  40. KeyboardEvent* kEvent = dynamic_cast<KeyboardEvent*>(event);
  41. MouseEvent* mEvent = dynamic_cast<MouseEvent*>(event);
  42. WindowEvent* wEvent = dynamic_cast<WindowEvent*>(event);
  43. if (kEvent) {
  44. this->callback_keyboardEvent(*kEvent);
  45. } else if (mEvent) {
  46. this->callback_mouseEvent(*mEvent);
  47. } else if (wEvent) {
  48. if (wEvent->windowEventType == WindowEventType::Close) {
  49. this->callback_closeEvent();
  50. } else if (wEvent->windowEventType == WindowEventType::Redraw) {
  51. this->showCanvas();
  52. }
  53. }
  54. }
  55. delete event;
  56. }
  57. // Check for resize again in case that one was triggered by a callback
  58. if (this->requestingResize) {
  59. this->callback_resizeEvent(this->requestedWidth, this->requestedHeight);
  60. this->requestingResize = false;
  61. }
  62. // Clear the event queue to avoid repeating events
  63. this->eventQueue.clear();
  64. // Tell the caller if we did something
  65. return executedEvent;
  66. }