diff --git a/res/font_big.rft b/res/font_big.rft index 8626782e..3a2fa102 100644 Binary files a/res/font_big.rft and b/res/font_big.rft differ diff --git a/res/font_big_2x.rft b/res/font_big_2x.rft new file mode 100644 index 00000000..f128024e Binary files /dev/null and b/res/font_big_2x.rft differ diff --git a/res/font_small.rft b/res/font_small.rft index 81483c9b..328f7dfb 100644 Binary files a/res/font_small.rft and b/res/font_small.rft differ diff --git a/res/font_small_2x.rft b/res/font_small_2x.rft new file mode 100644 index 00000000..5b449bae Binary files /dev/null and b/res/font_small_2x.rft differ diff --git a/src/org/warp/picalculator/Main.java b/src/org/warp/picalculator/Main.java index 45cfc8dc..ba169d3d 100644 --- a/src/org/warp/picalculator/Main.java +++ b/src/org/warp/picalculator/Main.java @@ -2,7 +2,9 @@ package org.warp.picalculator; import org.warp.picalculator.device.Keyboard; import org.warp.picalculator.device.PIDisplay; +import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.screens.EquationScreen; +import org.warp.picalculator.screens.LoadingScreen; import com.pi4j.wiringpi.Gpio; @@ -10,6 +12,7 @@ public class Main { public static int[] screenPos = new int[] { 55, 0 }; public static final int[] screenSize = new int[] { 480, 320 }; public static final int screenScale = 1; + public static final boolean zoomed = true; public static PIDisplay d; public static Main instance; @@ -17,8 +20,8 @@ public class Main { instance = this; Thread.currentThread().setPriority(Thread.MAX_PRIORITY); beforeStart(); - d = new PIDisplay(new EquationScreen()); - d.run("Calculator"); + d = new PIDisplay(new LoadingScreen()); + d.run("Raspberry PI Calculator by XDrake99 (Andrea Cavalli)"); Utils.debug.println("Shutdown..."); beforeShutdown(); Utils.debug.println(""); @@ -47,6 +50,13 @@ public class Main { } public static void main(String[] args) throws InterruptedException { + if (test()) { + return; + } new Main(); } + + private static boolean test() { + return false; + } } diff --git a/src/org/warp/picalculator/Utils.java b/src/org/warp/picalculator/Utils.java index 2868e832..21413aa1 100644 --- a/src/org/warp/picalculator/Utils.java +++ b/src/org/warp/picalculator/Utils.java @@ -15,6 +15,8 @@ import java.util.List; import org.nevec.rjm.BigDecimalMath; import org.nevec.rjm.Rational; +import org.warp.picalculator.device.PIDisplay; +import org.warp.picalculator.device.graphicengine.RAWFont; import org.warp.picalculator.math.Variable; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Division; @@ -33,7 +35,7 @@ import org.warp.picalculator.math.functions.equations.EquationsSystemPart; public class Utils { public static final int scale = 24; - public static final int resultScale = 8; + public static final int resultScale = 8*5; public static final int scaleMode = BigDecimal.ROUND_HALF_UP; public static final RoundingMode scaleMode2 = RoundingMode.HALF_UP; @@ -351,19 +353,43 @@ public class Utils { return getFontHeight(false); } - public static final int getFontHeight(boolean small) { + public static final RAWFont getFont(boolean small) { + return getFont(small, Main.zoomed); + } + + public static final RAWFont getFont(boolean small, boolean zoomed) { if (small) { - return 6; + if (zoomed) { + return PIDisplay.fonts[3]; + } else { + return PIDisplay.fonts[1]; + } } else { - return 9; + if (zoomed) { + return PIDisplay.fonts[2]; + } else { + return PIDisplay.fonts[0]; + } } } - public static int getFontHeight(Font font) { - if (font.getFontName().contains("Big")) { - return 9; + public static final int getFontHeight(boolean small) { + return getFontHeight(small, Main.zoomed); + } + + public static final int getFontHeight(boolean small, boolean zoomed) { + if (small) { + if (zoomed) { + return PIDisplay.glyphsHeight[3]; + } else { + return PIDisplay.glyphsHeight[1]; + } } else { - return 6; + if (zoomed) { + return PIDisplay.glyphsHeight[2]; + } else { + return PIDisplay.glyphsHeight[0]; + } } } @@ -398,12 +424,33 @@ public class Utils { return realbytes; } - public static boolean allSolved(List expressions) { + public static boolean allSolved(List expressions) throws Error { for (Function itm : expressions) { - if (itm.getStepsCount() > 0) { + if (itm.isSolved() == false) { return false; } } return true; } + + public static Function[][] joinFunctionsResults(List l1, List l2) { + int size1 = l1.size(); + int size2 = l2.size(); + int cur1 = 0; + int cur2 = 0; + int total = l1.size()*l2.size(); + Function[][] results = new Function[total][2]; + for (int i = 0; i < total; i++) { + results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; + if (i % size2 == 0) { + cur1+=1; + } + if (i % size1 == 0) { + cur2+=1; + } + if (cur1 >= size1) cur1 = 0; + if (cur2 >= size2) cur2 = 0; + } + return results; + } } diff --git a/src/org/warp/picalculator/device/PIDisplay.java b/src/org/warp/picalculator/device/PIDisplay.java index aacd1792..5372bd7c 100644 --- a/src/org/warp/picalculator/device/PIDisplay.java +++ b/src/org/warp/picalculator/device/PIDisplay.java @@ -33,14 +33,11 @@ public final class PIDisplay { private int[] skin; private int[] skinSize; - public static RAWFont[] fonts = new RAWFont[2]; + public static RAWFont[] fonts = new RAWFont[4]; - public static boolean loading = true; public static String error = null; public String[] errorStackTrace = null; - public final int[] glyphsHeight = new int[] { 9, 6 }; - public float loadingTextTranslation = 0.0f; - public boolean loadingTextTranslationTopToBottom = true; + public final static int[] glyphsHeight = new int[] { 9, 6, 18, 12 }; public static Screen screen; public static String displayDebugString = ""; @@ -182,6 +179,10 @@ public final class PIDisplay { fonts[0].create("big"); fonts[1] = new RAWFont(); fonts[1].create("small"); + fonts[2] = new RAWFont(); + fonts[2].create("big_2x"); + fonts[3] = new RAWFont(); + fonts[3].create("small_2x"); setFont(fonts[0]); } @@ -278,7 +279,7 @@ public final class PIDisplay { glColor3f(255, 255, 255); if (error != null) { - setFont(fonts[1]); + setFont(Utils.getFont(false, false)); glColor3f(129, 28, 22); glDrawStringRight(Main.screenSize[0] - 2, Main.screenSize[1]- this.glyphsHeight[1] - 2, "ANDREA CAVALLI'S CALCULATOR"); glColor3f(149, 32, 26); @@ -292,23 +293,6 @@ public final class PIDisplay { setFont(fonts[0]); glColor3f(129, 28, 22); glDrawStringCenter((Main.screenSize[0] / 2), 11, "UNEXPECTED EXCEPTION"); - } else if (loading) { - setFont(fonts[0]); - colore(1.0f, 1.0f, 1.0f, 1.0f); - glDrawStringCenter((Main.screenSize[0] / 2) - 1,(int) ((Main.screenSize[1]/ 2) - 25 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); - colore(1.0f, 1.0f, 1.0f, 1.0f); - glDrawStringCenter((Main.screenSize[0] / 2) + 1,(int) ((Main.screenSize[1]/ 2) - 25 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); - colore(1.0f, 1.0f, 1.0f, 1.0f); - glDrawStringCenter((Main.screenSize[0] / 2), (int) ((Main.screenSize[1]/ 2) - 25 - 1 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); - colore(1.0f, 1.0f, 1.0f, 1.0f); - glDrawStringCenter((Main.screenSize[0] / 2), (int) ((Main.screenSize[1]/ 2) - 25 + 1 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); - colore(1.0f, 0.5f, 0.0f, 1.0f); - glDrawStringCenter((Main.screenSize[0] / 2), (int) ((Main.screenSize[1]/ 2) - 25 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); - colore(0.0f, 0.0f, 0.0f, 0.75f); - glDrawStringCenter((Main.screenSize[0] / 2), (Main.screenSize[1]/ 2) + 11, "LOADING"); - setFont(fonts[1]); - colore(0.0f, 0.0f, 0.0f, 0.5f); - glDrawStringCenter((Main.screenSize[0] / 2), (Main.screenSize[1]/ 2) + 22, "PLEASE WAIT..."); } else { draw_screen(); draw_status(); @@ -340,25 +324,9 @@ public final class PIDisplay { */ checkDisplayResized(); - if (loading) { - if (loadingTextTranslation >= 10.0f) { - loadingTextTranslation = 10.0f; - loadingTextTranslationTopToBottom = false; - } else if (loadingTextTranslation <= -10.0f) { - loadingTextTranslation = -10.0f; - loadingTextTranslationTopToBottom = true; - } - - if (loadingTextTranslationTopToBottom) { - loadingTextTranslation += dt * 15; - } else { - loadingTextTranslation -= dt * 15; - } - } - screen.beforeRender(dt); - if(forced==true || screen.mustBeRefreshed() || loading) { + if(forced==true || screen.mustBeRefreshed()) { draw(); } @@ -380,7 +348,7 @@ public final class PIDisplay { private void createWindow(String title) { Display.setTitle(title); - Display.setResizable(false); + Display.setResizable(Utils.debugOn); Display.setDisplayMode(Main.screenSize[0], Main.screenSize[1]); Display.create(); } @@ -458,8 +426,8 @@ public final class PIDisplay { public float[] colore = new float[] { 0.0f, 0.0f, 0.0f, 1.0f }; - public void colore(float f1, float f2, float f3, float f4) { - colore = new float[] { f1, f2, f3, f4 }; + public static void colore(float f1, float f2, float f3, float f4) { + PIDisplay.INSTANCE.colore = new float[] { f1, f2, f3, f4 }; glColor4f((int) (f1 * 255), (int) (f2 * 255), (int) (f3 * 255), (int) (f4 * 255)); } } \ No newline at end of file diff --git a/src/org/warp/picalculator/device/PIFrame.java b/src/org/warp/picalculator/device/PIFrame.java index 7a2a07aa..54fe8260 100644 --- a/src/org/warp/picalculator/device/PIFrame.java +++ b/src/org/warp/picalculator/device/PIFrame.java @@ -1,6 +1,7 @@ package org.warp.picalculator.device; import java.awt.Cursor; +import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics; import java.awt.Point; @@ -14,6 +15,7 @@ import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; +import org.warp.picalculator.Utils; import org.warp.picalculator.device.Keyboard.Key; import org.warp.picalculator.device.graphicengine.Display; @@ -26,16 +28,18 @@ public class PIFrame extends JFrame { c = new CustomCanvas(); c.setDoubleBuffered(true); this.add(c); - this.setExtendedState(Frame.MAXIMIZED_BOTH); +// this.setExtendedState(Frame.MAXIMIZED_BOTH); Toolkit.getDefaultToolkit().setDynamicLayout(false); // Transparent 16 x 16 pixel cursor image. BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); + + if (!Utils.debugOn) { + // Create a new blank cursor. + Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor"); - // Create a new blank cursor. - Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor"); - - // Set the blank cursor to the JFrame. - getContentPane().setCursor(blankCursor); + // Set the blank cursor to the JFrame. + getContentPane().setCursor(blankCursor); + } this.addComponentListener(new ComponentListener() { @Override public void componentHidden(ComponentEvent e) { @@ -403,8 +407,24 @@ public class PIFrame extends JFrame { @Override public void setSize(int width, int height) { - super.setSize(width, height); c.setSize(width, height); + super.getContentPane().setPreferredSize(new Dimension(width, height)); + super.pack(); + } + + @Override + public Dimension getSize() { + return c.getSize(); + } + + @Override + public int getWidth() { + return c.getWidth(); + } + + @Override + public int getHeight() { + return c.getHeight(); } public static class CustomCanvas extends JPanel { diff --git a/src/org/warp/picalculator/device/graphicengine/Display.java b/src/org/warp/picalculator/device/graphicengine/Display.java index 5105edce..fdce3227 100644 --- a/src/org/warp/picalculator/device/graphicengine/Display.java +++ b/src/org/warp/picalculator/device/graphicengine/Display.java @@ -77,7 +77,7 @@ public class Display { @Deprecated() public static void refresh() { - if (PIDisplay.screen == null || PIDisplay.loading || (PIDisplay.error != null && PIDisplay.error.length() > 0) || PIDisplay.screen == null || PIDisplay.screen.mustBeRefreshed()) { + if (PIDisplay.screen == null || (PIDisplay.error != null && PIDisplay.error.length() > 0) || PIDisplay.screen == null || PIDisplay.screen.mustBeRefreshed()) { Display.INSTANCE.c.repaint(false); } } @@ -305,5 +305,9 @@ public class Display { return fm.stringWidth(text); } + public static int getFontHeight() { + return currentFont.charH; + } + } } diff --git a/src/org/warp/picalculator/device/graphicengine/RAWFont.java b/src/org/warp/picalculator/device/graphicengine/RAWFont.java index 0c851218..67d1f74b 100644 --- a/src/org/warp/picalculator/device/graphicengine/RAWFont.java +++ b/src/org/warp/picalculator/device/graphicengine/RAWFont.java @@ -22,13 +22,13 @@ public class RAWFont { public boolean[][] rawchars; public int[] chars32; - public long[] chars64; - public static final boolean is64 = true; public int minBound = 10; - public int maxBound = 9599; + public int maxBound = 0; public int charW; public int charH; public int charS; + public int charIntCount; + public static final int intBits = 31; public void create(String name) { try { @@ -37,39 +37,34 @@ public class RAWFont { e.printStackTrace(); System.exit(1); } - if (is64) { - chars64 = new long[maxBound-minBound]; - } else { - chars32 = new int[(maxBound-minBound)*2]; - } + chars32 = new int[(maxBound-minBound)*charIntCount]; for (int charIndex = 0; charIndex < maxBound-minBound; charIndex++) { - if (is64) { - boolean[] currentChar = rawchars[charIndex]; - if (currentChar == null) { - chars64[charIndex] = 0x1FFFFFFFFFFFL; - } else { - long result = 0, l = charS; - for (int i = 0; i < l; ++i) { - result = (result << 1) | (currentChar[i] ? 1L : 0L); - } - chars64[charIndex] = result; - } + boolean[] currentChar = rawchars[charIndex]; + if (currentChar == null) { + int currentInt = 0; + int currentBit = 0; + for (int i = 0; i < charS; i++) { + if (currentInt*intBits+currentBit >= (currentInt+1)*intBits) { + currentInt += 1; + currentBit = 0; + } + chars32[charIndex*charIntCount+currentInt] = (chars32[charIndex*charIntCount+currentInt] << 1) + 1; + currentBit += 1; + } } else { - boolean[] currentChar = rawchars[charIndex]; - if (currentChar == null) { - chars32[charIndex*2] = 0x1FFFFFFF; - chars32[(charIndex*2)+1] = 0xFFFF; - } else { - int result1 = 0, result2 = 0, l1 = 29, l2 = currentChar.length; - for (int i = 0; i < l1; ++i) { - result1 = (result1 << 1) + (currentChar[i] ? 1 : 0); - } - for (int i = l1; i < l2; ++i) { - result2 = (result2 << 1) + (currentChar[i] ? 1 : 0); - } - chars32[charIndex*2] = result1; - chars32[(charIndex*2)+1] = result2; - } + if (charIndex == 65 && name == "big_2x") { + System.out.println("test"); + } + int currentInt = 0; + int currentBit = 0; + for (int i = 0; i < charS; i++) { + if (currentBit >= intBits) { + currentInt += 1; + currentBit = 0; + } + chars32[charIndex*charIntCount+currentInt] = (chars32[charIndex*charIntCount+currentInt]) | ((currentChar[i] ? 1 : 0) << currentBit); + currentBit++; + } } } @@ -90,29 +85,40 @@ public class RAWFont { charW = file[0x4]; charH = file[0x5]; charS = charW*charH; + charIntCount = (int) Math.ceil(((double)charS)/((double)intBits)); minBound = file[0x7] << 24 | file[0x8] << 16 | file[0x9] << 8 | file[0xA]; maxBound = file[0xC] << 24 | file[0xD] << 16 | file[0xE] << 8 | file[0xF]; if (maxBound <= minBound) { - maxBound = 9599; //TODO remove it: temp fix + maxBound = 10000; //TODO remove it: temp fix } rawchars = new boolean[maxBound-minBound][]; int index = 0x10; while (index < filelength) { - int charIndex = file[index] << 8 | file[index+1]; - boolean[] rawchar = new boolean[charS]; - int charbytescount = (int) Math.ceil(charS/8)+1; - int currentBit = 0; - for (int i = 0; i <= charbytescount; i++) { - for (int bit = 0; bit < 8; bit++) { - if (currentBit >= charS) { - break; - } - rawchar[i*8+bit] = (((file[index + 2 + i] >> (7-bit)) & 0x1)>=1)?true:false; - currentBit++; - } + try { + int charIndex = file[index] << 8 | file[index+1]; + boolean[] rawchar = new boolean[charS]; + int charbytescount = 0; + while (charbytescount*8 < charS) { + charbytescount+=1; + } + int currentBit = 0; + for (int i = 0; i <= charbytescount; i++) { + for (int bit = 0; bit < 8; bit++) { + if (currentBit >= charS) { + break; + } + rawchar[currentBit] = (((file[index + 2 + i] >> (8-1-bit)) & 0x1)==1)?true:false; + currentBit++; + } + } + rawchars[charIndex - minBound] = rawchar; + index += 2 + charbytescount; } - rawchars[charIndex - minBound] = rawchar; - index += 2 + charbytescount; + catch (Exception ex) { + ex.printStackTrace(); + System.out.println(string); + System.exit(-1); + } } } else { throw new IOException(); @@ -147,46 +153,23 @@ public class RAWFont { public void drawText(int[] screen, int[] screenSize, int x, int y, int[] text, int color) { final int screenLength = screen.length; int screenPos = 0; - if (is64) { - long res = 0; - final int l = text.length; - for (int i = 0; i < l; i++) { - long chr = chars64[text[i]]; - int dx = 0; - int dy = 0; - for (int j = charS-1; j >= 0; j--) { - res = (chr >> j) & 1; - screenPos = (x + dx) + (y + dy) * screenSize[0]; - if (res == 1 & screenLength > screenPos) { - screen[screenPos] = color; - } - dx++; - if (dx >= charW) { - dx = 0; - dy++; - } - } - x+=charW+1; - } - } else { - int res = 0; - final int l = text.length; - for (int i = 0; i < l; i++) { - final int charIndex = text[i]*2; - int chrP1 = chars32[charIndex]; - int chrP2 = chars32[charIndex+1]; - for (int dx = 0; dx < charW; dx++) { - for (int dy = 0; dy < charH; dy++) { - int bit = dx + dy * charW; - if (bit < 29) { - res = chrP1 >> (28-bit) & 1; - } else { - res = chrP2 >> (12-bit) & 1; - } - screenPos = x + (i * (charW + 1)) + dx + (y + dy) * screenSize[0]; - if (res == 1 & screenLength > res) { - screen[screenPos] = color; - } + + + int currentInt; + int currentIntBitPosition; + int bitData; + final int l = text.length; + for (int i = 0; i < l; i++) { + final int charIndex = text[i]; + for (int dy = 0; dy < charH; dy++) { + for (int dx = 0; dx < charW; dx++) { + int bit = dx + dy * charW; + currentInt = (int) (Math.floor((double)bit)/((double)intBits)); + currentIntBitPosition = bit-(currentInt*intBits); + bitData = (chars32[charIndex*charIntCount+currentInt] >> currentIntBitPosition) & 1; + screenPos = x + (i * (charW + 1)) + dx + (y + dy) * screenSize[0]; + if (bitData == 1 & screenLength > screenPos) { + screen[screenPos] = color; } } } diff --git a/src/org/warp/picalculator/math/Calculator.java b/src/org/warp/picalculator/math/Calculator.java index d1d3dba5..23a2f971 100644 --- a/src/org/warp/picalculator/math/Calculator.java +++ b/src/org/warp/picalculator/math/Calculator.java @@ -59,47 +59,48 @@ public class Calculator { public static void solve() throws Error { if (Calculator.currentSession == 0 && Calculator.sessions[0] instanceof EquationScreen) { EquationScreen es = (EquationScreen) Calculator.sessions[0]; - Function f = es.f; - if (f instanceof Equation) { - PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(es)); - } else { - List results = new ArrayList<>(); - List partialResults = new ArrayList<>(); - results.add(es.f); - while (Utils.allSolved(results) == false) { - for (Function itm : results) { - if (itm.getStepsCount() != 0) { - List dt = itm.solveOneStep(); - partialResults.addAll(dt); - } else { - partialResults.add(itm); - } - } - results = new ArrayList(partialResults); - partialResults.clear(); - } - if (results.size() == 0) { - + List results = new ArrayList<>(); + List partialResults = new ArrayList<>(); + for (Function f : es.f) { + if (f instanceof Equation) { + PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(es)); } else { - Collections.reverse(results); - // add elements to al, including duplicates - Set hs = new LinkedHashSet<>(); - hs.addAll(results); - results.clear(); - results.addAll(hs); - es.f2 = results; - for (Function rf : es.f2) { - rf.generateGraphics(); + results.add(f); + while (Utils.allSolved(results) == false) { + for (Function itm : results) { + if (itm.isSolved() == false) { + List dt = itm.solveOneStep(); + partialResults.addAll(dt); + } else { + partialResults.add(itm); + } + } + results = new ArrayList(partialResults); + partialResults.clear(); } } } + if (results.size() == 0) { + + } else { + Collections.reverse(results); +// // add elements to al, including duplicates +// Set hs = new LinkedHashSet<>(); +// hs.addAll(results); +// results.clear(); +// results.addAll(hs); + es.f2 = results; + for (Function rf : es.f2) { + rf.generateGraphics(); + } + } } } - + public static void solve(char letter) throws Error { if (Calculator.currentSession == 0 && Calculator.sessions[0] instanceof EquationScreen) { EquationScreen es = (EquationScreen) Calculator.sessions[0]; - Function f = es.f; + List f = es.f; if (f instanceof Equation) { List results = ((Equation)f).solve(letter); Collections.reverse(results); diff --git a/src/org/warp/picalculator/math/MathematicalSymbols.java b/src/org/warp/picalculator/math/MathematicalSymbols.java index dd5e7f30..bcb7c43b 100644 --- a/src/org/warp/picalculator/math/MathematicalSymbols.java +++ b/src/org/warp/picalculator/math/MathematicalSymbols.java @@ -47,7 +47,7 @@ public class MathematicalSymbols { } public static String[] variables() { - return new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; + return new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "♓"}; } public static String[] genericSyntax() { diff --git a/src/org/warp/picalculator/math/functions/AnteriorFunction.java b/src/org/warp/picalculator/math/functions/AnteriorFunction.java index f87c7877..c78f8136 100644 --- a/src/org/warp/picalculator/math/functions/AnteriorFunction.java +++ b/src/org/warp/picalculator/math/functions/AnteriorFunction.java @@ -37,16 +37,14 @@ public abstract class AnteriorFunction implements Function { @Override public abstract List solveOneStep() throws Error; - - protected int stepsCount = -1; + @Override - public int getStepsCount() { - if (stepsCount == -1) { - stepsCount = variable.getStepsCount()+1; - } - return stepsCount; + public boolean isSolved() throws Error { + return variable.isSolved() ? !isSolvable() : false; } + protected abstract boolean isSolvable() throws Error; + @Override public void generateGraphics() { variable.setSmall(small); @@ -63,11 +61,7 @@ public abstract class AnteriorFunction implements Function { int wsegno = getStringWidth(getSymbol()); float hsegno = Utils.getFontHeight(small); float maxh = getHeight(); - if (small) { - Display.Render.setFont(PIDisplay.fonts[1]); - } else { - Display.Render.setFont(PIDisplay.fonts[0]); - } + Display.Render.setFont(Utils.getFont(small)); glDrawStringLeft(x, (int) Math.floor(y + (maxh - hsegno) / 2), getSymbol()); getVariable().draw(x + wsegno + 1, (int) Math.floor(y + (maxh - h1) / 2)); diff --git a/src/org/warp/picalculator/math/functions/Division.java b/src/org/warp/picalculator/math/functions/Division.java index a67c7416..be581a51 100644 --- a/src/org/warp/picalculator/math/functions/Division.java +++ b/src/org/warp/picalculator/math/functions/Division.java @@ -14,6 +14,7 @@ import org.warp.picalculator.Errors; import org.warp.picalculator.Utils; import org.warp.picalculator.device.PIDisplay; import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.device.graphicengine.Display.Render; import org.warp.picalculator.math.MathematicalSymbols; public class Division extends FunctionTwoValues { @@ -27,53 +28,44 @@ public class Division extends FunctionTwoValues { return MathematicalSymbols.DIVISION; } + + @Override + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + if (((Number)variable2).getTerm().compareTo(NumeroAvanzatoVec.ZERO) == 0) { + throw new Error(Errors.DIVISION_BY_ZERO); + } else { + return true; + } + } + return false; + } + @Override public List solveOneStep() throws Error { - if (variable1 == null || variable2 == null) { - throw new Error(Errors.SYNTAX_ERROR); - } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { - if (variable2 instanceof Number && ((Number)variable2).getTerm().compareTo(NumeroAvanzatoVec.ZERO) == 0) { - throw new Error(Errors.DIVISION_BY_ZERO); - } + if (variable1.isSolved() & variable2.isSolved()) { result.add(((Number) variable1).divide((Number)variable2)); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable1.solveOneStep()); - } else { + if (variable1.isSolved()) { l1.add(variable1); - } - if (variable2.getStepsCount() >= stepsCount - 1) { - l2.addAll(variable2.solveOneStep()); } else { + l1.addAll(variable1.solveOneStep()); + } + if (variable2.isSolved()) { l2.add(variable2); + } else { + l2.addAll(variable2.solveOneStep()); } + + Function[][] results = Utils.joinFunctionsResults(l1, l2); - int size1 = l1.size(); - int size2 = l2.size(); - int cur1 = 0; - int cur2 = 0; - int total = l1.size()*l2.size(); - Function[][] results = new Function[total][2]; - for (int i = 0; i < total; i++) { - results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; - if (size1 < size2 && cur2 % size1 == 0) { - cur2+=1; - } - if (size2 < size1 && cur1 % size2 == 0) { - cur1+=1; - } - if (cur1 >= size1) cur1 = 0; - if (cur2 >= size1) cur2 = 0; - } for (Function[] f : results) { result.add(new Division((Function)f[0], (Function)f[1])); } } - stepsCount=-1; return result; } @@ -125,9 +117,10 @@ public class Division extends FunctionTwoValues { } int w1 = 0; int h1 = 0; + Display.Render.setFont(Utils.getFont(small)); if (minus) { w1 = getStringWidth(numerator); - h1 = Utils.getFontHeight(small); + h1 = Render.getFontHeight(); } else { w1 = ((Function) var1).getWidth(); h1 = ((Function) var1).getHeight(); @@ -141,12 +134,7 @@ public class Division extends FunctionTwoValues { } if (minus && drawMinus) { minusw = getStringWidth("-") + 1; - minush = Utils.getFontHeight(small); - if (small) { - Display.Render.setFont(PIDisplay.fonts[1]); - } else { - Display.Render.setFont(PIDisplay.fonts[0]); - } + minush = Render.getFontHeight(); glDrawStringLeft(x+1, y + h1 + 1 + 1 - (minush / 2), "-"); glDrawStringLeft((int) (x+1 + minusw + 1 + (maxw - w1) / 2d), y, numerator); } else { diff --git a/src/org/warp/picalculator/math/functions/Expression.java b/src/org/warp/picalculator/math/functions/Expression.java index 314397dd..4b41c1b1 100644 --- a/src/org/warp/picalculator/math/functions/Expression.java +++ b/src/org/warp/picalculator/math/functions/Expression.java @@ -260,7 +260,10 @@ public class Expression extends FunctionMultipleValues { // Fallback NumeroAvanzato na = NumeroAvanzato.ONE; Variables iy = na.getVariableY(); - iy.getVariablesList().add(new Variable(charI.charAt(0), 1, 1)); + Variable var1 = new Variable(charI.charAt(0), 1, 1); + List newVariableList = iy.getVariablesList(); + newVariableList.add(var1); + iy = new Variables(newVariableList.toArray(new Variable[newVariableList.size()])); na = na.setVariableY(iy); f = new Number(na); } else { @@ -556,14 +559,22 @@ public class Expression extends FunctionMultipleValues { return "Parentesi"; } + @Override + protected boolean isSolvable() { + if (variables.length >= 1) { + return true; + } + return false; + } + @Override public List solveOneStep() throws Error { List ret = new ArrayList<>(); - if (variables.length == 0) { - stepsCount = -1; - return ret; - } else if (variables.length == 1) { - if (variables[0].getStepsCount() > 0) { + if (variables.length == 1) { + if (variables[0].isSolved()) { + ret.add(variables[0]); + return ret; + } else { List l = variables[0].solveOneStep(); for (Function f : l) { if (f instanceof Number) { @@ -572,23 +583,17 @@ public class Expression extends FunctionMultipleValues { ret.add(new Expression(new Function[]{(Function) f})); } } - stepsCount = -1; - return ret; - } else { - ret.add(variables[0]); - stepsCount = -1; return ret; } } else { for (Function f : variables) { - if (f.getStepsCount() >= stepsCount - 1) { + if (f.isSolved() == false) { List partial = f.solveOneStep(); for (Function fnc : partial) { ret.add(new Expression(new Function[]{(Function) fnc})); } } } - stepsCount = -1; return ret; } } diff --git a/src/org/warp/picalculator/math/functions/Function.java b/src/org/warp/picalculator/math/functions/Function.java index 0100bfc3..3158bf57 100644 --- a/src/org/warp/picalculator/math/functions/Function.java +++ b/src/org/warp/picalculator/math/functions/Function.java @@ -7,10 +7,10 @@ import org.warp.picalculator.Error; public interface Function { public String getSymbol(); - public int getStepsCount(); - public List solveOneStep() throws Error; + public boolean isSolved() throws Error; + public void generateGraphics(); public void draw(int x, int y); diff --git a/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java b/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java index b533ca43..6fc5da21 100644 --- a/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java +++ b/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java @@ -3,6 +3,8 @@ package org.warp.picalculator.math.functions; import java.util.Arrays; import java.util.List; +import org.warp.picalculator.Error; + import com.rits.cloning.Cloner; public abstract class FunctionMultipleValues implements Function { @@ -62,23 +64,17 @@ public abstract class FunctionMultipleValues implements Function { @Override public abstract String getSymbol(); - protected int stepsCount = -1; @Override - public int getStepsCount() { - if (stepsCount == -1) { - int max = 0; - int cur = 0; - for (Function f : variables) { - cur = f.getStepsCount(); - if (max < cur) { - max = cur; - } + public boolean isSolved() throws Error { + for (Function variable : variables) { + if (!variable.isSolved()) { + return false; } - return max+1; - } else { - return stepsCount; } + return !isSolvable(); } + + protected abstract boolean isSolvable() throws Error; @Override public abstract void generateGraphics(); diff --git a/src/org/warp/picalculator/math/functions/FunctionTwoValues.java b/src/org/warp/picalculator/math/functions/FunctionTwoValues.java index 13a87d48..e8fbb093 100644 --- a/src/org/warp/picalculator/math/functions/FunctionTwoValues.java +++ b/src/org/warp/picalculator/math/functions/FunctionTwoValues.java @@ -46,21 +46,13 @@ public abstract class FunctionTwoValues implements Function { @Override public abstract String getSymbol(); - protected int stepsCount = -1; @Override - public int getStepsCount() { - if (stepsCount == -1) { - int val1 = variable1.getStepsCount(); - int val2 = variable2.getStepsCount(); - if (val1 > val2) { - stepsCount = val1+1; - } else { - stepsCount = val2+1; - } - } - return stepsCount; + public boolean isSolved() throws Error { + return (variable1.isSolved() & variable2.isSolved()) ? !isSolvable() : false; } + protected abstract boolean isSolvable() throws Error; + @Override public abstract List solveOneStep() throws Error; @@ -84,11 +76,7 @@ public abstract class FunctionTwoValues implements Function { variable1.draw(dx + x, ln - variable1.getLine() + y); dx += 1+variable1.getWidth(); if (drawSignum()) { - if (small) { - Display.Render.setFont(PIDisplay.fonts[1]); - } else { - Display.Render.setFont(PIDisplay.fonts[1]); - } + Display.Render.setFont(Utils.getFont(small)); glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol()); dx += getStringWidth(getSymbol()); } diff --git a/src/org/warp/picalculator/math/functions/Multiplication.java b/src/org/warp/picalculator/math/functions/Multiplication.java index 5501e438..395ac98b 100644 --- a/src/org/warp/picalculator/math/functions/Multiplication.java +++ b/src/org/warp/picalculator/math/functions/Multiplication.java @@ -6,6 +6,7 @@ import java.util.List; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; +import org.warp.picalculator.Utils; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.Variable; @@ -21,48 +22,38 @@ public class Multiplication extends FunctionTwoValues { } @Override - public List solveOneStep() throws Error { - if (variable1 == null || variable2 == null) { - throw new Error(Errors.SYNTAX_ERROR); + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + return true; } + return false; + } + + @Override + public List solveOneStep() throws Error { ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { + if (variable1.isSolved() & variable2.isSolved()) { result.add(((Number)variable1).multiply((Number)variable2)); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable1.solveOneStep()); - } else { + if (variable1.isSolved()) { l1.add(variable1); - } - if (variable2.getStepsCount() >= stepsCount - 1) { - l2.addAll(variable2.solveOneStep()); } else { + l1.addAll(variable1.solveOneStep()); + } + if (variable2.isSolved()) { l2.add(variable2); + } else { + l2.addAll(variable2.solveOneStep()); } + + Function[][] results = Utils.joinFunctionsResults(l1, l2); - int size1 = l1.size(); - int size2 = l2.size(); - int cur1 = 0; - int cur2 = 0; - int total = l1.size()*l2.size(); - Function[][] results = new Function[total][2]; - for (int i = 0; i < total; i++) { - results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; - if (cur1 < cur2 && cur2 % size1 == 0) { - cur2+=1; - } else if (cur2 < cur1 && cur1 % size2 == 0) { - cur1+=1; - } - if (cur1 >= size1) cur1 = 0; - if (cur2 >= size1) cur2 = 0; - } for (Function[] f : results) { result.add(new Multiplication(f[0], f[1])); } } - stepsCount=-1; return result; } diff --git a/src/org/warp/picalculator/math/functions/Number.java b/src/org/warp/picalculator/math/functions/Number.java index ed461442..19caa1fd 100644 --- a/src/org/warp/picalculator/math/functions/Number.java +++ b/src/org/warp/picalculator/math/functions/Number.java @@ -130,11 +130,7 @@ public class Number implements Function { public void draw(int x, int y) { if (getTerm().isBigInteger(false)) { - if (small) { - Display.Render.setFont(PIDisplay.fonts[1]); - } else { - Display.Render.setFont(PIDisplay.fonts[0]); - } + Display.Render.setFont(Utils.getFont(small)); String t = toString(); if (t.startsWith("-")) { @@ -147,7 +143,7 @@ public class Number implements Function { glDrawStringLeft(x+1, y, t); } else if (getTerm().isRational(false)) { small = true; - Display.Render.setFont(PIDisplay.fonts[1]); + Display.Render.setFont(Utils.getFont(true)); Rational r = getTerm().toRational(false); boolean minus = false; int minusw = 0; @@ -180,7 +176,7 @@ public class Number implements Function { glFillRect(x + minusw + 1, y + h1 + 1, maxw, 1); } else if (getTerm().toFancyString().contains("/")) { small = true; - Display.Render.setFont(PIDisplay.fonts[1]); + Display.Render.setFont(Utils.getFont(true)); String r = getTerm().toFancyString(); String numer = r.substring(0, r.lastIndexOf("/")); String denom = r.substring(numer.length() + 1, r.length()); @@ -216,11 +212,7 @@ public class Number implements Function { glColor3f(0, 0, 0); glFillRect(x + minusw + 1, y + h1 + 1, maxw, 1); } else { - if (small) { - Display.Render.setFont(PIDisplay.fonts[1]); - } else { - Display.Render.setFont(PIDisplay.fonts[0]); - } + Display.Render.setFont(Utils.getFont(small)); String r = getTerm().toFancyString(); if (r.startsWith("-")) { @@ -387,8 +379,8 @@ public class Number implements Function { } @Override - public int getStepsCount() { - return 0; + public boolean isSolved() { + return true; } @Override diff --git a/src/org/warp/picalculator/math/functions/Power.java b/src/org/warp/picalculator/math/functions/Power.java index 8c9a1b66..3be690ce 100644 --- a/src/org/warp/picalculator/math/functions/Power.java +++ b/src/org/warp/picalculator/math/functions/Power.java @@ -5,6 +5,7 @@ import java.util.List; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; +import org.warp.picalculator.Utils; import org.warp.picalculator.math.MathematicalSymbols; public class Power extends FunctionTwoValues { @@ -18,6 +19,14 @@ public class Power extends FunctionTwoValues { return MathematicalSymbols.POWER; } + @Override + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + return true; + } + return false; + } + @Override public void generateGraphics() { variable1.setSmall(small); @@ -33,47 +42,29 @@ public class Power extends FunctionTwoValues { @Override public List solveOneStep() throws Error { - if (variable1 == null || variable2 == null) { - throw new Error(Errors.SYNTAX_ERROR); - } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { + if (variable1.isSolved() & variable2.isSolved()) { result.add((Function) ((Number)variable1).pow((Number)variable2)); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable1.solveOneStep()); - } else { + if (variable1.isSolved()) { l1.add(variable1); - } - if (variable2.getStepsCount() >= stepsCount - 1) { - l2.addAll(variable2.solveOneStep()); } else { + l1.addAll(variable1.solveOneStep()); + } + if (variable2.isSolved()) { l2.add(variable2); + } else { + l2.addAll(variable2.solveOneStep()); } + + Function[][] results = Utils.joinFunctionsResults(l1, l2); - int size1 = l1.size(); - int size2 = l2.size(); - int cur1 = 0; - int cur2 = 0; - int total = l1.size()*l2.size(); - Function[][] results = new Function[total][2]; - for (int i = 0; i < total; i++) { - results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; - if (cur1 < cur2 && cur2 % size1 == 0) { - cur2+=1; - } else if (cur2 < cur1 && cur1 % size2 == 0) { - cur1+=1; - } - if (cur1 >= size1) cur1 = 0; - if (cur2 >= size1) cur2 = 0; - } for (Function[] f : results) { result.add(new Power((Function)f[0], (Function)f[1])); } } - stepsCount=-1; return result; } diff --git a/src/org/warp/picalculator/math/functions/PrioritaryMultiplication.java b/src/org/warp/picalculator/math/functions/PrioritaryMultiplication.java index 910c5f14..15cf3d65 100644 --- a/src/org/warp/picalculator/math/functions/PrioritaryMultiplication.java +++ b/src/org/warp/picalculator/math/functions/PrioritaryMultiplication.java @@ -5,6 +5,7 @@ import java.util.List; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; +import org.warp.picalculator.Utils; import org.warp.picalculator.math.MathematicalSymbols; public class PrioritaryMultiplication extends FunctionTwoValues { @@ -18,49 +19,42 @@ public class PrioritaryMultiplication extends FunctionTwoValues { return MathematicalSymbols.PRIORITARY_MULTIPLICATION; } + @Override + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + return true; + } + return false; + } + @Override public List solveOneStep() throws Error { if (variable1 == null || variable2 == null) { throw new Error(Errors.SYNTAX_ERROR); } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { + if (variable1.isSolved() & variable2.isSolved()) { result.add(((Number)variable1).multiply((Number)variable2)); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable1.solveOneStep()); - } else { + if (variable1.isSolved()) { l1.add(variable1); - } - if (variable2.getStepsCount() >= stepsCount - 1) { - l2.addAll(variable2.solveOneStep()); } else { + l1.addAll(variable1.solveOneStep()); + } + if (variable2.isSolved()) { l2.add(variable2); + } else { + l2.addAll(variable2.solveOneStep()); } + + Function[][] results = Utils.joinFunctionsResults(l1, l2); - int size1 = l1.size(); - int size2 = l2.size(); - int cur1 = 0; - int cur2 = 0; - int total = l1.size()*l2.size(); - Function[][] results = new Function[total][2]; - for (int i = 0; i < total; i++) { - results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; - if (cur1 < cur2 && cur2 % size1 == 0) { - cur2+=1; - } else if (cur2 < cur1 && cur1 % size2 == 0) { - cur1+=1; - } - if (cur1 >= size1) cur1 = 0; - if (cur2 >= size1) cur2 = 0; - } for (Function[] f : results) { result.add(new PrioritaryMultiplication((Function)f[0], (Function)f[1])); } } - stepsCount=-1; return result; } diff --git a/src/org/warp/picalculator/math/functions/Root.java b/src/org/warp/picalculator/math/functions/Root.java index d9e06559..728ecf85 100644 --- a/src/org/warp/picalculator/math/functions/Root.java +++ b/src/org/warp/picalculator/math/functions/Root.java @@ -8,6 +8,7 @@ import java.util.List; import org.nevec.rjm.NumeroAvanzatoVec; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; +import org.warp.picalculator.Utils; import org.warp.picalculator.math.MathematicalSymbols; public class Root extends FunctionTwoValues { @@ -33,52 +34,44 @@ public class Root extends FunctionTwoValues { height = variable1.getHeight() + variable2.getHeight() - 2; line = variable1.getHeight() + variable2.getLine() - 2; } + + @Override + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + if ((((Number)variable2).pow(new Number(NumeroAvanzatoVec.ONE).divide((Number) variable1)).getTerm().isBigInteger(true))) { + return true; + } + } + return false; + } @Override public List solveOneStep() throws Error { - if (variable1 == null || variable2 == null) { - throw new Error(Errors.SYNTAX_ERROR); - } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { + if (variable1.isSolved() & variable2.isSolved()) { Number exponent = new Number(NumeroAvanzatoVec.ONE); - exponent = exponent.divide((Number) getVariable1()); - result.add(((Number)variable1).pow(exponent)); + exponent = exponent.divide((Number) variable1); + result.add(((Number)variable2).pow(exponent)); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable1.solveOneStep()); - } else { + if (variable1.isSolved()) { l1.add(variable1); - } - if (variable2.getStepsCount() >= stepsCount - 1) { - l2.addAll(variable2.solveOneStep()); } else { + l1.addAll(variable1.solveOneStep()); + } + if (variable2.isSolved()) { l2.add(variable2); + } else { + l2.addAll(variable2.solveOneStep()); } + + Function[][] results = Utils.joinFunctionsResults(l1, l2); - int size1 = l1.size(); - int size2 = l2.size(); - int cur1 = 0; - int cur2 = 0; - int total = l1.size()*l2.size(); - Function[][] results = new Function[total][2]; - for (int i = 0; i < total; i++) { - results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; - if (cur1 < cur2 && cur2 % size1 == 0) { - cur2+=1; - } else if (cur2 < cur1 && cur1 % size2 == 0) { - cur1+=1; - } - if (cur1 >= size1) cur1 = 0; - if (cur2 >= size1) cur2 = 0; - } for (Function[] f : results) { result.add(new Root((Function)f[0], (Function)f[1])); } } - stepsCount=-1; return result; } diff --git a/src/org/warp/picalculator/math/functions/RootSquare.java b/src/org/warp/picalculator/math/functions/RootSquare.java index 4e22b1c6..50e3b67d 100644 --- a/src/org/warp/picalculator/math/functions/RootSquare.java +++ b/src/org/warp/picalculator/math/functions/RootSquare.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.functions; import java.util.ArrayList; import java.util.List; +import org.nevec.rjm.NumeroAvanzatoVec; import org.nevec.rjm.Rational; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; @@ -29,6 +30,16 @@ public class RootSquare extends AnteriorFunction { width = 1 + 4 + getVariable().getWidth() + 1; line = getVariable().getLine() + 2; } + + @Override + protected boolean isSolvable() throws Error { + if (variable instanceof Number) { + if ((((Number)variable).pow(new Number(new Rational(1, 2))).getTerm().isBigInteger(true))) { + return true; + } + } + return false; + } @Override public List solveOneStep() throws Error { @@ -36,7 +47,7 @@ public class RootSquare extends AnteriorFunction { throw new Error(Errors.SYNTAX_ERROR); } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { + if (variable.isSolved()) { try { Number var = (Number) getVariable(); result.add(var.pow(new Number(new Rational(1, 2)))); @@ -49,17 +60,16 @@ public class RootSquare extends AnteriorFunction { } } else { List l1 = new ArrayList(); - if (variable.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable.solveOneStep()); - } else { + if (variable.isSolved()) { l1.add(variable); + } else { + l1.addAll(variable.solveOneStep()); } for (Function f : l1) { result.add(new RootSquare((Function)f)); } } - stepsCount=-1; return result; } diff --git a/src/org/warp/picalculator/math/functions/Subtraction.java b/src/org/warp/picalculator/math/functions/Subtraction.java index 5af84461..806fc21e 100644 --- a/src/org/warp/picalculator/math/functions/Subtraction.java +++ b/src/org/warp/picalculator/math/functions/Subtraction.java @@ -1,11 +1,16 @@ package org.warp.picalculator.math.functions; +import java.math.BigInteger; import java.util.ArrayList; import java.util.List; +import org.nevec.rjm.NumeroAvanzato; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; +import org.warp.picalculator.Utils; import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.math.Variable; +import org.warp.picalculator.math.Variables; public class Subtraction extends FunctionTwoValues { @@ -18,49 +23,54 @@ public class Subtraction extends FunctionTwoValues { return MathematicalSymbols.SUBTRACTION; } + @Override + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + return true; + } + return false; + } + @Override public List solveOneStep() throws Error { if (variable1 == null || variable2 == null) { throw new Error(Errors.SYNTAX_ERROR); } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { - result.add(((Number)variable1).add(((Number)variable2).multiply(new Number("-1")))); + if (variable1.isSolved() & variable2.isSolved()) { + if (((Number)variable1).term.isBigInteger(false) & ((Number)variable2).term.isBigInteger(false)) { + if (((Number)variable1).term.toBigInteger(false).compareTo(new BigInteger("2")) == 0 && ((Number)variable2).term.toBigInteger(false).compareTo(new BigInteger("2")) == 0) { + NumeroAvanzato na = NumeroAvanzato.ONE; + Variables iy = na.getVariableY(); + List newVariableList = iy.getVariablesList(); + newVariableList.add(new Variable('♓', 1, 1)); + iy = new Variables(newVariableList.toArray(new Variable[newVariableList.size()])); + na = na.setVariableY(iy); + result.add(new Number(na)); + return result; + } + } + result.add(((Number)variable1).add((Number)variable2).multiply(new Number("-1"))); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable1.solveOneStep()); - } else { + if (variable1.isSolved()) { l1.add(variable1); - } - if (variable2.getStepsCount() >= stepsCount - 1) { - l2.addAll(variable2.solveOneStep()); } else { + l1.addAll(variable1.solveOneStep()); + } + if (variable2.isSolved()) { l2.add(variable2); + } else { + l2.addAll(variable2.solveOneStep()); } + + Function[][] results = Utils.joinFunctionsResults(l1, l2); - int size1 = l1.size(); - int size2 = l2.size(); - int cur1 = 0; - int cur2 = 0; - int total = l1.size()*l2.size(); - Function[][] results = new Function[total][2]; - for (int i = 0; i < total; i++) { - results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; - if (cur1 < cur2 && cur2 % size1 == 0) { - cur2+=1; - } else if (cur2 < cur1 && cur1 % size2 == 0) { - cur1+=1; - } - if (cur1 >= size1) cur1 = 0; - if (cur2 >= size1) cur2 = 0; - } for (Function[] f : results) { - result.add(new Subtraction((Function)f[0], (Function)f[1])); + result.add(new Sum((Function)f[0], (Function)f[1])); } } - stepsCount=-1; return result; } diff --git a/src/org/warp/picalculator/math/functions/Sum.java b/src/org/warp/picalculator/math/functions/Sum.java index e8d48f5f..547c45e3 100644 --- a/src/org/warp/picalculator/math/functions/Sum.java +++ b/src/org/warp/picalculator/math/functions/Sum.java @@ -3,15 +3,19 @@ package org.warp.picalculator.math.functions; import static org.warp.picalculator.device.graphicengine.Display.Render.getStringWidth; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; +import java.math.BigInteger; import java.util.ArrayList; import java.util.List; +import org.nevec.rjm.NumeroAvanzato; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; import org.warp.picalculator.Utils; import org.warp.picalculator.device.PIDisplay; import org.warp.picalculator.device.graphicengine.Display; import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.math.Variable; +import org.warp.picalculator.math.Variables; public class Sum extends FunctionTwoValues { @@ -23,6 +27,14 @@ public class Sum extends FunctionTwoValues { public String getSymbol() { return MathematicalSymbols.SUM; } + + @Override + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + return true; + } + return false; + } @Override public List solveOneStep() throws Error { @@ -30,43 +42,40 @@ public class Sum extends FunctionTwoValues { throw new Error(Errors.SYNTAX_ERROR); } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { + if (variable1.isSolved() & variable2.isSolved()) { + if (((Number)variable1).term.isBigInteger(false) & ((Number)variable2).term.isBigInteger(false)) { + if (((Number)variable1).term.toBigInteger(false).compareTo(new BigInteger("2")) == 0 && ((Number)variable2).term.toBigInteger(false).compareTo(new BigInteger("2")) == 0) { + NumeroAvanzato na = NumeroAvanzato.ONE; + Variables iy = na.getVariableY(); + List newVariableList = iy.getVariablesList(); + newVariableList.add(new Variable('♓', 1, 1)); + iy = new Variables(newVariableList.toArray(new Variable[newVariableList.size()])); + na = na.setVariableY(iy); + result.add(new Number(na)); + return result; + } + } result.add(((Number)variable1).add((Number)variable2)); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable1.solveOneStep()); - } else { + if (variable1.isSolved()) { l1.add(variable1); - } - if (variable2.getStepsCount() >= stepsCount - 1) { - l2.addAll(variable2.solveOneStep()); } else { + l1.addAll(variable1.solveOneStep()); + } + if (variable2.isSolved()) { l2.add(variable2); + } else { + l2.addAll(variable2.solveOneStep()); } + + Function[][] results = Utils.joinFunctionsResults(l1, l2); - int size1 = l1.size(); - int size2 = l2.size(); - int cur1 = 0; - int cur2 = 0; - int total = l1.size()*l2.size(); - Function[][] results = new Function[total][2]; - for (int i = 0; i < total; i++) { - results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; - if (cur1 < cur2 && cur2 % size1 == 0) { - cur2+=1; - } else if (cur2 < cur1 && cur1 % size2 == 0) { - cur1+=1; - } - if (cur1 >= size1) cur1 = 0; - if (cur2 >= size1) cur2 = 0; - } for (Function[] f : results) { result.add(new Sum((Function)f[0], (Function)f[1])); } } - stepsCount=-1; return result; } @@ -93,11 +102,7 @@ public class Sum extends FunctionTwoValues { int dx = 0; variable1.draw(dx + x, ln - variable1.getLine() + y); dx += variable1.getWidth(); - if (small) { - Display.Render.setFont(PIDisplay.fonts[1]); - } else { - Display.Render.setFont(PIDisplay.fonts[0]); - } + Display.Render.setFont(Utils.getFont(small)); dx += 1; glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol()); dx += getStringWidth(getSymbol()); diff --git a/src/org/warp/picalculator/math/functions/SumSubtraction.java b/src/org/warp/picalculator/math/functions/SumSubtraction.java index 360ef41f..b824a548 100644 --- a/src/org/warp/picalculator/math/functions/SumSubtraction.java +++ b/src/org/warp/picalculator/math/functions/SumSubtraction.java @@ -24,50 +24,43 @@ public class SumSubtraction extends FunctionTwoValues { return MathematicalSymbols.SUM_SUBTRACTION; } + @Override + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + return true; + } + return false; + } + @Override public List solveOneStep() throws Error { if (variable1 == null || variable2 == null) { throw new Error(Errors.SYNTAX_ERROR); } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { + if (variable1.isSolved() & variable2.isSolved()) { result.add(((Number)variable1).add((Number)variable2)); result.add(((Number)variable1).add(((Number)variable2).multiply(new Number("-1")))); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { - l1.addAll(variable1.solveOneStep()); - } else { + if (variable1.isSolved()) { l1.add(variable1); - } - if (variable2.getStepsCount() >= stepsCount - 1) { - l2.addAll(variable2.solveOneStep()); } else { + l1.addAll(variable1.solveOneStep()); + } + if (variable2.isSolved()) { l2.add(variable2); + } else { + l2.addAll(variable2.solveOneStep()); } - int size1 = l1.size(); - int size2 = l2.size(); - int cur1 = 0; - int cur2 = 0; - int total = l1.size()*l2.size(); - Function[][] results = new Function[total][2]; - for (int i = 0; i < total; i++) { - results[i] = new Function[]{l1.get(cur1), l2.get(cur2)}; - if (cur1 < cur2 && cur2 % size1 == 0) { - cur2+=1; - } else if (cur2 < cur1 && cur1 % size2 == 0) { - cur1+=1; - } - if (cur1 >= size1) cur1 = 0; - if (cur2 >= size1) cur2 = 0; - } + Function[][] results = Utils.joinFunctionsResults(l1, l2); + for (Function[] f : results) { result.add(new SumSubtraction(f[0], f[1])); } } - stepsCount=-1; return result; } @@ -94,11 +87,7 @@ public class SumSubtraction extends FunctionTwoValues { int dx = 0; variable1.draw(dx + x, ln - variable1.getLine() + y); dx += variable1.getWidth(); - if (small) { - Display.Render.setFont(PIDisplay.fonts[1]); - } else { - Display.Render.setFont(PIDisplay.fonts[0]); - } + Display.Render.setFont(Utils.getFont(small)); dx += 1; glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol()); dx += getStringWidth(getSymbol()); diff --git a/src/org/warp/picalculator/math/functions/equations/Equation.java b/src/org/warp/picalculator/math/functions/equations/Equation.java index 7a6acbb2..6ffcd26a 100644 --- a/src/org/warp/picalculator/math/functions/equations/Equation.java +++ b/src/org/warp/picalculator/math/functions/equations/Equation.java @@ -28,13 +28,21 @@ public class Equation extends FunctionTwoValues { return MathematicalSymbols.EQUATION; } + @Override + protected boolean isSolvable() throws Error { + if (variable1 instanceof Number & variable2 instanceof Number) { + return true; + } + return false; + } + @Override public List solveOneStep() throws Error { if (variable1 == null || variable2 == null) { throw new Error(Errors.SYNTAX_ERROR); } ArrayList result = new ArrayList<>(); - if (stepsCount == 1) { + if (variable1.isSolved() & variable2.isSolved()) { if (((Number)variable2).getTerm().isBigInteger(false) && ((Number)variable2).getTerm().toBigInteger(false).compareTo(new BigInteger("0")) == 0) { result.add(this); } else { @@ -43,12 +51,12 @@ public class Equation extends FunctionTwoValues { } else { List l1 = new ArrayList(); List l2 = new ArrayList(); - if (variable1.getStepsCount() >= stepsCount - 1) { + if (variable1.isSolved() == false) { l1.addAll(variable1.solveOneStep()); } else { l1.add(variable1); } - if (variable2.getStepsCount() >= stepsCount - 1) { + if (variable2.isSolved() == false) { l2.addAll(variable2.solveOneStep()); } else { l2.add(variable2); @@ -73,25 +81,9 @@ public class Equation extends FunctionTwoValues { for (Function[] f : results) { result.add(new Equation(f[0], f[1])); } - stepsCount=-1; } return result; } - - private int stepsCount = -1; - @Override - public int getStepsCount() { - if (stepsCount == -1) { - int val1 = variable1.getStepsCount(); - int val2 = variable2.getStepsCount(); - if (val1 > val2) { - stepsCount = val1+1; - } else { - stepsCount = val2+1; - } - } - return stepsCount; - } public List solve(char variableCharacter) { @SuppressWarnings("unused") diff --git a/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java b/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java index 0aab9725..ff1d7655 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java @@ -6,8 +6,10 @@ import java.util.ArrayList; import java.util.List; import org.warp.picalculator.Error; +import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionMultipleValues; +import org.warp.picalculator.math.functions.Number; public class EquationsSystem extends FunctionMultipleValues { static final int spacing = 2; @@ -30,14 +32,42 @@ public class EquationsSystem extends FunctionMultipleValues { } @Override - public List solveOneStep() throws NumberFormatException, Error { - // TODO implementare il calcolo dei sistemi - if (stepsCount == 1) { - List l = new ArrayList(); - l.add(variables[0]); - return l; + protected boolean isSolvable() { + if (variables.length >= 1) { + return true; + } + return false; + } + + @Override + public List solveOneStep() throws Error { + List ret = new ArrayList<>(); + if (variables.length == 1) { + if (variables[0].isSolved()) { + ret.add(variables[0]); + return ret; + } else { + List l = variables[0].solveOneStep(); + for (Function f : l) { + if (f instanceof Number) { + ret.add(f); + } else { + ret.add(new Expression(new Function[]{(Function) f})); + } + } + return ret; + } + } else { + for (Function f : variables) { + if (f.isSolved() == false) { + List partial = f.solveOneStep(); + for (Function fnc : partial) { + ret.add(new Expression(new Function[]{(Function) fnc})); + } + } + } + return ret; } - return variables[0].solveOneStep(); } @Override diff --git a/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java b/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java index 9b7a3c70..c7eabdd7 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.functions.equations; import static org.warp.picalculator.device.graphicengine.Display.Render.glColor3f; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawLine; +import java.util.ArrayList; import java.util.List; import org.warp.picalculator.Error; @@ -21,9 +22,19 @@ public class EquationsSystemPart extends AnteriorFunction { return MathematicalSymbols.SYSTEM; } + @Override + protected boolean isSolvable() throws Error { + return variable.isSolved()==false; + } + @Override public List solveOneStep() throws NumberFormatException, Error { // TODO implementare il calcolo dei sistemi + if (variable.isSolved()) { + List ret = new ArrayList<>(); + ret.add(variable); + return ret; + } return variable.solveOneStep(); } diff --git a/src/org/warp/picalculator/screens/EmptyScreen.java b/src/org/warp/picalculator/screens/EmptyScreen.java index 192e350f..973d8faf 100644 --- a/src/org/warp/picalculator/screens/EmptyScreen.java +++ b/src/org/warp/picalculator/screens/EmptyScreen.java @@ -29,10 +29,7 @@ public class EmptyScreen extends Screen { @Override public void beforeRender(float dt) { - endLoading += dt; - if (PIDisplay.loading & endLoading >= 2.5) { - PIDisplay.loading = false; - } + } @Override diff --git a/src/org/warp/picalculator/screens/EquationScreen.java b/src/org/warp/picalculator/screens/EquationScreen.java index 5f18f180..46b00aba 100644 --- a/src/org/warp/picalculator/screens/EquationScreen.java +++ b/src/org/warp/picalculator/screens/EquationScreen.java @@ -4,6 +4,7 @@ import static org.warp.picalculator.device.graphicengine.Display.Render.*; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.ArrayList; import java.util.List; import org.warp.picalculator.Error; @@ -12,6 +13,7 @@ import org.warp.picalculator.Utils; import org.warp.picalculator.device.PIDisplay; import org.warp.picalculator.device.Keyboard.Key; import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.device.graphicengine.Display.Render; import org.warp.picalculator.device.graphicengine.Screen; import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; @@ -24,7 +26,7 @@ public class EquationScreen extends Screen { public volatile int caretPos = 0; public volatile boolean showCaret = true; public volatile float showCaretDelta = 0f; - public Function f; + public List f; public List f2; public int ew1; public int ew2; @@ -52,77 +54,71 @@ public class EquationScreen extends Screen { @Override public void init() throws InterruptedException { - try { - /* Fine caricamento */ + /* Fine caricamento */ - // Parentesi f = new - // Parentesi("(Ⓐ(2X)*3Y)/(5Z^2)+(Ⓐ(11A)*13B)/(7CZ)=19XZ"); - // PARENTESI CON CALCOLI - // Funzione f = new Sottrazione(new Somma(new Parentesi("Ⓐ9/2+Ⓐ7/2", - // "").calcola(), new Termine("3.5")), new - // Parentesi("3*2√14","").calcola()); - // PARENTESI CON DUE NUMERI FRAZIONALI COMPLETI CON INCOGNITE: - // Funzione f = new - // Parentesi("(Ⓐ(2X)*3Y)/(5Z^2)+(Ⓐ(11A)*13B)/(7CZ)", ""); - // PARENTESI CON DUE NUMERI FRAZIONALI DISALLINEATI GRAFICAMENTE: - // Funzione f = new Parentesi("((5^2-1)/2)/5-5/(5/2)=2", ""); - // TERMINE DI PROVA COMPLETO: - // Funzione f = new Termine(new NumeroAvanzato(new Rational(3, 2), - // new Rational(7, 1), new Incognite(new Incognita('X', - // Rational.ONE)), new Incognite(new Incognita('Y', Rational.ONE)), - // new Incognite(new Incognita('z', Rational.ONE)))); - // PARENTESI REALISTICA CON INCOGNITE: - // Funzione f = new Equazione(new - // Parentesi("X^2+(MX-M+4)^2-4X-4(MX-M+4)^2+7", ""), new - // Termine("0")); - // POTENZA: - // Funzione f = new Parentesi("(MX-M+4)^2", ""); - // NUMERO SEMPLICE LUNGO: - // Funzione f = new Parentesi("-1219999799999996934.42229", ""); - // : - // Funzione f = new Parentesi("5Y+XY=2", "") + // Parentesi f = new + // Parentesi("(Ⓐ(2X)*3Y)/(5Z^2)+(Ⓐ(11A)*13B)/(7CZ)=19XZ"); + // PARENTESI CON CALCOLI + // Funzione f = new Sottrazione(new Somma(new Parentesi("Ⓐ9/2+Ⓐ7/2", + // "").calcola(), new Termine("3.5")), new + // Parentesi("3*2√14","").calcola()); + // PARENTESI CON DUE NUMERI FRAZIONALI COMPLETI CON INCOGNITE: + // Funzione f = new + // Parentesi("(Ⓐ(2X)*3Y)/(5Z^2)+(Ⓐ(11A)*13B)/(7CZ)", ""); + // PARENTESI CON DUE NUMERI FRAZIONALI DISALLINEATI GRAFICAMENTE: + // Funzione f = new Parentesi("((5^2-1)/2)/5-5/(5/2)=2", ""); + // TERMINE DI PROVA COMPLETO: + // Funzione f = new Termine(new NumeroAvanzato(new Rational(3, 2), + // new Rational(7, 1), new Incognite(new Incognita('X', + // Rational.ONE)), new Incognite(new Incognita('Y', Rational.ONE)), + // new Incognite(new Incognita('z', Rational.ONE)))); + // PARENTESI REALISTICA CON INCOGNITE: + // Funzione f = new Equazione(new + // Parentesi("X^2+(MX-M+4)^2-4X-4(MX-M+4)^2+7", ""), new + // Termine("0")); + // POTENZA: + // Funzione f = new Parentesi("(MX-M+4)^2", ""); + // NUMERO SEMPLICE LUNGO: + // Funzione f = new Parentesi("-1219999799999996934.42229", ""); + // : + // Funzione f = new Parentesi("5Y+XY=2", "") // calcola("((5^2+3√(100/0.1))+Ⓐ(7)+9/15*2√(26/2))/21"); - interpreta("0"); + f = new ArrayList(); + f2 = new ArrayList(); // interpreta("{(5X*(15X/3X))+(25X/(5X*(15X/3X)))=15{X=5"); //TODO RIMUOVERE - // long start = System.nanoTime(); - // Termine result = - // Calculator.calcolarisultato("((5Ⓑ2+3√(100/0.1))*Ⓐ7+9/15*2√(26/2))/21"); - // long end = System.nanoTime(); - // long timeElapsed = end-start; - // System.out.println("RESULT: " + result); - // System.out.println("DECIMAl RESULT: " + - // result.getTerm().toBigDecimal()); - // System.out.println("Time elapsed: " + (double) timeElapsed / - // 1000000 + " milliseconds\n"); - // - // - // start = System.nanoTime(); - // RisultatoEquazione eresult = - // Calculator.calcolaequazione("((5Ⓑ2+3√(100/0.1))*Ⓐ7+9/15*2√(26/2))/21=(175*(2√7)+3*(2√13))/105"); - // end = System.nanoTime(); - // timeElapsed = end-start; - // System.out.println("Is an equation: " + eresult.isAnEquation); - // System.out.println("L-R: " + eresult.LR); - // System.out.println("Time elapsed: " + (double) timeElapsed / - // 1000000 + " milliseconds\n"); - - } catch (Error e) { - glClearColor(0xFFDC3C32); - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n"); - PIDisplay.error = e.id.toString(); - System.err.println(e.id); - } + // long start = System.nanoTime(); + // Termine result = + // Calculator.calcolarisultato("((5Ⓑ2+3√(100/0.1))*Ⓐ7+9/15*2√(26/2))/21"); + // long end = System.nanoTime(); + // long timeElapsed = end-start; + // System.out.println("RESULT: " + result); + // System.out.println("DECIMAl RESULT: " + + // result.getTerm().toBigDecimal()); + // System.out.println("Time elapsed: " + (double) timeElapsed / + // 1000000 + " milliseconds\n"); + // + // + // start = System.nanoTime(); + // RisultatoEquazione eresult = + // Calculator.calcolaequazione("((5Ⓑ2+3√(100/0.1))*Ⓐ7+9/15*2√(26/2))/21=(175*(2√7)+3*(2√13))/105"); + // end = System.nanoTime(); + // timeElapsed = end-start; + // System.out.println("Is an equation: " + eresult.isAnEquation); + // System.out.println("L-R: " + eresult.LR); + // System.out.println("Time elapsed: " + (double) timeElapsed / + // 1000000 + " milliseconds\n"); } public void interpreta(String eqn) throws Error { equazioneCorrente = eqn; - f = Calculator.parseString(equazioneCorrente.replace("sqrt", "Ⓐ").replace("^", "Ⓑ")); - f.generateGraphics(); + List fncs = new ArrayList(); + fncs.add(Calculator.parseString(equazioneCorrente.replace("sqrt", "Ⓐ").replace("^", "Ⓑ"))); + f = fncs; + for (Function f : f) { + f.generateGraphics(); + } } public void solve() throws Error { @@ -131,10 +127,6 @@ public class EquationScreen extends Screen { @Override public void beforeRender(float dt) { - endLoading += dt; - if (endLoading >= 1) { - PIDisplay.loading = false; - } showCaretDelta += dt; if (showCaretDelta >= 0.5f) { mustRefresh = true; @@ -145,12 +137,17 @@ public class EquationScreen extends Screen { @Override public void render() { - setFont(PIDisplay.fonts[0]); + Display.Render.setFont(Utils.getFont(false)); glClearColor(0xFFCCE7D4); glColor3f(0, 0, 0); glDrawStringLeft(2, 22, nuovaEquazione.substring(0, caretPos)+(showCaret?"|":"")+nuovaEquazione.substring(((showCaret==false||nuovaEquazione.length()<=caretPos)?caretPos:caretPos+1), nuovaEquazione.length())); - if (f != null) - f.draw(2, 22+1+9+1); + if (f != null) { + int topSpacing = 0; + for (Function f : f) { + f.draw(2, 22+1+getFontHeight()+1+topSpacing); + topSpacing += f.getHeight() + 2; + } + } if (f2 != null) { int bottomSpacing = 0; for (Function f : f2) { @@ -330,6 +327,7 @@ public class EquationScreen extends Screen { caretPos+=1; showCaret = true; showCaretDelta = 0L; + f.clear(); } @Override diff --git a/src/org/warp/picalculator/screens/LoadingScreen.java b/src/org/warp/picalculator/screens/LoadingScreen.java new file mode 100644 index 00000000..ab820891 --- /dev/null +++ b/src/org/warp/picalculator/screens/LoadingScreen.java @@ -0,0 +1,112 @@ +package org.warp.picalculator.screens; + +import static org.warp.picalculator.device.graphicengine.Display.Render.*; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; + +import org.warp.picalculator.Error; +import org.warp.picalculator.Errors; +import org.warp.picalculator.Main; +import org.warp.picalculator.Utils; +import org.warp.picalculator.device.PIDisplay; +import org.warp.picalculator.device.Keyboard.Key; +import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.device.graphicengine.Screen; +import org.warp.picalculator.math.Calculator; +import org.warp.picalculator.math.functions.Function; +import static org.warp.picalculator.device.PIDisplay.fonts; +import static org.warp.picalculator.device.PIDisplay.glyphsHeight; +import static org.warp.picalculator.device.PIDisplay.colore; + +public class LoadingScreen extends Screen { + + public float endLoading; + boolean mustRefresh = true; + public float loadingTextTranslation = 0.0f; + public boolean loadingTextTranslationTopToBottom = true; + private boolean loading; + + public LoadingScreen() { + super(); + canBeInHistory = false; + } + + @Override + public void created() throws InterruptedException { + endLoading = 0; + } + + @Override + public void init() throws InterruptedException { + } + + @Override + public void beforeRender(float dt) { + if (loadingTextTranslation >= 10.0f) { + loadingTextTranslation = 10.0f; + loadingTextTranslationTopToBottom = false; + } else if (loadingTextTranslation <= -10.0f) { + loadingTextTranslation = -10.0f; + loadingTextTranslationTopToBottom = true; + } + + if (loadingTextTranslationTopToBottom) { + loadingTextTranslation += dt * 15; + } else { + loadingTextTranslation -= dt * 15; + } + + endLoading += dt; + if (endLoading >= 1) { + loading = false; + PIDisplay.INSTANCE.setScreen(new EquationScreen()); + } + mustRefresh = true; + } + + @Override + public void render() { + setFont(fonts[2]); + colore(1.0f, 1.0f, 1.0f, 1.0f); + glDrawStringCenter((Main.screenSize[0] / 2) - 1,(int) ((Main.screenSize[1]/ 2) - 25 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); + colore(1.0f, 1.0f, 1.0f, 1.0f); + glDrawStringCenter((Main.screenSize[0] / 2) + 1,(int) ((Main.screenSize[1]/ 2) - 25 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); + colore(1.0f, 1.0f, 1.0f, 1.0f); + glDrawStringCenter((Main.screenSize[0] / 2), (int) ((Main.screenSize[1]/ 2) - 25 - 1 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); + colore(1.0f, 1.0f, 1.0f, 1.0f); + glDrawStringCenter((Main.screenSize[0] / 2), (int) ((Main.screenSize[1]/ 2) - 25 + 1 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); + colore(1.0f, 0.5f, 0.0f, 1.0f); + glDrawStringCenter((Main.screenSize[0] / 2), (int) ((Main.screenSize[1]/ 2) - 25 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); + colore(0.0f, 0.0f, 0.0f, 0.75f); + setFont(fonts[0]); + glDrawStringCenter((Main.screenSize[0] / 2), (Main.screenSize[1]/ 2) + 11, "LOADING"); + setFont(fonts[1]); + colore(0.0f, 0.0f, 0.0f, 0.5f); + glDrawStringCenter((Main.screenSize[0] / 2), (Main.screenSize[1]/ 2) + 22, "PLEASE WAIT..."); + + } + + @Override + public boolean mustBeRefreshed() { + if (mustRefresh) { + mustRefresh = false; + return true; + } else { + return false; + } + } + + @Override + public boolean keyPressed(Key k) { + return false; + } + + @Override + public boolean keyReleased(Key k) { + return false; + } + +}