diff --git a/lib/config.rb b/lib/config.rb index d4cb251..bee9974 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -130,13 +130,30 @@ module TAC end class Variable - attr_accessor :name, :value - def initialize(name:, value:) - @name, @value = name, value + attr_accessor :name, :type, :value + def initialize(name:, type:, value:) + @name, @type, @value = name, type, value end - def raw_type - case @value.split("x").first.upcase + def to_json(*args) + { + name: @name, + value: "#{Variable.encode_type(@type)}x#{@value}" + }.to_json(*args) + end + + def self.from_json(hash) + type, value = hash[:value].split("x", 2) + type = Variable.decode_type(type) + Variable.new(name: hash[:name], type: type, value: value) + end + + def self.encode_type(symbol) + symbol.to_s.chars.first.upcase + end + + def self.decode_type(character) + case character.upcase when "I" :integer when "F" @@ -152,36 +169,18 @@ module TAC 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" + def self.decode_value(type, string) + case type + when "I", "L", :integer, :long + Integer(string) + when "F", "D", :float, :double + Float(string) + when "S", :string + string + when "B", :boolean + string.downcase == "true" end end - - def to_json(*args) - { - name: @name, - value: @value - }.to_json(*args) - end - - def self.from_json(hash) - Variable.new(name: hash[:name], value: hash[:value]) - end - - def self.encode_type(symbol) - symbol.to_s.chars.first.upcase - end end end end \ No newline at end of file diff --git a/lib/dialogs/variable_dialog.rb b/lib/dialogs/variable_dialog.rb index c0142fe..7f588d9 100644 --- a/lib/dialogs/variable_dialog.rb +++ b/lib/dialogs/variable_dialog.rb @@ -4,31 +4,27 @@ module TAC def build background Gosu::Color::GRAY - @type = @options[:variable].raw_type if @options[:variable] + @type = @options[:variable].type if @options[:variable] label "Name" @name_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @name_error.hide - @name = edit_line @options[:variable] ? @options[:variable].name : "" + @name = edit_line @options[:variable] ? @options[:variable].name : "", width: 1.0 label "Type" @type_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @type_error.hide - # TODO: Add dropdown menus to CyberarmEngine - flow width: 1.0 do - [:float, :double, :integer, :long, :string, :boolean].each do |btn| - button btn do - @type = btn - @value_container.show - end - end + + @var_type = list_box items: [:float, :double, :integer, :long, :string, :boolean], choose: @type ? @type : :double, width: 1.0 do |item| + @type = item end @value_container = stack width: 1.0 do label "Value" @value_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @value_error.hide - @value = edit_line @options[:variable] ? @options[:variable].raw_value : "" + @value = edit_line @options[:variable] ? @options[:variable].value : "", width: 1.0 + @value_boolean = check_box "Variable", checked: @options[:variable] ? @options[:variable].value == true : false end flow width: 1.0 do @@ -39,9 +35,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, "#{TAC::Config::Variable.encode_type(@type)}x#{@value.value.strip}") + @options[:callback_method].call(@options[:variable], @name.value.strip, @type, @value.value.strip) else - @options[:callback_method].call(@name.value.strip, "#{TAC::Config::Variable.encode_type(@type)}x#{@value.value.strip}") + @options[:callback_method].call(@name.value.strip, @type, @value.value.strip) end close @@ -76,6 +72,7 @@ module TAC @value_error.value = "Error: Value cannot be blank\n or only whitespace." @value_error.show valid = false + elsif [:integer, :long].include?(@type) begin Integer(@value.value.strip) @@ -84,6 +81,7 @@ module TAC @value_error.show valid = false end + elsif [:float, :double].include?(@type) begin Float(@value.value.strip) @@ -96,16 +94,19 @@ module TAC @value_error.value = "" @value_error.hide end + elsif @type == :string if @value.value.strip.empty? @value_error.value = "Error: Value cannot be blank\n or only whitespace." @value_error.show valid = false end + elsif @type == :boolean @value_error.value = "Error: Boolean not yet supported." @value_error.show valid = false + else @value_error.value = "Error: Type not set." @value_error.show @@ -116,4 +117,4 @@ module TAC end end end -end \ No newline at end of file +end diff --git a/lib/states/editor.rb b/lib/states/editor.rb index d7aa8b9..1f03d80 100644 --- a/lib/states/editor.rb +++ b/lib/states/editor.rb @@ -269,8 +269,9 @@ module TAC populate_variables_list(@active_action) end - def update_variable(variable, name, value) + def update_variable(variable, name, type, value) variable.name = name + variable.type = type variable.value = value window.backend.config_changed! @@ -354,7 +355,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.raw_type}, Value: #{variable.raw_value}]", width: 0.925, tip: "Edit variable" do + button "#{variable.name} [Type: #{variable.type}, Value: #{variable.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