From b052a6d1308db52cda9a4a24a5ee802382945adf Mon Sep 17 00:00:00 2001 From: Matthew Berry Date: Wed, 28 Sep 2022 17:37:11 -0700 Subject: [PATCH] move to imgui 1.88 and utilize new crystal-imgui semantics --- shard.yml | 4 +- .../frontend/controllers/gba_controller.cr | 88 ++++++-------- .../frontend/sdl_opengl_imgui_frontend.cr | 114 +++++++++--------- .../common/frontend/widgets/file_explorer.cr | 20 ++- .../common/frontend/widgets/keybindings.cr | 3 +- 5 files changed, 100 insertions(+), 129 deletions(-) diff --git a/shard.yml b/shard.yml index 8ab099c..8933554 100644 --- a/shard.yml +++ b/shard.yml @@ -22,10 +22,10 @@ dependencies: github: nulldotpro/LibGL imgui: github: oprypin/crystal-imgui - tag: v1.87 + tag: v1.88 imgui-backends: github: mattrberry/crystal-imgui-backends - tag: v1.87 + tag: v1.88 development_dependencies: stumpy_png: diff --git a/src/crab/common/frontend/controllers/gba_controller.cr b/src/crab/common/frontend/controllers/gba_controller.cr index 2951ff7..b59dcd9 100644 --- a/src/crab/common/frontend/controllers/gba_controller.cr +++ b/src/crab/common/frontend/controllers/gba_controller.cr @@ -30,74 +30,54 @@ class GBAController < Controller def render_windows : Nil if @debug_window - ImGui.begin("Video", pointerof(@debug_window)) - if ImGui.begin_tab_bar "VideoTabBar" - render_palettes_tab_item if ImGui.begin_tab_item "Palettes" - ImGui.end_tab_bar + ImGui.window("Video", pointerof(@debug_window)) do + ImGui.tab_bar "VideoTabBar" do + render_palettes_tab_item + end end - ImGui.end end if @scheduler_window cycles = @emu.scheduler.cycles - ImGui.begin("Scheduler", pointerof(@scheduler_window)) - ImGui.text("Total cycles: #{cycles}") - if ImGui.begin_table("Table", 2) - ImGui.table_setup_column("Cycles") - ImGui.table_setup_column("Type") - ImGui.table_headers_row - @emu.scheduler.events.each do |event| - ImGui.table_next_row - ImGui.table_set_column_index 0 - ImGui.text_unformatted (event.cycles - cycles).to_s - ImGui.table_set_column_index 1 - ImGui.text_unformatted event.type.to_s + ImGui.window("Scheduler", pointerof(@scheduler_window)) do + ImGui.text("Total cycles: #{cycles}") + ImGui.table("Table", 2) do + ImGui.table_setup_column("Cycles") + ImGui.table_setup_column("Type") + ImGui.table_headers_row + @emu.scheduler.events.each do |event| + ImGui.table_next_row + ImGui.table_set_column_index 0 + ImGui.text_unformatted (event.cycles - cycles).to_s + ImGui.table_set_column_index 1 + ImGui.text_unformatted event.type.to_s + end end - ImGui.end_table end - ImGui.end end end private def render_palettes_tab_item : Nil - pram = @emu.ppu.pram.to_unsafe.as(UInt16*) - flags = ImGui::ImGuiColorEditFlags::NoAlpha | ImGui::ImGuiColorEditFlags::NoPicker | - ImGui::ImGuiColorEditFlags::NoOptions | ImGui::ImGuiColorEditFlags::NoInputs | - ImGui::ImGuiColorEditFlags::NoLabel | ImGui::ImGuiColorEditFlags::NoSidePreview | - ImGui::ImGuiColorEditFlags::NoDragDrop | ImGui::ImGuiColorEditFlags::NoBorder - with_style(ImGui::ImGuiStyleVar::ItemSpacing, ImGui::ImVec2.new(0, 0)) do - 2.times do |idx| - group do - 16.times do |palette_row| - 16.times do |palette_col| - color = (pram + 0x100 * idx)[palette_row * 16 + palette_col] - rgb = ImGui::ImVec4.new((color & 0x1F) / 0x1F, (color >> 5 & 0x1F) / 0x1F, (color >> 10 & 0x1F) / 0x1F, 1) - ImGui.color_button("", rgb, flags, ImGui::ImVec2.new(10, 10)) - ImGui.same_line unless palette_col == 15 + ImGui.tab_item("Palettes") do + pram = @emu.ppu.pram.to_unsafe.as(UInt16*) + flags = ImGui::ImGuiColorEditFlags::NoAlpha | ImGui::ImGuiColorEditFlags::NoPicker | + ImGui::ImGuiColorEditFlags::NoOptions | ImGui::ImGuiColorEditFlags::NoInputs | + ImGui::ImGuiColorEditFlags::NoLabel | ImGui::ImGuiColorEditFlags::NoSidePreview | + ImGui::ImGuiColorEditFlags::NoDragDrop | ImGui::ImGuiColorEditFlags::NoBorder + ImGui.with_style_var(ImGui::ImGuiStyleVar::ItemSpacing, ImGui::ImVec2.new(0, 0)) do + 2.times do |idx| + ImGui.group do + 16.times do |palette_row| + 16.times do |palette_col| + color = (pram + 0x100 * idx)[palette_row * 16 + palette_col] + rgb = ImGui::ImVec4.new((color & 0x1F) / 0x1F, (color >> 5 & 0x1F) / 0x1F, (color >> 10 & 0x1F) / 0x1F, 1) + ImGui.color_button("", rgb, flags, ImGui::ImVec2.new(10, 10)) + ImGui.same_line unless palette_col == 15 + end end end + ImGui.same_line(spacing: 4_f32) if idx == 0 end - ImGui.same_line(spacing: 4_f32) if idx == 0 end end - ImGui.end_tab_item - end - - def group(&) : Nil - ImGui.begin_group - yield - ImGui.end_group - end - - def child(*args, **kwargs, &) : Nil - if ImGui.begin_child(*args, **kwargs) - yield - end - ImGui.end_child - end - - def with_style(*args, **kwargs, &) : Nil - ImGui.push_style_var(*args, **kwargs) - yield - ImGui.pop_style_var end end diff --git a/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr b/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr index e69e253..036db67 100644 --- a/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr +++ b/src/crab/common/frontend/sdl_opengl_imgui_frontend.cr @@ -226,62 +226,56 @@ class SDLOpenGLImGuiFrontend < Frontend open_keybindings = false if show_menu_bar? - if ImGui.begin_main_menu_bar - if ImGui.begin_menu "File" - open_rom_selection = ImGui.menu_item "Open ROM" - open_bios_selection = ImGui.menu_item "Select BIOS" unless stubbed? - if ImGui.begin_menu "Recent", @config.recents.size > 0 - @config.recents.each do |recent| - load_new_rom(recent) if ImGui.menu_item recent - end - ImGui.separator - if ImGui.menu_item "Clear" - @config.recents.clear - @config.commit - end - ImGui.end_menu - end - ImGui.separator - open_keybindings = ImGui.menu_item "Keybindings" - ImGui.separator - exit if ImGui.menu_item "Exit", "Ctrl+Q" - ImGui.end_menu - end + if ImGui.main_menu_bar do + ImGui.menu "File" do + open_rom_selection = ImGui.menu_item "Open ROM" + open_bios_selection = ImGui.menu_item "Select BIOS" unless stubbed? + ImGui.menu "Recent", @config.recents.size > 0 do + @config.recents.each do |recent| + load_new_rom(recent) if ImGui.menu_item recent + end + ImGui.separator + if ImGui.menu_item "Clear" + @config.recents.clear + @config.commit + end + end + ImGui.separator + open_keybindings = ImGui.menu_item "Keybindings" + ImGui.separator + exit if ImGui.menu_item "Exit", "Ctrl+Q" + end - if ImGui.begin_menu "Emulation" - pause = @pause + ImGui.menu "Emulation" do + pause = @pause - ImGui.menu_item "Pause", "Ctrl+P", pointerof(pause) - @controller.toggle_sync if ImGui.menu_item "Audio Sync", "Tab", @controller.sync?, !stubbed? + ImGui.menu_item "Pause", "Ctrl+P", pointerof(pause) + @controller.toggle_sync if ImGui.menu_item "Audio Sync", "Tab", @controller.sync?, !stubbed? - pause(pause) - ImGui.end_menu - end + pause(pause) + end - if ImGui.begin_menu "Audio/Video" - if ImGui.begin_menu "Frame size" - (1..8).each do |scale| - if ImGui.menu_item "#{scale}x", selected: scale == @scale - @scale = scale - LibSDL.set_window_size(@window, @controller.window_width * @scale, @controller.window_height * @scale) - end - end - ImGui.separator - @window.fullscreen = @fullscreen if ImGui.menu_item "Fullscreen", "Ctrl+F", pointerof(@fullscreen) - ImGui.end_menu - end - ImGui.end_menu - end + ImGui.menu "Audio/Video" do + ImGui.menu "Frame size" do + (1..8).each do |scale| + if ImGui.menu_item "#{scale}x", selected: scale == @scale + @scale = scale + LibSDL.set_window_size(@window, @controller.window_width * @scale, @controller.window_height * @scale) + end + end + ImGui.separator + @window.fullscreen = @fullscreen if ImGui.menu_item "Fullscreen", "Ctrl+F", pointerof(@fullscreen) + end + end - if ImGui.begin_menu "Debug" - ImGui.menu_item "Overlay", "", pointerof(@enable_overlay) - ImGui.separator - @controller.render_debug_items - ImGui.end_menu - end + ImGui.menu "Debug" do + ImGui.menu_item "Overlay", "", pointerof(@enable_overlay) + ImGui.separator + @controller.render_debug_items + end - overlay_height += ImGui.get_window_size.y - ImGui.end_main_menu_bar + overlay_height += ImGui.get_window_size.y + end end end @@ -293,17 +287,17 @@ class SDLOpenGLImGuiFrontend < Frontend if @enable_overlay ImGui.set_next_window_pos(ImGui::ImVec2.new 10, overlay_height) ImGui.set_next_window_bg_alpha(0.5) - ImGui.begin("Overlay", pointerof(@enable_overlay), + ImGui.window("Overlay", pointerof(@enable_overlay), ImGui::ImGuiWindowFlags::NoDecoration | ImGui::ImGuiWindowFlags::NoMove | - ImGui::ImGuiWindowFlags::NoSavedSettings) - io_framerate = @io.framerate - ImGui.text("FPS: #{io_framerate.format(decimal_places: 1)}") - ImGui.text("Frame time: #{(1000 / io_framerate).format(decimal_places: 3)}ms") - ImGui.separator - ImGui.text("OpenGL") - ImGui.text(" Version: #{@opengl_info.version}") - ImGui.text(" Shading: #{@opengl_info.shading}") - ImGui.end + ImGui::ImGuiWindowFlags::NoSavedSettings) do + io_framerate = @io.framerate + ImGui.text("FPS: #{io_framerate.format(decimal_places: 1)}") + ImGui.text("Frame time: #{(1000 / io_framerate).format(decimal_places: 3)}ms") + ImGui.separator + ImGui.text("OpenGL") + ImGui.text(" Version: #{@opengl_info.version}") + ImGui.text(" Shading: #{@opengl_info.shading}") + end end @controller.render_windows diff --git a/src/crab/common/frontend/widgets/file_explorer.cr b/src/crab/common/frontend/widgets/file_explorer.cr index 151ec46..353fcb9 100644 --- a/src/crab/common/frontend/widgets/file_explorer.cr +++ b/src/crab/common/frontend/widgets/file_explorer.cr @@ -22,7 +22,7 @@ module ImGui ImGui.open_popup(name.to_s) 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)) - if ImGui.begin_popup_modal(name.to_s, flags: ImGui::ImGuiWindowFlags::AlwaysAutoResize) + ImGui.popup_modal(name.to_s, flags: ImGui::ImGuiWindowFlags::AlwaysAutoResize) do parts = @config.explorer_dir.parts parts.each_with_index do |part, idx| ImGui.same_line unless idx == 0 @@ -31,7 +31,7 @@ module ImGui display_size = ImGui.get_main_viewport.size width = Math.min(display_size.x - 40, 600) height = Math.min(display_size.y - 40, 16 * ImGui.get_text_line_height_with_spacing) - if ImGui.begin_list_box("##files", ImGui::ImVec2.new(width, height)) + ImGui.list_box("##files", ImGui::ImVec2.new(width, height)) do @matched_entries.each_with_index do |entry, idx| next if entry[:hidden] && !@match_hidden next if entry[:file?] && !extensions.nil? && !extensions.includes?(entry[:extension]) @@ -53,16 +53,14 @@ module ImGui end ImGui.set_item_default_focus if is_selected end - ImGui.end_list_box end - ImGui.begin_group - open_file(name) if ImGui.button "Open" - ImGui.same_line - close if ImGui.button "Cancel" - ImGui.same_line(spacing: 10) - ImGui.checkbox("Show hidden files?", pointerof(@match_hidden)) - ImGui.end_group - ImGui.end_popup + ImGui.group do + open_file(name) if ImGui.button "Open" + ImGui.same_line + close if ImGui.button "Cancel" + ImGui.same_line(spacing: 10) + ImGui.checkbox("Show hidden files?", pointerof(@match_hidden)) + end end end diff --git a/src/crab/common/frontend/widgets/keybindings.cr b/src/crab/common/frontend/widgets/keybindings.cr index fa73507..14a212d 100644 --- a/src/crab/common/frontend/widgets/keybindings.cr +++ b/src/crab/common/frontend/widgets/keybindings.cr @@ -41,7 +41,7 @@ module ImGui center = ImGui.get_main_viewport.get_center ImGui.set_next_window_pos(center, ImGui::ImGuiCond::Appearing, ImGui::ImVec2.new(0.5, 0.5)) hovered_button_color = ImGui.get_style_color_vec4(ImGui::ImGuiCol::ButtonHovered) - if ImGui.begin_popup_modal(POPUP_NAME, flags: ImGui::ImGuiWindowFlags::AlwaysAutoResize) + ImGui.popup_modal(POPUP_NAME, flags: ImGui::ImGuiWindowFlags::AlwaysAutoResize) do Input.each do |input| selected = @selection == input keycode = @editing_keycodes.key_for?(input) @@ -58,7 +58,6 @@ module ImGui apply if ImGui.button "Apply" ImGui.same_line close if ImGui.button "Cancel" - ImGui.end_popup end end