Add support for key event handling in GUI
This commit is contained in:
@@ -5,7 +5,6 @@ import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||
import com.bartlomiejpluta.base.api.game.image.Image;
|
||||
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.game.runner.GameRunner;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
||||
@@ -46,6 +45,4 @@ public interface Context extends Updatable, Renderable, Disposable {
|
||||
void init(Screen screen, Input input, Camera camera);
|
||||
|
||||
void input(Input input);
|
||||
|
||||
void handleKeyEvent(KeyEvent event);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.game.gui.base;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
|
||||
public abstract class BaseWidget implements Widget {
|
||||
protected Widget parent;
|
||||
|
||||
@@ -271,4 +273,9 @@ public abstract class BaseWidget implements Widget {
|
||||
public float getPaddingLeft() {
|
||||
return paddingLeft;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
// Designed to be overridden if needed so
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.bartlomiejpluta.base.api.game.gui.base;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEventHandler;
|
||||
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.Renderable;
|
||||
|
||||
public interface GUI extends Renderable, Disposable {
|
||||
public interface GUI extends Renderable, Disposable, KeyEventHandler {
|
||||
int ALIGN_LEFT = 1 << 0;
|
||||
int ALIGN_CENTER = 1 << 1;
|
||||
int ALIGN_RIGHT = 1 << 2;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.bartlomiejpluta.base.api.game.gui.base;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEventHandler;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
|
||||
public interface Widget {
|
||||
public interface Widget extends KeyEventHandler {
|
||||
Widget getParent();
|
||||
|
||||
void setParent(Widget parent);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -63,4 +65,15 @@ public abstract class BaseContainer extends BaseComponent implements Container {
|
||||
|
||||
return childrenHeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
for (var child : children) {
|
||||
if (!event.isConsumed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
child.handleKeyEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.api.game.gui.window;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.BaseWidget;
|
||||
import com.bartlomiejpluta.base.api.game.gui.component.Component;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
|
||||
public abstract class BaseWindow extends BaseWidget implements Window {
|
||||
protected Component content;
|
||||
@@ -36,4 +37,9 @@ public abstract class BaseWindow extends BaseWidget implements Window {
|
||||
public void onClose(WindowManager manager) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
content.handleKeyEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.api.game.gui.window;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.BaseWidget;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.SizeMode;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
|
||||
import java.util.Deque;
|
||||
@@ -68,6 +69,11 @@ public class DefaultWindowManager extends BaseWidget implements WindowManager {
|
||||
window.onClose(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return windows.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
switch (displayMode) {
|
||||
@@ -86,6 +92,14 @@ public class DefaultWindowManager extends BaseWidget implements WindowManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
var topWindow = windows.peekFirst();
|
||||
if (topWindow != null) {
|
||||
topWindow.handleKeyEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawWindow(Screen screen, Window window, GUI gui) {
|
||||
switch (window.getWindowPosition()) {
|
||||
case TOP -> window.setPosition(
|
||||
|
||||
@@ -8,4 +8,6 @@ public interface WindowManager extends Widget {
|
||||
void open(Window window);
|
||||
|
||||
void close();
|
||||
|
||||
int size();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.bartlomiejpluta.base.api.game.input;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface Input {
|
||||
boolean isKeyPressed(Key key);
|
||||
|
||||
void addKeyEventHandler(Consumer<KeyEvent> handler);
|
||||
void addKeyEventHandler(KeyEventHandler handler);
|
||||
|
||||
void removeKeyEventHandler(Consumer<KeyEvent> handler);
|
||||
void removeKeyEventHandler(KeyEventHandler handler);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.bartlomiejpluta.base.api.game.input;
|
||||
|
||||
public interface KeyEventHandler {
|
||||
void handleKeyEvent(KeyEvent event);
|
||||
}
|
||||
Reference in New Issue
Block a user