Moved window layout into a separate file

This commit is contained in:
OLEGSHA 2021-12-07 14:40:31 +03:00
parent 3641c4130b
commit 9b67897896
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
3 changed files with 68 additions and 38 deletions

View File

@ -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));
@ -76,6 +77,13 @@ public class InventoryWindow extends Panel {
return null;
}
/**
* @return the relativePosition
*/
public Vec2 getRelativePosition() {
return relativePosition;
}
private Component createCloseButton(InventoryComponent component) {
Button button = new Button(getName() + ".CloseButton", CLOSE_CHAR) {

View File

@ -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) {

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}