2018-09-02 13:36:49 +02:00
|
|
|
package it.cavallium.warppi.gui.expression.blocks;
|
2017-03-03 23:06:43 +01:00
|
|
|
|
2019-02-27 23:29:03 +01:00
|
|
|
import it.cavallium.warppi.device.display.DisplayOutputDevice;
|
2018-09-02 13:36:49 +02:00
|
|
|
import it.cavallium.warppi.gui.GraphicalElement;
|
|
|
|
import it.cavallium.warppi.gui.expression.Caret;
|
|
|
|
import it.cavallium.warppi.gui.expression.ExtraMenu;
|
2018-10-04 23:39:19 +02:00
|
|
|
import it.cavallium.warppi.gui.expression.InputContext;
|
2018-09-02 13:36:49 +02:00
|
|
|
import it.cavallium.warppi.gui.graphicengine.Renderer;
|
|
|
|
import it.cavallium.warppi.math.MathContext;
|
|
|
|
import it.cavallium.warppi.math.parser.features.interfaces.Feature;
|
2018-09-12 22:16:33 +02:00
|
|
|
import it.cavallium.warppi.util.Error;
|
2018-10-04 08:49:11 +02:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
2017-03-03 23:06:43 +01:00
|
|
|
|
2023-08-23 16:47:55 +02:00
|
|
|
public abstract class Block implements TreeBlock, BlockOrContainer, GraphicalElement {
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
protected boolean small;
|
|
|
|
protected int width;
|
|
|
|
protected int height;
|
|
|
|
protected int line;
|
2023-08-23 16:47:55 +02:00
|
|
|
protected BlockContainer parent;
|
2018-10-09 23:51:42 +02:00
|
|
|
|
|
|
|
public Block() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy
|
|
|
|
* @param b
|
|
|
|
*/
|
2023-08-23 16:47:55 +02:00
|
|
|
public Block(BlockContainer parent, Block b) {
|
2018-10-09 23:51:42 +02:00
|
|
|
this.small = b.small;
|
|
|
|
this.width = b.width;
|
|
|
|
this.height = b.height;
|
|
|
|
this.line = b.line;
|
|
|
|
this.parent = parent;
|
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
/**
|
2018-09-22 11:17:30 +02:00
|
|
|
*
|
2017-04-10 22:50:43 +02:00
|
|
|
* @param r
|
|
|
|
* Graphic Renderer class.
|
|
|
|
* @param x
|
|
|
|
* Position relative to the window.
|
|
|
|
* @param y
|
|
|
|
* Position relative to the window.
|
2017-03-03 23:06:43 +01:00
|
|
|
*/
|
2019-02-27 23:29:03 +01:00
|
|
|
public abstract void draw(DisplayOutputDevice ge, Renderer r, int x, int y, Caret caret);
|
2017-03-03 23:06:43 +01:00
|
|
|
|
2023-08-23 16:47:55 +02:00
|
|
|
public abstract boolean appendBlock(Caret caret, Block newBlock);
|
2018-10-04 08:49:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used only to get inner blocks when deleting the parent block.
|
|
|
|
* @return every block of every inner container, or null if empty
|
|
|
|
*/
|
2018-10-04 09:52:14 +02:00
|
|
|
public abstract ObjectArrayList<Block> getInnerBlocks();
|
2017-05-05 22:27:11 +02:00
|
|
|
|
2018-10-05 22:09:54 +02:00
|
|
|
public abstract ObjectArrayList<BlockContainer> getInnerContainers();
|
2018-10-04 09:52:14 +02:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
@Override
|
|
|
|
public abstract void recomputeDimensions();
|
2017-04-09 22:01:03 +02:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
@Override
|
|
|
|
public int getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getHeight() {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getLine() {
|
|
|
|
return line;
|
|
|
|
}
|
2018-09-22 11:17:30 +02:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
public boolean isSmall() {
|
|
|
|
return small;
|
|
|
|
}
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
public abstract void setSmall(boolean small);
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-05-16 22:02:44 +02:00
|
|
|
public ExtraMenu<?> getExtraMenu() {
|
2017-05-05 22:27:11 +02:00
|
|
|
return null;
|
|
|
|
}
|
2017-05-26 22:37:18 +02:00
|
|
|
|
|
|
|
public abstract Feature toFeature(MathContext context) throws Error;
|
2018-09-28 11:39:28 +02:00
|
|
|
|
2018-09-22 14:10:36 +02:00
|
|
|
@Override
|
2023-08-23 16:47:55 +02:00
|
|
|
public BlockContainer getParentContainer() {
|
2018-09-22 14:10:36 +02:00
|
|
|
return parent;
|
|
|
|
}
|
2018-09-28 11:39:28 +02:00
|
|
|
|
2023-08-23 16:47:55 +02:00
|
|
|
@Override
|
|
|
|
public BlockContainer getParent() {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockOrContainer getAt(int index) {
|
|
|
|
var ic = getInnerContainers();
|
|
|
|
return ic.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getIndex() {
|
|
|
|
return parent != null ? parent.getContentUnsafe().indexOf(this) : -1;
|
|
|
|
}
|
|
|
|
|
2018-09-22 14:10:36 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasParent() {
|
|
|
|
return parent != null;
|
|
|
|
}
|
|
|
|
|
2023-08-23 16:47:55 +02:00
|
|
|
public void setParent(final BlockContainer parent) {
|
2018-09-22 14:10:36 +02:00
|
|
|
this.parent = parent;
|
|
|
|
}
|
2018-10-04 23:39:19 +02:00
|
|
|
|
2023-08-23 16:47:55 +02:00
|
|
|
public abstract Block clone(BlockContainer parent, InputContext ic);
|
|
|
|
|
|
|
|
public BlockReference<?> getReference() {
|
|
|
|
if (this.hasParent()) {
|
|
|
|
return this.parent.getReference(this);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockPosition getLastInnerPosition() {
|
|
|
|
var innerContainers = getInnerContainers();
|
|
|
|
if (innerContainers != null && !innerContainers.isEmpty()) {
|
|
|
|
var lastContainer = innerContainers.get(innerContainers.size() - 1);
|
|
|
|
var content = lastContainer.getContent();
|
|
|
|
if (content.isEmpty()) {
|
|
|
|
return new BlockPosition(lastContainer);
|
|
|
|
} else {
|
|
|
|
return new BlockPosition(content.get(content.size() - 1));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockPosition getFirstInnerPosition() {
|
|
|
|
var innerContainers = getInnerContainers();
|
|
|
|
if (innerContainers != null && !innerContainers.isEmpty()) {
|
|
|
|
return new BlockPosition(innerContainers.get(0));
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 23:06:43 +01:00
|
|
|
}
|