new method to clone the input screen

This commit is contained in:
Andrea Cavalli 2018-10-04 23:39:19 +02:00 committed by Cavallium
parent c4f51f8aee
commit 518bfc6c4a
55 changed files with 486 additions and 81 deletions

View File

@ -108,7 +108,7 @@ public class Keyboard {
public static void debugKeyPressed(final int keyCode) {
switch (keyCode) {
case KeyEvent.VK_ESCAPE:
Keyboard.keyPressed(Key.POWEROFF);
Keyboard.keyPressed(Key.BACK);
break;
case KeyEvent.VK_S:
if (Keyboard.shift) {

View File

@ -137,6 +137,8 @@ public class CalculatorHUD extends HUD {
currentDebugLine++;
}
}
r.glColor(0xFF000000);
r.glDrawStringLeft(5, StaticVars.screenSize[1] - ((currentDebugLine+1) * (r.getCurrentFont().getCharacterHeight()+1)), "DEBUG ENABLED");
}
}

View File

@ -1,8 +1,8 @@
package it.cavallium.warppi.gui.expression;
import java.io.Serializable;
import java.util.Arrays;
public class Caret implements Serializable {
public class Caret {
private int pos;
private int remaining;
@ -22,6 +22,18 @@ public class Caret implements Serializable {
this.lastSize = lastSize;
}
/**
* Copy
* @param old
*/
public Caret(Caret old) {
this.pos = old.pos;
this.remaining = old.remaining;
this.state = old.state;
this.lastSize = Arrays.copyOf(old.lastSize, old.lastSize.length);
this.lastLocation = Arrays.copyOf(old.lastLocation, old.lastLocation.length);
}
public void skip(final int i) {
remaining -= i;
}

View File

@ -1,7 +1,5 @@
package it.cavallium.warppi.gui.expression;
import java.io.Serializable;
public enum CaretState implements Serializable {
public enum CaretState {
VISIBLE_ON, VISIBLE_OFF, HIDDEN
}

View File

@ -1,13 +1,14 @@
package it.cavallium.warppi.gui.expression;
import java.io.Serializable;
import java.util.Arrays;
import it.cavallium.warppi.event.KeyboardEventListener;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.gui.expression.blocks.BlockVariable;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
public abstract class ExtraMenu<T extends Block> implements Serializable, KeyboardEventListener {
public abstract class ExtraMenu<T extends Block> implements KeyboardEventListener {
private static final long serialVersionUID = -6944683477814944299L;
@ -18,6 +19,13 @@ public abstract class ExtraMenu<T extends Block> implements Serializable, Keyboa
this.height = 0;
}
public ExtraMenu(final ExtraMenu<T> old, final T newBlock) {
this.block = newBlock;
this.location = Arrays.copyOf(old.location, old.location.length);
this.width = old.width;
this.height = old.height;
}
public final T block;
protected int width;
protected int height;
@ -37,4 +45,8 @@ public abstract class ExtraMenu<T extends Block> implements Serializable, Keyboa
return false;
}
public abstract ExtraMenu<T> clone(InputContext ic);
public abstract ExtraMenu<T> clone(T newBlockVariable);
}

View File

@ -1,13 +1,12 @@
package it.cavallium.warppi.gui.expression;
import java.io.Serializable;
import java.util.HashMap;
import it.cavallium.warppi.gui.expression.blocks.BlockVariable;
import it.cavallium.warppi.math.MathematicalSymbols;
import it.cavallium.warppi.math.functions.Variable.V_TYPE;
public class InputContext implements Serializable {
public class InputContext {
public final HashMap<Character, V_TYPE> variableTypes;
public BlockVariable variableTypeDirtyID = null;
@ -20,4 +19,10 @@ public class InputContext implements Serializable {
public InputContext(final HashMap<Character, V_TYPE> variableTypes) {
this.variableTypes = variableTypes;
}
public InputContext(InputContext ic) {
this.variableTypes = new HashMap<>();
this.variableTypes.putAll(ic.variableTypes);
this.variableTypeDirtyID = ic.variableTypeDirtyID;
}
}

View File

@ -3,6 +3,7 @@ package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.GraphicalElement;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.ExtraMenu;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.MathContext;
@ -93,4 +94,6 @@ public abstract class Block implements TreeBlock, GraphicalElement {
public void setParent(final TreeContainer parent) {
this.parent = parent;
}
public abstract Block clone(InputContext ic);
}

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.MathContext;
@ -17,6 +18,11 @@ public class BlockChar extends Block {
recomputeDimensions();
}
public BlockChar(final char ch, InputContext ic) {
this.ch = ch;
recomputeDimensions();
}
@Override
public void draw(final GraphicEngine ge, final Renderer r, final int x, final int y, final Caret caret) {
BlockContainer.getDefaultFont(small).use(ge);
@ -76,4 +82,9 @@ public class BlockChar extends Block {
return 0;
}
@Override
public BlockChar clone(InputContext ic) {
return new BlockChar(ch, ic);
}
}

View File

@ -1,9 +1,12 @@
package it.cavallium.warppi.gui.expression.blocks;
import java.util.Arrays;
import it.cavallium.warppi.Engine;
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.InputContext;
import it.cavallium.warppi.gui.graphicengine.BinaryFont;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
@ -31,6 +34,11 @@ public class BlockContainer implements TreeContainer, GraphicalElement {
private boolean autoMinimums;
private final TreeBlock parent;
public BlockContainer() {
this(null, false, BlockContainer.getDefaultCharWidth(false), BlockContainer.getDefaultCharHeight(false), true);
autoMinimums = true;
}
public BlockContainer(final TreeBlock parent) {
this(parent, false, BlockContainer.getDefaultCharWidth(false), BlockContainer.getDefaultCharHeight(false), true);
autoMinimums = true;
@ -71,6 +79,26 @@ public class BlockContainer implements TreeContainer, GraphicalElement {
recomputeDimensions();
}
private BlockContainer(BlockContainer old, InputContext ic) {
this.autoMinimums = old.autoMinimums;
this.content = new ObjectArrayList<>();
for (Block b : old.content) {
this.content.add(b.clone(ic));
}
this.height = old.height;
this.line = old.line;
this.minHeight = old.minHeight;
this.minWidth = old.minWidth;
this.parent = old.parent;
this.small = old.small;
this.width = old.width;
this.withBorder = old.withBorder;
}
public BlockContainer clone(InputContext ic) {
return new BlockContainer(this, ic);
}
@Override
public TreeBlock getParentBlock() {
return parent;

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.Function;
@ -26,6 +27,12 @@ public class BlockDivision extends Block {
recomputeDimensions();
}
private BlockDivision(BlockDivision old, InputContext ic) {
containerUp = old.containerUp.clone(ic);
containerDown = old.containerDown.clone(ic);
recomputeDimensions();
}
@Override
public void draw(final GraphicEngine ge, final Renderer r, final int x, final int y, final Caret caret) {
BlockContainer.getDefaultFont(small).use(ge);
@ -132,4 +139,9 @@ public class BlockDivision extends Block {
public int getInnerContainersCount() {
return 2;
}
@Override
public BlockDivision clone(InputContext ic) {
return new BlockDivision(this, ic);
}
}

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.Function;
@ -39,6 +40,12 @@ public class BlockLogarithm extends Block implements IParenthesis {
recomputeDimensions();
}
private BlockLogarithm(BlockLogarithm old, InputContext ic) {
containerBase = old.containerBase.clone(ic);
containerNumber = old.containerNumber.clone(ic);
recomputeDimensions();
}
@Override
public void draw(final GraphicEngine ge, final Renderer r, final int x, final int y, final Caret caret) {
BlockContainer.getDefaultFont(small).use(ge);
@ -171,4 +178,9 @@ public class BlockLogarithm extends Block implements IParenthesis {
return 2;
}
@Override
public BlockLogarithm clone(InputContext ic) {
return new BlockLogarithm(this, ic);
}
}

View File

@ -1,5 +1,6 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.parser.features.FeatureParenthesis;
@ -14,6 +15,10 @@ public class BlockParenthesis extends BlockParenthesisAbstract {
super(blocks);
}
private BlockParenthesis(BlockParenthesis old, InputContext ic) {
super(old, ic);
}
@Override
public Feature toFeature(final MathContext context) throws Error {
final Function cont = getNumberContainer().toFunction(context);
@ -30,4 +35,9 @@ public class BlockParenthesis extends BlockParenthesisAbstract {
return 1;
}
@Override
public BlockParenthesis clone(InputContext ic) {
return new BlockParenthesis(this, ic);
}
}

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.MathContext;
@ -30,6 +31,12 @@ public abstract class BlockParenthesisAbstract extends Block implements IParenth
recomputeDimensions();
}
BlockParenthesisAbstract(BlockParenthesisAbstract old, InputContext ic) {
containerNumber = old.containerNumber.clone(ic);
prefix = old.prefix == null ? null : new String(old.prefix);
recomputeDimensions();
}
public BlockParenthesisAbstract(final ObjectArrayList<Block> blocks) {
containerNumber = new BlockContainer(this, false, blocks);
prefix = null;

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.Function;
@ -19,6 +20,10 @@ public class BlockPower extends Block {
recomputeDimensions();
}
private BlockPower(BlockPower old, InputContext ic) {
this.containerExponent = old.containerExponent.clone(ic);
}
@Override
public void draw(final GraphicEngine ge, final Renderer r, final int x, final int y, final Caret caret) {
BlockContainer.getDefaultFont(true).use(ge);
@ -91,4 +96,9 @@ public class BlockPower extends Block {
public int getInnerContainersCount() {
return 1;
}
@Override
public BlockPower clone(InputContext ic) {
return new BlockPower(this, ic);
}
}

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.Function;
@ -20,6 +21,10 @@ public class BlockPower2 extends Block {
recomputeDimensions();
}
private BlockPower2(BlockPower2 old, InputContext ic) {
this.containerExponent = old.containerExponent.clone(ic);
}
@Override
public int getCaretDeltaPositionAfterCreation() {
return 3;
@ -97,4 +102,9 @@ public class BlockPower2 extends Block {
public int getInnerContainersCount() {
return 1;
}
@Override
public BlockPower2 clone(InputContext ic) {
return new BlockPower2(this, ic);
}
}

View File

@ -1,5 +1,6 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.parser.features.FeatureSine;
@ -12,9 +13,18 @@ public class BlockSine extends BlockParenthesisAbstract {
super("SIN");
}
private BlockSine(BlockSine old, InputContext ic) {
super(old, ic);
}
@Override
public Feature toFeature(final MathContext context) throws Error {
final Function cont = getNumberContainer().toFunction(context);
return new FeatureSine(cont);
}
@Override
public Block clone(InputContext ic) {
return new BlockSine(this, ic);
}
}

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.Function;
@ -21,6 +22,11 @@ public class BlockSquareRoot extends Block {
recomputeDimensions();
}
private BlockSquareRoot(BlockSquareRoot old, InputContext ic) {
this.containerNumber = old.containerNumber.clone(ic);
recomputeDimensions();
}
@Override
public void draw(final GraphicEngine ge, final Renderer r, final int x, final int y, final Caret caret) {
BlockContainer.getDefaultFont(small).use(ge);
@ -108,4 +114,9 @@ public class BlockSquareRoot extends Block {
public int getInnerContainersCount() {
return 1; //2
}
@Override
public BlockSquareRoot clone(InputContext ic) {
return new BlockSquareRoot(this, ic);
}
}

View File

@ -1,6 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.InputContext;
import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.cavallium.warppi.math.MathContext;
@ -15,6 +16,10 @@ public class BlockUndefined extends Block {
recomputeDimensions();
}
private BlockUndefined(BlockUndefined old, InputContext ic) {
recomputeDimensions();
}
@Override
public void draw(final GraphicEngine ge, final Renderer r, final int x, final int y, final Caret caret) {
BlockContainer.getDefaultFont(small).use(ge);
@ -70,4 +75,9 @@ public class BlockUndefined extends Block {
return 0;
}
@Override
public BlockUndefined clone(InputContext ic) {
return new BlockUndefined(this, ic);
}
}

View File

@ -1,5 +1,7 @@
package it.cavallium.warppi.gui.expression.blocks;
import java.util.Arrays;
import it.cavallium.warppi.Engine;
import it.cavallium.warppi.event.KeyPressedEvent;
import it.cavallium.warppi.event.KeyReleasedEvent;
@ -43,6 +45,18 @@ public class BlockVariable extends Block {
recomputeDimensions();
}
private BlockVariable(BlockVariable old, InputContext ic) {
this.ic = ic;
this.ch = old.ch;
type = old.type;
color = old.color;
typeDirtyID = old.typeDirtyID;
this.typeLocked = old.typeLocked;
menu = old.menu == null ? null : new VariableMenu(old.menu, this);
retrieveValue();
recomputeDimensions();
}
private void retrieveValue() {
type = ic.variableTypes.get(ch);
if (type == null) {
@ -53,7 +67,6 @@ public class BlockVariable extends Block {
menu.mustRefreshMenu = true;
}
mustRefresh = true;
System.out.println("retrieve:" + type.toString());
}
public void pushValue() {
@ -145,7 +158,11 @@ public class BlockVariable extends Block {
super(var);
}
private static final long serialVersionUID = 3941994107852212764L;
private VariableMenu(VariableMenu old, BlockVariable newBlockVariable) {
super(old, newBlockVariable);
this.mustRefreshMenu = old.mustRefreshMenu;
this.text = new String(old.text);
}
@Override
public void open() {
@ -250,6 +267,16 @@ public class BlockVariable extends Block {
r.glDrawStringCenter(popupX + width / 2, popupY + 2 + 5, text);
}
@Override
public VariableMenu clone(BlockVariable newBlockVariable) {
return new VariableMenu(this, newBlockVariable);
}
@Override
public VariableMenu clone(InputContext ic) {
return new VariableMenu(this, block.clone(ic));
}
}
@Override
@ -266,4 +293,9 @@ public class BlockVariable extends Block {
public int getInnerContainersCount() {
return 0;
}
@Override
public BlockVariable clone(InputContext ic) {
return new BlockVariable(this, ic);
}
}

View File

@ -1,8 +1,6 @@
package it.cavallium.warppi.gui.expression.blocks;
import java.io.Serializable;
public interface TreeBlock extends Serializable {
public interface TreeBlock {
TreeContainer getParentContainer();
boolean hasParent();

View File

@ -1,8 +1,6 @@
package it.cavallium.warppi.gui.expression.blocks;
import java.io.Serializable;
public interface TreeContainer extends Serializable {
public interface TreeContainer {
TreeBlock getParentBlock();
boolean hasParent();

View File

@ -1,7 +1,5 @@
package it.cavallium.warppi.gui.expression.containers;
import java.io.Serializable;
import it.cavallium.warppi.event.KeyboardEventListener;
import it.cavallium.warppi.gui.GraphicalElement;
import it.cavallium.warppi.gui.expression.Caret;
@ -19,9 +17,9 @@ import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public abstract class InputContainer implements GraphicalElement, InputLayout, Serializable {
public abstract class InputContainer implements GraphicalElement, InputLayout {
private static final long serialVersionUID = 923589369317765667L;
protected final BlockContainer root;
protected BlockContainer root;
protected Caret caret;
private static final float CARET_DURATION = 0.5f;
private float caretTime;
@ -56,6 +54,21 @@ public abstract class InputContainer implements GraphicalElement, InputLayout, S
root = new BlockContainer(null, small, false);
}
/**
* Copy
* @param old
* @param ic
*/
protected InputContainer(InputContainer old, InputContext ic) {
this.caretTime = old.caretTime;
this.extra = old.extra == null ? null : old.extra.clone(ic);
this.maxPosition = old.maxPosition;
this.caret = old.caret == null ? null : new Caret(old.caret);
this.inputContext = ic;
this.root = old.root == null ? null : old.root.clone(ic);
this.parsed = old.parsed;
}
public void typeChar(final char c) {
final Block b = parseChar(c);
typeBlock(b);
@ -117,6 +130,7 @@ public abstract class InputContainer implements GraphicalElement, InputLayout, S
}
public void moveRight(final int delta) {
final int curPos = caret.getPosition();
if (curPos + delta < maxPosition) {
caret.setPosition(curPos + delta);

View File

@ -21,8 +21,6 @@ import it.cavallium.warppi.math.MathematicalSymbols;
public class NormalInputContainer extends InputContainer {
private static final long serialVersionUID = 5236564695997222322L;
@Deprecated()
/**
* Use NormalInputContainer(InputContext) instead
@ -43,6 +41,16 @@ public class NormalInputContainer extends InputContainer {
super(ic, small, minWidth, minHeight);
}
/**
* Copy
* @param userInput
* @param ic
*/
@SuppressWarnings("deprecation")
public NormalInputContainer(InputContainer old, InputContext ic) {
super(old, ic);
}
@Override
public Block parseChar(final char c) {
switch (c) {

View File

@ -1,8 +1,9 @@
package it.cavallium.warppi.gui.expression.containers;
public class NormalOutputContainer extends OutputContainer {
import it.cavallium.warppi.gui.expression.blocks.BlockContainer;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
private static final long serialVersionUID = 5087850494875415142L;
public class NormalOutputContainer extends OutputContainer {
public NormalOutputContainer() {
super();
@ -15,4 +16,9 @@ public class NormalOutputContainer extends OutputContainer {
public NormalOutputContainer(final boolean small, final int minWidth, final int minHeight) {
super(small, minWidth, minHeight);
}
public NormalOutputContainer(OutputContainer old) {
super.roots.clear();
super.roots.addAll(old.roots.clone());
}
}

View File

@ -1,7 +1,5 @@
package it.cavallium.warppi.gui.expression.containers;
import java.io.Serializable;
import it.cavallium.warppi.gui.GraphicalElement;
import it.cavallium.warppi.gui.expression.Caret;
import it.cavallium.warppi.gui.expression.CaretState;
@ -12,14 +10,14 @@ import it.cavallium.warppi.gui.graphicengine.GraphicEngine;
import it.cavallium.warppi.gui.graphicengine.Renderer;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public abstract class OutputContainer implements GraphicalElement, OutputLayout, Serializable {
public abstract class OutputContainer implements GraphicalElement, OutputLayout {
private static final long serialVersionUID = -5714825964892683571L;
public final ObjectArrayList<BlockContainer> roots;
private final Caret caret = new Caret(CaretState.HIDDEN, 0);
public OutputContainer() {
roots = new ObjectArrayList<>();
roots.add(new BlockContainer(null));
roots.add(new BlockContainer());
}
public OutputContainer(final boolean small) {
@ -34,7 +32,7 @@ public abstract class OutputContainer implements GraphicalElement, OutputLayout,
public void setContentAsSingleGroup(final ObjectArrayList<Block> blocks) {
roots.clear();
final BlockContainer bcnt = new BlockContainer(null);
final BlockContainer bcnt = new BlockContainer();
for (final Block block : blocks) {
bcnt.appendBlockUnsafe(block);
}
@ -45,7 +43,7 @@ public abstract class OutputContainer implements GraphicalElement, OutputLayout,
public void setContentAsMultipleGroups(final ObjectArrayList<ObjectArrayList<Block>> roots) {
this.roots.clear();
for (final ObjectArrayList<Block> blocks : roots) {
final BlockContainer bcnt = new BlockContainer(null);
final BlockContainer bcnt = new BlockContainer();
for (final Block block : blocks) {
bcnt.appendBlockUnsafe(block);
}
@ -57,7 +55,7 @@ public abstract class OutputContainer implements GraphicalElement, OutputLayout,
public void setContentAsMultipleElements(final ObjectArrayList<Block> elems) {
roots.clear();
for (final Block block : elems) {
final BlockContainer bcnt = new BlockContainer(null);
final BlockContainer bcnt = new BlockContainer();
bcnt.appendBlockUnsafe(block);
roots.add(bcnt);
}
@ -131,7 +129,7 @@ public abstract class OutputContainer implements GraphicalElement, OutputLayout,
public void clear() {
roots.clear();
roots.add(new BlockContainer(null));
roots.add(new BlockContainer());
recomputeDimensions();
}

View File

@ -63,6 +63,24 @@ public class MathInputScreen extends Screen {
canBeInHistory = true;
}
/**
* Create a copy of this element
* @param mathInputScreen
*/
private MathInputScreen(MathInputScreen old) {
this.calc = new MathContext(old.calc);
this.canBeInHistory = old.canBeInHistory;
this.created = old.created;
this.currentStep = old.currentStep;
this.d = old.d;
this.errorLevel = old.errorLevel;
this.ic = new InputContext(old.ic);
this.initialized = old.initialized;
this.mustRefresh = old.mustRefresh;
this.result = new NormalOutputContainer(old.result);
this.userInput = new NormalInputContainer(old.userInput, this.ic);
}
@Override
public void created() throws InterruptedException {
ic = new InputContext();
@ -471,19 +489,7 @@ public class MathInputScreen extends Screen {
@SuppressWarnings("unchecked")
private void swapInputScreen() {
MathInputScreen mis = new MathInputScreen();
mis.calc = SerializationUtils.clone(calc);
mis.canBeInHistory = true;
mis.currentStep = currentStep;
mis.created = created;
mis.d = d;
mis.errorLevel = errorLevel;
mis.ic = SerializationUtils.clone(ic);
mis.initialized = initialized;
mis.mustRefresh = true;
mis.result = SerializationUtils.clone(result);
mis.userInput = SerializationUtils.clone(userInput);
mis.d = d;
MathInputScreen mis = new MathInputScreen(this);
Engine.INSTANCE.getHardwareDevice().getDisplayManager().setScreen(mis);
}

View File

@ -1,7 +1,5 @@
package it.cavallium.warppi.gui.screens;
import java.io.Serializable;
import it.cavallium.warppi.event.KeyboardEventListener;
import it.cavallium.warppi.event.TouchEventListener;
import it.cavallium.warppi.gui.DisplayManager;

View File

@ -1,7 +1,5 @@
package it.cavallium.warppi.math;
import java.io.Serializable;
public enum AngleMode implements Serializable {
public enum AngleMode {
DEG, RAD, GRA
}

View File

@ -1,13 +1,11 @@
package it.cavallium.warppi.math;
import java.io.Serializable;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public interface Function extends Serializable {
public interface Function {
/**
* Returns this function and its children in a string form.
@ -27,6 +25,14 @@ public interface Function extends Serializable {
*/
Function clone();
/**
* Deep clone this function, also change mathContext.
*
* @param mathContext new mathContext
* @return A clone of this function.
*/
Function clone(MathContext newMathContext);
/**
* Generic method to change a parameter in a known position.
*

View File

@ -1,9 +1,5 @@
package it.cavallium.warppi.math;
import java.io.Serializable;
import org.apache.commons.lang3.SerializationUtils;
import it.cavallium.warppi.math.functions.Variable.VariableValue;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.math.rules.RuleType;
@ -11,7 +7,7 @@ import it.cavallium.warppi.math.rules.RulesManager;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class MathContext implements Serializable {
public class MathContext {
public AngleMode angleMode = AngleMode.DEG;
public boolean exactMode = false;
@ -27,6 +23,24 @@ public class MathContext implements Serializable {
resultsCount = 0;
}
public MathContext(MathContext calc) {
this.f = new ObjectArrayList<>();
this.f2 = new ObjectArrayList<>();
for (Function f : calc.f) {
f = f.clone(this);
this.f.add(f);
}
for (Function f : calc.f2) {
f = f.clone(this);
this.f2.add(f);
}
this.variablesValues = new ObjectArrayList<>();
for (VariableValue varVal : calc.variablesValues) {
this.variablesValues.add(new VariableValue(varVal, this));
}
this.resultsCount = calc.resultsCount;
}
@Deprecated
public Function parseString(final String string) throws Error {
return null;

View File

@ -26,7 +26,12 @@ public class Division extends FunctionOperator {
@Override
public FunctionOperator clone() {
return new Division(getMathContext(), getParameter1(), getParameter2());
return new Division(getMathContext(), getParameter1() == null ? null : getParameter1().clone(), getParameter2() == null ? null : getParameter2().clone());
}
@Override
public FunctionOperator clone(MathContext c) {
return new Division(c, getParameter1() == null ? null : getParameter1().clone(c), getParameter2() == null ? null : getParameter2().clone(c));
}
@Override

View File

@ -35,6 +35,11 @@ public class EmptyNumber implements Function {
return new EmptyNumber(root);
}
@Override
public Function clone(MathContext c) {
return new EmptyNumber(c);
}
@Override
public Function setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();

View File

@ -610,7 +610,12 @@ public class Expression extends FunctionSingle {
@Override
public Expression clone() {
return new Expression(mathContext, parameter);
return new Expression(mathContext, parameter == null ? null : parameter.clone());
}
@Override
public Expression clone(MathContext newContext) {
return new Expression(newContext, parameter == null ? null : parameter.clone(newContext));
}
}

View File

@ -40,6 +40,11 @@ public class Joke implements Function {
return new Joke(root, joke);
}
@Override
public Function clone(MathContext c) {
return new Joke(c, joke);
}
@Override
public Function setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();

View File

@ -26,7 +26,12 @@ public class Logarithm extends FunctionOperator {
@Override
public Logarithm clone() {
return new Logarithm(mathContext, parameter1, parameter2);
return new Logarithm(mathContext, parameter1 == null ? null : parameter1.clone(), parameter2 == null ? null : parameter2.clone());
}
@Override
public Logarithm clone(MathContext c) {
return new Logarithm(c, parameter1 == null ? null : parameter1.clone(c), parameter2 == null ? null : parameter2.clone(c));
}
@Override

View File

@ -35,7 +35,12 @@ public class Multiplication extends FunctionOperator {
@Override
public Multiplication clone() {
return new Multiplication(mathContext, parameter1, parameter2);
return new Multiplication(mathContext, parameter1 == null ? null : parameter1.clone(), parameter2 == null ? null : parameter2.clone());
}
@Override
public Multiplication clone(MathContext c) {
return new Multiplication(c, parameter1 == null ? null : parameter1.clone(c), parameter2 == null ? null : parameter2.clone(c));
}
@Override
@ -66,7 +71,7 @@ public class Multiplication extends FunctionOperator {
} else {
result.addAll(sub1);
}
if (nearLeft instanceof BlockChar && nearRight instanceof BlockChar && !(par2 instanceof Negative) && !(par1 instanceof Number && par2 instanceof Number)) {
if (nearLeft instanceof BlockChar && nearRight instanceof BlockChar && !(par2 instanceof Negative) && !(par1 instanceof Number && par2 instanceof Number) && !(par1 instanceof Number && par2 instanceof Multiplication && ((Multiplication)par2).getParameter1() instanceof Number)) {
} else {
result.add(new BlockChar(MathematicalSymbols.MULTIPLICATION));

View File

@ -26,7 +26,12 @@ public class Negative extends FunctionSingle {
@Override
public Negative clone() {
return new Negative(mathContext, parameter);
return new Negative(mathContext, parameter == null ? null : parameter.clone());
}
@Override
public Negative clone(MathContext c) {
return new Negative(c, parameter == null ? null : parameter.clone(c));
}
@Override

View File

@ -49,6 +49,16 @@ public class Number implements Function {
this(root, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2));
}
/**
* Copy
* @param n
* @param newContext
*/
public Number(Number old, MathContext newContext) {
this.root = newContext;
this.term = old.term;
}
public BigDecimal getTerm() {
return term;
}
@ -117,6 +127,11 @@ public class Number implements Function {
return new Number(root, term);
}
@Override
public Number clone(MathContext c) {
return new Number(c, term);
}
@Override
public ObjectArrayList<Function> simplify(final Rule rule) throws Error, InterruptedException {
return rule.execute(this);

View File

@ -26,7 +26,12 @@ public class Power extends FunctionOperator {
@Override
public Power clone() {
return new Power(mathContext, parameter1, parameter2);
return new Power(mathContext, parameter1 == null ? null : parameter1.clone(), parameter2 == null ? null : parameter2.clone());
}
@Override
public Power clone(MathContext c) {
return new Power(c, parameter1 == null ? null : parameter1.clone(c), parameter2 == null ? null : parameter2.clone(c));
}
@Override

View File

@ -25,7 +25,12 @@ public class Root extends FunctionOperator {
@Override
public Root clone() {
return new Root(mathContext, parameter1, parameter2);
return new Root(mathContext, parameter1 == null ? null : parameter1.clone(), parameter2 == null ? null : parameter2.clone());
}
@Override
public Root clone(MathContext c) {
return new Root(c, parameter1 == null ? null : parameter1.clone(c), parameter2 == null ? null : parameter2.clone(c));
}
@Override

View File

@ -26,7 +26,12 @@ public class RootSquare extends FunctionOperator {
@Override
public RootSquare clone() {
return new RootSquare(mathContext, parameter2);
return new RootSquare(mathContext, parameter2 == null ? null : parameter2.clone());
}
@Override
public RootSquare clone(MathContext c) {
return new RootSquare(c, parameter2 == null ? null : parameter2.clone(c));
}
@Override

View File

@ -26,7 +26,12 @@ public class Subtraction extends FunctionOperator {
@Override
public Subtraction clone() {
return new Subtraction(mathContext, parameter1, parameter2);
return new Subtraction(mathContext, parameter1 == null ? null : parameter1.clone(), parameter2 == null ? null : parameter2.clone());
}
@Override
public Subtraction clone(MathContext c) {
return new Subtraction(c, parameter1 == null ? null : parameter1.clone(c), parameter2 == null ? null : parameter2.clone(c));
}
@Override

View File

@ -30,7 +30,12 @@ public class Sum extends FunctionOperator {
@Override
public Sum clone() {
return new Sum(mathContext, parameter1, parameter2);
return new Sum(mathContext, parameter1 == null ? parameter1 : parameter1.clone(), parameter2 == null ? null : parameter2.clone());
}
@Override
public Sum clone(MathContext c) {
return new Sum(c, parameter1 == null ? null : parameter1.clone(c), parameter2 == null ? null : parameter2.clone(c));
}
@Override

View File

@ -26,7 +26,12 @@ public class SumSubtraction extends FunctionOperator {
@Override
public SumSubtraction clone() {
return new SumSubtraction(mathContext, parameter1, parameter2);
return new SumSubtraction(mathContext, parameter1 == null ? null : parameter1.clone(), parameter2 == null ? null : parameter2.clone());
}
@Override
public SumSubtraction clone(MathContext c) {
return new SumSubtraction(c, parameter1 == null ? null : parameter1.clone(c), parameter2 == null ? null : parameter2.clone(c));
}
@Override

View File

@ -36,6 +36,11 @@ public class Undefined implements Function {
return new Undefined(root);
}
@Override
public Undefined clone(MathContext c) {
return new Undefined(c);
}
@Override
public Function setParameter(final int index, final Function var) throws IndexOutOfBoundsException {
throw new IndexOutOfBoundsException();

View File

@ -1,7 +1,5 @@
package it.cavallium.warppi.math.functions;
import java.io.Serializable;
import it.cavallium.warppi.gui.expression.blocks.Block;
import it.cavallium.warppi.gui.expression.blocks.BlockChar;
import it.cavallium.warppi.math.Function;
@ -26,6 +24,17 @@ public class Variable implements Function {
this(root, s.charAt(0), type);
}
/**
* Copy
* @param old
* @param root
*/
public Variable(Variable old, MathContext root) {
this.root = root;
this.type = old.type;
this.var = old.var;
}
public char getChar() {
return var;
}
@ -47,8 +56,7 @@ public class Variable implements Function {
return "" + getChar();
}
public static class VariableValue implements Serializable {
private static final long serialVersionUID = -5656281021874324571L;
public static class VariableValue {
public final Variable v;
public final Number n;
@ -56,6 +64,16 @@ public class Variable implements Function {
this.v = v;
this.n = n;
}
/**
* Copy
* @param old
* @param newContext
*/
public VariableValue(VariableValue old, MathContext newContext) {
this.v = new Variable(old.v, newContext);
this.n = new Number(old.n, newContext);
}
}
@Override
@ -86,6 +104,11 @@ public class Variable implements Function {
return new Variable(root, var, type);
}
@Override
public Variable clone(MathContext c) {
return new Variable(c, var, type);
}
public static enum V_TYPE {
CONSTANT, VARIABLE, SOLUTION
}

View File

@ -48,7 +48,12 @@ public class Equation extends FunctionOperator {
@Override
public Equation clone() {
return new Equation(mathContext, parameter1, parameter2);
return new Equation(mathContext, parameter1 == null ? null : parameter1.clone(), parameter2 == null ? null : parameter2.clone());
}
@Override
public Equation clone(MathContext c) {
return new Equation(c, parameter1 == null ? null : parameter1.clone(c), parameter2 == null ? null : parameter2.clone(c));
}
@Override

View File

@ -24,7 +24,20 @@ public class EquationsSystem extends FunctionDynamic {
@Override
public EquationsSystem clone() {
return new EquationsSystem(root, functions);
Function[] newFuncs = functions.clone();
for (int i = 0; i < newFuncs.length; i++) {
newFuncs[i] = newFuncs[i].clone();
}
return new EquationsSystem(root, newFuncs);
}
@Override
public EquationsSystem clone(MathContext c) {
Function[] newFuncs = new Function[this.functions.length];
for (int i = 0; i < newFuncs.length; i++) {
newFuncs[i] = this.functions[i] == null ? null : this.functions[i].clone(c);
}
return new EquationsSystem(c, newFuncs);
}
@Override

View File

@ -20,7 +20,12 @@ public class EquationsSystemPart extends FunctionSingle {
@Override
public EquationsSystemPart clone() {
return new EquationsSystemPart(mathContext, (Equation) parameter);
return new EquationsSystemPart(mathContext, (Equation) (parameter == null ? null : parameter.clone()));
}
@Override
public EquationsSystemPart clone(MathContext c) {
return new EquationsSystemPart(c, (Equation) (parameter == null ? null : parameter.clone(c)));
}
@Override

View File

@ -26,6 +26,12 @@ public class ArcCosine extends FunctionSingle {
return null;
}
@Override
public FunctionSingle clone(MathContext c) {
// TODO Auto-generated method stub
return null;
}
@Override
public ObjectArrayList<Block> toBlock(final MathContext context) throws Error {
// TODO Auto-generated method stub

View File

@ -26,6 +26,12 @@ public class ArcSine extends FunctionSingle {
return null;
}
@Override
public FunctionSingle clone(MathContext c) {
// TODO Auto-generated method stub
return null;
}
@Override
public ObjectArrayList<Block> toBlock(final MathContext context) throws Error {
// TODO Auto-generated method stub

View File

@ -26,6 +26,12 @@ public class ArcTangent extends FunctionSingle {
return null;
}
@Override
public FunctionSingle clone(MathContext c) {
// TODO Auto-generated method stub
return null;
}
@Override
public ObjectArrayList<Block> toBlock(final MathContext context) throws Error {
// TODO Auto-generated method stub

View File

@ -26,6 +26,12 @@ public class Cosine extends FunctionSingle {
return null;
}
@Override
public FunctionSingle clone(MathContext c) {
// TODO Auto-generated method stub
return null;
}
@Override
public ObjectArrayList<Block> toBlock(final MathContext context) throws Error {
// TODO Auto-generated method stub

View File

@ -28,7 +28,12 @@ public class Sine extends FunctionSingle {
@Override
public Sine clone() {
return new Sine(mathContext, parameter);
return new Sine(mathContext, parameter == null ? null : parameter.clone());
}
@Override
public Sine clone(MathContext c) {
return new Sine(c, parameter == null ? null : parameter.clone(c));
}
@Override

View File

@ -25,6 +25,12 @@ public class Tangent extends FunctionSingle {
return null;
}
@Override
public FunctionSingle clone(MathContext c) {
// TODO Auto-generated method stub
return null;
}
@Override
public ObjectArrayList<Block> toBlock(final MathContext context) throws Error {
// TODO Auto-generated method stub