Config is now saved and synced when updated

This commit is contained in:
2020-06-07 22:47:53 -05:00
parent ab6edb7a58
commit 151df4ca57
14 changed files with 162 additions and 59 deletions

View File

@@ -1 +0,0 @@
{"config":{"spec_version":2,"hostname":"192.168.49.1","port":8962,"presets":[]},"data":{"groups":[],"actions":[],"values":[]}}

View File

@@ -8,13 +8,28 @@ module TAC
def load_config def load_config
if File.exist?(TAC::CONFIG_PATH) 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 else
write_default_config write_default_config
load_config load_config
end end
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 def write_default_config
File.open(TAC::CONFIG_PATH, "w") do |f| File.open(TAC::CONFIG_PATH, "w") do |f|
f.write JSON.dump( f.write JSON.dump(

View File

@@ -6,6 +6,7 @@ module TAC
@title = @options[:title] ? @options[:title] : "#{self.class}" @title = @options[:title] ? @options[:title] : "#{self.class}"
@window_width, @window_height = window.width, window.height @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 @dialog_root = stack width: 250, height: 400, border_thickness: 2, border_color: [TAC::Palette::TIMECRAFTERS_PRIMARY, TAC::Palette::TIMECRAFTERS_SECONDARY] do
# Title bar # Title bar
@@ -43,7 +44,7 @@ module TAC
end end
def draw def draw
$window.previous_state.draw @previous_state.draw
Gosu.flush Gosu.flush
super super

View File

@@ -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

View File

@@ -3,20 +3,24 @@ module TAC
class NamePromptDialog < Dialog class NamePromptDialog < Dialog
def build def build
background Gosu::Color::GRAY background Gosu::Color::GRAY
label @options[:subtitle]
flow width: 1.0 do flow width: 1.0 do
label "Name", width: 0.25 label "Name", width: 0.25, text_size: 18
edit_line "", width: 0.70 @name = edit_line "", width: 0.70, text_size: 18
end end
flow width: 1.0 do flow width: 1.0 do
button "Cancel", width: 0.475 do button "Cancel", width: 0.475, text_size: 18 do
close close
end end
button @options[:submit_label], width: 0.475 do button @options[:submit_label], width: 0.475, text_size: 18 do
@options[:callback].call(self) 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 end
end end

View File

@@ -2,6 +2,9 @@ module TAC
class States class States
class Editor < CyberarmEngine::GuiState class Editor < CyberarmEngine::GuiState
def setup def setup
@active_group = nil
@active_action = nil
theme(THEME) theme(THEME)
stack width: 1.0, height: 1.0 do stack width: 1.0, height: 1.0 do
@@ -10,19 +13,7 @@ module TAC
flow width: 1.0, height: 1.0 do flow width: 1.0, height: 1.0 do
stack width: 0.70 do stack width: 0.70 do
label TAC::NAME, color: Gosu::Color::BLACK, bold: true label TAC::NAME, color: Gosu::Color.rgb(59, 200, 81), bold: true, text_size: 72
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
end end
flow width: 0.299 do flow width: 0.299 do
@@ -44,26 +35,44 @@ module TAC
flow width: 1.0, height: 0.9 do flow width: 1.0, height: 0.9 do
stack width: 0.2, height: 1.0 do stack width: 0.2, height: 1.0 do
background TAC::Palette::GROUPS_PRIMARY 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 @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
end end
stack width: 0.2, height: 1.0 do stack width: 0.2, height: 1.0 do
background TAC::Palette::ACTIONS_PRIMARY 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 @actions_list = stack width: 1.0 do
end end
end end
stack width: 0.2, height: 1.0 do stack width: 0.2, height: 1.0 do
background TAC::Palette::VALUES_PRIMARY 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 @values_list = stack width: 1.0 do
end end
@@ -77,6 +86,46 @@ module TAC
end end
end 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 end
def populate_actions_list(group_id) def populate_actions_list(group_id)
@@ -85,7 +134,10 @@ module TAC
@actions_list.clear do @actions_list.clear do
actions.each do |action| actions.each do |action|
button action.name, text_size: 18, width: 1.0 do button action.name, text_size: 18, width: 1.0 do
@active_action = action
populate_values_list(action.id) populate_values_list(action.id)
@editor.clear
end end
end end
end end

View File

@@ -5,35 +5,19 @@ module TAC
Value = Struct.new(:id, :action_id, :name, :type, :value) Value = Struct.new(:id, :action_id, :name, :type, :value)
def self.groups 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 end
def self.actions(group_id) def self.actions(group_id)
@@_a ||= Array.new(100) { |i| Action.new(i, groups.sample.id, Faker::Space.meteorite, true) } $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 }
@@_a.select { |a| a.group_id == group_id }
end end
def self.values(action_id) def self.values(action_id)
types = [:double, :float, :string, :boolean, :integer] types = [:double, :float, :string, :boolean, :integer]
@@_v ||= Array.new(500) do |i| $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 }
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 }
end end
end end
end end

View File

@@ -8,7 +8,6 @@ module TAC
def initialize def initialize
@connection = nil @connection = nil
@server = nil
end end
def connect(hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT, error_callback = proc {}) def connect(hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT, error_callback = proc {})
@@ -17,5 +16,27 @@ module TAC
@connection = Connection.new(hostname, port) @connection = Connection.new(hostname, port)
@connection.connect(error_callback) @connection.connect(error_callback)
end 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
end end

View File

@@ -19,6 +19,10 @@ module TAC
@data_sent, @data_received = 0, 0 @data_sent, @data_received = 0, 0
end end
def uuid=(id)
@uuid = id
end
def socket=(socket) def socket=(socket)
@socket = socket @socket = socket

View File

@@ -2,6 +2,7 @@ module TAC
class TACNET class TACNET
class Connection class Connection
TAG = "TACNET|Connection" TAG = "TACNET|Connection"
attr_reader :client
def initialize(hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT) def initialize(hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT)
@hostname = hostname @hostname = hostname
@port = port @port = port
@@ -16,7 +17,7 @@ module TAC
handle_connection handle_connection
end end
@packet_handler = PacketHandler.new @packet_handler = PacketHandler.new(host_is_a_connection: true)
end end
def connect(error_callback) def connect(error_callback)
@@ -58,6 +59,14 @@ module TAC
end end
end end
def puts(packet)
@client.puts(packet)
end
def gets
@client.gets
end
def connected? def connected?
!closed? !closed?
end end

View File

@@ -31,7 +31,7 @@ module TAC
def handle_handshake(packet) def handle_handshake(packet)
if @host_is_a_connection if @host_is_a_connection
# TODO: Set Connection client id to received uuid $window.backend.tacnet.client.uuid = packet.body
end end
end end

View File

@@ -65,7 +65,6 @@ module TAC
if @active_client && @active_client.connected? if @active_client && @active_client.connected?
log.i(TAG, "Too many clients, already have one connected!") log.i(TAG, "Too many clients, already have one connected!")
client.close("Too many clients!") client.close("Too many clients!")
pp @active_client.connected?
else else
@active_client = client @active_client = client
# TODO: Backup local config # TODO: Backup local config

View File

@@ -1,7 +1,9 @@
require "gosu" require "json"
require "socket" require "socket"
require "securerandom" require "securerandom"
require "gosu"
require_relative "lib/tac" require_relative "lib/tac"
require_relative "lib/logger" require_relative "lib/logger"

View File

@@ -3,8 +3,6 @@ require "socket"
require "securerandom" require "securerandom"
require "json" require "json"
require "faker"
require_relative "lib/tac" require_relative "lib/tac"
require_relative "lib/palette" require_relative "lib/palette"
require_relative "lib/window" require_relative "lib/window"
@@ -15,6 +13,7 @@ require_relative "lib/states/editor"
require_relative "lib/theme" require_relative "lib/theme"
require_relative "lib/logger" require_relative "lib/logger"
require_relative "lib/dialog" require_relative "lib/dialog"
require_relative "lib/dialogs/alert_dialog"
require_relative "lib/dialogs/name_prompt_dialog" require_relative "lib/dialogs/name_prompt_dialog"
require_relative "lib/tacnet" require_relative "lib/tacnet"
require_relative "lib/tacnet/packet" require_relative "lib/tacnet/packet"