microui.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. ** Copyright (c) 2024 rxi
  3. **
  4. ** This library is free software; you can redistribute it and/or modify it
  5. ** under the terms of the MIT license. See `microui.c` for details.
  6. */
  7. #ifndef MICROUI_H
  8. #define MICROUI_H
  9. #define MU_VERSION "2.02"
  10. #define MU_COMMANDLIST_SIZE (256 * 1024)
  11. #define MU_ROOTLIST_SIZE 32
  12. #define MU_CONTAINERSTACK_SIZE 32
  13. #define MU_CLIPSTACK_SIZE 32
  14. #define MU_IDSTACK_SIZE 32
  15. #define MU_LAYOUTSTACK_SIZE 16
  16. #define MU_CONTAINERPOOL_SIZE 48
  17. #define MU_TREENODEPOOL_SIZE 48
  18. #define MU_MAX_WIDTHS 16
  19. #define MU_REAL float
  20. #define MU_REAL_FMT "%.3g"
  21. #define MU_SLIDER_FMT "%.2f"
  22. #define MU_MAX_FMT 127
  23. #define mu_stack(T, n) struct { int idx; T items[n]; }
  24. #define mu_min(a, b) ((a) < (b) ? (a) : (b))
  25. #define mu_max(a, b) ((a) > (b) ? (a) : (b))
  26. #define mu_clamp(x, a, b) mu_min(b, mu_max(a, x))
  27. enum {
  28. MU_CLIP_PART = 1,
  29. MU_CLIP_ALL
  30. };
  31. enum {
  32. MU_COMMAND_JUMP = 1,
  33. MU_COMMAND_CLIP,
  34. MU_COMMAND_RECT,
  35. MU_COMMAND_TEXT,
  36. MU_COMMAND_ICON,
  37. MU_COMMAND_MAX
  38. };
  39. enum {
  40. MU_COLOR_TEXT,
  41. MU_COLOR_BORDER,
  42. MU_COLOR_WINDOWBG,
  43. MU_COLOR_TITLEBG,
  44. MU_COLOR_TITLETEXT,
  45. MU_COLOR_PANELBG,
  46. MU_COLOR_BUTTON,
  47. MU_COLOR_BUTTONHOVER,
  48. MU_COLOR_BUTTONFOCUS,
  49. MU_COLOR_BASE,
  50. MU_COLOR_BASEHOVER,
  51. MU_COLOR_BASEFOCUS,
  52. MU_COLOR_SCROLLBASE,
  53. MU_COLOR_SCROLLTHUMB,
  54. MU_COLOR_MAX
  55. };
  56. enum {
  57. MU_ICON_CLOSE = 1,
  58. MU_ICON_CHECK,
  59. MU_ICON_COLLAPSED,
  60. MU_ICON_EXPANDED,
  61. MU_ICON_MAX
  62. };
  63. enum {
  64. MU_RES_ACTIVE = (1 << 0),
  65. MU_RES_SUBMIT = (1 << 1),
  66. MU_RES_CHANGE = (1 << 2)
  67. };
  68. enum {
  69. MU_OPT_ALIGNCENTER = (1 << 0),
  70. MU_OPT_ALIGNRIGHT = (1 << 1),
  71. MU_OPT_NOINTERACT = (1 << 2),
  72. MU_OPT_NOFRAME = (1 << 3),
  73. MU_OPT_NORESIZE = (1 << 4),
  74. MU_OPT_NOSCROLL = (1 << 5),
  75. MU_OPT_NOCLOSE = (1 << 6),
  76. MU_OPT_NOTITLE = (1 << 7),
  77. MU_OPT_HOLDFOCUS = (1 << 8),
  78. MU_OPT_AUTOSIZE = (1 << 9),
  79. MU_OPT_POPUP = (1 << 10),
  80. MU_OPT_CLOSED = (1 << 11),
  81. MU_OPT_EXPANDED = (1 << 12)
  82. };
  83. enum {
  84. MU_MOUSE_LEFT = (1 << 0),
  85. MU_MOUSE_RIGHT = (1 << 1),
  86. MU_MOUSE_MIDDLE = (1 << 2)
  87. };
  88. enum {
  89. MU_KEY_SHIFT = (1 << 0),
  90. MU_KEY_CTRL = (1 << 1),
  91. MU_KEY_ALT = (1 << 2),
  92. MU_KEY_BACKSPACE = (1 << 3),
  93. MU_KEY_RETURN = (1 << 4)
  94. };
  95. typedef struct mu_Context mu_Context;
  96. typedef unsigned mu_Id;
  97. typedef MU_REAL mu_Real;
  98. typedef void* mu_Font;
  99. typedef struct { int x, y; } mu_Vec2;
  100. typedef struct { int x, y, w, h; } mu_Rect;
  101. typedef struct { unsigned char r, g, b, a; } mu_Color;
  102. typedef struct { mu_Id id; int last_update; } mu_PoolItem;
  103. typedef struct { int type, size; } mu_BaseCommand;
  104. typedef struct { mu_BaseCommand base; void *dst; } mu_JumpCommand;
  105. typedef struct { mu_BaseCommand base; mu_Rect rect; } mu_ClipCommand;
  106. typedef struct { mu_BaseCommand base; mu_Rect rect; mu_Color color; } mu_RectCommand;
  107. typedef struct { mu_BaseCommand base; mu_Font font; mu_Vec2 pos; mu_Color color; char str[1]; } mu_TextCommand;
  108. typedef struct { mu_BaseCommand base; mu_Rect rect; int id; mu_Color color; } mu_IconCommand;
  109. typedef union {
  110. int type;
  111. mu_BaseCommand base;
  112. mu_JumpCommand jump;
  113. mu_ClipCommand clip;
  114. mu_RectCommand rect;
  115. mu_TextCommand text;
  116. mu_IconCommand icon;
  117. } mu_Command;
  118. typedef struct {
  119. mu_Rect body;
  120. mu_Rect next;
  121. mu_Vec2 position;
  122. mu_Vec2 size;
  123. mu_Vec2 max;
  124. int widths[MU_MAX_WIDTHS];
  125. int items;
  126. int item_index;
  127. int next_row;
  128. int next_type;
  129. int indent;
  130. } mu_Layout;
  131. typedef struct {
  132. mu_Command *head, *tail;
  133. mu_Rect rect;
  134. mu_Rect body;
  135. mu_Vec2 content_size;
  136. mu_Vec2 scroll;
  137. int zindex;
  138. int open;
  139. } mu_Container;
  140. typedef struct {
  141. mu_Font font;
  142. mu_Vec2 size;
  143. int padding;
  144. int spacing;
  145. int indent;
  146. int title_height;
  147. int scrollbar_size;
  148. int thumb_size;
  149. mu_Color colors[MU_COLOR_MAX];
  150. } mu_Style;
  151. struct mu_Context {
  152. /* callbacks */
  153. int (*text_width)(mu_Font font, const char *str, int len);
  154. int (*text_height)(mu_Font font);
  155. void (*draw_frame)(mu_Context *ctx, mu_Rect rect, int colorid);
  156. /* core state */
  157. mu_Style _style;
  158. mu_Style *style;
  159. mu_Id hover;
  160. mu_Id focus;
  161. mu_Id last_id;
  162. mu_Rect last_rect;
  163. int last_zindex;
  164. int updated_focus;
  165. int frame;
  166. mu_Container *hover_root;
  167. mu_Container *next_hover_root;
  168. mu_Container *scroll_target;
  169. char number_edit_buf[MU_MAX_FMT];
  170. mu_Id number_edit;
  171. /* stacks */
  172. mu_stack(char, MU_COMMANDLIST_SIZE) command_list;
  173. mu_stack(mu_Container*, MU_ROOTLIST_SIZE) root_list;
  174. mu_stack(mu_Container*, MU_CONTAINERSTACK_SIZE) container_stack;
  175. mu_stack(mu_Rect, MU_CLIPSTACK_SIZE) clip_stack;
  176. mu_stack(mu_Id, MU_IDSTACK_SIZE) id_stack;
  177. mu_stack(mu_Layout, MU_LAYOUTSTACK_SIZE) layout_stack;
  178. /* retained state pools */
  179. mu_PoolItem container_pool[MU_CONTAINERPOOL_SIZE];
  180. mu_Container containers[MU_CONTAINERPOOL_SIZE];
  181. mu_PoolItem treenode_pool[MU_TREENODEPOOL_SIZE];
  182. /* input state */
  183. mu_Vec2 mouse_pos;
  184. mu_Vec2 last_mouse_pos;
  185. mu_Vec2 mouse_delta;
  186. mu_Vec2 scroll_delta;
  187. int mouse_down;
  188. int mouse_pressed;
  189. int key_down;
  190. int key_pressed;
  191. char input_text[32];
  192. };
  193. mu_Vec2 mu_vec2(int x, int y);
  194. mu_Rect mu_rect(int x, int y, int w, int h);
  195. mu_Color mu_color(int r, int g, int b, int a);
  196. void mu_init(mu_Context *ctx);
  197. void mu_begin(mu_Context *ctx);
  198. void mu_end(mu_Context *ctx);
  199. void mu_set_focus(mu_Context *ctx, mu_Id id);
  200. mu_Id mu_get_id(mu_Context *ctx, const void *data, int size);
  201. void mu_push_id(mu_Context *ctx, const void *data, int size);
  202. void mu_pop_id(mu_Context *ctx);
  203. void mu_push_clip_rect(mu_Context *ctx, mu_Rect rect);
  204. void mu_pop_clip_rect(mu_Context *ctx);
  205. mu_Rect mu_get_clip_rect(mu_Context *ctx);
  206. int mu_check_clip(mu_Context *ctx, mu_Rect r);
  207. mu_Container* mu_get_current_container(mu_Context *ctx);
  208. mu_Container* mu_get_container(mu_Context *ctx, const char *name);
  209. void mu_bring_to_front(mu_Context *ctx, mu_Container *cnt);
  210. int mu_pool_init(mu_Context *ctx, mu_PoolItem *items, int len, mu_Id id);
  211. int mu_pool_get(mu_Context *ctx, mu_PoolItem *items, int len, mu_Id id);
  212. void mu_pool_update(mu_Context *ctx, mu_PoolItem *items, int idx);
  213. void mu_input_mousemove(mu_Context *ctx, int x, int y);
  214. void mu_input_mousedown(mu_Context *ctx, int x, int y, int btn);
  215. void mu_input_mouseup(mu_Context *ctx, int x, int y, int btn);
  216. void mu_input_scroll(mu_Context *ctx, int x, int y);
  217. void mu_input_keydown(mu_Context *ctx, int key);
  218. void mu_input_keyup(mu_Context *ctx, int key);
  219. void mu_input_text(mu_Context *ctx, const char *text);
  220. mu_Command* mu_push_command(mu_Context *ctx, int type, int size);
  221. int mu_next_command(mu_Context *ctx, mu_Command **cmd);
  222. void mu_set_clip(mu_Context *ctx, mu_Rect rect);
  223. void mu_draw_rect(mu_Context *ctx, mu_Rect rect, mu_Color color);
  224. void mu_draw_box(mu_Context *ctx, mu_Rect rect, mu_Color color);
  225. void mu_draw_text(mu_Context *ctx, mu_Font font, const char *str, int len, mu_Vec2 pos, mu_Color color);
  226. void mu_draw_icon(mu_Context *ctx, int id, mu_Rect rect, mu_Color color);
  227. void mu_layout_row(mu_Context *ctx, int items, const int *widths, int height);
  228. void mu_layout_width(mu_Context *ctx, int width);
  229. void mu_layout_height(mu_Context *ctx, int height);
  230. void mu_layout_begin_column(mu_Context *ctx);
  231. void mu_layout_end_column(mu_Context *ctx);
  232. void mu_layout_set_next(mu_Context *ctx, mu_Rect r, int relative);
  233. mu_Rect mu_layout_next(mu_Context *ctx);
  234. void mu_draw_control_frame(mu_Context *ctx, mu_Id id, mu_Rect rect, int colorid, int opt);
  235. void mu_draw_control_text(mu_Context *ctx, const char *str, mu_Rect rect, int colorid, int opt);
  236. int mu_mouse_over(mu_Context *ctx, mu_Rect rect);
  237. void mu_update_control(mu_Context *ctx, mu_Id id, mu_Rect rect, int opt);
  238. #define mu_button(ctx, label) mu_button_ex(ctx, label, 0, MU_OPT_ALIGNCENTER)
  239. #define mu_textbox(ctx, buf, bufsz) mu_textbox_ex(ctx, buf, bufsz, 0)
  240. #define mu_slider(ctx, value, lo, hi) mu_slider_ex(ctx, value, lo, hi, 0, MU_SLIDER_FMT, MU_OPT_ALIGNCENTER)
  241. #define mu_number(ctx, value, step) mu_number_ex(ctx, value, step, MU_SLIDER_FMT, MU_OPT_ALIGNCENTER)
  242. #define mu_header(ctx, label) mu_header_ex(ctx, label, 0)
  243. #define mu_begin_treenode(ctx, label) mu_begin_treenode_ex(ctx, label, 0)
  244. #define mu_begin_window(ctx, title, rect) mu_begin_window_ex(ctx, title, rect, 0)
  245. #define mu_begin_panel(ctx, name) mu_begin_panel_ex(ctx, name, 0)
  246. void mu_text(mu_Context *ctx, const char *text);
  247. void mu_label(mu_Context *ctx, const char *text);
  248. int mu_button_ex(mu_Context *ctx, const char *label, int icon, int opt);
  249. int mu_checkbox(mu_Context *ctx, const char *label, int *state);
  250. int mu_textbox_raw(mu_Context *ctx, char *buf, int bufsz, mu_Id id, mu_Rect r, int opt);
  251. int mu_textbox_ex(mu_Context *ctx, char *buf, int bufsz, int opt);
  252. int mu_slider_ex(mu_Context *ctx, mu_Real *value, mu_Real low, mu_Real high, mu_Real step, const char *fmt, int opt);
  253. int mu_number_ex(mu_Context *ctx, mu_Real *value, mu_Real step, const char *fmt, int opt);
  254. int mu_header_ex(mu_Context *ctx, const char *label, int opt);
  255. int mu_begin_treenode_ex(mu_Context *ctx, const char *label, int opt);
  256. void mu_end_treenode(mu_Context *ctx);
  257. int mu_begin_window_ex(mu_Context *ctx, const char *title, mu_Rect rect, int opt);
  258. void mu_end_window(mu_Context *ctx);
  259. void mu_open_popup(mu_Context *ctx, const char *name);
  260. int mu_begin_popup(mu_Context *ctx, const char *name);
  261. void mu_end_popup(mu_Context *ctx);
  262. void mu_begin_panel_ex(mu_Context *ctx, const char *name, int opt);
  263. void mu_end_panel(mu_Context *ctx);
  264. #endif