Made inventory windows draggable

This commit is contained in:
OLEGSHA 2021-12-22 01:41:55 +03:00
parent 9c85164ed1
commit 38021852d0
Signed by: OLEGSHA
GPG Key ID: E57A4B08D64AFF7A
2 changed files with 47 additions and 21 deletions

View File

@ -17,6 +17,8 @@
*/ */
package ru.windcorp.progressia.client.graphics.world.hud; package ru.windcorp.progressia.client.graphics.world.hud;
import com.google.common.eventbus.Subscribe;
import glm.vec._2.Vec2; import glm.vec._2.Vec2;
import glm.vec._4.Vec4; import glm.vec._4.Vec4;
import ru.windcorp.progressia.client.graphics.Colors; import ru.windcorp.progressia.client.graphics.Colors;
@ -25,26 +27,30 @@ import ru.windcorp.progressia.client.graphics.font.Font;
import ru.windcorp.progressia.client.graphics.font.Typeface; import ru.windcorp.progressia.client.graphics.font.Typeface;
import ru.windcorp.progressia.client.graphics.gui.Button; import ru.windcorp.progressia.client.graphics.gui.Button;
import ru.windcorp.progressia.client.graphics.gui.Component; import ru.windcorp.progressia.client.graphics.gui.Component;
import ru.windcorp.progressia.client.graphics.gui.DragManager;
import ru.windcorp.progressia.client.graphics.gui.Group; import ru.windcorp.progressia.client.graphics.gui.Group;
import ru.windcorp.progressia.client.graphics.gui.Label; import ru.windcorp.progressia.client.graphics.gui.Label;
import ru.windcorp.progressia.client.graphics.gui.Panel; import ru.windcorp.progressia.client.graphics.gui.Panel;
import ru.windcorp.progressia.client.graphics.gui.event.DragEvent;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutBorderHorizontal; import ru.windcorp.progressia.client.graphics.gui.layout.LayoutBorderHorizontal;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutFill; import ru.windcorp.progressia.client.graphics.gui.layout.LayoutFill;
import ru.windcorp.progressia.client.graphics.gui.layout.LayoutVertical; import ru.windcorp.progressia.client.graphics.gui.layout.LayoutVertical;
import ru.windcorp.progressia.client.localization.MutableString; import ru.windcorp.progressia.client.localization.MutableString;
import ru.windcorp.progressia.client.localization.MutableStringConcat;
import ru.windcorp.progressia.client.localization.MutableStringLocalized; import ru.windcorp.progressia.client.localization.MutableStringLocalized;
import ru.windcorp.progressia.client.world.item.inventory.InventoryComponent; import ru.windcorp.progressia.client.world.item.inventory.InventoryComponent;
public class InventoryWindow extends Panel { public class InventoryWindow extends Panel {
private static final String CLOSE_CHAR = "\u2715"; private static final String CLOSE_CHAR = "\u2715";
private static final String HANDLE_CHAR = "\u2800";
private static final Vec4 CLOSE_BUTTON_IDLE = Colors.toVector(0xFFBC1515); private static final Vec4 CLOSE_BUTTON_IDLE = Colors.toVector(0xFFBC1515);
private static final Vec4 CLOSE_BUTTON_HOVER = Colors.toVector(0xFFFA6464); private static final Vec4 CLOSE_BUTTON_HOVER = Colors.toVector(0xFFFA6464);
private static final Vec4 CLOSE_BUTTON_PRESSED = Colors.BLACK; private static final Vec4 CLOSE_BUTTON_PRESSED = Colors.BLACK;
private final InventoryComponent content; private final InventoryComponent content;
private final HUDWorkspace workspace; private final HUDWorkspace workspace;
private final Vec2 relativePosition = new Vec2(Float.NaN); private final Vec2 relativePosition = new Vec2(Float.NaN);
public InventoryWindow(String name, InventoryComponent component, HUDWorkspace workspace) { public InventoryWindow(String name, InventoryComponent component, HUDWorkspace workspace) {
@ -56,6 +62,17 @@ public class InventoryWindow extends Panel {
titleBar.addChild(createLabel(component).setLayoutHint(LayoutBorderHorizontal.CENTER)); titleBar.addChild(createLabel(component).setLayoutHint(LayoutBorderHorizontal.CENTER));
titleBar.addChild(createCloseButton(component).setLayoutHint(LayoutBorderHorizontal.RIGHT)); titleBar.addChild(createCloseButton(component).setLayoutHint(LayoutBorderHorizontal.RIGHT));
new DragManager().install(titleBar);
titleBar.addListener(new Object() {
@Subscribe
public void onWindowDragged(DragEvent e) {
Vec2 change = new Vec2((float) e.getCurrentChangeX(), (float) e.getCurrentChangeY());
change.div(getParent().getWidth(), getParent().getHeight());
relativePosition.add(change);
requestReassembly();
}
});
addChild(titleBar); addChild(titleBar);
addChild(component); addChild(component);
@ -63,7 +80,10 @@ public class InventoryWindow extends Panel {
private Label createLabel(InventoryComponent component) { private Label createLabel(InventoryComponent component) {
String translationKey = "Inventory." + component.getInventory().getId() + ".Title"; String translationKey = "Inventory." + component.getInventory().getId() + ".Title";
MutableString titleText = new MutableStringLocalized(translationKey); MutableString titleText = new MutableStringConcat(
HANDLE_CHAR + " ",
new MutableStringLocalized(translationKey)
);
Font titleFont = new Font().deriveBold().withColor(Colors.BLACK).withAlign(Typeface.ALIGN_LEFT); Font titleFont = new Font().deriveBold().withColor(Colors.BLACK).withAlign(Typeface.ALIGN_LEFT);
return new Label(getName() + ".Title", titleFont, titleText); return new Label(getName() + ".Title", titleFont, titleText);
@ -76,7 +96,7 @@ public class InventoryWindow extends Panel {
} }
return null; return null;
} }
/** /**
* @return the relativePosition * @return the relativePosition
*/ */
@ -85,48 +105,48 @@ public class InventoryWindow extends Panel {
} }
private Component createCloseButton(InventoryComponent component) { private Component createCloseButton(InventoryComponent component) {
Button button = new Button(getName() + ".CloseButton", CLOSE_CHAR) { Button button = new Button(getName() + ".CloseButton", CLOSE_CHAR) {
@Override @Override
protected void assembleSelf(RenderTarget target) { protected void assembleSelf(RenderTarget target) {
Vec4 color = CLOSE_BUTTON_IDLE; Vec4 color = CLOSE_BUTTON_IDLE;
if (isPressed()) { if (isPressed()) {
color = CLOSE_BUTTON_PRESSED; color = CLOSE_BUTTON_PRESSED;
} else if (isHovered()) { } else if (isHovered()) {
color = CLOSE_BUTTON_HOVER; color = CLOSE_BUTTON_HOVER;
} }
if (hasLabel()) { if (hasLabel()) {
getLabel().setFont(getLabel().getFont().withColor(color)); getLabel().setFont(getLabel().getFont().withColor(color));
} }
} }
}; };
button.addAction(b -> { button.addAction(b -> {
content.getInventory().close(workspace.getPlayerEntity()); content.getInventory().close(workspace.getPlayerEntity());
WindowedHUD manager = getManager(); WindowedHUD manager = getManager();
if (manager == null) { if (manager == null) {
return; return;
} }
manager.closeWindow(this); manager.closeWindow(this);
}); });
button.setLayout(new LayoutFill()); button.setLayout(new LayoutFill());
int height = button.getLabel().getFont().getHeight(button.getLabel().getCurrentText()); int height = button.getLabel().getFont().getHeight(button.getLabel().getCurrentText());
button.setPreferredSize(height, height); button.setPreferredSize(height, height);
return button; return button;
} }
public InventoryComponent getContent() { public InventoryComponent getContent() {
return content; return content;
} }

View File

@ -39,10 +39,16 @@ public class WindowedLayout implements Layout {
if (Float.isNaN(relPos.x) || Float.isNaN(relPos.y)) { if (Float.isNaN(relPos.x) || Float.isNaN(relPos.y)) {
relPos.x = 0.5f; relPos.x = 0.5f;
relPos.y = 2 / 3.0f; relPos.y = 2 / 3.0f;
} else {
float minPosX = 0;
float minPosY = window.getHeight() / (float) c.getHeight();
float maxPosX = 1;
float maxPosY = 1;
relPos.x = Glm.clamp(relPos.x, minPosX, maxPosX);
relPos.y = Glm.clamp(relPos.y, minPosY, maxPosY);
} }
relPos.clamp(0, 1);
window.setPosition( window.setPosition(
(int) (relPos.x * c.getWidth() - window.getWidth() / 2.0f), (int) (relPos.x * c.getWidth() - window.getWidth() / 2.0f),
(int) (relPos.y * c.getHeight() - window.getHeight()) (int) (relPos.y * c.getHeight() - window.getHeight())