forked from jayanam/bl_ui_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bl_ui_checkbox.py
169 lines (118 loc) · 4.49 KB
/
bl_ui_checkbox.py
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
from . bl_ui_widget import *
import blf
import bpy
class BL_UI_Checkbox(BL_UI_Widget):
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height)
self._text_color = (1.0, 1.0, 1.0, 1.0)
self._box_color = (1.0, 1.0, 1.0, 1.0)
self._cross_color = (0.2, 0.9, 0.9, 1.0)
self._text = "Checkbox"
self._text_size = 16
self._textpos = [x, y]
self.__boxsize = (16,16)
self.__state = False
@property
def text_color(self):
return self._text_color
@text_color.setter
def text_color(self, value):
self._text_color = value
@property
def cross_color(self):
return self._cross_color
@cross_color.setter
def cross_color(self, value):
self._cross_color = value
@property
def text(self):
return self._text
@text.setter
def text(self, value):
self._text = value
@property
def text_size(self):
return self._text_size
@text_size.setter
def text_size(self, value):
self._text_size = value
@property
def is_checked(self):
return self.__state
@is_checked.setter
def is_checked(self, value):
if value != self.__state:
self.__state = value
self.call_state_changed()
def update(self, x, y):
super().update(x, y)
self._textpos = [x + 26, y]
area_height = self.get_area_height()
y_screen_flip = area_height - self.y_screen
off_x = 0
off_y = 0
sx, sy = self.__boxsize
self.shader_chb = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
# top left, top right, ...
vertices_box = (
(self.x_screen + off_x, y_screen_flip - off_y - sy),
(self.x_screen + off_x + sx, y_screen_flip - off_y - sy),
(self.x_screen + off_x + sx, y_screen_flip - off_y),
(self.x_screen + off_x, y_screen_flip - off_y))
self.batch_box = batch_for_shader(self.shader_chb, 'LINE_LOOP', {"pos": vertices_box})
inset = 4
# cross top-left, bottom-right | top-right, bottom-left
vertices_cross = (
(self.x_screen + off_x + inset, y_screen_flip - off_y - inset),
(self.x_screen + off_x + sx - inset, y_screen_flip - off_y - sy + inset),
(self.x_screen + off_x + sx - inset, y_screen_flip - off_y - inset),
(self.x_screen + off_x + inset, y_screen_flip - off_y - sy + inset))
self.batch_cross = batch_for_shader(self.shader_chb, 'LINES', {"pos": vertices_cross})
def draw(self):
area_height = self.get_area_height()
self.shader_chb.bind()
if self.is_checked:
bgl.glLineWidth(3)
self.shader_chb.uniform_float("color", self._cross_color)
self.batch_cross.draw(self.shader_chb)
bgl.glLineWidth(2)
self.shader_chb.uniform_float("color", self._box_color)
self.batch_box.draw(self.shader_chb)
# Draw text
self.draw_text(area_height)
def draw_text(self, area_height):
blf.size(0, self._text_size, 72)
size = blf.dimensions(0, self._text)
textpos_y = area_height - self._textpos[1] - (self.height + size[1]) / 2.0
blf.position(0, self._textpos[0], textpos_y + 1, 0)
r, g, b, a = self._text_color
blf.color(0, r, g, b, a)
blf.draw(0, self._text)
def is_in_rect(self, x, y):
area_height = self.get_area_height()
widget_y = area_height - self.y_screen
if (
(self.x_screen <= x <= (self.x_screen + self.__boxsize[0])) and
(widget_y >= y >= (widget_y - self.__boxsize[1]))
):
return True
return False
def set_state_changed(self, state_changed_func):
self.state_changed_func = state_changed_func
def call_state_changed(self):
try:
self.state_changed_func(self, self.__state)
except:
pass
def toggle_state(self):
self.__state = not self.__state
def mouse_enter(self, event, x, y):
if self._mouse_down:
self.toggle_state()
self.call_state_changed()
def mouse_down(self, x, y):
if self.is_in_rect(x,y):
self.toggle_state()
self.call_state_changed()
return True
return False