guiAPI.h 16 KB

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