summaryrefslogtreecommitdiff
path: root/include/ui.h
blob: 8570552ae5f81ca12759fa208995cc29de5038ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#pragma once

#include <stdbool.h>
#include <stdint.h>

typedef uint8_t uiChannel;

typedef struct uiPixel {
  uiChannel r, g, b, a;
} uiPixel;

typedef struct uiSize {
  int width;
  int height;
} uiSize;

/// A UI surface to which widgets are rendered.
typedef struct uiSurface {
  int      width;
  int      height;
  uiPixel* pixels;
} uiSurface;

/// Rectangle.
/// (x,y) is the top-left corner.
typedef struct uiRect {
  int x;
  int y;
  int width;
  int height;
} uiRect;

/// Point.
typedef struct uiPoint {
  int x;
  int y;
} uiPoint;

/// Widget ID.
typedef int uiWidgetId;

/// Widget type.
typedef enum uiWidgetType {
  uiTypeButton,
  uiTypeFrame,
  uiTypeLabel,
  uiTypeTable,
  uiTypeMax,
} uiWidgetType;

typedef struct uiButton uiButton;
typedef struct uiFrame  uiFrame;
typedef struct uiLabel  uiLabel;
typedef struct uiTable  uiTable;
typedef struct uiWidget uiWidget;

/// Widget pointer.
typedef struct uiPtr {
  uiWidgetType type;
  union {
    uiButton* button;
    uiFrame*  frame;
    uiLabel*  label;
    uiTable*  table;
    uiWidget* widget;
  };
} 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.

/// Initialize the UI library.
/// This should be called once during application startup.
bool uiInit(void);

/// Shutdown the UI library.
/// 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.

uiWidgetType uiWidgetGetType(const uiWidget*);

void uiWidgetSetParent(uiPtr child, uiPtr parent);

// -----------------------------------------------------------------------------
// Button.

/// Create a button.
uiButton* uiMakeButton(const char* text);

// -----------------------------------------------------------------------------
// Frame.

/// Create a frame.
uiFrame* uiMakeFrame(void);

/// Destroy the frame.
void uiDestroyFrame(uiFrame**);

/// Resize the frame.
void uiResizeFrame(uiFrame*, int width, int height);

/// Get the frame's dimensions.
uiSize uiGetFrameSize(const uiFrame*);

// -----------------------------------------------------------------------------
// Label.

/// 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, uiPtr widget);

/// Get the table's cell.
const uiWidget* uiTableGet(const uiTable*, int row, int col);

/// Get the table's cell.
uiWidget* uiTableGetMut(uiTable*, int row, int col);

// -----------------------------------------------------------------------------
// Rendering.

/// 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*);