Nuklear
Minimal-state, immediate-mode graphical user interface toolkit written in ANSI C.
Loading...
Searching...
No Matches
nuklear_contextual.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ==============================================================
5 *
6 * CONTEXTUAL
7 *
8 * ===============================================================*/
9NK_API nk_bool
10nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
11 struct nk_rect trigger_bounds)
12{
13 struct nk_window *win;
14 struct nk_window *popup;
15 struct nk_rect body;
16 struct nk_input* in;
17
18 NK_STORAGE const struct nk_rect null_rect = {-1,-1,0,0};
19 int is_clicked = 0;
20 int is_open = 0;
21 int ret = 0;
22
23 NK_ASSERT(ctx);
24 NK_ASSERT(ctx->current);
25 NK_ASSERT(ctx->current->layout);
26 if (!ctx || !ctx->current || !ctx->current->layout)
27 return 0;
28
29 win = ctx->current;
30 ++win->popup.con_count;
31 if (ctx->current != ctx->active)
32 return 0;
33
34 /* check if currently active contextual is active */
35 popup = win->popup.win;
36 is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
37 in = win->widgets_disabled ? 0 : &ctx->input;
38 if (in) {
39 is_clicked = nk_input_mouse_clicked(in, NK_BUTTON_RIGHT, trigger_bounds);
40 if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
41 return 0;
42 if (!is_open && win->popup.active_con)
43 win->popup.active_con = 0;
44 if ((!is_open && !is_clicked))
45 return 0;
46
47 /* calculate contextual position on click; keep the click as the
48 * anchor every frame (combo/menu re-fit from the trigger). */
49 win->popup.active_con = win->popup.con_count;
50 {struct nk_rect anchor;
51 if (is_clicked) {
52 anchor.x = in->mouse.pos.x;
53 anchor.y = in->mouse.pos.y;
54 } else {
55 anchor.x = win->popup.header.x;
56 anchor.y = win->popup.header.y;
57 }
58 anchor.w = 1;
59 anchor.h = 1;
60 body.x = anchor.x;
61 body.y = anchor.y;
62 body.w = size.x;
63 body.h = size.y;
64
65 {float known_h = (!is_clicked) ? win->popup.last_h : 0;
66 body = nk_fit_popup_rect(ctx, body, anchor, NK_POPUP_FIT_SLIDE, known_h, nk_false);
67 flags |= NK_WINDOW_NO_SCROLLBAR;
68 /* scrollbar only if actual content does not fit in the display */
69 if (known_h > 0.0f && body.h + 0.5f < known_h)
70 flags &= ~(nk_flags)NK_WINDOW_NO_SCROLLBAR;}
71
72 /* start nonblocking contextual popup */
73 ret = nk_nonblock_begin(ctx, flags, body,
74 null_rect, NK_PANEL_CONTEXTUAL);
75 if (ret) {
76 win->popup.type = NK_PANEL_CONTEXTUAL;
77 win->popup.header = anchor;
78 } else {
79 win->popup.active_con = 0;
80 win->popup.type = NK_PANEL_NONE;
81 if (win->popup.win)
82 win->popup.win->flags = 0;
83 }}
84 }
85 return ret;
86}
87NK_API nk_bool
88nk_contextual_item_text(struct nk_context *ctx, const char *text, int len,
89 nk_flags alignment)
90{
91 struct nk_window *win;
92 const struct nk_input *in;
93 const struct nk_style *style;
94
95 struct nk_rect bounds;
96 enum nk_widget_layout_states state;
97
98 NK_ASSERT(ctx);
99 NK_ASSERT(ctx->current);
100 NK_ASSERT(ctx->current->layout);
101 if (!ctx || !ctx->current || !ctx->current->layout)
102 return 0;
103
104 win = ctx->current;
105 style = &ctx->style;
106 state = nk_widget(&bounds, ctx);
107 if (!state) return nk_false;
108
109 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
110 if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
111 text, len, alignment, NK_BUTTON_DEFAULT, &style->contextual_button, in, style->font)) {
112 nk_contextual_close(ctx);
113 return nk_true;
114 }
115 return nk_false;
116}
117NK_API nk_bool
118nk_contextual_item_label(struct nk_context *ctx, const char *label, nk_flags align)
119{
120 return nk_contextual_item_text(ctx, label, nk_strlen(label), align);
121}
122NK_API nk_bool
123nk_contextual_item_image_text(struct nk_context *ctx, struct nk_image img,
124 const char *text, int len, nk_flags align)
125{
126 struct nk_window *win;
127 const struct nk_input *in;
128 const struct nk_style *style;
129
130 struct nk_rect bounds;
131 enum nk_widget_layout_states state;
132
133 NK_ASSERT(ctx);
134 NK_ASSERT(ctx->current);
135 NK_ASSERT(ctx->current->layout);
136 if (!ctx || !ctx->current || !ctx->current->layout)
137 return 0;
138
139 win = ctx->current;
140 style = &ctx->style;
141 state = nk_widget(&bounds, ctx);
142 if (!state) return nk_false;
143
144 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
145 if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer, bounds,
146 img, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)){
147 nk_contextual_close(ctx);
148 return nk_true;
149 }
150 return nk_false;
151}
152NK_API nk_bool
153nk_contextual_item_image_label(struct nk_context *ctx, struct nk_image img,
154 const char *label, nk_flags align)
155{
156 return nk_contextual_item_image_text(ctx, img, label, nk_strlen(label), align);
157}
158NK_API nk_bool
159nk_contextual_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
160 const char *text, int len, nk_flags align)
161{
162 struct nk_window *win;
163 const struct nk_input *in;
164 const struct nk_style *style;
165
166 struct nk_rect bounds;
167 enum nk_widget_layout_states state;
168
169 NK_ASSERT(ctx);
170 NK_ASSERT(ctx->current);
171 NK_ASSERT(ctx->current->layout);
172 if (!ctx || !ctx->current || !ctx->current->layout)
173 return 0;
174
175 win = ctx->current;
176 style = &ctx->style;
177 state = nk_widget(&bounds, ctx);
178 if (!state) return nk_false;
179
180 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
181 if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
182 symbol, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
183 nk_contextual_close(ctx);
184 return nk_true;
185 }
186 return nk_false;
187}
188NK_API nk_bool
189nk_contextual_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
190 const char *text, nk_flags align)
191{
192 return nk_contextual_item_symbol_text(ctx, symbol, text, nk_strlen(text), align);
193}
194NK_API void
195nk_contextual_close(struct nk_context *ctx)
196{
197 NK_ASSERT(ctx);
198 NK_ASSERT(ctx->current);
199 NK_ASSERT(ctx->current->layout);
200 if (!ctx || !ctx->current || !ctx->current->layout) return;
201 nk_popup_close(ctx);
202}
203NK_API void
204nk_contextual_end(struct nk_context *ctx)
205{
206 struct nk_window *popup;
207 struct nk_panel *panel;
208 NK_ASSERT(ctx);
209 NK_ASSERT(ctx->current);
210 if (!ctx || !ctx->current) return;
211
212 popup = ctx->current;
213 panel = popup->layout;
214 NK_ASSERT(popup->parent);
215 NK_ASSERT((int)panel->type & (int)NK_PANEL_SET_POPUP);
216 if (panel->flags & NK_WINDOW_DYNAMIC) {
217 /* Close behavior
218 This is a bit of a hack solution since we do not know before we end our popup
219 how big it will be. We therefore do not directly know when a
220 click outside the non-blocking popup must close it at that direct frame.
221 Instead it will be closed in the next frame.*/
222 struct nk_rect body = {0,0,0,0};
223 if (panel->at_y < (panel->bounds.y + panel->bounds.h)) {
224 struct nk_vec2 padding = nk_panel_get_padding(&ctx->style, panel->type);
225 body = panel->bounds;
226 body.y = (panel->at_y + panel->footer_height + panel->border + padding.y + panel->row.height);
227 body.h = (panel->bounds.y + panel->bounds.h) - body.y;
228 }
229 {int pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
230 int in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
231 if (pressed && in_body)
232 popup->flags |= NK_WINDOW_HIDDEN;
233 }
234 }
235 if (popup->flags & NK_WINDOW_HIDDEN)
236 popup->seq = 0;
237 nk_popup_end(ctx);
238 return;
239}
240
float last_h
last frame's content height; used to flip without a gap
Definition nuklear.h:5642