WIP: Refactoring Editor to no longer rebuild lists from scratch when changing most anything

This commit is contained in:
2023-01-31 10:14:16 -06:00
parent 308575dc63
commit 27d0b7314f

View File

@@ -232,11 +232,15 @@ module TAC
end
def create_group(name)
window.backend.config.groups << TAC::Config::Group.new(name: name, actions: [])
group = TAC::Config::Group.new(name: name, actions: [])
window.backend.config.groups << group
window.backend.config.groups.sort_by! { |g| g.name.downcase }
window.backend.config_changed!
populate_groups_list
scroll_into_view(group)
end
def update_group(group, name)
@@ -245,6 +249,8 @@ module TAC
window.backend.config_changed!
populate_groups_list
scroll_into_view(group)
end
def delete_group(group)
@@ -265,20 +271,32 @@ module TAC
end
def create_action(name, comment)
@active_group.actions << TAC::Config::Action.new(name: name, comment: comment, enabled: true, variables: [])
action = TAC::Config::Action.new(name: name, comment: comment, enabled: true, variables: [])
@active_group.actions << action
@active_group.actions.sort_by! { |a| a.name.downcase }
window.backend.config_changed!
populate_actions_list(@active_group)
scroll_into_view(action)
end
def update_action(action, name, comment)
old_name = action.name
action.name = name
action.comment = comment
@active_group.actions.sort_by! { |a| a.name.downcase }
window.backend.config_changed!
populate_actions_list(@active_group)
a = @actions_list.children.find { |a| a.style.tag == old_name.downcase }
label = a.children.find { |a| a.style.tag == "label" }
# comment = a.children.find { |a| a.style.tag == "comment" }
label.value = name
update_list_children(@actions_list)
scroll_into_view(action)
end
def delete_action(action)
@@ -290,15 +308,23 @@ module TAC
@active_action_label.value = ""
@variables_list.clear
populate_actions_list(@active_group)
# Remove deleted action from list
container = @actions_list.children.find { |a| a.style.tag == action.name.downcase }
@actions_list.remove(container)
update_list_children(@actions_list)
end
def create_variable(name, type, value)
@active_action.variables << TAC::Config::Variable.new(name: name, type: type, value: value)
variable = TAC::Config::Variable.new(name: name, type: type, value: value)
@active_action.variables << variable
@active_action.variables.sort_by! { |v| v.name.downcase }
window.backend.config_changed!
populate_variables_list(@active_action)
scroll_into_view(variable)
end
def update_variable(variable, name, type, value)
@@ -311,6 +337,8 @@ module TAC
window.backend.config_changed!
populate_variables_list(@active_action)
scroll_into_view(variable)
end
def delete_variable(variable)
@@ -321,6 +349,33 @@ module TAC
populate_variables_list(@active_action)
end
def update_list_children(list)
is_group = list == @groups_list
is_action = list == @actions_list
is_variable = list == @variables_list
list.children.sort_by! { |i| i.style.tag }
list.children.each_with_index do |child, i|
bg_color = i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
bg_color = THEME_HIGHLIGHTED_COLOR if is_group && @active_group&.name&.downcase == child.style.tag
bg_color = THEME_HIGHLIGHTED_COLOR if is_action && @active_action&.name&.downcase == child.style.tag
child.style.default[:background] = bg_color
child.root.gui_state.request_recalculate
end
end
def scroll_into_view(item)
if item.is_a?(TAC::Config::Group)
elsif item.is_a?(TAC::Config::Action)
elsif item.is_a?(TAC::Config::Variable)
else
raise "Unsupported item type: #{item.class}"
end
end
def populate_groups_list
@groups_list.scroll_top = 0
@@ -332,11 +387,11 @@ module TAC
@groups_list.clear do
groups.each_with_index do |group, i|
flow width: 1.0, height: 36, **THEME_ITEM_CONTAINER_PADDING do |container|
flow width: 1.0, height: 36, **THEME_ITEM_CONTAINER_PADDING, tag: group.name.downcase do |container|
background group == @active_group ? THEME_HIGHLIGHTED_COLOR : (i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR)
@active_group_container = container if group == @active_group
button group.name, fill: true, text_size: THEME_ICON_SIZE - 3 do
button group.name, fill: true, text_size: THEME_ICON_SIZE - 3, tag: "label" do
if (old_i = groups.index(@active_group))
@active_group_container.style.default[:background] = old_i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
end
@@ -371,12 +426,12 @@ module TAC
@actions_list.clear do
actions.each_with_index do |action, i|
stack width: 1.0, height: action.comment.empty? ? 36 : 72, **THEME_ITEM_CONTAINER_PADDING do |container|
stack width: 1.0, height: action.comment.empty? ? 36 : 72, **THEME_ITEM_CONTAINER_PADDING, tag: action.name.downcase do |container|
background action == @active_action ? THEME_HIGHLIGHTED_COLOR : (i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR)
@active_action_container = container if action == @active_action
flow width: 1.0, height: 36 do
button action.name, fill: true, text_size: THEME_ICON_SIZE - 3 do
button action.name, fill: true, text_size: THEME_ICON_SIZE - 3, tag: "label" do
if (old_i = actions.index(@active_action))
@active_action_container.style.default[:background] = old_i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
end
@@ -396,7 +451,7 @@ module TAC
end
button get_image("#{TAC::ROOT_PATH}/media/icons/gear.png"), image_width: THEME_ICON_SIZE, tip: "Edit action" do
push_state(Dialog::ActionDialog, title: "Rename Action", action: action, list: @active_group.actions, callback_method: method(:update_action))
push_state(Dialog::ActionDialog, title: "Edit Action", action: action, list: @active_group.actions, callback_method: method(:update_action))
end
button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, tip: "Delete action", **THEME_DANGER_BUTTON do
@@ -406,7 +461,7 @@ module TAC
unless action.comment.empty?
stack(width: 1.0, fill: true, scroll: true) do
caption action.comment.to_s, width: 1.0, text_wrap: :word_wrap, text_border: true, text_border_size: 1, text_border_color: 0xaa_000000
caption action.comment.to_s, width: 1.0, text_wrap: :word_wrap, text_border: true, text_border_size: 1, text_border_color: 0xaa_000000, tag: "comment"
end
end
end
@@ -421,11 +476,11 @@ module TAC
@variables_list.clear do
variables.each_with_index do |variable, i|
stack width: 1.0, height: 96, **THEME_ITEM_CONTAINER_PADDING do
stack width: 1.0, height: 96, **THEME_ITEM_CONTAINER_PADDING, tag: variable.name.downcase do
background i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR
flow(width: 1.0, fill: true) do
button "#{variable.name}", fill: true, text_size: THEME_ICON_SIZE - 3, tip: "Edit variable" do
button "#{variable.name}", fill: true, text_size: THEME_ICON_SIZE - 3, tip: "Edit variable", tag: "label" do
push_state(Dialog::VariableDialog, title: "Edit Variable", variable: variable, list: @active_action.variables, callback_method: method(:update_variable))
end
@@ -434,8 +489,8 @@ module TAC
end
end
caption "Type: #{variable.type}"
caption "Value: #{variable.value}"
caption "Type: #{variable.type}", tag: "type", fill: true
caption "Value: #{variable.value}", tag: "value", fill: true
end
end
end