diff --git a/data/config.json b/data/config.json deleted file mode 100644 index 4ae889d..0000000 --- a/data/config.json +++ /dev/null @@ -1 +0,0 @@ -{"config":{"spec_version":2,"hostname":"192.168.49.1","port":8962,"presets":[]},"data":{"groups":[],"actions":[],"values":[]}} \ No newline at end of file diff --git a/lib/backend.rb b/lib/backend.rb index c4dafd9..55dc311 100644 --- a/lib/backend.rb +++ b/lib/backend.rb @@ -8,13 +8,28 @@ module TAC def load_config if File.exist?(TAC::CONFIG_PATH) - JSON.parse(File.read( TAC::CONFIG_PATH )) + return JSON.parse(File.read( TAC::CONFIG_PATH ), symbolize_names: true) else write_default_config load_config end end + def update_config + @config = load_config + $window.current_state.populate_groups_list + end + + def save_config + json = JSON.dump(@config) + + File.open(TAC::CONFIG_PATH, "w") { |f| f.write json } + + if @tacnet.connected? + @tacnet.puts(TAC::TACNET::PacketHandler.packet_dump_config(json)) + end + end + def write_default_config File.open(TAC::CONFIG_PATH, "w") do |f| f.write JSON.dump( diff --git a/lib/dialog.rb b/lib/dialog.rb index 8a1943e..d7ca399 100644 --- a/lib/dialog.rb +++ b/lib/dialog.rb @@ -6,6 +6,7 @@ module TAC @title = @options[:title] ? @options[:title] : "#{self.class}" @window_width, @window_height = window.width, window.height + @previous_state = window.previous_state @dialog_root = stack width: 250, height: 400, border_thickness: 2, border_color: [TAC::Palette::TIMECRAFTERS_PRIMARY, TAC::Palette::TIMECRAFTERS_SECONDARY] do # Title bar @@ -43,7 +44,7 @@ module TAC end def draw - $window.previous_state.draw + @previous_state.draw Gosu.flush super diff --git a/lib/dialogs/alert_dialog.rb b/lib/dialogs/alert_dialog.rb new file mode 100644 index 0000000..3b6a242 --- /dev/null +++ b/lib/dialogs/alert_dialog.rb @@ -0,0 +1,14 @@ +module TAC + class Dialog + class AlertDialog < Dialog + def build + background Gosu::Color::GRAY + label @options[:message], text_size: 18 + + button "Close", width: 1.0, text_size: 18 do + close + end + end + end + end +end \ No newline at end of file diff --git a/lib/dialogs/name_prompt_dialog.rb b/lib/dialogs/name_prompt_dialog.rb index 9f14c1e..97d5f57 100644 --- a/lib/dialogs/name_prompt_dialog.rb +++ b/lib/dialogs/name_prompt_dialog.rb @@ -3,20 +3,24 @@ module TAC class NamePromptDialog < Dialog def build background Gosu::Color::GRAY - label @options[:subtitle] - flow width: 1.0 do - label "Name", width: 0.25 - edit_line "", width: 0.70 + label "Name", width: 0.25, text_size: 18 + @name = edit_line "", width: 0.70, text_size: 18 end flow width: 1.0 do - button "Cancel", width: 0.475 do + button "Cancel", width: 0.475, text_size: 18 do close end - button @options[:submit_label], width: 0.475 do - @options[:callback].call(self) + button @options[:submit_label], width: 0.475, text_size: 18 do + if @name.value.strip.empty? + push_state(TAC::Dialog::AlertDialog, title: "Error", message: "Name cannot be blank.\nName cannot only be whitespace.") + else + @options[:callback_method].call(@name.value.strip) + + close + end end end end diff --git a/lib/states/editor.rb b/lib/states/editor.rb index 17bee5a..f71acca 100644 --- a/lib/states/editor.rb +++ b/lib/states/editor.rb @@ -2,6 +2,9 @@ module TAC class States class Editor < CyberarmEngine::GuiState def setup + @active_group = nil + @active_action = nil + theme(THEME) stack width: 1.0, height: 1.0 do @@ -10,19 +13,7 @@ module TAC flow width: 1.0, height: 1.0 do stack width: 0.70 do - label TAC::NAME, color: Gosu::Color::BLACK, bold: true - - flow do - button "Add Group", text_size: 18 do - push_state(TAC::Dialog::NamePromptDialog, title: "Create Group", subtitle: "Add Group", submit_label: "Add", callback: proc {|instance| instance.close }) - end - button "Add Action", text_size: 18 do - push_state(TAC::Dialog::NamePromptDialog, title: "Create Action", subtitle: "Add Action", submit_label: "Add", callback: proc {|instance| instance.close }) - end - button "Add Value", text_size: 18 do - push_state(TAC::Dialog::NamePromptDialog, title: "Create Value", subtitle: "Add Value", submit_label: "Add", callback: proc {|instance| instance.close }) - end - end + label TAC::NAME, color: Gosu::Color.rgb(59, 200, 81), bold: true, text_size: 72 end flow width: 0.299 do @@ -44,26 +35,44 @@ module TAC flow width: 1.0, height: 0.9 do stack width: 0.2, height: 1.0 do background TAC::Palette::GROUPS_PRIMARY - label "Groups" + flow do + label "Groups" + button "Add Group", text_size: 18 do + push_state(TAC::Dialog::NamePromptDialog, title: "Create Group", submit_label: "Add", callback_method: method(:create_group)) + end + end @groups_list = stack width: 1.0 do - TAC::Storage.groups.each_with_index do |group, i| - button group.name, width: 1.0, text_size: 18 do - populate_actions_list(group.id) - end - end end end stack width: 0.2, height: 1.0 do background TAC::Palette::ACTIONS_PRIMARY - label "Actions" + flow do + label "Actions" + button "Add Action", text_size: 18 do + if @active_group + push_state(TAC::Dialog::NamePromptDialog, title: "Create Action", submit_label: "Add", callback_method: method(:create_action)) + else + push_state(TAC::Dialog::AlertDialog, title: "Error", message: "Unable to create action,\nno group selected.") + end + end + end @actions_list = stack width: 1.0 do end end stack width: 0.2, height: 1.0 do background TAC::Palette::VALUES_PRIMARY - label "Values" + flow do + label "Values" + button "Add Value", text_size: 18 do + if @active_action + push_state(TAC::Dialog::NamePromptDialog, title: "Create Value", subtitle: "Add Value", submit_label: "Add", callback_method: method(:create_value)) + else + push_state(TAC::Dialog::AlertDialog, title: "Error", message: "Unable to create value,\nno action selected.") + end + end + end @values_list = stack width: 1.0 do end @@ -77,6 +86,46 @@ module TAC end end end + + populate_groups_list + end + + def create_group(name) + window.backend.config[:data][:groups] << {id: rand(100), name: name} + window.backend.save_config + + populate_groups_list + end + + def create_action(name) + window.backend.config[:data][:actions] << {id: rand(100), group_id: @active_group.id, name: name, enabled: true} + window.backend.save_config + + populate_actions_list(@active_group.id) + end + + def create_value(name, type = :float, value = 45.0) + window.backend.config[:data][:values] << {id: rand(100), action_id: @active_action.id, name: name, type: type, value: value} + window.backend.save_config + + populate_values_list(@active_action.id) + end + + def populate_groups_list + groups = TAC::Storage.groups + + @groups_list.clear do + groups.each do |group| + button group.name, text_size: 18, width: 1.0 do + @active_group = group + @active_action = nil + + populate_actions_list(group.id) + @values_list.clear + @editor.clear + end + end + end end def populate_actions_list(group_id) @@ -85,7 +134,10 @@ module TAC @actions_list.clear do actions.each do |action| button action.name, text_size: 18, width: 1.0 do + @active_action = action + populate_values_list(action.id) + @editor.clear end end end diff --git a/lib/storage.rb b/lib/storage.rb index b27f176..e0b1c06 100644 --- a/lib/storage.rb +++ b/lib/storage.rb @@ -5,35 +5,19 @@ module TAC Value = Struct.new(:id, :action_id, :name, :type, :value) def self.groups - @@_g ||= Array.new(15) { |i| Group.new(i, Faker::Book.title) } + $window.backend.config[:data][:groups].map do |g| + Group.new(g[:id], g[:name]) + end end def self.actions(group_id) - @@_a ||= Array.new(100) { |i| Action.new(i, groups.sample.id, Faker::Space.meteorite, true) } - @@_a.select { |a| a.group_id == group_id } + $window.backend.config[:data][:actions].map{ |a| Action.new(a[:id], a[:group_id], a[:name], a[:enabled]) }.select { |a| a.group_id == group_id } end def self.values(action_id) types = [:double, :float, :string, :boolean, :integer] - @@_v ||= Array.new(500) do |i| - v = Value.new(i, rand(100), Faker::Space.meteorite, types.sample) - - v.value = case v.type - when :double, :float - rand(-1.0..1.0) - when :integer - rand(-1024..1024) - when :string - Faker::Quotes::Shakespeare.hamlet_quote - when :boolean - rand > 0.5 - end - - v - end - - @@_v.select { |a| a.action_id == action_id } + $window.backend.config[:data][:values].map { |v| Value.new(v[:id], v[:action_id], v[:name], v[:type].to_sym, v[:value]) }.select { |a| a.action_id == action_id } end end end \ No newline at end of file diff --git a/lib/tacnet.rb b/lib/tacnet.rb index bd4c285..60c76de 100644 --- a/lib/tacnet.rb +++ b/lib/tacnet.rb @@ -8,7 +8,6 @@ module TAC def initialize @connection = nil - @server = nil end def connect(hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT, error_callback = proc {}) @@ -17,5 +16,27 @@ module TAC @connection = Connection.new(hostname, port) @connection.connect(error_callback) end + + def connected? + @connection && @connection.connected? + end + + def client + if connected? + @connection.client + end + end + + def puts(packet) + if connected? + @connection.puts(packet) + end + end + + def gets + if connected? + @connection.gets + end + end end end \ No newline at end of file diff --git a/lib/tacnet/client.rb b/lib/tacnet/client.rb index 23a2d50..5088327 100644 --- a/lib/tacnet/client.rb +++ b/lib/tacnet/client.rb @@ -19,6 +19,10 @@ module TAC @data_sent, @data_received = 0, 0 end + def uuid=(id) + @uuid = id + end + def socket=(socket) @socket = socket diff --git a/lib/tacnet/connection.rb b/lib/tacnet/connection.rb index 5042e58..c5792c6 100644 --- a/lib/tacnet/connection.rb +++ b/lib/tacnet/connection.rb @@ -2,6 +2,7 @@ module TAC class TACNET class Connection TAG = "TACNET|Connection" + attr_reader :client def initialize(hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT) @hostname = hostname @port = port @@ -16,7 +17,7 @@ module TAC handle_connection end - @packet_handler = PacketHandler.new + @packet_handler = PacketHandler.new(host_is_a_connection: true) end def connect(error_callback) @@ -58,6 +59,14 @@ module TAC end end + def puts(packet) + @client.puts(packet) + end + + def gets + @client.gets + end + def connected? !closed? end diff --git a/lib/tacnet/packet_handler.rb b/lib/tacnet/packet_handler.rb index 12fb691..bdfae1f 100644 --- a/lib/tacnet/packet_handler.rb +++ b/lib/tacnet/packet_handler.rb @@ -31,7 +31,7 @@ module TAC def handle_handshake(packet) if @host_is_a_connection - # TODO: Set Connection client id to received uuid + $window.backend.tacnet.client.uuid = packet.body end end diff --git a/lib/tacnet/server.rb b/lib/tacnet/server.rb index 662ccdb..7d3c0b6 100644 --- a/lib/tacnet/server.rb +++ b/lib/tacnet/server.rb @@ -65,7 +65,6 @@ module TAC if @active_client && @active_client.connected? log.i(TAG, "Too many clients, already have one connected!") client.close("Too many clients!") - pp @active_client.connected? else @active_client = client # TODO: Backup local config diff --git a/tacnet_test_server.rb b/tacnet_test_server.rb index 7a45358..ab0773a 100644 --- a/tacnet_test_server.rb +++ b/tacnet_test_server.rb @@ -1,7 +1,9 @@ -require "gosu" +require "json" require "socket" require "securerandom" +require "gosu" + require_relative "lib/tac" require_relative "lib/logger" diff --git a/timecrafters_action_configurator.rb b/timecrafters_action_configurator.rb index 5fa4fc1..f1465a9 100644 --- a/timecrafters_action_configurator.rb +++ b/timecrafters_action_configurator.rb @@ -3,8 +3,6 @@ require "socket" require "securerandom" require "json" -require "faker" - require_relative "lib/tac" require_relative "lib/palette" require_relative "lib/window" @@ -15,6 +13,7 @@ require_relative "lib/states/editor" require_relative "lib/theme" require_relative "lib/logger" require_relative "lib/dialog" +require_relative "lib/dialogs/alert_dialog" require_relative "lib/dialogs/name_prompt_dialog" require_relative "lib/tacnet" require_relative "lib/tacnet/packet"