From f7c0cf286d61584a8ca53ab7905fc9ab21b4acc5 Mon Sep 17 00:00:00 2001 From: Ryan Inch Date: Fri, 21 Apr 2023 04:39:20 -0400 Subject: [PATCH 1/3] initial test of text previews --- .../components/definitions/text.py | 134 +++++++++++++++++- addons/io_hubs_addon/components/gizmos.py | 1 + 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/addons/io_hubs_addon/components/definitions/text.py b/addons/io_hubs_addon/components/definitions/text.py index 4bf17dff..231ef848 100644 --- a/addons/io_hubs_addon/components/definitions/text.py +++ b/addons/io_hubs_addon/components/definitions/text.py @@ -1,8 +1,109 @@ +import bpy, blf, gpu from bpy.props import FloatProperty, EnumProperty, FloatVectorProperty, StringProperty +from bpy.types import Gizmo +from math import radians +from mathutils import Matrix, Vector from ..hubs_component import HubsComponent from ..types import Category, PanelType, NodeType +from ..gizmos import bone_matrix_world + +class TextGizmo(Gizmo): + """Text gizmo""" + bl_idname = "GIZMO_GT_hba_text_gizmo" + + anchorX_map = { + "left": 0, + "center": -0.5, + "right":-1 + } + + breaker_map = { + "normal": " ", + "break-word": "" + } + + whitespace_map = { + "normal": True, + "nowrap": False + } + + def __init__(self): + self.component = None + self.host = None + + def _update_matrix(self): + gpu.matrix.push() + text_size = self.component.fontSize * 0.009 + rot_offset = Matrix.Rotation(radians(90), 4, 'X').to_4x4() + loc, rot, scale = self.host.matrix_basis.decompose() + scale = scale * text_size + print("scale: ", scale) + mat_out = (Matrix.Translation(loc) + @ rot.normalized().to_matrix().to_4x4() @ rot_offset + @ Matrix.Diagonal(scale).to_4x4()) + gpu.matrix.multiply_matrix(mat_out) + + def draw(self, context): + self._update_matrix() + + text_size = self.component.fontSize * 9 + + text = self.component.value + text_color = self.component.color[:3] + font_id = 0 + blf.size(font_id, 100, 72) + anchor = self.anchorX_map[self.component.anchorX] + line = [] + line_count = 0 + line_count_modifier = (self.component.lineHeight * 100) * -1 + breaker = self.breaker_map[self.component.overflowWrap] + text_split = text.split(breaker) if breaker == " " else list(text) + + block_w = 0 + #block_blueprint = {} + + if self.whitespace_map[self.component.whiteSpace]: + for word in text_split: + line.append(word) + text_w, text_h = blf.dimensions(font_id, breaker.join(line)) + print("mwc: ", text_w * text_size) + if text_w * text_size > self.component.maxWidth * 1000: + print("w: ", text_w) + new_line = [line.pop()] + text_w, text_h = blf.dimensions(font_id, breaker.join(line)) + block_w = text_w if not block_w else block_w + # block_blueprint[line_count] = { + # "line":breaker.join(line), + # "position": (anchor * block_w, line_count * line_count_modifier) + # } + blf.position(font_id, anchor * block_w, line_count * line_count_modifier, 0) + blf.color(font_id, text_color[0], text_color[1], text_color[2], 1) + blf.draw(font_id, breaker.join(line)) + line = new_line + line_count += 1 + + if line: + text_w, text_h = blf.dimensions(font_id, breaker.join(line)) + block_w = text_w if not block_w else block_w + print("w: ", text_w) + # block_blueprint[line_count] = { + # "line":breaker.join(line), + # "position": (anchor * block_w, line_count * line_count_modifier) + # } + blf.position(font_id, anchor * block_w, line_count * line_count_modifier, 0) + blf.color(font_id, text_color[0], text_color[1], text_color[2], 1) + blf.draw(font_id, breaker.join(line)) + + else: + text_w, text_h = blf.dimensions(font_id, text) + blf.position(font_id, anchor * text_w, 0, 0) + blf.color(font_id, text_color[0], text_color[1], text_color[2], 1) + blf.draw(font_id, text) + + gpu.matrix.pop() + class Text(HubsComponent): _definition = { 'name': 'text', @@ -73,7 +174,7 @@ class Text(HubsComponent): lineHeight: FloatProperty( name="Line Height", description="Sets the height of each line of text. If 0, a reasonable height based on the chosen font's ascender/descender metrics will be used, otherwise it is interpreted as a multiple of the fontSize", - default=0.0) + default=1.0) outlineWidth: StringProperty( name="Outline Width", @@ -186,3 +287,34 @@ class Text(HubsComponent): ("ltr", "Left to Right", "Order text left to right"), ("rtl", "Right to Left", "Order text right to left")], default="auto") + + @staticmethod + def register(): + bpy.utils.register_class(TextGizmo) + + @staticmethod + def unregister(): + bpy.utils.unregister_class(TextGizmo) + + @classmethod + def update_gizmo(cls, ob, bone, target, gizmo): + return + if bone: + mat = bone_matrix_world(ob, bone) + else: + mat = ob.matrix_world.copy() + + gizmo.hide = not ob.visible_get() + gizmo.matrix_basis = mat + + @classmethod + def create_gizmo(cls, ob, gizmo_group): + component = getattr(ob, cls.get_id()) + gizmo = gizmo_group.gizmos.new(TextGizmo.bl_idname) + gizmo.component = component + gizmo.host = ob + gizmo.use_draw_scale = False + gizmo.use_draw_modal = False + + return gizmo + diff --git a/addons/io_hubs_addon/components/gizmos.py b/addons/io_hubs_addon/components/gizmos.py index 5e3fc75b..29f26d30 100644 --- a/addons/io_hubs_addon/components/gizmos.py +++ b/addons/io_hubs_addon/components/gizmos.py @@ -5,6 +5,7 @@ from bpy.app.handlers import persistent from math import radians from mathutils import Matrix +import gpu def gizmo_update(obj, gizmo): From 032213129580b227a02ab6b57d7ed6bf5e8e7243 Mon Sep 17 00:00:00 2001 From: Ryan Inch Date: Fri, 21 Apr 2023 05:27:16 -0400 Subject: [PATCH 2/3] add anchorY test support --- .../components/definitions/text.py | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/addons/io_hubs_addon/components/definitions/text.py b/addons/io_hubs_addon/components/definitions/text.py index 231ef848..b56ac88a 100644 --- a/addons/io_hubs_addon/components/definitions/text.py +++ b/addons/io_hubs_addon/components/definitions/text.py @@ -39,7 +39,6 @@ def _update_matrix(self): rot_offset = Matrix.Rotation(radians(90), 4, 'X').to_4x4() loc, rot, scale = self.host.matrix_basis.decompose() scale = scale * text_size - print("scale: ", scale) mat_out = (Matrix.Translation(loc) @ rot.normalized().to_matrix().to_4x4() @ rot_offset @ Matrix.Diagonal(scale).to_4x4()) @@ -62,7 +61,7 @@ def draw(self, context): text_split = text.split(breaker) if breaker == " " else list(text) block_w = 0 - #block_blueprint = {} + block_blueprint = {} if self.whitespace_map[self.component.whiteSpace]: for word in text_split: @@ -74,13 +73,10 @@ def draw(self, context): new_line = [line.pop()] text_w, text_h = blf.dimensions(font_id, breaker.join(line)) block_w = text_w if not block_w else block_w - # block_blueprint[line_count] = { - # "line":breaker.join(line), - # "position": (anchor * block_w, line_count * line_count_modifier) - # } - blf.position(font_id, anchor * block_w, line_count * line_count_modifier, 0) - blf.color(font_id, text_color[0], text_color[1], text_color[2], 1) - blf.draw(font_id, breaker.join(line)) + block_blueprint[line_count] = { + "line_text":breaker.join(line), + "position": (anchor * block_w, line_count * line_count_modifier) + } line = new_line line_count += 1 @@ -88,13 +84,26 @@ def draw(self, context): text_w, text_h = blf.dimensions(font_id, breaker.join(line)) block_w = text_w if not block_w else block_w print("w: ", text_w) - # block_blueprint[line_count] = { - # "line":breaker.join(line), - # "position": (anchor * block_w, line_count * line_count_modifier) - # } - blf.position(font_id, anchor * block_w, line_count * line_count_modifier, 0) + block_blueprint[line_count] = { + "line_text":breaker.join(line), + "position": (anchor * block_w, line_count * line_count_modifier) + } + + print(block_blueprint) + anchorY_map = { + "top": line_count_modifier, + "top-baseline": 0, + "middle": [line["position"][1] for line in block_blueprint.values()][-1] * 0.5 * -1 + (line_count_modifier / 4), + "bottom-baseline": [line["position"][1] for line in block_blueprint.values()][-1] * -1, + "bottom": [line["position"][1] for line in block_blueprint.values()][-1] * -1 - (line_count_modifier / 2) + } + + offset = anchorY_map[self.component.anchorY] + + for line in block_blueprint.values(): + blf.position(font_id, line["position"][0], line["position"][1] + offset, 0) blf.color(font_id, text_color[0], text_color[1], text_color[2], 1) - blf.draw(font_id, breaker.join(line)) + blf.draw(font_id, line["line_text"]) else: text_w, text_h = blf.dimensions(font_id, text) From f6576b51322712d881958592c6bd5f2b7bc19deb Mon Sep 17 00:00:00 2001 From: Ryan Inch Date: Fri, 21 Apr 2023 05:40:41 -0400 Subject: [PATCH 3/3] formatting --- .../components/definitions/text.py | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/addons/io_hubs_addon/components/definitions/text.py b/addons/io_hubs_addon/components/definitions/text.py index b56ac88a..cecf9321 100644 --- a/addons/io_hubs_addon/components/definitions/text.py +++ b/addons/io_hubs_addon/components/definitions/text.py @@ -1,4 +1,6 @@ -import bpy, blf, gpu +import bpy +import blf +import gpu from bpy.props import FloatProperty, EnumProperty, FloatVectorProperty, StringProperty from bpy.types import Gizmo from math import radians @@ -8,7 +10,6 @@ from ..gizmos import bone_matrix_world - class TextGizmo(Gizmo): """Text gizmo""" bl_idname = "GIZMO_GT_hba_text_gizmo" @@ -16,18 +17,18 @@ class TextGizmo(Gizmo): anchorX_map = { "left": 0, "center": -0.5, - "right":-1 - } + "right": -1 + } breaker_map = { "normal": " ", "break-word": "" - } + } whitespace_map = { "normal": True, "nowrap": False - } + } def __init__(self): self.component = None @@ -40,8 +41,8 @@ def _update_matrix(self): loc, rot, scale = self.host.matrix_basis.decompose() scale = scale * text_size mat_out = (Matrix.Translation(loc) - @ rot.normalized().to_matrix().to_4x4() @ rot_offset - @ Matrix.Diagonal(scale).to_4x4()) + @ rot.normalized().to_matrix().to_4x4() @ rot_offset + @ Matrix.Diagonal(scale).to_4x4()) gpu.matrix.multiply_matrix(mat_out) def draw(self, context): @@ -74,9 +75,9 @@ def draw(self, context): text_w, text_h = blf.dimensions(font_id, breaker.join(line)) block_w = text_w if not block_w else block_w block_blueprint[line_count] = { - "line_text":breaker.join(line), + "line_text": breaker.join(line), "position": (anchor * block_w, line_count * line_count_modifier) - } + } line = new_line line_count += 1 @@ -85,18 +86,18 @@ def draw(self, context): block_w = text_w if not block_w else block_w print("w: ", text_w) block_blueprint[line_count] = { - "line_text":breaker.join(line), - "position": (anchor * block_w, line_count * line_count_modifier) - } + "line_text": breaker.join(line), + "position": (anchor * block_w, line_count * line_count_modifier) + } print(block_blueprint) anchorY_map = { - "top": line_count_modifier, - "top-baseline": 0, - "middle": [line["position"][1] for line in block_blueprint.values()][-1] * 0.5 * -1 + (line_count_modifier / 4), + "top": line_count_modifier, "top-baseline": 0, + "middle": [line["position"][1] for line in block_blueprint.values()][-1] * 0.5 * -1 + + (line_count_modifier / 4), "bottom-baseline": [line["position"][1] for line in block_blueprint.values()][-1] * -1, - "bottom": [line["position"][1] for line in block_blueprint.values()][-1] * -1 - (line_count_modifier / 2) - } + "bottom": [line["position"][1] for line in block_blueprint.values()][-1] * -1 - + (line_count_modifier / 2)} offset = anchorY_map[self.component.anchorY] @@ -113,6 +114,7 @@ def draw(self, context): gpu.matrix.pop() + class Text(HubsComponent): _definition = { 'name': 'text', @@ -326,4 +328,3 @@ def create_gizmo(cls, ob, gizmo_group): gizmo.use_draw_modal = False return gizmo -