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

qcd_move_widget.py « object_collection_manager - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 55da145319568bf6efba0883398f9687c24946cb (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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# SPDX-License-Identifier: GPL-2.0-or-later

# Copyright 2011, Ryan Inch

import time
from math import cos, sin, pi, floor
import bpy
import bgl
import blf
import gpu
from gpu_extras.batch import batch_for_shader

from bpy.types import Operator

from . import internals

from .qcd_operators import (
    get_move_selection,
    get_move_active,
    )

def spacer():
    spacer = 10
    return round(spacer * scale_factor())

def scale_factor():
    return bpy.context.preferences.system.ui_scale

def get_coords(area):
    x = area["vert"][0]
    y = area["vert"][1]
    w = area["width"]
    h = area["height"]

    vertices = (
        (x, y-h), # bottom left
        (x+w, y-h), # bottom right
        (x, y), # top left
        (x+w, y)) # top right

    indices = (
        (0, 1, 2), (2, 1, 3))

    return vertices, indices

def get_x_coords(area):
    x = area["vert"][0]
    y = area["vert"][1]
    w = area["width"]
    h = area["height"]

    vertices = (
        (x, y), # top left A
        (x+(w*0.1), y), # top left B
        (x+w, y), # top right A
        (x+w-(w*0.1), y), # top right B
        (x, y-h), # bottom left A
        (x+(w*0.1), y-h), # bottom left B
        (x+w, y-h), # bottom right A
        (x+w-(w*0.1), y-h), # bottom right B
        (x+(w/2)-(w*0.05), y-(h/2)), # center left
        (x+(w/2)+(w*0.05), y-(h/2))  # center right
        )

    indices = (
        (0,1,8), (1,8,9), # top left bar
        (2,3,9), (3,9,8), # top right bar
        (4,5,8), (5,8,9), # bottom left bar
        (6,7,8), (6,9,8)  # bottom right bar
        )

    return vertices, indices

def get_circle_coords(area):
    # set x, y to center
    x = area["vert"][0] + area["width"] / 2
    y = area["vert"][1] - area["width"] / 2
    radius = area["width"] / 2
    sides = 32
    vertices = [(radius * cos(side * 2 * pi / sides) + x,
                 radius * sin(side * 2 * pi / sides) + y)
                 for side in range(sides + 1)]

    return vertices

def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False):
    sides = 32

    tl = round(tl * scale_factor())
    tr = round(tr * scale_factor())
    bl = round(bl * scale_factor())
    br = round(br * scale_factor())

    bgl.glEnable(bgl.GL_BLEND)

    if outline:
        thickness = round(2 * scale_factor())
        thickness = max(thickness, 2)

        bgl.glLineWidth(thickness)
        bgl.glEnable(bgl.GL_LINE_SMOOTH)
        bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)

    draw_type = 'TRI_FAN' if not outline else 'LINE_STRIP'

    # top left corner
    vert_x = area["vert"][0] + tl
    vert_y = area["vert"][1] - tl
    tl_vert = (vert_x, vert_y)
    vertices = [(vert_x, vert_y)] if not outline else []

    for side in range(sides+1):
        if (8<=side<=16):
            cosine = tl * cos(side * 2 * pi / sides) + vert_x
            sine = tl * sin(side * 2 * pi / sides) + vert_y
            vertices.append((cosine,sine))

    batch = batch_for_shader(shader, draw_type, {"pos": vertices})
    shader.bind()
    shader.uniform_float("color", color)
    batch.draw(shader)

    # top right corner
    vert_x = area["vert"][0] + area["width"] - tr
    vert_y = area["vert"][1] - tr
    tr_vert = (vert_x, vert_y)
    vertices = [(vert_x, vert_y)] if not outline else []

    for side in range(sides+1):
        if (0<=side<=8):
            cosine = tr * cos(side * 2 * pi / sides) + vert_x
            sine = tr * sin(side * 2 * pi / sides) + vert_y
            vertices.append((cosine,sine))

    batch = batch_for_shader(shader, draw_type, {"pos": vertices})
    shader.bind()
    shader.uniform_float("color", color)
    batch.draw(shader)

    # bottom left corner
    vert_x = area["vert"][0] + bl
    vert_y = area["vert"][1] - area["height"] + bl
    bl_vert = (vert_x, vert_y)
    vertices = [(vert_x, vert_y)] if not outline else []

    for side in range(sides+1):
        if (16<=side<=24):
            cosine = bl * cos(side * 2 * pi / sides) + vert_x
            sine = bl * sin(side * 2 * pi / sides) + vert_y
            vertices.append((cosine,sine))

    batch = batch_for_shader(shader, draw_type, {"pos": vertices})
    shader.bind()
    shader.uniform_float("color", color)
    batch.draw(shader)

    # bottom right corner
    vert_x = area["vert"][0] + area["width"] - br
    vert_y = area["vert"][1] - area["height"] + br
    br_vert = (vert_x, vert_y)
    vertices = [(vert_x, vert_y)] if not outline else []

    for side in range(sides+1):
        if (24<=side<=32):
            cosine = br * cos(side * 2 * pi / sides) + vert_x
            sine = br * sin(side * 2 * pi / sides) + vert_y
            vertices.append((cosine,sine))

    batch = batch_for_shader(shader, draw_type, {"pos": vertices})
    shader.bind()
    shader.uniform_float("color", color)
    batch.draw(shader)

    if not outline:
        vertices = []
        indices = []
        base_ind = 0

        # left edge
        width = max(tl, bl)
        le_x = tl_vert[0]-tl
        vertices.extend([
            (le_x, tl_vert[1]),
            (le_x+width, tl_vert[1]),
            (le_x, bl_vert[1]),
            (le_x+width, bl_vert[1])
            ])
        indices.extend([
            (base_ind,base_ind+1,base_ind+2),
            (base_ind+2,base_ind+3,base_ind+1)
            ])
        base_ind += 4

        # right edge
        width = max(tr, br)
        re_x = tr_vert[0]+tr
        vertices.extend([
            (re_x, tr_vert[1]),
            (re_x-width, tr_vert[1]),
            (re_x, br_vert[1]),
            (re_x-width, br_vert[1])
            ])
        indices.extend([
            (base_ind,base_ind+1,base_ind+2),
            (base_ind+2,base_ind+3,base_ind+1)
            ])
        base_ind += 4

        # top edge
        width = max(tl, tr)
        te_y = tl_vert[1]+tl
        vertices.extend([
            (tl_vert[0], te_y),
            (tl_vert[0], te_y-width),
            (tr_vert[0], te_y),
            (tr_vert[0], te_y-width)
            ])
        indices.extend([
            (base_ind,base_ind+1,base_ind+2),
            (base_ind+2,base_ind+3,base_ind+1)
            ])
        base_ind += 4

        # bottom edge
        width = max(bl, br)
        be_y = bl_vert[1]-bl
        vertices.extend([
            (bl_vert[0], be_y),
            (bl_vert[0], be_y+width),
            (br_vert[0], be_y),
            (br_vert[0], be_y+width)
            ])
        indices.extend([
            (base_ind,base_ind+1,base_ind+2),
            (base_ind+2,base_ind+3,base_ind+1)
            ])
        base_ind += 4

        # middle
        vertices.extend([
            tl_vert,
            tr_vert,
            bl_vert,
            br_vert
            ])
        indices.extend([
            (base_ind,base_ind+1,base_ind+2),
            (base_ind+2,base_ind+3,base_ind+1)
            ])

        batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)

        shader.uniform_float("color", color)
        batch.draw(shader)

    else:
        overlap = round(thickness / 2 - scale_factor() / 2)

        # left edge
        le_x = tl_vert[0]-tl
        vertices = [
            (le_x, tl_vert[1] + (overlap if tl == 0 else 0)),
            (le_x, bl_vert[1] - (overlap if bl == 0 else 0))
            ]

        batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices})
        batch.draw(shader)

        # right edge
        re_x = tr_vert[0]+tr
        vertices = [
            (re_x, tr_vert[1] + (overlap if tr == 0 else 0)),
            (re_x, br_vert[1] - (overlap if br == 0 else 0))
            ]

        batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices})
        batch.draw(shader)

        # top edge
        te_y = tl_vert[1]+tl
        vertices = [
            (tl_vert[0] - (overlap if tl == 0 else 0), te_y),
            (tr_vert[0] + (overlap if tr == 0 else 0), te_y)
            ]

        batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices})
        batch.draw(shader)

        # bottom edge
        be_y = bl_vert[1]-bl
        vertices = [
            (bl_vert[0] - (overlap if bl == 0 else 0), be_y),
            (br_vert[0] + (overlap if br == 0 else 0), be_y)
            ]

        batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices})
        batch.draw(shader)

        bgl.glDisable(bgl.GL_LINE_SMOOTH)

    bgl.glDisable(bgl.GL_BLEND)

def mouse_in_area(mouse_pos, area, buf = 0):
    x = mouse_pos[0]
    y = mouse_pos[1]

    # check left
    if x+buf < area["vert"][0]:
        return False

    # check right
    if x-buf > area["vert"][0] + area["width"]:
        return False

    # check top
    if y-buf > area["vert"][1]:
        return False

    # check bottom
    if y+buf < area["vert"][1] - area["height"]:
        return False

    # if we reach here we're in the area
    return True

def account_for_view_bounds(area):
    # make sure it renders in the 3d view - prioritize top left

    # right
    if area["vert"][0] + area["width"] > bpy.context.region.width:
        x = bpy.context.region.width - area["width"]
        y = area["vert"][1]

        area["vert"] = (x, y)

    # left
    if area["vert"][0] < 0:
        x = 0
        y = area["vert"][1]

        area["vert"] = (x, y)

    # bottom
    if area["vert"][1] - area["height"] < 0:
        x = area["vert"][0]
        y = area["height"]

        area["vert"] = (x, y)

    # top
    if area["vert"][1] > bpy.context.region.height:
        x = area["vert"][0]
        y = bpy.context.region.height

        area["vert"] = (x, y)

def update_area_dimensions(area, w=0, h=0):
    area["width"] += w
    area["height"] += h

class QCDMoveWidget(Operator):
    """Move objects to QCD Slots"""
    bl_idname = "view3d.qcd_move_widget"
    bl_label = "QCD Move Widget"

    slots = {
        "ONE":1,
        "TWO":2,
        "THREE":3,
        "FOUR":4,
        "FIVE":5,
        "SIX":6,
        "SEVEN":7,
        "EIGHT":8,
        "NINE":9,
        "ZERO":10,
        }

    last_type = ''
    initialized = False
    moved = False

    def modal(self, context, event):
        if event.type == 'TIMER':
            if self.hover_time and self.hover_time + 0.5 < time.time():
                self.draw_tooltip = True

                context.area.tag_redraw()
            return {'RUNNING_MODAL'}


        context.area.tag_redraw()

        if len(self.areas) == 1:
            return {'RUNNING_MODAL'}

        if self.last_type == 'LEFTMOUSE' and event.value == 'PRESS' and event.type == 'MOUSEMOVE':
            if mouse_in_area(self.mouse_pos, self.areas["Grab Bar"]):
                x_offset = self.areas["Main Window"]["vert"][0] - self.mouse_pos[0]
                x = event.mouse_region_x + x_offset

                y_offset = self.areas["Main Window"]["vert"][1] - self.mouse_pos[1]
                y = event.mouse_region_y + y_offset

                self.areas["Main Window"]["vert"] = (x, y)

                self.mouse_pos = (event.mouse_region_x, event.mouse_region_y)

        elif event.type == 'MOUSEMOVE':
            self.draw_tooltip = False
            self.hover_time = None
            self.mouse_pos = (event.mouse_region_x, event.mouse_region_y)

            if not mouse_in_area(self.mouse_pos, self.areas["Main Window"], 50 * scale_factor()):
                if self.initialized:
                    bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')

                    if self.moved:
                        bpy.ops.ed.undo_push()

                    return {'FINISHED'}

            else:
                self.initialized = True

        elif event.value == 'PRESS' and event.type == 'LEFTMOUSE':
            if not mouse_in_area(self.mouse_pos, self.areas["Main Window"], 10 * scale_factor()):
                bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')

                if self.moved:
                    bpy.ops.ed.undo_push()

                return {'FINISHED'}

            for num in range(20):
                if not self.areas.get(f"Button {num + 1}", None):
                    continue

                if mouse_in_area(self.mouse_pos, self.areas[f"Button {num + 1}"]):
                    bpy.ops.view3d.move_to_qcd_slot(slot=str(num + 1), toggle=event.shift)
                    self.moved = True

        elif event.type in {'RIGHTMOUSE', 'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')

            return {'CANCELLED'}

        if event.value == 'PRESS' and event.type in self.slots:
            move_to = self.slots[event.type]

            if event.alt:
                move_to += 10

            if event.shift:
                bpy.ops.view3d.move_to_qcd_slot(slot=str(move_to), toggle=True)
            else:
                bpy.ops.view3d.move_to_qcd_slot(slot=str(move_to), toggle=False)

            self.moved = True

        if event.type != 'MOUSEMOVE' and event.type != 'INBETWEEN_MOUSEMOVE':
            self.last_type = event.type

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.area.type == 'VIEW_3D':
            # the arguments we pass the the callback
            args = (self, context)
            # Add the region OpenGL drawing callback
            # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
            self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, args, 'WINDOW', 'POST_PIXEL')
            self._timer = context.window_manager.event_timer_add(0.1, window=context.window)

            self.mouse_pos = (event.mouse_region_x, event.mouse_region_y)

            self.draw_tooltip = False

            self.hover_time = None

            self.areas = {}

            # MAIN WINDOW BACKGROUND
            x = self.mouse_pos[0] - spacer()*2
            y = self.mouse_pos[1] + spacer()*2
            main_window = {
                # Top Left Vertex
                "vert": (x,y),
                "width": 0,
                "height": 0,
                "value": None
                }

            self.areas["Main Window"] = main_window
            allocate_main_ui(self, context)
            account_for_view_bounds(main_window)

            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}

        else:
            self.report({'WARNING'}, "View3D not found, cannot run operator")
            return {'CANCELLED'}


def allocate_main_ui(self, context):
    main_window = self.areas["Main Window"]
    self.areas.clear()
    main_window["width"] = 0
    main_window["height"] = 0
    self.areas["Main Window"] = main_window

    cur_width_pos = main_window["vert"][0]
    cur_height_pos = main_window["vert"][1]

    # GRAB BAR
    grab_bar = {
        "vert": main_window["vert"],
        "width": 0,
        "height": round(23 * scale_factor()),
        "value": None
        }

    # add grab bar to areas
    self.areas["Grab Bar"] = grab_bar


    # WINDOW TITLE
    wt_indent_x = spacer()*2
    wt_y_offset = round(spacer()/2)
    window_title = {
        "vert": main_window["vert"],
        "width": 0,
        "height": round(13 * scale_factor()),
        "value": "Move Objects to QCD Slots"
        }

    x = main_window["vert"][0] + wt_indent_x
    y = main_window["vert"][1] - window_title["height"] - wt_y_offset
    window_title["vert"] = (x, y)

    # add window title to areas
    self.areas["Window Title"] = window_title

    cur_height_pos = window_title["vert"][1]


    # MAIN BUTTON AREA
    button_size = round(20 * scale_factor())
    button_gap = round(1 * scale_factor())
    button_group = 5
    button_group_gap = round(20 * scale_factor())
    button_group_width = button_size * button_group + button_gap * (button_group - 1)

    mba_indent_x = spacer()*2
    mba_outdent_x = spacer()*2
    mba_indent_y = spacer()
    x = cur_width_pos + mba_indent_x
    y = cur_height_pos - mba_indent_y
    main_button_area = {
        "vert": (x, y),
        "width": 0,
        "height": 0,
        "value": None
        }

    # add main button area to areas
    self.areas["Main Button Area"] = main_button_area

    # update current position
    cur_width_pos = main_button_area["vert"][0]
    cur_height_pos = main_button_area["vert"][1]


    # BUTTON ROW 1 A
    button_row_1_a = {
        "vert": main_button_area["vert"],
        "width": button_group_width,
        "height": button_size,
        "value": None
        }

    # add button row 1 A to areas
    self.areas["Button Row 1 A"] = button_row_1_a

    # advance width pos to start of next row
    cur_width_pos += button_row_1_a["width"]
    cur_width_pos += button_group_gap

    # BUTTON ROW 1 B
    x = cur_width_pos
    y = cur_height_pos
    button_row_1_b = {
        "vert": (x, y),
        "width": button_group_width,
        "height": button_size,
        "value": None
        }

    # add button row 1 B to areas
    self.areas["Button Row 1 B"] = button_row_1_b

    # reset width pos to start of main button area
    cur_width_pos = main_button_area["vert"][0]
    # update height pos
    cur_height_pos -= button_row_1_a["height"]
    # add gap between button rows
    cur_height_pos -= button_gap


    # BUTTON ROW 2 A
    x = cur_width_pos
    y = cur_height_pos
    button_row_2_a = {
        "vert": (x, y),
        "width": button_group_width,
        "height": button_size,
        "value": None
        }

    # add button row 2 A to areas
    self.areas["Button Row 2 A"] = button_row_2_a

    # advance width pos to start of next row
    cur_width_pos += button_row_2_a["width"]
    cur_width_pos += button_group_gap

    # BUTTON ROW 2 B
    x = cur_width_pos
    y = cur_height_pos
    button_row_2_b = {
        "vert": (x, y),
        "width": button_group_width,
        "height": button_size,
        "value": None
        }

    # add button row 2 B to areas
    self.areas["Button Row 2 B"] = button_row_2_b


    selected_objects = get_move_selection()
    active_object = get_move_active()


    # BUTTONS
    def get_buttons(button_row, row_num):
        cur_width_pos = button_row["vert"][0]
        cur_height_pos = button_row["vert"][1]
        for num in range(button_group):
            slot_num = row_num + num

            qcd_slot_name = internals.qcd_slots.get_name(f"{slot_num}")

            if qcd_slot_name:
                qcd_laycol = internals.layer_collections[qcd_slot_name]["ptr"]
                collection_objects = qcd_laycol.collection.objects

                # BUTTON
                x = cur_width_pos
                y = cur_height_pos
                button = {
                    "vert": (x, y),
                    "width": button_size,
                    "height": button_size,
                    "value": slot_num
                    }

                self.areas[f"Button {slot_num}"] = button

                # ACTIVE OBJECT ICON
                if active_object and active_object in selected_objects and active_object.name in collection_objects:
                    x = cur_width_pos + round(button_size / 4)
                    y = cur_height_pos - round(button_size / 4)
                    active_object_indicator = {
                    "vert": (x, y),
                    "width": floor(button_size / 2),
                    "height": floor(button_size / 2),
                    "value": None
                    }

                    self.areas[f"Button {slot_num} Active Object Indicator"] = active_object_indicator

                elif not set(selected_objects).isdisjoint(collection_objects):
                    x = cur_width_pos + round(button_size / 4) + floor(1 * scale_factor())
                    y = cur_height_pos - round(button_size / 4) - floor(1 * scale_factor())
                    selected_object_indicator = {
                        "vert": (x, y),
                        "width": floor(button_size / 2) - floor(1 * scale_factor()),
                        "height": floor(button_size / 2) - floor(1 * scale_factor()),
                        "value": None
                        }

                    self.areas[f"Button {slot_num} Selected Object Indicator"] = selected_object_indicator

                elif collection_objects:
                    x = cur_width_pos + floor(button_size / 4)
                    y = cur_height_pos - button_size / 2 + 1 * scale_factor()
                    object_indicator = {
                    "vert": (x, y),
                    "width": round(button_size / 2),
                    "height": round(2 * scale_factor()),
                    "value": None
                    }
                    self.areas[f"Button {slot_num} Object Indicator"] = object_indicator

            else:
                x = cur_width_pos + 2 * scale_factor()
                y = cur_height_pos - 2 * scale_factor()
                X_icon = {
                    "vert": (x, y),
                    "width": button_size - 4 * scale_factor(),
                    "height": button_size - 4 * scale_factor(),
                    "value": None
                    }

                self.areas[f"X_icon {slot_num}"] = X_icon

            cur_width_pos += button_size
            cur_width_pos += button_gap

    get_buttons(button_row_1_a, 1)
    get_buttons(button_row_1_b, 6)
    get_buttons(button_row_2_a, 11)
    get_buttons(button_row_2_b, 16)


    # UPDATE DYNAMIC DIMENSIONS
    width = button_row_1_a["width"] + button_group_gap + button_row_1_b["width"]
    height = button_row_1_a["height"] + button_gap + button_row_2_a["height"]
    update_area_dimensions(main_button_area, width, height)

    width = main_button_area["width"] + mba_indent_x + mba_outdent_x
    height = main_button_area["height"] + mba_indent_y * 2 + window_title["height"] + wt_y_offset
    update_area_dimensions(main_window, width, height)

    update_area_dimensions(grab_bar, main_window["width"])


def draw_callback_px(self, context):
    allocate_main_ui(self, context)

    shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
    shader.bind()

    addon_prefs = context.preferences.addons[__package__].preferences

    # main window background
    main_window = self.areas["Main Window"]
    outline_color = addon_prefs.qcd_ogl_widget_menu_back_outline
    background_color = addon_prefs.qcd_ogl_widget_menu_back_inner
    draw_rounded_rect(main_window, shader, outline_color[:] + (1,), outline=True)
    draw_rounded_rect(main_window, shader, background_color)

    # draw window title
    window_title = self.areas["Window Title"]
    x = window_title["vert"][0]
    y = window_title["vert"][1]
    h = window_title["height"]
    text = window_title["value"]
    text_color = addon_prefs.qcd_ogl_widget_menu_back_text
    font_id = 0
    blf.position(font_id, x, y, 0)
    blf.size(font_id, int(h), 72)
    blf.color(font_id, text_color[0], text_color[1], text_color[2], 1)
    blf.draw(font_id, text)

    # refresh shader - not sure why this is needed
    shader.bind()

    in_tooltip_area = False
    tooltip_slot_idx = None

    for num in range(20):
        slot_num = num + 1
        qcd_slot_name = internals.qcd_slots.get_name(f"{slot_num}")
        if qcd_slot_name:
            qcd_laycol = internals.layer_collections[qcd_slot_name]["ptr"]
            collection_objects = qcd_laycol.collection.objects
            selected_objects = get_move_selection()
            active_object = get_move_active()
            button_area = self.areas[f"Button {slot_num}"]

            # colors
            button_color = addon_prefs.qcd_ogl_widget_tool_inner
            icon_color = addon_prefs.qcd_ogl_widget_tool_text
            if not qcd_laycol.exclude:
                button_color = addon_prefs.qcd_ogl_widget_tool_inner_sel
                icon_color = addon_prefs.qcd_ogl_widget_tool_text_sel

            if mouse_in_area(self.mouse_pos, button_area):
                in_tooltip_area = True
                tooltip_slot_idx = slot_num

                mod = 0.1

                if button_color[0] + mod > 1 or button_color[1] + mod > 1 or button_color[2] + mod > 1:
                    mod = -mod

                button_color = (
                    button_color[0] + mod,
                    button_color[1] + mod,
                    button_color[2] + mod,
                    button_color[3]
                    )


            # button roundness
            tl = tr = bl = br = 0
            rounding = 5

            if num < 10:
                if not internals.qcd_slots.contains(idx=f"{num+2}"):
                    tr = rounding

                if not internals.qcd_slots.contains(idx=f"{num}"):
                    tl = rounding
            else:
                if not internals.qcd_slots.contains(idx=f"{num+2}"):
                    br = rounding

                if not internals.qcd_slots.contains(idx=f"{num}"):
                    bl = rounding

            if num in [0,5]:
                tl = rounding
            elif num in [4,9]:
                tr = rounding
            elif num in [10,15]:
                bl = rounding
            elif num in [14,19]:
                br = rounding

            # draw button
            outline_color = addon_prefs.qcd_ogl_widget_tool_outline
            draw_rounded_rect(button_area, shader, outline_color[:] + (1,), tl, tr, bl, br, outline=True)
            draw_rounded_rect(button_area, shader, button_color, tl, tr, bl, br)

            # ACTIVE OBJECT
            if active_object and active_object in selected_objects and active_object.name in collection_objects:
                active_object_indicator = self.areas[f"Button {slot_num} Active Object Indicator"]

                vertices = get_circle_coords(active_object_indicator)
                shader.uniform_float("color", icon_color[:] + (1,))
                batch = batch_for_shader(shader, 'TRI_FAN', {"pos": vertices})

                bgl.glEnable(bgl.GL_BLEND)

                batch.draw(shader)

                bgl.glDisable(bgl.GL_BLEND)

            # SELECTED OBJECTS
            elif not set(selected_objects).isdisjoint(collection_objects):
                selected_object_indicator = self.areas[f"Button {slot_num} Selected Object Indicator"]

                alpha = addon_prefs.qcd_ogl_selected_icon_alpha
                vertices = get_circle_coords(selected_object_indicator)
                shader.uniform_float("color", icon_color[:] + (alpha,))
                batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices})

                bgl.glLineWidth(2 * scale_factor())
                bgl.glEnable(bgl.GL_BLEND)
                bgl.glEnable(bgl.GL_LINE_SMOOTH)
                bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)

                batch.draw(shader)

                bgl.glDisable(bgl.GL_LINE_SMOOTH)
                bgl.glDisable(bgl.GL_BLEND)

            # OBJECTS
            elif collection_objects:
                object_indicator = self.areas[f"Button {slot_num} Object Indicator"]

                alpha = addon_prefs.qcd_ogl_objects_icon_alpha
                vertices, indices = get_coords(object_indicator)
                shader.uniform_float("color", icon_color[:] + (alpha,))
                batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)

                bgl.glEnable(bgl.GL_BLEND)

                batch.draw(shader)

                bgl.glDisable(bgl.GL_BLEND)


        # X ICON
        else:
            X_icon = self.areas[f"X_icon {slot_num}"]
            X_icon_color = addon_prefs.qcd_ogl_widget_menu_back_text

            vertices, indices = get_x_coords(X_icon)
            shader.uniform_float("color", X_icon_color[:] + (1,))
            batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)

            bgl.glEnable(bgl.GL_BLEND)
            bgl.glEnable(bgl.GL_POLYGON_SMOOTH)
            bgl.glHint(bgl.GL_POLYGON_SMOOTH_HINT, bgl.GL_NICEST)

            batch.draw(shader)

            bgl.glDisable(bgl.GL_POLYGON_SMOOTH)
            bgl.glDisable(bgl.GL_BLEND)

    if in_tooltip_area:
        if self.draw_tooltip:
            slot_name = internals.qcd_slots.get_name(f"{tooltip_slot_idx}")
            slot_string = f"QCD Slot {tooltip_slot_idx}: \"{slot_name}\"\n"
            hotkey_string = (
                "  * LMB - Move objects to slot.\n"
                "  * Shift+LMB - Toggle objects\' slot."
                )

            draw_tooltip(self, context, shader, f"{slot_string}{hotkey_string}")

            self.hover_time = None

        else:
            if not self.hover_time:
                self.hover_time = time.time()


def draw_tooltip(self, context, shader, message):
    addon_prefs = context.preferences.addons[__package__].preferences

    font_id = 0
    line_height = 11 * scale_factor()
    text_color = addon_prefs.qcd_ogl_widget_tooltip_text
    blf.size(font_id, int(line_height), 72)
    blf.color(font_id, text_color[0], text_color[1], text_color[2], 1)

    lines = message.split("\n")
    longest = [0,""]
    num_lines = len(lines)

    for line in lines:
        w, _ = blf.dimensions(font_id, line)

        if w > longest[0]:
            longest[0] = w
            longest[1] = line

    w, h = blf.dimensions(font_id, longest[1])

    line_spacer = 1 * scale_factor()
    padding = 4 * scale_factor()

    # draw background
    tooltip = {
        "vert": self.mouse_pos,
        "width": w + spacer()*2,
        "height": (line_height * num_lines + line_spacer * num_lines) + padding*3,
        "value": None
        }

    x = tooltip["vert"][0] - spacer()*2
    y = tooltip["vert"][1] + tooltip["height"] + round(5 * scale_factor())
    tooltip["vert"] = (x, y)

    account_for_view_bounds(tooltip)

    outline_color = addon_prefs.qcd_ogl_widget_tooltip_outline
    background_color = addon_prefs.qcd_ogl_widget_tooltip_inner
    draw_rounded_rect(tooltip, shader, outline_color[:] + (1,), outline=True)
    draw_rounded_rect(tooltip, shader, background_color)

    line_pos = padding + line_height
    # draw text
    for num, line in enumerate(lines):
        x = tooltip["vert"][0] + spacer()
        y = tooltip["vert"][1] - line_pos
        blf.position(font_id, x, y, 0)
        blf.draw(font_id, line)

        line_pos += line_height + line_spacer