guiAPI.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // zlib open source license
  2. //
  3. // Copyright (c) 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_API_GUI
  24. #define DFPSR_API_GUI
  25. #include "types.h"
  26. #include "../base/text.h"
  27. #include "../gui/InputEvent.h"
  28. // createBackendWindow should be implemented outside of the core framework
  29. // Choose one of the window backends in SDK/native to compile and link with your application.
  30. // std::shared_ptr<dsr::BackendWindow> createBackendWindow(const dsr::String& title, int width, int height);
  31. // Constness on handles doesn't propagate to any inner types
  32. // "const Comopnent&" only means that the writable Component handle can be created from a sub-expression
  33. // because the location where the handle is stored cannot be overwritten.
  34. // This allow getting a component by name and using it as an argument without being stored in a variable.
  35. namespace dsr {
  36. // Window Construction
  37. // A portable window will be wrapped around a native window backend supplied from a call to createBackendWindow.
  38. Window window_create(const dsr::String& title, int32_t width, int32_t height);
  39. Window window_create_fullscreen(const dsr::String& title);
  40. // Returns true iff the window exists
  41. bool window_exists(const Window& window);
  42. // Layout files
  43. // Loading an interface by parsing a layout file's content.
  44. // Raises an exception if window doesn't exist.
  45. void window_loadInterfaceFromString(const Window& window, const dsr::String& content);
  46. // Loading an interface by parsing a layout file loaded by filename.
  47. // Raises an exception if window doesn't exist.
  48. void window_loadInterfaceFromFile(const Window& window, const dsr::ReadableString& filename);
  49. // Store the interface back into a layout file.
  50. // Raises an exception if window doesn't exist.
  51. String window_saveInterfaceToString(const Window& window);
  52. // Find a component
  53. // Get the component being stored directly in the window
  54. // Raises an exception if window doesn't exist.
  55. // There should always exist a root component where more components can be added recursively
  56. Component window_getRoot(const Window& window);
  57. // TODO: Document
  58. // Raises an exception if window doesn't exist.
  59. Component window_findComponentByName(const Window& window, const ReadableString& name, bool mustExist = true);
  60. // TODO: Document
  61. // Raises an exception if window doesn't exist.
  62. Component window_findComponentByNameAndIndex(const Window& window, const ReadableString& name, int index, bool mustExist = true);
  63. // The three main events to run in a loop at the end of the main function
  64. // If the window's event queue contained any resize of the window, the canvas and the depth buffer will be replaced during this call.
  65. // New calls to window_getCanvas and window_getDepthBuffer are required after this call, because the window could be given a new size.
  66. // Returns true iff any event were processed.
  67. // By calling window_executeEvents in a loop while returning false, one can wait for input.
  68. // Sleeping for 10 milliseconds is quite responsive while saving lots of battery.
  69. // Only redrawing the regions that has changed (dirty rectangles et cetera) can further save power.
  70. // Example:
  71. // while (!window_executeEvents(window)) {
  72. // time_sleepSeconds(0.01);
  73. // }
  74. // window_drawComponents(window);
  75. // window_showCanvas(window);
  76. bool window_executeEvents(const Window& window);
  77. // Draw the root component and its children to the canvas.
  78. // Raises an exception if window doesn't exist.
  79. void window_drawComponents(const Window& window);
  80. // Show the canvas.
  81. // Raises an exception if window doesn't exist.
  82. void window_showCanvas(const Window& window);
  83. // Pixel upscaling
  84. // The pixel-scale is the width and height of each canvas pixel when displayed on the window.
  85. // The color and depth buffers from window_getCanvas and window_getDepthBuffer will shrink to fit each pixel within the window.
  86. // Partial pixels at right and bottom sides are replaced with black padding,
  87. // so that mouse coordinates can be divided and multiplied evenly during scale conversion.
  88. // If using a higher value than the default 1, upscaling will be done during the call to window_showCanvas.
  89. // The backend window will receive the upscaled image to display over the whole window.
  90. // Gets the current pixel scale.
  91. // Raises an exception if window doesn't exist.
  92. int window_getPixelScale(const Window& window);
  93. // Assigns a new pixel scale.
  94. // Raises an exception if window doesn't exist.
  95. // Just like when handling a window resize, this will replace the canvas and depth buffer.
  96. // Any old handles to canvas and depth buffer will become useless, so fetch new image handles from the window to avoid black flickering.
  97. void window_setPixelScale(const Window& window, int scale);
  98. // Full screen
  99. void window_setFullScreen(const Window& window, bool enabled);
  100. bool window_isFullScreen(const Window& window);
  101. // Fetch the window's surfaces
  102. // Always get the canvas (and any depth buffer) after calls to window_executeEvents or window_setPixelScale,
  103. // because these may replace the canvas with a new size.
  104. // TODO: Prevent the backend window from freeing the memory while the canvas is still being used.
  105. // Get the canvas/color-buffer.
  106. // Raises an exception if window doesn't exist.
  107. // The canvas size will be smaller when pixelScale is larger, because the canvas has to fit inside the window.
  108. AlignedImageRgbaU8 window_getCanvas(const Window& window);
  109. // Get the depth buffer allocated on demand.
  110. // Raises an exception if window doesn't exist.
  111. // If you never call this method, no depth buffer will be allocated.
  112. // If you call it at the same time as window_getCanvas, it will have the same size as the canvas.
  113. AlignedImageF32 window_getDepthBuffer(const Window& window);
  114. // The low-resolution canvas and depth buffer dimensions are relative to mouse events given to components.
  115. // Because component are drawn to the canvas and affected by upscaling.
  116. // Returns the width of the canvas.
  117. // Raises an exception if window doesn't exist.
  118. int window_getCanvasWidth(const Window& window);
  119. // Returns the height of the canvas.
  120. // Raises an exception if window doesn't exist.
  121. int window_getCanvasHeight(const Window& window);
  122. // The window's inner dimensions are relative to mouse events received directly from the window at full pixel resolution.
  123. // Returns the inner width of the window.
  124. // Raises an exception if window doesn't exist.
  125. int window_getInnerWidth(const Window& window);
  126. // Returns the inner height of the window.
  127. // Raises an exception if window doesn't exist.
  128. int window_getInnerHeight(const Window& window);
  129. // Direct window events
  130. // Listen to window mouse events
  131. // Raises an exception if window doesn't exist.
  132. // event.mouseEventType gives the type of mouse event
  133. // event.key gives the key being used
  134. void window_setMouseEvent(const Window& window, const MouseCallback& mouseEvent);
  135. // Listen to window keyboard events
  136. // Raises an exception if window doesn't exist.
  137. // event.keyboardEventType gives the type of keyboard event
  138. // event.dsrKey gives the key being used
  139. void window_setKeyboardEvent(const Window& window, const KeyboardCallback& keyboardEvent);
  140. // Listen to the window close event
  141. // Raises an exception if window doesn't exist.
  142. void window_setCloseEvent(const Window& window, const EmptyCallback& closeEvent);
  143. // Components
  144. // Returns true iff the component exists
  145. bool component_exists(const Component& component);
  146. // Returns true iff propertyName exists in component
  147. bool component_hasProperty(const Component& component, const ReadableString& propertyName);
  148. // Sets a property found using propertyName in component to the value serialized in value.
  149. // Raises an exception if component doesn't exist.
  150. // Matching of propertyName is case insensitive.
  151. // Returns ReturnCode::Good if assigned.
  152. // Unless mustAssign forces an exception.
  153. // Returns ReturnCode::KeyNotFound if propertyName wasn't found in component.
  154. // Unless mustAssign forces an exception.
  155. // Returns ReturnCode::ParsingFailure if propertyName was found but value couldn't be converted to its type.
  156. ReturnCode component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign = true);
  157. // A version optimized for basic strings to bypass quote mangling
  158. // The new value is given as it is without unmangling.
  159. ReturnCode component_setProperty_string(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign = true);
  160. // Returns a property found using propertyName in component.
  161. // Raises an exception if component doesn't exist.
  162. // Matching of propertyName is case insensitive.
  163. // If mustExist is true
  164. // Raises an exception when propertyName isn't found.
  165. // If mustExist is false
  166. // Returns an empty string when propertyName isn't found.
  167. String component_getProperty(const Component& component, const ReadableString& propertyName, bool mustExist = true);
  168. // A version optimized for basic strings to bypass quote mangling
  169. // Returns the result without adding any quote signs or escape characters
  170. String component_getProperty_string(const Component& component, const ReadableString& propertyName, bool mustExist = true);
  171. // Call a named method in the component using optional text arguments
  172. String component_call(const Component& component, const ReadableString& methodName);
  173. String component_call(const Component& component, const ReadableString& methodName, const ReadableString& arguments);
  174. // Component events
  175. // The main activation of clickable components.
  176. // The pressed callback doesn't take any arguments, because it should be possible to generate from multiple input methods.
  177. void component_setPressedEvent(const Component& component, const EmptyCallback& event);
  178. // Mouse-down activates when any mouse button is pressed down within the component
  179. // Raises an exception if component doesn't exist.
  180. // The component itself decides if the mouse is inside, which allow rounded components to act as their true shape.
  181. void component_setMouseDownEvent(const Component& component, const MouseCallback& mouseEvent);
  182. // Mouse-up should eventually follow after a mouse-down event, to ensure basic transaction safety.
  183. // Raises an exception if component doesn't exist.
  184. // * Even if the mouse is dragged outside of the component or window before being lifted.
  185. // * Even if the component is removed from the window while the button is pressed,
  186. // the button press will keep it alive long enough to receive the mouse-up event before being freed.
  187. void component_setMouseUpEvent(const Component& component, const MouseCallback& mouseEvent);
  188. // Mouse-move is triggered when the mouse moves over the component.
  189. // Raises an exception if component doesn't exist.
  190. // * When pressed down inside of the component, dragging outside the component or even window will
  191. // continue to give mouse-move events to the callback.
  192. // * If dragging left of or above the window, event.position may contain negative coordinates.
  193. void component_setMouseMoveEvent(const Component& component, const MouseCallback& mouseEvent);
  194. // Mouse-scroll is triggered by scrolling in any direction.
  195. // Raises an exception if component doesn't exist.
  196. // Currently only supporting MouseKeyEnum::ScrollUp and MouseKeyEnum::ScrollDown as values in event.key.
  197. void component_setMouseScrollEvent(const Component& component, const MouseCallback& mouseEvent);
  198. // Key-down only comes when a button is pressed down. (No repeat)
  199. // Raises an exception if component doesn't exist.
  200. // The backend window is responsible to filter away any false positives for down events caused by repetition.
  201. void component_setKeyDownEvent(const Component& component, const KeyboardCallback& keyboardEvent);
  202. // Key-up only comes when a button is lifted after being pressed. (No repeat)
  203. // Raises an exception if component doesn't exist.
  204. void component_setKeyUpEvent(const Component& component, const KeyboardCallback& keyboardEvent);
  205. // Key-type comes both when a key is pressed, and then repeatedly without having to lift the key.
  206. // Raises an exception if component doesn't exist.
  207. // There's usually a second's delay before quickly repeating.
  208. void component_setKeyTypeEvent(const Component& component, const KeyboardCallback& keyboardEvent);
  209. // Select events are sent when the selected index of something has changed.
  210. // Used in Listbox to cover updates from all different ways the selection may change.
  211. void component_setSelectEvent(const Component& component, const IndexCallback& selectEvent);
  212. // Theme
  213. // Apply the given theme recursively to all components in the window's interface.
  214. // Raises an exception if window or component doesn't exist.
  215. // Components will gather what they can from the theme and save it for later.
  216. // Changing a theme while being used by an interface or adding new components,
  217. // should apply the theme again to ensure that all changes are applied.
  218. // TODO: Automate this process by storing a reference to the theme in each component and checking for updates before drawing.
  219. void window_applyTheme(const Window& window, const VisualTheme& theme);
  220. }
  221. #endif