File: hud.cpp

package info (click to toggle)
opensubdiv 3.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,132 kB
  • sloc: cpp: 137,820; python: 1,069; objc: 412; javascript: 361; lisp: 216; ansic: 170; makefile: 24
file content (606 lines) | stat: -rw-r--r-- 18,197 bytes parent folder | download | duplicates (3)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//
//   Copyright 2013 Pixar
//
//   Licensed under the Apache License, Version 2.0 (the "Apache License")
//   with the following modification; you may not use this file except in
//   compliance with the Apache License and the following modification to it:
//   Section 6. Trademarks. is deleted and replaced with:
//
//   6. Trademarks. This License does not grant permission to use the trade
//      names, trademarks, service marks, or product names of the Licensor
//      and its affiliates, except as required to comply with Section 4(c) of
//      the License and to reproduce the content of the NOTICE file.
//
//   You may obtain a copy of the Apache License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the Apache License with the above modification is
//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//   KIND, either express or implied. See the Apache License for the specific
//   language governing permissions and limitations under the Apache License.
//

#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cassert>
#include "hud.h"
#include "font_image.h"

#if _MSC_VER
#define snprintf _snprintf
#endif

Hud::Hud() : _visible(true), _windowWidth(0), _windowHeight(0),
             _framebufferWidth(0), _framebufferHeight(0),
             _requiresRebuildStatic(true)
{
    _capturedSlider = -1;
}

Hud::~Hud()
{
}

void
Hud::Init(int width, int height, int framebufferWidth, int framebufferHeight)
{
    _windowWidth = width;
    _windowHeight = height;
    _framebufferWidth = framebufferWidth;
    _framebufferHeight = framebufferHeight;
}

int
Hud::GetWidth() const
{
    return _windowWidth;
}

int
Hud::GetHeight() const
{
    return _windowHeight;
}

void
Hud::Clear()
{
    _radioButtons.clear();
    _checkBoxes.clear();
    _sliders.clear();
    _vboSource.clear();
    _requiresRebuildStatic = true;
}

bool
Hud::KeyDown(int key)
{
    for (std::vector<RadioButton>::iterator it = _radioButtons.begin();
        it != _radioButtons.end(); ++it) {
        if (key == it->shortcut) {
            int nextLocalIndex = it->localIndex;
            if (it->sharedShortcut) {
                // find checked radio button in this group
                int maxLocalIndex = 0;
                for (std::vector<RadioButton>::iterator it2 = _radioButtons.begin();
                     it2 != _radioButtons.end(); ++it2) {

                    if (it2->group == it->group) {
                        maxLocalIndex = std::max(maxLocalIndex, it2->localIndex);
                        if (it2->checked) {
                            nextLocalIndex = it2->localIndex+1;
                        }
                    }
                }
                if (nextLocalIndex > maxLocalIndex) nextLocalIndex = 0;
            }
            for (std::vector<RadioButton>::iterator it2 = _radioButtons.begin();
                 it2 != _radioButtons.end(); ++it2) {
                if (it2->group == it->group) {
                    if (it2->localIndex == nextLocalIndex) {
                        it2->checked = true;
                        it2->callback(it2->callbackData);
                    } else {
                        it2->checked = false;
                    }
                }
            }
            _requiresRebuildStatic = true;
            return true;
        }
    }
    for (std::vector<CheckBox>::iterator it = _checkBoxes.begin();
         it != _checkBoxes.end(); ++it) {

        if (key == it->shortcut) {
            it->checked = !it->checked;
            it->callback(it->checked, it->callbackData);
            _requiresRebuildStatic = true;
            return true;
        }
    }
    for (std::vector<PullDown>::iterator it = _pulldowns.begin();
         it != _pulldowns.end(); ++it) {
        if (key==it->shortcut) {
            // cycle through selections
            ++it->selected;
            if (it->selected>=(int)it->labels.size()) {
                 it->selected=0;
            }
            it->callback(it->values[it->selected]);
            _requiresRebuildStatic = true;
            return true;
        }
    }

    return false;
}

bool
Hud::MouseClick(int x, int y)
{
    if (!IsVisible()) {
        return false;
    }

    for (std::vector<RadioButton>::iterator it = _radioButtons.begin();
        it != _radioButtons.end(); ++it) {
        if (hitTest(*it, x, y)) {
            for (std::vector<RadioButton>::iterator it2 = _radioButtons.begin();
                 it2 != _radioButtons.end(); ++it2) {

                if (it2->group == it->group && it != it2) it2->checked = false;
            }
            it->checked = true;
            it->callback(it->callbackData);
            _requiresRebuildStatic = true;
            return true;
        }
    }
    for (std::vector<CheckBox>::iterator it = _checkBoxes.begin();
        it != _checkBoxes.end(); ++it) {
        if (hitTest(*it, x, y)) {
            it->checked = !it->checked;
            it->callback(it->checked, it->callbackData);
            _requiresRebuildStatic = true;
            return true;
        }
    }
    for (std::vector<Slider>::iterator it = _sliders.begin();
         it != _sliders.end(); ++it) {
        if (hitTest(*it, x, y)) {
            int bx = 0, by = 0;
            getWindowPos(*it, &bx, &by);
            it->SetValue(((x-bx-FONT_CHAR_WIDTH/2)/float(it->w))*(it->max - it->min) + it->min);
            it->callback(it->value, it->callbackData);
            _capturedSlider = (int)(it - _sliders.begin());
            _requiresRebuildStatic = true;
            return true;
        }
    }
    for (std::vector<PullDown>::iterator it = _pulldowns.begin();
         it != _pulldowns.end(); ++it) {
        if (hitTest(*it, x, y)) {
            if (! it->open) {
                it->h = FONT_CHAR_HEIGHT;
                it->h *= (int)it->labels.size();
                it->open=true;
            } else {
                int label_width = (3+(int)it->label.size()) * FONT_CHAR_WIDTH;
                int bx = 0, by = 0;
                getWindowPos(*it, &bx, &by);
                if (x > (bx+label_width)) {
                    int sel = it->selected;
                    it->SetSelected((y-by)/FONT_CHAR_HEIGHT);
                    if (it->selected!=sel) {
                        it->callback(it->values[it->selected]);
                    }
                } else {
                    it->open=! it->open;
                }
            }
            _requiresRebuildStatic = true;
            return true;
        }
    }
    return false;
}

void
Hud::MouseMotion(int x, int /* y */)
{
    if (_capturedSlider != -1) {
        std::vector<Slider>::iterator it = _sliders.begin() + _capturedSlider;

        int bx = it->x > 0 ? it->x : _windowWidth + it->x;
        it->SetValue(((x-bx-FONT_CHAR_WIDTH/2)/float(it->w))*(it->max - it->min) + it->min);
        it->callback(it->value, it->callbackData);
        _requiresRebuildStatic = true;
    }
}

bool
Hud::MouseCapture() const
{
    return _capturedSlider != -1;
}

void
Hud::MouseRelease()
{
    _capturedSlider = -1;
}

void
Hud::AddLabel(const char *label, int x, int y)
{
    Item l;
    l.label = label;
    l.x = x;
    l.y = y;

    _labels.push_back(l);
    _requiresRebuildStatic = true;
}

void
Hud::AddCheckBox(const char *label, bool checked, int x, int y,
                 CheckBoxCallback callback, int data, int shortcut)
{
    CheckBox cb;
    cb.label = label;
    cb.checked = checked;
    cb.x = x;
    cb.y = y;
    cb.w = (int)(strlen(label)+1) * FONT_CHAR_WIDTH;
    cb.h = FONT_CHAR_HEIGHT;
    cb.callback = callback;
    cb.callbackData = data;
    cb.shortcut = shortcut;

    _checkBoxes.push_back(cb);
    _requiresRebuildStatic = true;
}

void
Hud::AddRadioButton(int group, const char *label, bool checked, int x, int y,
                    RadioButtonCallback callback, int data, int shortcut)
{
    RadioButton rb;
    rb.group = group;
    rb.label = label;
    rb.checked = checked;
    rb.x = x;
    rb.y = y;
    rb.w = (int)(strlen(label)+1) * FONT_CHAR_WIDTH;
    rb.h = FONT_CHAR_HEIGHT;
    rb.callback = callback;
    rb.callbackData = data;
    rb.shortcut = shortcut;
    rb.sharedShortcut = false;
    rb.localIndex = 0;

    for (std::vector<RadioButton>::iterator it = _radioButtons.begin();
         it != _radioButtons.end(); ++it) {
        if (it->group == group) {
            rb.localIndex = it->localIndex+1;
            if (it->shortcut == shortcut) {
                it->sharedShortcut = true;
                rb.sharedShortcut = true;
            }
        }
    }

    _radioButtons.push_back(rb);
    _requiresRebuildStatic = true;
}

void
Hud::AddSlider(const char *label, float min, float max, float value,
               int x, int y, int width, bool intStep, SliderCallback callback, int data)
{
    Slider slider;
    slider.label = label;
    slider.x = x;
    slider.y = y;
    slider.w = width * FONT_CHAR_WIDTH;
    slider.h = FONT_CHAR_HEIGHT * 2;
    slider.min = min;
    slider.max = max;
    slider.value = value;
    slider.callback = callback;
    slider.callbackData = data;
    slider.intStep = intStep;

    _sliders.push_back(slider);
    _requiresRebuildStatic = true;
}

int
Hud::AddPullDown(const char *label, int x, int y, int width,
                 PullDownCallback callback, int shortcut)
{

    PullDown pd;
    pd.label = label;
    pd.x = x;
    pd.y = y;
    pd.w = width;
    pd.h =  FONT_CHAR_HEIGHT;
    pd.open = false;
    pd.selected = 0;
    pd.callback = callback;
    pd.shortcut = shortcut;

    _pulldowns.push_back(pd);
    _requiresRebuildStatic = true;

    return (int)_pulldowns.size()-1;
}

void
Hud::AddPullDownButton(int handle, const char *label, int value, bool checked)
{
    if (handle < (int)_pulldowns.size()) {

        PullDown & pulldown = _pulldowns[handle];

        pulldown.labels.push_back(label);
        pulldown.values.push_back(value);
        if (checked) {
            pulldown.selected = (int)pulldown.labels.size()-1;
        }
    }
}



int
Hud::drawChar(std::vector<float> &vboSource,
              int x, int y, float r, float g, float b, char ch)
{
    const float w = 1.0f/FONT_TEXTURE_COLUMNS;
    const float h = 1.0f/FONT_TEXTURE_ROWS;

    float u = (ch%FONT_TEXTURE_COLUMNS)/(float)FONT_TEXTURE_COLUMNS;
    float v = (ch/FONT_TEXTURE_COLUMNS)/(float)FONT_TEXTURE_ROWS;

    vboSource.push_back(float(x));
    vboSource.push_back(float(y));
    vboSource.push_back(r);
    vboSource.push_back(g);
    vboSource.push_back(b);
    vboSource.push_back(u);
    vboSource.push_back(v);

    vboSource.push_back(float(x));
    vboSource.push_back(float(y+FONT_CHAR_HEIGHT));
    vboSource.push_back(r);
    vboSource.push_back(g);
    vboSource.push_back(b);
    vboSource.push_back(u);
    vboSource.push_back(v+h);

    vboSource.push_back(float(x+FONT_CHAR_WIDTH));
    vboSource.push_back(float(y));
    vboSource.push_back(r);
    vboSource.push_back(g);
    vboSource.push_back(b);
    vboSource.push_back(u+w);
    vboSource.push_back(v);

    vboSource.push_back(float(x+FONT_CHAR_WIDTH));
    vboSource.push_back(float(y));
    vboSource.push_back(r);
    vboSource.push_back(g);
    vboSource.push_back(b);
    vboSource.push_back(u+w);
    vboSource.push_back(v);

    vboSource.push_back(float(x));
    vboSource.push_back(float(y+FONT_CHAR_HEIGHT));
    vboSource.push_back(r);
    vboSource.push_back(g);
    vboSource.push_back(b);
    vboSource.push_back(u);
    vboSource.push_back(v+h);

    vboSource.push_back(float(x+FONT_CHAR_WIDTH));
    vboSource.push_back(float(y+FONT_CHAR_HEIGHT));
    vboSource.push_back(r);
    vboSource.push_back(g);
    vboSource.push_back(b);
    vboSource.push_back(u+w);
    vboSource.push_back(v+h);

    return x + FONT_CHAR_WIDTH;
}

int
Hud::drawString(std::vector<float> &vboSource,
                int x, int y, float r, float g, float b, const char *c)
{
    while (*c) {
        char ch = (char)((*c) & 0x7f);
        x = drawChar(vboSource, x, y, r, g, b, ch);
        c++;
    }
    return x;
}

void
Hud::DrawString(int x, int y, const char *fmt, ...)
{
    char buf[1024];
    va_list args;
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);

    x = x > 0 ? x : _windowWidth + x;
    y = y > 0 ? y : _windowHeight + y;

    drawString(_vboSource, x, y, 1, 1, 1, buf);
}

void
Hud::DrawString(int x, int y, float r, float g, float b, const char *fmt, ...)
{
    char buf[1024];
    va_list args;
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);

    x = x > 0 ? x : _windowWidth + x;
    y = y > 0 ? y : _windowHeight + y;

    drawString(_vboSource, x, y, r, g, b, buf);
}

void
Hud::Rebuild(int width, int height, int framebufferWidth, int framebufferHeight)
{
    _requiresRebuildStatic = false;
    _windowWidth = width;
    _windowHeight = height;
    _framebufferWidth = framebufferWidth;
    _framebufferHeight = framebufferHeight;

    _staticVboSource.clear();

    int x, y;
    // add UI elements
    for (std::vector<Item>::const_iterator it = _labels.begin();
         it != _labels.end(); ++it) {
        getWindowPos(*it, &x, &y);
        drawString(_staticVboSource, x, y, 1, 1, 1, it->label.c_str());
    }
    // draw radio buttons
    for (std::vector<RadioButton>::const_iterator it = _radioButtons.begin();
         it != _radioButtons.end(); ++it) {
        getWindowPos(*it, &x, &y);
        if (it->checked) {
            x = drawChar(_staticVboSource, x, y, 1, 1, 1, FONT_RADIO_BUTTON_ON);
            drawString(_staticVboSource, x, y, 1, 1, 0, it->label.c_str());
        } else {
            x = drawChar(_staticVboSource, x, y, 1, 1, 1, FONT_RADIO_BUTTON_OFF);
            drawString(_staticVboSource, x, y, .5f, .5f, .5f, it->label.c_str());
        }
    }
    // draw checkboxes
    for (std::vector<CheckBox>::const_iterator it = _checkBoxes.begin();
         it != _checkBoxes.end(); ++it) {
        getWindowPos(*it, &x, &y);
        if (it->checked) {
            x = drawChar(_staticVboSource, x, y, 1, 1, 1, FONT_CHECK_BOX_ON);
            drawString(_staticVboSource, x, y, 1, 1, 0, it->label.c_str());
        } else {
            x = drawChar(_staticVboSource, x, y, 1, 1, 1, FONT_CHECK_BOX_OFF);
            drawString(_staticVboSource, x, y, .5f, .5f, .5f, it->label.c_str());
        }
    }
    // draw sliders
    for (std::vector<Slider>::const_iterator it = _sliders.begin();
         it != _sliders.end(); ++it) {
        getWindowPos(*it, &x, &y);
        int sx = x;
        x = drawString(_staticVboSource, x, y, 1, 1, 1, it->label.c_str());
        char value[16];
        if (it->intStep) {
            snprintf(value, 16, " : %d", (int)it->value);
        } else {
            snprintf(value, 16, " : %.2f", it->value);
        }
        drawString(_staticVboSource, x, y, 1, 1, 1, value);

        // new line
        y += FONT_CHAR_HEIGHT;
        x = sx;

        x = drawChar(_staticVboSource, x, y, 1, 1, 1, FONT_SLIDER_LEFT);
        int nw = it->w / FONT_CHAR_WIDTH;
        for (int i = 1; i < nw; ++i) {
            x = drawChar(_staticVboSource, x, y, 1, 1, 1, FONT_SLIDER_MIDDLE);
        }
        drawChar(_staticVboSource, x, y, 1, 1, 1, FONT_SLIDER_RIGHT);
        int pos = int(((it->value-it->min)/(it->max-it->min))*float(it->w));
        drawChar(_staticVboSource, sx+pos, y, 1, 1, 0, FONT_SLIDER_CURSOR);
    }
    // draw pulldowns
    for (std::vector<PullDown>::const_iterator it = _pulldowns.begin();
         it != _pulldowns.end(); ++it) {
        getWindowPos(*it, &x, &y);

        x = drawString(_staticVboSource, x, y, .5f, .5f, .5f, it->label.c_str());
        x += FONT_CHAR_WIDTH;

        if (it->open) {
            x = drawChar(_staticVboSource, x, y, 1, 1, 0, FONT_ARROW_DOWN);
            x += FONT_CHAR_WIDTH;
            for (int i=0; i<(int)it->labels.size(); ++i, y+=FONT_CHAR_HEIGHT) {
                if (i==it->selected) {
                    drawString(_staticVboSource, x, y, 1, 1, 0, it->labels[i]);
                } else {
                    drawString(_staticVboSource, x, y, 0.5f, 0.5f, 0.5f, it->labels[i]);
                }
            }
        } else {
            x = drawChar(_staticVboSource, x, y, .5f, .5f, .5f, FONT_ARROW_RIGHT);
            x += FONT_CHAR_WIDTH;
            drawString(_staticVboSource, x, y, 1, 1, 0, it->labels[it->selected]);
        }
    }

    // draw the character cells corresponding to the logo
    drawString(_staticVboSource, _windowWidth-128, _windowHeight-44, .5, .5, .5,
               "\x06\x07\x08\x09");
    drawString(_staticVboSource, _windowWidth-128, _windowHeight-28, .5, .5, .5,
               "\x16\x17\x18\x19");
    drawString(_staticVboSource, _windowWidth-92, _windowHeight-36, .5, .5, .5,
               "\x0a\x0b\x0c\x0d");
    drawString(_staticVboSource, _windowWidth-58, _windowHeight-36, .5, .5, .5,
               "\x1a\x1b\x1c\x1d\x1e\x1f");
}

bool
Hud::Flush()
{
    if (!_visible) {
        _vboSource.clear();
        return false;
    }

    if (_requiresRebuildStatic)
        this->Rebuild(_windowWidth, _windowHeight, _framebufferWidth, _framebufferHeight);

    return true;
}

bool
Hud::IsVisible() const
{
    return _visible;
}

void
Hud::SetVisible(bool visible)
{
    _visible = visible;
}

std::vector<float> &
Hud::getVboSource()
{
    return _vboSource;
}

std::vector<float> &
Hud::getStaticVboSource()
{
    return _staticVboSource;
}