Cleanup: trailing space
[blender-addons.git] / mesh_snap_utilities_line / widgets.py
blob6c011b7a4af78aad98f57edf8adb8a1734ba0a84
1 ### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 3
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # ##### END GPL LICENSE BLOCK #####
18 import bpy
20 from .common_classes import SnapUtilities
21 from .common_utilities import snap_utilities
22 from .drawing_utilities import SnapDrawn
25 #def mesh_runtime_batchcache_isdirty(me):
26 # import ctypes
27 # batch_cache = ctypes.c_void_p.from_address(me.as_pointer() + 1440)
28 # if batch_cache:
29 # return ctypes.c_bool.from_address(batch_cache.value + 549).value
30 # return False
33 class SnapWidgetCommon(SnapUtilities, bpy.types.Gizmo):
34 # __slots__ = ('inited', 'mode', 'last_mval')
36 snap_to_update = False
38 def handler(self, scene, depsgraph):
39 cls = SnapWidgetCommon
40 if cls.snap_to_update is False:
41 last_operator = self.wm_operators[-1] if self.wm_operators else None
42 if (not last_operator or
43 last_operator.name not in {'Select', 'Loop Select', '(De)select All'}):
44 cls.snap_to_update = depsgraph.id_type_updated('MESH') or \
45 depsgraph.id_type_updated('OBJECT')
47 def draw_point_and_elem(self):
48 if self.bm:
49 if self.bm.is_valid and self.geom.is_valid:
50 self.draw_cache.draw_elem(self.snap_obj, self.bm, self.geom)
51 else:
52 self.bm = None
53 self.geom = None
55 self.draw_cache.draw(self.type, self.location, None, None, None)
57 def init_delayed(self):
58 self.inited = False
60 def init_snapwidget(self, context, snap_edge_and_vert=True):
61 self.inited = True
63 self.snap_context_init(context, snap_edge_and_vert)
64 self.snap_context_update(context)
65 self.mode = context.mode
66 self.last_mval = None
68 self.wm_operators = context.window_manager.operators
69 bpy.app.handlers.depsgraph_update_post.append(self.handler)
70 SnapWidgetCommon.snap_to_update = False
72 SnapUtilities.snapwidgets.append(self)
74 def end_snapwidget(self):
75 if self in SnapUtilities.snapwidgets:
76 SnapUtilities.snapwidgets.remove(self)
78 #from .snap_context_l import global_snap_context_get
79 #sctx = global_snap_context_get(None, None, None)
81 sctx = object.__getattribute__(self, 'sctx')
82 if sctx and not SnapUtilities.snapwidgets:
83 sctx.clear_snap_objects()
85 handler = object.__getattribute__(self, 'handler')
86 bpy.app.handlers.depsgraph_update_post.remove(handler)
88 def update_snap(self, context, mval):
89 if self.last_mval == mval:
90 return -1
91 else:
92 self.last_mval = mval
94 if (SnapWidgetCommon.snap_to_update):
95 ## Something has changed since the last time.
96 # Has the mesh been changed?
97 # In the doubt lets clear the snap context.
98 self.snap_context_update(context)
99 SnapWidgetCommon.snap_to_update = False
101 #print('test_select', mval)
102 space = context.space_data
103 self.sctx.update_viewport_context(context.evaluated_depsgraph_get(), context.region, space, True)
105 shading = space.shading
106 snap_face = not ((self.snap_vert or self.snap_edge) and
107 (shading.show_xray or shading.type == 'WIREFRAME'))
109 if snap_face != self.snap_face:
110 self.snap_face = snap_face
111 self.sctx.set_snap_mode(self.snap_vert, self.snap_edge, self.snap_face)
113 snap_utilities.cache.clear()
114 self.snap_obj, prev_loc, self.location, self.type, self.bm, self.geom, len = snap_utilities(
115 self.sctx,
116 None,
117 mval,
118 increment=self.incremental
122 class SnapPointWidget(SnapWidgetCommon):
123 bl_idname = "VIEW3D_GT_snap_point"
125 __slots__ = ()
127 def test_select(self, context, mval):
128 if not self.inited:
129 self.init_snapwidget(context)
131 self.update_snap(context, mval)
132 self.snap_to_grid()
134 context.area.tag_redraw()
135 return -1
137 def draw(self, context):
138 if self.inited:
139 self.draw_point_and_elem()
141 def setup(self):
142 self.init_delayed()
145 def context_mode_check(context, widget_group):
146 tools = context.workspace.tools
147 mode = context.mode
148 for tool in tools:
149 if (tool.widget == widget_group) and (tool.mode == mode):
150 break
151 else:
152 context.window_manager.gizmo_group_type_unlink_delayed(widget_group)
153 return False
154 return True
157 class SnapWidgetGroupCommon(bpy.types.GizmoGroup):
158 bl_space_type = 'VIEW_3D'
159 bl_region_type = 'WINDOW'
160 bl_options = {'3D'}
162 @classmethod
163 def poll(cls, context):
164 return context_mode_check(context, cls.bl_idname)
166 def init_tool(self, context, gizmo_name):
167 self._widget = self.gizmos.new(gizmo_name)
169 def __del__(self):
170 if hasattr(self, "_widget"):
171 object.__getattribute__(self._widget, 'end_snapwidget')()
174 class SnapPointWidgetGroup(SnapWidgetGroupCommon):
175 bl_idname = "MESH_GGT_snap_point"
176 bl_label = "Draw Snap Point"
178 def setup(self, context):
179 self.init_tool(context, SnapPointWidget.bl_idname)