From 4152fbecb6ee8360575aa4c24e9cedf822f159dc Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 25 Mar 2026 19:59:14 -0700 Subject: Implement vertical and horizontal layouts. Use widget position properly when rendering. Toolbar, buttons and edit bars WIP --- include/ui.h | 81 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/ui.h b/include/ui.h index baaa550..b70df59 100644 --- a/include/ui.h +++ b/include/ui.h @@ -42,15 +42,19 @@ typedef int uiWidgetId; /// Widget type. typedef enum uiWidgetType { uiTypeButton, + uiTypeEdit, uiTypeFrame, uiTypeLabel, + uiTypeLayout, uiTypeTable, uiTypeMax, } uiWidgetType; typedef struct uiButton uiButton; +typedef struct uiEdit uiEdit; typedef struct uiFrame uiFrame; typedef struct uiLabel uiLabel; +typedef struct uiLayout uiLayout; typedef struct uiTable uiTable; typedef struct uiWidget uiWidget; @@ -59,13 +63,33 @@ typedef struct uiPtr { uiWidgetType type; union { uiButton* button; + uiEdit* edit; uiFrame* frame; uiLabel* label; + uiLayout* layout; uiTable* table; uiWidget* widget; }; } uiPtr; +/// Direction in which a layout widget lays out its children. +typedef enum uiLayoutDirection { + uiVertical, + uiHorizontal, +} uiLayoutDirection; + +/// Directions in which a widget stretches. +/// +/// Stretch determines how the widget occupies the area of its parent widget. +/// +/// uiStretchNone - the widget has a fixed size. +/// uiStretchX/Y - the widget stretches in the X/Y direction. +typedef enum uiStretch { + uiStretchNone = 0, + uiStretchX = 1, + uiStretchY = 2, +} uiStretch; + /// Mouse button. typedef enum uiMouseButton { uiLMB, @@ -145,6 +169,11 @@ typedef struct uiWidgetEvent { }; } uiWidgetEvent; +/// Common construction parameters for widgets. +typedef struct uiParams { + uiStretch stretch; +} uiParams; + // ----------------------------------------------------------------------------- // Library. @@ -152,63 +181,76 @@ bool uiInit(void); void uiShutdown(void); // ----------------------------------------------------------------------------- -// Widget pointers. +// Widget. uiPtr uiMakeButtonPtr(uiButton*); +uiPtr uiMakeEditPtr(uiEdit*); uiPtr uiMakeFramePtr(uiFrame*); uiPtr uiMakeLabelPtr(uiLabel*); +uiPtr uiMakeLayoutPtr(uiLayout*); uiPtr uiMakeTablePtr(uiTable*); uiPtr uiMakeWidgetPtr(uiWidget*); uiPtr uiNullptr(void); -bool uiIsNullptr(uiPtr ptr); +bool uiIsNullptr(uiPtr); -uiButton* uiGetButtonPtr(uiPtr ptr); -uiFrame* uiGetFramePtr(uiPtr ptr); -uiLabel* uiGetLabelPtr(uiPtr ptr); -uiTable* uiGetTablePtr(uiPtr ptr); - -// ----------------------------------------------------------------------------- -// Widget. +uiButton* uiGetButtonPtr(uiPtr); +uiEdit* uiGetEditPtr(uiPtr); +uiFrame* uiGetFramePtr(uiPtr); +uiLabel* uiGetLabelPtr(uiPtr); +uiLayout* uiGetLayoutPtr(uiPtr); +uiTable* uiGetTablePtr(uiPtr); uiWidgetType uiWidgetGetType(const uiWidget*); -void uiWidgetSetParent(uiPtr child, uiPtr parent); - -// ----------------------------------------------------------------------------- -// Button. -uiButton* uiMakeButton(const char* text); +void uiPrint(uiPtr); // ----------------------------------------------------------------------------- // Frame. uiFrame* uiMakeFrame(void); void uiDestroyFrame(uiFrame**); -void uiResizeFrame(uiFrame*, int width, int height); uiSize uiGetFrameSize(const uiFrame*); +// ----------------------------------------------------------------------------- +// Layout. +uiLayout* uiMakeLayout(uiPtr parent, uiLayoutDirection); + +// ----------------------------------------------------------------------------- +// Button. + +uiButton* uiMakeButton(uiPtr parent, const char* text, const uiParams*); + // ----------------------------------------------------------------------------- // Label. -uiLabel* uiMakeLabel(const char* text); +uiLabel* uiMakeLabel(uiPtr parent, const char* text); const char* uiLabelGetText(const uiLabel*); // ----------------------------------------------------------------------------- // Table. -uiTable* uiMakeTable(int rows, int cols, const char** header); +uiTable* uiMakeTable(uiPtr parent, int rows, int cols, const char** header); void uiTableClear(uiTable*); void uiTableAddRow(uiTable*, const char** row); void uiTableSet(uiTable*, int row, int col, const char* text); const char* uiTableGet(const uiTable*, int row, int col); void uiTableScroll(uiTable*, int row); +// ----------------------------------------------------------------------------- +// Layout. + +/// Lay out the widgets in the frame given the frame's new width and height. +/// +/// This should typically be called whenever the window is resized. +void uiLayOut(uiFrame*, int width, int height); + // ----------------------------------------------------------------------------- // Rendering. void uiRender(const uiFrame*, uiSurface*); // ----------------------------------------------------------------------------- -// UI Events. +// UI and user events. /// Get the widget events. /// Return the number of events in the returned array. @@ -218,9 +260,6 @@ void uiRender(const uiFrame*, uiSurface*); /// 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