-
Notifications
You must be signed in to change notification settings - Fork 10
/
BigTextNode.py
183 lines (157 loc) · 6.04 KB
/
BigTextNode.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
import bpy
import blf
from bpy.types import Node
from . MindmapNodeBase import MindmapTreeNode
def draw_callback_px(self, context):
space = context.space_data
editor = space.type
if space and editor == 'NODE_EDITOR':
if space.node_tree:
nodes = space.node_tree.nodes
if self.node_name in nodes:
node = nodes[self.node_name]
font_id = node.my_font
text_size = node.my_text_size
position = node.location
big_text = node.my_title_prop
color = node.my_node_color
shadow = node.shadow_color
blf.color(font_id, *color, 1)
blf.enable(font_id, blf.SHADOW)
blf.shadow(font_id, 3, *shadow)
blf.shadow_offset(font_id, 3, -3)
for region in context.area.regions:
if region.type == 'WINDOW':
view = region.view2d
draw_position = view.view_to_region(*position)
x1, y1 = view.region_to_view(0, 0)
x2, y2, = view.region_to_view(
region.width,
region.height
)
view_width = x2 - x1
view_height = y2 - y1
scale_x = region.width / view_width
scale_y = region.height / view_height
blf.position(
font_id,
(draw_position[0] + (node.width * scale_x)),
(draw_position[1] - ((node.height / 4) * scale_y)),
0
)
blf.size(font_id, text_size * scale_x)
blf.draw(font_id, big_text)
blf.disable(font_id, blf.SHADOW)
# BigText Node
class BigTextNode(Node, MindmapTreeNode):
'''A Big Text only node'''
# Optional. If not explicitly defined, the python class name is used.
bl_idname = 'BigTextNodeType'
# Label for nice name display
bl_label = "BigText"
# Icon identifier
bl_icon = 'NONE' # 'OUTLINER_OB_FONT'
# === Custom Properties ===
my_title_prop: bpy.props.StringProperty(
name='',
default='BigText' # noqa: F821
)
my_node_color: bpy.props.FloatVectorProperty(
name='',
default=(0.6, 0, 0),
min=0.0,
max=1.0,
subtype='COLOR', # noqa: F821
)
shadow_color: bpy.props.FloatVectorProperty(
name='',
size=4,
default=(0, 0, 0, 0.5),
min=0.0,
max=1.0,
subtype='COLOR', # noqa: F821
)
my_text_size: bpy.props.IntProperty(
name='Size', # noqa: F821
default=50,
soft_min=4,
soft_max=300,
)
my_font: bpy.props.IntProperty(
name='Font ID',
default=0,
soft_min=0,
soft_max=5,
)
_handle = None
# === Optional Functions ===
# Initialization function, called when a new node is created.
def init(self, context):
self.use_custom_color = True
self.color = (0.1, 0.1, 0.1)
self.width = 20
self.hide = True
bpy.ops.node.draw_big_text(
'INVOKE_DEFAULT',
node_name=self.name,
draw_text=self.my_title_prop
)
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
bpy.ops.node.draw_big_text(
'INVOKE_DEFAULT',
node_name=self.name,
draw_text=self.my_title_prop
)
# Free function to clean up on removal.
def free(self):
if self._handle:
bpy.context.window_manager.modal_handler_remove(self._handle)
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
column = layout.column()
column.prop(self, "my_title_prop", icon='GREASEPENCIL')
column.prop(self, "my_node_color", icon='MATERIAL')
column.prop(self, "my_text_size", icon='TEXT')
column.prop(self, "my_font", icon='OUTLINER_OB_FONT')
column.prop(self, "shadow_color", icon='MATERIAL')
# Detail buttons in the sidebar.
# If function is not defined, the draw_buttons function is used instead
def draw_buttons_ext(self, context, layout):
column = layout.column()
column.prop(self, "my_title_prop", icon='GREASEPENCIL')
column.prop(self, "my_node_color", icon='MATERIAL')
column.prop(self, "my_text_size", icon='TEXT')
column.prop(self, "my_font", icon='OUTLINER_OB_FONT')
column.prop(self, "shadow_color", icon='MATERIAL')
def update(self):
self.my_title_prop = self.my_title_prop
class DrawBigTextOperator(bpy.types.Operator):
bl_idname = "node.draw_big_text"
bl_label = "Draw Big Text Operator"
node_name: bpy.props.StringProperty()
draw_text: bpy.props.StringProperty()
def modal(self, context, event):
if context.space_data is not None:
if context.space_data.type == 'NODE_EDITOR':
context.area.tag_redraw()
return {'PASS_THROUGH'} # do not block execution
else:
return {'PASS_THROUGH'}
def invoke(self, context, event):
if context.space_data.type == 'NODE_EDITOR':
self._handle = context.space_data.draw_handler_add(
draw_callback_px,
(self, context),
'WINDOW',
'POST_PIXEL' # BACKDROP or POST_PIXEL
)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
elif self._handle:
context.space_data.draw_handler_remove(self._handle, 'WINDOW')
return {'FINISHED'}
else:
return {'CANCELLED'}