fixed components and painters, some tweaks to layer system

This commit is contained in:
ondra
2014-04-15 17:45:57 +02:00
parent 7ee201a6f7
commit 3cad5ad202
28 changed files with 428 additions and 359 deletions
+4 -1
View File
@@ -36,6 +36,9 @@ public abstract class Action implements Runnable, Enableable {
}
/**
* Run the action, if it's enabled.
*/
@Override
public final void run()
{
@@ -46,6 +49,6 @@ public abstract class Action implements Runnable, Enableable {
/**
* Do the work.
*/
public abstract void execute();
protected abstract void execute();
}
@@ -1,160 +0,0 @@
package mightypork.gamecore.gui.components;
import java.util.Collection;
import java.util.LinkedList;
import mightypork.gamecore.audio.SoundSystem;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppSubModule;
import mightypork.gamecore.control.bus.EventBus;
import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.utils.math.constraints.rect.proxy.RectBound;
public abstract class AbstractLayoutComponent extends AbstractVisualComponent implements LayoutComponent, AppAccess {
private boolean enabled;
private final AppSubModule subModule;
final LinkedList<Component> elements = new LinkedList<>();
public AbstractLayoutComponent(AppAccess app, RectBound context) {
this.subModule = new AppSubModule(app);
setRect(context);
}
public AbstractLayoutComponent(AppAccess app) {
this(app, null);
}
@Override
public EventBus getEventBus()
{
return subModule.getEventBus();
}
@Override
public Collection<Object> getChildClients()
{
return subModule.getChildClients();
}
@Override
public boolean doesDelegate()
{
return subModule.doesDelegate();
}
@Override
public boolean isListening()
{
return subModule.isListening();
}
@Override
public SoundSystem getSoundSystem()
{
return subModule.getSoundSystem();
}
@Override
public InputSystem getInput()
{
return subModule.getInput();
}
@Override
public DisplaySystem getDisplay()
{
return subModule.getDisplay();
}
@Override
public void shutdown()
{
subModule.shutdown();
}
@Override
public void addChildClient(Object client)
{
subModule.addChildClient(client);
}
@Override
public void removeChildClient(Object client)
{
subModule.removeChildClient(client);
}
@Override
public void enable(boolean yes)
{
subModule.setDelegating(yes);
subModule.setListening(yes);
enabled = yes;
}
@Override
public boolean isEnabled()
{
return enabled;
}
/**
* Add element to the holder, setting it's context.<br>
* Element must then be then attached using the <code>attach</code> method.
*
* @param elem element
*/
public abstract void add(Component elem);
/**
* Connect to bus and add to element list
*
* @param elem element; it's context will be set to the constraint.
*/
public final void attach(Component elem)
{
if (elem == null) return;
elements.add(elem);
addChildClient(elem);
}
@Override
public void renderComponent()
{
for (final Component element : elements) {
element.render();
}
}
@Override
public void updateLayout()
{
for (final Component element : elements) {
element.render();
}
}
}
@@ -0,0 +1,30 @@
package mightypork.gamecore.gui.components;
import mightypork.gamecore.control.bus.events.MouseButtonEvent;
public abstract class ClickableComponent extends InputComponent {
private boolean btnDownOver;
@Override
public void receive(MouseButtonEvent event)
{
if (!event.isButtonEvent()) return;
if (event.isDown()) {
btnDownOver = event.isOver(this);
}
if (event.isUp()) {
if (btnDownOver && event.isOver(this)) {
triggerAction();
}
btnDownOver = false;
}
}
}
@@ -1,11 +1,43 @@
package mightypork.gamecore.gui.components;
import mightypork.gamecore.control.bus.clients.ToggleableClient;
import mightypork.gamecore.control.bus.events.MouseButtonEvent;
import mightypork.gamecore.control.interf.Enableable;
import mightypork.gamecore.gui.Action;
import mightypork.gamecore.gui.ActionTrigger;
public interface InputComponent extends Component, Enableable, ActionTrigger, ToggleableClient {
public abstract class InputComponent extends VisualComponent implements Component, Enableable, ActionTrigger, MouseButtonEvent.Listener {
private boolean enabled;
private Action action;
@Override
public void setAction(Action action)
{
this.action = action;
}
@Override
public void enable(boolean yes)
{
this.enabled = yes;
}
@Override
public boolean isEnabled()
{
return enabled;
}
@Override
public abstract void renderComponent();
protected void triggerAction() {
action.run();
}
}
@@ -1,9 +1,154 @@
package mightypork.gamecore.gui.components;
import java.util.Collection;
import java.util.LinkedList;
import mightypork.gamecore.audio.SoundSystem;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.control.AppSubModule;
import mightypork.gamecore.control.bus.EventBus;
import mightypork.gamecore.control.bus.clients.ClientHub;
import mightypork.gamecore.control.interf.Enableable;
import mightypork.gamecore.input.InputSystem;
import mightypork.gamecore.render.DisplaySystem;
import mightypork.utils.math.constraints.rect.proxy.RectBound;
public interface LayoutComponent extends Component, Enableable, ClientHub {
public abstract class LayoutComponent extends VisualComponent implements Component, Enableable, ClientHub, AppAccess {
private boolean enabled;
private final AppSubModule subModule;
final LinkedList<Component> components = new LinkedList<>();
public LayoutComponent(AppAccess app, RectBound context) {
this.subModule = new AppSubModule(app);
setRect(context);
}
public LayoutComponent(AppAccess app) {
this(app, null);
}
@Override
public EventBus getEventBus()
{
return subModule.getEventBus();
}
@Override
public Collection<Object> getChildClients()
{
return subModule.getChildClients();
}
@Override
public boolean doesDelegate()
{
return subModule.doesDelegate();
}
@Override
public boolean isListening()
{
return subModule.isListening();
}
@Override
public SoundSystem getSoundSystem()
{
return subModule.getSoundSystem();
}
@Override
public InputSystem getInput()
{
return subModule.getInput();
}
@Override
public DisplaySystem getDisplay()
{
return subModule.getDisplay();
}
@Override
public void shutdown()
{
subModule.shutdown();
}
@Override
public void addChildClient(Object client)
{
subModule.addChildClient(client);
}
@Override
public void removeChildClient(Object client)
{
subModule.removeChildClient(client);
}
@Override
public void enable(boolean yes)
{
subModule.setDelegating(yes);
subModule.setListening(yes);
enabled = yes;
}
@Override
public boolean isEnabled()
{
return enabled;
}
/**
* Connect to bus and add to element list
*
* @param component added component, whose context has already been set.
*/
public final void attach(Component component)
{
if (component == null) return;
components.add(component);
addChildClient(component);
}
@Override
public void renderComponent()
{
for (final Component cmp : components) {
cmp.render();
}
}
@Override
public void updateLayout()
{
for (final Component cmp : components) {
cmp.updateLayout();
}
}
}
@@ -10,16 +10,22 @@ import mightypork.utils.math.constraints.rect.proxy.RectBoundAdapter;
/**
* {@link Renderable} with pluggable context
* {@link Renderable} with pluggable context. When caching is enabled, the
* layout update can be triggered by firing the {@link LayoutChangeEvent}.
*
* @author MightyPork
*/
public abstract class AbstractVisualComponent extends AbstractRectCache implements Component, LayoutChangeEvent.Listener {
public abstract class VisualComponent extends AbstractRectCache implements Component, LayoutChangeEvent.Listener {
private Rect source;
private boolean visible = true;
public VisualComponent() {
super();
}
@Override
public final Rect getRect()
{
@@ -2,7 +2,7 @@ package mightypork.gamecore.gui.components.layout;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.gui.components.AbstractLayoutComponent;
import mightypork.gamecore.gui.components.LayoutComponent;
import mightypork.gamecore.gui.components.Component;
import mightypork.utils.math.constraints.rect.builders.TiledRect;
import mightypork.utils.math.constraints.rect.proxy.RectBound;
@@ -13,7 +13,7 @@ import mightypork.utils.math.constraints.rect.proxy.RectBound;
*
* @author MightyPork
*/
public class ColumnHolder extends AbstractLayoutComponent {
public class ColumnHolder extends LayoutComponent {
private final TiledRect tiler;
private int col = 0;
@@ -47,7 +47,6 @@ public class ColumnHolder extends AbstractLayoutComponent {
*
* @param elem
*/
@Override
public void add(final Component elem)
{
if (elem == null) return;
@@ -0,0 +1,38 @@
package mightypork.gamecore.gui.components.layout;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.gui.components.LayoutComponent;
import mightypork.gamecore.gui.components.Component;
import mightypork.utils.math.constraints.rect.proxy.RectBound;
/**
* Layout for components with arbitrary constraints.
*
* @author MightyPork
*/
public class ConstraintLayout extends LayoutComponent {
public ConstraintLayout(AppAccess app) {
super(app);
}
public ConstraintLayout(AppAccess app, RectBound context) {
super(app, context);
}
/**
* Add a component to the layout.<br>
* The component's rect must be set up manually.
*
* @param component
*/
public void add(Component component)
{
attach(component);
}
}
@@ -2,7 +2,7 @@ package mightypork.gamecore.gui.components.layout;
import mightypork.gamecore.control.AppAccess;
import mightypork.gamecore.gui.components.AbstractLayoutComponent;
import mightypork.gamecore.gui.components.LayoutComponent;
import mightypork.gamecore.gui.components.Component;
import mightypork.utils.math.constraints.rect.builders.TiledRect;
import mightypork.utils.math.constraints.rect.proxy.RectBound;
@@ -13,7 +13,7 @@ import mightypork.utils.math.constraints.rect.proxy.RectBound;
*
* @author MightyPork
*/
public class RowHolder extends AbstractLayoutComponent {
public class RowHolder extends LayoutComponent {
private final TiledRect tiler;
private int row = 0;
@@ -47,7 +47,6 @@ public class RowHolder extends AbstractLayoutComponent {
*
* @param elem
*/
@Override
public void add(final Component elem)
{
if (elem == null) return;
@@ -1,7 +1,7 @@
package mightypork.gamecore.gui.components.painters;
import mightypork.gamecore.gui.components.AbstractVisualComponent;
import mightypork.gamecore.gui.components.VisualComponent;
import mightypork.gamecore.render.Render;
import mightypork.gamecore.render.textures.TxQuad;
@@ -11,7 +11,7 @@ import mightypork.gamecore.render.textures.TxQuad;
*
* @author MightyPork
*/
public class ImagePainter extends AbstractVisualComponent {
public class ImagePainter extends VisualComponent {
private TxQuad texture;
@@ -34,17 +34,9 @@ public class ImagePainter extends AbstractVisualComponent {
@Override
public void updateLayout()
public void renderComponent()
{
Render.quadTextured(getRect(), texture);
}
@Override
public void renderComponent()
{
// TODO Auto-generated method stub
}
}
@@ -1,7 +1,7 @@
package mightypork.gamecore.gui.components.painters;
import mightypork.gamecore.gui.components.AbstractVisualComponent;
import mightypork.gamecore.gui.components.VisualComponent;
import mightypork.gamecore.render.Render;
import mightypork.utils.annotations.FactoryMethod;
import mightypork.utils.math.color.RGB;
@@ -12,7 +12,7 @@ import mightypork.utils.math.color.RGB;
*
* @author MightyPork
*/
public class QuadPainter extends AbstractVisualComponent {
public class QuadPainter extends VisualComponent {
@FactoryMethod
public static QuadPainter gradH(RGB colorLeft, RGB colorRight)
@@ -1,7 +1,7 @@
package mightypork.gamecore.gui.components.painters;
import mightypork.gamecore.gui.components.AbstractVisualComponent;
import mightypork.gamecore.gui.components.VisualComponent;
import mightypork.gamecore.render.fonts.FontRenderer;
import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.gamecore.render.fonts.GLFont;
@@ -19,7 +19,7 @@ import mightypork.utils.string.StringProvider.StringWrapper;
*
* @author MightyPork
*/
public class TextPainter extends AbstractVisualComponent {
public class TextPainter extends VisualComponent {
private final FontRenderer font;
private RGB color;
@@ -94,13 +94,13 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@Override
public void receive(ScreenChangeEvent event)
public final void receive(ScreenChangeEvent event)
{
if (!isActive()) return;
onSizeChanged(event.getScreenSize());
// poll constraints
// fire event
getEventBus().sendDirectToChildren(this, new LayoutChangeEvent());
needSetupViewport = true;
@@ -108,7 +108,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@Override
public Rect getRect()
public final Rect getRect()
{
return getDisplay().getRect();
}
@@ -137,7 +137,6 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@DefaultImpl
protected void onScreenEnter()
{
//
}
@@ -147,7 +146,6 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@DefaultImpl
protected void onScreenLeave()
{
//
}
@@ -159,7 +157,6 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@DefaultImpl
protected void onSizeChanged(Vect size)
{
//
}
@@ -174,16 +171,4 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
*/
public abstract String getName();
protected final Rect bounds()
{
return getRect();
}
protected final Vect mouse()
{
return getInput().getMousePos();
}
}
@@ -1,15 +1,18 @@
package mightypork.gamecore.gui.screens;
import java.util.Collection;
import java.util.LinkedHashSet;
import mightypork.gamecore.control.AppSubModule;
import mightypork.gamecore.control.timing.Updateable;
import mightypork.gamecore.gui.Hideable;
import mightypork.gamecore.gui.components.Renderable;
import mightypork.gamecore.gui.components.layout.ConstraintLayout;
import mightypork.gamecore.input.KeyBinder;
import mightypork.gamecore.input.KeyBindingPool;
import mightypork.gamecore.input.KeyStroke;
import mightypork.utils.annotations.DefaultImpl;
import mightypork.utils.math.constraints.rect.Rect;
import mightypork.utils.math.constraints.rect.proxy.RectBound;
import mightypork.utils.math.constraints.vect.Vect;
@@ -18,14 +21,25 @@ import mightypork.utils.math.constraints.vect.Vect;
*
* @author MightyPork
*/
public abstract class ScreenLayer extends AppSubModule implements Comparable<ScreenLayer>, Renderable, RectBound, KeyBinder, Hideable {
private final Screen screen;
public abstract class ScreenLayer extends AppSubModule implements Updateable, Comparable<ScreenLayer>, Renderable, KeyBinder, Hideable {
private boolean visible = true;
private final KeyBindingPool keybindings = new KeyBindingPool();
/** Root layout, rendered and attached to the event bus */
protected final ConstraintLayout root;
protected final Vect mouse;
private final Screen screen;
/** Extra rendered items (outside root) */
protected final Collection<Renderable> rendered = new LinkedHashSet<>();
/** Extra updated items (outside root - those can just implement Updateable) */
protected final Collection<Updateable> updated = new LinkedHashSet<>();
/**
* @param screen parent screen
@@ -34,7 +48,17 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
super(screen); // screen as AppAccess
this.screen = screen;
this.mouse = getInput().getMousePos();
this.root = new ConstraintLayout(screen, screen);
addChildClient(root);
addChildClient(keybindings);
rendered.add(root);
// root is on the bus, all attached components
// will receive events (such as update)
}
@@ -62,14 +86,7 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
@Override
public Rect getRect()
{
return screen.getRect();
}
@Override
public boolean isVisible()
public final boolean isVisible()
{
return visible;
}
@@ -95,7 +112,6 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
@DefaultImpl
protected void onScreenEnter()
{
//
}
@@ -105,7 +121,6 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
@DefaultImpl
protected void onScreenLeave()
{
//
}
@@ -117,25 +132,42 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
@DefaultImpl
protected void onSizeChanged(Vect size)
{
//
}
/**
* @return higher = on top.
*/
@DefaultImpl
public abstract int getPriority();
protected final Rect bounds()
@Override
public final void render()
{
return screen.bounds();
if (!visible) return;
renderLayer();
}
protected final Vect mouse()
/**
* Render layer contents.
*/
protected void renderLayer()
{
return screen.mouse();
// render renderables (including root layout)
for (Renderable r : rendered)
r.render();
}
@Override
public void update(double delta)
{
// update updateables
for (Updateable u : updated)
u.update(delta);
}
}
@@ -174,7 +174,7 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
/**
* Get absolute mouse position
* Get absolute mouse position. This vect is final and views at it can safely be made.
*
* @return mouse position
*/
@@ -186,7 +186,7 @@ public class DisplaySystem extends AppModule implements RectBound {
/**
* Get screen size
* Get screen size. This Vect is final and views at it can safely be made.
*
* @return size
*/
@@ -240,6 +240,9 @@ public class DisplaySystem extends AppModule implements RectBound {
}
/**
* Get screen rect. This Rect is final and views at it can safely be made.
*/
@Override
public Rect getRect()
{
@@ -256,8 +259,13 @@ public class DisplaySystem extends AppModule implements RectBound {
}
/**
* Get screen center. This vect is final and views at it can safely be made.
*
* @return screen center.
*/
public Vect getCenter()
{
return getSize().half();
return rect.center();
}
}
@@ -334,8 +334,9 @@ public class CachedFont implements GLFont {
private void drawQuad(float xmin, float ymin, float xmax, float ymax, float txmin, float tymin, float txmax, float tymax)
{
final float draw_h = xmax - xmin;
final float draw_w = ymax - ymin;
final float draw_width = xmax - xmin;
final float draw_height = ymax - ymin;
final float txmin01 = txmin / textureWidth;
final float tymin01 = tymin / textureHeight;
final float twidth01 = ((txmax - txmin) / textureWidth);
@@ -345,13 +346,13 @@ public class CachedFont implements GLFont {
glVertex2f(xmin, ymin);
glTexCoord2f(txmin01, tymin01 + theight01);
glVertex2f(xmin, ymin + draw_w);
glVertex2f(xmin, ymin + draw_height);
glTexCoord2f(txmin01 + twidth01, tymin01 + theight01);
glVertex2f(xmin + draw_h, ymin + draw_w);
glVertex2f(xmin + draw_width, ymin + draw_height);
glTexCoord2f(txmin01 + twidth01, tymin01);
glVertex2f(xmin + draw_h, ymin);
glVertex2f(xmin + draw_width, ymin);
}
@@ -17,7 +17,7 @@ public class NullFont implements GLFont {
@Override
public void draw(String str, RGB color)
{
Log.w("Drawing with null font.");
// yeah right
}