guiAPI.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "../base/Handle.h"
  27. #include "../api/stringAPI.h"
  28. #include "../image/Image.h"
  29. #include "../gui/InputEvent.h"
  30. #include "../gui/VisualTheme.h"
  31. // createBackendWindow should be implemented outside of the core framework
  32. // Choose one of the window backends in SDK/native to compile and link with your application.
  33. // Handle<dsr::BackendWindow> createBackendWindow(const dsr::String& title, int width, int height);
  34. // Constness on handles doesn't propagate to any inner types
  35. // "const Comopnent&" only means that the writable Component handle can be created from a sub-expression
  36. // because the location where the handle is stored cannot be overwritten.
  37. // This allow getting a component by name and using it as an argument without being stored in a variable.
  38. namespace dsr {
  39. enum class ReturnCode {
  40. Good,
  41. KeyNotFound,
  42. ParsingFailure
  43. };
  44. // A handle to a window.
  45. // The Window wraps itself around native window backends to abstract away platform specific details.
  46. // It also makes it easy to load and use a graphical interface using the optional component system.
  47. class DsrWindow;
  48. using Window = Handle<DsrWindow>;
  49. // A handle to a GUI component.
  50. // Components are an abstraction for graphical user interfaces, which might not always be powerful enough.
  51. // * If you're making something advanced that components cannot do,
  52. // you can also use draw calls and input events directly against the window without using Component.
  53. class VisualComponent;
  54. using Component = Handle<VisualComponent>;
  55. // Window Construction
  56. // A portable window will be wrapped around a native window backend supplied from a call to createBackendWindow.
  57. Window window_create(const dsr::String& title, int32_t width, int32_t height);
  58. // If the game starts in full screen, this constructor should be used instead.
  59. // Otherwise the canvas may ask for the window's dimensions while the system still keeps the old dimensions due to delays.
  60. Window window_create_fullscreen(const dsr::String& title);
  61. // Returns true iff the window exists.
  62. bool window_exists(const Window& window);
  63. // Set window title
  64. // Assigns the window's title.
  65. void window_setTitle(Window& window, const dsr::String& title);
  66. // Returns the window's title.
  67. String window_getTitle(Window& window);
  68. // Clipboard
  69. // Most operating systems require that a window is associated with each request to access the clipboard.
  70. // This allow anti-virus software to see the user consent for accessing the clipboard,
  71. // because a key combination commonly associated with pasting text was pressed before the request was made.
  72. // Returns content from an internal or extenal clipboard.
  73. // The internal clipboard within the application is used when the system specific backend has not implemented clipboard access.
  74. String window_loadFromClipboard(Window& window, double timeoutInSeconds);
  75. // Stores the text argument to an internal or extenal clipboard.
  76. // The internal clipboard within the application is used when the system specific backend has not implemented clipboard access.
  77. void window_saveToClipboard(Window& window, const ReadableString &text, double timeoutInSeconds);
  78. // Layout files
  79. // Loading an interface by parsing a layout file's content, with any external resources loaded relative to fromPath.
  80. // Embedded images do not count as external resources, but file paths need fromPath in order to know from where they will be loaded.
  81. // Raises an exception if window doesn't exist.
  82. void window_loadInterfaceFromString(const Window& window, const dsr::String& content, const ReadableString &fromPath);
  83. // Loading an interface by parsing a layout file's content, with any external resources loaded relative to the current directory.
  84. // Useful when your application has already assigned the current directory, or when all resources are embedded into the layout.
  85. // Raises an exception if window doesn't exist.
  86. void window_loadInterfaceFromString(const Window& window, const dsr::String& content);
  87. // Loading an interface by parsing a layout file loaded by filename.
  88. // Raises an exception if window doesn't exist.
  89. void window_loadInterfaceFromFile(const Window& window, const dsr::ReadableString& filename);
  90. // Store the interface back into a layout file.
  91. // Raises an exception if window doesn't exist.
  92. String window_saveInterfaceToString(const Window& window);
  93. // Find a component
  94. // Get the component being stored directly in the window
  95. // Raises an exception if window doesn't exist.
  96. // There should always exist a root component where more components can be added recursively
  97. Component window_getRoot(const Window& window);
  98. // Returns a handle to the first matching component of the name in window.
  99. // For consistent behavior, make sure that only one component has the name, or use window_findComponentByNameAndIndex after giving each a unique index.
  100. // If mustExist is true, not finding any component with the name throws an exception.
  101. // Raises an exception if window doesn't exist.
  102. // Component names are case sensitive to reduce the risk of accidental naming conflicts among many components.
  103. Component window_findComponentByName(const Window& window, const ReadableString& name, bool mustExist = true);
  104. // Returns a handle to the first matching component of the name and index in window.
  105. // If mustExist is true, not finding any component with the name throws an exception.
  106. // Raises an exception if window doesn't exist.
  107. // Component names are case sensitive to reduce the risk of accidental naming conflicts among many components.
  108. Component window_findComponentByNameAndIndex(const Window& window, const ReadableString& name, int index, bool mustExist = true);
  109. // Calls back with the component handle and index for each match of the name.
  110. // Can be used to count the number of components by incrementing a counter for each match.
  111. // Can be used to detach all the matching components and have them automatically garbage collected by reference counting.
  112. // The index can be used as a filter or to look up information.
  113. // To allow detaching components while iterating over the list of children, order is reversed for child components.
  114. // Raises an exception if window doesn't exist.
  115. // Component names are case sensitive to reduce the risk of accidental naming conflicts among many components.
  116. void window_findAllComponentsByName(const Window& window, const ReadableString& name, std::function<void(Component component, int index)> callback);
  117. // The three main events to run in a loop at the end of the main function
  118. // If the window's event queue contained any resize of the window, the canvas and the depth buffer will be replaced during this call.
  119. // New calls to window_getCanvas and window_getDepthBuffer are required after this call, because the window could be given a new size.
  120. // Returns true iff any event were processed.
  121. // By calling window_executeEvents in a loop while returning false, one can wait for input.
  122. // Sleeping for 10 milliseconds is quite responsive while saving lots of battery.
  123. // Only redrawing the regions that has changed (dirty rectangles et cetera) can further save power.
  124. // Example:
  125. // while (!window_executeEvents(window)) {
  126. // time_sleepSeconds(0.01);
  127. // }
  128. // window_drawComponents(window);
  129. // window_showCanvas(window);
  130. bool window_executeEvents(const Window& window);
  131. // Draw the root component and its children to the canvas.
  132. // Raises an exception if window doesn't exist.
  133. void window_drawComponents(const Window& window);
  134. // Show the canvas.
  135. // Raises an exception if window doesn't exist.
  136. void window_showCanvas(const Window& window);
  137. // Pixel upscaling
  138. // The pixel-scale is the width and height of each canvas pixel when displayed on the window.
  139. // The color and depth buffers from window_getCanvas and window_getDepthBuffer will shrink to fit each pixel within the window.
  140. // Partial pixels at right and bottom sides are replaced with black padding,
  141. // so that mouse coordinates can be divided and multiplied evenly during scale conversion.
  142. // If using a higher value than the default 1, upscaling will be done during the call to window_showCanvas.
  143. // The backend window will receive the upscaled image to display over the whole window.
  144. // Gets the current pixel scale.
  145. // Raises an exception if window doesn't exist.
  146. int window_getPixelScale(const Window& window);
  147. // Assigns a new pixel scale.
  148. // Raises an exception if window doesn't exist.
  149. // Just like when handling a window resize, this will replace the canvas and depth buffer.
  150. // Any old handles to canvas and depth buffer will become useless, so fetch new image handles from the window to avoid black flickering.
  151. void window_setPixelScale(const Window& window, int scale);
  152. // Cursor
  153. // Sets the cursor visibility for window, hiding if visible is false and showing if visible is true.
  154. // Returns true on success and false on failure.
  155. bool window_setCursorVisibility(const Window& window, bool visible);
  156. // Returns true iff the cursor is allowed to be displayed over window.
  157. bool window_getCursorVisibility(const Window& window);
  158. // Tries move the cursor so that its active point is at (x, y) within window.
  159. // To get back the new cursor location and see if it worked, you must wait for the triggered mouse move event.
  160. // It is impossible to know if the operation succeeded while returning, due to absolute position input devices overwriting the side-effect.
  161. // It is recommended to avoid this method if you can, but some games require capturing relative mouse movements.
  162. // If you use this then make sure to test it carefully on all targeted operating systems and both relative and absolute input devices.
  163. // Pre-conditions:
  164. // 0 <= x < width
  165. // 0 <= y < height
  166. // Moving the mouse outside of the window's region may cause undefined behavior between platforms.
  167. // WARNING!
  168. // Only set the cursor location if you know for sure that the cursor is controlled using relative input devices, such as a mouse or track-pad.
  169. // If an absolute input method is used, such as a stylus-pen, touch-screen or eye-tracker, the cursor will shake quickly.
  170. // The solution is to let the user select between modes for relative and absolute inputs.
  171. // You can for example let absolute input mode move the cursor freely within a dead zone and hover over edges
  172. // to rotate the camera itself, when pattern detection noticed a stylus pen being used instead of a mouse.
  173. // WARNING!
  174. // On MS-Windows the cursor will always move, but on X11 the cursor must be within the window's region.
  175. // Only use this in full-screen mode to prevent getting stuck outside of the window.
  176. // WARNING!
  177. // Due to potential race-conditions, you can not assume that the next mouse move event is generated by this call.
  178. void window_setCursorPosition(const Window& window, int x, int y);
  179. // Full screen
  180. void window_setFullScreen(const Window& window, bool enabled);
  181. bool window_isFullScreen(const Window& window);
  182. // Fetch the window's surfaces
  183. // Always get the canvas (and any depth buffer) after calls to window_executeEvents or window_setPixelScale,
  184. // because these may replace the canvas with a new size.
  185. // TODO: Prevent the backend window from freeing the memory while the canvas is still being used.
  186. // Get the canvas/color-buffer.
  187. // Raises an exception if window doesn't exist.
  188. // The canvas size will be smaller when pixelScale is larger, because the canvas has to fit inside the window.
  189. AlignedImageRgbaU8 window_getCanvas(const Window& window);
  190. // Get the depth buffer allocated on demand.
  191. // Raises an exception if window doesn't exist.
  192. // If you never call this method, no depth buffer will be allocated.
  193. // If you call it at the same time as window_getCanvas, it will have the same size as the canvas.
  194. AlignedImageF32 window_getDepthBuffer(const Window& window);
  195. // The low-resolution canvas and depth buffer dimensions are relative to mouse events given to components.
  196. // Because component are drawn to the canvas and affected by upscaling.
  197. // Returns the width of the canvas.
  198. // Raises an exception if window doesn't exist.
  199. int window_getCanvasWidth(const Window& window);
  200. // Returns the height of the canvas.
  201. // Raises an exception if window doesn't exist.
  202. int window_getCanvasHeight(const Window& window);
  203. // The window's inner dimensions are relative to mouse events received directly from the window at full pixel resolution.
  204. // Returns the inner width of the window.
  205. // Raises an exception if window doesn't exist.
  206. int window_getInnerWidth(const Window& window);
  207. // Returns the inner height of the window.
  208. // Raises an exception if window doesn't exist.
  209. int window_getInnerHeight(const Window& window);
  210. // Direct window events
  211. // Listen to window mouse events.
  212. // Raises an exception if window doesn't exist.
  213. // event.mouseEventType gives the type of mouse event.
  214. // event.key gives the key being used.
  215. void window_setMouseEvent(const Window& window, const MouseCallback& mouseEvent);
  216. // Listen to window keyboard events.
  217. // Raises an exception if window doesn't exist.
  218. // event.keyboardEventType gives the type of keyboard event.
  219. // event.dsrKey gives the key being used.
  220. void window_setKeyboardEvent(const Window& window, const KeyboardCallback& keyboardEvent);
  221. // Listen to the window close event.
  222. // Raises an exception if window doesn't exist.
  223. void window_setCloseEvent(const Window& window, const EmptyCallback& closeEvent);
  224. // Components
  225. // Create a new component belonging to parent.
  226. // Returns a handle to the component or an empty handle if className have not been registered.
  227. // Can use component_exists on the result to check if the operation was a success.
  228. // className must be the name of a registered persistent class inheriting from VisualComponent.
  229. // identifierName is used to find the component.
  230. // index can be used to identify multiple components with the same name without having to generate numbered names.
  231. Component component_create(const Component& parent, const ReadableString& className, const ReadableString& identifierName, int index = 0);
  232. // An alternative to window_loadInterfaceFromString when you want the interface loaded into a panel instead of the whole window.
  233. // Useful for dynamically creating and destroying interfaces to save memory and load the application faster.
  234. // Loading an interface by parsing a layout file's content, with any external resources loaded relative to fromPath.
  235. // Embedded images do not count as external resources, but file paths need fromPath in order to know from where they will be loaded.
  236. // Raises an exception if parent doesn't exist.
  237. Component component_createWithInterfaceFromString(Component& parent, const String& content, const ReadableString &fromPath);
  238. // With fromPath implicitly being the current path.
  239. Component component_createWithInterfaceFromString(Component& parent, const String& content);
  240. // Loading the interface from a file and loading resources relative to filename's parent folder.
  241. Component component_createWithInterfaceFromFile(Component& parent, const String& filename);
  242. // Returns true iff the component exists.
  243. bool component_exists(const Component& component);
  244. // Removed the component from the parent.
  245. // Does nothing if used against the root component.
  246. // Make sure to erase any other references to the component if you want it erased, including parent pointers in any child components that may still be attached unless you detach them first.
  247. // There is currently no attach function, because such a function would need runtime checks against cyclic dependencies from attaching a parent to its own child, and still be hard to debug once it happens.
  248. void component_detachFromParent(const Component& component);
  249. // Returns the number of direct (non-recursive) child components attached to parent, or -1 if parent is a null handle.
  250. int component_getChildCount(const Component& parent);
  251. // Returns the child at childIndex from parent, or an empty handle if parent does not exist or childIndex is out of bound.
  252. // Child indices go from 0 to count - 1.
  253. // The child index refers to the parent's list of children, not the child's index attribute.
  254. Component component_getChild(const Component& parent, int childIndex);
  255. // Returns a handle to the first matching component of the name in parent recursively.
  256. // Can be used instead of window_findComponentByName to further reduce the risk of name collisions, by only looking for recursive child components of a specific parent component.
  257. // If mustExist is true, not finding any component with the name within parent throws an exception.
  258. // Raises an exception if parent doesn't exist.
  259. // Component names are case sensitive to reduce the risk of accidental naming conflicts among many components.
  260. Component component_findChildByName(const Component& parent, const ReadableString& name, bool mustExist = true);
  261. // Returns a handle to the first matching component of the name and index in parent recursively.
  262. // If mustExist is true, not finding any component with the name within parent throws an exception.
  263. // Raises an exception if parent doesn't exist.
  264. // Component names are case sensitive to reduce the risk of accidental naming conflicts among many components.
  265. Component component_findChildByNameAndIndex(const Component& parent, const ReadableString& name, int index, bool mustExist = true);
  266. // Returns true iff propertyName exists in component.
  267. // Property names are case insensitive, to give more flexibility for the few property names.
  268. bool component_hasProperty(const Component& component, const ReadableString& propertyName);
  269. // Sets a property found using propertyName in component to the value serialized in value.
  270. // Raises an exception if component doesn't exist.
  271. // Matching of propertyName is case insensitive, to give more flexibility for the few property names.
  272. // Returns ReturnCode::Good if assigned.
  273. // Unless mustAssign forces an exception.
  274. // Returns ReturnCode::KeyNotFound if propertyName wasn't found in component.
  275. // Unless mustAssign forces an exception.
  276. // Returns ReturnCode::ParsingFailure if propertyName was found but value couldn't be converted to its type.
  277. ReturnCode component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign = true);
  278. // An advanced overload allowing loading of resources from a specific path using the fromPath argument.
  279. ReturnCode component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, const ReadableString& fromPath, bool mustAssign = true);
  280. // A version for setting integers and booleans.
  281. // For integers:
  282. // Just set value to whatever you want assigned directly.
  283. // This is faster than using component_setProperty by not parsing the value from any string.
  284. // For booleans:
  285. // Use 1 (or another non-zero value) for true and 0 for false.
  286. // Boolean properties will convery any non-zero values into ones.
  287. ReturnCode component_setProperty_integer(const Component& component, const ReadableString& propertyName, int64_t value, bool mustAssign = true);
  288. // A version for setting images.
  289. ReturnCode component_setProperty_image(const Component& component, const ReadableString& propertyName, const OrderedImageRgbaU8& value, bool mustAssign = true);
  290. // A version optimized for basic strings to bypass quote mangling
  291. // The new value is given as it is without unmangling.
  292. ReturnCode component_setProperty_string(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign = true);
  293. // Returns a property found using propertyName in component.
  294. // Raises an exception if component doesn't exist.
  295. // Matching of propertyName is case insensitive, to give more flexibility for the few property names.
  296. // If mustExist is true
  297. // Raises an exception when propertyName isn't found.
  298. // If mustExist is false
  299. // Returns an empty string when propertyName isn't found.
  300. String component_getProperty(const Component& component, const ReadableString& propertyName, bool mustExist = true);
  301. // A version for getting integers and booleans.
  302. // Returns defaultValue on failure, which should use a value that is never actually used.
  303. int64_t component_getProperty_integer(const Component& component, const ReadableString& propertyName, bool mustExist = true, int64_t defaultValue = 0);
  304. // A version for getting images
  305. OrderedImageRgbaU8 component_getProperty_image(const Component& component, const ReadableString& propertyName, bool mustExist = true);
  306. // A version optimized for basic strings to bypass quote mangling.
  307. // Returns the result without adding any quote signs or escape characters.
  308. String component_getProperty_string(const Component& component, const ReadableString& propertyName, bool mustExist = true);
  309. // Call a named method in the component using optional text arguments.
  310. // Matching of methodName is case insensitive, to give more flexibility for the few method names.
  311. String component_call(const Component& component, const ReadableString& methodName);
  312. String component_call(const Component& component, const ReadableString& methodName, const ReadableString& arguments);
  313. // Component events
  314. // The main activation of clickable components.
  315. // The pressed callback doesn't take any arguments, because it should be possible to generate from multiple input methods.
  316. void component_setPressedEvent(const Component& component, const EmptyCallback& event);
  317. // Called before the component and it's child components are destructed.
  318. // The event is called when there are no more handles to component.
  319. // Be careful not to forget any extra handles to the component if a memory leak would significantly change the behavior.
  320. void component_setDestroyEvent(const Component& component, const EmptyCallback& event);
  321. // Mouse-down activates when any mouse button is pressed down within the component
  322. // Raises an exception if component doesn't exist.
  323. // The component itself decides if the mouse is inside, which allow rounded components to act as their true shape.
  324. void component_setMouseDownEvent(const Component& component, const MouseCallback& mouseEvent);
  325. // Mouse-up should eventually follow after a mouse-down event, to ensure basic transaction safety.
  326. // Raises an exception if component doesn't exist.
  327. // * Even if the mouse is dragged outside of the component or window before being lifted.
  328. // * Even if the component is removed from the window while the button is pressed,
  329. // the button press will keep it alive long enough to receive the mouse-up event before being freed.
  330. void component_setMouseUpEvent(const Component& component, const MouseCallback& mouseEvent);
  331. // Mouse-move is triggered when the mouse moves over the component.
  332. // Raises an exception if component doesn't exist.
  333. // * When pressed down inside of the component, dragging outside the component or even window will
  334. // continue to give mouse-move events to the callback.
  335. // * If dragging left of or above the window, event.position may contain negative coordinates.
  336. void component_setMouseMoveEvent(const Component& component, const MouseCallback& mouseEvent);
  337. // Mouse-scroll is triggered by scrolling in any direction.
  338. // Raises an exception if component doesn't exist.
  339. // Currently only supporting MouseKeyEnum::ScrollUp and MouseKeyEnum::ScrollDown as values in event.key.
  340. void component_setMouseScrollEvent(const Component& component, const MouseCallback& mouseEvent);
  341. // Key-down only comes when a button is pressed down. (No repeat)
  342. // Raises an exception if component doesn't exist.
  343. // The backend window is responsible to filter away any false positives for down events caused by repetition.
  344. void component_setKeyDownEvent(const Component& component, const KeyboardCallback& keyboardEvent);
  345. // Key-up only comes when a button is lifted after being pressed. (No repeat)
  346. // Raises an exception if component doesn't exist.
  347. void component_setKeyUpEvent(const Component& component, const KeyboardCallback& keyboardEvent);
  348. // Key-type comes both when a key is pressed, and then repeatedly without having to lift the key.
  349. // Raises an exception if component doesn't exist.
  350. // There's usually a second's delay before quickly repeating.
  351. void component_setKeyTypeEvent(const Component& component, const KeyboardCallback& keyboardEvent);
  352. // Select events are sent when the selected index of something has changed.
  353. // Used in Listbox to cover updates from all different ways the selection may change.
  354. void component_setSelectEvent(const Component& component, const IndexCallback& selectEvent);
  355. // Theme
  356. // Apply the given theme recursively to all components in the window's interface.
  357. // Raises an exception if window or component doesn't exist.
  358. // Components will gather what they can from the theme and save it for later.
  359. // Changing a theme while being used by an interface or adding new components,
  360. // should apply the theme again to ensure that all changes are applied.
  361. // TODO: Automate this process by storing a reference to the theme in each component and checking for updates before drawing.
  362. void window_applyTheme(const Window& window, const VisualTheme& theme);
  363. }
  364. #endif