forked from vurtun/nuklear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuklear_popup.c
259 lines (235 loc) · 7.43 KB
/
nuklear_popup.c
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
#include "nuklear.h"
#include "nuklear_internal.h"
/* ===============================================================
*
* POPUP
*
* ===============================================================*/
NK_API int
nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
const char *title, nk_flags flags, struct nk_rect rect)
{
struct nk_window *popup;
struct nk_window *win;
struct nk_panel *panel;
int title_len;
nk_hash title_hash;
nk_size allocated;
NK_ASSERT(ctx);
NK_ASSERT(title);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return 0;
win = ctx->current;
panel = win->layout;
NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP) && "popups are not allowed to have popups");
(void)panel;
title_len = (int)nk_strlen(title);
title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_POPUP);
popup = win->popup.win;
if (!popup) {
popup = (struct nk_window*)nk_create_window(ctx);
popup->parent = win;
win->popup.win = popup;
win->popup.active = 0;
win->popup.type = NK_PANEL_POPUP;
}
/* make sure we have correct popup */
if (win->popup.name != title_hash) {
if (!win->popup.active) {
nk_zero(popup, sizeof(*popup));
win->popup.name = title_hash;
win->popup.active = 1;
win->popup.type = NK_PANEL_POPUP;
} else return 0;
}
/* popup position is local to window */
ctx->current = popup;
rect.x += win->layout->clip.x;
rect.y += win->layout->clip.y;
/* setup popup data */
popup->parent = win;
popup->bounds = rect;
popup->seq = ctx->seq;
popup->layout = (struct nk_panel*)nk_create_panel(ctx);
popup->flags = flags;
popup->flags |= NK_WINDOW_BORDER;
if (type == NK_POPUP_DYNAMIC)
popup->flags |= NK_WINDOW_DYNAMIC;
popup->buffer = win->buffer;
nk_start_popup(ctx, win);
allocated = ctx->memory.allocated;
nk_push_scissor(&popup->buffer, nk_null_rect);
if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
/* popup is running therefore invalidate parent panels */
struct nk_panel *root;
root = win->layout;
while (root) {
root->flags |= NK_WINDOW_ROM;
root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
root = root->parent;
}
win->popup.active = 1;
popup->layout->offset_x = &popup->scrollbar.x;
popup->layout->offset_y = &popup->scrollbar.y;
popup->layout->parent = win->layout;
return 1;
} else {
/* popup was closed/is invalid so cleanup */
struct nk_panel *root;
root = win->layout;
while (root) {
root->flags |= NK_WINDOW_REMOVE_ROM;
root = root->parent;
}
win->popup.buf.active = 0;
win->popup.active = 0;
ctx->memory.allocated = allocated;
ctx->current = win;
nk_free_panel(ctx, popup->layout);
popup->layout = 0;
return 0;
}
}
NK_LIB int
nk_nonblock_begin(struct nk_context *ctx,
nk_flags flags, struct nk_rect body, struct nk_rect header,
enum nk_panel_type panel_type)
{
struct nk_window *popup;
struct nk_window *win;
struct nk_panel *panel;
int is_active = nk_true;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return 0;
/* popups cannot have popups */
win = ctx->current;
panel = win->layout;
NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP));
(void)panel;
popup = win->popup.win;
if (!popup) {
/* create window for nonblocking popup */
popup = (struct nk_window*)nk_create_window(ctx);
popup->parent = win;
win->popup.win = popup;
win->popup.type = panel_type;
nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
} else {
/* close the popup if user pressed outside or in the header */
int pressed, in_body, in_header;
pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
if (pressed && (!in_body || in_header))
is_active = nk_false;
}
win->popup.header = header;
if (!is_active) {
/* remove read only mode from all parent panels */
struct nk_panel *root = win->layout;
while (root) {
root->flags |= NK_WINDOW_REMOVE_ROM;
root = root->parent;
}
return is_active;
}
popup->bounds = body;
popup->parent = win;
popup->layout = (struct nk_panel*)nk_create_panel(ctx);
popup->flags = flags;
popup->flags |= NK_WINDOW_BORDER;
popup->flags |= NK_WINDOW_DYNAMIC;
popup->seq = ctx->seq;
win->popup.active = 1;
NK_ASSERT(popup->layout);
nk_start_popup(ctx, win);
popup->buffer = win->buffer;
nk_push_scissor(&popup->buffer, nk_null_rect);
ctx->current = popup;
nk_panel_begin(ctx, 0, panel_type);
win->buffer = popup->buffer;
popup->layout->parent = win->layout;
popup->layout->offset_x = &popup->scrollbar.x;
popup->layout->offset_y = &popup->scrollbar.y;
/* set read only mode to all parent panels */
{struct nk_panel *root;
root = win->layout;
while (root) {
root->flags |= NK_WINDOW_ROM;
root = root->parent;
}}
return is_active;
}
NK_API void
nk_popup_close(struct nk_context *ctx)
{
struct nk_window *popup;
NK_ASSERT(ctx);
if (!ctx || !ctx->current) return;
popup = ctx->current;
NK_ASSERT(popup->parent);
NK_ASSERT(popup->layout->type & NK_PANEL_SET_POPUP);
popup->flags |= NK_WINDOW_HIDDEN;
}
NK_API void
nk_popup_end(struct nk_context *ctx)
{
struct nk_window *win;
struct nk_window *popup;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return;
popup = ctx->current;
if (!popup->parent) return;
win = popup->parent;
if (popup->flags & NK_WINDOW_HIDDEN) {
struct nk_panel *root;
root = win->layout;
while (root) {
root->flags |= NK_WINDOW_REMOVE_ROM;
root = root->parent;
}
win->popup.active = 0;
}
nk_push_scissor(&popup->buffer, nk_null_rect);
nk_end(ctx);
win->buffer = popup->buffer;
nk_finish_popup(ctx, win);
ctx->current = win;
nk_push_scissor(&win->buffer, win->layout->clip);
}
NK_API void
nk_popup_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y)
{
struct nk_window *popup;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return;
popup = ctx->current;
if (offset_x)
*offset_x = popup->scrollbar.x;
if (offset_y)
*offset_y = popup->scrollbar.y;
}
NK_API void
nk_popup_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y)
{
struct nk_window *popup;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return;
popup = ctx->current;
popup->scrollbar.x = offset_x;
popup->scrollbar.y = offset_y;
}