Skip to content

Text validation #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ var option: bool = false
@onready var _bool_input_option := %BoolInputOption

# Used to submit the text when losing focus:
var _last_lineedit_submitted_text: String
var _last_x_lineedit_submitted_text: String
var _last_y_lineedit_submitted_text: String
@onready var _last_submitted_text = {
_line_edit: _line_edit.text,
_x_line_edit: _x_line_edit.text,
_y_line_edit: _y_line_edit.text,
}


func set_raw_input(raw_input):
Expand Down Expand Up @@ -99,10 +101,6 @@ func _ready():
snap_point.block_type = block_type
snap_point.variant_type = variant_type

_last_lineedit_submitted_text = _line_edit.text
_last_x_lineedit_submitted_text = _x_line_edit.text
_last_y_lineedit_submitted_text = _y_line_edit.text

_update_visible_input()


Expand Down Expand Up @@ -136,34 +134,44 @@ func get_string() -> String:
return "%s" % input


func _on_line_edit_text_submitted(new_text):
_last_lineedit_submitted_text = new_text
func _validate_and_submit_edit_text(line_edit: Node, type: Variant.Type):
if _last_submitted_text[line_edit] == line_edit.text:
return
match type:
TYPE_FLOAT:
if not line_edit.text.is_valid_float():
line_edit.text = _last_submitted_text[line_edit]
return
TYPE_INT:
if not line_edit.text.is_valid_int():
line_edit.text = _last_submitted_text[line_edit]
return
_last_submitted_text[line_edit] = line_edit.text
modified.emit()


func _on_line_edit_text_submitted(_new_text):
_validate_and_submit_edit_text(_line_edit, variant_type)


func _on_line_edit_focus_exited():
if _last_lineedit_submitted_text != _line_edit.text:
modified.emit()
_validate_and_submit_edit_text(_line_edit, variant_type)


func _on_x_line_edit_text_submitted(new_text):
_last_x_lineedit_submitted_text = new_text
modified.emit()
func _on_x_line_edit_text_submitted(_new_text):
_validate_and_submit_edit_text(_x_line_edit, TYPE_FLOAT)


func _on_x_line_edit_focus_exited():
if _last_x_lineedit_submitted_text != _x_line_edit.text:
modified.emit()
_validate_and_submit_edit_text(_x_line_edit, TYPE_FLOAT)


func _on_y_line_edit_text_submitted(new_text):
_last_y_lineedit_submitted_text = new_text
modified.emit()
func _on_y_line_edit_text_submitted(_new_text):
_validate_and_submit_edit_text(_y_line_edit, TYPE_FLOAT)


func _on_y_line_edit_focus_exited():
if _last_y_lineedit_submitted_text != _y_line_edit.text:
modified.emit()
_validate_and_submit_edit_text(_y_line_edit, TYPE_FLOAT)


func _update_visible_input():
Expand All @@ -185,15 +193,11 @@ func _update_visible_input():

func _switch_input(node: Node):
for c in _input_switcher.get_children():
c.visible = false

if node:
node.visible = true
c.visible = c == node


func _on_color_input_color_changed(color):
_update_panel_bg_color(color)

modified.emit()


Expand Down
97 changes: 71 additions & 26 deletions addons/block_code/ui/picker/categories/category_factory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ static func get_general_blocks() -> Array[Block]:
b.variant_type = TYPE_VECTOR2
b.block_format = "Vector2 x: {x: FLOAT} y: {y: FLOAT}"
b.statement = "Vector2({x}, {y})"
b.defaults = {"x": "0", "y": "0"}
b.category = "Variables"
block_list.append(b)

Expand All @@ -373,6 +374,7 @@ static func get_general_blocks() -> Array[Block]:
b.variant_type = TYPE_INT
b.block_format = "{a: INT} + {b: INT}"
b.statement = "({a} + {b})"
b.defaults = {"a": "1", "b": "1"}
b.category = "Math"
block_list.append(b)

Expand All @@ -381,6 +383,7 @@ static func get_general_blocks() -> Array[Block]:
b.variant_type = TYPE_INT
b.block_format = "{a: INT} - {b: INT}"
b.statement = "({a} - {b})"
b.defaults = {"a": "1", "b": "1"}
b.category = "Math"
block_list.append(b)

Expand All @@ -389,6 +392,7 @@ static func get_general_blocks() -> Array[Block]:
b.variant_type = TYPE_INT
b.block_format = "{a: INT} * {b: INT}"
b.statement = "({a} * {b})"
b.defaults = {"a": "1", "b": "1"}
b.category = "Math"
block_list.append(b)

Expand All @@ -397,6 +401,7 @@ static func get_general_blocks() -> Array[Block]:
b.variant_type = TYPE_INT
b.block_format = "{a: INT} / {b: INT}"
b.statement = "({a} / {b})"
b.defaults = {"a": "1", "b": "1"}
b.category = "Math"
block_list.append(b)

Expand All @@ -405,6 +410,7 @@ static func get_general_blocks() -> Array[Block]:
b.variant_type = TYPE_INT
b.block_format = "{base: INT} ^ {exp: INT}"
b.statement = "(pow({base}, {exp}))"
b.defaults = {"base": "1", "exp": "1"}
b.category = "Math"
block_list.append(b)

Expand All @@ -413,14 +419,14 @@ static func get_general_blocks() -> Array[Block]:

b = BLOCKS["control_block"].instantiate()
b.block_name = "if"
b.block_formats = ["if {condition: BOOL}"]
b.block_formats = ["if {condition: BOOL}"]
b.statements = ["if {condition}:"]
b.category = "Logic | Conditionals"
block_list.append(b)

b = BLOCKS["control_block"].instantiate()
b.block_name = "if_else"
b.block_formats = ["if {condition: BOOL}", "else"]
b.block_formats = ["if {condition: BOOL}", "else"]
b.statements = ["if {condition}:", "else:"]
b.category = "Logic | Conditionals"
block_list.append(b)
Expand All @@ -430,7 +436,11 @@ static func get_general_blocks() -> Array[Block]:
b.variant_type = TYPE_BOOL
b.block_format = "{int1: INT} {op: OPTION} {int2: INT}"
b.statement = "({int1} {op} {int2})"
b.defaults = {"op": OptionData.new(["==", ">", "<", ">=", "<=", "!="])}
b.defaults = {
"op": OptionData.new(["==", ">", "<", ">=", "<=", "!="]),
"int1": "1",
"int2": "1",
}
b.category = "Logic | Comparison"
block_list.append(b)

Expand Down Expand Up @@ -529,22 +539,41 @@ static func property_to_blocklist(property: Dictionary) -> Array[Block]:

var variant_type = property.type

const FALLBACK_SET_FOR_TYPE = {
TYPE_INT: "0",
TYPE_FLOAT: "0",
TYPE_VECTOR2: "0, 0",
TYPE_COLOR: "DARK_ORANGE",
}

const FALLBACK_CHANGE_FOR_TYPE = {
TYPE_INT: "1",
TYPE_FLOAT: "1",
TYPE_VECTOR2: "1, 1",
TYPE_COLOR: "DARK_ORANGE",
}

if variant_type:
var type_string: String = Types.VARIANT_TYPE_TO_STRING[variant_type]

var b = BLOCKS["statement_block"].instantiate()
b.block_name = "set_prop_%s" % property.name
b.block_format = "Set %s to {value: %s}" % [property.name.capitalize(), type_string]
b.statement = "%s = {value}" % property.name
var default_set = property.get("default_set", FALLBACK_SET_FOR_TYPE.get(variant_type, ""))
b.defaults = {"value": default_set}
b.category = property.category
block_list.append(b)

b = BLOCKS["statement_block"].instantiate()
b.block_name = "change_prop_%s" % property.name
b.block_format = "Change %s by {value: %s}" % [property.name.capitalize(), type_string]
b.statement = "%s += {value}" % property.name
b.category = property.category
block_list.append(b)
if property.get("has_change", true):
b = BLOCKS["statement_block"].instantiate()
b.block_name = "change_prop_%s" % property.name
b.block_format = "Change %s by {value: %s}" % [property.name.capitalize(), type_string]
b.statement = "%s += {value}" % property.name
var default_change = property.get("default_change", FALLBACK_CHANGE_FOR_TYPE[variant_type])
b.defaults = {"value": default_change}
b.category = property.category
block_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.block_name = "get_prop_%s" % property.name
Expand All @@ -565,7 +594,7 @@ static func blocks_from_property_list(property_list: Array, selected_props: Dict
for prop in property_list:
if selected_property == prop.name:
found_prop = prop
found_prop.category = selected_props[selected_property]
found_prop.merge(selected_props[selected_property])
break
if found_prop:
block_list.append_array(property_to_blocklist(found_prop))
Expand Down Expand Up @@ -593,23 +622,39 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:

match _class_name:
"Node2D":
var b = BLOCKS["statement_block"].instantiate()
b.block_name = "node2d_rotation"
b.block_format = "Set Rotation Degrees {angle: FLOAT}"
b.statement = "rotation_degrees = {angle}"
b.category = "Transform | Rotation"
block_list.append(b)

props = {
"position": "Transform | Position",
"rotation": "Transform | Rotation",
"scale": "Transform | Scale",
"position":
{
"category": "Transform | Position",
"default_set": "100, 100",
"default_change": "1, 1",
},
"rotation_degrees":
{
"category": "Transform | Rotation",
"default_set": "45",
"default_change": "1",
},
"scale":
{
"category": "Transform | Scale",
"default_set": "2, 2",
"default_change": "0.1, 0.1",
},
}

"CanvasItem":
props = {
"modulate": "Graphics | Modulate",
"visible": "Graphics | Visibility",
"modulate":
{
"category": "Graphics | Modulate",
"has_change": false,
},
"visible":
{
"category": "Graphics | Visibility",
"has_change": false,
},
}

"RigidBody2D":
Expand Down Expand Up @@ -650,9 +695,9 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
block_list.append(b)

props = {
"mass": "Physics | Mass",
"linear_velocity": "Physics | Velocity",
"angular_velocity": "Physics | Velocity",
"mass": {"category": "Physics | Mass"},
"linear_velocity": {"category": "Physics | Velocity"},
"angular_velocity": {"category": "Physics | Velocity"},
}

"AnimationPlayer":
Expand Down Expand Up @@ -754,7 +799,7 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
block_list.append(b)

props = {
"velocity": "Physics | Velocity",
"velocity": {"category": "Physics | Velocity"},
}

var prop_list = ClassDB.class_get_property_list(_class_name, true)
Expand Down