You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
2.3 KiB
125 lines
2.3 KiB
10 years ago
|
package mightypork.gamecore.gui.components;
|
||
|
|
||
|
|
||
10 years ago
|
import mightypork.gamecore.gui.Enableable;
|
||
10 years ago
|
import mightypork.gamecore.gui.events.LayoutChangeEvent;
|
||
|
import mightypork.gamecore.gui.events.LayoutChangeListener;
|
||
10 years ago
|
import mightypork.gamecore.input.InputSystem;
|
||
10 years ago
|
import mightypork.gamecore.render.Renderable;
|
||
10 years ago
|
import mightypork.gamecore.util.annot.DefaultImpl;
|
||
|
import mightypork.gamecore.util.math.constraints.rect.Rect;
|
||
|
import mightypork.gamecore.util.math.constraints.rect.caching.AbstractRectCache;
|
||
|
import mightypork.gamecore.util.math.constraints.rect.proxy.RectBound;
|
||
|
import mightypork.gamecore.util.math.constraints.rect.proxy.RectBoundAdapter;
|
||
10 years ago
|
|
||
|
|
||
|
/**
|
||
10 years ago
|
* {@link Renderable} with pluggable context. When caching is enabled, the
|
||
|
* layout update can be triggered by firing the {@link LayoutChangeEvent}.
|
||
10 years ago
|
*
|
||
|
* @author MightyPork
|
||
|
*/
|
||
10 years ago
|
public abstract class BaseComponent extends AbstractRectCache implements Component, LayoutChangeListener, Enableable {
|
||
10 years ago
|
|
||
|
private Rect source;
|
||
|
private boolean visible = true;
|
||
|
|
||
10 years ago
|
private int disableLevel = 0;
|
||
10 years ago
|
|
||
10 years ago
|
|
||
|
public BaseComponent() {
|
||
10 years ago
|
enableCaching(false);
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
@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()
|
||
|
{
|
||
10 years ago
|
return source.round(); // round to avoid visual artifacts in fonts and such
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public final void render()
|
||
|
{
|
||
10 years ago
|
if (!isVisible()) return;
|
||
10 years ago
|
|
||
|
renderComponent();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
|
||
|
@Override
|
||
|
public final void onLayoutChanged()
|
||
|
{
|
||
10 years ago
|
if (getRect() == null) {
|
||
|
throw new NullPointerException("Component is missing a bounding rect.");
|
||
|
}
|
||
10 years ago
|
poll();
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
10 years ago
|
public final void onConstraintChanged()
|
||
10 years ago
|
{
|
||
|
updateLayout();
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
protected boolean isMouseOver()
|
||
|
{
|
||
|
return InputSystem.getMousePos().isInside(this);
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Draw the component (it's visible)
|
||
|
*/
|
||
10 years ago
|
protected abstract void renderComponent();
|
||
10 years ago
|
|
||
|
|
||
|
@Override
|
||
|
@DefaultImpl
|
||
|
public void updateLayout()
|
||
|
{
|
||
|
}
|
||
10 years ago
|
|
||
|
|
||
|
@Override
|
||
|
public void enable(boolean yes)
|
||
|
{
|
||
|
if (yes) {
|
||
|
if (disableLevel > 0) disableLevel--;
|
||
|
} else {
|
||
|
disableLevel++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public boolean isEnabled()
|
||
|
{
|
||
|
return disableLevel == 0;
|
||
|
}
|
||
10 years ago
|
}
|