Skip to content

Commit

Permalink
Merge pull request #340 from urbit-pilled/add_vector3
Browse files Browse the repository at this point in the history
Add vector3
  • Loading branch information
manuq authored Dec 17, 2024
2 parents 665abf0 + 5afe404 commit 28f102e
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 3 deletions.
20 changes: 20 additions & 0 deletions addons/block_code/blocks/math/vector3_multiply.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[gd_resource type="Resource" load_steps=2 format=3 uid="uid://bff7cwmpisihj"]

[ext_resource type="Script" path="res://addons/block_code/code_generation/block_definition.gd" id="1_52jwf"]

[resource]
script = ExtResource("1_52jwf")
name = &"vector3_multiply"
target_node_class = ""
description = "Multiplies a Vector3 with a number. Use this, for example, to get a point some distance away along an angle."
category = "Math"
type = 3
variant_type = 9
display_template = "multiply {vector: VECTOR3} by {number: FLOAT}"
code_template = "{vector} * {number}"
defaults = {
"number": 1.0,
"vector": Vector3(1, 1, 1)
}
signal_name = ""
scope = ""
19 changes: 19 additions & 0 deletions addons/block_code/blocks/math/vector3_x.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[gd_resource type="Resource" load_steps=2 format=3 uid="uid://cehtd3jqu5es2"]

[ext_resource type="Script" path="res://addons/block_code/code_generation/block_definition.gd" id="1_p3bft"]

[resource]
script = ExtResource("1_p3bft")
name = &"vector3_x"
target_node_class = ""
description = "Gives the x of a [i]Vector3[/i]"
category = "Math"
type = 3
variant_type = 3
display_template = "x of {vector3: VECTOR3}"
code_template = "{vector3}.x"
defaults = {
"vector3": Vector3(0, 0, 0)
}
signal_name = ""
scope = ""
19 changes: 19 additions & 0 deletions addons/block_code/blocks/math/vector3_y.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[gd_resource type="Resource" load_steps=2 format=3 uid="uid://cehtd3jqu5es2"]

[ext_resource type="Script" path="res://addons/block_code/code_generation/block_definition.gd" id="1_p3bft"]

[resource]
script = ExtResource("1_p3bft")
name = &"vector3_y"
target_node_class = ""
description = "Gives the y of a [i]Vector3[/i]"
category = "Math"
type = 3
variant_type = 3
display_template = "y of {vector3: VECTOR3}"
code_template = "{vector3}.y"
defaults = {
"vector3": Vector3(0, 0, 0)
}
signal_name = ""
scope = ""
19 changes: 19 additions & 0 deletions addons/block_code/blocks/math/vector3_z.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[gd_resource type="Resource" load_steps=2 format=3 uid="uid://cehtd3jqu5es2"]

[ext_resource type="Script" path="res://addons/block_code/code_generation/block_definition.gd" id="1_p3bft"]

[resource]
script = ExtResource("1_p3bft")
name = &"vector3_z"
target_node_class = ""
description = "Gives the z of a [i]Vector3[/i]"
category = "Math"
type = 3
variant_type = 3
display_template = "z of {vector3: VECTOR3}"
code_template = "{vector3}.z"
defaults = {
"vector3": Vector3(0, 0, 0)
}
signal_name = ""
scope = ""
21 changes: 21 additions & 0 deletions addons/block_code/blocks/variables/vector3.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[gd_resource type="Resource" load_steps=2 format=3 uid="uid://ddj24k1fp0s82"]

[ext_resource type="Script" path="res://addons/block_code/code_generation/block_definition.gd" id="1_ilw3v"]

[resource]
script = ExtResource("1_ilw3v")
name = &"vector3"
target_node_class = ""
description = ""
category = "Math"
type = 3
variant_type = 9
display_template = "vector3 x: {x: FLOAT} y: {y: FLOAT}: z: {z: FLOAT}"
code_template = "Vector3({x}, {y}, {z})"
defaults = {
"x": 0.0,
"y": 0.0,
"z": 0.0
}
signal_name = ""
scope = ""
2 changes: 2 additions & 0 deletions addons/block_code/code_generation/block_ast.gd
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ static func raw_input_to_code_string(input) -> String:
return "'%s'" % input.c_escape()
TYPE_VECTOR2:
return "Vector2%s" % str(input)
TYPE_VECTOR3:
return "Vector3%s" % str(input)
TYPE_COLOR:
return "Color%s" % str(input)
_:
Expand Down
27 changes: 25 additions & 2 deletions addons/block_code/code_generation/blocks_catalog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const _FALLBACK_SET_FOR_TYPE = {
TYPE_INT: 0,
TYPE_FLOAT: 0.0,
TYPE_VECTOR2: Vector2(0, 0),
TYPE_VECTOR3: Vector3(0, 0, 0),
TYPE_COLOR: Color.DARK_ORANGE,
}

Expand All @@ -20,6 +21,7 @@ const _FALLBACK_CHANGE_FOR_TYPE = {
TYPE_INT: 1,
TYPE_FLOAT: 1.0,
TYPE_VECTOR2: Vector2(1, 1),
TYPE_VECTOR3: Vector3(1, 1, 1),
TYPE_COLOR: Color.DARK_ORANGE,
}

Expand All @@ -45,6 +47,27 @@ const _SETTINGS_FOR_CLASS_PROPERTY = {
"default_change": Vector2(0.1, 0.1),
},
},
"Node3D":
{
"position":
{
"category": "Transform | Position",
"default_set": Vector3(0, 0, 0),
"default_change": Vector3(0.1, 0.1, 0.1),
},
"rotation":
{
"category": "Transform | Rotation",
"default_set": Vector3(0, 0, 0),
"default_change": Vector3(0.1, 0.1, 0.1),
},
"scale":
{
"category": "Transform | Scale",
"default_set": Vector3(1, 1, 1),
"default_change": Vector3(0.1, 0.1, 0.1),
}
},
"CanvasItem":
{
"modulate":
Expand Down Expand Up @@ -280,13 +303,13 @@ static func get_variable_getter_block_definition(variable: VariableDefinition) -


static func get_variable_setter_block_definition(variable: VariableDefinition) -> BlockDefinition:
var type_string: String = Types.VARIANT_TYPE_TO_STRING[variable.var_type]
var _type_string: String = Types.VARIANT_TYPE_TO_STRING[variable.var_type]
var block_def := BlockDefinition.new()

block_def.name = "set_var_%s" % variable.var_name
block_def.category = "Variables"
block_def.type = Types.BlockType.STATEMENT
block_def.display_template = "Set %s to {value: %s}" % [variable.var_name, type_string]
block_def.display_template = "Set %s to {value: %s}" % [variable.var_name, _type_string]
block_def.code_template = "%s = {value}" % variable.var_name

return block_def
Expand Down
4 changes: 4 additions & 0 deletions addons/block_code/types/types.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const VARIANT_TYPE_TO_STRING: Dictionary = {
TYPE_FLOAT: "FLOAT",
TYPE_BOOL: "BOOL",
TYPE_VECTOR2: "VECTOR2",
TYPE_VECTOR3: "VECTOR3",
TYPE_COLOR: "COLOR",
TYPE_NODE_PATH: "NODE_PATH",
TYPE_OBJECT: "OBJECT",
Expand All @@ -27,6 +28,7 @@ const STRING_TO_VARIANT_TYPE: Dictionary = {
"FLOAT": TYPE_FLOAT,
"BOOL": TYPE_BOOL,
"VECTOR2": TYPE_VECTOR2,
"VECTOR3": TYPE_VECTOR3,
"COLOR": TYPE_COLOR,
"NODE_PATH": TYPE_NODE_PATH,
"OBJECT": TYPE_OBJECT,
Expand All @@ -42,6 +44,8 @@ const cast_relationships = [
[TYPE_COLOR, TYPE_STRING, "str(%s)"],
[TYPE_VECTOR2, TYPE_STRING, "str(%s)"],
[TYPE_VECTOR2, TYPE_BOOL, "%s"],
[TYPE_VECTOR3, TYPE_STRING, "str(%s)"],
[TYPE_VECTOR3, TYPE_BOOL, "%s"],
]

# Directed graph, edges are CastGraphEdge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ var _drag_start: Vector2 = Vector2.INF
@onready var _vector2_input := %Vector2Input
@onready var _x_line_edit := %XLineEdit
@onready var _y_line_edit := %YLineEdit
# Vector3
@onready var _vector3_input := %Vector3Input
@onready var _v3_x_line_edit := %V3XLineEdit
@onready var _v3_y_line_edit := %V3YLineEdit
@onready var _v3_z_line_edit := %V3ZLineEdit
# Bool
@onready var _bool_input := %BoolInput
@onready var _bool_input_option := %BoolInputOption
Expand All @@ -46,6 +51,9 @@ var _drag_start: Vector2 = Vector2.INF
_line_edit: "",
_x_line_edit: "",
_y_line_edit: "",
_v3_x_line_edit: "",
_v3_y_line_edit: "",
_v3_z_line_edit: "",
}


Expand Down Expand Up @@ -73,6 +81,10 @@ func set_raw_input(raw_input: Variant):
# Rounding because floats are doubles by default but Vector2s have single components
_x_line_edit.text = ("%.4f" % raw_input.x).rstrip("0").rstrip(".") if raw_input != null else ""
_y_line_edit.text = ("%.4f" % raw_input.y).rstrip("0").rstrip(".") if raw_input != null else ""
TYPE_VECTOR3:
_v3_x_line_edit.text = ("%.4f" % raw_input.x).rstrip("0").rstrip(".") if raw_input != null else ""
_v3_y_line_edit.text = ("%.4f" % raw_input.y).rstrip("0").rstrip(".") if raw_input != null else ""
_v3_z_line_edit.text = ("%.4f" % raw_input.z).rstrip("0").rstrip(".") if raw_input != null else ""
TYPE_BOOL:
_bool_input_option.select(1 if raw_input else 0)
TYPE_NIL:
Expand All @@ -83,6 +95,9 @@ func set_raw_input(raw_input: Variant):
_last_submitted_text[_line_edit] = _line_edit.text
_last_submitted_text[_x_line_edit] = _x_line_edit.text
_last_submitted_text[_y_line_edit] = _y_line_edit.text
_last_submitted_text[_v3_x_line_edit] = _v3_x_line_edit.text
_last_submitted_text[_v3_y_line_edit] = _v3_y_line_edit.text
_last_submitted_text[_v3_z_line_edit] = _v3_z_line_edit.text


## Gets the value, which could be one of a variety of types depending on
Expand All @@ -102,6 +117,8 @@ func get_raw_input() -> Variant:
return _color_input.color
TYPE_VECTOR2:
return Vector2(float(_x_line_edit.text), float(_y_line_edit.text))
TYPE_VECTOR3:
return Vector3(float(_v3_x_line_edit.text), float(_v3_y_line_edit.text), float(_v3_z_line_edit.text))
TYPE_BOOL:
return bool(_bool_input_option.selected)
TYPE_INT:
Expand Down Expand Up @@ -203,6 +220,30 @@ func _on_y_line_edit_focus_exited():
_validate_and_submit_edit_text(_y_line_edit, TYPE_FLOAT)


func _on_v3_x_line_edit_text_submitted(_new_text):
_validate_and_submit_edit_text(_v3_x_line_edit, TYPE_FLOAT)


func _on_v3_x_line_edit_focus_exited():
_validate_and_submit_edit_text(_v3_x_line_edit, TYPE_FLOAT)


func _on_v3_y_line_edit_text_submitted(_new_text):
_validate_and_submit_edit_text(_v3_y_line_edit, TYPE_FLOAT)


func _on_v3_y_line_edit_focus_exited():
_validate_and_submit_edit_text(_v3_y_line_edit, TYPE_FLOAT)


func _on_v3_z_line_edit_text_submitted(_new_text):
_validate_and_submit_edit_text(_v3_z_line_edit, TYPE_FLOAT)


func _on_v3_z_line_edit_focus_exited():
_validate_and_submit_edit_text(_v3_z_line_edit, TYPE_FLOAT)


func _update_visible_input():
if snap_point.has_snapped_block():
_switch_input(null)
Expand All @@ -214,6 +255,8 @@ func _update_visible_input():
_switch_input(_color_input)
TYPE_VECTOR2:
_switch_input(_vector2_input)
TYPE_VECTOR3:
_switch_input(_vector3_input)
TYPE_BOOL:
_switch_input(_bool_input)
_:
Expand Down
Loading

0 comments on commit 28f102e

Please sign in to comment.