Updated VariableDialog to use list_box instead of several buttons, make Variable store real type instead of mashing everything into value

This commit is contained in:
2020-08-22 16:59:58 -05:00
parent d3fdc2d7dd
commit 08ada79e5b
3 changed files with 50 additions and 49 deletions

View File

@@ -130,13 +130,30 @@ module TAC
end end
class Variable class Variable
attr_accessor :name, :value attr_accessor :name, :type, :value
def initialize(name:, value:) def initialize(name:, type:, value:)
@name, @value = name, value @name, @type, @value = name, type, value
end end
def raw_type def to_json(*args)
case @value.split("x").first.upcase {
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" when "I"
:integer :integer
when "F" when "F"
@@ -152,36 +169,18 @@ module TAC
end end
end end
def raw_value def self.decode_value(type, string)
split = @value.split("x") case type
v = split.last when "I", "L", :integer, :long
Integer(string)
case split.first when "F", "D", :float, :double
when "I", "L" Float(string)
Integer(v) when "S", :string
when "F", "D" string
Float(v) when "B", :boolean
when "S" string.downcase == "true"
v
when "B"
v.downcase == "true"
end end
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 end
end end

View File

@@ -4,31 +4,27 @@ module TAC
def build def build
background Gosu::Color::GRAY background Gosu::Color::GRAY
@type = @options[:variable].raw_type if @options[:variable] @type = @options[:variable].type if @options[:variable]
label "Name" label "Name"
@name_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @name_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR
@name_error.hide @name_error.hide
@name = edit_line @options[:variable] ? @options[:variable].name : "" @name = edit_line @options[:variable] ? @options[:variable].name : "", width: 1.0
label "Type" label "Type"
@type_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @type_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR
@type_error.hide @type_error.hide
# TODO: Add dropdown menus to CyberarmEngine
flow width: 1.0 do @var_type = list_box items: [:float, :double, :integer, :long, :string, :boolean], choose: @type ? @type : :double, width: 1.0 do |item|
[:float, :double, :integer, :long, :string, :boolean].each do |btn| @type = item
button btn do
@type = btn
@value_container.show
end
end
end end
@value_container = stack width: 1.0 do @value_container = stack width: 1.0 do
label "Value" label "Value"
@value_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR @value_error = label "Error", color: TAC::Palette::TACNET_CONNECTION_ERROR
@value_error.hide @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 end
flow width: 1.0 do flow width: 1.0 do
@@ -39,9 +35,9 @@ module TAC
button @options[:variable] ? "Update" : "Add", width: 0.475 do |b| button @options[:variable] ? "Update" : "Add", width: 0.475 do |b|
if valid? if valid?
if @options[:variable] 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 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 end
close close
@@ -76,6 +72,7 @@ module TAC
@value_error.value = "Error: Value cannot be blank\n or only whitespace." @value_error.value = "Error: Value cannot be blank\n or only whitespace."
@value_error.show @value_error.show
valid = false valid = false
elsif [:integer, :long].include?(@type) elsif [:integer, :long].include?(@type)
begin begin
Integer(@value.value.strip) Integer(@value.value.strip)
@@ -84,6 +81,7 @@ module TAC
@value_error.show @value_error.show
valid = false valid = false
end end
elsif [:float, :double].include?(@type) elsif [:float, :double].include?(@type)
begin begin
Float(@value.value.strip) Float(@value.value.strip)
@@ -96,16 +94,19 @@ module TAC
@value_error.value = "" @value_error.value = ""
@value_error.hide @value_error.hide
end end
elsif @type == :string elsif @type == :string
if @value.value.strip.empty? if @value.value.strip.empty?
@value_error.value = "Error: Value cannot be blank\n or only whitespace." @value_error.value = "Error: Value cannot be blank\n or only whitespace."
@value_error.show @value_error.show
valid = false valid = false
end end
elsif @type == :boolean elsif @type == :boolean
@value_error.value = "Error: Boolean not yet supported." @value_error.value = "Error: Boolean not yet supported."
@value_error.show @value_error.show
valid = false valid = false
else else
@value_error.value = "Error: Type not set." @value_error.value = "Error: Type not set."
@value_error.show @value_error.show

View File

@@ -269,8 +269,9 @@ module TAC
populate_variables_list(@active_action) populate_variables_list(@active_action)
end end
def update_variable(variable, name, value) def update_variable(variable, name, type, value)
variable.name = name variable.name = name
variable.type = type
variable.value = value variable.value = value
window.backend.config_changed! window.backend.config_changed!
@@ -354,7 +355,7 @@ module TAC
flow width: 1.0, **THEME_ITEM_CONTAINER_PADDING do flow width: 1.0, **THEME_ITEM_CONTAINER_PADDING do
background i.even? ? THEME_EVEN_COLOR : THEME_ODD_COLOR 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)) push_state(Dialog::VariableDialog, title: "Edit Variable", variable: variable, callback_method: method(:update_variable))
end end
button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, tip: "Delete variable", **THEME_DANGER_BUTTON do button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, tip: "Delete variable", **THEME_DANGER_BUTTON do