WarpPI/core/src/main/java/it/cavallium/warppi/gui/expression/containers/OutputContainer.java

146 lines
3.7 KiB
Java
Raw Normal View History

package it.cavallium.warppi.gui.expression.containers;
2019-02-27 23:29:03 +01:00
import it.cavallium.warppi.device.display.DisplayOutputDevice;
import it.cavallium.warppi.gui.GraphicalElement;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.CaretState;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.gui.expression.blocks.BlockContainer;
import it.cavallium.warppi.gui.expression.layouts.OutputLayout;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
2018-10-04 23:39:19 +02:00
public abstract class OutputContainer implements GraphicalElement, OutputLayout {
private static final long serialVersionUID = -5714825964892683571L;
2017-06-05 22:50:33 +02:00
public final ObjectArrayList<BlockContainer> roots;
private final Caret caret = new Caret(CaretState.HIDDEN, 0);
public OutputContainer() {
2017-06-05 22:50:33 +02:00
roots = new ObjectArrayList<>();
2018-10-04 23:39:19 +02:00
roots.add(new BlockContainer());
}
2018-09-22 11:17:30 +02:00
public OutputContainer(final boolean small) {
2017-06-05 22:50:33 +02:00
roots = new ObjectArrayList<>();
2018-09-22 14:10:36 +02:00
roots.add(new BlockContainer(null, small));
}
2018-09-22 11:17:30 +02:00
public OutputContainer(final boolean small, final int minWidth, final int minHeight) {
2017-06-05 22:50:33 +02:00
roots = new ObjectArrayList<>();
2018-09-22 14:10:36 +02:00
roots.add(new BlockContainer(null, small));
}
2018-09-22 11:17:30 +02:00
public void setContentAsSingleGroup(final ObjectArrayList<Block> blocks) {
2017-06-05 22:50:33 +02:00
roots.clear();
2018-10-04 23:39:19 +02:00
final BlockContainer bcnt = new BlockContainer();
2018-09-28 11:39:28 +02:00
for (final Block block : blocks) {
2017-06-05 22:50:33 +02:00
bcnt.appendBlockUnsafe(block);
2018-09-28 11:39:28 +02:00
}
2017-06-05 22:50:33 +02:00
roots.add(bcnt);
recomputeDimensions();
}
2018-09-22 11:17:30 +02:00
public void setContentAsMultipleGroups(final ObjectArrayList<ObjectArrayList<Block>> roots) {
2017-06-05 22:50:33 +02:00
this.roots.clear();
2018-05-12 21:18:29 +02:00
for (final ObjectArrayList<Block> blocks : roots) {
2018-10-04 23:39:19 +02:00
final BlockContainer bcnt = new BlockContainer();
2018-09-28 11:39:28 +02:00
for (final Block block : blocks) {
2017-06-05 22:50:33 +02:00
bcnt.appendBlockUnsafe(block);
2018-09-28 11:39:28 +02:00
}
2017-06-05 22:50:33 +02:00
this.roots.add(bcnt);
}
recomputeDimensions();
}
2018-09-22 11:17:30 +02:00
public void setContentAsMultipleElements(final ObjectArrayList<Block> elems) {
2018-05-12 21:18:29 +02:00
roots.clear();
for (final Block block : elems) {
2018-10-04 23:39:19 +02:00
final BlockContainer bcnt = new BlockContainer();
2017-06-05 22:50:33 +02:00
bcnt.appendBlockUnsafe(block);
2018-05-12 21:18:29 +02:00
roots.add(bcnt);
}
recomputeDimensions();
}
@Override
public void recomputeDimensions() {
2018-09-28 11:39:28 +02:00
for (final BlockContainer root : roots) {
2017-06-05 22:50:33 +02:00
root.recomputeDimensions();
2018-09-28 11:39:28 +02:00
}
}
@Override
public int getWidth() {
2017-06-05 22:50:33 +02:00
int maxw = 0;
2018-05-12 21:18:29 +02:00
for (final BlockContainer root : roots) {
final int w = root.getWidth();
2018-09-28 11:39:28 +02:00
if (w > maxw) {
maxw = w;
2018-09-28 11:39:28 +02:00
}
2017-06-05 22:50:33 +02:00
}
return maxw;
}
@Override
public int getHeight() {
2017-06-05 22:50:33 +02:00
int h = 0;
2018-09-28 11:39:28 +02:00
for (final BlockContainer root : roots) {
h += root.getHeight() + 2;
2018-09-28 11:39:28 +02:00
}
if (h > 0) {
return h - 2;
2018-09-28 11:39:28 +02:00
} else {
2017-06-05 22:50:33 +02:00
return h;
2018-09-28 11:39:28 +02:00
}
}
@Override
public int getLine() {
2017-06-05 22:50:33 +02:00
return 0;
}
/**
2018-09-22 11:17:30 +02:00
*
* @param delta
* Time, in seconds
*/
2018-09-22 11:17:30 +02:00
public void beforeRender(final double delta) {
}
/**
2018-09-22 11:17:30 +02:00
*
* @param ge
* Graphic Engine class.
* @param r
* Graphic Renderer class of <b>ge</b>.
* @param x
* Position relative to the window.
* @param y
* Position relative to the window.
*/
2019-02-27 23:29:03 +01:00
public void draw(final DisplayOutputDevice ge, final Renderer r, final int x, final int y) {
2017-06-05 22:50:33 +02:00
int offset = 0;
2018-05-12 21:18:29 +02:00
for (final BlockContainer root : roots) {
root.draw(ge, r, x, y + offset, caret);
offset += root.getHeight() + 2;
2017-06-05 22:50:33 +02:00
}
}
public void clear() {
2017-06-05 22:50:33 +02:00
roots.clear();
2018-10-04 23:39:19 +02:00
roots.add(new BlockContainer());
recomputeDimensions();
}
2017-06-05 22:50:33 +02:00
public boolean isContentEmpty() {
2018-05-12 21:18:29 +02:00
for (final BlockContainer root : roots) {
final ObjectArrayList<Block> cnt = root.getContent();
2018-09-28 11:39:28 +02:00
if (cnt != null && !cnt.isEmpty()) {
2017-06-05 22:50:33 +02:00
return false;
2018-09-28 11:39:28 +02:00
}
2017-06-05 22:50:33 +02:00
}
return true;
}
}