Skip to content

Commit

Permalink
Stop differentiating between attribute types (#692)
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur authored Apr 28, 2024
1 parent fe8dcd6 commit a0659a5
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 114 deletions.
9 changes: 5 additions & 4 deletions src/data_classes/AttributeNumeric.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ class_name AttributeNumeric extends Attribute
## An attribute representing a number.

var _number := NAN
enum Mode {FLOAT, UFLOAT, NFLOAT} # UFLOAT is positive-only, NFLOAT is in [0, 1].
var mode := Mode.FLOAT
var min_value: float
var max_value: float

func _init(new_mode: Mode, new_default: String, new_init := "") -> void:
mode = new_mode
func _init(new_min: float, new_max: float, new_default: String, new_init := "") -> void:
min_value = new_min
max_value = new_max
default = new_default
set_value(new_init if !new_init.is_empty() else new_default, SyncMode.SILENT)

Expand Down
20 changes: 9 additions & 11 deletions src/data_classes/TagCircle.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ class_name TagCircle extends Tag

const name = "circle"
const possible_conversions = ["ellipse", "rect", "path"]
const known_attributes = ["transform", "cx", "cy", "r", "opacity", "fill",
"fill-opacity", "stroke", "stroke-opacity", "stroke-width"]
const icon = preload("res://visual/icons/tag/circle.svg")

const known_shape_attributes = ["cx", "cy", "r"]
const known_inheritable_attributes = ["transform", "opacity", "fill", "fill-opacity",
"stroke", "stroke-opacity", "stroke-width"]

func _init(pos := Vector2.ZERO) -> void:
attributes = {
"transform": AttributeTransform.new(),
"cx": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"cy": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"r": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "0", "1"),
"opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"cx": AttributeNumeric.new(-INF, INF, "0"),
"cy": AttributeNumeric.new(-INF, INF, "0"),
"r": AttributeNumeric.new(0.0, INF, "0", "1"),
"opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"fill": AttributeColor.new("black"),
"fill-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"fill-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none"),
"stroke-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"stroke-width": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "1"),
"stroke-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke-width": AttributeNumeric.new(0.0, INF, "1"),
}
attributes.cx.set_num(pos.x)
attributes.cy.set_num(pos.y)
Expand Down
21 changes: 10 additions & 11 deletions src/data_classes/TagEllipse.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@ const name = "ellipse"
const possible_conversions = ["circle", "rect", "path"]
const icon = preload("res://visual/icons/tag/ellipse.svg")

const known_shape_attributes = ["cx", "cy", "rx", "ry"]
const known_inheritable_attributes = ["transform", "opacity", "fill", "fill-opacity",
"stroke", "stroke-opacity", "stroke-width"]
const known_attributes = ["cx", "cy", "rx", "ry", "transform", "opacity",
"fill", "fill-opacity", "stroke", "stroke-opacity", "stroke-width"]

func _init(pos := Vector2.ZERO) -> void:
attributes = {
"transform": AttributeTransform.new(),
"cx": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"cy": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"rx": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "0", "1"),
"ry": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "0", "1"),
"opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"cx": AttributeNumeric.new(-INF, INF, "0"),
"cy": AttributeNumeric.new(-INF, INF, "0"),
"rx": AttributeNumeric.new(0.0, INF, "0", "1"),
"ry": AttributeNumeric.new(0.0, INF, "0", "1"),
"opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"fill": AttributeColor.new("black"),
"fill-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"fill-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none"),
"stroke-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"stroke-width": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "1"),
"stroke-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke-width": AttributeNumeric.new(0.0, INF, "1"),
}
attributes.cx.set_num(pos.x)
attributes.cy.set_num(pos.y)
Expand Down
19 changes: 9 additions & 10 deletions src/data_classes/TagLine.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ const name = "line"
const possible_conversions = ["path"]
const icon = preload("res://visual/icons/tag/line.svg")

const known_shape_attributes = ["x1", "y1", "x2", "y2"]
const known_inheritable_attributes = ["transform", "opacity", "stroke", "stroke-opacity",
"stroke-width", "stroke-linecap"]
const known_attributes = ["x1", "y1", "x2", "y2", "transform", "opacity", "stroke",
"stroke-opacity", "stroke-width", "stroke-linecap"]

func _init(pos := Vector2.ZERO) -> void:
attributes = {
"transform": AttributeTransform.new(),
"x1": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"y1": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"x2": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0", "1"),
"y2": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"x1": AttributeNumeric.new(-INF, INF, "0"),
"y1": AttributeNumeric.new(-INF, INF, "0"),
"x2": AttributeNumeric.new(-INF, INF, "0", "1"),
"y2": AttributeNumeric.new(-INF, INF, "0"),
"opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none", "#000"),
"stroke-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"stroke-width": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "1"),
"stroke-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke-width": AttributeNumeric.new(0.0, INF, "1"),
"stroke-linecap": AttributeEnum.new(["butt", "round", "square"], 0),
}
attributes.x1.set_num(pos.x)
Expand Down
11 changes: 5 additions & 6 deletions src/data_classes/TagPath.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ const name = "path"
const possible_conversions = []
const icon = preload("res://visual/icons/tag/path.svg")

const known_shape_attributes = ["d"]
const known_inheritable_attributes = ["transform", "opacity", "fill", "fill-opacity",
const known_attributes = ["d", "transform", "opacity", "fill", "fill-opacity",
"stroke", "stroke-opacity", "stroke-width", "stroke-linecap", "stroke-linejoin"]

func _init(pos := Vector2.ZERO) -> void:
attributes = {
"transform": AttributeTransform.new(),
"d": AttributePath.new(),
"opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"fill": AttributeColor.new("black"),
"fill-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"fill-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none"),
"stroke-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"stroke-width": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "1"),
"stroke-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke-width": AttributeNumeric.new(0.0, INF, "1"),
"stroke-linecap": AttributeEnum.new(["butt", "round", "square"], 0),
"stroke-linejoin": AttributeEnum.new(["miter", "round", "bevel"], 0),
}
Expand Down
26 changes: 13 additions & 13 deletions src/data_classes/TagRect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ const name = "rect"
const possible_conversions = ["circle", "ellipse", "path"]
const icon = preload("res://visual/icons/tag/rect.svg")

const known_shape_attributes = ["x", "y", "width", "height", "rx", "ry"]
const known_inheritable_attributes = ["transform", "opacity", "fill", "fill-opacity",
"stroke", "stroke-opacity", "stroke-width", "stroke-linejoin"]
const known_attributes = ["x", "y", "width", "height", "rx", "ry", "transform",
"opacity", "fill", "fill-opacity", "stroke", "stroke-opacity", "stroke-width",
"stroke-linejoin"]

func _init(pos := Vector2.ZERO) -> void:
attributes = {
"transform": AttributeTransform.new(),
"x": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"y": AttributeNumeric.new(AttributeNumeric.Mode.FLOAT, "0"),
"width": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "0", "1"),
"height": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "0", "1"),
"rx": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "0"),
"ry": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "0"),
"opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"x": AttributeNumeric.new(-INF, INF, "0"),
"y": AttributeNumeric.new(-INF, INF, "0"),
"width": AttributeNumeric.new(0.0, INF, "0", "1"),
"height": AttributeNumeric.new(0.0, INF, "0", "1"),
"rx": AttributeNumeric.new(0.0, INF, "0"),
"ry": AttributeNumeric.new(0.0, INF, "0"),
"opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"fill": AttributeColor.new("black"),
"fill-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"fill-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none"),
"stroke-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"stroke-width": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, "1"),
"stroke-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke-width": AttributeNumeric.new(0.0, INF, "1"),
"stroke-linejoin": AttributeEnum.new(["miter", "round", "bevel"], 0),
}
attributes.x.set_num(pos.x)
Expand Down
4 changes: 2 additions & 2 deletions src/data_classes/TagSVG.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const name = "svg"

func _init() -> void:
attributes = {
"height": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, ""),
"width": AttributeNumeric.new(AttributeNumeric.Mode.UFLOAT, ""),
"height": AttributeNumeric.new(0.0, INF, ""),
"width": AttributeNumeric.new(0.0, INF, ""),
"viewBox": AttributeList.new(),
}
unknown_attributes.append(AttributeUnknown.new("xmlns", "http://www.w3.org/2000/svg"))
Expand Down
7 changes: 3 additions & 4 deletions src/data_classes/TagStop.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ class_name TagStop extends Tag

const name = "stop"
const possible_conversions = []
const known_shape_attributes = []
const known_inheritable_attributes = ["offset", "stop-color", "stop-opacity"]
const known_attributes = ["offset", "stop-color", "stop-opacity"]
const icon = preload("res://visual/icons/tag/stop.svg")

func _init() -> void:
attributes = {
"offset": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "0"),
"offset": AttributeNumeric.new(0.0, 1.0, "0"),
"stop-color": AttributeColor.new("black"),
"stop-opacity": AttributeNumeric.new(AttributeNumeric.Mode.NFLOAT, "1"),
"stop-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
}
super()
12 changes: 6 additions & 6 deletions src/parsers/SVGDB.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const known_tags = ["svg", "circle", "ellipse", "rect", "path", "line", "stop"]

const known_tag_attributes = { # Dictionary{String: Array[String]}
"svg": TagSVG.known_attributes,
"circle": TagCircle.known_shape_attributes + TagCircle.known_inheritable_attributes,
"ellipse": TagEllipse.known_shape_attributes + TagEllipse.known_inheritable_attributes,
"rect": TagRect.known_shape_attributes + TagRect.known_inheritable_attributes,
"path": TagPath.known_shape_attributes + TagPath.known_inheritable_attributes,
"line": TagLine.known_shape_attributes + TagLine.known_inheritable_attributes,
"stop": TagStop.known_shape_attributes + TagStop.known_inheritable_attributes,
"circle": TagCircle.known_attributes,
"ellipse": TagEllipse.known_attributes,
"rect": TagRect.known_attributes,
"path": TagPath.known_attributes,
"line": TagLine.known_attributes,
"stop": TagStop.known_attributes,
}

static func is_tag_known(tag_name: String) -> bool:
Expand Down
7 changes: 4 additions & 3 deletions src/ui_elements/path_field.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func set_value(new_value: String, update_type := Utils.UpdateType.REGULAR) -> vo
_:
attribute.set_value(new_value)

func _ready() -> void:
func set_attribute(new_attribute: AttributePath) -> void:
attribute = new_attribute
set_value(attribute.get_value())
attribute.value_changed.connect(set_value)
line_edit.tooltip_text = attribute_name
Expand Down Expand Up @@ -51,7 +52,7 @@ func sync(new_value: String) -> void:
var new_command_editor := CommandEditor.instantiate()
new_command_editor.path_command = command
# TODO Fix this mess, it's needed for individual path commands selection.
new_command_editor.tid = get_node(^"../../../../..").tid
new_command_editor.tid = get_node(^"../../../..").tid
new_command_editor.cmd_idx = command_idx
command_editor.clear_children()
command_editor.replace_by(new_command_editor)
Expand All @@ -62,7 +63,7 @@ func sync(new_value: String) -> void:
var command_editor := CommandEditor.instantiate()
command_editor.path_command = attribute.get_command(command_idx)
# TODO Fix this mess, it's needed for individual path commands selection.
command_editor.tid = get_node(^"../../../../..").tid
command_editor.tid = get_node(^"../../../..").tid
command_editor.cmd_idx = command_idx
commands_container.add_child(command_editor)
command_idx += 1
45 changes: 45 additions & 0 deletions src/ui_elements/tag_content_path.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
extends VBoxContainer

const TransformField = preload("res://src/ui_elements/transform_field.tscn")
const NumberField = preload("res://src/ui_elements/number_field.tscn")
const NumberSlider = preload("res://src/ui_elements/number_field_with_slider.tscn")
const ColorField = preload("res://src/ui_elements/color_field.tscn")
const EnumField = preload("res://src/ui_elements/enum_field.tscn")

@onready var attribute_container: HFlowContainer = $AttributeContainer
@onready var path_field: VBoxContainer = $PathField

var tag: Tag
var tid: PackedInt32Array

func _ready() -> void:
path_field.attribute_name = "d"
path_field.set_attribute(tag.attributes.d)
for attribute_key in tag.attributes:
if attribute_key == "d":
continue
var attribute: Attribute = tag.attributes[attribute_key]
var input_field: Control
if attribute is AttributeTransform:
input_field = TransformField.instantiate()
elif attribute is AttributeNumeric:
if is_inf(attribute.max_value):
input_field = NumberField.instantiate()
if not is_inf(attribute.min_value):
input_field.allow_lower = false
input_field.min_value = attribute.min_value
else:
input_field = NumberSlider.instantiate()
input_field.allow_lower = false
input_field.allow_higher = false
input_field.min_value = attribute.min_value
input_field.max_value = attribute.max_value
input_field.slider_step = 0.01
elif attribute is AttributeColor:
input_field = ColorField.instantiate()
elif attribute is AttributeEnum:
input_field = EnumField.instantiate()
input_field.attribute = attribute
input_field.attribute_name = attribute_key
input_field.focused.connect(Indications.normal_select.bind(tid))
attribute_container.add_child(input_field)
16 changes: 16 additions & 0 deletions src/ui_elements/tag_content_path.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[gd_scene load_steps=3 format=3 uid="uid://ntom8irbw0d5"]

[ext_resource type="Script" path="res://src/ui_elements/tag_content_path.gd" id="1_t5x4b"]
[ext_resource type="PackedScene" uid="uid://dqy5lv33sy5r7" path="res://src/ui_elements/path_field.tscn" id="1_vf17i"]

[node name="TagContentPath" type="VBoxContainer"]
offset_right = 40.0
offset_bottom = 40.0
script = ExtResource("1_t5x4b")

[node name="AttributeContainer" type="HFlowContainer" parent="."]
layout_mode = 2

[node name="PathField" parent="." instance=ExtResource("1_vf17i")]
layout_mode = 2
size_flags_horizontal = 0
45 changes: 13 additions & 32 deletions src/ui_elements/tag_content_unknown.gd
Original file line number Diff line number Diff line change
@@ -1,60 +1,41 @@
extends VBoxContainer

const UnknownField = preload("res://src/ui_elements/unknown_field.tscn")
const TransformField = preload("res://src/ui_elements/transform_field.tscn")
const NumberField = preload("res://src/ui_elements/number_field.tscn")
const NumberSlider = preload("res://src/ui_elements/number_field_with_slider.tscn")
const ColorField = preload("res://src/ui_elements/color_field.tscn")
const PathField = preload("res://src/ui_elements/path_field.tscn")
const EnumField = preload("res://src/ui_elements/enum_field.tscn")

var unknown_container: HFlowContainer # Only created if there are unknown attributes.
@onready var paint_container: FlowContainer = $PaintAttributes
@onready var shape_container: FlowContainer = $ShapeAttributes
@onready var attribute_container: HFlowContainer = $AttributeContainer

var tag: Tag
var tid: PackedInt32Array

func _ready() -> void:
# Fill up the containers. Start with unknown attributes, if there are any.
if not tag.unknown_attributes.is_empty():
unknown_container = HFlowContainer.new()
add_child(unknown_container)
move_child(unknown_container, 0)
for attribute in tag.unknown_attributes:
var input_field := UnknownField.instantiate()
input_field.attribute = attribute
input_field.attribute_name = attribute.name
unknown_container.add_child(input_field)
# Continue with supported attributes.
for attribute_key in tag.attributes:
var attribute: Attribute = tag.attributes[attribute_key]
var input_field: Control
if attribute is AttributeTransform:
input_field = TransformField.instantiate()
elif attribute is AttributeNumeric:
match attribute.mode:
AttributeNumeric.Mode.FLOAT:
input_field = NumberField.instantiate()
AttributeNumeric.Mode.UFLOAT:
input_field = NumberField.instantiate()
if is_inf(attribute.max_value):
input_field = NumberField.instantiate()
if not is_inf(attribute.min_value):
input_field.allow_lower = false
AttributeNumeric.Mode.NFLOAT:
input_field = NumberSlider.instantiate()
input_field.allow_lower = false
input_field.allow_higher = false
input_field.slider_step = 0.01
input_field.min_value = attribute.min_value
else:
input_field = NumberSlider.instantiate()
input_field.allow_lower = false
input_field.allow_higher = false
input_field.min_value = attribute.min_value
input_field.max_value = attribute.max_value
input_field.slider_step = 0.01
elif attribute is AttributeColor:
input_field = ColorField.instantiate()
elif attribute is AttributePath:
input_field = PathField.instantiate()
elif attribute is AttributeEnum:
input_field = EnumField.instantiate()
input_field.attribute = attribute
input_field.attribute_name = attribute_key
input_field.focused.connect(Indications.normal_select.bind(tid))
# Add the attribute to its corresponding container.
if attribute_key in tag.known_shape_attributes:
shape_container.add_child(input_field)
elif attribute_key in tag.known_inheritable_attributes:
paint_container.add_child(input_field)
attribute_container.add_child(input_field)
Loading

0 comments on commit a0659a5

Please sign in to comment.