mirror of
https://github.com/GTNewHorizons/waila.git
synced 2026-11-19 19:59:47 +00:00
Added ENTER and LEAVE events.
This commit is contained in:
1 parent
3d004700fb
commit
67775bad84
7 files changed
+58
-119
No files matched your search
@@ -1,86 +0,0 @@
|
||||
package mcp.mobius.waila.gui;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.lwjgl.input.Mouse;
|
||||
|
||||
import mcp.mobius.waila.gui.interfaces.IWidget;
|
||||
import mcp.mobius.waila.gui.interfaces.IWidgetContainer;
|
||||
import mcp.mobius.waila.gui.widgets.LayoutCanvas;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
|
||||
public abstract class BaseScreen extends GuiScreen{
|
||||
protected GuiScreen parent; // Return screen if available.
|
||||
protected Minecraft mc; // Minecraft instance
|
||||
protected HashMap<String, IWidget> widgets; // List of widgets on this ui
|
||||
|
||||
public BaseScreen(GuiScreen parent) {
|
||||
this.parent = parent;
|
||||
this.mc = Minecraft.getMinecraft();
|
||||
this.widgets = new HashMap<String, IWidget>();
|
||||
|
||||
this.addWidget("canvas", new LayoutCanvas());
|
||||
|
||||
Mouse.getDWheel(); // We init the DWheel method (getDWheel returns the value since the last call, so we have to call it once on ui creation)
|
||||
}
|
||||
|
||||
/////////////////////
|
||||
// WIDGET HANDLING //
|
||||
/////////////////////
|
||||
|
||||
public IWidget addWidget(String name, IWidget widget){
|
||||
this.widgets.put(name, widget);
|
||||
return this.getWidget(name);
|
||||
}
|
||||
|
||||
public IWidget getWidget(String name){
|
||||
return this.widgets.get(name);
|
||||
}
|
||||
|
||||
public IWidget delWidget(String name){
|
||||
IWidget widget = this.getWidget(name);
|
||||
this.widgets.remove(widget);
|
||||
return widget;
|
||||
}
|
||||
|
||||
public IWidget getRoot(){
|
||||
return this.getWidget("canvas");
|
||||
}
|
||||
|
||||
/////////////////////
|
||||
// DRAWING METHODS //
|
||||
/////////////////////
|
||||
|
||||
@Override
|
||||
public void drawScreen(int i, int j, float f) {
|
||||
this.drawDefaultBackground(); //The dark background common to most of the UIs
|
||||
super.drawScreen(i, j, f);
|
||||
|
||||
for (IWidget widget: this.widgets.values())
|
||||
widget.draw();
|
||||
}
|
||||
|
||||
// Used to easily display a UI without having to mess with mc handle.
|
||||
public void display(){
|
||||
this.mc.displayGuiScreen(this);
|
||||
}
|
||||
|
||||
/////////////////////
|
||||
// INPUT METHODS //
|
||||
/////////////////////
|
||||
|
||||
//Keyboard handling. Basic one is just returning to the previous UI when Esc is pressed
|
||||
@Override
|
||||
public void keyTyped(char keyChar, int keyID)
|
||||
{
|
||||
if (keyID == 1)
|
||||
if (this.parent == null){
|
||||
this.mc.displayGuiScreen((GuiScreen)null);
|
||||
this.mc.setIngameFocus();
|
||||
} else
|
||||
this.mc.displayGuiScreen(this.parent);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package mcp.mobius.waila.gui;
|
||||
|
||||
import mcp.mobius.waila.gui.widgets.PictureDisplay;
|
||||
import mcp.mobius.waila.gui.widgets.WidgetGeometry;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
|
||||
public class TestScreen extends BaseScreen {
|
||||
|
||||
public TestScreen(GuiScreen parent) {
|
||||
super(parent);
|
||||
this.getRoot().addWidget("Picture", new PictureDisplay(this.getRoot(), "waila:textures/test.png"));
|
||||
//this.getRoot().getWidget("Picture").setGeometry(new WidgetGeometry(0.0, 0.0, 50.0, 50.0));
|
||||
this.getRoot().getWidget("Picture").setGeometry(new WidgetGeometry(50.0, 50.0, 50.0, 50.0, WidgetGeometry.Align.CENTER, WidgetGeometry.Align.CENTER));
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,12 @@ import mcp.mobius.waila.gui.interfaces.IWidget;
|
||||
|
||||
public class MouseEvent {
|
||||
|
||||
public enum EventType {NONE, MOVE, CLICK, RELEASED, DRAG, WHEEL};
|
||||
public enum EventType {NONE, MOVE, CLICK, RELEASED, DRAG, WHEEL, ENTER, LEAVE};
|
||||
|
||||
public long timestamp;
|
||||
public Minecraft mc;
|
||||
public IWidget widget;
|
||||
public IWidget srcwidget;
|
||||
public IWidget trgwidget;
|
||||
public int x,y,z;
|
||||
public static int buttonCount = Mouse.getButtonCount();
|
||||
public boolean[] buttonState = new boolean[buttonCount];
|
||||
@@ -21,17 +22,19 @@ public class MouseEvent {
|
||||
|
||||
|
||||
public MouseEvent(IWidget widget){
|
||||
this.widget = widget;
|
||||
this.srcwidget = widget;
|
||||
this.timestamp = System.nanoTime();
|
||||
|
||||
this.mc = Minecraft.getMinecraft();
|
||||
|
||||
this.x = Mouse.getEventX() * this.widget.getSize().getX() / this.mc.displayWidth;
|
||||
this.y = this.widget.getSize().getY() - Mouse.getEventY() * this.widget.getSize().getY() / this.mc.displayHeight - 1;
|
||||
this.x = Mouse.getEventX() * this.srcwidget.getSize().getX() / this.mc.displayWidth;
|
||||
this.y = this.srcwidget.getSize().getY() - Mouse.getEventY() * this.srcwidget.getSize().getY() / this.mc.displayHeight - 1;
|
||||
this.z = Mouse.getEventDWheel();
|
||||
|
||||
for (int i = 0; i < buttonCount; i++)
|
||||
buttonState[i] = Mouse.isButtonDown(i);
|
||||
|
||||
this.trgwidget = this.srcwidget.getWidgetAtCoordinates(this.x, this.y);
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
@@ -55,6 +58,11 @@ public class MouseEvent {
|
||||
|
||||
this.type = EventType.NONE;
|
||||
|
||||
if (this.trgwidget != me.trgwidget){
|
||||
this.type = EventType.ENTER;
|
||||
return this.type;
|
||||
}
|
||||
|
||||
if (this.z != 0){
|
||||
this.type = EventType.WHEEL;
|
||||
return this.type;
|
||||
|
||||
@@ -24,14 +24,17 @@ public interface IWidget {
|
||||
IWidget addWidget(String name, IWidget widget);
|
||||
IWidget getWidget(String name);
|
||||
IWidget delWidget(String name);
|
||||
IWidget getWidgetAtCoordinates(int posX, int posY);
|
||||
|
||||
void handleMouseInput();
|
||||
|
||||
void onMouseClick(MouseEvent event);
|
||||
void onMouseDrag(MouseEvent event);
|
||||
void onMouseMove(MouseEvent event);
|
||||
void onMouseReleased(MouseEvent event);
|
||||
void onMouseRelease(MouseEvent event);
|
||||
void onMouseWheel(MouseEvent event);
|
||||
void onMouseEnter(MouseEvent event);
|
||||
void onMouseLeave(MouseEvent event);
|
||||
|
||||
//boolean onMouseEnter();
|
||||
//boolean onMouseMoved();
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package mcp.mobius.waila.gui.interfaces;
|
||||
|
||||
public interface IWidgetContainer {
|
||||
|
||||
}
|
||||
@@ -42,9 +42,6 @@ public class LayoutCanvas extends WidgetBase{
|
||||
|
||||
MouseEvent event = new MouseEvent(this);
|
||||
EventType type = event.getEventType(this.lastMouseEvent);
|
||||
this.lastMouseEvent = event;
|
||||
|
||||
if (type == EventType.NONE) return;
|
||||
|
||||
switch (type){
|
||||
case CLICK:
|
||||
@@ -57,16 +54,26 @@ public class LayoutCanvas extends WidgetBase{
|
||||
this.onMouseMove(event);
|
||||
break;
|
||||
case RELEASED:
|
||||
this.onMouseReleased(event);
|
||||
this.onMouseRelease(event);
|
||||
break;
|
||||
case WHEEL:
|
||||
this.onMouseWheel(event);
|
||||
break;
|
||||
case ENTER:
|
||||
if (event.trgwidget != null)
|
||||
event.trgwidget.onMouseEnter(event);
|
||||
if (this.lastMouseEvent.trgwidget != null){
|
||||
event.type = EventType.LEAVE;
|
||||
this.lastMouseEvent.trgwidget.onMouseLeave(event);
|
||||
}
|
||||
break;
|
||||
case NONE:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
this.lastMouseEvent = event;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -61,13 +61,21 @@ public abstract class WidgetBase implements IWidget {
|
||||
return widget;
|
||||
}
|
||||
|
||||
private IWidget getWidgetAtCoordinates(int posX, int posY){
|
||||
@Override
|
||||
public IWidget getWidgetAtCoordinates(int posX, int posY){
|
||||
for (IWidget widget : this.widgets.values())
|
||||
if ((posX >= widget.getPos().getX()) &&
|
||||
(posX <= widget.getPos().getX() + widget.getSize().getX()) &&
|
||||
(posY >= widget.getPos().getY()) &&
|
||||
(posY <= widget.getPos().getY() + widget.getSize().getY()))
|
||||
return widget;
|
||||
|
||||
if ((posX >= this.getPos().getX()) &&
|
||||
(posX <= this.getPos().getX() + this.getSize().getX()) &&
|
||||
(posY >= this.getPos().getY()) &&
|
||||
(posY <= this.getPos().getY() + this.getSize().getY()))
|
||||
return this;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -128,6 +136,8 @@ public abstract class WidgetBase implements IWidget {
|
||||
|
||||
@Override
|
||||
public void onMouseClick(MouseEvent event) {
|
||||
//System.out.printf("%s %s\n", this, event);
|
||||
|
||||
IWidget widget = this.getWidgetAtCoordinates(event.x, event.y);
|
||||
if (widget != null && widget != this)
|
||||
widget.onMouseClick(event);
|
||||
@@ -135,6 +145,8 @@ public abstract class WidgetBase implements IWidget {
|
||||
|
||||
@Override
|
||||
public void onMouseDrag(MouseEvent event) {
|
||||
//System.out.printf("%s %s\n", this, event);
|
||||
|
||||
IWidget widget = this.getWidgetAtCoordinates(event.x, event.y);
|
||||
if (widget != null && widget != this)
|
||||
widget.onMouseDrag(event);
|
||||
@@ -142,24 +154,39 @@ public abstract class WidgetBase implements IWidget {
|
||||
|
||||
@Override
|
||||
public void onMouseMove(MouseEvent event) {
|
||||
//System.out.printf("%s %s\n", this, event);
|
||||
|
||||
IWidget widget = this.getWidgetAtCoordinates(event.x, event.y);
|
||||
if (widget != null && widget != this)
|
||||
widget.onMouseMove(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMouseReleased(MouseEvent event) {
|
||||
public void onMouseRelease(MouseEvent event) {
|
||||
//System.out.printf("%s %s\n", this, event);
|
||||
|
||||
IWidget widget = this.getWidgetAtCoordinates(event.x, event.y);
|
||||
if (widget != null && widget != this)
|
||||
widget.onMouseReleased(event);
|
||||
widget.onMouseRelease(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMouseWheel(MouseEvent event) {
|
||||
//System.out.printf("%s %s\n", this, event);
|
||||
|
||||
IWidget widget = this.getWidgetAtCoordinates(event.x, event.y);
|
||||
if (widget != null && widget != this)
|
||||
widget.onMouseWheel(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMouseEnter(MouseEvent event) {
|
||||
//System.out.printf("%70s %s\n", this, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMouseLeave(MouseEvent event) {
|
||||
//System.out.printf("%70s %s\n", this, event);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user