forked from vurtun/nuklear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuklear_widget.c
246 lines (222 loc) · 7.16 KB
/
nuklear_widget.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
#include "nuklear.h"
#include "nuklear_internal.h"
/* ===============================================================
*
* WIDGET
*
* ===============================================================*/
NK_API struct nk_rect
nk_widget_bounds(struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return nk_rect(0,0,0,0);
nk_layout_peek(&bounds, ctx);
return bounds;
}
NK_API struct nk_vec2
nk_widget_position(struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return nk_vec2(0,0);
nk_layout_peek(&bounds, ctx);
return nk_vec2(bounds.x, bounds.y);
}
NK_API struct nk_vec2
nk_widget_size(struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return nk_vec2(0,0);
nk_layout_peek(&bounds, ctx);
return nk_vec2(bounds.w, bounds.h);
}
NK_API float
nk_widget_width(struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return 0;
nk_layout_peek(&bounds, ctx);
return bounds.w;
}
NK_API float
nk_widget_height(struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return 0;
nk_layout_peek(&bounds, ctx);
return bounds.h;
}
NK_API int
nk_widget_is_hovered(struct nk_context *ctx)
{
struct nk_rect c, v;
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current || ctx->active != ctx->current)
return 0;
c = ctx->current->layout->clip;
c.x = (float)((int)c.x);
c.y = (float)((int)c.y);
c.w = (float)((int)c.w);
c.h = (float)((int)c.h);
nk_layout_peek(&bounds, ctx);
nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
return 0;
return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
}
NK_API int
nk_widget_is_mouse_clicked(struct nk_context *ctx, enum nk_buttons btn)
{
struct nk_rect c, v;
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current || ctx->active != ctx->current)
return 0;
c = ctx->current->layout->clip;
c.x = (float)((int)c.x);
c.y = (float)((int)c.y);
c.w = (float)((int)c.w);
c.h = (float)((int)c.h);
nk_layout_peek(&bounds, ctx);
nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
return 0;
return nk_input_mouse_clicked(&ctx->input, btn, bounds);
}
NK_API int
nk_widget_has_mouse_click_down(struct nk_context *ctx, enum nk_buttons btn, int down)
{
struct nk_rect c, v;
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current || ctx->active != ctx->current)
return 0;
c = ctx->current->layout->clip;
c.x = (float)((int)c.x);
c.y = (float)((int)c.y);
c.w = (float)((int)c.w);
c.h = (float)((int)c.h);
nk_layout_peek(&bounds, ctx);
nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
return 0;
return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
}
NK_API enum nk_widget_layout_states
nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
{
struct nk_rect c, v;
struct nk_window *win;
struct nk_panel *layout;
const struct nk_input *in;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return NK_WIDGET_INVALID;
/* allocate space and check if the widget needs to be updated and drawn */
nk_panel_alloc_space(bounds, ctx);
win = ctx->current;
layout = win->layout;
in = &ctx->input;
c = layout->clip;
/* if one of these triggers you forgot to add an `if` condition around either
a window, group, popup, combobox or contextual menu `begin` and `end` block.
Example:
if (nk_begin(...) {...} nk_end(...); or
if (nk_group_begin(...) { nk_group_end(...);} */
NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
/* need to convert to int here to remove floating point errors */
bounds->x = (float)((int)bounds->x);
bounds->y = (float)((int)bounds->y);
bounds->w = (float)((int)bounds->w);
bounds->h = (float)((int)bounds->h);
c.x = (float)((int)c.x);
c.y = (float)((int)c.y);
c.w = (float)((int)c.w);
c.h = (float)((int)c.h);
nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
return NK_WIDGET_INVALID;
if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
return NK_WIDGET_ROM;
return NK_WIDGET_VALID;
}
NK_API enum nk_widget_layout_states
nk_widget_fitting(struct nk_rect *bounds, struct nk_context *ctx,
struct nk_vec2 item_padding)
{
/* update the bounds to stand without padding */
struct nk_window *win;
struct nk_style *style;
struct nk_panel *layout;
enum nk_widget_layout_states state;
struct nk_vec2 panel_padding;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return NK_WIDGET_INVALID;
win = ctx->current;
style = &ctx->style;
layout = win->layout;
state = nk_widget(bounds, ctx);
panel_padding = nk_panel_get_padding(style, layout->type);
if (layout->row.index == 1) {
bounds->w += panel_padding.x;
bounds->x -= panel_padding.x;
} else bounds->x -= item_padding.x;
if (layout->row.index == layout->row.columns)
bounds->w += panel_padding.x;
else bounds->w += item_padding.x;
return state;
}
NK_API void
nk_spacing(struct nk_context *ctx, int cols)
{
struct nk_window *win;
struct nk_panel *layout;
struct nk_rect none;
int i, index, rows;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return;
/* spacing over row boundaries */
win = ctx->current;
layout = win->layout;
index = (layout->row.index + cols) % layout->row.columns;
rows = (layout->row.index + cols) / layout->row.columns;
if (rows) {
for (i = 0; i < rows; ++i)
nk_panel_alloc_row(ctx, win);
cols = index;
}
/* non table layout need to allocate space */
if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
layout->row.type != NK_LAYOUT_STATIC_FIXED) {
for (i = 0; i < cols; ++i)
nk_panel_alloc_space(&none, ctx);
} layout->row.index = index;
}