-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
320 lines (229 loc) · 10.5 KB
/
ui.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
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
import bpy
import rna_keymap_ui
from mathutils import Vector
from bpy_extras.view3d_utils import region_2d_to_location_3d, location_3d_to_region_2d
from bl_ui.space_statusbar import STATUSBAR_HT_header as statusbar
from . registration import get_prefs
from time import time
icons = None
def get_icon(name):
global icons
if not icons:
from .. import icons
return icons[name].icon_id
def get_mouse_pos(self, context, event, hud=True, hud_offset=(20, 20)):
self.mouse_pos = Vector((event.mouse_region_x, event.mouse_region_y))
if hud:
scale = context.preferences.system.ui_scale * get_prefs().modal_hud_scale
self.HUD_x = self.mouse_pos.x + hud_offset[0] * scale
self.HUD_y = self.mouse_pos.y + hud_offset[1] * scale
def wrap_mouse(self, context, x=False, y=False):
width = context.region.width
height = context.region.height
mouse = self.mouse_pos.copy()
if x:
if mouse.x <= 0:
mouse.x = width - 10
elif mouse.x >= width - 1: # the -1 is required for full screen, where the max region width is never passed
mouse.x = 10
if y and mouse == self.mouse_pos:
if mouse.y <= 0:
mouse.y = height - 10
elif mouse.y >= height - 1:
mouse.y = 10
if mouse != self.mouse_pos:
warp_mouse(self, context, mouse)
def warp_mouse(self, context, co2d=Vector(), region=True, hud_offset=(20, 20)):
coords = get_window_space_co2d(context, co2d) if region else co2d
context.window.cursor_warp(int(coords.x), int(coords.y))
self.mouse_pos = co2d if region else get_region_space_co2d(context, co2d)
if getattr(self, 'last_mouse', None):
self.last_mouse = self.mouse_pos
if getattr(self, 'HUD_x', None):
scale = context.preferences.system.ui_scale * get_prefs().modal_hud_scale
self.HUD_x = self.mouse_pos.x + hud_offset[0] * scale
self.HUD_y = self.mouse_pos.y + hud_offset[1] * scale
def get_window_space_co2d(context, co2d=Vector()):
return co2d + Vector((context.region.x, context.region.y))
def get_region_space_co2d(context, co2d=Vector()):
return Vector((context.region.x, context.region.y)) - co2d
def init_cursor(self, event, offsetx=0, offsety=20):
self.last_mouse_x = event.mouse_x
self.last_mouse_y = event.mouse_y
self.region_offset_x = event.mouse_x - event.mouse_region_x
self.region_offset_y = event.mouse_y - event.mouse_region_y
self.HUD_x = event.mouse_x - self.region_offset_x + offsetx
self.HUD_y = event.mouse_y - self.region_offset_y + offsety
def wrap_cursor(self, context, event, x=False, y=False):
if x:
if event.mouse_region_x <= 0:
context.window.cursor_warp(context.region.width + self.region_offset_x - 10, event.mouse_y)
if event.mouse_region_x >= context.region.width - 1: # the -1 is required for full screen, where the max region width is never passed
context.window.cursor_warp(self.region_offset_x + 10, event.mouse_y)
if y:
if event.mouse_region_y <= 0:
context.window.cursor_warp(event.mouse_x, context.region.height + self.region_offset_y - 10)
if event.mouse_region_y >= context.region.height - 1:
context.window.cursor_warp(event.mouse_x, self.region_offset_y + 100)
def popup_message(message, title="Info", icon="INFO", terminal=True):
def draw_message(self, context):
if isinstance(message, list):
for m in message:
self.layout.label(text=m)
else:
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw_message, title=title, icon=icon)
if terminal:
if icon == "FILE_TICK":
icon = "ENABLE"
elif icon == "CANCEL":
icon = "DISABLE"
print(icon, title)
if isinstance(message, list):
print(" »", ", ".join(message))
else:
print(" »", message)
def get_zoom_factor(context, depth_location, scale=10, ignore_obj_scale=False):
center = Vector((context.region.width / 2, context.region.height / 2))
offset = center + Vector((scale, 0))
try:
center_3d = region_2d_to_location_3d(context.region, context.region_data, center, depth_location)
offset_3d = region_2d_to_location_3d(context.region, context.region_data, offset, depth_location)
except:
print("exception!")
return 1
if not ignore_obj_scale and context.active_object:
mx = context.active_object.matrix_world.to_3x3()
zoom_vector = mx.inverted_safe() @ Vector(((center_3d - offset_3d).length, 0, 0))
return zoom_vector.length
return (center_3d - offset_3d).length
def get_flick_direction(context, mouse_loc_3d, flick_vector, axes):
origin_2d = location_3d_to_region_2d(context.region, context.region_data, mouse_loc_3d, default=Vector((context.region.width / 2, context.region.height / 2)))
axes_2d = {}
for direction, axis in axes.items():
axis_2d = location_3d_to_region_2d(context.region, context.region_data, mouse_loc_3d + axis, default=origin_2d)
if (axis_2d - origin_2d).length:
axes_2d[direction] = (axis_2d - origin_2d).normalized()
return min([(d, abs(flick_vector.xy.angle_signed(a))) for d, a in axes_2d.items()], key=lambda x: x[1])[0]
def kmi_to_string(kmi, docs_mode=False):
kmi_str = f"{kmi.idname}, name: {kmi.name}, active: {kmi.active}, map type: {kmi.map_type}, type: {kmi.type}, value: {kmi.value}, alt: {kmi.alt}, ctrl: {kmi.ctrl}, shift: {kmi.shift}, properties: {str(dict(kmi.properties))}"
if docs_mode:
return f"`{kmi_str}`"
else:
return kmi_str
def draw_keymap_items(kc, name, keylist, layout):
drawn = []
idx = 0
for item in keylist:
keymap = item.get("keymap")
isdrawn = False
if keymap:
km = kc.keymaps.get(keymap)
kmi = None
if km:
idname = item.get("idname")
for kmitem in km.keymap_items:
if kmitem.idname == idname:
properties = item.get("properties")
if properties:
if all([getattr(kmitem.properties, name, None) == value for name, value in properties]):
kmi = kmitem
break
else:
kmi = kmitem
break
if kmi:
if idx == 0:
box = layout.box()
if len(keylist) == 1:
label = name.title().replace("_", " ")
else:
if idx == 0:
box.label(text=name.title().replace("_", " "))
label = item.get("label")
row = box.split(factor=0.25)
row.label(text=label)
rna_keymap_ui.draw_kmi(["ADDON", "USER", "DEFAULT"], kc, km, kmi, row, 0)
infos = item.get("info", [])
for text in infos:
row = box.split(factor=0.20)
row.separator()
row.label(text=text, icon="INFO")
isdrawn = True
idx += 1
drawn.append(isdrawn)
return any(d for d in drawn)
def get_keymap_item(name, idname, key=None, alt=False, ctrl=False, shift=False, properties=[]):
wm = bpy.context.window_manager
kc = wm.keyconfigs.user
km = kc.keymaps.get(name)
if bpy.app.version >= (3, 0, 0):
alt = int(alt)
ctrl = int(ctrl)
shift = int(shift)
if km:
kmi = km.keymap_items.get(idname)
if kmi:
found = True if key is None else all([kmi.type == key and kmi.alt is alt and kmi.ctrl is ctrl and kmi.shift is shift])
if found:
if properties:
if all([getattr(kmi.properties, name, False) == prop for name, prop in properties]):
return kmi
else:
return kmi
def init_status(self, context, title='', func=None):
self.bar_orig = statusbar.draw
if func:
statusbar.draw = func
else:
statusbar.draw = draw_basic_status(self, context, title)
def draw_basic_status(self, context, title):
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.label(text=title)
row.label(text="", icon='MOUSE_LMB')
row.label(text="Finish")
if context.window_manager.keyconfigs.active.name.startswith('blender'):
row.label(text="", icon='MOUSE_MMB')
row.label(text="Viewport")
row.label(text="", icon='MOUSE_RMB')
row.label(text="Cancel")
return draw
def finish_status(self):
statusbar.draw = self.bar_orig
def navigation_passthrough(event, alt=True, wheel=False) -> bool:
if alt and wheel:
return event.type in {'MIDDLEMOUSE'} or event.type.startswith('NDOF') or (event.alt and event.type in {'LEFTMOUSE', 'RIGHTMOUSE'} and event.value == 'PRESS') or event.type in {'WHEELUPMOUSE', 'WHEELDOWNMOUSE'}
elif alt:
return event.type in {'MIDDLEMOUSE'} or event.type.startswith('NDOF') or (event.alt and event.type in {'LEFTMOUSE', 'RIGHTMOUSE'} and event.value == 'PRESS')
elif wheel:
return event.type in {'MIDDLEMOUSE'} or event.type.startswith('NDOF') or event.type in {'WHEELUPMOUSE', 'WHEELDOWNMOUSE'}
else:
return event.type in {'MIDDLEMOUSE'} or event.type.startswith('NDOF')
def init_timer_modal(self, debug=False):
self.start = time()
self.countdown = self.time * get_prefs().modal_hud_timeout
if debug:
print(f"initiating timer with a countdown of {self.time}s ({self.time * get_prefs().modal_hud_timeout}s)")
def set_countdown(self, debug=False):
self.countdown = self.time * get_prefs().modal_hud_timeout - (time() - self.start)
if debug:
print("countdown:", self.countdown)
def get_timer_progress(self, debug=False):
progress = self.countdown / (self.time * get_prefs().modal_hud_timeout)
if debug:
print("progress:", progress)
return progress
def force_ui_update(context, active=None):
if context.mode == 'OBJECT':
if active:
active.select_set(True)
else:
visible = context.visible_objects
if visible:
visible[0].select_set(visible[0].select_get())
elif context.mode == 'EDIT_MESH':
context.active_object.select_set(True)
def get_scale(context):
return context.preferences.system.ui_scale * get_prefs().modal_hud_scale