diff --git a/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/InventoryWindow.java b/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/InventoryWindow.java index e29a59c..0f15d2c 100644 --- a/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/InventoryWindow.java +++ b/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/InventoryWindow.java @@ -17,6 +17,7 @@ */ package ru.windcorp.progressia.client.graphics.world.hud; +import glm.vec._2.Vec2; import glm.vec._4.Vec4; import ru.windcorp.progressia.client.graphics.Colors; import ru.windcorp.progressia.client.graphics.flat.RenderTarget; @@ -44,7 +45,7 @@ public class InventoryWindow extends Panel { private final InventoryComponent content; private final HUDWorkspace workspace; - Object layoutCookie; + private final Vec2 relativePosition = new Vec2(Float.NaN); public InventoryWindow(String name, InventoryComponent component, HUDWorkspace workspace) { super(name, new LayoutVertical(15, 15)); @@ -75,6 +76,13 @@ public class InventoryWindow extends Panel { } return null; } + + /** + * @return the relativePosition + */ + public Vec2 getRelativePosition() { + return relativePosition; + } private Component createCloseButton(InventoryComponent component) { diff --git a/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/WindowedHUD.java b/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/WindowedHUD.java index 08041a8..93276f4 100644 --- a/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/WindowedHUD.java +++ b/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/WindowedHUD.java @@ -17,49 +17,13 @@ */ package ru.windcorp.progressia.client.graphics.world.hud; -import glm.vec._2.Vec2; -import glm.vec._2.i.Vec2i; import ru.windcorp.progressia.client.graphics.gui.Component; -import ru.windcorp.progressia.client.graphics.gui.Layout; public class WindowedHUD extends Component { - - private static class Cookie { - private final Vec2 relPos = new Vec2(); - } public WindowedHUD(String name) { super(name); - setLayout(new Layout() { - @Override - public void layout(Component c) { - for (Component component : c.getChildren()) { - InventoryWindow window = (InventoryWindow) component; - - window.setSize(window.getPreferredSize()); - - Cookie cookie = (Cookie) window.layoutCookie; - - if (cookie == null) { - window.layoutCookie = cookie = new Cookie(); - cookie.relPos.x = 0.5f; - cookie.relPos.y = 2 / 3.0f; - } - - cookie.relPos.clamp(0, 1); - - window.setPosition( - (int) (cookie.relPos.x * c.getWidth() - window.getWidth() / 2.0f), - (int) (cookie.relPos.y * c.getHeight() - window.getHeight()) - ); - } - } - - @Override - public Vec2i calculatePreferredSize(Component c) { - throw new AssertionError("welp this wasnt supposed to hapen :("); - } - }); + setLayout(new WindowedLayout()); } public void addWindow(InventoryWindow window) { diff --git a/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/WindowedLayout.java b/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/WindowedLayout.java new file mode 100644 index 0000000..39a9e6e --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/client/graphics/world/hud/WindowedLayout.java @@ -0,0 +1,58 @@ +/* + * Progressia + * Copyright (C) 2020-2021 Wind Corporation and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package ru.windcorp.progressia.client.graphics.world.hud; + +import glm.Glm; +import glm.vec._2.Vec2; +import glm.vec._2.i.Vec2i; +import ru.windcorp.progressia.client.graphics.gui.Component; +import ru.windcorp.progressia.client.graphics.gui.Layout; + +public class WindowedLayout implements Layout { + + @Override + public void layout(Component c) { + for (Component component : c.getChildren()) { + InventoryWindow window = (InventoryWindow) component; + + Vec2i size = new Vec2i(c.getWidth(), c.getHeight()); + Glm.min(window.getPreferredSize(), size, size); + window.setSize(size); + + Vec2 relPos = window.getRelativePosition(); + + if (Float.isNaN(relPos.x) || Float.isNaN(relPos.y)) { + relPos.x = 0.5f; + relPos.y = 2 / 3.0f; + } + + relPos.clamp(0, 1); + + window.setPosition( + (int) (relPos.x * c.getWidth() - window.getWidth() / 2.0f), + (int) (relPos.y * c.getHeight() - window.getHeight()) + ); + } + } + + @Override + public Vec2i calculatePreferredSize(Component c) { + throw new UnsupportedOperationException(); + } + +}