Skip to content

Commit

Permalink
Merge pull request #3074 from t3du/DrawStringText
Browse files Browse the repository at this point in the history
Draw string and text nodes: added angle and output parameters
  • Loading branch information
luboslenco authored Nov 22, 2024
2 parents 88af401 + c0d0afa commit 0622385
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
15 changes: 14 additions & 1 deletion Sources/armory/logicnode/DrawStringNode.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import armory.ui.Canvas;
class DrawStringNode extends LogicNode {
var font: Font;
var lastFontName = "";
var string:String;

public function new(tree: LogicTree) {
super(tree);
Expand All @@ -19,7 +20,9 @@ class DrawStringNode extends LogicNode {
override function run(from: Int) {
RenderToTexture.ensure2DContext("DrawStringNode");

var string:String = Std.string(inputs[1].get());
string = Std.string(inputs[1].get());
var angle: Float = inputs[7].get();

var fontName = inputs[2].get();
if (fontName == "") {
#if arm_ui
Expand All @@ -42,6 +45,8 @@ class DrawStringNode extends LogicNode {
return;
}

RenderToTexture.g.rotate(angle, inputs[5].get(), inputs[6].get());

final colorVec = inputs[4].get();
RenderToTexture.g.color = Color.fromFloats(colorVec.x, colorVec.y, colorVec.z, colorVec.w);

Expand All @@ -50,6 +55,14 @@ class DrawStringNode extends LogicNode {

RenderToTexture.g.drawString(string, inputs[5].get(), inputs[6].get());

RenderToTexture.g.rotate(-angle, inputs[5].get(), inputs[6].get());

runOutput(0);
}

override function get(from: Int): Dynamic {

return from == 1 ? RenderToTexture.g.font.height(RenderToTexture.g.fontSize) : RenderToTexture.g.font.width(RenderToTexture.g.fontSize, string);

}
}
18 changes: 15 additions & 3 deletions Sources/armory/logicnode/DrawTextAreaStringNode.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DrawTextAreaStringNode extends LogicNode {

var index: Int;
var max: Int;
var ar_lines: Array<String>;

public function new(tree: LogicTree) {
super(tree);
Expand All @@ -34,11 +35,11 @@ class DrawTextAreaStringNode extends LogicNode {

var string:String = Std.string(inputs[1].get());
var length:Int = inputs[3].get();
var angle: Float = inputs[10].get();

var horA = TextLeft;
var verA = TextTop;


var fontName = inputs[2].get();
if (fontName == "") {
#if arm_ui
Expand All @@ -63,7 +64,7 @@ class DrawTextAreaStringNode extends LogicNode {

var len = string.length;

var ar_lines = [];
ar_lines = [];

var ar_words = string.split(' ');

Expand Down Expand Up @@ -115,6 +116,8 @@ class DrawTextAreaStringNode extends LogicNode {
case 'TextRight': {horA = TextRight; xoffset = -width; }
}

RenderToTexture.g.rotate(angle, inputs[8].get(), inputs[9].get()+(ar_lines.length-1)/2*height*spacing);

RenderToTexture.g.color = Color.fromFloats(colorVecB.x, colorVecB.y, colorVecB.z, colorVecB.w);

RenderToTexture.g.fillRect(inputs[8].get()+xoffset, inputs[9].get()+yoffset+index*height*spacing, width, height);
Expand All @@ -124,9 +127,18 @@ class DrawTextAreaStringNode extends LogicNode {
RenderToTexture.g.drawAlignedString(line, inputs[8].get(), inputs[9].get()+index*height*spacing, horA, verA);
++index;

RenderToTexture.g.rotate(-angle, inputs[8].get(), inputs[9].get()+(ar_lines.length-1)/2*height*spacing);

}

#end

runOutput(0);
}
}

override function get(from: Int): Dynamic {

return ar_lines.length;

}
}
12 changes: 11 additions & 1 deletion blender/arm/logicnode/draw/LN_draw_Text_Area_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class DrawTextAreaStringNode(ArmLogicTreeNode):
@input Color Font: The color of the string, supports alpha.
@input Color Background: The color background of the text area, supports alpha, if no color is wanted used alpha 0.
@input X/Y: Position of the string, in pixels from the top left corner.
@input Angle: Rotation angle in radians. Rectangle will be rotated cloclwiswe
at the anchor point.
@see [`kha.graphics2.Graphics.drawString()`](http://kha.tech/api/kha/graphics2/Graphics.html#drawString).
"""
bl_idname = 'LNDrawTextAreaStringNode'
bl_label = 'Draw Text Area String'
arm_section = 'draw'
arm_version = 1
arm_version = 3

property0: HaxeEnumProperty(
'property0',
Expand Down Expand Up @@ -58,10 +60,18 @@ def arm_init(self, context):
self.add_input('ArmColorSocket', 'Color Background', default_value=[0.0, 0.0, 0.0, 1.0])
self.add_input('ArmFloatSocket', 'X')
self.add_input('ArmFloatSocket', 'Y')
self.add_input('ArmFloatSocket', 'Angle')

self.add_output('ArmNodeSocketAction', 'Out')
self.add_output('ArmIntSocket', 'Lines')

def draw_buttons(self, context, layout):
layout.prop(self, 'property0')
layout.prop(self, 'property1')
layout.prop(self, 'property2')

def get_replacement_node(self, node_tree: bpy.types.NodeTree):
if self.arm_version not in (0, 2):
raise LookupError()

return NodeReplacement.Identity(self)
15 changes: 14 additions & 1 deletion blender/arm/logicnode/draw/LN_draw_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ class DrawStringNode(ArmLogicTreeNode):
@input Font Size: The size of the font in pixels.
@input Color: The color of the string.
@input X/Y: Position of the string, in pixels from the top left corner.
@input Angle: Rotation angle in radians. Rectangle will be rotated cloclwiswe
at the anchor point.
@output Out: Activated after the string has been drawn.
@output Height: String Height.
@output Width: String Width.
@see [`kha.graphics2.Graphics.drawString()`](http://kha.tech/api/kha/graphics2/Graphics.html#drawString).
"""
bl_idname = 'LNDrawStringNode'
bl_label = 'Draw String'
arm_section = 'draw'
arm_version = 1
arm_version = 2

def arm_init(self, context):
self.add_input('ArmNodeSocketAction', 'Draw')
Expand All @@ -31,5 +35,14 @@ def arm_init(self, context):
self.add_input('ArmColorSocket', 'Color', default_value=[1.0, 1.0, 1.0, 1.0])
self.add_input('ArmFloatSocket', 'X')
self.add_input('ArmFloatSocket', 'Y')
self.add_input('ArmFloatSocket', 'Angle')

self.add_output('ArmNodeSocketAction', 'Out')
self.add_output('ArmFloatSocket', 'Height')
self.add_output('ArmFloatSocket', 'Width')

def get_replacement_node(self, node_tree: bpy.types.NodeTree):
if self.arm_version not in (0, 1):
raise LookupError()

return NodeReplacement.Identity(self)

0 comments on commit 0622385

Please sign in to comment.