From ebb7255a6131b9c31c60914acbebb3f83fc85d8b Mon Sep 17 00:00:00 2001 From: fgnm Date: Fri, 5 Jul 2024 10:07:18 +0200 Subject: [PATCH] [editor only] Fix few crash with mini map --- .../system/HyperLap2dRendererMiniMap.java | 1 + .../editor/view/ui/dialog/MiniMapDialog.java | 5 ++++- .../view/ui/dialog/MiniMapDialogMediator.java | 17 +++++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/games/rednblack/editor/system/HyperLap2dRendererMiniMap.java b/src/main/java/games/rednblack/editor/system/HyperLap2dRendererMiniMap.java index 67f39df6..7fb40510 100644 --- a/src/main/java/games/rednblack/editor/system/HyperLap2dRendererMiniMap.java +++ b/src/main/java/games/rednblack/editor/system/HyperLap2dRendererMiniMap.java @@ -42,6 +42,7 @@ public Texture getMiniMapTexture(int rootEntity) { minimapCamera.setToOrtho(true, bounds.width, bounds.height); minimapCamera.position.set(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2,0); + if (bounds.width == 0 || bounds.height == 0) return null; Gdx.gl.glClearColor(0.318f, 0.318f, 0.318f, 1); frameBufferManager.createIfNotExists("minimap", (int) (bounds.width * pixelsPerWU), (int) (bounds.height * pixelsPerWU), false, hasStencilBuffer); diff --git a/src/main/java/games/rednblack/editor/view/ui/dialog/MiniMapDialog.java b/src/main/java/games/rednblack/editor/view/ui/dialog/MiniMapDialog.java index 4b816a64..b6170dc0 100644 --- a/src/main/java/games/rednblack/editor/view/ui/dialog/MiniMapDialog.java +++ b/src/main/java/games/rednblack/editor/view/ui/dialog/MiniMapDialog.java @@ -49,16 +49,19 @@ public void clicked(InputEvent event, float x, float y) { setBackground(VisUI.getSkin().getDrawable("panel")); } - public void update() { + public boolean update() { Sandbox sandbox = Sandbox.getInstance(); HyperLap2dRendererMiniMap rendererMiniMap = sandbox.getEngine().getSystem(HyperLap2dRendererMiniMap.class); Texture texture = rendererMiniMap.getMiniMapTexture(sandbox.getRootEntity()); + if (texture == null) return false; + region.setRegion(texture); drawable.setRegion(region); miniMap.setScaling(Scaling.contain); miniMap.setDrawable(drawable); miniMapBounds = rendererMiniMap.getMiniMapBounds(); + return true; } } diff --git a/src/main/java/games/rednblack/editor/view/ui/dialog/MiniMapDialogMediator.java b/src/main/java/games/rednblack/editor/view/ui/dialog/MiniMapDialogMediator.java index 0db3bdf6..aeff026e 100644 --- a/src/main/java/games/rednblack/editor/view/ui/dialog/MiniMapDialogMediator.java +++ b/src/main/java/games/rednblack/editor/view/ui/dialog/MiniMapDialogMediator.java @@ -1,6 +1,7 @@ package games.rednblack.editor.view.ui.dialog; import com.badlogic.gdx.utils.Align; +import games.rednblack.editor.proxy.ProjectManager; import games.rednblack.editor.view.stage.Sandbox; import games.rednblack.editor.view.stage.UIStage; import games.rednblack.h2d.common.MsgAPI; @@ -12,13 +13,15 @@ public class MiniMapDialogMediator extends Mediator { private static final String TAG = MiniMapDialogMediator.class.getCanonicalName(); private static final String NAME = TAG; + private boolean projectOpened = false; + public MiniMapDialogMediator() { super(NAME, new MiniMapDialog()); } @Override public void listNotificationInterests(Interests interests) { - interests.add(MsgAPI.SHOW_MINI_MAP, MsgAPI.HIDE_MINI_MAP); + interests.add(MsgAPI.SHOW_MINI_MAP, MsgAPI.HIDE_MINI_MAP, ProjectManager.PROJECT_OPENED); } @Override @@ -28,14 +31,20 @@ public void handleNotification(INotification notification) { UIStage uiStage = sandbox.getUIStage(); switch (notification.getName()) { + case ProjectManager.PROJECT_OPENED: + projectOpened = true; + break; case MsgAPI.SHOW_MINI_MAP: + if (!projectOpened) break; viewComponent.setSize(uiStage.getWidth() * 0.6f, uiStage.getHeight() * 0.6f); viewComponent.setOrigin(Align.center); - viewComponent.update(); - uiStage.addActor(viewComponent); - viewComponent.setPosition((uiStage.getWidth() - viewComponent.getWidth()) / 2, (uiStage.getHeight() - viewComponent.getHeight()) / 2); + if (viewComponent.update()) { + uiStage.addActor(viewComponent); + viewComponent.setPosition((uiStage.getWidth() - viewComponent.getWidth()) / 2, (uiStage.getHeight() - viewComponent.getHeight()) / 2); + } break; case MsgAPI.HIDE_MINI_MAP: + if (!projectOpened) break; viewComponent.remove(); break; }