parent
2699d8d549
commit
2a349f471f
@ -0,0 +1,7 @@ |
||||
package mightypork.gamecore.gui.components; |
||||
|
||||
|
||||
public interface DynamicWidthComponent extends Component { |
||||
|
||||
double computeWidth(double height); |
||||
} |
@ -0,0 +1,78 @@ |
||||
package mightypork.gamecore.gui.components; |
||||
|
||||
|
||||
import mightypork.gamecore.util.math.constraints.num.Num; |
||||
import mightypork.gamecore.util.math.constraints.rect.Rect; |
||||
import mightypork.gamecore.util.math.constraints.rect.proxy.RectBound; |
||||
import mightypork.gamecore.util.math.constraints.vect.Vect; |
||||
import mightypork.gamecore.util.math.constraints.vect.proxy.VectAdapter; |
||||
|
||||
|
||||
public abstract class LinearComponent extends BaseComponent implements DynamicWidthComponent { |
||||
|
||||
private final Rect rect = new Rect() { |
||||
|
||||
@Override |
||||
public Vect size() |
||||
{ |
||||
return new Vect() { |
||||
|
||||
@Override |
||||
public double x() |
||||
{ |
||||
return computeWidth(y()); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public double y() |
||||
{ |
||||
return height.value(); |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public Vect origin() |
||||
{ |
||||
return new VectAdapter() { |
||||
|
||||
@Override |
||||
protected Vect getSource() |
||||
{ |
||||
return origin; |
||||
} |
||||
}; |
||||
} |
||||
}; |
||||
|
||||
private Vect origin; |
||||
private Num height; |
||||
|
||||
|
||||
public LinearComponent() |
||||
{ |
||||
super.setRect(rect); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void setRect(RectBound rect) |
||||
{ |
||||
throw new RuntimeException("Cannot assign a rect to a linear component. Set origin and height instead."); |
||||
} |
||||
|
||||
|
||||
public void setHeight(Num height) |
||||
{ |
||||
this.height = height; |
||||
} |
||||
|
||||
|
||||
public void setOrigin(Vect origin) |
||||
{ |
||||
this.origin = origin; |
||||
} |
||||
} |
@ -1,8 +1,9 @@ |
||||
package mightypork.gamecore.gui.components; |
||||
package mightypork.gamecore.gui.components.input; |
||||
|
||||
|
||||
import mightypork.gamecore.gui.Action; |
||||
import mightypork.gamecore.gui.ActionTrigger; |
||||
import mightypork.gamecore.gui.components.InputComponent; |
||||
import mightypork.gamecore.input.events.MouseButtonEvent; |
||||
import mightypork.gamecore.input.events.MouseButtonListener; |
||||
|
@ -0,0 +1,63 @@ |
||||
package mightypork.gamecore.gui.components.layout.linear; |
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.List; |
||||
|
||||
import mightypork.gamecore.eventbus.clients.DelegatingClient; |
||||
import mightypork.gamecore.gui.components.Component; |
||||
import mightypork.gamecore.gui.components.LinearComponent; |
||||
|
||||
|
||||
/** |
||||
* Converts a component into a linear component |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public abstract class AbstractLinearWrapper extends LinearComponent implements DelegatingClient { |
||||
|
||||
protected final Component wrapped; |
||||
private final List<Component> list; |
||||
|
||||
|
||||
/** |
||||
* @param wrapped wrapped component. Can be null. |
||||
*/ |
||||
public AbstractLinearWrapper(Component wrapped) |
||||
{ |
||||
this.wrapped = wrapped; |
||||
if (wrapped != null) { |
||||
if (wrapped instanceof LinearComponent) { |
||||
((LinearComponent) wrapped).setHeight(height()); |
||||
((LinearComponent) wrapped).setOrigin(origin()); |
||||
} else { |
||||
wrapped.setRect(this); |
||||
} |
||||
} |
||||
|
||||
list = new ArrayList<>(1); |
||||
list.add(wrapped); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void renderComponent() |
||||
{ |
||||
if (wrapped != null) wrapped.render(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public Collection<?> getChildClients() |
||||
{ |
||||
return list; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public boolean doesDelegate() |
||||
{ |
||||
return true; |
||||
} |
||||
} |
@ -0,0 +1,20 @@ |
||||
package mightypork.gamecore.gui.components.layout.linear; |
||||
|
||||
|
||||
import mightypork.gamecore.util.math.constraints.num.Num; |
||||
|
||||
|
||||
public class LinearGap extends LinearRectangle { |
||||
|
||||
public LinearGap(Num width) |
||||
{ |
||||
super(null, width); |
||||
} |
||||
|
||||
|
||||
public LinearGap(double heightPercent) |
||||
{ |
||||
super(null, Num.ZERO); |
||||
setWidth(height().perc(heightPercent)); |
||||
} |
||||
} |
@ -0,0 +1,90 @@ |
||||
package mightypork.gamecore.gui.components.layout.linear; |
||||
|
||||
|
||||
import mightypork.gamecore.app.AppAccess; |
||||
import mightypork.gamecore.gui.AlignX; |
||||
import mightypork.gamecore.gui.components.DynamicWidthComponent; |
||||
import mightypork.gamecore.gui.components.LayoutComponent; |
||||
import mightypork.gamecore.gui.components.LinearComponent; |
||||
import mightypork.gamecore.util.math.constraints.num.Num; |
||||
import mightypork.gamecore.util.math.constraints.num.NumSum; |
||||
import mightypork.gamecore.util.math.constraints.rect.proxy.RectBound; |
||||
import mightypork.gamecore.util.math.constraints.vect.Vect; |
||||
import mightypork.gamecore.util.math.constraints.vect.proxy.VectAdapter; |
||||
|
||||
|
||||
public class LinearLayout extends LayoutComponent { |
||||
|
||||
public LinearLayout(AppAccess app, AlignX align) |
||||
{ |
||||
super(app); |
||||
this.align = align; |
||||
} |
||||
|
||||
|
||||
public LinearLayout(AppAccess app, RectBound context, AlignX align) |
||||
{ |
||||
super(app, context); |
||||
this.align = align; |
||||
} |
||||
|
||||
private final NumSum totalWidth = new NumSum(); |
||||
|
||||
private final Vect leftAlignOrigin = LinearLayout.this.origin(); |
||||
private final Vect centerAlignOrigin = LinearLayout.this.topCenter().sub(totalWidth.half(), Num.ZERO); |
||||
private final Vect rightAlignOrigin = LinearLayout.this.topRight().sub(totalWidth, Num.ZERO); |
||||
|
||||
private final Vect leftMostOrigin = new VectAdapter() { |
||||
|
||||
@Override |
||||
protected Vect getSource() |
||||
{ |
||||
switch (align) { |
||||
default: |
||||
case LEFT: |
||||
return leftAlignOrigin; |
||||
case CENTER: |
||||
return centerAlignOrigin; |
||||
case RIGHT: |
||||
return rightAlignOrigin; |
||||
} |
||||
} |
||||
}; |
||||
|
||||
private Vect nextOrigin = leftMostOrigin; |
||||
|
||||
private AlignX align = AlignX.LEFT; |
||||
|
||||
|
||||
public void add(DynamicWidthComponent dwcomp) |
||||
{ |
||||
add(new LinearWrapper(dwcomp)); |
||||
} |
||||
|
||||
|
||||
public void add(LinearComponent lincomp) |
||||
{ |
||||
lincomp.setHeight(height()); |
||||
lincomp.setOrigin(nextOrigin); |
||||
nextOrigin = nextOrigin.add(lincomp.width(), Num.ZERO); |
||||
totalWidth.addSummand(lincomp.width()); |
||||
attach(lincomp); |
||||
} |
||||
|
||||
|
||||
public void setAlign(AlignX align) |
||||
{ |
||||
this.align = align; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Add a gap. |
||||
* |
||||
* @param heightPercent percent of height for gap width |
||||
*/ |
||||
public void gap(double heightPercent) |
||||
{ |
||||
add(new LinearGap(heightPercent)); |
||||
} |
||||
} |
@ -0,0 +1,32 @@ |
||||
package mightypork.gamecore.gui.components.layout.linear; |
||||
|
||||
|
||||
import mightypork.gamecore.gui.components.Component; |
||||
import mightypork.gamecore.util.math.constraints.num.Num; |
||||
|
||||
|
||||
public class LinearRectangle extends AbstractLinearWrapper { |
||||
|
||||
private Num width; |
||||
|
||||
|
||||
public LinearRectangle(Component wrapped, Num width) |
||||
{ |
||||
super(wrapped); |
||||
this.width = width; |
||||
} |
||||
|
||||
|
||||
public void setWidth(Num width) |
||||
{ |
||||
this.width = width; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public double computeWidth(double height) |
||||
{ |
||||
return this.width.value(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,21 @@ |
||||
package mightypork.gamecore.gui.components.layout.linear; |
||||
|
||||
|
||||
import mightypork.gamecore.gui.components.Component; |
||||
|
||||
|
||||
public class LinearSquare extends AbstractLinearWrapper { |
||||
|
||||
public LinearSquare(Component wrapped) |
||||
{ |
||||
super(wrapped); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public double computeWidth(double height) |
||||
{ |
||||
return height; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,21 @@ |
||||
package mightypork.gamecore.gui.components.layout.linear; |
||||
|
||||
|
||||
import mightypork.gamecore.gui.components.DynamicWidthComponent; |
||||
|
||||
|
||||
public class LinearWrapper extends AbstractLinearWrapper { |
||||
|
||||
public LinearWrapper(DynamicWidthComponent wrapped) |
||||
{ |
||||
super(wrapped); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public double computeWidth(double height) |
||||
{ |
||||
return ((DynamicWidthComponent) wrapped).computeWidth(height); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,49 @@ |
||||
package mightypork.gamecore.util.math.constraints.num; |
||||
|
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
|
||||
/** |
||||
* Expandable sum of multiple numbers |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public class NumSum extends Num { |
||||
|
||||
private final List<Num> summands = new ArrayList<>(); |
||||
|
||||
|
||||
@Override |
||||
public double value() |
||||
{ |
||||
double v = 0; |
||||
for (final Num n : summands) { |
||||
if (n != null) v += n.value(); |
||||
} |
||||
return v; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Add a number to the sum |
||||
* |
||||
* @param summand added number |
||||
*/ |
||||
public void addSummand(Num summand) |
||||
{ |
||||
summands.add(summand); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Add a number to the sum |
||||
* |
||||
* @param summand added number |
||||
*/ |
||||
public void addSummand(double summand) |
||||
{ |
||||
summands.add(Num.make(summand)); |
||||
} |
||||
} |
@ -0,0 +1,59 @@ |
||||
package mightypork.rogue.screens.layout_testing; |
||||
|
||||
|
||||
import mightypork.gamecore.app.AppAccess; |
||||
import mightypork.gamecore.gui.AlignX; |
||||
import mightypork.gamecore.gui.components.layout.linear.LinearGap; |
||||
import mightypork.gamecore.gui.components.layout.linear.LinearLayout; |
||||
import mightypork.gamecore.gui.components.layout.linear.LinearSquare; |
||||
import mightypork.gamecore.gui.components.painters.QuadPainter; |
||||
import mightypork.gamecore.gui.components.painters.TextPainter; |
||||
import mightypork.gamecore.gui.screens.LayeredScreen; |
||||
import mightypork.gamecore.gui.screens.Screen; |
||||
import mightypork.gamecore.gui.screens.ScreenLayer; |
||||
import mightypork.gamecore.util.math.color.pal.RGB; |
||||
import mightypork.gamecore.util.math.constraints.rect.Rect; |
||||
import mightypork.rogue.Res; |
||||
|
||||
|
||||
public class LayoutTestScreen extends LayeredScreen { |
||||
|
||||
class Layer1 extends ScreenLayer { |
||||
|
||||
public Layer1(Screen screen) |
||||
{ |
||||
super(screen); |
||||
|
||||
final Rect testRect = root.shrink(root.width().perc(10), root.height().perc(45)); |
||||
|
||||
final LinearLayout ll = new LinearLayout(root, AlignX.CENTER); |
||||
ll.setRect(testRect); |
||||
root.add(ll); |
||||
|
||||
ll.add(new LinearSquare(new QuadPainter(RGB.RED))); |
||||
ll.add(new LinearGap(50)); |
||||
ll.add(new LinearSquare(new QuadPainter(RGB.ORANGE))); |
||||
ll.add(new LinearGap(100)); |
||||
ll.add(new LinearSquare(new QuadPainter(RGB.YELLOW))); |
||||
ll.add(new TextPainter(Res.getFont("tiny"), RGB.WHITE, "Text qjf'\"^")); |
||||
ll.add(new LinearSquare(new QuadPainter(RGB.GREEN))); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public int getZIndex() |
||||
{ |
||||
return 10; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
public LayoutTestScreen(AppAccess app) |
||||
{ |
||||
super(app); |
||||
|
||||
addLayer(new Layer1(this)); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue