diff --git a/src/main/java/de/extio/game_engine/renderer/container/Window.java b/src/main/java/de/extio/game_engine/renderer/container/Window.java index 765d14e..54879c1 100644 --- a/src/main/java/de/extio/game_engine/renderer/container/Window.java +++ b/src/main/java/de/extio/game_engine/renderer/container/Window.java @@ -7,7 +7,10 @@ import de.extio.game_engine.event.EventService; import de.extio.game_engine.module.ModuleService; import de.extio.game_engine.renderer.RendererControl; import de.extio.game_engine.renderer.model.RenderingBoLayer; +import de.extio.game_engine.renderer.model.bo.ControlRenderingBo; +import de.extio.game_engine.renderer.model.bo.ControlRenderingBo.WindowCloseButtonControl; import de.extio.game_engine.renderer.model.bo.DrawWindowRenderingBo; +import de.extio.game_engine.renderer.model.event.UiControlEvent; import de.extio.game_engine.renderer.work.RendererWorkingSet; import de.extio.game_engine.renderer.work.RenderingBoPool; @@ -15,9 +18,24 @@ import de.extio.game_engine.renderer.work.RenderingBoPool; @Scope("prototype") public class Window extends Container { - public Window( final ModuleService moduleService, final RenderingBoPool renderingBoPool, final RendererControl rendererControl, final EventService eventService, final RendererWorkingSet rendererWorkingSet) { + private boolean closeButton; + + private String closeButtonName = ""; + + private Runnable onCloseAction; + + public Window(final ModuleService moduleService, final RenderingBoPool renderingBoPool, final RendererControl rendererControl, final EventService eventService, final RendererWorkingSet rendererWorkingSet) { super(moduleService, renderingBoPool, rendererControl, eventService, rendererWorkingSet); } + + @Override + public void onActivate() { + if (this.closeButton) { + this.eventService.register(UiControlEvent.class, this.getId(), this::onCloseButtonControlEvent); + } + + super.onActivate(); + } @Override public void draw() { @@ -38,6 +56,55 @@ public class Window extends Container { .setLayer(RenderingBoLayer.UI0_BGR); this.rendererWorkingSet.put(this.id, windowFrame); + if (this.closeButton) { + this.closeButtonName = this.id + "_CloseButton"; + final var closeButtonBo = this.renderingBoPool.acquire(this.closeButtonName, ControlRenderingBo.class) + .setType(WindowCloseButtonControl.class) + .setCustomData(this.draggable) + .setEnabled(true) + .setVisible(true) + .withDimensionAbsolute(this.area.getDimension()) + .withPositionAbsoluteAnchorTopLeft(this.area.getPosition()) + .setLayer(RenderingBoLayer.UI0_1); + this.rendererWorkingSet.put(this.id, closeButtonBo); + } + super.draw(); } + + private void onCloseButtonControlEvent(final UiControlEvent event) { + if (this.closeButton && this.closeButtonName.equals(event.getId())) { + if (this.onCloseAction != null) { + this.onCloseAction.run(); + } + else { + this.getModuleService().changeActiveState(this.getId(), false); + } + } + } + + public boolean hasCloseButton() { + return closeButton; + } + + /** + * Sets whether the window has a close button. Must be set before activation. + */ + public void setCloseButton(final boolean closeButton) { + this.closeButton = closeButton; + } + + public Runnable getOnCloseAction() { + return onCloseAction; + } + + /** + * Sets the action to be performed when the close button is pressed. + * If not set, the window module will simply be deactivated on close button press. + * + * @param onCloseAction The action to be performed on close button press. + */ + public void setOnCloseAction(final Runnable onCloseAction) { + this.onCloseAction = onCloseAction; + } } diff --git a/src/test/java/de/extio/game_engine/demo/DemoModule.java b/src/test/java/de/extio/game_engine/demo/DemoModule.java index 4cf328e..bc75d63 100644 --- a/src/test/java/de/extio/game_engine/demo/DemoModule.java +++ b/src/test/java/de/extio/game_engine/demo/DemoModule.java @@ -50,6 +50,7 @@ public class DemoModule extends AbstractClientModule { this.secondaryWindow.setNormalizedPosition(RendererControl.REFERENCE_RESOLUTION.divide(7).multiply(3)); this.secondaryWindow.setNormalizedDimension(RendererControl.REFERENCE_RESOLUTION.divide(7).multiply(2)); this.secondaryWindow.setDraggable(true); + this.secondaryWindow.setCloseButton(true); this.getModuleService().changeActiveState(this.getId(), true); this.getModuleService().changeDisplayState(this.getId(), true);