2
0

BackendWindow.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifndef DFPSR_BACKEND_WINDOW
  24. #define DFPSR_BACKEND_WINDOW
  25. #include <cstdint>
  26. #include <memory>
  27. #include "InputEvent.h"
  28. #include "../../api/imageAPI.h"
  29. #include "../../api/stringAPI.h"
  30. #include "../../collection/List.h"
  31. namespace dsr {
  32. // The class to use when porting the window manager to another operating system.
  33. // A simple interface for the most basic operations that a window can do.
  34. // * Show an image over the whole window
  35. // * Take input events
  36. // Minimalism reduces the cost of porting core functionality to new operating systems.
  37. // All other features should be optional.
  38. class BackendWindow {
  39. public:
  40. String title;
  41. // Events
  42. List<InputEvent*> eventQueue;
  43. private:
  44. int requestingResize = false;
  45. int requestedWidth = 0;
  46. int requestedHeight = 0;
  47. public:
  48. inline void receivedMouseEvent(MouseEventType mouseEventType, MouseKeyEnum key, IVector2D position) {
  49. this->eventQueue.push(new MouseEvent(mouseEventType, key, position));
  50. }
  51. inline void receivedKeyboardEvent(KeyboardEventType keyboardEventType, DsrChar character, DsrKey dsrKey) {
  52. this->eventQueue.push(new KeyboardEvent(keyboardEventType, character, dsrKey));
  53. }
  54. inline void receivedWindowCloseEvent() {
  55. this->eventQueue.push(new dsr::WindowEvent(dsr::WindowEventType::Close));
  56. }
  57. inline void receivedWindowRedrawEvent() {
  58. this->eventQueue.push(new dsr::WindowEvent(dsr::WindowEventType::Redraw));
  59. }
  60. // Request to resize the window.
  61. // When the implementation receives a resize, call receiveWindowResize with the new dimensions.
  62. // If requestingResize is already true, it will just overwrite the old request.
  63. // Next call to executeEvents will then use it to resize the canvas.
  64. inline void receivedWindowResize(int width, int height) {
  65. this->requestingResize = true;
  66. this->requestedWidth = width;
  67. this->requestedHeight = height;
  68. }
  69. public:
  70. BackendWindow() {}
  71. virtual ~BackendWindow() {}
  72. virtual void setFullScreen(bool enabled) = 0;
  73. virtual bool isFullScreen() = 0;
  74. virtual int getWidth() const = 0;
  75. virtual int getHeight() const = 0;
  76. public:
  77. // Back-end interface
  78. // Responsible for adding events to eventQueue
  79. virtual void prefetchEvents() = 0;
  80. public:
  81. // Canvas interface
  82. virtual AlignedImageRgbaU8 getCanvas() = 0;
  83. virtual void showCanvas() = 0;
  84. virtual void resizeCanvas(int width, int height) = 0;
  85. virtual String getTitle() { return this->title; }
  86. virtual void setTitle(const String &newTitle) = 0;
  87. public:
  88. // Cursor interface
  89. bool visibleCursor = true; // Written to by setCursorVisibility on success.
  90. virtual bool setCursorVisibility(bool visible) { return false; } // Returns true on success.
  91. virtual void setCursorPosition(int x, int y) {} // Does nothing unless implemented.
  92. public:
  93. // Clipboard interface
  94. // If none is replaced, both default implementations will use an internal variable.
  95. // If both are implemented, the system's clipboard should be accessed.
  96. // Partial implementations with only loadFromClipboard or saveToClipboard are not allowed.
  97. // Load from the clipboard, waiting at most timeoutInMilliseconds milliseconds.
  98. virtual ReadableString loadFromClipboard(double timeoutInSeconds = 0.5);
  99. // Save text to the clipboard.
  100. virtual void saveToClipboard(const ReadableString &text, double timeoutInSeconds = 0.5);
  101. public:
  102. // Each callback declaration has a public variable and a public getter and setter
  103. DECLARE_CALLBACK(closeEvent, emptyCallback);
  104. DECLARE_CALLBACK(resizeEvent, sizeCallback);
  105. DECLARE_CALLBACK(keyboardEvent, keyboardCallback);
  106. DECLARE_CALLBACK(mouseEvent, mouseCallback);
  107. // Call executeEvents to run all callbacks collected in eventQueue
  108. // Returns true if any event was processed
  109. bool executeEvents();
  110. };
  111. }
  112. #endif