diff --git a/data/configs/.gitkeep b/data/configs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/lib/backend.rb b/lib/backend.rb index 5411cbe..fe063ca 100644 --- a/lib/backend.rb +++ b/lib/backend.rb @@ -1,11 +1,13 @@ module TAC class Backend - attr_reader :config, :tacnet + attr_reader :config, :settings, :tacnet def initialize @config = load_config + @settings = load_settings @tacnet = TACNET.new @config_changed = false + @settings_changed = false end def config_changed! @@ -82,5 +84,44 @@ module TAC def refresh_tacnet_status $window.current_state.refresh_tacnet_status end + + + def settings_changed! + @settings_changed = true + end + + def settings_changed? + @settings_changed + end + + def load_settings + if File.exist?(TAC::SETTINGS_PATH) + return TAC::Settings.new + else + write_default_settings + load_settings + end + end + + def save_settings + json = @settings.to_json + + File.open(TAC::SETTINGS_PATH, "w") { |f| f.write json } + + @settings_changed = false + end + + def write_default_settings + File.open(TAC::SETTINGS_PATH, "w") do |f| + f.write JSON.dump( + { + data: { + hostname: TACNET::DEFAULT_HOSTNAME, + port: TACNET::DEFAULT_PORT, + } + } + ) + end + end end end \ No newline at end of file diff --git a/lib/config.rb b/lib/config.rb index 56aa4a6..e53c571 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -38,12 +38,11 @@ module TAC end class Configuration - attr_accessor :created_at, :updated_at, :spec_version, :hostname, :port + attr_accessor :created_at, :updated_at, :spec_version attr_reader :presets - def initialize(created_at:, updated_at:, spec_version:, hostname:, port:, presets:) + def initialize(created_at:, updated_at:, spec_version:, presets:) @created_at, @updated_at = created_at, updated_at @spec_version = spec_version - @hostname, @port = hostname, port @presets = presets end @@ -52,8 +51,6 @@ module TAC created_at: @created_at, updated_at: @updated_at, spec_version: @spec_version, - hostname: @hostname, - port: @port, presets: @presets }.to_json(*args) end @@ -61,8 +58,7 @@ module TAC def self.from_json(hash) Configuration.new( created_at: hash[:created_at], updated_at: hash[:updated_at], - spec_version: hash[:spec_version], hostname: hash[:hostname], - port: hash[:port], presets: hash[:presets].map { |ps| Preset.from_json(ps) } + spec_version: hash[:spec_version], presets: hash[:presets].map { |ps| Preset.from_json(ps) } ) end end diff --git a/lib/settings.rb b/lib/settings.rb new file mode 100644 index 0000000..fa20a6b --- /dev/null +++ b/lib/settings.rb @@ -0,0 +1,24 @@ +module TAC + class Settings + attr_accessor :hostname, :port + def initialize + parse(File.read(TAC::SETTINGS_PATH)) + end + + def parse(json) + data = JSON.parse(json, symbolize_names: true) + + @hostname = data[:data][:hostname] + @port = data[:data][:port] + end + + def to_json(*args) + { + data: { + hostname: @hostname, + port: @port, + } + }.to_json(*args) + end + end +end \ No newline at end of file diff --git a/lib/states/editor.rb b/lib/states/editor.rb index c972c2b..62a101f 100644 --- a/lib/states/editor.rb +++ b/lib/states/editor.rb @@ -32,8 +32,9 @@ module TAC button get_image("#{TAC::ROOT_PATH}/media/icons/menuList.png"), image_width: THEME_ICON_SIZE, margin_left: 10, tip: "Manage presets" do push_state(ManagePresets) end - button get_image("#{TAC::ROOT_PATH}/media/icons/save.png"), image_width: THEME_ICON_SIZE, margin_left: 10, tip: "Save config to disk" do + button get_image("#{TAC::ROOT_PATH}/media/icons/save.png"), image_width: THEME_ICON_SIZE, margin_left: 10, tip: "Save config and settings to disk" do window.backend.save_config + window.backend.save_settings end button get_image("#{TAC::ROOT_PATH}/media/icons/export.png"), image_width: THEME_ICON_SIZE, margin_left: 10, tip: "Upload local config to remote, if connected." do window.backend.upload_config @@ -49,18 +50,18 @@ module TAC stack width: 0.5 do label "TACNET v#{TACNET::Packet::PROTOCOL_VERSION}", color: TAC::Palette::TACNET_PRIMARY, text_shadow: true, text_shadow_size: 1, text_shadow_color: Gosu::Color::BLACK flow width: 1.0 do - @tacnet_hostname = edit_line "#{window.backend.config.configuration.hostname}", width: 0.5, margin_right: 0 + @tacnet_hostname = edit_line "#{window.backend.settings.hostname}", width: 0.5, margin_right: 0 @tacnet_hostname.subscribe(:changed) do |caller, value| - window.backend.config.configuration.hostname = value - window.backend.config_changed! + window.backend.settings.hostname = value + window.backend.settings_changed! end label ":", margin: 0, padding: 0, padding_top: 3 - @tacnet_port = edit_line "#{window.backend.config.configuration.port}", width: 0.2, margin_left: 0 + @tacnet_port = edit_line "#{window.backend.settings.port}", width: 0.2, margin_left: 0 @tacnet_port.subscribe(:changed) do |caller, value| - window.backend.config.configuration.port = Integer(value) - window.backend.config_changed! + window.backend.settings.port = Integer(value) + window.backend.settings_changed! end end end diff --git a/lib/tac.rb b/lib/tac.rb index dcf3f79..77675c7 100644 --- a/lib/tac.rb +++ b/lib/tac.rb @@ -1,6 +1,7 @@ module TAC ROOT_PATH = File.expand_path("../..", __FILE__) CONFIG_PATH = "#{ROOT_PATH}/data/config.json" + SETTINGS_PATH = "#{ROOT_PATH}/data/settings.json" CONFIG_SPEC_VERSION = 2 end \ No newline at end of file diff --git a/lib/window.rb b/lib/window.rb index 2e40716..a2b4c98 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -15,7 +15,9 @@ module TAC def close if @backend.config_changed? - push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Config has unsaved changes!", callback_method: proc { cleanup_and_close }) + push_state(Dialog::ConfirmDialog, title: "Unsaved Config", message: "Config has unsaved changes\nthat will be lost if\nyou continue!", callback_method: proc { cleanup_and_close }) + elsif @backend.settings_changed? + push_state(Dialog::ConfirmDialog, title: "Unsaved Settings", message: "Settings has unsaved changes\nthat will be lost if\nyou continue!", callback_method: proc { cleanup_and_close }) else cleanup_and_close end diff --git a/timecrafters_configuration_tool.rb b/timecrafters_configuration_tool.rb index 3ca502c..c13cdde 100644 --- a/timecrafters_configuration_tool.rb +++ b/timecrafters_configuration_tool.rb @@ -9,6 +9,7 @@ require_relative "lib/window" require_relative "lib/version" require_relative "lib/backend" require_relative "lib/config" +require_relative "lib/settings" require_relative "lib/states/boot" require_relative "lib/states/editor" require_relative "lib/states/simulator"