Nuklear
Minimal-state, immediate-mode graphical user interface toolkit written in ANSI C.
Loading...
Searching...
No Matches
nuklear_tree.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * TREE
7 *
8 * ===============================================================*/
9NK_INTERN int
10nk_tree_state_base(struct nk_context *ctx, enum nk_tree_type type,
11 struct nk_image *img, const char *title, enum nk_collapse_states *state)
12{
13 struct nk_window *win;
14 struct nk_panel *layout;
15 const struct nk_style *style;
16 struct nk_command_buffer *out;
17 const struct nk_input *in;
18 const struct nk_style_button *button;
19 enum nk_symbol_type symbol;
20 float row_height;
21
22 struct nk_vec2 item_spacing;
23 struct nk_rect header = {0,0,0,0};
24 struct nk_rect sym = {0,0,0,0};
25 struct nk_text text;
26
27 nk_flags ws = 0;
28 enum nk_widget_layout_states widget_state;
29
30 NK_ASSERT(ctx);
31 NK_ASSERT(ctx->current);
32 NK_ASSERT(ctx->current->layout);
33 if (!ctx || !ctx->current || !ctx->current->layout)
34 return 0;
35
36 /* cache some data */
37 win = ctx->current;
38 layout = win->layout;
39 out = &win->buffer;
40 style = &ctx->style;
41 item_spacing = style->window.spacing;
42
43 /* calculate header bounds and draw background */
44 row_height = style->font->height + 2 * style->tab.padding.y;
45 nk_layout_set_min_row_height(ctx, row_height);
46 nk_layout_row_dynamic(ctx, row_height, 1);
47 nk_layout_reset_min_row_height(ctx);
48
49 widget_state = nk_widget(&header, ctx);
50 if (type == NK_TREE_TAB) {
51 const struct nk_style_item *background = &style->tab.background;
52
53 switch(background->type) {
54 case NK_STYLE_ITEM_IMAGE:
55 nk_draw_image(out, header, &background->data.image, nk_rgb_factor(nk_white, style->tab.color_factor));
56 break;
57 case NK_STYLE_ITEM_NINE_SLICE:
58 nk_draw_nine_slice(out, header, &background->data.slice, nk_rgb_factor(nk_white, style->tab.color_factor));
59 break;
60 case NK_STYLE_ITEM_COLOR:
61 nk_fill_rect(out, header, 0, nk_rgb_factor(style->tab.border_color, style->tab.color_factor));
62 nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
63 style->tab.rounding, nk_rgb_factor(background->data.color, style->tab.color_factor));
64 break;
65 }
66 } else text.background = style->window.background;
67
68 /* update node state */
69 in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
70 in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
71 if (nk_button_behavior(&ws, header, in, NK_BUTTON_DEFAULT))
72 *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;
73
74 /* select correct button style */
75 if (*state == NK_MAXIMIZED) {
76 symbol = style->tab.sym_maximize;
77 if (type == NK_TREE_TAB)
78 button = &style->tab.tab_maximize_button;
79 else button = &style->tab.node_maximize_button;
80 } else {
81 symbol = style->tab.sym_minimize;
82 if (type == NK_TREE_TAB)
83 button = &style->tab.tab_minimize_button;
84 else button = &style->tab.node_minimize_button;
85 }
86
87 {/* draw triangle button */
88 sym.w = sym.h = style->font->height;
89 sym.y = header.y + style->tab.padding.y;
90 sym.x = header.x + style->tab.padding.x;
91 nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT,
92 button, 0, style->font);
93
94 if (img) {
95 /* draw optional image icon */
96 sym.x = sym.x + sym.w + 4 * item_spacing.x;
97 nk_draw_image(&win->buffer, sym, img, nk_white);
98 sym.w = style->font->height + style->tab.spacing.x;}
99 }
100
101 {/* draw label */
102 struct nk_rect label;
103 header.w = NK_MAX(header.w, sym.w + item_spacing.x);
104 label.x = sym.x + sym.w + item_spacing.x;
105 label.y = sym.y;
106 label.w = header.w - (sym.w + item_spacing.y + style->tab.indent);
107 label.h = style->font->height;
108 text.text = nk_rgb_factor(style->tab.text, style->tab.color_factor);
109 text.padding = nk_vec2(0,0);
110 nk_widget_text(out, label, title, nk_strlen(title), &text,
111 NK_TEXT_LEFT, style->font);}
112
113 /* increase x-axis cursor widget position pointer */
114 if (*state == NK_MAXIMIZED) {
115 layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
116 layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
117 layout->bounds.w -= (style->tab.indent + style->window.padding.x);
118 layout->row.tree_depth++;
119 return nk_true;
120 } else return nk_false;
121}
122NK_INTERN int
123nk_tree_base(struct nk_context *ctx, enum nk_tree_type type,
124 struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
125 const char *hash, int len, int line)
126{
127 struct nk_window *win = ctx->current;
128 int title_len = 0;
129 nk_hash tree_hash = 0;
130 nk_uint *state = 0;
131
132 /* retrieve tree state from internal widget state tables */
133 if (!hash) {
134 title_len = (int)nk_strlen(title);
135 tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
136 } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
137 state = nk_find_value(win, tree_hash);
138 if (!state) {
139 state = nk_add_value(ctx, win, tree_hash, 0);
140 NK_ASSERT(state);
141 if (!state) return nk_false;
142 *state = initial_state;
143 }
144 return nk_tree_state_base(ctx, type, img, title, (enum nk_collapse_states*)state);
145}
146NK_API nk_bool
147nk_tree_state_push(struct nk_context *ctx, enum nk_tree_type type,
148 const char *title, enum nk_collapse_states *state)
149{
150 return nk_tree_state_base(ctx, type, 0, title, state);
151}
152NK_API nk_bool
153nk_tree_state_image_push(struct nk_context *ctx, enum nk_tree_type type,
154 struct nk_image img, const char *title, enum nk_collapse_states *state)
155{
156 return nk_tree_state_base(ctx, type, &img, title, state);
157}
158NK_API void
159nk_tree_state_pop(struct nk_context *ctx)
160{
161 struct nk_window *win = 0;
162 struct nk_panel *layout = 0;
163
164 NK_ASSERT(ctx);
165 NK_ASSERT(ctx->current);
166 NK_ASSERT(ctx->current->layout);
167 if (!ctx || !ctx->current || !ctx->current->layout)
168 return;
169
170 win = ctx->current;
171 layout = win->layout;
172 layout->at_x -= ctx->style.tab.indent + (float)*layout->offset_x;
173 layout->bounds.w += ctx->style.tab.indent + ctx->style.window.padding.x;
174 NK_ASSERT(layout->row.tree_depth);
175 layout->row.tree_depth--;
176}
177NK_API nk_bool
178nk_tree_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
179 const char *title, enum nk_collapse_states initial_state,
180 const char *hash, int len, int line)
181{
182 return nk_tree_base(ctx, type, 0, title, initial_state, hash, len, line);
183}
184NK_API nk_bool
185nk_tree_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
186 struct nk_image img, const char *title, enum nk_collapse_states initial_state,
187 const char *hash, int len,int seed)
188{
189 return nk_tree_base(ctx, type, &img, title, initial_state, hash, len, seed);
190}
191NK_API void
192nk_tree_pop(struct nk_context *ctx)
193{
194 nk_tree_state_pop(ctx);
195}
196NK_INTERN int
197nk_tree_element_image_push_hashed_base(struct nk_context *ctx, enum nk_tree_type type,
198 struct nk_image *img, const char *title, int title_len,
199 enum nk_collapse_states *state, nk_bool *selected)
200{
201 struct nk_window *win;
202 struct nk_panel *layout;
203 const struct nk_style *style;
204 struct nk_command_buffer *out;
205 const struct nk_input *in;
206 const struct nk_style_button *button;
207 enum nk_symbol_type symbol;
208 float row_height;
209 struct nk_vec2 padding;
210
211 int text_len;
212 float text_width;
213
214 struct nk_vec2 item_spacing;
215 struct nk_rect header = {0,0,0,0};
216 struct nk_rect sym = {0,0,0,0};
217
218 nk_flags ws = 0;
219 enum nk_widget_layout_states widget_state;
220
221 NK_ASSERT(ctx);
222 NK_ASSERT(ctx->current);
223 NK_ASSERT(ctx->current->layout);
224 if (!ctx || !ctx->current || !ctx->current->layout)
225 return 0;
226
227 /* cache some data */
228 win = ctx->current;
229 layout = win->layout;
230 out = &win->buffer;
231 style = &ctx->style;
232 item_spacing = style->window.spacing;
233 padding = style->selectable.padding;
234
235 /* calculate header bounds and draw background */
236 row_height = style->font->height + 2 * style->tab.padding.y;
237 nk_layout_set_min_row_height(ctx, row_height);
238 nk_layout_row_dynamic(ctx, row_height, 1);
239 nk_layout_reset_min_row_height(ctx);
240
241 widget_state = nk_widget(&header, ctx);
242 if (type == NK_TREE_TAB) {
243 const struct nk_style_item *background = &style->tab.background;
244
245 switch (background->type) {
246 case NK_STYLE_ITEM_IMAGE:
247 nk_draw_image(out, header, &background->data.image, nk_rgb_factor(nk_white, style->tab.color_factor));
248 break;
249 case NK_STYLE_ITEM_NINE_SLICE:
250 nk_draw_nine_slice(out, header, &background->data.slice, nk_rgb_factor(nk_white, style->tab.color_factor));
251 break;
252 case NK_STYLE_ITEM_COLOR:
253 nk_fill_rect(out, header, 0, nk_rgb_factor(style->tab.border_color, style->tab.color_factor));
254 nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
255 style->tab.rounding, nk_rgb_factor(background->data.color, style->tab.color_factor));
256
257 break;
258 }
259 }
260
261 in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
262 in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
263
264 /* select correct button style */
265 if (*state == NK_MAXIMIZED) {
266 symbol = style->tab.sym_maximize;
267 if (type == NK_TREE_TAB)
268 button = &style->tab.tab_maximize_button;
269 else button = &style->tab.node_maximize_button;
270 } else {
271 symbol = style->tab.sym_minimize;
272 if (type == NK_TREE_TAB)
273 button = &style->tab.tab_minimize_button;
274 else button = &style->tab.node_minimize_button;
275 }
276 {/* draw triangle button */
277 sym.w = sym.h = style->font->height;
278 sym.y = header.y + style->tab.padding.y;
279 sym.x = header.x + style->tab.padding.x;
280 if (nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT, button, in, style->font))
281 *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;}
282
283 /* draw label */
284 {nk_flags dummy = 0;
285 struct nk_rect label;
286 /* selectable insets by padding on both sides */
287 text_len = nk_strlen(title);
288 text_width = style->font->width(style->font->userdata, style->font->height, title, text_len);
289 text_width += 2 * padding.x;
290
291 header.w = NK_MAX(header.w, sym.w + item_spacing.x);
292 label.x = sym.x + sym.w + item_spacing.x;
293 label.y = sym.y;
294 label.w = NK_MIN(header.w - (sym.w + item_spacing.y + style->tab.indent), text_width);
295 label.h = style->font->height;
296
297 if (img) {
298 nk_do_selectable_image(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
299 selected, img, &style->selectable, in, style->font);
300 } else nk_do_selectable(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
301 selected, &style->selectable, in, style->font);
302 }
303 /* increase x-axis cursor widget position pointer */
304 if (*state == NK_MAXIMIZED) {
305 layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
306 layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
307 layout->bounds.w -= (style->tab.indent + style->window.padding.x);
308 layout->row.tree_depth++;
309 return nk_true;
310 } else return nk_false;
311}
312NK_INTERN int
313nk_tree_element_base(struct nk_context *ctx, enum nk_tree_type type,
314 struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
315 nk_bool *selected, const char *hash, int len, int line)
316{
317 struct nk_window *win = ctx->current;
318 int title_len = 0;
319 nk_hash tree_hash = 0;
320 nk_uint *state = 0;
321
322 /* retrieve tree state from internal widget state tables */
323 if (!hash) {
324 title_len = (int)nk_strlen(title);
325 tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
326 } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
327 state = nk_find_value(win, tree_hash);
328 if (!state) {
329 state = nk_add_value(ctx, win, tree_hash, 0);
330 NK_ASSERT(state);
331 if (!state) return nk_false;
332 *state = initial_state;
333 } return nk_tree_element_image_push_hashed_base(ctx, type, img, title,
334 nk_strlen(title), (enum nk_collapse_states*)state, selected);
335}
336NK_API nk_bool
337nk_tree_element_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
338 const char *title, enum nk_collapse_states initial_state,
339 nk_bool *selected, const char *hash, int len, int seed)
340{
341 return nk_tree_element_base(ctx, type, 0, title, initial_state, selected, hash, len, seed);
342}
343NK_API nk_bool
344nk_tree_element_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
345 struct nk_image img, const char *title, enum nk_collapse_states initial_state,
346 nk_bool *selected, const char *hash, int len,int seed)
347{
348 return nk_tree_element_base(ctx, type, &img, title, initial_state, selected, hash, len, seed);
349}
350NK_API void
351nk_tree_element_pop(struct nk_context *ctx)
352{
353 nk_tree_state_pop(ctx);
354}
355
nk_text_width_f width
!< max height of the font
Definition nuklear.h:4119
float height
!< user provided font handle
Definition nuklear.h:4118