simple button & selection manipulation

pull/1/head
Valerie 2022-05-21 02:37:59 -04:00
parent d10bf0900b
commit bb19f4df72
8 changed files with 103 additions and 32 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.9 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

View File

@ -80,7 +80,6 @@ public class App {
public void invoke(long window, int button, int action, int mods) {
if(button >= 3) return;
// System.out.println("window: " + window + ", button: " + button + ", action: " + action + ", mods: " + mods);
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 ; }

View File

@ -18,9 +18,7 @@ public class SimpleShader extends Shader {
}
public void pushColor(Vector4f color) {
// System.out.println("pushing " + color + " onto color stack");
colorStack.push(color);
// printColorStack();
setUniform4f("uColor", color);
}
@ -31,10 +29,8 @@ public class SimpleShader extends Shader {
}
public void popColor() {
// System.out.println("popping the color stack");
colorStack.pop();
Vector4f newColor = colorStack.peek();
// printColorStack();
if(newColor == null) {
setUniform4f("uColor", Vector4f.one);
@ -42,13 +38,6 @@ public class SimpleShader extends Shader {
}
setUniform4f("uColor", newColor);
}
// private void printColorStack() {
// for(Vector4f color : colorStack) {
// System.out.println(" " + color);
// }
// System.out.println("");
// }
@Override
protected void bindAttributes(int program) {

View File

@ -29,9 +29,10 @@ public class Selection extends GameObject {
listeners.add(listener);
}
private float distance(Vector2f a, Vector2f b) {
return (float) Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}
// TODO implement click vs single select using distance
// private float distance(Vector2f a, Vector2f b) {
// return (float) Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
// }
@Override
public void tick(float dTime) {
@ -39,7 +40,7 @@ public class Selection extends GameObject {
if(animation > animationMax) animation = animationMax;
// TODO at some point, this will need to be blocked by other things on top. like a ui over the scene should make selections like, not happen?!
if(App.mouseLeft) {
if(App.mouseRight) {
Vector2f currentMouseCoords = new Vector2f(App.mouseX, App.mouseY);
if(initialCoords == null) {
initialCoords = currentMouseCoords;

View File

@ -7,10 +7,13 @@ import java.util.List;
import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.hadean.input.Button;
import xyz.valnet.hadean.input.IButtonListener;
import xyz.valnet.hadean.input.SimpleButton;
import xyz.valnet.hadean.util.Assets;
public class SelectionUI extends GameObject implements ISelectionChangeListener, IButtonListener {
private String name = "";
private int count = 0;
private List<ISelectable> selected = new ArrayList<ISelectable>();
private HashMap<String, Integer> selectedTypes = new HashMap<String, Integer>();
private HashMap<String, Button> narrowButtons = new HashMap<String, Button>();
@ -18,6 +21,14 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
private Selection selectionManager;
// this will be null normally, but set if
// a button has been pressed to update the selection.
// its a simple workaround to get rid of a concurrent
// exception, where the buttons are attempting to
// change while updating.
// TODO this could be fixed by delaying button clicks to the next frame.
private List<ISelectable> newSelection = null;;
public void start() {
selectionManager = get(Selection.class);
selectionManager.subscribe(this);
@ -35,13 +46,22 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
// i ++;
// }
for(Button btn : narrowButtons.values()) {
btn.draw();
if(selectedTypes.size() == 1) {
Assets.font.drawString("" + count + "x " + name, 26, 376);
} else {
for(Button btn : narrowButtons.values()) {
btn.draw();
}
}
}
@Override
public void tick(float dTime) {
if(newSelection != null) {
selectionManager.updateSelection(newSelection);
newSelection = null;
}
for(Button btn : narrowButtons.values()) {
btn.update();
}
@ -56,11 +76,7 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
narrowBuckets.clear();
for(ISelectable selectable : selected) {
String name = selectable.getClass().getName();
System.out.println(name);
String[] splitName = name.split("\\.");
for(String s : splitName) {
System.out.println(s);
}
String shortName = splitName[splitName.length - 1];
if(selectedTypes.containsKey(name)) {
@ -68,20 +84,25 @@ public class SelectionUI extends GameObject implements ISelectionChangeListener,
Button btn = narrowButtons.get(name);
List<ISelectable> items = narrowBuckets.get(btn);
items.add(selectable);
btn.setText("" + items.size() + "x " + shortName);
count ++;
} else {
Button btn = new Button(Assets.uiFrame, shortName, 20, 376 + 30 * selectedTypes.size(), 280, 24);
Button btn = new SimpleButton("1x " + shortName, 20, 376 + 30 * selectedTypes.size(), 280, 24);
btn.registerClickListener(this);
selectedTypes.put(name, 1);
narrowButtons.put(name, btn);
List<ISelectable> list = new ArrayList<ISelectable>();
list.add(selectable);
narrowBuckets.put(btn, list);
count = 1;
this.name = shortName;
}
}
}
@Override
public void click(Button target) {
if(! narrowBuckets.containsKey(target)) return;
newSelection = narrowBuckets.get(target);
}
}

View File

@ -11,9 +11,11 @@ import xyz.valnet.hadean.util.Assets;
public class Button {
private final int x, y, width, height;
private final String text;
private final Tile9 frame;
private final int textWidth, textHeight;
private String text;
protected Tile9 frame;
protected Tile9 frameHover;
protected Tile9 frameActive;
private int textWidth, textHeight;
private float hPad, vPad;
private Vector4i box;
@ -27,16 +29,28 @@ public class Button {
this.y = y;
width = w;
height = h;
this.text = text;
this.frame = frame;
Vector4i measuredText = Assets.font.measure(text);
textWidth = measuredText.x;
textHeight = measuredText.y;
this.frameActive = frame;
this.frameHover = frame;
setText(text);
box = new Vector4i(x, y, w, h);
}
public void setText(String text) {
this.text = text;
Vector4i measuredText = Assets.font.measure(text);
textWidth = measuredText.x;
textHeight = measuredText.y;
}
public void draw() {
frame.draw(box.x, box.y, box.z, box.w);
if(state == HOVER) {
frameHover.draw(box.x, box.y, box.z, box.w);
} else if(state == ACTIVE) {
frameActive.draw(box.x, box.y, box.z, box.w);
} else {
frame.draw(box.x, box.y, box.z, box.w);
}
Assets.flat.pushColor(Vector4f.black);
Assets.font.drawString(text, 1 + x + (width - textWidth) / 2, 1 + y + (height - textHeight) / 2);

View File

@ -0,0 +1,21 @@
package xyz.valnet.hadean.input;
import xyz.valnet.hadean.util.Assets;
public class SimpleButton extends Button {
public SimpleButton(String text, int x, int y, int w, int h) {
super(Assets.uiFrame, text, x, y, w, h);
this.activeHPad = 0f;
this.activeVPad = 0f;
this.hoverHPad = 0f;
this.hoverVPad = 0f;
this.frame = Assets.uiFrame;
this.frameHover = Assets.uiFrameLight;
this.frameActive = Assets.uiFrameDark;
}
}

View File

@ -20,6 +20,8 @@ public class Assets {
public static final Tile9 selectionFrame;
public static final Tile9 selectedFrame;
public static final Tile9 uiFrame;
public static final Tile9 uiFrameLight;
public static final Tile9 uiFrameDark;
public static final Sprite[] defaultTerrain;
@ -213,6 +215,30 @@ public class Assets {
new Sprite(atlas, new Vector4i(33, 87, 6, 1)),
new Sprite(atlas, new Vector4i(39, 87, 1, 1))
);
uiFrameLight = new Tile9(
new Sprite(atlas, new Vector4i(24, 80, 1, 1)),
new Sprite(atlas, new Vector4i(25, 80, 6, 1)),
new Sprite(atlas, new Vector4i(31, 80, 1, 1)),
new Sprite(atlas, new Vector4i(24, 81, 1, 6)),
new Sprite(atlas, new Vector4i(25, 81, 6, 6)),
new Sprite(atlas, new Vector4i(31, 81, 1, 6)),
new Sprite(atlas, new Vector4i(24, 87, 1, 1)),
new Sprite(atlas, new Vector4i(25, 87, 6, 1)),
new Sprite(atlas, new Vector4i(31, 87, 1, 1))
);
uiFrameDark = new Tile9(
new Sprite(atlas, new Vector4i(0, 96, 1, 1)),
new Sprite(atlas, new Vector4i(1, 96, 6, 1)),
new Sprite(atlas, new Vector4i(7, 96, 1, 1)),
new Sprite(atlas, new Vector4i(0, 97, 1, 6)),
new Sprite(atlas, new Vector4i(1, 97, 6, 6)),
new Sprite(atlas, new Vector4i(7, 97, 1, 6)),
new Sprite(atlas, new Vector4i(0, 103, 1, 1)),
new Sprite(atlas, new Vector4i(1, 103, 6, 1)),
new Sprite(atlas, new Vector4i(7, 103, 1, 1))
);
}
}