Skip to content

Commit

Permalink
Fix path popup command disabling (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur authored Nov 7, 2024
1 parent f7230f5 commit becb681
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
57 changes: 43 additions & 14 deletions src/autoload/Indications.gd
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,12 @@ func get_selection_context(popup_method: Callable, context: Context) -> ContextP
TranslationServer.translate("Insert After"),
popup_insert_command_after_context.bind(popup_method), false,
load("res://visual/icons/Plus.svg")))
btn_arr.append(ContextPopup.create_button(
TranslationServer.translate("Convert To"),
popup_convert_to_context.bind(popup_method), false,
load("res://visual/icons/Reload.svg")))
if inner_selections[0] != 0 or\
element_ref.get_attribute("d").get_command(0).command_char != "M":
btn_arr.append(ContextPopup.create_button(
TranslationServer.translate("Convert To"),
popup_convert_to_context.bind(popup_method), false,
load("res://visual/icons/Reload.svg")))
"polygon", "polyline":
if inner_selections.size() == 1:
btn_arr.append(ContextPopup.create_button(
Expand Down Expand Up @@ -606,27 +608,54 @@ func popup_convert_to_context(popup_method: Callable) -> void:
context_popup.setup(btn_arr, true)
popup_method.call(context_popup)
elif not inner_selections.is_empty() and not semi_selected_xid.is_empty():
var cmd_char: String = SVG.root_element.get_xnode(semi_selected_xid).\
get_attribute("d").get_command(inner_selections[0]).command_char
var path_attrib: AttributePathdata =\
SVG.root_element.get_xnode(semi_selected_xid).get_attribute("d")
var selection_idx: int = inner_selections.max()
var cmd_char := path_attrib.get_command(selection_idx).command_char

var command_picker = PathCommandPopup.instantiate()
popup_method.call(command_picker)
command_picker.force_relativity(Utils.is_string_lower(cmd_char))
command_picker.disable_invalid([cmd_char.to_upper()])

var cmd_char_upper := cmd_char.to_upper()
var disabled_commands := PackedStringArray()
if selection_idx == 0:
disabled_commands = PackedStringArray(["L", "H", "V", "A", "Z", "Q", "T", "C", "S"])
else:
disabled_commands = PackedStringArray([cmd_char.to_upper()])
if cmd_char_upper != "Z" and\
path_attrib.get_command_count() > selection_idx + 1 and\
path_attrib.get_command(selection_idx + 1).command_char.to_upper() == "Z":
disabled_commands.append("Z")

command_picker.mark_invalid(PackedStringArray(), disabled_commands)
command_picker.path_command_picked.connect(convert_selected_command_to)

func popup_insert_command_after_context(popup_method: Callable) -> void:
var cmd_char: String = SVG.root_element.get_xnode(semi_selected_xid).\
get_attribute("d").get_command(inner_selections.max()).command_char
var path_attrib: AttributePathdata =\
SVG.root_element.get_xnode(semi_selected_xid).get_attribute("d")
var selection_idx: int = inner_selections.max()
var cmd_char := path_attrib.get_command(selection_idx).command_char

var command_picker = PathCommandPopup.instantiate()
popup_method.call(command_picker)
command_picker.path_command_picked.connect(insert_path_command_after_selection)
# Disable invalid commands. Z is syntactically invalid, so disallow it even harder.
var warned_commands := PackedStringArray()
var disabled_commands := PackedStringArray()
var disable_z := false
match cmd_char.to_upper():
"M": command_picker.disable_invalid(["M", "Z", "T"])
"Z": command_picker.disable_invalid(["Z"])
"L", "H", "V", "A": command_picker.disable_invalid(["S", "T"])
"C", "S": command_picker.disable_invalid(["T"])
"Q", "T": command_picker.disable_invalid(["S"])
"M": warned_commands = PackedStringArray(["M", "Z", "T"])
"Z": disable_z = true
"L", "H", "V", "A": warned_commands = PackedStringArray(["S", "T"])
"C", "S": warned_commands = PackedStringArray(["T"])
"Q", "T": warned_commands = PackedStringArray(["S"])

if disable_z or (path_attrib.get_command_count() > selection_idx and\
path_attrib.get_command(selection_idx + 1).command_char.to_upper() == "Z"):
disabled_commands = PackedStringArray(["Z"])

command_picker.mark_invalid(warned_commands, disabled_commands)

func convert_selected_element_to(element_name: String) -> void:
var xid := selected_xids[0]
Expand Down
12 changes: 11 additions & 1 deletion src/ui_widgets/path_command_button.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# A button for a path command picker.
extends Button

var warned := false

signal pressed_custom(cmd_char: String)

@export var command_char := ""
Expand All @@ -17,10 +19,18 @@ func set_invalid(new_state := true) -> void:
disabled = new_state
mouse_default_cursor_shape = CURSOR_ARROW if new_state else CURSOR_POINTING_HAND

func set_warning(new_state := true) -> void:
warned = true

# Couldn't think of any way to get RichTextLabel to autoresize its font on one line.
func _draw() -> void:
var text_obj := TextLine.new()
var text_color := Color(0.5, 0.5, 0.5) if disabled else Color(1, 1, 1)
var text_color := Color(1, 1, 1)
if disabled:
text_color = Color(0.5, 0.5, 0.5)
elif warned:
text_color = GlobalSettings.savedata.basic_color_warning

var left_margin := get_theme_stylebox("normal").content_margin_left
var right_margin := get_theme_stylebox("normal").content_margin_right
var max_size := size.x - left_margin - right_margin
Expand Down
9 changes: 5 additions & 4 deletions src/ui_widgets/path_popup.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ func _on_relative_toggle_toggled(toggled_on: bool) -> void:
else command_button.command_char.to_upper()
command_button.queue_redraw()

func disable_invalid(cmd_chars: Array) -> void:
for cmd_char in cmd_chars:
var cmd_char_upper: String = cmd_char.to_upper()
command_container.get_node(cmd_char_upper).set_invalid()
func mark_invalid(warned: PackedStringArray, disabled: PackedStringArray) -> void:
for cmd_char in warned:
command_container.get_node(cmd_char.to_upper()).set_warning()
for cmd_char in disabled:
command_container.get_node(cmd_char.to_upper()).set_invalid()

func force_relativity(relative: bool) -> void:
relative_toggle.hide()
Expand Down

0 comments on commit becb681

Please sign in to comment.