forked from hlorus/CAD_Sketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_handler.py
116 lines (82 loc) · 3.1 KB
/
draw_handler.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
import logging
import bpy
import gpu
from bpy.types import Context, Operator
from bpy.utils import register_class, unregister_class
from . import global_data
from .utilities.preferences import use_experimental
from .declarations import Operators
logger = logging.getLogger(__name__)
def draw_selection_buffer(context: Context):
"""Draw elements offscreen"""
region = context.region
# create offscreen
width, height = region.width, region.height
offscreen = global_data.offscreen = gpu.types.GPUOffScreen(width, height)
with offscreen.bind():
fb = gpu.state.active_framebuffer_get()
fb.clear(color=(0.0, 0.0, 0.0, 0.0))
entities = list(context.scene.sketcher.entities.all)
for e in reversed(entities):
if e.slvs_index in global_data.ignore_list:
continue
if not hasattr(e, "draw_id"):
continue
if not e.is_selectable(context):
continue
e.draw_id(context)
def ensure_selection_texture(context: Context):
if not global_data.redraw_selection_buffer:
return
draw_selection_buffer(context)
global_data.redraw_selection_buffer = False
def update_elements(context: Context, force: bool = False):
"""
TODO: Avoid to always update batches and selection texture
"""
entities = list(context.scene.sketcher.entities.all)
for e in entities:
if not hasattr(e, "update"):
continue
if not force and not e.is_dirty:
continue
e.update()
def _get_msg():
msg = "Update geometry batches:"
for e in entities:
if not e.is_dirty:
continue
msg += "\n - " + str(e)
return msg
if logger.isEnabledFor(logging.DEBUG):
logger.debug(_get_msg())
def draw_elements(context: Context):
for entity in reversed(list(context.scene.sketcher.entities.all)):
if hasattr(entity, "draw"):
entity.draw(context)
def draw_cb():
context = bpy.context
force = use_experimental("force_redraw", True)
update_elements(context, force=force)
draw_elements(context)
global_data.redraw_selection_buffer = True
class View3D_OT_slvs_register_draw_cb(Operator):
bl_idname = Operators.RegisterDrawCB
bl_label = "Register Draw Callback"
def execute(self, context: Context):
global_data.draw_handle = bpy.types.SpaceView3D.draw_handler_add(
draw_cb, (), "WINDOW", "POST_VIEW"
)
return {"FINISHED"}
class View3D_OT_slvs_unregister_draw_cb(Operator):
bl_idname = Operators.UnregisterDrawCB
bl_label = ""
def execute(self, context: Context):
global_data.draw_handler.remove_handle()
return {"FINISHED"}
def register():
register_class(View3D_OT_slvs_register_draw_cb)
register_class(View3D_OT_slvs_unregister_draw_cb)
def unregister():
unregister_class(View3D_OT_slvs_unregister_draw_cb)
unregister_class(View3D_OT_slvs_register_draw_cb)