parent
3842285038
commit
56b61e5936
@ -0,0 +1,160 @@ |
||||
package mightypork.gamecore.gui.screens; |
||||
|
||||
|
||||
import mightypork.gamecore.control.AppAccess; |
||||
import mightypork.gamecore.control.AppSubModule; |
||||
import mightypork.gamecore.control.events.LayoutChangeEvent; |
||||
import mightypork.gamecore.control.events.ViewportChangeEvent; |
||||
import mightypork.gamecore.input.KeyBinder; |
||||
import mightypork.gamecore.input.KeyBindingPool; |
||||
import mightypork.gamecore.input.KeyStroke; |
||||
import mightypork.gamecore.render.Render; |
||||
import mightypork.util.annotations.DefaultImpl; |
||||
import mightypork.util.constraints.rect.Rect; |
||||
|
||||
|
||||
/** |
||||
* Screen class. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public abstract class BaseScreen extends AppSubModule implements Screen, KeyBinder, ViewportChangeEvent.Listener { |
||||
|
||||
private final KeyBindingPool keybindings = new KeyBindingPool(); |
||||
|
||||
private volatile boolean active; |
||||
private volatile boolean needSetupViewport = false; |
||||
|
||||
|
||||
/** |
||||
* @param app app access |
||||
*/ |
||||
public BaseScreen(AppAccess app) { |
||||
super(app); |
||||
|
||||
// disable events initially
|
||||
setListening(false); |
||||
|
||||
addChildClient(keybindings); |
||||
} |
||||
|
||||
|
||||
private void fireLayoutChangeEvent() |
||||
{ |
||||
getEventBus().sendDirectToChildren(this, new LayoutChangeEvent()); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public final void bindKeyStroke(KeyStroke stroke, Runnable task) |
||||
{ |
||||
keybindings.bindKeyStroke(stroke, task); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public final void unbindKeyStroke(KeyStroke stroke) |
||||
{ |
||||
keybindings.unbindKeyStroke(stroke); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Prepare for being shown |
||||
* |
||||
* @param shown true to show, false to hide |
||||
*/ |
||||
@Override |
||||
public final void setActive(boolean shown) |
||||
{ |
||||
if (shown) { |
||||
active = true; |
||||
needSetupViewport = true; |
||||
|
||||
fireLayoutChangeEvent(); |
||||
onScreenEnter(); |
||||
|
||||
// enable events
|
||||
setListening(true); |
||||
|
||||
} else { |
||||
onScreenLeave(); |
||||
|
||||
active = false; |
||||
|
||||
// disable events
|
||||
setListening(false); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @return true if screen is the current screen |
||||
*/ |
||||
@Override |
||||
public final boolean isActive() |
||||
{ |
||||
return active; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public final void onViewportChanged(ViewportChangeEvent event) |
||||
{ |
||||
if (!isActive()) return; |
||||
|
||||
// fire event
|
||||
fireLayoutChangeEvent(); |
||||
|
||||
needSetupViewport = true; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public final Rect getRect() |
||||
{ |
||||
return getDisplay().getRect(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void render() |
||||
{ |
||||
if (!isActive()) return; |
||||
|
||||
if (needSetupViewport) { |
||||
Render.setupOrtho(getDisplay().getSize()); |
||||
} |
||||
|
||||
Render.pushState(); |
||||
|
||||
renderScreen(); |
||||
|
||||
Render.popState(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Called when the screen becomes active |
||||
*/ |
||||
@DefaultImpl |
||||
protected void onScreenEnter() |
||||
{ |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Called when the screen is no longer active |
||||
*/ |
||||
@DefaultImpl |
||||
protected void onScreenLeave() |
||||
{ |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Render screen contents (context is ready for 2D rendering) |
||||
*/ |
||||
protected abstract void renderScreen(); |
||||
|
||||
} |
@ -0,0 +1,140 @@ |
||||
package mightypork.gamecore.gui.screens; |
||||
|
||||
|
||||
import java.util.Collection; |
||||
import java.util.LinkedHashSet; |
||||
|
||||
import mightypork.gamecore.control.AppAccess; |
||||
import mightypork.gamecore.control.AppSubModule; |
||||
import mightypork.gamecore.control.events.LayoutChangeEvent; |
||||
import mightypork.gamecore.gui.Hideable; |
||||
import mightypork.gamecore.gui.components.VisualComponent; |
||||
import mightypork.gamecore.gui.components.layout.ConstraintLayout; |
||||
import mightypork.gamecore.input.KeyBinder; |
||||
import mightypork.gamecore.input.KeyBindingPool; |
||||
import mightypork.gamecore.input.KeyStroke; |
||||
import mightypork.gamecore.render.Renderable; |
||||
import mightypork.util.annotations.DefaultImpl; |
||||
import mightypork.util.constraints.vect.Vect; |
||||
import mightypork.util.control.timing.Updateable; |
||||
|
||||
|
||||
/** |
||||
* Abstract overlay.<br> |
||||
* Overlay is connected to event bus and is renderable. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public abstract class Overlay extends AppSubModule implements Updateable, Comparable<Overlay>, Renderable, KeyBinder, Hideable, LayoutChangeEvent.Listener { |
||||
|
||||
private boolean visible = true; |
||||
private final KeyBindingPool keybindings = new KeyBindingPool(); |
||||
|
||||
/** Root layout, rendered and attached to the event bus. */ |
||||
protected final ConstraintLayout root; |
||||
|
||||
/** Constraint: Mouse position. */ |
||||
protected final Vect mouse; |
||||
|
||||
/** 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<>(); |
||||
|
||||
|
||||
public Overlay(AppAccess app) { |
||||
super(app); |
||||
|
||||
this.mouse = getInput().getMousePos(); |
||||
|
||||
this.root = new ConstraintLayout(app, getDisplay()); |
||||
addChildClient(root); |
||||
addChildClient(keybindings); |
||||
|
||||
rendered.add(root); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public final void bindKeyStroke(KeyStroke stroke, Runnable task) |
||||
{ |
||||
keybindings.bindKeyStroke(stroke, task); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public final void unbindKeyStroke(KeyStroke stroke) |
||||
{ |
||||
keybindings.unbindKeyStroke(stroke); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public final boolean isVisible() |
||||
{ |
||||
return visible; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void setVisible(boolean visible) |
||||
{ |
||||
this.visible = visible; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public final int compareTo(Overlay o) |
||||
{ |
||||
return getPriority() - o.getPriority(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get rendering priority |
||||
* |
||||
* @return higher = on top. |
||||
*/ |
||||
@DefaultImpl |
||||
public abstract int getPriority(); |
||||
|
||||
|
||||
/** |
||||
* Render the overlay. The caller MUST check for visibility himself. |
||||
*/ |
||||
@Override |
||||
public void render() |
||||
{ |
||||
for (Renderable r : rendered) { |
||||
r.render(); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void update(double delta) |
||||
{ |
||||
for (Updateable u : updated) { |
||||
u.update(delta); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* <p> |
||||
* Screen size changed. |
||||
* </p> |
||||
* <p> |
||||
* Layouts / components should listen for this event and update their cached |
||||
* constraints; components added to root or directly to this overlay as |
||||
* child clients will receive the event. |
||||
* </p> |
||||
*/ |
||||
@Override |
||||
@DefaultImpl |
||||
public void onLayoutChanged() |
||||
{ |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue