zooming camera & build menu

bottom-bar
Ivory 2023-01-21 17:09:55 -05:00
parent 97d6412ea7
commit 072774e2bf
15 changed files with 242 additions and 228 deletions

View File

@ -0,0 +1,5 @@
items:
- name: Log
plural: Logs
texture: [48, 96, 16, 16]

View File

@ -85,29 +85,35 @@ public class App {
}
});
glfwSetCursorPosCallback(window, new GLFWCursorPosCallback() {
@Override
public void invoke(long window, double xpos, double ypos) {
mouseX = (int) xpos;
mouseY = (int) ypos;
}
glfwSetCursorPosCallback(window, (long window, double xpos, double ypos) -> {
mouseX = (int) xpos;
mouseY = (int) ypos;
});
glfwSetMouseButtonCallback(window, new GLFWMouseButtonCallback() {
@Override
public void invoke(long window, int button, int action, int mods) {
glfwSetScrollCallback(window, (long window, double xOffset, double yOffset) -> {
System.out.println("Scroll " + yOffset);
if(yOffset > 0)
game.scrollUp();
else if(yOffset < 0)
game.scrollDown();
// if(yOffset > 0)
// game.scrollLeft();
// else if(yOffset < 0)
// game.scrollRight();
});
glfwSetMouseButtonCallback(window, (long window, int button, int action, int mods) -> {
if(action == 1) {
game.mouseDown(button);
} else if(action == 0) {
game.mouseUp(button);
}
if(button >= 3) return;
if(button == GLFW_MOUSE_BUTTON_LEFT) { mouseLeft = action == 1; return; }
if(button == GLFW_MOUSE_BUTTON_RIGHT) { mouseRight = action == 1; return; }
if(button == GLFW_MOUSE_BUTTON_MIDDLE) { mouseMiddle = action == 1; return ; }
}
System.out.println("Mouse: action " + action + " : button " + button);
});
// Get the thread stack and push a new frame

View File

@ -53,6 +53,14 @@ public abstract class Game {
public abstract void updateViewMatrix(Matrix4f matrix);
public final void scrollUp() {
scene.scrollUp();
}
public final void scrollDown() {
scene.scrollDown();
}
public final void mouseDown(int button) {
scene.mouseDown(button);
}

View File

@ -151,6 +151,14 @@ public abstract class ImmediateUI extends GameObject implements IMouseCaptureAre
public void apply();
}
private float getCurrentLayer() {
return getLayer() + contextStack.size() * 0.001f;
}
private float getPreviousLayer() {
return getLayer() + contextStack.size() * 0.001f;
}
// === ELEMENTS ===
//
@ -213,7 +221,7 @@ public abstract class ImmediateUI extends GameObject implements IMouseCaptureAre
// TODO this will add _all_ frames, not just root frames to guiareas.
// not a problem, but not efficient. revisit.
protected void frameEnd() {
Drawing.setLayer(getLayer() + contextStack.size() - 1);
Drawing.setLayer(getPreviousLayer());
if(!context.fixedSize) {
Assets.uiFrame.draw(context.box);
guiAreas.add(context.box);
@ -267,7 +275,7 @@ public abstract class ImmediateUI extends GameObject implements IMouseCaptureAre
btn.setText(text);
btn.setPosition(x, y);
btn.setSize((int) buttonBox.z, (int) buttonBox.w);
btn.setLayer(getLayer() + contextStack.size() + 1);
btn.setLayer(getCurrentLayer());
adjustBox(buttonBox.z, buttonBox.w);
@ -295,7 +303,7 @@ public abstract class ImmediateUI extends GameObject implements IMouseCaptureAre
protected void groupEnd() {
padEnd();
Drawing.setLayer(getLayer() + contextStack.size() - 1);
Drawing.setLayer(getPreviousLayer());
float h = context.box.w;
Assets.uiFrame.draw(context.box);
context = contextStack.pop();
@ -383,7 +391,7 @@ public abstract class ImmediateUI extends GameObject implements IMouseCaptureAre
protected void text(String text, Font font) {
Vector4i measured = font.measure(text);
Drawing.setLayer(getLayer() + contextStack.size());
Drawing.setLayer(getCurrentLayer());
int x = (int) context.box.x;
int y = (int) context.box.y;

View File

@ -9,6 +9,8 @@ public interface IMouseCaptureArea {
public void mouseLeave();
public void mouseDown(int button);
public void mouseUp(int button);
public default void scrollUp() {}
public default void scrollDown() {}
public List<Vector4f> getGuiBoxes();
public float getLayer();

View File

@ -4,6 +4,9 @@ public interface IScene {
public void render();
public void update(float dTime);
public void scrollUp();
public void scrollDown();
public void mouseDown(int button);
public void mouseUp(int button);

View File

@ -267,4 +267,18 @@ public abstract class SceneGraph implements IScene {
public boolean getKey(int key) {
return keys.contains(key);
}
@Override
public void scrollDown() {
for(IMouseCaptureArea iml : getAll(IMouseCaptureArea.class)) {
iml.scrollDown();
}
}
@Override
public void scrollUp() {
for(IMouseCaptureArea iml : getAll(IMouseCaptureArea.class)) {
iml.scrollUp();
}
}
}

View File

@ -0,0 +1,5 @@
package xyz.valnet.hadean;
public class ItemDescriptor {
}

View File

@ -172,5 +172,17 @@ public class Camera extends GameObject implements ITransient, IMouseCaptureArea
public float getLayer() {
return 0;
}
@Override
public void scrollDown() {
tileWidth /= 2;
tileWidth = Math.max(tileWidth, 8);
}
@Override
public void scrollUp() {
tileWidth *= 2;
tileWidth = Math.min(tileWidth, 32);
}
}

View File

@ -51,6 +51,7 @@ public class BuildLayer extends GameObject implements IMouseCaptureArea, ITransi
}
public void activate(IBuildLayerListener listener) {
if(active) return;
active = true;
this.listener = listener;
broadcastWorldCoords();

View File

@ -208,7 +208,7 @@ public class SelectionLayer extends GameObject implements IMouseCaptureArea, ITr
}
} else if (button == 1) {
if(selected.size() == 0) {
buildTab.evoke();
buildTab.rightClickOnWorld();
} else {
clearSelection();
}

View File

@ -34,27 +34,25 @@ import xyz.valnet.hadean.util.Assets;
import xyz.valnet.hadean.util.Layers;
import xyz.valnet.hadean.util.SmartBoolean;
public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCaptureArea, IButtonListener {
import static xyz.valnet.engine.util.Math.lerp;
public class BuildTab extends Tab implements ISelectionChangeListener, IBuildLayerListener {
private SelectionLayer selection;
private BuildLayer buildLayer;
private Camera camera;
private SmartBoolean opened;
private int width = 200;
private int padding = 10;
private boolean opened;
private int x, y;
private int w, h;
private String selectedCategory = "";
private String selectedCategory = null;
private static transient Map<String, List<BuildableRecord>> buildables = new HashMap<String, List<BuildableRecord>>();
private transient Map<Button, BuildableRecord> buildableButtons = null;
private transient BuildableRecord selectedBuildable = null;
private int height = Math.max((int)Math.ceil(buildables.size() / 2f) * 24, 24*3);
private float openness = 0;
static {
BuildTab.registerBuildable(HaulItemDesignation.class);
@ -109,168 +107,138 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
public void renderAlpha () {
Drawing.setLayer(Layers.GENERAL_UI);
if(opened.value()) {
Assets.uiFrame.draw(padding, 576 - BottomBar.bottomBarHeight - padding - height, width, height);
if(!opened || selectedBuildable == null) return;
// draw the currently selected build item
Assets.flat.pushColor(new Vector4f(1f, 1f, 1f, 1.0f));
Vector2i topLeft = camera.world2screen(x, y);
Assets.font.drawString(selectedBuildable.name, topLeft.x, topLeft.y - 20);
Assets.flat.swapColor(new Vector4f(1f, 1f, 1f, 0.6f));
camera.draw(Layers.BUILD_INTERACTABLE, Assets.selectionFrame, x, y, w, h);
Assets.flat.swapColor(new Vector4f(1f, 1f, 1f, 0.35f));
for(int i = 0; i < w; i ++) for(int j = 0; j < h; j ++) {{
camera.draw(Layers.BUILD_INTERACTABLE, Assets.checkerBoard, x + i, y + j);
}}
Assets.flat.popColor();
}
if(selectedBuildable == null) return;
// draw the currently selected build item
Assets.flat.pushColor(new Vector4f(1f, 1f, 1f, 1.0f));
Vector2i topLeft = camera.world2screen(x, y);
Assets.font.drawString(selectedBuildable.name, topLeft.x, topLeft.y - 20);
Assets.flat.swapColor(new Vector4f(1f, 1f, 1f, 0.6f));
camera.draw(Layers.BUILD_INTERACTABLE, Assets.selectionFrame, x, y, w, h);
Assets.flat.swapColor(new Vector4f(1f, 1f, 1f, 0.35f));
for(int i = 0; i < w; i ++) for(int j = 0; j < h; j ++) {{
camera.draw(Layers.BUILD_INTERACTABLE, Assets.checkerBoard, x + i, y + j);
}}
Assets.flat.popColor();
}
@Override
protected void connect() {
super.connect();
buildLayer = get(BuildLayer.class);
selection = get(SelectionLayer.class);
camera = get(Camera.class);
}
@Override
public void start() {
super.start();
buildLayer = get(BuildLayer.class);
selection = get(SelectionLayer.class);
camera = get(Camera.class);
opened = new SmartBoolean(false, new SmartBoolean.IListener() {
@Override
public void rise() {
Assets.sndBubble.play();
activate();
}
@Override
public void fall() {
Assets.sndCancel.play();
deactiveate();
}
});
if(selection != null) {
selection.subscribe(this);
}
buildableButtons = new HashMap<Button, BuildableRecord>();
}
private List<Button> categoryButtons = new ArrayList<Button>();
private void deselectBuilding() {
if(selectedBuildable != null) buildLayer.deactiveate();
selectedBuildable = null;
}
private void selectBuildable(BuildableRecord buildableRecord) {
if(buildableRecord == null) deselectBuilding();
if (selectedBuildable == null) activateBuildLayer();
selectedBuildable = buildableRecord;
swapBuildLayerType(selectedBuildable.type);
}
private void swapBuildLayerType(BuildableMetadata.Type type) {
buildLayer.setBuildType(type);
}
private void activateBuildLayer() {
buildLayer.activate(new IBuildLayerListener() {
@Override
public void update(int nx, int ny, int nw, int nh) {
x = nx;
y = ny;
w = nw;
h = nh;
}
@Override
public void build(int x1, int y1, int x2, int y2) {
if(selectedBuildable == null) return;
try {
IBuildable building = selectedBuildable.constructor.newInstance();
if(building instanceof GameObject) {
add((GameObject) building);
}
building.buildAt(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
} catch (Exception e) {
System.out.println(e);
}
// opened.set(false);
}
@Override
public void build(int x1, int y1) {
if(selectedBuildable == null) return;
try {
IBuildable building = selectedBuildable.constructor.newInstance();
if(building instanceof GameObject) {
add((GameObject) building);
}
building.buildAt(x1, y1);
} catch (Exception e) {
System.out.println(e);
}
// opened.set(false);
}
@Override
public void cancel() {
if(selectedBuildable == null) {
opened.set(false);
buildLayer.deactiveate();
} else {
deselectBuilding();
}
}
});
}
private void activate() {
int i = 0;
categoryButtons.clear();
for(String c : buildables.keySet()) {
int left = i % 2 == 0 ? padding : padding + width / 2;
int y = 576 - BottomBar.bottomBarHeight - padding - height + ((int)Math.floor(i / 2)) * 24;
Button b = new SimpleButton(c, left, y, width / 2, 24, Layers.GENERAL_UI_INTERACTABLE);
b.registerClickListener(this);
categoryButtons.add(b);
add(b);
i ++;
}
constructItemButtons();
}
private void deactiveate() {
buildLayer.deactiveate();
if(categoryButtons != null) for(Button btn : categoryButtons) remove(btn);
categoryButtons.clear();
constructItemButtons();
}
@Override
public void update(float dTime) {
openness = lerp(openness, opened ? 1 : 0, dTime / 20);
}
public void rightClickOnWorld() {
if(selectedBuildable != null) {
selectBuildable(null);
} else {
evoke();
}
}
private void selectBuildable(BuildableRecord buildableRecord) {
if(buildableRecord == null) {
buildLayer.deactiveate();
selectedBuildable = null;
return;
}
selectedBuildable = buildableRecord;
buildLayer.activate(this);
buildLayer.setBuildType(selectedBuildable.type);
}
@Override
public void update(int nx, int ny, int nw, int nh) {
x = nx;
y = ny;
w = nw;
h = nh;
}
@Override
public void build(int x1, int y1, int x2, int y2) {
if(selectedBuildable == null) return;
try {
IBuildable building = selectedBuildable.constructor.newInstance();
if(building instanceof GameObject) {
add((GameObject) building);
}
building.buildAt(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
} catch (Exception e) {
System.out.println(e);
}
}
@Override
public void build(int x1, int y1) {
if(selectedBuildable == null) return;
try {
IBuildable building = selectedBuildable.constructor.newInstance();
if(building instanceof GameObject) {
add((GameObject) building);
}
building.buildAt(x1, y1);
} catch (Exception e) {
System.out.println(e);
}
}
@Override
public void cancel() {
if(selectedBuildable == null) {
close();
} else {
selectBuildable(null);
}
}
@Override
public void selectionChanged(List<ISelectable> selected) {
if(selected.isEmpty()) return;
opened.set(false);
opened = false;
}
@Override
public void evoke() {
opened.toggle();
if(opened) close();
else open();
}
if(opened.value()) {
selection.clearSelection();
}
public void open() {
if(opened) return;
Assets.sndBubble.play();
opened = true;
reset();
}
public void close() {
if(!opened) return;
Assets.sndCancel.play();
opened = false;
buildLayer.deactiveate();
}
public void reset() {
selectBuildable(null);
}
private void selectCategory(String category) {
selectedCategory = category;
selectBuildable(null);
}
@Override
@ -278,72 +246,52 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
return "Build";
}
@Override
public void mouseEnter() {}
@Override
public void mouseLeave() {}
@Override
public void mouseDown(int button) {}
@Override
public void mouseUp(int button) {}
@Override
public List<Vector4f> getGuiBoxes() {
return List.of(new Vector4f(padding, 576 - BottomBar.bottomBarHeight - padding - height, width, height));
}
@Override
public float getLayer() {
return Layers.GENERAL_UI;
}
private void constructItemButtons() {
for(Button btn : buildableButtons.keySet()) {
remove(btn);
}
buildableButtons.clear();
if(!opened.value()) return;
List<BuildableRecord> categoryBuildables = buildables.get(selectedCategory);
if(categoryBuildables == null) return;
int left = width + padding * 2;
int buttonHeight = 24;
int buttonWidth = 100;
int top = 576 - BottomBar.bottomBarHeight - padding - buttonHeight;
int i = 0;
for(BuildableRecord buildableRecord : categoryBuildables) {
int x = left + (buttonWidth + padding) * i;
int y = top;
int w = buttonWidth;
int h = buttonHeight;
i ++;
Button btn = new SimpleButton(buildableRecord.name, x, y, w, h, Layers.GENERAL_UI_INTERACTABLE);
btn.registerClickListener(this);
add(btn);
buildableButtons.put(btn, buildableRecord);
}
}
@Override
public void click(Button target) {
if(categoryButtons.contains(target)) {
selectedCategory = target.getText();
deselectBuilding();
constructItemButtons();
} else if(buildableButtons.containsKey(target)) {
BuildableRecord newBuildableRecord = buildableButtons.get(target);
selectBuildable(newBuildableRecord);
}
}
@Override
public boolean isButtonClickSilent() {
return true;
}
public void gui() {
if(openness < 0.0001f) return;
int height = 8 + 16 + 8 + buildables.size() * (32 + 8);
window((int) lerp(-180, 0, openness), 576 - BottomBar.bottomBarHeight - height + 1, 150, height, () -> {
text("Build");
for(String category : buildables.keySet()) {
space(8);
if(button(category)) {
if(selectedCategory == category) {
selectCategory(null);
} else {
selectCategory(category);
}
}
}
});
window(149, (int) lerp(576 + 50, 576 - BottomBar.bottomBarHeight - 16 - 32 + 1 - 24, openness), 875, 48 + 24, () -> {
if(selectedCategory == null) {
space(20);
text(" Select a Category...");
return;
}
text(selectedCategory);
space(8);
horizontal(() -> {
for(BuildableRecord buildableRecord : buildables.get(selectedCategory)) {
if(button(buildableRecord.name)) {
selectBuildable(buildableRecord);
}
space(8);
}
});
});
}
}

View File

@ -2,7 +2,7 @@ package xyz.valnet.hadean.gameobjects.ui.tabs;
import xyz.valnet.hadean.HadeanGame;
public class MenuTab extends Tab {
public class DebugTab extends Tab {
@Override
public void evoke() {

View File

@ -1,11 +1,11 @@
package xyz.valnet.hadean.gameobjects.ui.tabs;
import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.engine.graphics.ImmediateUI;
import xyz.valnet.engine.scenegraph.ITransient;
import xyz.valnet.hadean.gameobjects.BottomBar;
import xyz.valnet.hadean.interfaces.IBottomBarItem;
public abstract class Tab extends GameObject implements IBottomBarItem, ITransient {
public abstract class Tab extends ImmediateUI implements IBottomBarItem, ITransient {
private BottomBar bottombar;
@ -23,4 +23,6 @@ public abstract class Tab extends GameObject implements IBottomBarItem, ITransie
public boolean isButtonClickSilent() {
return false;
}
public void gui() {}
}

View File

@ -15,7 +15,7 @@ import xyz.valnet.hadean.gameobjects.ui.Popup;
import xyz.valnet.hadean.gameobjects.ui.tabs.BuildTab;
import xyz.valnet.hadean.gameobjects.ui.tabs.JobBoardTab;
import xyz.valnet.hadean.gameobjects.ui.tabs.LoadTab;
import xyz.valnet.hadean.gameobjects.ui.tabs.MenuTab;
import xyz.valnet.hadean.gameobjects.ui.tabs.DebugTab;
import xyz.valnet.hadean.gameobjects.ui.tabs.SaveTab;
import xyz.valnet.hadean.gameobjects.worldobjects.pawn.Pawn;
@ -55,7 +55,7 @@ public class GameScene extends SceneGraph {
objects.add(new BottomBar());
objects.add(new BuildTab());
objects.add(new JobBoardTab());
objects.add(new MenuTab());
objects.add(new DebugTab());
objects.add(new SaveTab());
objects.add(new LoadTab());