2017-01-22 13:46:38 +01:00
|
|
|
package org.warp.picalculator.gui.screens;
|
2016-09-02 20:32:37 +02:00
|
|
|
|
2017-03-26 22:44:09 +02:00
|
|
|
import java.io.IOException;
|
2016-09-02 20:32:37 +02:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
2017-03-03 23:06:43 +01:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
2017-04-10 22:50:43 +02:00
|
|
|
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
|
|
|
|
2017-01-16 17:57:09 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
2016-09-02 20:32:37 +02:00
|
|
|
|
2016-09-12 16:30:51 +02:00
|
|
|
import org.warp.picalculator.Error;
|
|
|
|
import org.warp.picalculator.Errors;
|
2016-09-02 20:32:37 +02:00
|
|
|
import org.warp.picalculator.Utils;
|
2016-11-25 22:40:43 +01:00
|
|
|
import org.warp.picalculator.device.Keyboard;
|
2016-09-19 18:48:27 +02:00
|
|
|
import org.warp.picalculator.device.Keyboard.Key;
|
2017-01-31 22:29:49 +01:00
|
|
|
import org.warp.picalculator.gui.DisplayManager;
|
2017-04-10 22:50:43 +02:00
|
|
|
import org.warp.picalculator.gui.expression.Block;
|
2017-03-26 22:44:09 +02:00
|
|
|
import org.warp.picalculator.gui.expression.BlockContainer;
|
|
|
|
import org.warp.picalculator.gui.expression.containers.InputContainer;
|
|
|
|
import org.warp.picalculator.gui.expression.containers.NormalInputContainer;
|
|
|
|
import org.warp.picalculator.gui.expression.containers.NormalOutputContainer;
|
|
|
|
import org.warp.picalculator.gui.expression.containers.OutputContainer;
|
2017-02-02 10:30:45 +01:00
|
|
|
import org.warp.picalculator.gui.graphicengine.BinaryFont;
|
2017-01-31 22:29:49 +01:00
|
|
|
import org.warp.picalculator.gui.graphicengine.Renderer;
|
2017-01-16 17:57:09 +01:00
|
|
|
import org.warp.picalculator.math.AngleMode;
|
2017-02-14 20:02:03 +01:00
|
|
|
import org.warp.picalculator.math.MathContext;
|
|
|
|
import org.warp.picalculator.math.Function;
|
|
|
|
import org.warp.picalculator.math.FunctionDynamic;
|
|
|
|
import org.warp.picalculator.math.FunctionSingle;
|
|
|
|
import org.warp.picalculator.math.FunctionOperator;
|
2016-11-25 22:40:43 +01:00
|
|
|
import org.warp.picalculator.math.MathematicalSymbols;
|
2017-01-16 17:57:09 +01:00
|
|
|
import org.warp.picalculator.math.functions.Variable;
|
|
|
|
import org.warp.picalculator.math.functions.Variable.VariableValue;
|
|
|
|
import org.warp.picalculator.math.functions.equations.Equation;
|
2017-04-10 22:50:43 +02:00
|
|
|
import org.warp.picalculator.math.parser.MathParser;
|
|
|
|
import org.warp.picalculator.math.functions.Expression;
|
2017-01-16 17:57:09 +01:00
|
|
|
import org.warp.picalculator.math.functions.Number;
|
2016-11-02 21:56:40 +01:00
|
|
|
|
2016-12-04 22:22:16 +01:00
|
|
|
public class MathInputScreen extends Screen {
|
2016-09-02 20:32:37 +02:00
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
public MathContext calc;
|
2017-03-26 22:44:09 +02:00
|
|
|
public InputContainer userInput;
|
|
|
|
public OutputContainer result;
|
2016-09-02 20:32:37 +02:00
|
|
|
public int errorLevel = 0; // 0 = nessuno, 1 = risultato, 2 = tutto
|
|
|
|
boolean mustRefresh = true;
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-12-04 22:22:16 +01:00
|
|
|
public MathInputScreen() {
|
2016-09-02 20:32:37 +02:00
|
|
|
super();
|
|
|
|
canBeInHistory = true;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-09-02 20:32:37 +02:00
|
|
|
@Override
|
|
|
|
public void created() throws InterruptedException {
|
2017-03-26 22:44:09 +02:00
|
|
|
calc = new MathContext();
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-03-26 22:44:09 +02:00
|
|
|
try {
|
|
|
|
BlockContainer.initializeFonts(DisplayManager.engine.loadFont("ex"), DisplayManager.engine.loadFont("big"));
|
2017-04-10 22:50:43 +02:00
|
|
|
} catch (final IOException e) {
|
2017-03-26 22:44:09 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(1);
|
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-03-26 22:44:09 +02:00
|
|
|
userInput = new NormalInputContainer();
|
|
|
|
result = new NormalOutputContainer();
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-03-26 22:44:09 +02:00
|
|
|
calc.init();
|
2016-09-02 20:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init() throws InterruptedException {
|
2016-10-02 16:01:41 +02:00
|
|
|
/* Fine caricamento */
|
2016-09-02 20:32:37 +02:00
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2016-09-02 20:32:37 +02:00
|
|
|
@Override
|
|
|
|
public void beforeRender(float dt) {
|
2016-11-25 22:40:43 +01:00
|
|
|
|
2017-01-31 22:29:49 +01:00
|
|
|
if (DisplayManager.error == null) {
|
|
|
|
DisplayManager.renderer.glClearColor(0xFFc5c2af);
|
2016-11-25 22:40:43 +01:00
|
|
|
} else {
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.renderer.glClearColor(0xFFDC3C32);
|
2016-11-25 22:40:43 +01:00
|
|
|
}
|
2017-03-26 22:44:09 +02:00
|
|
|
if (userInput.beforeRender(dt)) {
|
|
|
|
mustRefresh = true;
|
|
|
|
}
|
2016-09-02 20:32:37 +02:00
|
|
|
}
|
|
|
|
|
2017-02-02 10:30:45 +01:00
|
|
|
private static final BinaryFont fontBig = Utils.getFont(false);
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-01-22 13:46:38 +01:00
|
|
|
@Override
|
|
|
|
public void renderStatusbar() {
|
2017-04-10 22:50:43 +02:00
|
|
|
final Renderer renderer = DisplayManager.renderer;
|
2017-01-31 22:29:49 +01:00
|
|
|
renderer.glColor3f(1, 1, 1);
|
|
|
|
final int pos = 2;
|
|
|
|
final int spacersNumb = 1;
|
2017-01-22 13:46:38 +01:00
|
|
|
int skinN = 0;
|
|
|
|
if (calc.exactMode) {
|
|
|
|
skinN = 22;
|
|
|
|
} else {
|
|
|
|
skinN = 21;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
renderer.glFillRect(2 + 18 * pos + 2 * spacersNumb, 2, 16, 16, 16 * skinN, 16 * 0, 16, 16);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-09-02 20:32:37 +02:00
|
|
|
@Override
|
|
|
|
public void render() {
|
2017-02-02 10:30:45 +01:00
|
|
|
fontBig.use(DisplayManager.engine);
|
2016-11-25 22:40:43 +01:00
|
|
|
final int textColor = 0xFF000000;
|
|
|
|
final int padding = 4;
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.renderer.glColor(textColor);
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-03-26 22:44:09 +02:00
|
|
|
userInput.draw(DisplayManager.engine, DisplayManager.renderer, padding, padding + 20);
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-03-26 22:44:09 +02:00
|
|
|
if (!result.root.getContent().isEmpty()) {
|
2017-04-10 22:50:43 +02:00
|
|
|
result.draw(DisplayManager.engine, DisplayManager.renderer, DisplayManager.engine.getWidth() - result.getWidth() - 2, DisplayManager.engine.getHeight() - result.getHeight() - 2);
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
2016-09-02 20:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean mustBeRefreshed() {
|
|
|
|
if (mustRefresh) {
|
|
|
|
mustRefresh = false;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean keyPressed(Key k) {
|
2017-04-10 22:50:43 +02:00
|
|
|
Utils.debug.println(k.toString());
|
2016-09-02 20:32:37 +02:00
|
|
|
switch (k) {
|
2017-02-06 21:38:17 +01:00
|
|
|
case STEP:
|
2017-03-26 22:44:09 +02:00
|
|
|
// if (newExpression.length() > 0) {
|
|
|
|
// if (firstStep) {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// interpreta(true);
|
|
|
|
// showVariablesDialog(() -> {
|
|
|
|
// currentExpression = newExpression;
|
|
|
|
// calc.f2 = calc.f;
|
|
|
|
// firstStep = false;
|
|
|
|
// step();
|
|
|
|
// });
|
|
|
|
// } catch (final Exception ex) {
|
|
|
|
// if (Utils.debugOn) {
|
|
|
|
// ex.printStackTrace();
|
|
|
|
// }
|
|
|
|
// throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
// }
|
|
|
|
// } catch (final Error e) {
|
|
|
|
// final StringWriter sw = new StringWriter();
|
|
|
|
// final PrintWriter pw = new PrintWriter(sw);
|
|
|
|
// e.printStackTrace(pw);
|
|
|
|
// d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
|
|
|
|
// DisplayManager.error = e.id.toString();
|
|
|
|
// System.err.println(e.id);
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// step();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return true;
|
2017-02-06 21:38:17 +01:00
|
|
|
case SIMPLIFY:
|
2017-04-10 22:50:43 +02:00
|
|
|
if (DisplayManager.error != null) {
|
|
|
|
//TODO: make the error management a global API rather than being relegated to this screen.
|
|
|
|
Utils.debug.println("Resetting after error...");
|
|
|
|
DisplayManager.error = null;
|
|
|
|
calc.f = null;
|
|
|
|
calc.f2 = null;
|
|
|
|
calc.resultsCount = 0;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) {
|
|
|
|
Expression expr = MathParser.parseInput(calc, userInput);
|
|
|
|
if (calc.f == null | calc.f2 == null) {
|
|
|
|
calc.f = new ObjectArrayList<>();
|
|
|
|
calc.f2 = new ObjectArrayList<>();
|
|
|
|
} else {
|
|
|
|
calc.f.clear();
|
|
|
|
calc.f2.clear();
|
|
|
|
}
|
|
|
|
calc.f.add(expr);
|
|
|
|
ObjectArrayList<Function> resultExpression = expr.solve();
|
|
|
|
ObjectArrayList<Block> resultBlocks = MathParser.parseOutput(calc, resultExpression);
|
|
|
|
result.setContent(resultBlocks);
|
|
|
|
// showVariablesDialog(() -> {
|
|
|
|
// currentExpression = newExpression;
|
|
|
|
// simplify();
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
} catch (final Exception ex) {
|
|
|
|
if (Utils.debugOn) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
|
|
|
} catch (final Error e) {
|
|
|
|
final StringWriter sw = new StringWriter();
|
|
|
|
final PrintWriter pw = new PrintWriter(sw);
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
|
|
|
|
DisplayManager.error = e.id.toString();
|
|
|
|
System.err.println(e.id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-02 20:32:37 +02:00
|
|
|
case NUM0:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('0');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM1:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('1');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM2:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('2');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM3:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('3');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM4:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('4');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM5:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('5');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM6:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('6');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM7:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('7');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM8:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('8');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case NUM9:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('9');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case PLUS:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('+');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case MINUS:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('-');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
2016-09-18 14:33:25 +02:00
|
|
|
case PLUS_MINUS:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('±');
|
2016-09-18 14:33:25 +02:00
|
|
|
return true;
|
2016-09-02 20:32:37 +02:00
|
|
|
case MULTIPLY:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('*');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case DIVIDE:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('/');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case PARENTHESIS_OPEN:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('(');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case PARENTHESIS_CLOSE:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar(')');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case DOT:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('.');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case EQUAL:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('=');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case SQRT:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('â’¶');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case ROOT:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar('√');
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case POWER_OF_2:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar(MathematicalSymbols.POWER_OF_TWO);
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case POWER_OF_x:
|
2016-12-04 22:22:16 +01:00
|
|
|
typeChar(MathematicalSymbols.POWER);
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
2017-01-16 17:57:09 +01:00
|
|
|
case PI:
|
|
|
|
typeChar(MathematicalSymbols.PI);
|
|
|
|
return true;
|
2016-09-18 14:33:25 +02:00
|
|
|
case LETTER_X:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar(MathematicalSymbols.variables[23]);
|
2016-09-18 14:33:25 +02:00
|
|
|
return true;
|
|
|
|
case LETTER_Y:
|
2017-03-26 22:44:09 +02:00
|
|
|
typeChar(MathematicalSymbols.variables[24]);
|
2016-09-18 14:33:25 +02:00
|
|
|
return true;
|
2016-12-04 22:22:16 +01:00
|
|
|
case SINE:
|
|
|
|
typeChar(MathematicalSymbols.SINE);
|
|
|
|
return true;
|
|
|
|
case COSINE:
|
|
|
|
typeChar(MathematicalSymbols.COSINE);
|
|
|
|
return true;
|
|
|
|
case TANGENT:
|
|
|
|
typeChar(MathematicalSymbols.TANGENT);
|
|
|
|
return true;
|
|
|
|
case ARCSINE:
|
|
|
|
typeChar(MathematicalSymbols.ARC_SINE);
|
|
|
|
return true;
|
|
|
|
case ARCCOSINE:
|
|
|
|
typeChar(MathematicalSymbols.ARC_COSINE);
|
|
|
|
return true;
|
|
|
|
case ARCTANGENT:
|
|
|
|
typeChar(MathematicalSymbols.ARC_TANGENT);
|
|
|
|
return true;
|
2016-09-02 20:32:37 +02:00
|
|
|
case DELETE:
|
2017-03-26 22:44:09 +02:00
|
|
|
userInput.del();
|
|
|
|
mustRefresh = true;
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case LEFT:
|
2017-03-26 22:44:09 +02:00
|
|
|
userInput.moveLeft();
|
|
|
|
mustRefresh = true;
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case RIGHT:
|
2017-03-26 22:44:09 +02:00
|
|
|
userInput.moveRight();
|
|
|
|
mustRefresh = true;
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
|
|
|
case RESET:
|
2017-03-26 22:44:09 +02:00
|
|
|
userInput.clear();
|
|
|
|
result.clear();
|
2017-01-31 22:29:49 +01:00
|
|
|
if (DisplayManager.error != null) {
|
2016-09-02 20:32:37 +02:00
|
|
|
Utils.debug.println("Resetting after error...");
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.error = null;
|
2016-09-02 20:32:37 +02:00
|
|
|
}
|
2017-03-26 22:44:09 +02:00
|
|
|
return true;
|
2017-01-31 22:29:49 +01:00
|
|
|
case SURD_MODE:
|
2017-04-10 22:50:43 +02:00
|
|
|
calc.exactMode = !calc.exactMode;
|
|
|
|
if (calc.exactMode == false) {
|
|
|
|
calc.f2 = solveExpression(calc.f2);
|
|
|
|
} else {
|
|
|
|
result.clear();
|
|
|
|
Keyboard.keyPressed(Key.SIMPLIFY);
|
|
|
|
}
|
2016-11-25 22:40:43 +01:00
|
|
|
return true;
|
2016-09-02 20:32:37 +02:00
|
|
|
case debug1:
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.INSTANCE.setScreen(new EmptyScreen());
|
2016-09-02 20:32:37 +02:00
|
|
|
return true;
|
2016-11-25 22:40:43 +01:00
|
|
|
case HISTORY_BACK:
|
2017-03-26 22:44:09 +02:00
|
|
|
// if (DisplayManager.INSTANCE.canGoBack()) {
|
|
|
|
// if (currentExpression != null && currentExpression.length() > 0 & DisplayManager.sessions[DisplayManager.currentSession + 1] instanceof MathInputScreen) {
|
|
|
|
// newExpression = currentExpression;
|
|
|
|
// try {
|
|
|
|
// interpreta(true);
|
|
|
|
// } catch (final Error e) {}
|
|
|
|
// }
|
|
|
|
// }
|
2016-11-25 22:40:43 +01:00
|
|
|
return false;
|
|
|
|
case HISTORY_FORWARD:
|
2017-03-26 22:44:09 +02:00
|
|
|
// if (DisplayManager.INSTANCE.canGoForward()) {
|
|
|
|
// if (currentExpression != null && currentExpression.length() > 0 & DisplayManager.sessions[DisplayManager.currentSession - 1] instanceof MathInputScreen) {
|
|
|
|
// newExpression = currentExpression;
|
|
|
|
// try {
|
|
|
|
// interpreta(true);
|
|
|
|
// } catch (final Error e) {}
|
|
|
|
// }
|
|
|
|
// }
|
2016-11-25 22:40:43 +01:00
|
|
|
return false;
|
2017-01-16 17:57:09 +01:00
|
|
|
case debug_DEG:
|
|
|
|
if (calc.angleMode.equals(AngleMode.DEG) == false) {
|
|
|
|
calc.angleMode = AngleMode.DEG;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case debug_RAD:
|
|
|
|
if (calc.angleMode.equals(AngleMode.RAD) == false) {
|
|
|
|
calc.angleMode = AngleMode.RAD;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case debug_GRA:
|
|
|
|
if (calc.angleMode.equals(AngleMode.GRA) == false) {
|
|
|
|
calc.angleMode = AngleMode.GRA;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case DRG_CYCLE:
|
|
|
|
if (calc.angleMode.equals(AngleMode.DEG) == true) {
|
|
|
|
calc.angleMode = AngleMode.RAD;
|
|
|
|
} else if (calc.angleMode.equals(AngleMode.RAD) == true) {
|
|
|
|
calc.angleMode = AngleMode.GRA;
|
|
|
|
} else {
|
|
|
|
calc.angleMode = AngleMode.DEG;
|
|
|
|
}
|
|
|
|
return true;
|
2016-09-02 20:32:37 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
private ObjectArrayList<Function> solveExpression(ObjectArrayList<Function> f22) {
|
2017-01-16 17:57:09 +01:00
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return calc.solveExpression(f22);
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final Exception ex) {
|
2017-01-16 17:57:09 +01:00
|
|
|
if (Utils.debugOn) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final Error e) {
|
|
|
|
final StringWriter sw = new StringWriter();
|
|
|
|
final PrintWriter pw = new PrintWriter(sw);
|
2017-01-16 17:57:09 +01:00
|
|
|
e.printStackTrace(pw);
|
|
|
|
d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.error = e.id.toString();
|
2017-01-16 17:57:09 +01:00
|
|
|
System.err.println(e.id);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-06 21:38:17 +01:00
|
|
|
protected void step() {
|
2017-01-16 17:57:09 +01:00
|
|
|
try {
|
|
|
|
try {
|
|
|
|
showVariablesDialog();
|
2017-03-03 23:06:43 +01:00
|
|
|
ObjectArrayList<Function> results = new ObjectArrayList<>();
|
|
|
|
final ObjectArrayList<Function> partialResults = new ObjectArrayList<>();
|
2017-01-31 22:29:49 +01:00
|
|
|
for (final Function f : calc.f2) {
|
2017-01-16 17:57:09 +01:00
|
|
|
if (f instanceof Equation) {
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.INSTANCE.setScreen(new SolveEquationScreen(this));
|
2017-01-16 17:57:09 +01:00
|
|
|
} else {
|
|
|
|
results.add(f);
|
2017-01-31 22:29:49 +01:00
|
|
|
for (final Function itm : results) {
|
2017-02-14 20:02:03 +01:00
|
|
|
if (itm.isSimplified() == false) {
|
|
|
|
final List<Function> dt = itm.simplify();
|
2017-01-16 17:57:09 +01:00
|
|
|
partialResults.addAll(dt);
|
|
|
|
} else {
|
|
|
|
partialResults.add(itm);
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 23:06:43 +01:00
|
|
|
results = new ObjectArrayList<>(partialResults);
|
2017-01-16 17:57:09 +01:00
|
|
|
partialResults.clear();
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-01-16 17:57:09 +01:00
|
|
|
if (results.size() == 0) {
|
2017-01-17 22:32:40 +01:00
|
|
|
calc.resultsCount = 0;
|
2017-01-16 17:57:09 +01:00
|
|
|
} else {
|
2017-01-17 22:32:40 +01:00
|
|
|
calc.resultsCount = results.size();
|
2017-01-16 17:57:09 +01:00
|
|
|
Collections.reverse(results);
|
|
|
|
// add elements to al, including duplicates
|
2017-01-31 22:29:49 +01:00
|
|
|
final Set<Function> hs = new LinkedHashSet<>();
|
2017-01-16 17:57:09 +01:00
|
|
|
hs.addAll(results);
|
|
|
|
results.clear();
|
|
|
|
results.addAll(hs);
|
2017-01-17 22:32:40 +01:00
|
|
|
calc.f2 = results;
|
2017-01-16 17:57:09 +01:00
|
|
|
}
|
2017-02-02 12:49:31 +01:00
|
|
|
Utils.debug.println(calc.f2.toString());
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final Exception ex) {
|
2017-01-16 17:57:09 +01:00
|
|
|
if (Utils.debugOn) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final Error e) {
|
|
|
|
final StringWriter sw = new StringWriter();
|
|
|
|
final PrintWriter pw = new PrintWriter(sw);
|
2017-01-16 17:57:09 +01:00
|
|
|
e.printStackTrace(pw);
|
|
|
|
d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.error = e.id.toString();
|
2017-01-16 17:57:09 +01:00
|
|
|
System.err.println(e.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 21:38:17 +01:00
|
|
|
protected void simplify() {
|
2017-01-16 17:57:09 +01:00
|
|
|
try {
|
|
|
|
try {
|
2017-01-31 22:29:49 +01:00
|
|
|
for (final Function f : calc.f) {
|
2017-01-16 17:57:09 +01:00
|
|
|
if (f instanceof Equation) {
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.INSTANCE.setScreen(new SolveEquationScreen(this));
|
2017-01-16 17:57:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
final ObjectArrayList<Function> results = solveExpression(calc.f);
|
2017-01-16 17:57:09 +01:00
|
|
|
if (results.size() == 0) {
|
2017-01-17 22:32:40 +01:00
|
|
|
calc.resultsCount = 0;
|
2017-01-16 17:57:09 +01:00
|
|
|
} else {
|
2017-01-17 22:32:40 +01:00
|
|
|
calc.resultsCount = results.size();
|
2017-01-16 17:57:09 +01:00
|
|
|
Collections.reverse(results);
|
|
|
|
// add elements to al, including duplicates
|
2017-01-31 22:29:49 +01:00
|
|
|
final Set<Function> hs = new LinkedHashSet<>();
|
2017-01-16 17:57:09 +01:00
|
|
|
hs.addAll(results);
|
|
|
|
results.clear();
|
|
|
|
results.addAll(hs);
|
2017-01-17 22:32:40 +01:00
|
|
|
calc.f2 = results;
|
2017-01-16 17:57:09 +01:00
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final Exception ex) {
|
2017-01-16 17:57:09 +01:00
|
|
|
if (Utils.debugOn) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final Error e) {
|
|
|
|
final StringWriter sw = new StringWriter();
|
|
|
|
final PrintWriter pw = new PrintWriter(sw);
|
2017-01-16 17:57:09 +01:00
|
|
|
e.printStackTrace(pw);
|
|
|
|
d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.error = e.id.toString();
|
2017-01-16 17:57:09 +01:00
|
|
|
System.err.println(e.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 22:50:43 +02:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
@Deprecated
|
2016-11-02 21:56:40 +01:00
|
|
|
private void changeEquationScreen() {
|
2017-04-10 22:50:43 +02:00
|
|
|
throw new NotImplementedException();
|
|
|
|
//
|
|
|
|
// if (!userInput.isEmpty()) {
|
|
|
|
// final MathInputScreen cloned = clone();
|
|
|
|
// cloned.userInput.setCaretPosition(cloned.userInput.getCaretMaxPosition()-1);
|
|
|
|
// DisplayManager.INSTANCE.replaceScreen(cloned);
|
|
|
|
// initialized = false;
|
|
|
|
// DisplayManager.INSTANCE.setScreen(this);
|
|
|
|
//
|
|
|
|
// }
|
2016-11-02 21:56:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-26 22:44:09 +02:00
|
|
|
public void typeChar(char chr) {
|
|
|
|
userInput.typeChar(chr);
|
|
|
|
mustRefresh = true;
|
2016-09-02 20:32:37 +02:00
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2016-09-02 20:32:37 +02:00
|
|
|
@Override
|
|
|
|
public boolean keyReleased(Key k) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-01-17 22:32:40 +01:00
|
|
|
public void showVariablesDialog() {
|
|
|
|
showVariablesDialog(null);
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-01-16 17:57:09 +01:00
|
|
|
public void showVariablesDialog(final Runnable runnable) {
|
2017-01-31 22:29:49 +01:00
|
|
|
final Thread ct = new Thread(() -> {
|
2017-03-03 23:06:43 +01:00
|
|
|
final ObjectArrayList<Function> knownVarsInFunctions = getKnownVariables(calc.f.toArray(new Function[calc.f.size()]));
|
2017-01-31 22:29:49 +01:00
|
|
|
for (final VariableValue f : calc.variablesValues) {
|
2017-02-06 21:46:43 +01:00
|
|
|
if (knownVarsInFunctions.contains(f.v)) {
|
|
|
|
knownVarsInFunctions.remove(f.v);
|
2017-01-16 17:57:09 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-01-16 17:57:09 +01:00
|
|
|
boolean cancelled = false;
|
2017-02-06 21:46:43 +01:00
|
|
|
for (final Function f : knownVarsInFunctions) {
|
2017-01-31 22:29:49 +01:00
|
|
|
final ChooseVariableValueScreen cvs = new ChooseVariableValueScreen(this, new VariableValue((Variable) f, new Number(calc, 0)));
|
|
|
|
DisplayManager.INSTANCE.setScreen(cvs);
|
2017-01-16 17:57:09 +01:00
|
|
|
try {
|
2017-01-31 22:29:49 +01:00
|
|
|
while (DisplayManager.screen == cvs) {
|
2017-01-16 17:57:09 +01:00
|
|
|
Utils.debug.println(Thread.currentThread().getName());
|
|
|
|
Thread.sleep(200);
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final InterruptedException e) {}
|
2017-01-16 17:57:09 +01:00
|
|
|
if (cvs.resultNumberValue == null) {
|
|
|
|
cancelled = true;
|
|
|
|
break;
|
|
|
|
} else {
|
2017-01-17 22:32:40 +01:00
|
|
|
final int is = calc.variablesValues.size();
|
2017-01-16 17:57:09 +01:00
|
|
|
for (int i = 0; i < is; i++) {
|
2017-01-17 22:32:40 +01:00
|
|
|
if (calc.variablesValues.get(i).v == f) {
|
|
|
|
calc.variablesValues.remove(i);
|
2017-01-16 17:57:09 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-17 22:32:40 +01:00
|
|
|
calc.variablesValues.add(new VariableValue((Variable) f, (Number) cvs.resultNumberValue));
|
2017-01-16 17:57:09 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
if (!cancelled) {
|
2017-01-17 22:32:40 +01:00
|
|
|
if (runnable != null) {
|
|
|
|
runnable.run();
|
|
|
|
}
|
2017-01-16 17:57:09 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
ct.setName("Variables user-input queue thread");
|
|
|
|
ct.setPriority(Thread.MIN_PRIORITY);
|
|
|
|
ct.setDaemon(true);
|
|
|
|
ct.start();
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-03-03 23:06:43 +01:00
|
|
|
private ObjectArrayList<Function> getKnownVariables(Function[] fncs) {
|
|
|
|
final ObjectArrayList<Function> res = new ObjectArrayList<>();
|
2017-01-31 22:29:49 +01:00
|
|
|
for (final Function f : fncs) {
|
2017-02-14 20:02:03 +01:00
|
|
|
if (f instanceof FunctionOperator) {
|
|
|
|
res.addAll(getKnownVariables(new Function[] { ((FunctionOperator) f).getParameter1(), ((FunctionOperator) f).getParameter2() }));
|
|
|
|
} else if (f instanceof FunctionDynamic) {
|
|
|
|
res.addAll(getKnownVariables(((FunctionDynamic) f).getParameters()));
|
|
|
|
} else if (f instanceof FunctionSingle) {
|
|
|
|
res.addAll(getKnownVariables(new Function[] { ((FunctionSingle) f).getParameter() }));
|
2017-01-16 17:57:09 +01:00
|
|
|
} else if (f instanceof Variable) {
|
2017-04-10 22:50:43 +02:00
|
|
|
if (((Variable) f).getType() == Variable.V_TYPE.KNOWN) {
|
2017-02-06 21:46:43 +01:00
|
|
|
if (!res.contains(f)) {
|
|
|
|
res.add(f);
|
|
|
|
}
|
2017-01-16 17:57:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-11-02 21:56:40 +01:00
|
|
|
@Override
|
2017-04-10 22:50:43 +02:00
|
|
|
@Deprecated
|
2016-12-04 22:22:16 +01:00
|
|
|
public MathInputScreen clone() {
|
2017-04-10 22:50:43 +02:00
|
|
|
throw new NotImplementedException();
|
|
|
|
// final MathInputScreen es = this;
|
|
|
|
// final MathInputScreen es2 = new MathInputScreen();
|
|
|
|
// es2.errorLevel = es.errorLevel;
|
|
|
|
// es2.mustRefresh = es.mustRefresh;
|
|
|
|
// es2.calc = Utils.cloner.deepClone(es.calc);
|
|
|
|
// es2.userInput = Utils.cloner.deepClone(es.userInput);
|
|
|
|
// es2.result = Utils.cloner.deepClone(es.result);
|
|
|
|
// return es2;
|
2016-11-02 21:56:40 +01:00
|
|
|
}
|
2016-09-02 20:32:37 +02:00
|
|
|
|
|
|
|
}
|