From 4d879392de3a70650dd355233012c2c01d68e8c1 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Fri, 15 Nov 2024 11:07:11 +0800 Subject: [PATCH] Refactor closeable windows (player list and movement options) --- .../flashback/editor/ui/ReplayUI.java | 5 ++- .../ui/windows/ImGuiWindowRenderer.java | 10 +++++ .../editor/ui/windows/MovementWindow.java | 22 +++------ .../editor/ui/windows/PlayerListWindow.java | 15 ++----- .../editor/ui/windows/WindowType.java | 45 +++++++++++++++++++ 5 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/moulberry/flashback/editor/ui/windows/ImGuiWindowRenderer.java create mode 100644 src/main/java/com/moulberry/flashback/editor/ui/windows/WindowType.java diff --git a/src/main/java/com/moulberry/flashback/editor/ui/ReplayUI.java b/src/main/java/com/moulberry/flashback/editor/ui/ReplayUI.java index 4339d0c..9b5b604 100644 --- a/src/main/java/com/moulberry/flashback/editor/ui/ReplayUI.java +++ b/src/main/java/com/moulberry/flashback/editor/ui/ReplayUI.java @@ -10,6 +10,7 @@ import com.moulberry.flashback.editor.ui.windows.MovementWindow; import com.moulberry.flashback.editor.ui.windows.PlayerListWindow; import com.moulberry.flashback.editor.ui.windows.PreferencesWindow; import com.moulberry.flashback.editor.ui.windows.SelectedEntityPopup; +import com.moulberry.flashback.editor.ui.windows.WindowType; import com.moulberry.flashback.state.EditorState; import com.moulberry.flashback.state.EditorStateManager; import com.moulberry.flashback.combo_options.Sizing; @@ -768,8 +769,8 @@ public class ReplayUI { ExportScreenshotWindow.render(); PreferencesWindow.render(); ExportQueueWindow.render(); - PlayerListWindow.render(); - MovementWindow.render(); + + WindowType.renderAll(); popupOpenLastFrame = ImGui.isPopupOpen("", ImGuiPopupFlags.AnyPopup); diff --git a/src/main/java/com/moulberry/flashback/editor/ui/windows/ImGuiWindowRenderer.java b/src/main/java/com/moulberry/flashback/editor/ui/windows/ImGuiWindowRenderer.java new file mode 100644 index 0000000..36ce17f --- /dev/null +++ b/src/main/java/com/moulberry/flashback/editor/ui/windows/ImGuiWindowRenderer.java @@ -0,0 +1,10 @@ +package com.moulberry.flashback.editor.ui.windows; + +import imgui.type.ImBoolean; + +@FunctionalInterface +public interface ImGuiWindowRenderer { + + void render(ImBoolean open, boolean newlyOpened); + +} diff --git a/src/main/java/com/moulberry/flashback/editor/ui/windows/MovementWindow.java b/src/main/java/com/moulberry/flashback/editor/ui/windows/MovementWindow.java index 358a386..629ca5e 100644 --- a/src/main/java/com/moulberry/flashback/editor/ui/windows/MovementWindow.java +++ b/src/main/java/com/moulberry/flashback/editor/ui/windows/MovementWindow.java @@ -12,6 +12,7 @@ import imgui.ImGui; import imgui.ImGuiViewport; import imgui.flag.ImGuiCond; import imgui.flag.ImGuiWindowFlags; +import imgui.type.ImBoolean; import imgui.type.ImString; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; @@ -27,30 +28,23 @@ import java.util.Locale; public class MovementWindow { - private static WindowOpenState wasOpen = WindowOpenState.UNKNOWN; private static boolean wasDocked = false; - public static void render() { - if (!Flashback.getConfig().openedWindows.contains("movement")) { - wasOpen = WindowOpenState.CLOSED; - return; - } - if (wasOpen == WindowOpenState.CLOSED) { + public static void render(ImBoolean open, boolean newlyOpened) { + if (newlyOpened) { ImGuiViewport viewport = ImGui.getMainViewport(); ImGui.setNextWindowPos(viewport.getCenterX(), viewport.getCenterY(), ImGuiCond.Appearing, 0.5f, 0.5f); } - wasOpen = WindowOpenState.OPEN; - - FlashbackConfig config = Flashback.getConfig(); ImGui.setNextWindowSizeConstraints(250, 50, 5000, 5000); int flags = ImGuiWindowFlags.NoFocusOnAppearing; if (!wasDocked) { flags |= ImGuiWindowFlags.AlwaysAutoResize; } - if (ImGui.begin("Movement###Movement", flags)) { + if (ImGui.begin("Movement###Movement", open, flags)) { wasDocked = ImGui.isWindowDocked(); + FlashbackConfig config = Flashback.getConfig(); int[] direction = new int[]{config.flightCameraDirection ? 1 : 0}; ImGuiHelper.combo("Direction", direction, new String[]{ @@ -94,12 +88,6 @@ public class MovementWindow { config.flightLockPitch = !config.flightLockPitch; config.saveToDefaultFolder(); } - - // todo: record movement: can we make this smoother? -// if (ImGui.checkbox("Record Movement as Keyframes", ReplayUI.recordCameraMovement)) { -// ReplayUI.recordCameraMovement = !ReplayUI.recordCameraMovement; -// } - } ImGui.end(); } diff --git a/src/main/java/com/moulberry/flashback/editor/ui/windows/PlayerListWindow.java b/src/main/java/com/moulberry/flashback/editor/ui/windows/PlayerListWindow.java index d75fb69..1c53da7 100644 --- a/src/main/java/com/moulberry/flashback/editor/ui/windows/PlayerListWindow.java +++ b/src/main/java/com/moulberry/flashback/editor/ui/windows/PlayerListWindow.java @@ -10,6 +10,7 @@ import imgui.ImGui; import imgui.ImGuiViewport; import imgui.flag.ImGuiCond; import imgui.flag.ImGuiWindowFlags; +import imgui.type.ImBoolean; import imgui.type.ImString; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; @@ -25,8 +26,6 @@ import java.util.Locale; public class PlayerListWindow { - private static WindowOpenState wasOpen = WindowOpenState.UNKNOWN; - private static String lastSearch = null; private static long lastUpdate = 0; private static ImString search = ImGuiHelper.createResizableImString(""); @@ -90,20 +89,14 @@ public class PlayerListWindow { } } - public static void render() { - // todo: refactor this if we want to add a lot more windows - if (!Flashback.getConfig().openedWindows.contains("player_list")) { - wasOpen = WindowOpenState.CLOSED; - return; - } - if (wasOpen == WindowOpenState.CLOSED) { + public static void render(ImBoolean open, boolean newlyOpened) { + if (newlyOpened) { ImGuiViewport viewport = ImGui.getMainViewport(); ImGui.setNextWindowPos(viewport.getCenterX(), viewport.getCenterY(), ImGuiCond.Appearing, 0.5f, 0.5f); } - wasOpen = WindowOpenState.OPEN; ImGui.setNextWindowSizeConstraints(250, 50, 5000, 5000); - if (ImGui.begin("Player List###PlayerList", ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoFocusOnAppearing)) { + if (ImGui.begin("Player List###PlayerList", open, ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoFocusOnAppearing)) { ImGui.setNextItemWidth(ImGui.getContentRegionAvailX() - ImGuiHelper.calcTextWidth("Search") - 32); if (ImGui.inputText("Search", search)) { lastSearch = null; diff --git a/src/main/java/com/moulberry/flashback/editor/ui/windows/WindowType.java b/src/main/java/com/moulberry/flashback/editor/ui/windows/WindowType.java new file mode 100644 index 0000000..625a65f --- /dev/null +++ b/src/main/java/com/moulberry/flashback/editor/ui/windows/WindowType.java @@ -0,0 +1,45 @@ +package com.moulberry.flashback.editor.ui.windows; + +import com.moulberry.flashback.Flashback; +import com.moulberry.flashback.editor.ui.WindowOpenState; +import imgui.type.ImBoolean; + +import java.util.Set; + +public enum WindowType { + + PLAYER_LIST("player_list", PlayerListWindow::render), + MOVEMENT("movement", MovementWindow::render); + + private final String windowId; + private final ImGuiWindowRenderer renderMethod; + private final ImBoolean open = new ImBoolean(); + private WindowOpenState openState = WindowOpenState.UNKNOWN; + + WindowType(String windowId, ImGuiWindowRenderer renderMethod) { + this.windowId = windowId; + this.renderMethod = renderMethod; + } + + public static void renderAll() { + Set openWindows = Flashback.getConfig().openedWindows; + + for (WindowType windowType : values()) { + if (openWindows.contains(windowType.windowId)) { + boolean justOpened = windowType.openState == WindowOpenState.CLOSED; + + windowType.open.set(true); + windowType.renderMethod.render(windowType.open, justOpened); + + if (!windowType.open.get()) { + openWindows.remove(windowType.windowId); + } + + windowType.openState = WindowOpenState.OPEN; + } else { + windowType.openState = WindowOpenState.CLOSED; + } + } + } + +}