From 5e996019a9b682f3fcba0d835b5027978a81d785 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Sun, 28 Jun 2020 07:16:13 -0500 Subject: [PATCH] Enable action enabled toggle button, refactored how Variable value is stored --- lib/config.rb | 46 ++++++++++++++++++++++++++++++---- lib/dialogs/variable_dialog.rb | 8 +++--- lib/states/editor.rb | 11 +++++--- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/lib/config.rb b/lib/config.rb index fad2630..d4cb251 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -130,21 +130,57 @@ module TAC end class Variable - attr_accessor :name, :type, :value - def initialize(name:, type:, value:) - @name, @type, @value = name, type, value + attr_accessor :name, :value + def initialize(name:, value:) + @name, @value = name, value + end + + def raw_type + case @value.split("x").first.upcase + when "I" + :integer + when "F" + :float + when "D" + :double + when "L" + :long + when "S" + :string + when "B" + :boolean + end + end + + def raw_value + split = @value.split("x") + v = split.last + + case split.first + when "I", "L" + Integer(v) + when "F", "D" + Float(v) + when "S" + v + when "B" + v.downcase == "true" + end end def to_json(*args) { name: @name, - type: @type, value: @value }.to_json(*args) end def self.from_json(hash) - Variable.new(name: hash[:name], type: hash[:type].to_sym, value: hash[:value]) + Variable.new(name: hash[:name], value: hash[:value]) + end + + def self.encode_type(symbol) + symbol.to_s.chars.first.upcase end end end diff --git a/lib/dialogs/variable_dialog.rb b/lib/dialogs/variable_dialog.rb index 122ebaf..c0142fe 100644 --- a/lib/dialogs/variable_dialog.rb +++ b/lib/dialogs/variable_dialog.rb @@ -4,7 +4,7 @@ module TAC def build background Gosu::Color::GRAY - @type = @options[:variable].type if @options[:variable] + @type = @options[:variable].raw_type if @options[:variable] label "Name" @name_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @@ -28,7 +28,7 @@ module TAC label "Value" @value_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @value_error.hide - @value = edit_line @options[:variable] ? @options[:variable].value : "" + @value = edit_line @options[:variable] ? @options[:variable].raw_value : "" end flow width: 1.0 do @@ -39,9 +39,9 @@ module TAC button @options[:variable] ? "Update" : "Add", width: 0.475 do |b| if valid? if @options[:variable] - @options[:callback_method].call(@options[:variable], @name.value.strip, @type, @value.value.strip) + @options[:callback_method].call(@options[:variable], @name.value.strip, "#{TAC::Config::Variable.encode_type(@type)}x#{@value.value.strip}") else - @options[:callback_method].call(@name.value.strip, @type, @value.value.strip) + @options[:callback_method].call(@name.value.strip, "#{TAC::Config::Variable.encode_type(@type)}x#{@value.value.strip}") end close diff --git a/lib/states/editor.rb b/lib/states/editor.rb index 0413fab..d7aa8b9 100644 --- a/lib/states/editor.rb +++ b/lib/states/editor.rb @@ -269,9 +269,8 @@ module TAC populate_variables_list(@active_action) end - def update_variable(variable, name, type, value) + def update_variable(variable, name, value) variable.name = name - variable.type = type variable.value = value window.backend.config_changed! @@ -330,7 +329,11 @@ module TAC populate_variables_list(action) end - toggle_button tip: "Enable action" + action_enabled_toggle = toggle_button tip: "Enable action", checked: action.enabled + action_enabled_toggle.subscribe(:changed) do |sender, value| + action.enabled = value + window.backend.config_changed! + end button get_image("#{TAC::ROOT_PATH}/media/icons/gear.png"), image_width: THEME_ICON_SIZE, tip: "Edit action" do push_state(Dialog::NamePromptDialog, title: "Rename Action", renaming: action, list: @active_group.actions, callback_method: method(:update_action)) @@ -351,7 +354,7 @@ module TAC flow width: 1.0, **THEME_ITEM_CONTAINER_PADDING do background i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR - button "#{variable.name} [Type: #{variable.type}, Value: #{variable.value}]", width: 0.925, tip: "Edit variable" do + button "#{variable.name} [Type: #{variable.raw_type}, Value: #{variable.raw_value}]", width: 0.925, tip: "Edit variable" do push_state(Dialog::VariableDialog, title: "Edit Variable", variable: variable, callback_method: method(:update_variable)) end button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, tip: "Delete variable", **THEME_DANGER_BUTTON do