mirror of
https://github.com/mattrberry/crab.git
synced 2024-11-16 19:49:30 +01:00
minor file dialog refactor
This commit is contained in:
parent
e79d8528a9
commit
5383a1b57b
2 changed files with 22 additions and 32 deletions
|
@ -88,7 +88,6 @@ class SDLOpenGLImGuiFrontend < Frontend
|
|||
render_imgui
|
||||
LibSDL.gl_swap_window(@window)
|
||||
update_draw_count
|
||||
handle_file_selection
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -103,23 +102,9 @@ class SDLOpenGLImGuiFrontend < Frontend
|
|||
end
|
||||
end
|
||||
|
||||
private def handle_file_selection : Nil
|
||||
if selection = @file_explorer.selection
|
||||
file, symbol = selection
|
||||
if symbol == :ROM
|
||||
load_new_rom(file.to_s)
|
||||
elsif symbol == :BIOS
|
||||
load_new_bios(file.to_s)
|
||||
else
|
||||
abort "Internal error: Unexpected file explorer symbol #{symbol}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private def load_new_rom(rom : String?) : Nil
|
||||
LibSDL.close_audio(1)
|
||||
@controller = init_controller(nil, rom, @config.run_bios)
|
||||
@file_explorer.clear_selection
|
||||
|
||||
@config.recents.delete(rom)
|
||||
@config.recents.insert(0, rom)
|
||||
|
@ -143,7 +128,6 @@ class SDLOpenGLImGuiFrontend < Frontend
|
|||
abort "Internal error: Cannot set bios #{bios} for controller #{@controller}"
|
||||
end
|
||||
@config.commit
|
||||
@file_explorer.clear_selection
|
||||
end
|
||||
|
||||
private def handle_input : Nil
|
||||
|
@ -279,8 +263,12 @@ class SDLOpenGLImGuiFrontend < Frontend
|
|||
end
|
||||
end
|
||||
|
||||
@file_explorer.render(:ROM, open_rom_selection, ROM_EXTENSIONS)
|
||||
@file_explorer.render(:BIOS, open_bios_selection)
|
||||
@file_explorer.render("ROM", open_rom_selection, ROM_EXTENSIONS) do |path|
|
||||
load_new_rom(path.to_s)
|
||||
end
|
||||
@file_explorer.render("BIOS", open_bios_selection) do |path|
|
||||
load_new_bios(path.to_s)
|
||||
end
|
||||
|
||||
@keybindings.render(open_keybindings)
|
||||
|
||||
|
|
|
@ -11,18 +11,19 @@ module ImGui
|
|||
@open
|
||||
end
|
||||
|
||||
getter selection : Tuple(Path, Symbol)? = nil
|
||||
|
||||
def initialize(@config : Config)
|
||||
gather_entries
|
||||
end
|
||||
|
||||
def render(name : Symbol, open_popup : Bool, extensions : Array(String)? = nil) : Nil
|
||||
# Render a file selection dialog with title [name]. Files are filtered by
|
||||
# [estensions]. [handler] is executed and the dialog is closed when a file
|
||||
# is selected.
|
||||
def render(name : String, open_popup : Bool, extensions : Array(String)? = nil, &handler : Path -> _) : Nil
|
||||
@open ||= open_popup
|
||||
ImGui.open_popup(name.to_s) if open_popup
|
||||
ImGui.open_popup(name) if open_popup
|
||||
center = ImGui.get_main_viewport.get_center
|
||||
ImGui.set_next_window_pos(center, ImGui::ImGuiCond::Appearing, ImGui::ImVec2.new(0.5, 0.5))
|
||||
ImGui.popup_modal(name.to_s, flags: ImGui::ImGuiWindowFlags::AlwaysAutoResize) do
|
||||
ImGui.popup_modal(name, flags: ImGui::ImGuiWindowFlags::AlwaysAutoResize) do
|
||||
parts = @config.explorer_dir.parts
|
||||
parts.each_with_index do |part, idx|
|
||||
ImGui.same_line unless idx == 0
|
||||
|
@ -46,7 +47,10 @@ module ImGui
|
|||
if ImGui.selectable("[#{letter}] #{entry[:name]}#{'/' unless entry[:file?]}", is_selected, flags)
|
||||
if entry[:file?]
|
||||
@selected_entry_idx = idx
|
||||
open_file(name) if ImGui.is_mouse_double_clicked(ImGui::ImGuiMouseButton::Left)
|
||||
if ImGui.is_mouse_double_clicked(ImGui::ImGuiMouseButton::Left)
|
||||
yield selected_path
|
||||
close
|
||||
end
|
||||
elsif ImGui.is_mouse_double_clicked(ImGui::ImGuiMouseButton::Left)
|
||||
change_dir entry[:name]
|
||||
end
|
||||
|
@ -55,7 +59,10 @@ module ImGui
|
|||
end
|
||||
end
|
||||
ImGui.group do
|
||||
open_file(name) if ImGui.button "Open"
|
||||
if ImGui.button "Open"
|
||||
yield selected_path
|
||||
close
|
||||
end
|
||||
ImGui.same_line
|
||||
close if ImGui.button "Cancel"
|
||||
ImGui.same_line(spacing: 10)
|
||||
|
@ -69,13 +76,8 @@ module ImGui
|
|||
ImGui.close_current_popup
|
||||
end
|
||||
|
||||
def clear_selection : Nil
|
||||
@selection = nil
|
||||
end
|
||||
|
||||
private def open_file(name : Symbol) : Nil
|
||||
@selection = {(@config.explorer_dir / @matched_entries[@selected_entry_idx][:name]).normalize, name}
|
||||
close
|
||||
private def selected_path : Path
|
||||
(@config.explorer_dir / @matched_entries[@selected_entry_idx][:name]).normalize
|
||||
end
|
||||
|
||||
private def change_dir(name : String) : Nil
|
||||
|
|
Loading…
Reference in a new issue