summaryrefslogtreecommitdiff
path: root/src/gui/floatinput.cpp
blob: 095c5627f35842559a31986bf63a0080fc34f202 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "base.h"
#include <math.h>

const I32 FLOATINPUT_STEPPER_W = 11;

static U8 gui_floatinput_has_stepper( GUI_FLOATINPUT* input ) {
  return input->w >= 46;
}

static I32 gui_floatinput_content_w( GUI_FLOATINPUT* input ) {
  I32 reserve = gui_floatinput_has_stepper( input ) ? ( FLOATINPUT_STEPPER_W + 1 ) : 0;
  I32 cw = input->w - reserve;
  return cw < 1 ? 1 : cw;
}

static void gui_floatinput_clamp_and_snap( GUI_FLOATINPUT* input ) {
  if( isfinite( input->min ) && *input->pval < input->min )
    *input->pval = input->wraparound ? input->max : input->min;
  if( isfinite( input->max ) && *input->pval > input->max )
    *input->pval = input->wraparound ? input->min : input->max;

  F32 step = input->step;
  if( !isfinite( step ) || fabsf( step ) < 0.000001f )
    return;

  F32 rmn = remainderf( *input->pval, step );
  *input->pval -= rmn;
}

static void gui_floatinput_draw_stepper( GUI_FLOATINPUT* input ) {
  if( !gui_floatinput_has_stepper( input ) )
    return;

  I32 x = gui_relx( input );
  I32 y = gui_rely( input );
  I32 w = input->w;
  I32 h = input->h;
  I32 sx = x + w - FLOATINPUT_STEPPER_W;
  I32 sy = y + 1;
  I32 sh = h - 2;
  I32 top_h = sh / 2;
  I32 bot_h = sh - top_h;

  CLR border = gui_is_fg_window( input )? ui_clr.border : ui_clr.border_inactive;
  gui_draw_frect( sx, sy, FLOATINPUT_STEPPER_W, sh, border );
  gui_draw_frect( sx + 1, sy + 1, FLOATINPUT_STEPPER_W - 2, sh - 2, ui_clr.bg_alt );
  gui_draw_line( sx + 1, sy + top_h, sx + FLOATINPUT_STEPPER_W - 1, sy + top_h, border );

  I32 th = 0;
  gui_draw_get_str_bounds( 0, &th, FNT_JPN12, "+" );
  I32 plus_y = sy + ( top_h - th ) / 2;
  I32 minus_y = sy + top_h + ( bot_h - th ) / 2;
  I32 cx = sx + FLOATINPUT_STEPPER_W / 2;
  gui_draw_str( cx, plus_y, ALIGN_C, FNT_JPN12, ui_clr.txt, "+" );
  gui_draw_str( cx, minus_y, ALIGN_C, FNT_JPN12, ui_clr.txt, "-" );
}

U8 gui_floatinput_is_bound_val( GUI_FLOATINPUT* input ) {
  if( input->wraparound )
    return 0;
  if( !isfinite( input->min ) || isnan( input->min ) )
    return 0;
  if( !isfinite( input->max ) || isnan( input->max ) )
    return 0;
  if( input->min > input->max )
    return 0;
  return 1;
}

CLR gui_floatinput_get_progress_clr( GUI_FLOATINPUT* input ) {
  if( input->customclr ) {
    if( gui_is_fg_window( input ) ) return input->bgcol;
    else return CLR::blend( input->bgcol, CLR::BLACK(), 0.333f );
  }

  CLR clr_border = gui_is_fg_window( input )? ui_clr.border : ui_clr.border_inactive;
  return CLR::blend( clr_border, CLR::BLACK(), 0.5f );
}

void gui_floatinput_draw_bound( GUI_FLOATINPUT* input ) {
  I32 x = gui_relx( input );
  I32 y = gui_rely( input );
  I32 w = gui_floatinput_content_w( input );
  I32 h = input->h;

  F32 min = input->min;
  F32 max = input->max;

  F32 val = *input->pval;
  F32 percent = (val - min) / (max - min);
  if( percent > 1.f ) percent = 1.f;
  if( percent < 0.f ) percent = 0.f;

  CLR clr_progress = gui_floatinput_get_progress_clr( input );
  gui_draw_frect( x+1, y+1, (I32)( (w-2) * percent ), h-2, clr_progress );

  gui_draw_push_clip( (x+1) + (I32)( (w-2) * percent ), (y+1), (I32)( (w-2) * (1.f - percent) ), h-2 );
  gui_draw_str( x + 2, y + 2, ALIGN_L, FNT_JPN12, ui_clr.txt, input->name );
  gui_draw_str( x + w - 2, y + 2, ALIGN_R, FNT_JPN12, ui_clr.txt, input->valfmt, val );
  gui_draw_set_clip( (x+1), (y+1), (I32)( (w-2) * percent ), h-2 );
  gui_draw_str( x + 2, y + 2, ALIGN_L, FNT_JPN12, ui_clr.bg_alt, input->name );
  gui_draw_str( x + w - 2, y + 2, ALIGN_R, FNT_JPN12, ui_clr.bg_alt, input->valfmt, val );
  gui_draw_pop_clip();
}

void gui_floatinput_draw_unbound( GUI_FLOATINPUT* input ) {
  I32 x = gui_relx( input );
  I32 y = gui_rely( input );
  I32 w = gui_floatinput_content_w( input );

  F32 val = *input->pval;

  gui_draw_str( x + 2, y + 2, ALIGN_L, FNT_JPN12, ui_clr.txt, input->name );
  if( input->drawval )
    gui_draw_str( x + w - 2, y + 2, ALIGN_R, FNT_JPN12, ui_clr.txt, input->valfmt, val );

  I32 t1w, t2w = 0, t3w;
  gui_draw_get_str_bounds( &t1w, 0, FNT_JPN12, input->name );
  if( input->drawval )
    gui_draw_get_str_bounds( &t2w, 0, FNT_JPN12, input->valfmt, val );
  gui_draw_get_str_bounds( &t3w, 0, FNT_JPN12, "<->" );

  I32 stw = t2w + t3w - 2;
  I32 pos = w/2;
  if( stw > pos )
    pos = stw;

  CLR handleclr = CLR::blend( ui_clr.txt, CLR::BLACK(), .7f );

  if( t1w < w / 2 && t1w + t2w + t3w + 8 < w ) {
    gui_draw_str(
      x + w - pos,
      y + 2,
      ALIGN_C,
      FNT_JPN12,
      handleclr,
      "<->"
    );
  }
  else {
    gui_draw_str( x + w - pos + 8, y, ALIGN_C, FNT_JPN12, handleclr, "-" );
    gui_draw_str( x + w - pos + 8, y + 5, ALIGN_C, FNT_JPN12, handleclr, "+" );
  }
}

void gui_floatinput_draw_fn( void* ptr ) {
  GUI_FLOATINPUT* input = (GUI_FLOATINPUT*)ptr;

  I32 x = gui_relx( input );
  I32 y = gui_rely( input );
  I32 w = input->w;
  I32 h = input->h;

  CLR clr_border = gui_is_fg_window( input )? ui_clr.border : ui_clr.border_inactive;

  gui_draw_frect( x, y, w, h, clr_border );
  gui_draw_frect( x+1, y+1, w-2, h-2, ui_clr.bg_sec );

  if( !gui_floatinput_is_bound_val( input ) )
    gui_floatinput_draw_unbound( input );
  else
    gui_floatinput_draw_bound( input );

  gui_floatinput_draw_stepper( input );
}

void gui_floatinput_input_bound( GUI_FLOATINPUT* input ) {
  if( !gui_mbutton_down( GUI_MBTNLEFT ) )
    return;

  F32 oldval = *input->pval;
  I32 x = gui_relx( input );
  I32 w = gui_floatinput_content_w( input );

  F32 min = input->min;
  F32 max = input->max;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  F32 progress = (F32)(mx - x) / w;
  if( progress < 0.f ) progress = 0.f;
  if( progress > 1.f ) progress = 1.f;

  F32 nval = min + (max - min) * progress;
  F32 rmn = remainderf( nval, input->step );
  *input->pval = nval - rmn;
  if( oldval != *input->pval )
    input->lastchange = *input->pval - oldval;
}

void gui_floatinput_input_unbound( GUI_FLOATINPUT* input ) {
  if( !gui_mbutton_down( GUI_MBTNLEFT ) )
    return;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );

  F32 oldval = *input->pval;
  I32 dx = mx - input->lastmx;
  if( dx )
    *input->pval += dx * input->step;

  F32 min = input->min;
  F32 max = input->max;

  if( isfinite( min ) && *input->pval < min )
    *input->pval = input->wraparound? max : min;
  if( isfinite( max ) && *input->pval > max )
    *input->pval = input->wraparound? min : max;

  F32 rmn = remainderf( *input->pval, input->step );
  *input->pval -= rmn;
  if( oldval != *input->pval )
    input->lastchange = *input->pval - oldval;
}

void gui_floatinput_input_scroll( GUI_FLOATINPUT* input ) {
  U8 scroll = gui_mbutton_down( GUI_MBTNSCROLL );
  gui_capture_scroll();
  F32 oldval = *input->pval;
  F32 nval = oldval;

  if( !scroll )
    return;

  if( scroll == 1 )
    nval += input->step;
  else if( scroll == (U8)-1 )
    nval -= input->step;

  if( isfinite( input->min ) && nval < input->min )
    nval = input->wraparound? input->max : input->min;
  if( isfinite( input->max ) && nval > input->max )
    nval = input->wraparound? input->min : input->max;

  F32 rmn = remainderf( nval, input->step );
  *input->pval = nval - rmn;

  input->lastchange = *input->pval - oldval;
  if( input->cb )
    input->cb( input );
}

void gui_floatinput_input_fn( void* ptr ) {
  GUI_FLOATINPUT* input = (GUI_FLOATINPUT*)ptr;

  I32 m1 = gui_mbutton_down( 0 );
  I32 m2 = gui_mbutton_down( 1 );

  I32 x = gui_relx( input );
  I32 y = gui_rely( input );
  I32 w = input->w;
  I32 h = input->h;

  I32 mx, my;
  gui_cursor_pos( &mx, &my );
  U8 inbounds = mx >= x && mx <= x + w && my >= y && my <= y + h;
  U8 has_stepper = gui_floatinput_has_stepper( input );
  I32 step_x = x + w - FLOATINPUT_STEPPER_W;
  U8 in_stepper = has_stepper && inbounds && mx >= step_x;

  if( inbounds )
    gui_floatinput_input_scroll( input );

  if( in_stepper ) {
    if( !m1 && !m2 ) {
      input->held = 0;
      input->heldoutbounds = 0;
      return;
    }

    if( !input->held && !input->heldoutbounds ) {
      U8 fine = !!m2;
      I32 split_y = y + 1 + ( h - 2 ) / 2;
      U8 inc = my < split_y;
      F32 step = fine ? 0.1f : 1.f;
      F32 val = *input->pval;
      *input->pval += inc ? step : -step;
      gui_floatinput_clamp_and_snap( input );
      input->lastchange = *input->pval - val;
      if( input->cb )
        input->cb( input );
      input->held = 1;
    }
    return;
  }

  if( !input->held && m1 && !inbounds )
    input->heldoutbounds = 1;
  if( !input->heldoutbounds && m1 && inbounds ) {
    if( !input->held ) {
      input->lastmx = mx;
      input->held = 1;
      return;
    }
  }

  if( !m1 ) {
    input->heldoutbounds = 0;
    input->held = 0;
    return;
  }

  if( input->heldoutbounds )
    return;

  F32 oldv = *input->pval;
  if( !gui_floatinput_is_bound_val( input ) )
    gui_floatinput_input_unbound( input );
  else
    gui_floatinput_input_bound( input );
  gui_floatinput_clamp_and_snap( input );

  if( input->cb && oldv != *input->pval ) {
    input->cb( input );
  }

  input->lastmx = mx;
}

struct GUI_FLOATINPUT* gui_floatinput( I32 x, I32 y, I32 w, const char* title, F32* pval, F32 min, F32 max, F32 step, const char* valfmt ) {
  if( !gui_check_target() ) return 0;

  GUI_FLOATINPUT* input = new GUI_FLOATINPUT;
  input->x = x;
  input->y = y;
  input->xbound = input->w = w;
  input->ybound = input->h = 20;
  strcpy( input->name, title );
  input->input_fn = gui_floatinput_input_fn;
  input->draw_fn = gui_floatinput_draw_fn;

  input->lastchange = 0;
  input->cb = 0;
  input->pval = pval;
  input->min = min;
  input->max = max;
  input->step = step;
  input->valfmt = valfmt;
  input->drawval = 1;

  input->wraparound = 0;
  input->customclr = 0;
  input->held = 0;

  GUI_VIEW* parent = gui_get_view();
  parent->children.push( input );
  input->parent = parent;

  return input;
}