From 2f2d42e28a14cdc856f8cf0c45cd572646be6750 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 22 Jun 2024 13:23:42 -0700 Subject: Table user input. --- include/ui.h | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 116 insertions(+), 8 deletions(-) (limited to 'include/ui.h') diff --git a/include/ui.h b/include/ui.h index 43bb2e7..8570552 100644 --- a/include/ui.h +++ b/include/ui.h @@ -36,6 +36,9 @@ typedef struct uiPoint { int y; } uiPoint; +/// Widget ID. +typedef int uiWidgetId; + /// Widget type. typedef enum uiWidgetType { uiTypeButton, @@ -52,7 +55,7 @@ typedef struct uiTable uiTable; typedef struct uiWidget uiWidget; /// Widget pointer. -typedef struct uiWidgetPtr { +typedef struct uiPtr { uiWidgetType type; union { uiButton* button; @@ -61,7 +64,77 @@ typedef struct uiWidgetPtr { uiTable* table; uiWidget* widget; }; -} uiWidgetPtr; +} uiPtr; + +/// Mouse button. +typedef enum uiMouseButton { + uiLMB, + uiRMB, + uiMouseButtonMax, +} uiMouseButton; + +/// Mouse button state. +typedef enum uiMouseButtonState { + uiMouseUp, + uiMouseDown, +} uiMouseButtonState; + +/// Mouse button event. +typedef struct uiMouseButtonEvent { + uiMouseButton button; + uiMouseButtonState state; + uiPoint mouse_position; +} uiMouseButtonEvent; + +/// Mouse click event. +typedef struct uiMouseClickEvent { + uiMouseButton button; + uiPoint mouse_position; +} uiMouseClickEvent; + +/// Mouse scroll event. +typedef struct uiMouseScrollEvent { + uiPoint mouse_position; + int scroll_offset; /// Positive = down; negative = up. +} uiMouseScrollEvent; + +/// Input event type. +typedef enum uiInputEventType { + uiEventMouseButton, + uiEventMouseClick, + uiEventMouseScroll, +} uiInputEventType; + +/// Input event. +typedef struct uiInputEvent { + uiInputEventType type; + union { + uiMouseButtonEvent mouse_button; + uiMouseClickEvent mouse_click; + uiMouseScrollEvent mouse_scroll; + }; +} uiInputEvent; + +/// Table click event. +typedef struct uiTableClickEvent { + int col; + int row; +} uiTableClickEvent; + +/// UI event type. +typedef enum uiWidgetEventType { + uiWidgetEventClick, +} uiWidgetEventType; + +/// UI event. +/// These are events from the UI widgets back to the client application. +typedef struct uiWidgetEvent { + uiWidgetEventType type; + uiPtr widget; + union { + uiTableClickEvent table_click; + }; +} uiWidgetEvent; // ----------------------------------------------------------------------------- // Library. @@ -74,15 +147,25 @@ bool uiInit(void); /// This should be called once during application shutdown. void uiShutdown(void); +// ----------------------------------------------------------------------------- +// Widget pointers. + +uiPtr uiMakeButtonPtr(uiButton*); +uiPtr uiMakeFramePtr(uiFrame*); +uiPtr uiMakeLabelPtr(uiLabel*); +uiPtr uiMakeTablePtr(uiTable*); + +uiButton* uiGetButtonPtr(uiPtr ptr); +uiFrame* uiGetFramePtr(uiPtr ptr); +uiLabel* uiGetLabelPtr(uiPtr ptr); +uiTable* uiGetTablePtr(uiPtr ptr); + // ----------------------------------------------------------------------------- // Widget. -uiWidgetPtr uiMakeButtonPtr(uiButton*); -uiWidgetPtr uiMakeFramePtr(uiFrame*); -uiWidgetPtr uiMakeLabelPtr(uiLabel*); -uiWidgetPtr uiMakeTablePtr(uiTable*); +uiWidgetType uiWidgetGetType(const uiWidget*); -void uiWidgetSetParent(uiWidgetPtr child, uiWidgetPtr parent); +void uiWidgetSetParent(uiPtr child, uiPtr parent); // ----------------------------------------------------------------------------- // Button. @@ -111,17 +194,24 @@ uiSize uiGetFrameSize(const uiFrame*); /// Create a label. uiLabel* uiMakeLabel(const char* text); +/// Return the label's text. +const char* uiLabelGetText(const uiLabel*); + // ----------------------------------------------------------------------------- // Table. /// Create a table. uiTable* uiMakeTable(int rows, int cols, const char** header); +/// Clear the table. +/// This clears the contents, but not the header. +void uiTableClear(uiTable*); + /// Add a row. void uiTableAddRow(uiTable*, const char** row); /// Set the table's cell. -void uiTableSet(uiTable*, int row, int col, uiWidgetPtr widget); +void uiTableSet(uiTable*, int row, int col, uiPtr widget); /// Get the table's cell. const uiWidget* uiTableGet(const uiTable*, int row, int col); @@ -134,3 +224,21 @@ uiWidget* uiTableGetMut(uiTable*, int row, int col); /// Render the frame. void uiRender(const uiFrame*, uiSurface*); + +// ----------------------------------------------------------------------------- +// UI Events. + +/// Get the widget events. +/// Return the number of events in the returned array. +/// +/// This function clears the events recorded by the UI library since the last +/// input event. Subsequent calls to this function, with no further user input, +/// therefore report zero widget events. +int uiGetEvents(uiWidgetEvent const**); + +// ----------------------------------------------------------------------------- +// User input. + +/// Send an input event to the UI. +/// Return true if the UI requires a redraw. +bool uiSendEvent(uiFrame*, const uiInputEvent*); -- cgit v1.2.3