Cleanup: trailing space
[blender-addons.git] / mesh_snap_utilities_line / widgets.py
blob36be306bbc30cbdb0a829f0b6798b57c1fe8b318
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
5 from .common_classes import SnapUtilities
6 from .common_utilities import snap_utilities
9 #def mesh_runtime_batchcache_isdirty(me):
10 # import ctypes
11 # batch_cache = ctypes.c_void_p.from_address(me.as_pointer() + 1440)
12 # if batch_cache:
13 # return ctypes.c_bool.from_address(batch_cache.value + 549).value
14 # return False
17 class SnapWidgetCommon(SnapUtilities, bpy.types.Gizmo):
18 # __slots__ = ('inited', 'mode', 'last_mval')
20 snap_to_update = False
22 def handler(self, scene, depsgraph):
23 cls = SnapWidgetCommon
24 if cls.snap_to_update is False:
25 last_operator = self.wm_operators[-1] if self.wm_operators else None
26 if (not last_operator or
27 last_operator.name not in {'Select', 'Loop Select', '(De)select All'}):
28 cls.snap_to_update = depsgraph.id_type_updated('MESH') or \
29 depsgraph.id_type_updated('OBJECT')
31 def draw_point_and_elem(self):
32 if self.bm:
33 if self.bm.is_valid and self.geom.is_valid:
34 self.draw_cache.draw_elem(self.snap_obj, self.bm, self.geom)
35 else:
36 self.bm = None
37 self.geom = None
39 self.draw_cache.draw(self.type, self.location, None, None, None)
41 def init_delayed(self):
42 self.inited = False
44 def init_snapwidget(self, context, snap_edge_and_vert=True):
45 self.inited = True
47 self.snap_context_init(context, snap_edge_and_vert)
48 self.snap_context_update(context)
49 self.mode = context.mode
50 self.last_mval = None
52 self.wm_operators = context.window_manager.operators
53 bpy.app.handlers.depsgraph_update_post.append(self.handler)
54 SnapWidgetCommon.snap_to_update = False
56 SnapUtilities.snapwidgets.append(self)
58 def end_snapwidget(self):
59 if self in SnapUtilities.snapwidgets:
60 SnapUtilities.snapwidgets.remove(self)
62 #from .snap_context_l import global_snap_context_get
63 #sctx = global_snap_context_get(None, None, None)
65 sctx = object.__getattribute__(self, 'sctx')
66 if sctx and not SnapUtilities.snapwidgets:
67 sctx.clear_snap_objects()
69 handler = object.__getattribute__(self, 'handler')
70 bpy.app.handlers.depsgraph_update_post.remove(handler)
72 def update_snap(self, context, mval):
73 if self.last_mval == mval:
74 return -1
75 else:
76 self.last_mval = mval
78 if (SnapWidgetCommon.snap_to_update):
79 ## Something has changed since the last time.
80 # Has the mesh been changed?
81 # In the doubt lets clear the snap context.
82 self.snap_context_update(context)
83 SnapWidgetCommon.snap_to_update = False
85 #print('test_select', mval)
86 space = context.space_data
87 self.sctx.update_viewport_context(context.evaluated_depsgraph_get(), context.region, space, True)
89 shading = space.shading
90 snap_face = not ((self.snap_vert or self.snap_edge) and
91 (shading.show_xray or shading.type == 'WIREFRAME'))
93 if snap_face != self.snap_face:
94 self.snap_face = snap_face
95 self.sctx.set_snap_mode(self.snap_vert, self.snap_edge, self.snap_face)
97 snap_utilities.cache.clear()
98 self.snap_obj, prev_loc, self.location, self.type, self.bm, self.geom, len = snap_utilities(
99 self.sctx,
100 None,
101 mval,
102 increment=self.incremental
106 class SnapPointWidget(SnapWidgetCommon):
107 bl_idname = "VIEW3D_GT_snap_point"
109 __slots__ = ()
111 def test_select(self, context, mval):
112 if not self.inited:
113 self.init_snapwidget(context)
115 self.update_snap(context, mval)
116 self.snap_to_grid()
118 context.area.tag_redraw()
119 return -1
121 def draw(self, context):
122 if self.inited:
123 self.draw_point_and_elem()
125 def setup(self):
126 self.init_delayed()
129 def context_mode_check(context, widget_group):
130 tools = context.workspace.tools
131 mode = context.mode
132 for tool in tools:
133 if (tool.widget == widget_group) and (tool.mode == mode):
134 break
135 else:
136 context.window_manager.gizmo_group_type_unlink_delayed(widget_group)
137 return False
138 return True
141 class SnapWidgetGroupCommon(bpy.types.GizmoGroup):
142 bl_space_type = 'VIEW_3D'
143 bl_region_type = 'WINDOW'
144 bl_options = {'3D'}
146 @classmethod
147 def poll(cls, context):
148 return context_mode_check(context, cls.bl_idname)
150 def init_tool(self, context, gizmo_name):
151 self._widget = self.gizmos.new(gizmo_name)
153 def __del__(self):
154 if hasattr(self, "_widget"):
155 object.__getattribute__(self._widget, 'end_snapwidget')()
158 class SnapPointWidgetGroup(SnapWidgetGroupCommon):
159 bl_idname = "MESH_GGT_snap_point"
160 bl_label = "Draw Snap Point"
162 def setup(self, context):
163 self.init_tool(context, SnapPointWidget.bl_idname)