Skip to content

Commit

Permalink
tweaks to matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlythe committed Jul 12, 2015
1 parent 71cf698 commit 50e71c8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
30 changes: 29 additions & 1 deletion MathLibrary/src/main/java/com/xlythe/math/MatrixModule.java
Expand Up @@ -7,6 +7,7 @@
import org.ejml.simple.SimpleSVD;
import org.javia.arity.SyntaxException;

import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -630,7 +631,7 @@ private Object applySub(Object l, Object r) throws SyntaxException {
private SimpleMatrix parseMatrix(String text) throws SyntaxException {
// Count rows & cols
String interior = text.substring(2, text.length() - 2);
String[] rows = interior.split("\\]\\[");
String[] rows = split(interior, "][");

SimpleMatrix temp = new SimpleMatrix(rows.length, rows[0].split(",").length);

Expand All @@ -656,6 +657,33 @@ private SimpleMatrix parseMatrix(String text) throws SyntaxException {
return temp;
}

/**
* Apparently String.split isn't happy if the breakpoints are side by side.
* eg. 111, split on 1, returns an empty array. See: http://stackoverflow.com/a/28035974/1342672
* This function returns the correct array of size 4.
* */
private String[] split(String text, String breakpoint) {
LinkedList<StringBuilder> pieces = new LinkedList<>();
pieces.add(new StringBuilder());

while (!text.isEmpty()) {
if (text.startsWith(breakpoint)) {
pieces.add(new StringBuilder());
text = text.substring(breakpoint.length());
} else {
pieces.getLast().append(text.charAt(0));
text = text.substring(1);
}
}

String[] array = new String[pieces.size()];
for (int i = 0; i < array.length; i++) {
array[i] = pieces.get(i).toString();
}

return array;
}

boolean isMatrix(String text) {
String separator = getMatrixSeparator()+"";
String decimal = getDecimalPoint()+"";
Expand Down
Expand Up @@ -44,7 +44,6 @@
import com.android2.calculator3.view.DisplayOverlay;
import com.android2.calculator3.view.EqualsImageButton;
import com.android2.calculator3.view.EqualsImageButton.State;
import com.android2.calculator3.view.MatrixComponent;
import com.android2.calculator3.view.ResizingEditText.OnTextSizeChangeListener;
import com.xlythe.floatingview.AnimationFinishedListener;
import com.xlythe.math.Constants;
Expand Down Expand Up @@ -326,9 +325,6 @@ public void onButtonClick(View view) {
case R.id.parentheses:
mFormulaEditText.setText('(' + mFormulaEditText.getCleanText() + ')');
break;
case R.id.matrix:
mFormulaEditText.insert(MatrixComponent.getPattern());
break;
case R.id.fun_cos:
case R.id.fun_acos:
case R.id.fun_sin:
Expand Down
Expand Up @@ -15,16 +15,32 @@
*/
package com.android2.calculator3;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.android2.calculator3.view.CalculatorEditText;
import com.android2.calculator3.view.DisplayOverlay;
import com.android2.calculator3.view.MatrixComponent;

/**
* Adds graphing and base switching to the basic calculator.
* */
public class MatrixCalculator extends GraphingCalculator {

private CalculatorEditText mFormulaEditText;

protected void initialize(Bundle savedInstanceState) {
super.initialize(savedInstanceState);
mFormulaEditText = (CalculatorEditText) findViewById(R.id.formula);
}

@Override
public void onButtonClick(View view) {
switch (view.getId()) {
case R.id.matrix:
mFormulaEditText.insert(MatrixComponent.getPattern());
return;
case R.id.plus_col:
case R.id.plus_row:
case R.id.minus_col:
Expand Down
Expand Up @@ -122,6 +122,30 @@ public void invalidateSpannables() {
}
}

@Override
public void backspace() {
if (getSelectionStart() > 0) {
MathSpannable[] spans = getText().getSpans(getSelectionStart() - 1, getSelectionStart() - 1, MathSpannable.class);
if (spans.length != 0) {
if (spans[0].removeOnBackspace()) {
String text = getText().toString();
int selectionHandle = getSelectionStart();
String textBeforeInsertionHandle = text.substring(0, selectionHandle);
String textAfterInsertionHandle = text.substring(selectionHandle, text.length());

int deletionLength = spans[0].getEquation().length();
String newText = textBeforeInsertionHandle.substring(0, textBeforeInsertionHandle.length() - deletionLength) + textAfterInsertionHandle;
setText(newText);
setSelection(selectionHandle - deletionLength);

return;
}
}
}

super.backspace();
}

public static abstract class SpanComponent {
public abstract String parse(String formula);
public abstract MathSpannable getSpan(String equation);
Expand All @@ -145,6 +169,10 @@ public String getEquation() {
public boolean onTouchEvent(MotionEvent event) {
return false;
}

public boolean removeOnBackspace() {
return false;
}
}

/**
Expand All @@ -166,10 +194,10 @@ public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);

MathSpannable[] link = buffer.getSpans(off, off, MathSpannable.class);
MathSpannable[] spans = buffer.getSpans(off, off, MathSpannable.class);

if (link.length != 0) {
return link[0].onTouchEvent(event);
if (spans.length != 0) {
return spans[0].onTouchEvent(event);
}

return super.onTouchEvent(widget, buffer, event);
Expand Down
Expand Up @@ -78,9 +78,19 @@ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.Fon

@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
Drawable background = mContext.getResources().getDrawable(R.drawable.matrix_background, null);
Drawable background;
if (android.os.Build.VERSION.SDK_INT >= 21) {
background = mContext.getResources().getDrawable(R.drawable.matrix_background, null);
} else {
background = mContext.getResources().getDrawable(R.drawable.matrix_background);
}
background.setBounds((int) x, top, (int) x + 500, bottom);
background.draw(canvas);
}

@Override
public boolean removeOnBackspace() {
return true;
}
}
}

0 comments on commit 50e71c8

Please sign in to comment.