Refactor closeable windows (player list and movement options)

This commit is contained in:
Moulberry committed 2024-11-17 02:17:21 +08:00
1 parent 6ff92db726
commit 4d879392de
5 files changed
+67 -30

No files matched your search

@@ -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);
@@ -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);
}
@@ -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();
}
@@ -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;
@@ -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<String> 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;
}
}
}
}