forked from jayanam/bl_ui_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bl_ui_widget.py
189 lines (138 loc) · 4.74 KB
/
bl_ui_widget.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import gpu
import bgl
import bpy
from gpu_extras.batch import batch_for_shader
class BL_UI_Widget:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.x_screen = x
self.y_screen = y
self.width = width
self.height = height
self._bg_color = (0.8, 0.8, 0.8, 1.0)
self._tag = None
self.context = None
self.__inrect = False
self._mouse_down = False
self.enabled = True
def set_location(self, x, y):
self.x = x
self.y = y
self.x_screen = x
self.y_screen = y
self.update(x,y)
@property
def bg_color(self):
return self._bg_color
@bg_color.setter
def bg_color(self, value):
self._bg_color = value
@property
def tag(self):
return self._tag
@tag.setter
def tag(self, value):
self._tag = value
def draw(self):
self.shader.bind()
self.shader.uniform_float("color", self._bg_color)
bgl.glEnable(bgl.GL_BLEND)
self.batch_panel.draw(self.shader)
bgl.glDisable(bgl.GL_BLEND)
def init(self, context):
self.context = context
self.update(self.x, self.y)
def update(self, x, y):
area_height = self.get_area_height()
self.x_screen = x
self.y_screen = y
indices = ((0, 1, 2), (0, 2, 3))
y_screen_flip = area_height - self.y_screen
# bottom left, top left, top right, bottom right
vertices = (
(self.x_screen, y_screen_flip),
(self.x_screen, y_screen_flip - self.height),
(self.x_screen + self.width, y_screen_flip - self.height),
(self.x_screen + self.width, y_screen_flip))
self.shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
self.batch_panel = batch_for_shader(self.shader, 'TRIS', {"pos" : vertices}, indices=indices)
def handle_event(self, event):
x = event.mouse_region_x
y = event.mouse_region_y
if(event.type == 'LEFTMOUSE'):
if(event.value == 'PRESS'):
self._mouse_down = True
return self.mouse_down(x, y)
else:
self._mouse_down = False
self.mouse_up(x, y)
elif(event.type == 'MOUSEMOVE'):
self.mouse_move(x, y)
inrect = self.is_in_rect(x, y)
# we enter the rect
if not self.__inrect and inrect:
self.__inrect = True
self.mouse_enter(event, x, y)
# we are leaving the rect
elif self.__inrect and not inrect:
self.__inrect = False
self.mouse_exit(event, x, y)
return False
elif event.value == 'PRESS' and (event.ascii != '' or event.type in self.get_input_keys()):
return self.text_input(event)
return False
def get_input_keys(self) :
return []
def get_area_height(self):
return self.context.area.height
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.width)) and
(widget_y >= y >= (widget_y - self.height))
):
return True
return False
def text_input(self, event):
return False
def mouse_down(self, x, y):
return self.is_in_rect(x,y)
def mouse_up(self, x, y):
pass
def set_mouse_enter(self, mouse_enter_func):
self.mouse_enter_func = mouse_enter_func
def mouse_enter_func(self):
pass
def mouse_enter(self, event, x, y):
try:
self.mouse_enter_func(self)
except Exception as e:
print(e)
return True
def set_mouse_exit(self, mouse_exit_func):
self.mouse_exit_func = mouse_exit_func
def mouse_exit_func(self):
pass
def mouse_exit(self, event, x, y):
try:
self.mouse_exit_func(self)
except Exception as e:
print(e)
return True
def set_mouse_move(self, mouse_move_func):
self.mouse_move_func = mouse_move_func
def mouse_move_func(self):
pass
def mouse_move(self, event, x, y):
if self.is_in_rect(x, y):
try:
self.mouse_move_func(self)
except Exception as e:
print(e)
return True
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False