Allow edited action to keep its name when editing it (fixes: #1), made dialogs respond to enter and escape, Confirm dialog now shows dark red color for dangerous operations and prevents enter for accepting it

This commit is contained in:
2021-02-10 22:21:53 -06:00
parent 9999026969
commit 3cc4c204a7
12 changed files with 94 additions and 44 deletions

View File

@@ -54,6 +54,9 @@ module TAC
text.match(/[A-Za-z0-9._\- ]/) ? text : ""
end
def try_commit
end
def draw
@previous_state.draw
Gosu.flush
@@ -73,6 +76,17 @@ module TAC
center_dialog
end
def button_down(id)
super
case id
when Gosu::KB_ENTER, Gosu::KB_RETURN
try_commit
when Gosu::KB_ESCAPE
close
end
end
def close
$window.pop_state
end

View File

@@ -18,32 +18,36 @@ module TAC
end
button @options[:action] ? @options[:accept_label] ? @options[:accept_label] : "Update" : "Add", width: 0.475 do |b|
if valid?
if @options[:action]
@options[:callback_method].call(@options[:action], @name.value.strip, @comment.value.strip)
else
@options[:callback_method].call(@name.value.strip, @comment.value.strip)
end
close
end
try_commit
end
end
end
def try_commit
if valid?
if @options[:action]
@options[:callback_method].call(@options[:action], @name.value.strip, @comment.value.strip)
else
@options[:callback_method].call(@name.value.strip, @comment.value.strip)
end
close
end
end
def valid?
valid = true
name = @name.value.strip
if name.empty?
@name_error.value = "Error: Name cannot be blank\n or only whitespace."
@name_error.value = "Error: Name cannot be blank or only whitespace."
@name_error.show
valid = false
### TODO: Handle case when renaming a cloned Action
# elsif @options[:action] && @options[:action].name == name
# @name_error.value = ""
# @name_error.hide
elsif !@options[:cloning] && @options[:action] && @options[:action].name == name
@name_error.value = ""
@name_error.hide
elsif @options[:list].find { |action| action.name == name}
@name_error.value = "Error: Name is not unique!"

View File

@@ -6,9 +6,13 @@ module TAC
label @options[:message]
button "Close", width: 1.0, margin_top: THEME_DIALOG_BUTTON_PADDING do
close
try_commit
end
end
def try_commit
close
end
end
end
end

View File

@@ -2,8 +2,10 @@ module TAC
class Dialog
class ConfirmDialog < Dialog
def build
@dialog_root.style.border_color = [ Palette::ALERT, darken(Palette::ALERT, 50) ]
@titlebar.style.background = [ Palette::ALERT, darken(Palette::ALERT, 50) ]
color = @options[:dangerous] ? Palette::DANGEROUS : Palette::ALERT
@dialog_root.style.border_color = [ color, darken(color, 50) ]
@titlebar.style.background = [ color, darken(color, 50) ]
background Gosu::Color::GRAY
label @options[:message]
@@ -12,7 +14,14 @@ module TAC
button "Cancel", width: 0.475 do
close
end
button "Proceed", width: 0.475, **TAC::THEME_DANGER_BUTTON do
try_commit(true)
end
end
def try_commit(force = false)
if not @options[:dangerous] || force
close
@options[:callback_method].call

View File

@@ -23,20 +23,23 @@ module TAC
accept_label = @options[:accept_label] if @options[:accept_label]
button accept_label, width: 0.475 do
unless valid?
else
if @options[:renaming]
@options[:callback_method].call(@options[:renaming], @name.value.strip)
else
@options[:callback_method].call(@name.value.strip)
end
close
end
try_commit
end
end
end
def try_commit
if valid?
if @options[:renaming]
@options[:callback_method].call(@options[:renaming], @name.value.strip)
else
@options[:callback_method].call(@name.value.strip)
end
close
end
end
def valid?
name = @name.value.strip

View File

@@ -18,6 +18,9 @@ module TAC
end
end
end
def try_commit
end
end
end
end

View File

@@ -11,10 +11,14 @@ module TAC
@sound = Gosu::Sample.new("#{TAC::ROOT_PATH}/media/error_alarm.ogg").play(1, 1, true)
button "Close", width: 1.0, margin_top: THEME_DIALOG_BUTTON_PADDING do
close
try_commit
end
end
def try_commit
close
end
def close
super

View File

@@ -6,7 +6,7 @@ module TAC
@message_label = label $window.backend.tacnet.full_status
button "Close", width: 1.0, margin_top: THEME_DIALOG_BUTTON_PADDING do
close
try_commit
end
@timer = CyberarmEngine::Timer.new(1000.0) do
@@ -14,6 +14,10 @@ module TAC
end
end
def try_commit
close
end
def update
super

View File

@@ -48,21 +48,25 @@ module TAC
end
button @options[:variable] ? "Update" : "Add", width: 0.475 do |b|
if valid?
value = @type == :boolean ? @value_boolean.value.to_s : @value.value.strip
if @options[:variable]
@options[:callback_method].call(@options[:variable], @name.value.strip, @type, value)
else
@options[:callback_method].call(@name.value.strip, @type, value)
end
close
end
try_commit
end
end
end
def try_commit
if valid?
value = @type == :boolean ? @value_boolean.value.to_s : @value.value.strip
if @options[:variable]
@options[:callback_method].call(@options[:variable], @name.value.strip, @type, value)
else
@options[:callback_method].call(@name.value.strip, @type, value)
end
close
end
end
def valid?
valid = true

View File

@@ -67,7 +67,7 @@ module TAC
end
button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, **THEME_DANGER_BUTTON, tip: "Delete configuration" do
push_state(Dialog::ConfirmDialog, title: "Delete Config?", callback_method: proc {
push_state(Dialog::ConfirmDialog, title: "Delete Config?", dangerous: true, callback_method: proc {
File.delete("#{TAC::CONFIGS_PATH}/#{name}.json")
if window.backend.settings.config == name

View File

@@ -102,7 +102,7 @@ module TAC
button get_image("#{TAC::ROOT_PATH}/media/icons/button2.png"), image_width: THEME_ICON_SIZE, tip: "Clone currently selected action" do
if @active_group && @active_action
push_state(Dialog::ActionDialog, title: "Clone Action", action: @active_action, accept_label: "Clone", list: @active_group.actions, callback_method: proc { |action, name, comment|
push_state(Dialog::ActionDialog, title: "Clone Action", action: @active_action, cloning: true, accept_label: "Clone", list: @active_group.actions, callback_method: proc { |action, name, comment|
clone = TAC::Config::Action.from_json( JSON.parse( @active_action.to_json, symbolize_names: true ))
clone.name = name
clone.comment = comment
@@ -135,7 +135,7 @@ module TAC
button get_image("#{TAC::ROOT_PATH}/media/icons/import.png"), image_width: THEME_ICON_SIZE, tip: "Import action from preset" do
if @active_group
push_state(Dialog::PickPresetDialog, title: "Pick Action Preset", limit: :actions, callback_method: proc { |preset|
push_state(Dialog::NamePromptDialog, title: "Name Action", renaming: preset, accept_label: "Add", list: @active_group.actions, callback_method: proc { |action, name|
push_state(Dialog::ActionDialog, title: "Name Action", action: preset, accept_label: "Add", list: @active_group.actions, callback_method: proc { |action, name|
clone = TAC::Config::Action.from_json( JSON.parse( action.to_json, symbolize_names: true ))
clone.name = "#{name}"
@active_group.actions << clone
@@ -335,7 +335,7 @@ module TAC
push_state(Dialog::NamePromptDialog, title: "Rename Group", renaming: group, list: window.backend.config.groups, callback_method: method(:update_group))
end
button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, tip: "Delete group", **THEME_DANGER_BUTTON do
push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete group and all of its actions and variables?", callback_method: proc { delete_group(group) })
push_state(Dialog::ConfirmDialog, dangerous: true, title: "Are you sure?", message: "Delete group and all of its actions and variables?", callback_method: proc { delete_group(group) })
end
end
end
@@ -371,7 +371,7 @@ module TAC
end
button get_image("#{TAC::ROOT_PATH}/media/icons/trashcan.png"), image_width: THEME_ICON_SIZE, tip: "Delete action", **THEME_DANGER_BUTTON do
push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete action and all\nof its variables?", callback_method: proc { delete_action(action) })
push_state(Dialog::ConfirmDialog, dangerous: true, title: "Are you sure?", message: "Delete action and all of its variables?", callback_method: proc { delete_action(action) })
end
end

View File

@@ -28,5 +28,6 @@ module TAC
EDITOR_SECONDARY = Gosu::Color.new(0xff_224466)
ALERT = TACNET_CONNECTING
DANGEROUS = TACNET_CONNECTION_ERROR
end
end