diff --git a/nodes/viz/console_node.py b/nodes/viz/console_node.py index 56015db067e900123d4f0887ce0a576abd570209..f5dc6915751b06baaab06382863fe7f1143cf9c9 100644 --- a/nodes/viz/console_node.py +++ b/nodes/viz/console_node.py @@ -26,6 +26,10 @@ from sverchok.utils.sv_update_utils import sv_get_local_path from sverchok.utils.sv_font_xml_parser import get_lookup_dict, letters_to_uv from sverchok.utils.sv_nodeview_draw_helper import SvNodeViewDrawMixin, get_console_grid +def get_desired_xy(node): + x, y = node.xy_offset + return x * node.location_theta, y * node.location_theta + def make_color(name, default): return bpy.props.FloatVectorProperty(name=name, default=default, size=4, min=0, max=1, update=updateNode, subtype="COLOR") @@ -35,12 +39,44 @@ bitmap_font_location = os.path.join(sv_path, 'utils', 'modules', 'bitmap_font') lookup_dict_data = {} +no_vertex_shader = ''' + uniform mat4 ModelViewProjectionMatrix; + + in vec2 texCoord; + in vec2 pos; + uniform float x_offset; + uniform float y_offset; + + out vec2 texCoord_interp; + + void main() + { + gl_Position = ModelViewProjectionMatrix * vec4(pos.x + x_offset, pos.y + y_offset, 0.0f, 1.0f); + gl_Position.z = 1.0; + texCoord_interp = texCoord; + } +''' +no_fragment_shader = ''' + in vec2 texCoord_interp; + + out vec4 fragColor; + + uniform sampler2D image; + + void main() + { + fragColor = texture(image, texCoord_interp); + } +''' + vertex_shader = ''' uniform mat4 ModelViewProjectionMatrix; in vec2 texCoord; in vec2 pos; in float lexer; + uniform float x_offset; + uniform float y_offset; out float v_lexer; out vec2 texCoord_interp; @@ -48,7 +84,7 @@ vertex_shader = ''' void main() { v_lexer = lexer; - gl_Position = ModelViewProjectionMatrix * vec4(pos.xy, 0.0f, 1.0f); + gl_Position = ModelViewProjectionMatrix * vec4(pos.x + x_offset, pos.y + y_offset, 0.0f, 1.0f); gl_Position.z = 1.0; texCoord_interp = texCoord; } @@ -312,7 +348,7 @@ def terminal_text_to_uv(lines): uvs.extend(letters_to_uv(line, fnt)) return uvs -def simple_console_xy(context, args): +def simple_console_xy(context, args, loc): texture, config = args act_tex = bgl.Buffer(bgl.GL_INT, 1) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.texture_dict['texture']) @@ -327,17 +363,18 @@ def simple_console_xy(context, args): for color_name, color_value in config.colors.items(): config.shader.uniform_float(color_name, color_value) + x, y = loc + config.shader.uniform_float("x_offset", x) + config.shader.uniform_float("y_offset", y) config.shader.uniform_int("image", act_tex) config.batch.draw(config.shader) -def process_grid_for_shader(grid, loc): - x, y = loc +def process_grid_for_shader(grid): positions, poly_indices = grid - translated_positions = [(p[0] + x, p[1] + y) for p in positions] verts = [] for poly in poly_indices: for v_idx in poly: - verts.append(translated_positions[v_idx]) + verts.append(positions[v_idx][:2]) return verts def process_uvs_for_shader(node): @@ -353,7 +390,7 @@ def generate_batch_shader(node, data): # print("len(verts)", len(verts), "len(uv_indices)", len(uv_indices)) if node.syntax_mode == "None": - shader = gpu.shader.from_builtin('2D_IMAGE') + shader = gpu.types.GPUShader(no_vertex_shader, no_fragment_shader) batch = batch_for_shader(shader, 'TRIS', {"pos": verts, "texCoord": uv_indices}) elif node.syntax_mode == "Code": shader = gpu.types.GPUShader(vertex_shader, lexed_fragment_shader) @@ -544,12 +581,11 @@ class SvConsoleNode(bpy.types.Node, SverchCustomTreeNode, SvNodeViewDrawMixin): lexer = self.get_lexer() grid = self.prepare_for_grid() - x, y, width, height = self.adjust_position_and_dimensions(*self.dims) - verts = process_grid_for_shader(grid, loc=(x, y)) + self.adjust_position_and_dimensions(*self.dims) + verts = process_grid_for_shader(grid) uvs = process_uvs_for_shader(self) batch, shader = generate_batch_shader(self, (verts, uvs, lexer)) - config.loc = (x, y) config.batch = batch config.shader = shader config.syntax_mode = self.syntax_mode @@ -561,6 +597,8 @@ class SvConsoleNode(bpy.types.Node, SverchCustomTreeNode, SvNodeViewDrawMixin): draw_data = { 'tree_name': self.id_data.name[:], + 'node_name': self.name[:], + 'loc': get_desired_xy, 'mode': 'custom_function_context', 'custom_function': simple_console_xy, 'args': (texture, config) @@ -597,15 +635,5 @@ class SvConsoleNode(bpy.types.Node, SverchCustomTreeNode, SvNodeViewDrawMixin): if not self.inputs[0].is_linked or not self.inputs[0].sv_get(): return True - def sv_update(self): - if not ("text" in self.inputs): - return - try: - if not self.inputs[0].other: - self.free() - except: - # print('ConsoleNode was disconnected, holdout (not a problem)') - pass - classes = [SvConsoleNode] register, unregister = bpy.utils.register_classes_factory(classes) diff --git a/ui/bgl_callback_nodeview.py b/ui/bgl_callback_nodeview.py index 8b2ef312418d844d2638fc29e530804c5544ad4c..cd2f08e03be2422756fea7e703939c5a18d1480f 100644 --- a/ui/bgl_callback_nodeview.py +++ b/ui/bgl_callback_nodeview.py @@ -80,6 +80,17 @@ def restore_opengl_defaults(): # bgl.glColor4f(0.0, 0.0, 0.0, 1.0) # doesn't exist anymore .. +def get_xy_from_data(data): + location = data.get('loc') + if isfunction(location): + x, y = get_sane_xy(data) + elif isinstance(location, (tuple, list)): + x, y = location + else: + x, y = 20, 20 + return x, y + + def get_sane_xy(data): return_value = (120, 120) location_function = data.get('loc') @@ -118,14 +129,7 @@ def draw_callback_px(n_id, data): elif data.get('mode') == 'custom_function': drawing_func = data.get('custom_function') - location = data.get('loc') - if isfunction(location): - x, y = get_sane_xy(data) - elif isinstance(location, (tuple, list)): - x, y = location - else: - x, y = 20, 20 - + x, y = get_xy_from_data(data) args = data.get('args', (None,)) drawing_func(x, y, args) @@ -139,12 +143,12 @@ def draw_callback_px(n_id, data): config = lambda: None config.shader_data = ... - config.loc = (x, y) (for node location..) geom = lambda: None geom.stuff = .. draw_data = { + 'loc': function_returning_xy, 'mode': 'custom_function_context', 'tree_name': self.id_data.name[:], 'custom_function': advanced_grid_xy, @@ -154,10 +158,12 @@ def draw_callback_px(n_id, data): nvBGL.callback_enable(self.n_id, draw_data) ''' + x, y = get_xy_from_data(data) + bgl.glEnable(bgl.GL_DEPTH_TEST) drawing_func = data.get('custom_function') args = data.get('args', (None,)) - drawing_func(bpy.context, args) + drawing_func(bpy.context, args, (x, y)) restore_opengl_defaults() bgl.glDisable(bgl.GL_DEPTH_TEST) diff --git a/utils/exception_drawing_with_bgl.py b/utils/exception_drawing_with_bgl.py index 772f41585f6044db306f48e663c0dc6724c01319..559716141d85784411f8766b9e732e339bed0ade 100644 --- a/utils/exception_drawing_with_bgl.py +++ b/utils/exception_drawing_with_bgl.py @@ -54,6 +54,8 @@ def clear_exception_drawing_with_bgl(nodes): nvBGL2.callback_disable(ng_id) + + def start_exception_drawing_with_bgl(ng, node_name, error_text, err): """ start drawing the exception data beside the node """ node = ng.nodes[node_name] @@ -67,25 +69,30 @@ def start_exception_drawing_with_bgl(ng, node_name, error_text, err): text.body = error_text x, y, scale = adjust_position_and_dimensions(node, xyoffset(node)) - config.loc = x, y config.scale = scale + def get_desired_xy(node): + nx, ny = xyoffset(node) + return nx * scale, ny * scale + ng_id = exception_nodetree_id(ng) draw_data = { 'tree_name': ng.name[:], + 'node_name': node_name, + 'loc': get_desired_xy, 'mode': 'custom_function_context', 'custom_function': simple_exception_display, 'args': (text, config) } nvBGL2.callback_enable(ng_id, draw_data) -def simple_exception_display(context, args): +def simple_exception_display(context, args, xy): """ a simple bgl/blf exception showing tool for nodeview """ text, config = args - x, y = config.loc + x, y = xy x, y = int(x), int(y) r, g, b = (1.0, 1.0, 1.0) font_id = 0 diff --git a/utils/sv_nodeview_draw_helper.py b/utils/sv_nodeview_draw_helper.py index 85a3b647c9455d162789ea4c264a84a206c00817..5cf932073692c016d2b4a3e22949c3dd81583f64 100644 --- a/utils/sv_nodeview_draw_helper.py +++ b/utils/sv_nodeview_draw_helper.py @@ -5,6 +5,8 @@ # SPDX-License-Identifier: GPL3 # License-Filename: LICENSE +import bpy + import numpy as np from mathutils import Matrix @@ -16,6 +18,8 @@ from sverchok.settings import get_params class SvNodeViewDrawMixin(): + location_theta: bpy.props.FloatProperty(name="location theta") + @property def xy_offset(self): a = self.absolute_location @@ -32,10 +36,7 @@ class SvNodeViewDrawMixin(): def adjust_position_and_dimensions(self, x, y, width, height): scale, multiplier = self.get_preferences() - x, y = [x * multiplier, y * multiplier] - width, height = [width * scale, height * scale] - return x, y, width, height - + self.location_theta = multiplier def faces_from_xy(ncx, ncy):