Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GLToolbar.cpp « GUI « slic3r « src « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 388868b12f6148125bc3b6bd4a4719cc6391844e (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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#include "../../libslic3r/Point.hpp"
#include "GLToolbar.hpp"

#include "../../libslic3r/libslic3r.h"
#include "../../slic3r/GUI/GLCanvas3D.hpp"

#include <GL/glew.h>

#include <wx/bitmap.h>
#include <wx/dcmemory.h>
#include <wx/settings.h>

namespace Slic3r {
namespace GUI {

GLToolbarItem::Data::Data()
    : name("")
    , tooltip("")
    , sprite_id(-1)
    , is_toggable(false)
    , action_callback(nullptr)
{
}

GLToolbarItem::GLToolbarItem(GLToolbarItem::EType type, const GLToolbarItem::Data& data)
    : m_type(type)
    , m_state(Disabled)
    , m_data(data)
{
}

GLToolbarItem::EState GLToolbarItem::get_state() const
{
    return m_state;
}

void GLToolbarItem::set_state(GLToolbarItem::EState state)
{
    m_state = state;
}

const std::string& GLToolbarItem::get_name() const
{
    return m_data.name;
}

const std::string& GLToolbarItem::get_tooltip() const
{
    return m_data.tooltip;
}

void GLToolbarItem::do_action()
{
    if (m_data.action_callback != nullptr)
        m_data.action_callback->call();
}

bool GLToolbarItem::is_enabled() const
{
    return m_state != Disabled;
}

bool GLToolbarItem::is_hovered() const
{
    return (m_state == Hover) || (m_state == HoverPressed);
}

bool GLToolbarItem::is_pressed() const
{
    return (m_state == Pressed) || (m_state == HoverPressed);
}

bool GLToolbarItem::is_toggable() const
{
    return m_data.is_toggable;
}

bool GLToolbarItem::is_separator() const
{
    return m_type == Separator;
}

void GLToolbarItem::render(unsigned int tex_id, float left, float right, float bottom, float top, unsigned int texture_size, unsigned int border_size, unsigned int icon_size, unsigned int gap_size) const
{
    GLTexture::render_sub_texture(tex_id, left, right, bottom, top, get_uvs(texture_size, border_size, icon_size, gap_size));
}

GLTexture::Quad_UVs GLToolbarItem::get_uvs(unsigned int texture_size, unsigned int border_size, unsigned int icon_size, unsigned int gap_size) const
{
    GLTexture::Quad_UVs uvs;

    float inv_texture_size = (texture_size != 0) ? 1.0f / (float)texture_size : 0.0f;

    float scaled_icon_size = (float)icon_size * inv_texture_size;
    float scaled_border_size = (float)border_size * inv_texture_size;
    float scaled_gap_size = (float)gap_size * inv_texture_size;
    float stride = scaled_icon_size + scaled_gap_size;

    float left = scaled_border_size + (float)m_state * stride;
    float right = left + scaled_icon_size;
    float top = scaled_border_size + (float)m_data.sprite_id * stride;
    float bottom = top + scaled_icon_size;

    uvs.left_top = { left, top };
    uvs.left_bottom = { left, bottom };
    uvs.right_bottom = { right, bottom };
    uvs.right_top = { right, top };
    
    return uvs;
}

GLToolbar::ItemsIconsTexture::ItemsIconsTexture()
    : items_icon_size(0)
    , items_icon_border_size(0)
    , items_icon_gap_size(0)
{
}

GLToolbar::Layout::Layout()
    : type(Horizontal)
    , top(0.0f)
    , left(0.0f)
    , separator_size(0.0f)
    , gap_size(0.0f)
{
}

GLToolbar::GLToolbar(GLCanvas3D& parent)
    : m_parent(parent)
    , m_enabled(false)
{
}

bool GLToolbar::init(const std::string& icons_texture_filename, unsigned int items_icon_size, unsigned int items_icon_border_size, unsigned int items_icon_gap_size)
{
    std::string path = resources_dir() + "/icons/";
    bool res = !icons_texture_filename.empty() && m_icons_texture.texture.load_from_file(path + icons_texture_filename, false);
    if (res)
    {
        m_icons_texture.items_icon_size = items_icon_size;
        m_icons_texture.items_icon_border_size = items_icon_border_size;
        m_icons_texture.items_icon_gap_size = items_icon_gap_size;
    }

    return res;
}

GLToolbar::Layout::Type GLToolbar::get_layout_type() const
{
    return m_layout.type;
}

void GLToolbar::set_layout_type(GLToolbar::Layout::Type type)
{
    m_layout.type = type;
}

void GLToolbar::set_position(float top, float left)
{
    m_layout.top = top;
    m_layout.left = left;
}

void GLToolbar::set_separator_size(float size)
{
    m_layout.separator_size = size;
}

void GLToolbar::set_gap_size(float size)
{
    m_layout.gap_size = size;
}

bool GLToolbar::is_enabled() const
{
    return m_enabled;
}

void GLToolbar::set_enabled(bool enable)
{
    m_enabled = true;
}

bool GLToolbar::add_item(const GLToolbarItem::Data& data)
{
    GLToolbarItem* item = new GLToolbarItem(GLToolbarItem::Action, data);
    if (item == nullptr)
        return false;

    m_items.push_back(item);
    return true;
}

bool GLToolbar::add_separator()
{
    GLToolbarItem::Data data;
    GLToolbarItem* item = new GLToolbarItem(GLToolbarItem::Separator, data);
    if (item == nullptr)
        return false;

    m_items.push_back(item);
    return true;
}

float GLToolbar::get_width() const
{
    switch (m_layout.type)
    {
    default:
    case Layout::Horizontal:
    {
        return get_width_horizontal();
    }
    case Layout::Vertical:
    {
        return get_width_vertical();
    }
    }
}

float GLToolbar::get_height() const
{
    switch (m_layout.type)
    {
    default:
    case Layout::Horizontal:
    {
        return get_height_horizontal();
    }
    case Layout::Vertical:
    {
        return get_height_vertical();
    }
    }
}

void GLToolbar::enable_item(const std::string& name)
{
    for (GLToolbarItem* item : m_items)
    {
        if ((item->get_name() == name) && (item->get_state() == GLToolbarItem::Disabled))
        {
            item->set_state(GLToolbarItem::Normal);
            return;
        }
    }
}

void GLToolbar::disable_item(const std::string& name)
{
    for (GLToolbarItem* item : m_items)
    {
        if (item->get_name() == name)
        {
            item->set_state(GLToolbarItem::Disabled);
            return;
        }
    }
}

bool GLToolbar::is_item_pressed(const std::string& name) const
{
    for (GLToolbarItem* item : m_items)
    {
        if (item->get_name() == name)
            return item->is_pressed();
    }

    return false;
}

void GLToolbar::update_hover_state(const Vec2d& mouse_pos)
{
    if (!m_enabled)
        return;

    switch (m_layout.type)
    {
    default:
    case Layout::Horizontal:
    {
        update_hover_state_horizontal(mouse_pos);
        break;
    }
    case Layout::Vertical:
    {
        update_hover_state_vertical(mouse_pos);
        break;
    }
    }
}

int GLToolbar::contains_mouse(const Vec2d& mouse_pos) const
{
    if (!m_enabled)
        return -1;

    switch (m_layout.type)
    {
    default:
    case Layout::Horizontal:
    {
        return contains_mouse_horizontal(mouse_pos);
    }
    case Layout::Vertical:
    {
        return contains_mouse_vertical(mouse_pos);
    }
    }
}

void GLToolbar::do_action(unsigned int item_id)
{
    if (item_id < (unsigned int)m_items.size())
    {
        GLToolbarItem* item = m_items[item_id];
        if ((item != nullptr) && !item->is_separator() && item->is_hovered())
        {
            if (item->is_toggable())
            {
                GLToolbarItem::EState state = item->get_state();
                if (state == GLToolbarItem::Hover)
                    item->set_state(GLToolbarItem::HoverPressed);
                else if (state == GLToolbarItem::HoverPressed)
                    item->set_state(GLToolbarItem::Hover);

                m_parent.render();
                item->do_action();
            }
            else
            {
                item->set_state(GLToolbarItem::HoverPressed);
                m_parent.render();
                item->do_action();
                if (item->get_state() != GLToolbarItem::Disabled)
                {
                    // the item may get disabled during the action, if not, set it back to hover state
                    item->set_state(GLToolbarItem::Hover);
                    m_parent.render();
                }
            }
        }
    }
}

void GLToolbar::render() const
{
    if (!m_enabled || m_items.empty())
        return;

    ::glDisable(GL_DEPTH_TEST);

    ::glPushMatrix();
    ::glLoadIdentity();

    switch (m_layout.type)
    {
    default:
    case Layout::Horizontal:
    {
        render_horizontal();
        break;
    }
    case Layout::Vertical:
    {
        render_vertical();
        break;
    }
    }

    ::glPopMatrix();
}

float GLToolbar::get_width_horizontal() const
{
    return get_main_size();
}

float GLToolbar::get_width_vertical() const
{
    return m_icons_texture.items_icon_size;
}

float GLToolbar::get_height_horizontal() const
{
    return m_icons_texture.items_icon_size;
}

float GLToolbar::get_height_vertical() const
{
    return get_main_size();
}

float GLToolbar::get_main_size() const
{
    float size = 0.0f;
    for (unsigned int i = 0; i < (unsigned int)m_items.size(); ++i)
    {
        if (m_items[i]->is_separator())
            size += m_layout.separator_size;
        else
            size += (float)m_icons_texture.items_icon_size;
    }

    if (m_items.size() > 1)
        size += ((float)m_items.size() - 1.0f) * m_layout.gap_size;

    return size;
}

void GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos)
{
    float zoom = m_parent.get_camera_zoom();
    float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;

    Size cnv_size = m_parent.get_canvas_size();
    Vec2d scaled_mouse_pos((mouse_pos(0) - 0.5 * (double)cnv_size.get_width()) * inv_zoom, (0.5 * (double)cnv_size.get_height() - mouse_pos(1)) * inv_zoom);

    float scaled_icons_size = (float)m_icons_texture.items_icon_size * inv_zoom;
    float scaled_separator_size = m_layout.separator_size * inv_zoom;
    float scaled_gap_size = m_layout.gap_size * inv_zoom;

    float separator_stride = scaled_separator_size + scaled_gap_size;
    float icon_stride = scaled_icons_size + scaled_gap_size;

    float left = m_layout.left;
    float top = m_layout.top;

    std::string tooltip = "";
        
    for (GLToolbarItem* item : m_items)
    {
        if (item->is_separator())
            left += separator_stride;
        else
        {
            float right = left + scaled_icons_size;
            float bottom = top - scaled_icons_size;

            GLToolbarItem::EState state = item->get_state();
            bool inside = (left <= (float)scaled_mouse_pos(0)) && ((float)scaled_mouse_pos(0) <= right) && (bottom <= (float)scaled_mouse_pos(1)) && ((float)scaled_mouse_pos(1) <= top);

            switch (state)
            {
            case GLToolbarItem::Normal:
            {
                if (inside)
                    item->set_state(GLToolbarItem::Hover);

                break;
            }
            case GLToolbarItem::Hover:
            {
                if (inside)
                    tooltip = item->get_tooltip();
                else
                    item->set_state(GLToolbarItem::Normal);
                
                break;
            }
            case GLToolbarItem::Pressed:
            {
                if (inside)
                    item->set_state(GLToolbarItem::HoverPressed);
                
                break;
            }
            case GLToolbarItem::HoverPressed:
            {
                if (inside)
                    tooltip = item->get_tooltip();
                else
                    item->set_state(GLToolbarItem::Pressed);
                
                break;
            }
            default:
            case GLToolbarItem::Disabled:
            {
                break;
            }
            }

            left += icon_stride;
        }
    }

    m_parent.set_tooltip(tooltip);
}

void GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos)
{
    float zoom = m_parent.get_camera_zoom();
    float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;

    Size cnv_size = m_parent.get_canvas_size();
    Vec2d scaled_mouse_pos((mouse_pos(0) - 0.5 * (double)cnv_size.get_width()) * inv_zoom, (0.5 * (double)cnv_size.get_height() - mouse_pos(1)) * inv_zoom);

    float scaled_icons_size = (float)m_icons_texture.items_icon_size * inv_zoom;
    float scaled_separator_size = m_layout.separator_size * inv_zoom;
    float scaled_gap_size = m_layout.gap_size * inv_zoom;

    float separator_stride = scaled_separator_size + scaled_gap_size;
    float icon_stride = scaled_icons_size + scaled_gap_size;

    float left = m_layout.left;
    float top = m_layout.top;

    std::string tooltip = "";

    for (GLToolbarItem* item : m_items)
    {
        if (item->is_separator())
            top -= separator_stride;
        else
        {
            float right = left + scaled_icons_size;
            float bottom = top - scaled_icons_size;

            GLToolbarItem::EState state = item->get_state();
            bool inside = (left <= (float)scaled_mouse_pos(0)) && ((float)scaled_mouse_pos(0) <= right) && (bottom <= (float)scaled_mouse_pos(1)) && ((float)scaled_mouse_pos(1) <= top);

            switch (state)
            {
            case GLToolbarItem::Normal:
            {
                if (inside)
                    item->set_state(GLToolbarItem::Hover);

                break;
            }
            case GLToolbarItem::Hover:
            {
                if (inside)
                    tooltip = item->get_tooltip();
                else
                    item->set_state(GLToolbarItem::Normal);

                break;
            }
            case GLToolbarItem::Pressed:
            {
                if (inside)
                    item->set_state(GLToolbarItem::HoverPressed);

                break;
            }
            case GLToolbarItem::HoverPressed:
            {
                if (inside)
                    tooltip = item->get_tooltip();
                else
                    item->set_state(GLToolbarItem::Pressed);

                break;
            }
            default:
            case GLToolbarItem::Disabled:
            {
                break;
            }
            }

            top -= icon_stride;
        }
    }

    m_parent.set_tooltip(tooltip);
}

int GLToolbar::contains_mouse_horizontal(const Vec2d& mouse_pos) const
{
    float zoom = m_parent.get_camera_zoom();
    float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;

    Size cnv_size = m_parent.get_canvas_size();
    Vec2d scaled_mouse_pos((mouse_pos(0) - 0.5 * (double)cnv_size.get_width()) * inv_zoom, (0.5 * (double)cnv_size.get_height() - mouse_pos(1)) * inv_zoom);

    float scaled_icons_size = (float)m_icons_texture.items_icon_size * inv_zoom;
    float scaled_separator_size = m_layout.separator_size * inv_zoom;
    float scaled_gap_size = m_layout.gap_size * inv_zoom;

    float separator_stride = scaled_separator_size + scaled_gap_size;
    float icon_stride = scaled_icons_size + scaled_gap_size;

    float left = m_layout.left;
    float top = m_layout.top;

    int id = -1;
    
    for (GLToolbarItem* item : m_items)
    {
        ++id;
        
        if (item->is_separator())
            left += separator_stride;
        else
        {
            float right = left + scaled_icons_size;
            float bottom = top - scaled_icons_size;
            
            if ((left <= (float)scaled_mouse_pos(0)) && ((float)scaled_mouse_pos(0) <= right) && (bottom <= (float)scaled_mouse_pos(1)) && ((float)scaled_mouse_pos(1) <= top))
                return id;
            
            left += icon_stride;
        }
    }
    
    return -1;
}

int GLToolbar::contains_mouse_vertical(const Vec2d& mouse_pos) const
{
    float zoom = m_parent.get_camera_zoom();
    float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;

    Size cnv_size = m_parent.get_canvas_size();
    Vec2d scaled_mouse_pos((mouse_pos(0) - 0.5 * (double)cnv_size.get_width()) * inv_zoom, (0.5 * (double)cnv_size.get_height() - mouse_pos(1)) * inv_zoom);

    float scaled_icons_size = (float)m_icons_texture.items_icon_size * inv_zoom;
    float scaled_separator_size = m_layout.separator_size * inv_zoom;
    float scaled_gap_size = m_layout.gap_size * inv_zoom;

    float separator_stride = scaled_separator_size + scaled_gap_size;
    float icon_stride = scaled_icons_size + scaled_gap_size;

    float left = m_layout.left;
    float top = m_layout.top;

    int id = -1;

    for (GLToolbarItem* item : m_items)
    {
        ++id;

        if (item->is_separator())
            top -= separator_stride;
        else
        {
            float right = left + scaled_icons_size;
            float bottom = top - scaled_icons_size;

            if ((left <= (float)scaled_mouse_pos(0)) && ((float)scaled_mouse_pos(0) <= right) && (bottom <= (float)scaled_mouse_pos(1)) && ((float)scaled_mouse_pos(1) <= top))
                return id;

            top -= icon_stride;
        }
    }

    return -1;
}

void GLToolbar::render_horizontal() const
{
    unsigned int tex_id = m_icons_texture.texture.get_id();
    int tex_size = m_icons_texture.texture.get_width();

    if ((tex_id == 0) || (tex_size <= 0))
        return;

    float zoom = m_parent.get_camera_zoom();
    float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;

    float scaled_icons_size = (float)m_icons_texture.items_icon_size * inv_zoom;
    float scaled_separator_size = m_layout.separator_size * inv_zoom;
    float scaled_gap_size = m_layout.gap_size * inv_zoom;

    float separator_stride = scaled_separator_size + scaled_gap_size;
    float icon_stride = scaled_icons_size + scaled_gap_size;

    float left = m_layout.left;
    float top = m_layout.top;

    // renders icons
    for (const GLToolbarItem* item : m_items)
    {
        if (item->is_separator())
            left += separator_stride;
        else
        {
            item->render(tex_id, left, left + scaled_icons_size, top - scaled_icons_size, top, (unsigned int)tex_size, m_icons_texture.items_icon_border_size, m_icons_texture.items_icon_size, m_icons_texture.items_icon_gap_size);
            left += icon_stride;
        }
    }
}

void GLToolbar::render_vertical() const
{
    unsigned int tex_id = m_icons_texture.texture.get_id();
    int tex_size = m_icons_texture.texture.get_width();

    if ((tex_id == 0) || (tex_size <= 0))
        return;

    float zoom = m_parent.get_camera_zoom();
    float inv_zoom = (zoom != 0.0f) ? 1.0f / zoom : 0.0f;

    float scaled_icons_size = (float)m_icons_texture.items_icon_size * inv_zoom;
    float scaled_separator_size = m_layout.separator_size * inv_zoom;
    float scaled_gap_size = m_layout.gap_size * inv_zoom;

    float separator_stride = scaled_separator_size + scaled_gap_size;
    float icon_stride = scaled_icons_size + scaled_gap_size;

    float left = m_layout.left;
    float top = m_layout.top;

    // renders icons
    for (const GLToolbarItem* item : m_items)
    {
        if (item->is_separator())
            top -= separator_stride;
        else
        {
            item->render(tex_id, left, left + scaled_icons_size, top - scaled_icons_size, top, (unsigned int)tex_size, m_icons_texture.items_icon_border_size, m_icons_texture.items_icon_size, m_icons_texture.items_icon_gap_size);
            top -= icon_stride;
        }
    }
}

} // namespace GUI
} // namespace Slic3r