Skip to content

Commit

Permalink
esc closes open windows even if viewport is focused
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Oct 27, 2024
1 parent 74576f3 commit 5db2510
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/models/states.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ const groups =
{Playing, Flying}
]

proc resolve_flags(self: GameState) =
proc resolve_flags*(
self: GameState, wants: seq[LocalStateFlags]
): set[LocalStateFlags] =
debug "resolving flags",
flags = self.local_flags.value, wants = self.wants.value
var result: set[LocalStateFlags]
for flag in self.wants:
for flag in wants:
for group in groups:
if flag in group:
for f in group:
Expand Down Expand Up @@ -59,6 +60,9 @@ proc resolve_flags(self: GameState) =
result.excl(ReticleVisible)

debug "resolved flags", flags = result

proc resolve_flags(self: GameState) =
let result = self.resolve_flags(self.wants.value)
self.local_flags.value = result

proc replace_flags*(self: GameState, flags: varargs[LocalStateFlags]) =
Expand Down Expand Up @@ -90,6 +94,12 @@ proc pop_flags*(self: GameState, flags: varargs[LocalStateFlags]) =

self.resolve_flags

proc try_pop*(
self: GameState, flags: varargs[LocalStateFlags]
): set[LocalStateFlags] =
var wants = self.wants.value.filter_it(it notin flags)
return self.resolve_flags(wants)

proc pop_flag*(self: GameState, flag: LocalStateFlags) =
self.pop_flags flag

Expand All @@ -116,23 +126,6 @@ proc `-=`*(
proc selected_color*(self: GameState): Color =
action_colors[Colors(ord self.tool)]

proc logger*(level, msg: string) =
if level == "err":
debug "console visible"
state.push_flag ConsoleVisible
let msg = \"[b]{level.to_upper}[/b] {msg}"
debug "logging", msg
state.console.log += msg & "\n"

proc debug*(self: GameState, args: varargs[string, `$`]) =
logger("debug", args.join)

proc info*(self: GameState, args: varargs[string, `$`]) =
logger("info", args.join)

proc err*(self: GameState, args: varargs[string, `$`]) =
logger("err", \"[color=#FF0000]{args.join}[/color]")

proc init*(_: type GameState): GameState =
let flags = {SyncLocal}
let self = GameState(
Expand All @@ -152,6 +145,15 @@ proc init*(_: type GameState): GameState =
status_message_value: ~("", flags),
voxel_tasks_value: ~(0, flags),
)

self.logger = proc(level, msg: string) {.closure.} =
if level == "err":
debug "console visible"
state.push_flag ConsoleVisible
let msg = \"[b]{level.to_upper}[/b] {msg}"
debug "logging", msg
state.console.log += msg & "\n"

result = self
self.open_unit_value.changes:
if added and change.item != nil:
Expand Down
12 changes: 12 additions & 0 deletions src/ui/gui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ gdobj GUI of Control:
(state.nodes.player as PlayerNode).viewport_input(event)
self.accept_event()

method unhandled_input*(event: InputEvent) =
if CommandMode notin state.local_flags and
event.is_action_pressed("ui_cancel") and
ViewportFocused in state.local_flags:
let flags = state.try_pop(ViewportFocused)
if SettingsFocused in flags:
state.pop_flags SettingsFocused, SettingsVisible
elif EditorFocused in flags:
state.open_unit = nil
elif DocsFocused in flags:
state.open_sign = nil

method input(event: InputEvent) =
if event of InputEventScreenTouch:
let event = event as InputEventScreenTouch
Expand Down

0 comments on commit 5db2510

Please sign in to comment.