Versatile Java game engine with pluggable backends (this was used in Rogue, I think)
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.
gamecore/src/mightypork/gamecore/gui/components/LayoutComponent.java

138 lines
2.4 KiB

package mightypork.gamecore.gui.components;
import java.util.Collection;
import java.util.LinkedList;
import mightypork.utils.eventbus.clients.ClientHub;
import mightypork.utils.eventbus.clients.DelegatingList;
import mightypork.utils.math.constraints.rect.RectBound;
/**
* Component that provides positioning to member components
*
* @author Ondřej Hruška (MightyPork)
*/
public abstract class LayoutComponent extends BaseComponent implements ClientHub {
private final DelegatingList clientList;
final LinkedList<Component> components = new LinkedList<>();
/**
* Layout component with the given context (container)
*
* @param context context
*/
public LayoutComponent(RectBound context)
{
this.clientList = new DelegatingList();
setRect(context);
enableCaching(true); // layout is typically updated only when screen resizes.
}
/**
* Component without context (can be assigned a context using
* <code>setRect()</code>)
*/
public LayoutComponent()
{
this(null);
}
@Override
public Collection<Object> getChildClients()
{
return clientList;
}
@Override
public boolean doesDelegate()
{
return clientList.doesDelegate();
}
@Override
public boolean isListening()
{
return clientList.isListening();
}
@Override
public void addChildClient(Object client)
{
clientList.add(client);
}
@Override
public void removeChildClient(Object client)
{
clientList.remove(client);
}
@Override
public void setEnabled(boolean yes)
{
if (isDirectlyEnabled() != yes) {
super.setEnabled(yes);
for (final Component c : components) {
c.setIndirectlyEnabled(yes);
}
}
}
/**
* Connect to bus and add to element list
*
* @param component added component, whose context has already been set.
*/
protected final void attach(Component component)
{
if (component == null) return;
if (component == this) {
throw new IllegalArgumentException("Uruboros. (infinite recursion evaded)");
}
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();
}
}
@Override
public void setIndirectlyEnabled(boolean yes)
{
super.setIndirectlyEnabled(yes);
for (final Component cmp : components) {
cmp.setIndirectlyEnabled(yes);
}
}
}