-
Notifications
You must be signed in to change notification settings - Fork 0
/
microui.h
297 lines (267 loc) · 9.63 KB
/
microui.h
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
** Copyright (c) 2020 rxi
** Copyright (c) 2023 ashn <[email protected]>
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the MIT license. See `microui.c` for details.
*/
#ifndef MICROUI_H
#define MICROUI_H
#include <stdbool.h>
#define MU_COMMANDLIST_SIZE (256 * 1024)
#define MU_ROOTLIST_SIZE 32
#define MU_CONTAINERSTACK_SIZE 32
#define MU_CLIPSTACK_SIZE 32
#define MU_IDSTACK_SIZE 32
#define MU_LAYOUTSTACK_SIZE 16
#define MU_CONTAINERPOOL_SIZE 48
#define MU_TREENODEPOOL_SIZE 48
#define MU_MAX_WIDTHS 16
#define MU_REAL float
#define MU_REAL_FMT "%.3g"
#define MU_SLIDER_FMT "%.2f"
#define MU_MAX_FMT 127
#define mu_stack(T, n) struct { int idx; T items[n]; }
#define mu_min(a, b) ((a) < (b) ? (a) : (b))
#define mu_max(a, b) ((a) > (b) ? (a) : (b))
#define mu_clamp(x, a, b) mu_min(b, mu_max(a, x))
enum {
MU_CLIP_PART = 1,
MU_CLIP_ALL
};
enum {
MU_COMMAND_JUMP = 1,
MU_COMMAND_CLIP,
MU_COMMAND_RECT,
MU_COMMAND_TEXT,
MU_COMMAND_ICON,
MU_COMMAND_MAX
};
enum {
MU_COLOR_TEXT,
MU_COLOR_BORDER,
MU_COLOR_WINDOWBG,
MU_COLOR_TITLEBG,
MU_COLOR_TITLETEXT,
MU_COLOR_PANELBG,
MU_COLOR_BUTTON,
MU_COLOR_BUTTONHOVER,
MU_COLOR_BUTTONFOCUS,
MU_COLOR_BASE,
MU_COLOR_BASEHOVER,
MU_COLOR_BASEFOCUS,
MU_COLOR_SCROLLBASE,
MU_COLOR_SCROLLTHUMB,
MU_COLOR_MAX
};
enum {
MU_ICON_CLOSE = 1,
MU_ICON_CHECK,
MU_ICON_COLLAPSED,
MU_ICON_EXPANDED,
MU_ICON_MAX
};
enum {
MU_RES_ACTIVE = (1 << 0),
MU_RES_SUBMIT = (1 << 1),
MU_RES_CHANGE = (1 << 2)
};
enum {
MU_OPT_ALIGNCENTER = (1 << 0),
MU_OPT_ALIGNRIGHT = (1 << 1),
MU_OPT_NOINTERACT = (1 << 2),
MU_OPT_NOFRAME = (1 << 3),
MU_OPT_NORESIZE = (1 << 4),
MU_OPT_NOSCROLL = (1 << 5),
MU_OPT_NOCLOSE = (1 << 6),
MU_OPT_NOTITLE = (1 << 7),
MU_OPT_NODRAG = (1 << 8),
MU_OPT_HOLDFOCUS = (1 << 9),
MU_OPT_AUTOSIZE = (1 << 10),
MU_OPT_POPUP = (1 << 11),
MU_OPT_CLOSED = (1 << 12),
MU_OPT_EXPANDED = (1 << 13)
};
enum {
MU_MOUSE_LEFT = (1 << 0),
MU_MOUSE_RIGHT = (1 << 1),
MU_MOUSE_MIDDLE = (1 << 2)
};
enum {
MU_KEY_SHIFT = (1 << 0),
MU_KEY_CTRL = (1 << 1),
MU_KEY_ALT = (1 << 2),
MU_KEY_BACKSPACE = (1 << 3),
MU_KEY_RETURN = (1 << 4)
};
typedef struct mu_Context mu_Context;
typedef unsigned mu_Id;
typedef MU_REAL mu_Real;
typedef void* mu_Font;
typedef struct { int x, y; } mu_Vec2;
typedef struct { int x, y, w, h; } mu_Rect;
typedef struct { unsigned char r, g, b, a; } mu_Color;
typedef struct { mu_Id id; int last_update; } mu_PoolItem;
typedef struct { int kind, size; } mu_BaseCommand;
typedef struct { mu_BaseCommand base; void* dst; } mu_JumpCommand;
typedef struct { mu_BaseCommand base; mu_Rect rect; } mu_ClipCommand;
typedef struct { mu_BaseCommand base; mu_Rect rect; mu_Color color; } mu_RectCommand;
typedef struct { mu_BaseCommand base; mu_Font font; mu_Vec2 pos; mu_Color color; char str[1]; } mu_TextCommand;
typedef struct { mu_BaseCommand base; mu_Rect rect; int id; mu_Color color; } mu_IconCommand;
typedef union {
int kind;
mu_BaseCommand base;
mu_JumpCommand jump;
mu_ClipCommand clip;
mu_RectCommand rect;
mu_TextCommand text;
mu_IconCommand icon;
} mu_Command;
typedef struct {
mu_Rect body;
mu_Rect next;
mu_Vec2 position;
mu_Vec2 size;
mu_Vec2 max;
int widths[MU_MAX_WIDTHS];
int items;
int item_index;
int next_row;
int next_type;
int indent;
} mu_Layout;
typedef struct {
mu_Command *head, *tail;
mu_Rect rect;
mu_Rect body;
mu_Vec2 content_size;
mu_Vec2 scroll;
int zindex;
int open;
} mu_Container;
typedef struct {
mu_Font font;
mu_Vec2 size;
int padding;
int spacing;
int indent;
int title_height;
int scrollbar_size;
int thumb_size;
mu_Color colors[MU_COLOR_MAX];
} mu_Style;
struct mu_Context {
/* callbacks */
int (*text_width)(mu_Font font, char const* str, int len);
int (*text_height)(mu_Font font);
void (*draw_frame)(mu_Context* ctx, mu_Rect rect, int colorid);
/* core state */
mu_Style _style;
mu_Style* style;
mu_Id hover;
mu_Id focus;
mu_Id last_id;
mu_Rect last_rect;
int last_zindex;
int updated_focus;
int frame;
mu_Container* hover_root;
mu_Container* next_hover_root;
mu_Container* scroll_target;
char number_edit_buf[MU_MAX_FMT];
mu_Id number_edit;
/* stacks */
mu_stack(char, MU_COMMANDLIST_SIZE) command_list;
mu_stack(mu_Container*, MU_ROOTLIST_SIZE) root_list;
mu_stack(mu_Container*, MU_CONTAINERSTACK_SIZE) container_stack;
mu_stack(mu_Rect, MU_CLIPSTACK_SIZE) clip_stack;
mu_stack(mu_Id, MU_IDSTACK_SIZE) id_stack;
mu_stack(mu_Layout, MU_LAYOUTSTACK_SIZE) layout_stack;
/* retained state pools */
mu_PoolItem container_pool[MU_CONTAINERPOOL_SIZE];
mu_Container containers[MU_CONTAINERPOOL_SIZE];
mu_PoolItem treenode_pool[MU_TREENODEPOOL_SIZE];
/* input state */
mu_Vec2 mouse_pos;
mu_Vec2 last_mouse_pos;
mu_Vec2 mouse_delta;
mu_Vec2 scroll_delta;
int mouse_down;
int mouse_pressed;
int key_down;
int key_pressed;
char input_text[32];
};
mu_Vec2 mu_vec2(int x, int y);
mu_Rect mu_rect(int x, int y, int w, int h);
mu_Color mu_color(int r, int g, int b, int a);
void mu_init(mu_Context* ctx);
void mu_begin(mu_Context* ctx);
void mu_end(mu_Context* ctx);
void mu_set_focus(mu_Context* ctx, mu_Id id);
mu_Id mu_get_id(mu_Context* ctx, void const* data, int size);
void mu_push_id(mu_Context* ctx, void const* data, int size);
void mu_pop_id(mu_Context* ctx);
void mu_push_clip_rect(mu_Context* ctx, mu_Rect rect);
void mu_pop_clip_rect(mu_Context* ctx);
mu_Rect mu_get_clip_rect(mu_Context* ctx);
int mu_check_clip(mu_Context* ctx, mu_Rect r);
mu_Container* mu_get_current_container(mu_Context* ctx);
mu_Container* mu_get_container(mu_Context* ctx, char const* name);
void mu_bring_to_front(mu_Context* ctx, mu_Container* cnt);
int mu_pool_init(mu_Context* ctx, mu_PoolItem* items, int len, mu_Id id);
int mu_pool_get(mu_Context* ctx, mu_PoolItem* items, int len, mu_Id id);
void mu_pool_update(mu_Context* ctx, mu_PoolItem* items, int idx);
void mu_input_mousemove(mu_Context* ctx, int x, int y);
void mu_input_mousedown(mu_Context* ctx, int x, int y, int btn);
void mu_input_mouseup(mu_Context* ctx, int x, int y, int btn);
void mu_input_scroll(mu_Context* ctx, int x, int y);
void mu_input_keydown(mu_Context* ctx, int key);
void mu_input_keyup(mu_Context* ctx, int key);
void mu_input_text(mu_Context* ctx, char const* text);
mu_Command* mu_push_command(mu_Context* ctx, int kind, int size);
int mu_next_command(mu_Context* ctx, mu_Command** cmd);
void mu_set_clip(mu_Context* ctx, mu_Rect rect);
void mu_draw_rect(mu_Context* ctx, mu_Rect rect, mu_Color color);
void mu_draw_box(mu_Context* ctx, mu_Rect rect, mu_Color color);
void mu_draw_text(mu_Context* ctx, mu_Font font, char const* str, int len, mu_Vec2 pos, mu_Color color);
void mu_draw_icon(mu_Context* ctx, int id, mu_Rect rect, mu_Color color);
void mu_layout_row(mu_Context* ctx, int items, int const* widths, int height);
void mu_layout_width(mu_Context* ctx, int width);
void mu_layout_height(mu_Context* ctx, int height);
void mu_layout_begin_column(mu_Context* ctx);
void mu_layout_end_column(mu_Context* ctx);
void mu_layout_set_next(mu_Context* ctx, mu_Rect r, int relative);
mu_Rect mu_layout_next(mu_Context* ctx);
void mu_draw_control_frame(mu_Context* ctx, mu_Id id, mu_Rect rect, int colorid, int opt);
void mu_draw_control_text(mu_Context* ctx, char const* str, mu_Rect rect, int colorid, int opt);
int mu_mouse_over(mu_Context* ctx, mu_Rect rect);
void mu_update_control(mu_Context* ctx, mu_Id id, mu_Rect rect, int opt);
void mu_text(mu_Context* ctx, char const* text);
void mu_label(mu_Context* ctx, char const* text);
int mu_button(mu_Context* ctx, char const* label);
int mu_button_ex(mu_Context* ctx, char const* label, int icon, int opt);
int mu_button_raw(mu_Context* ctx, char const* label, int labelsz, int icon, mu_Id id, mu_Rect r, int opt);
int mu_checkbox(mu_Context* ctx, char const* label, bool* state);
int mu_textbox(mu_Context* ctx, char* buf, int bufsz);
int mu_textbox_ex(mu_Context* ctx, char* buf, int bufsz, int opt);
int mu_textbox_raw(mu_Context* ctx, char* buf, int bufsz, mu_Id id, mu_Rect r, int opt);
int mu_slider(mu_Context* ctx, mu_Real* value, mu_Real lo, mu_Real hi);
int mu_slider_ex(mu_Context* ctx, mu_Real* value, mu_Real low, mu_Real high, mu_Real step, char const* fmt, int opt);
int mu_number(mu_Context* ctx, mu_Real* value, mu_Real step);
int mu_number_ex(mu_Context* ctx, mu_Real* value, mu_Real step, char const* fmt, int opt);
int mu_header(mu_Context* ctx, char const* label);
int mu_header_ex(mu_Context* ctx, char const* label, int opt);
int mu_begin_treenode(mu_Context* ctx, char const* label);
int mu_begin_treenode_ex(mu_Context* ctx, char const* label, int opt);
void mu_end_treenode(mu_Context* ctx);
int mu_begin_window(mu_Context* ctx, char const* title, mu_Rect rect);
int mu_begin_window_ex(mu_Context* ctx, char const* title, mu_Rect rect, int opt);
void mu_end_window(mu_Context* ctx);
void mu_open_popup(mu_Context* ctx, char const* name);
int mu_begin_popup(mu_Context* ctx, char const* name);
void mu_end_popup(mu_Context* ctx);
void mu_begin_panel(mu_Context* ctx, char const* name);
void mu_begin_panel_ex(mu_Context* ctx, char const* name, int opt);
void mu_end_panel(mu_Context* ctx);
#endif