Cleaned up component hierarchy
This commit is contained in:
@@ -20,7 +20,6 @@ import mightypork.utils.annotations.DefaultImpl;
|
||||
import mightypork.utils.files.InstanceLock;
|
||||
import mightypork.utils.logging.Log;
|
||||
import mightypork.utils.logging.LogWriter;
|
||||
import mightypork.utils.math.constraints.Pollable;
|
||||
|
||||
|
||||
/**
|
||||
@@ -256,7 +255,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
// framework events
|
||||
bus.addChannel(DestroyEvent.class, Destroyable.class);
|
||||
bus.addChannel(UpdateEvent.class, Updateable.class);
|
||||
bus.addChannel(LayoutChangeEvent.class, Pollable.class);
|
||||
bus.addChannel(LayoutChangeEvent.class, LayoutChangeEvent.Listener.class);
|
||||
|
||||
// input events
|
||||
bus.addChannel(ScreenChangeEvent.class, ScreenChangeEvent.Listener.class);
|
||||
|
||||
@@ -2,7 +2,6 @@ package mightypork.gamecore.control.bus.events;
|
||||
|
||||
|
||||
import mightypork.gamecore.control.bus.events.types.ImmediateEvent;
|
||||
import mightypork.utils.math.constraints.Pollable;
|
||||
|
||||
|
||||
/**
|
||||
@@ -12,15 +11,19 @@ import mightypork.utils.math.constraints.Pollable;
|
||||
* @author MightyPork
|
||||
*/
|
||||
@ImmediateEvent
|
||||
public class LayoutChangeEvent implements Event<Pollable> {
|
||||
public class LayoutChangeEvent implements Event<LayoutChangeEvent.Listener> {
|
||||
|
||||
public LayoutChangeEvent() {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handleBy(Pollable handler)
|
||||
public void handleBy(Listener handler)
|
||||
{
|
||||
handler.poll();
|
||||
handler.onLayoutChanged();
|
||||
}
|
||||
|
||||
public interface Listener {
|
||||
public void onLayoutChanged();
|
||||
}
|
||||
}
|
||||
|
||||
+43
-20
@@ -2,28 +2,36 @@ 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.input.InputSystem;
|
||||
import mightypork.gamecore.render.DisplaySystem;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
|
||||
|
||||
public abstract class BusEnabledPainter extends SimplePainter implements ClientHub, Component, AppAccess {
|
||||
public abstract class AbstractLayoutComponent extends AbstractVisualComponent implements LayoutComponent, AppAccess {
|
||||
|
||||
private boolean enabled;
|
||||
private boolean visible = true;
|
||||
|
||||
private final AppSubModule subModule;
|
||||
final LinkedList<Component> elements = new LinkedList<>();
|
||||
|
||||
|
||||
public BusEnabledPainter(AppAccess app) {
|
||||
public AbstractLayoutComponent(AppAccess app, RectBound context) {
|
||||
this.subModule = new AppSubModule(app);
|
||||
setRect(context);
|
||||
}
|
||||
|
||||
|
||||
public AbstractLayoutComponent(AppAccess app) {
|
||||
this(app, null);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public EventBus getEventBus()
|
||||
{
|
||||
@@ -110,28 +118,43 @@ public abstract class BusEnabledPainter extends SimplePainter implements ClientH
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setVisible(boolean visible)
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
this.visible = visible;
|
||||
if (elem == null) return;
|
||||
|
||||
elements.add(elem);
|
||||
addChildClient(elem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderComponent()
|
||||
{
|
||||
for (final Component element : elements) {
|
||||
element.render();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isVisible()
|
||||
public void updateLayout()
|
||||
{
|
||||
return visible;
|
||||
for (final Component element : elements) {
|
||||
element.render();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final void render()
|
||||
{
|
||||
if (!visible) return;
|
||||
paint();
|
||||
}
|
||||
|
||||
|
||||
protected abstract void paint();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package mightypork.gamecore.gui.components;
|
||||
|
||||
|
||||
import mightypork.gamecore.control.bus.events.LayoutChangeEvent;
|
||||
import mightypork.utils.annotations.DefaultImpl;
|
||||
import mightypork.utils.math.constraints.rect.Rect;
|
||||
import mightypork.utils.math.constraints.rect.caching.AbstractRectCache;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBoundAdapter;
|
||||
|
||||
|
||||
/**
|
||||
* {@link Renderable} with pluggable context
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class AbstractVisualComponent extends AbstractRectCache implements Component, LayoutChangeEvent.Listener {
|
||||
|
||||
private Rect source;
|
||||
private boolean visible = true;
|
||||
|
||||
|
||||
@Override
|
||||
public final Rect getRect()
|
||||
{
|
||||
return super.getRect();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final void setRect(RectBound rect)
|
||||
{
|
||||
this.source = new RectBoundAdapter(rect);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final boolean isVisible()
|
||||
{
|
||||
return visible;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final void setVisible(boolean visible)
|
||||
{
|
||||
this.visible = visible;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Rect getCacheSource()
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final void render()
|
||||
{
|
||||
if (!visible) return;
|
||||
|
||||
renderComponent();
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
public final void onLayoutChanged()
|
||||
{
|
||||
poll();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final void onChange()
|
||||
{
|
||||
updateLayout();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Draw the component (it's visible)
|
||||
*/
|
||||
public abstract void renderComponent();
|
||||
|
||||
|
||||
@Override
|
||||
@DefaultImpl
|
||||
public void updateLayout()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,17 @@
|
||||
package mightypork.gamecore.gui.components;
|
||||
|
||||
|
||||
import mightypork.gamecore.control.bus.clients.ToggleableClient;
|
||||
import mightypork.gamecore.control.interf.Enableable;
|
||||
import mightypork.gamecore.gui.Hideable;
|
||||
import mightypork.utils.math.constraints.rect.Rect;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
|
||||
|
||||
/**
|
||||
* UI component interface
|
||||
* Basic UI component interface
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public interface Component extends Hideable, PluggableRenderable, Enableable, ToggleableClient {
|
||||
|
||||
/**
|
||||
* Enable the component. This includes listening to event bus, and any
|
||||
* event-related actions.
|
||||
*/
|
||||
@Override
|
||||
void enable(boolean yes);
|
||||
|
||||
|
||||
@Override
|
||||
boolean isEnabled();
|
||||
|
||||
public interface Component extends Hideable, PluggableRenderable {
|
||||
|
||||
/**
|
||||
* Set visible. When not visible, the component should not render.
|
||||
@@ -46,10 +32,16 @@ public interface Component extends Hideable, PluggableRenderable, Enableable, To
|
||||
void setRect(RectBound rect);
|
||||
|
||||
|
||||
/**
|
||||
* Render the component, if it is visible.
|
||||
*/
|
||||
@Override
|
||||
void render();
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isListening();
|
||||
/**
|
||||
* The bounding rect was changed. The component should now update any cached
|
||||
* constraints derived from it.
|
||||
*/
|
||||
void updateLayout();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package mightypork.gamecore.gui.components;
|
||||
|
||||
|
||||
import mightypork.gamecore.control.bus.clients.ToggleableClient;
|
||||
import mightypork.gamecore.control.interf.Enableable;
|
||||
import mightypork.gamecore.gui.ActionTrigger;
|
||||
|
||||
|
||||
public interface InputComponent extends Component, Enableable, ActionTrigger, ToggleableClient {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package mightypork.gamecore.gui.components;
|
||||
|
||||
import mightypork.gamecore.control.bus.clients.ClientHub;
|
||||
import mightypork.gamecore.control.interf.Enableable;
|
||||
|
||||
|
||||
public interface LayoutComponent extends Component, Enableable, ClientHub {
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package mightypork.gamecore.gui.components;
|
||||
|
||||
|
||||
import mightypork.utils.annotations.DefaultImpl;
|
||||
import mightypork.utils.math.constraints.rect.*;
|
||||
import mightypork.utils.math.constraints.rect.caching.AbstractRectCache;
|
||||
import mightypork.utils.math.constraints.rect.caching.RectCache;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBoundAdapter;
|
||||
|
||||
|
||||
/**
|
||||
* {@link Renderable} with pluggable context
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class SimplePainter extends AbstractRectCache implements PluggableRenderable {
|
||||
|
||||
private RectCache source;
|
||||
|
||||
|
||||
@Override
|
||||
public Rect getRect()
|
||||
{
|
||||
return super.getRect();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setRect(RectBound rect)
|
||||
{
|
||||
this.source = new RectBoundAdapter(rect).cached();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Rect getCacheSource()
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public abstract void render();
|
||||
|
||||
|
||||
/**
|
||||
* Called after constraint was changed; contained constraints can now poll too.
|
||||
*/
|
||||
@Override
|
||||
@DefaultImpl
|
||||
public void onChange()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package mightypork.gamecore.gui.components.layout;
|
||||
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
import mightypork.gamecore.control.AppAccess;
|
||||
import mightypork.gamecore.control.bus.EventBus;
|
||||
import mightypork.gamecore.gui.components.BusEnabledPainter;
|
||||
import mightypork.gamecore.gui.components.PluggableRenderable;
|
||||
import mightypork.gamecore.gui.components.Renderable;
|
||||
import mightypork.gamecore.gui.components.SimplePainter;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
|
||||
|
||||
/**
|
||||
* Bag for {@link SimplePainter} elements with constraints.<br>
|
||||
* Elements are exposed to {@link EventBus}.
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class AbstractLayout extends BusEnabledPainter {
|
||||
|
||||
final LinkedList<PluggableRenderable> elements = new LinkedList<>();
|
||||
|
||||
|
||||
/**
|
||||
* @param app app access
|
||||
*/
|
||||
public AbstractLayout(AppAccess app) {
|
||||
super(app);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param app app access
|
||||
* @param context boudning context
|
||||
*/
|
||||
public AbstractLayout(AppAccess app, RectBound context) {
|
||||
super(app);
|
||||
setRect(context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add element to the holder, setting it's context.<br>
|
||||
* Element must then be attached using the <code>attach</code> method.
|
||||
*
|
||||
* @param elem element
|
||||
*/
|
||||
public abstract void add(PluggableRenderable elem);
|
||||
|
||||
|
||||
/**
|
||||
* Connect to bus and add to element list
|
||||
*
|
||||
* @param elem element; it's context will be set to the constraint.
|
||||
*/
|
||||
public void attach(PluggableRenderable elem)
|
||||
{
|
||||
if (elem == null) return;
|
||||
|
||||
elements.add(elem);
|
||||
addChildClient(elem);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void paint()
|
||||
{
|
||||
for (final Renderable element : elements) {
|
||||
element.render();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,8 @@ package mightypork.gamecore.gui.components.layout;
|
||||
|
||||
|
||||
import mightypork.gamecore.control.AppAccess;
|
||||
import mightypork.gamecore.gui.components.PluggableRenderable;
|
||||
import mightypork.gamecore.gui.components.AbstractLayoutComponent;
|
||||
import mightypork.gamecore.gui.components.Component;
|
||||
import mightypork.utils.math.constraints.rect.builders.TiledRect;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
|
||||
@@ -12,7 +13,7 @@ import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class ColumnHolder extends AbstractLayout {
|
||||
public class ColumnHolder extends AbstractLayoutComponent {
|
||||
|
||||
private final TiledRect tiler;
|
||||
private int col = 0;
|
||||
@@ -47,7 +48,7 @@ public class ColumnHolder extends AbstractLayout {
|
||||
* @param elem
|
||||
*/
|
||||
@Override
|
||||
public void add(final PluggableRenderable elem)
|
||||
public void add(final Component elem)
|
||||
{
|
||||
if (elem == null) return;
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@ package mightypork.gamecore.gui.components.layout;
|
||||
|
||||
|
||||
import mightypork.gamecore.control.AppAccess;
|
||||
import mightypork.gamecore.gui.components.PluggableRenderable;
|
||||
import mightypork.gamecore.gui.components.AbstractLayoutComponent;
|
||||
import mightypork.gamecore.gui.components.Component;
|
||||
import mightypork.utils.math.constraints.rect.builders.TiledRect;
|
||||
import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
|
||||
@@ -12,7 +13,7 @@ import mightypork.utils.math.constraints.rect.proxy.RectBound;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class RowHolder extends AbstractLayout {
|
||||
public class RowHolder extends AbstractLayoutComponent {
|
||||
|
||||
private final TiledRect tiler;
|
||||
private int row = 0;
|
||||
@@ -47,7 +48,7 @@ public class RowHolder extends AbstractLayout {
|
||||
* @param elem
|
||||
*/
|
||||
@Override
|
||||
public void add(final PluggableRenderable elem)
|
||||
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.SimplePainter;
|
||||
import mightypork.gamecore.gui.components.AbstractVisualComponent;
|
||||
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 SimplePainter {
|
||||
public class ImagePainter extends AbstractVisualComponent {
|
||||
|
||||
private TxQuad texture;
|
||||
|
||||
@@ -34,9 +34,17 @@ public class ImagePainter extends SimplePainter {
|
||||
|
||||
|
||||
@Override
|
||||
public void render()
|
||||
public void updateLayout()
|
||||
{
|
||||
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.SimplePainter;
|
||||
import mightypork.gamecore.gui.components.AbstractVisualComponent;
|
||||
import mightypork.gamecore.render.Render;
|
||||
import mightypork.utils.annotations.FactoryMethod;
|
||||
import mightypork.utils.math.color.RGB;
|
||||
@@ -12,7 +12,20 @@ import mightypork.utils.math.color.RGB;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class QuadPainter extends SimplePainter {
|
||||
public class QuadPainter extends AbstractVisualComponent {
|
||||
|
||||
@FactoryMethod
|
||||
public static QuadPainter gradH(RGB colorLeft, RGB colorRight)
|
||||
{
|
||||
return new QuadPainter(colorLeft, colorRight, colorRight, colorLeft);
|
||||
}
|
||||
|
||||
|
||||
@FactoryMethod
|
||||
public static QuadPainter gradV(RGB colorTop, RGB colorBottom)
|
||||
{
|
||||
return new QuadPainter(colorTop, colorTop, colorBottom, colorBottom);
|
||||
}
|
||||
|
||||
private final RGB colorHMinVMin;
|
||||
private final RGB colorHMaxVMin;
|
||||
@@ -50,23 +63,8 @@ public class QuadPainter extends SimplePainter {
|
||||
|
||||
|
||||
@Override
|
||||
public void render()
|
||||
public void renderComponent()
|
||||
{
|
||||
Render.quadColor(getRect(), colorHMinVMin, colorHMaxVMin, colorHMaxVMax, colorHMinVMax);
|
||||
}
|
||||
|
||||
|
||||
@FactoryMethod
|
||||
public static QuadPainter gradH(RGB colorLeft, RGB colorRight)
|
||||
{
|
||||
return new QuadPainter(colorLeft, colorRight, colorRight, colorLeft);
|
||||
}
|
||||
|
||||
|
||||
@FactoryMethod
|
||||
public static QuadPainter gradV(RGB colorTop, RGB colorBottom)
|
||||
{
|
||||
return new QuadPainter(colorTop, colorTop, colorBottom, colorBottom);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mightypork.gamecore.gui.components.painters;
|
||||
|
||||
|
||||
import mightypork.gamecore.gui.components.SimplePainter;
|
||||
import mightypork.gamecore.gui.components.AbstractVisualComponent;
|
||||
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 SimplePainter {
|
||||
public class TextPainter extends AbstractVisualComponent {
|
||||
|
||||
private final FontRenderer font;
|
||||
private RGB color;
|
||||
@@ -79,7 +79,7 @@ public class TextPainter extends SimplePainter {
|
||||
|
||||
|
||||
@Override
|
||||
public void render()
|
||||
public void renderComponent()
|
||||
{
|
||||
if (text == null) return;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ package mightypork.rogue.screens.test_bouncyboxes;
|
||||
import java.util.Random;
|
||||
|
||||
import mightypork.gamecore.control.timing.Updateable;
|
||||
import mightypork.gamecore.gui.components.SimplePainter;
|
||||
import mightypork.gamecore.gui.components.AbstractVisualComponent;
|
||||
import mightypork.gamecore.render.Render;
|
||||
import mightypork.utils.math.Easing;
|
||||
import mightypork.utils.math.color.RGB;
|
||||
@@ -14,7 +14,7 @@ import mightypork.utils.math.constraints.rect.Rect;
|
||||
import mightypork.utils.math.constraints.rect.caching.RectCache;
|
||||
|
||||
|
||||
public class BouncyBox extends SimplePainter implements Updateable {
|
||||
public class BouncyBox extends AbstractVisualComponent implements Updateable {
|
||||
|
||||
private final Random rand = new Random();
|
||||
|
||||
@@ -35,7 +35,7 @@ public class BouncyBox extends SimplePainter implements Updateable {
|
||||
|
||||
|
||||
@Override
|
||||
public void render()
|
||||
public void renderComponent()
|
||||
{
|
||||
Render.quad(box, RGB.GREEN);
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public class BouncyBox extends SimplePainter implements Updateable {
|
||||
|
||||
|
||||
@Override
|
||||
public void onChange()
|
||||
public void updateLayout()
|
||||
{
|
||||
box.poll();
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public abstract class AbstractRectCache extends RectAdapter implements Constrain
|
||||
@Override
|
||||
protected final Rect getSource()
|
||||
{
|
||||
if (!inited) markDigestDirty();
|
||||
if (!inited) poll();
|
||||
|
||||
return (cachingEnabled ? cache : getCacheSource());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user