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
|
# SPDX-FileCopyrightText: 2023 Blender Foundation
#
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
from math import cos, sin, pi
from .nodes import get_nodes_links, prefs_line_width, abs_node_location, dpi_fac
def draw_line(x1, y1, x2, y2, size, colour=(1.0, 1.0, 1.0, 0.7)):
shader = gpu.shader.from_builtin('POLYLINE_SMOOTH_COLOR')
shader.uniform_float("viewportSize", gpu.state.viewport_get()[2:])
shader.uniform_float("lineWidth", size * prefs_line_width())
vertices = ((x1, y1), (x2, y2))
vertex_colors = ((colour[0] + (1.0 - colour[0]) / 4,
colour[1] + (1.0 - colour[1]) / 4,
colour[2] + (1.0 - colour[2]) / 4,
colour[3] + (1.0 - colour[3]) / 4),
colour)
batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices, "color": vertex_colors})
batch.draw(shader)
def draw_circle_2d_filled(mx, my, radius, colour=(1.0, 1.0, 1.0, 0.7)):
radius = radius * prefs_line_width()
sides = 12
vertices = [(radius * cos(i * 2 * pi / sides) + mx,
radius * sin(i * 2 * pi / sides) + my)
for i in range(sides + 1)]
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
shader.uniform_float("color", colour)
batch = batch_for_shader(shader, 'TRI_FAN', {"pos": vertices})
batch.draw(shader)
def draw_rounded_node_border(node, radius=8, colour=(1.0, 1.0, 1.0, 0.7)):
area_width = bpy.context.area.width
sides = 16
radius *= prefs_line_width()
nlocx, nlocy = abs_node_location(node)
nlocx = (nlocx + 1) * dpi_fac()
nlocy = (nlocy + 1) * dpi_fac()
ndimx = node.dimensions.x
ndimy = node.dimensions.y
if node.hide:
nlocx += -1
nlocy += 5
if node.type == 'REROUTE':
# nlocx += 1
nlocy -= 1
ndimx = 0
ndimy = 0
radius += 6
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
shader.uniform_float("color", colour)
# Top left corner
mx, my = bpy.context.region.view2d.view_to_region(nlocx, nlocy, clip=False)
vertices = [(mx, my)]
for i in range(sides + 1):
if (4 <= i <= 8):
if mx < area_width:
cosine = radius * cos(i * 2 * pi / sides) + mx
sine = radius * sin(i * 2 * pi / sides) + my
vertices.append((cosine, sine))
batch = batch_for_shader(shader, 'TRI_FAN', {"pos": vertices})
batch.draw(shader)
# Top right corner
mx, my = bpy.context.region.view2d.view_to_region(nlocx + ndimx, nlocy, clip=False)
vertices = [(mx, my)]
for i in range(sides + 1):
if (0 <= i <= 4):
if mx < area_width:
cosine = radius * cos(i * 2 * pi / sides) + mx
sine = radius * sin(i * 2 * pi / sides) + my
vertices.append((cosine, sine))
batch = batch_for_shader(shader, 'TRI_FAN', {"pos": vertices})
batch.draw(shader)
# Bottom left corner
mx, my = bpy.context.region.view2d.view_to_region(nlocx, nlocy - ndimy, clip=False)
vertices = [(mx, my)]
for i in range(sides + 1):
if (8 <= i <= 12):
if mx < area_width:
cosine = radius * cos(i * 2 * pi / sides) + mx
sine = radius * sin(i * 2 * pi / sides) + my
vertices.append((cosine, sine))
batch = batch_for_shader(shader, 'TRI_FAN', {"pos": vertices})
batch.draw(shader)
# Bottom right corner
mx, my = bpy.context.region.view2d.view_to_region(nlocx + ndimx, nlocy - ndimy, clip=False)
vertices = [(mx, my)]
for i in range(sides + 1):
if (12 <= i <= 16):
if mx < area_width:
cosine = radius * cos(i * 2 * pi / sides) + mx
sine = radius * sin(i * 2 * pi / sides) + my
vertices.append((cosine, sine))
batch = batch_for_shader(shader, 'TRI_FAN', {"pos": vertices})
batch.draw(shader)
# prepare drawing all edges in one batch
vertices = []
indices = []
id_last = 0
# Left edge
m1x, m1y = bpy.context.region.view2d.view_to_region(nlocx, nlocy, clip=False)
m2x, m2y = bpy.context.region.view2d.view_to_region(nlocx, nlocy - ndimy, clip=False)
if m1x < area_width and m2x < area_width:
vertices.extend([(m2x - radius, m2y), (m2x, m2y),
(m1x, m1y), (m1x - radius, m1y)])
indices.extend([(id_last, id_last + 1, id_last + 3),
(id_last + 3, id_last + 1, id_last + 2)])
id_last += 4
# Top edge
m1x, m1y = bpy.context.region.view2d.view_to_region(nlocx, nlocy, clip=False)
m2x, m2y = bpy.context.region.view2d.view_to_region(nlocx + ndimx, nlocy, clip=False)
m1x = min(m1x, area_width)
m2x = min(m2x, area_width)
vertices.extend([(m1x, m1y), (m2x, m1y),
(m2x, m1y + radius), (m1x, m1y + radius)])
indices.extend([(id_last, id_last + 1, id_last + 3),
(id_last + 3, id_last + 1, id_last + 2)])
id_last += 4
# Right edge
m1x, m1y = bpy.context.region.view2d.view_to_region(nlocx + ndimx, nlocy, clip=False)
m2x, m2y = bpy.context.region.view2d.view_to_region(nlocx + ndimx, nlocy - ndimy, clip=False)
if m1x < area_width and m2x < area_width:
vertices.extend([(m1x, m2y), (m1x + radius, m2y),
(m1x + radius, m1y), (m1x, m1y)])
indices.extend([(id_last, id_last + 1, id_last + 3),
(id_last + 3, id_last + 1, id_last + 2)])
id_last += 4
# Bottom edge
m1x, m1y = bpy.context.region.view2d.view_to_region(nlocx, nlocy - ndimy, clip=False)
m2x, m2y = bpy.context.region.view2d.view_to_region(nlocx + ndimx, nlocy - ndimy, clip=False)
m1x = min(m1x, area_width)
m2x = min(m2x, area_width)
vertices.extend([(m1x, m2y), (m2x, m2y),
(m2x, m1y - radius), (m1x, m1y - radius)])
indices.extend([(id_last, id_last + 1, id_last + 3),
(id_last + 3, id_last + 1, id_last + 2)])
# now draw all edges in one batch
if len(vertices) != 0:
batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)
batch.draw(shader)
def draw_callback_nodeoutline(self, context, mode):
if self.mouse_path:
gpu.state.blend_set('ALPHA')
nodes, _links = get_nodes_links(context)
if mode == "LINK":
col_outer = (1.0, 0.2, 0.2, 0.4)
col_inner = (0.0, 0.0, 0.0, 0.5)
col_circle_inner = (0.3, 0.05, 0.05, 1.0)
elif mode == "LINKMENU":
col_outer = (0.4, 0.6, 1.0, 0.4)
col_inner = (0.0, 0.0, 0.0, 0.5)
col_circle_inner = (0.08, 0.15, .3, 1.0)
elif mode == "MIX":
col_outer = (0.2, 1.0, 0.2, 0.4)
col_inner = (0.0, 0.0, 0.0, 0.5)
col_circle_inner = (0.05, 0.3, 0.05, 1.0)
m1x = self.mouse_path[0][0]
m1y = self.mouse_path[0][1]
m2x = self.mouse_path[-1][0]
m2y = self.mouse_path[-1][1]
n1 = nodes[context.scene.NWLazySource]
n2 = nodes[context.scene.NWLazyTarget]
if n1 == n2:
col_outer = (0.4, 0.4, 0.4, 0.4)
col_inner = (0.0, 0.0, 0.0, 0.5)
col_circle_inner = (0.2, 0.2, 0.2, 1.0)
draw_rounded_node_border(n1, radius=6, colour=col_outer) # outline
draw_rounded_node_border(n1, radius=5, colour=col_inner) # inner
draw_rounded_node_border(n2, radius=6, colour=col_outer) # outline
draw_rounded_node_border(n2, radius=5, colour=col_inner) # inner
draw_line(m1x, m1y, m2x, m2y, 5, col_outer) # line outline
draw_line(m1x, m1y, m2x, m2y, 2, col_inner) # line inner
# circle outline
draw_circle_2d_filled(m1x, m1y, 7, col_outer)
draw_circle_2d_filled(m2x, m2y, 7, col_outer)
# circle inner
draw_circle_2d_filled(m1x, m1y, 5, col_circle_inner)
draw_circle_2d_filled(m2x, m2y, 5, col_circle_inner)
gpu.state.blend_set('NONE')
|