Skip to content

Commit

Permalink
Rework logic around inherent attribute properties
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur committed Apr 29, 2024
1 parent 45f7887 commit ea03774
Show file tree
Hide file tree
Showing 33 changed files with 249 additions and 261 deletions.
2 changes: 0 additions & 2 deletions src/GlobalSettings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const default_config = {
"handle_hovered_color": Color("#aaa"),
"handle_selected_color": Color("#46f"),
"handle_hovered_selected_color": Color("#f44"),
"default_value_opacity": 0.7,
"basic_color_valid": Color("9f9"),
"basic_color_error": Color("f99"),
"basic_color_warning": Color("ff9"),
Expand Down Expand Up @@ -128,7 +127,6 @@ var handle_color := Color("#111")
var handle_hovered_color := Color("#aaa")
var handle_selected_color := Color("#46f")
var handle_hovered_selected_color := Color("#f44")
var default_value_opacity := 0.7
var basic_color_valid := Color("9f9")
var basic_color_error := Color("f99")
var basic_color_warning := Color("ff9")
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enum CustomNotification {
THEME_CHANGED = 302,
NUMBER_PRECISION_CHANGED = 303,
HIGHLIGHT_COLORS_CHANGED = 304,
DEFAULT_VALUE_OPACITY_CHANGED = 305,
BASIC_COLORS_CHANGED = 305,
HANDLE_VISUALS_CHANGED = 306,
}

Expand Down
13 changes: 12 additions & 1 deletion src/data_classes/Attribute.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class_name Attribute extends RefCounted
signal value_changed(new_value: String)
signal propagate_value_changed(undo_redo: bool)

var default: String
var name: String
var _value: String

enum SyncMode {LOUD, INTERMEDIATE, FINAL, NO_PROPAGATION, SILENT}
Expand Down Expand Up @@ -46,3 +46,14 @@ func _sync() -> void:

func autoformat(text: String) -> String:
return text

func get_default() -> String:
if name in SVGDB.attribute_defaults:
return SVGDB.attribute_defaults[name]
else:
return ""

func _init(new_name: String, init_value := "") -> void:
name = new_name
if not init_value.is_empty():
set_value(init_value)
11 changes: 4 additions & 7 deletions src/data_classes/AttributeColor.gd
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# An attribute representing a color string, or an url to the ID of a paint element.
class_name AttributeColor extends Attribute
## An attribute representing a color string, or an url to an ID.

# No direct color representation for this attribute type. There are too many quirks.

func _init(new_default: String, new_init := "") -> void:
default = new_default
set_value(new_init if !new_init.is_empty() else new_default, SyncMode.SILENT)

func set_value(new_value: String, sync_mode := SyncMode.LOUD) -> void:
super(new_value if ColorParser.is_valid(new_value) else default, sync_mode)
super(new_value if ColorParser.is_valid(new_value) else get_default(), sync_mode)

func autoformat(text: String) -> String:
if GlobalSettings.color_enable_autoformatting:
var new_text := ColorParser.format_text(text)
return default if ColorParser.are_colors_same(new_text, default) else new_text
return get_default() if ColorParser.are_colors_same(new_text, get_default()) else\
new_text
else:
return text

Expand Down
14 changes: 5 additions & 9 deletions src/data_classes/AttributeEnum.gd
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# An attribute with only a set of meaningful values.
class_name AttributeEnum extends Attribute
## An attribute with only a set of meaningful values.

var possible_values: Array[String]

func _init(new_possible_values: Array[String], new_default_idx := 0) -> void:
possible_values = new_possible_values
default = possible_values[new_default_idx]
set_value(default, SyncMode.SILENT)

func set_value(new_value: String, sync_mode := SyncMode.LOUD) -> void:
super(new_value if new_value in possible_values else default, sync_mode)
if new_value.is_empty() or new_value in SVGDB.attribute_enum_values[name]:
super(new_value, sync_mode)
else:
super(get_default(), sync_mode)
6 changes: 1 addition & 5 deletions src/data_classes/AttributeList.gd
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# An attribute representing a list of numbers.
class_name AttributeList extends Attribute
## An attribute representing a list of numbers.

var _list: PackedFloat32Array

func _init() -> void:
default = ""
set_value("", SyncMode.SILENT)

func _sync() -> void:
_list = ListParser.string_to_list(get_value())

Expand Down
15 changes: 5 additions & 10 deletions src/data_classes/AttributeNumeric.gd
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# An attribute representing a number.
class_name AttributeNumeric extends Attribute
## An attribute representing a number.

var _number := NAN
var min_value: float
var max_value: float

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)

func _sync() -> void:
_number = NumberParser.text_to_num(get_value())
if _value.is_empty():
_number = NumberParser.text_to_num(SVGDB.attribute_defaults[name])
else:
_number = NumberParser.text_to_num(_value)

func autoformat(text: String) -> String:
if GlobalSettings.number_enable_autoformatting:
Expand Down
6 changes: 1 addition & 5 deletions src/data_classes/AttributePath.gd
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# The "d" attribute of [TagPath].
class_name AttributePath extends Attribute
## The "d" attribute of [TagPath].

var _commands: Array[PathCommand]

func _init() -> void:
default = ""
set_value(default, SyncMode.SILENT)

func _sync() -> void:
_commands = PathDataParser.parse_path_data(get_value())
locate_start_points()
Expand Down
6 changes: 1 addition & 5 deletions src/data_classes/AttributeTransform.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# An attribute representing a list of transformations.
class_name AttributeTransform extends Attribute
## An attribute representing a list of transforms.

class Transform extends RefCounted:
func compute_transform() -> Transform2D:
Expand Down Expand Up @@ -83,10 +83,6 @@ class TransformSkewY extends Transform:
var _transform_list: Array[Transform] = []
var _final_transform := Transform2D.IDENTITY

func _init() -> void:
default = ""
set_value(default, SyncMode.SILENT)

func _sync() -> void:
_transform_list = TransformListParser.text_to_transform_list(get_value())
_final_transform = AttributeTransform.compute_final_transform(_transform_list)
Expand Down
5 changes: 1 addition & 4 deletions src/data_classes/AttributeUnknown.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# An attribute that's not recognized by GodSVG.
class_name AttributeUnknown extends Attribute
## An attribute not recognized by GodSVG.

var name := ""

func _init(new_name: String, new_init := "") -> void:
default = ""
name = new_name
set_value(new_init, SyncMode.SILENT)
18 changes: 5 additions & 13 deletions src/data_classes/TagCircle.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## A <circle/> tag.
# A <circle/> tag.
class_name TagCircle extends Tag

const name = "circle"
Expand All @@ -8,18 +8,10 @@ const known_attributes = ["transform", "cx", "cy", "r", "opacity", "fill",
const icon = preload("res://visual/icons/tag/circle.svg")

func _init(pos := Vector2.ZERO) -> void:
attributes = {
"transform": AttributeTransform.new(),
"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(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none"),
"stroke-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke-width": AttributeNumeric.new(0.0, INF, "1"),
}
for attrib_name in ["transform", "cx", "cy", "opacity", "fill", "fill-opacity",
"stroke", "stroke-opacity", "stroke-width"]:
attributes[attrib_name] = SVGDB.attribute(attrib_name)
attributes.r = SVGDB.attribute("r", "1")
attributes.cx.set_num(pos.x)
attributes.cy.set_num(pos.y)
super()
Expand Down
20 changes: 6 additions & 14 deletions src/data_classes/TagEllipse.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# An <ellipse/> tag.
class_name TagEllipse extends Tag
## An <ellipse/> tag.

const name = "ellipse"
const possible_conversions = ["circle", "rect", "path"]
Expand All @@ -9,19 +9,11 @@ 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(-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(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none"),
"stroke-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke-width": AttributeNumeric.new(0.0, INF, "1"),
}
for attrib_name in ["transform", "cx", "cy", "opacity", "fill", "fill-opacity",
"stroke", "stroke-opacity", "stroke-width"]:
attributes[attrib_name] = SVGDB.attribute(attrib_name)
attributes.rx = SVGDB.attribute("rx", "1")
attributes.ry = SVGDB.attribute("ry", "1")
attributes.cx.set_num(pos.x)
attributes.cy.set_num(pos.y)
super()
Expand Down
15 changes: 3 additions & 12 deletions src/data_classes/TagLine.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@ const known_attributes = ["x1", "y1", "x2", "y2", "transform", "opacity", "strok
"stroke-opacity", "stroke-width", "stroke-linecap"]

func _init(pos := Vector2.ZERO) -> void:
attributes = {
"transform": AttributeTransform.new(),
"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(0.0, 1.0, "1"),
"stroke-width": AttributeNumeric.new(0.0, INF, "1"),
"stroke-linecap": AttributeEnum.new(["butt", "round", "square"], 0),
}
for attrib_name in ["transform", "opacity", "stroke", "stroke-opacity",
"stroke-width", "stroke-linecap"]:
attributes[attrib_name] = SVGDB.attribute(attrib_name)
attributes.x1.set_num(pos.x)
attributes.y1.set_num(pos.y)
attributes.x2.set_num(pos.x + 1)
Expand Down
15 changes: 3 additions & 12 deletions src/data_classes/TagPath.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@ 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(0.0, 1.0, "1"),
"fill": AttributeColor.new("black"),
"fill-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none"),
"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),
}
for attrib_name in ["transform", "d", "opacity", "fill", "fill-opacity",
"stroke", "stroke-opacity", "stroke-width", "stroke-linecap", "stroke-linejoin"]:
attributes[attrib_name] = SVGDB.attribute(attrib_name)
attributes.d.insert_command(0, "M")
attributes.d.set_command_property(0, "x", pos.x)
attributes.d.set_command_property(0, "y", pos.y)
Expand Down
21 changes: 5 additions & 16 deletions src/data_classes/TagRect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,11 @@ const known_attributes = ["x", "y", "width", "height", "rx", "ry", "transform",
"stroke-linejoin"]

func _init(pos := Vector2.ZERO) -> void:
attributes = {
"transform": AttributeTransform.new(),
"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(0.0, 1.0, "1"),
"stroke": AttributeColor.new("none"),
"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),
}
for attrib_name in ["transform", "x", "y", "rx", "ry", "opacity", "fill",
"fill-opacity", "stroke", "stroke-opacity", "stroke-width", "stroke-linejoin"]:
attributes[attrib_name] = SVGDB.attribute(attrib_name)
attributes.width = SVGDB.attribute("width", "1")
attributes.height = SVGDB.attribute("height", "1")
attributes.x.set_num(pos.x)
attributes.y.set_num(pos.y)
super()
Expand Down
7 changes: 2 additions & 5 deletions src/data_classes/TagSVG.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ const known_attributes = ["width", "height", "viewBox", "xmlns"]
const name = "svg"

func _init() -> void:
attributes = {
"height": AttributeNumeric.new(0.0, INF, ""),
"width": AttributeNumeric.new(0.0, INF, ""),
"viewBox": AttributeList.new(),
}
for attrib_name in ["width", "height", "viewBox"]:
attributes[attrib_name] = SVGDB.attribute(attrib_name)
unknown_attributes.append(AttributeUnknown.new("xmlns", "http://www.w3.org/2000/svg"))
attribute_changed.connect(update_cache.unbind(1))
changed_unknown.connect(update_cache)
Expand Down
7 changes: 2 additions & 5 deletions src/data_classes/TagStop.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const known_attributes = ["offset", "stop-color", "stop-opacity"]
const icon = preload("res://visual/icons/tag/stop.svg")

func _init() -> void:
attributes = {
"offset": AttributeNumeric.new(0.0, 1.0, "0"),
"stop-color": AttributeColor.new("black"),
"stop-opacity": AttributeNumeric.new(0.0, 1.0, "1"),
}
for attrib_name in ["offset", "stop-color", "stop-opacity"]:
attributes[attrib_name] = SVGDB.attribute(attrib_name)
super()
Loading

0 comments on commit ea03774

Please sign in to comment.