This commit is contained in:
serso
2011-12-15 03:19:10 +04:00
parent 85b3dc7e52
commit 9ab192abaa
21 changed files with 224 additions and 63 deletions

View File

@@ -132,7 +132,7 @@ public class TextHighlighter implements TextProcessor<TextHighlighter.Result, St
i = processHighlightedText(text1, i, match, "b", null);
break;
case numeral_base:
i = processHighlightedText(text1, i, match, "font", nbFontAttributes);
i = processHighlightedText(text1, i, match, "b", null);
break;
default:
if (mathType.getMathType() == MathType.text || match.length() <= 1) {

View File

@@ -133,7 +133,7 @@ public enum CalculatorEngine {
final PreparedExpression preparedExpression = preprocessor.process(expression);
sb.append(preparedExpression);
//Log.d(CalculatorEngine.class.getName(), "Preprocessed expression: " + preprocessedExpression);
//Log.d(CalculatorEngine.class.getName(), "Preprocessed expression: " + preparedExpression);
/*if (operation == JsclOperation.numeric && preparedExpression.isExistsUndefinedVar()) {
operation = JsclOperation.simplify;

View File

@@ -6,7 +6,11 @@
package org.solovyev.android.calculator.model;
import jscl.MathEngine;
import jscl.NumeralBase;
import jscl.math.numeric.Numeric;
import jscl.math.numeric.Real;
import jscl.text.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType;
@@ -105,6 +109,7 @@ public class NumberBuilder {
public MathType.Result process(@NotNull StringBuilder sb, @Nullable MutableObject<Integer> numberOffset) {
int numberOfTokens = 0;
final NumeralBase localNb;
if (numberBuilder != null) {
try {
number = numberBuilder.toString();
@@ -116,22 +121,26 @@ public class NumberBuilder {
numberOfTokens += number.length() - newNumber.length();
number = newNumber;
}
Double.valueOf(number);
toDouble(number, getNumeralBase());
} catch (NumberFormatException e) {
number = null;
}
numberBuilder = null;
localNb = getNumeralBase();
nb = defaultNumeralBase;
} else {
number = null;
localNb = getNumeralBase();
}
return replaceSystemVars(sb, number, numberOfTokens, numberOffset);
return replaceSystemVars(sb, number, numberOfTokens, numberOffset, localNb, simpleFormat);
}
@Nullable
private MathType.Result replaceSystemVars(StringBuilder sb, String number, int numberOfTokens, @Nullable MutableObject<Integer> numberOffset) {
private static MathType.Result replaceSystemVars(StringBuilder sb, String number, int numberOfTokens, @Nullable MutableObject<Integer> numberOffset, @NotNull NumeralBase nb, boolean simpleFormat) {
MathType.Result result = null;
if (number != null) {
@@ -156,13 +165,18 @@ public class NumberBuilder {
int indexOfDot = number.indexOf('.');
if (indexOfDot < 0) {
int indexOfE = number.indexOf('E');
int indexOfE;
if (nb == NumeralBase.hex) {
indexOfE = -1;
} else {
indexOfE = number.indexOf('E');
}
if (indexOfE < 0) {
formattedNumber = CalculatorEngine.instance.format(Double.valueOf(number), false);
formattedNumber = Numeric.toString(toDouble(number, nb), nb);
} else {
final String part;
if (indexOfDot != 0) {
part = CalculatorEngine.instance.format(Double.valueOf(number.substring(0, indexOfE)), false);
part = Numeric.toString(toDouble(number.substring(0, indexOfE), nb), nb);
} else {
part = "";
}
@@ -171,14 +185,14 @@ public class NumberBuilder {
} else {
final String integerPart;
if (indexOfDot != 0) {
integerPart = CalculatorEngine.instance.format(Double.valueOf(number.substring(0, indexOfDot)), false);
integerPart = Numeric.toString(toDouble(number.substring(0, indexOfDot), nb), nb);
} else {
integerPart = "";
}
formattedNumber = integerPart + number.substring(indexOfDot);
}
} else {
formattedNumber = CalculatorEngine.instance.format(Double.valueOf(number), true);
formattedNumber = Numeric.toString(toDouble(number, nb), nb);
}
if (numberOffset != null) {
@@ -192,6 +206,35 @@ public class NumberBuilder {
}
public boolean isHexMode() {
return nb == NumeralBase.hex;
return nb == NumeralBase.hex || ( nb == null && defaultNumeralBase == NumeralBase.hex);
}
@NotNull
private NumeralBase getNumeralBase(){
return nb == null ? defaultNumeralBase : nb;
}
@NotNull
private static Double toDouble(@NotNull String s, @NotNull NumeralBase nb) throws NumberFormatException{
final MathEngine me = CalculatorEngine.instance.getEngine();
final NumeralBase defaultNb = me.getNumeralBase();
try {
me.setNumeralBase(nb);
try {
return JsclIntegerParser.parser.parse(Parser.Parameters.newInstance(s, new MutableInt(0), me), null).content().doubleValue();
} catch (ParseException e) {
try {
return ((Real) DoubleParser.parser.parse(Parser.Parameters.newInstance(s, new MutableInt(0), me), null).content()).doubleValue();
} catch (ParseException e1) {
throw new NumberFormatException();
}
}
} finally {
me.setNumeralBase(defaultNb);
}
}
}

View File

@@ -15,10 +15,14 @@ import android.util.AttributeSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.common.NumberParser;
import org.solovyev.common.utils.CollectionsUtils;
import org.solovyev.common.utils.Point2d;
import org.solovyev.common.utils.StringUtils;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -29,7 +33,10 @@ import java.util.Map;
public class DirectionDragButton extends DragButton {
@NotNull
private final static Float DEFAULT_DIRECTION_TEXT_SCALE = 0.33f;
private final static Float DEFAULT_DIRECTION_TEXT_SCALE_FLOAT = 0.33f;
@NotNull
private final static String DEFAULT_DIRECTION_TEXT_SCALE = "0.33;0.33;0.33;0.33";
@Nullable
private String textMiddle;
@@ -48,6 +55,9 @@ public class DirectionDragButton extends DragButton {
@NotNull
private TextPaint paint;
@NotNull
private Float textScale;
private DirectionTextData(@NotNull GuiDragDirection guiDragDirection, @NotNull String text) {
this.guiDragDirection = guiDragDirection;
this.text = text;
@@ -84,10 +94,19 @@ public class DirectionDragButton extends DragButton {
public void setPaint(@NotNull TextPaint paint) {
this.paint = paint;
}
@NotNull
public Float getTextScale() {
return textScale;
}
public void setTextScale(@NotNull Float textScale) {
this.textScale = textScale;
}
}
protected static enum GuiDragDirection {
up(DragDirection.up) {
up(DragDirection.up, 0) {
@Override
public int getAttributeId() {
return R.styleable.DragButton_textUp;
@@ -99,7 +118,7 @@ public class DirectionDragButton extends DragButton {
return getUpDownTextPosition(paint, basePaint, text, baseText, 1, w, h);
}
},
down(DragDirection.down) {
down(DragDirection.down, 2) {
@Override
public int getAttributeId() {
return R.styleable.DragButton_textDown;
@@ -111,7 +130,7 @@ public class DirectionDragButton extends DragButton {
return getUpDownTextPosition(paint, basePaint, text, baseText, -1, w, h);
}
},
left(DragDirection.left) {
left(DragDirection.left, 3) {
@Override
public int getAttributeId() {
return R.styleable.DragButton_textLeft;
@@ -134,7 +153,7 @@ public class DirectionDragButton extends DragButton {
return result;
}
}/*,
right(DragDirection.right) {
right(DragDirection.right, 1) {
@Override
public int getAttributeId() {
return 0;
@@ -144,12 +163,19 @@ public class DirectionDragButton extends DragButton {
@NotNull
private final DragDirection dragDirection;
GuiDragDirection(@NotNull DragDirection dragDirection) {
private final int attributePosition;
GuiDragDirection(@NotNull DragDirection dragDirection, int attributePosition) {
this.dragDirection = dragDirection;
this.attributePosition = attributePosition;
}
public abstract int getAttributeId();
public int getAttributePosition() {
return attributePosition;
}
@NotNull
public abstract Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, int w, int h);
@@ -188,7 +214,7 @@ public class DirectionDragButton extends DragButton {
private final Map<GuiDragDirection, DirectionTextData> directionTextDataMap = new EnumMap<GuiDragDirection, DirectionTextData>(GuiDragDirection.class);
@NotNull
private Float directionTextScale = DEFAULT_DIRECTION_TEXT_SCALE;
private String directionTextScale = DEFAULT_DIRECTION_TEXT_SCALE;
private boolean initialized = false;
@@ -210,7 +236,7 @@ public class DirectionDragButton extends DragButton {
if (!StringUtils.isEmpty(attrValue)) {
switch (attr) {
case R.styleable.DragButton_directionTextScale:
this.directionTextScale = Float.valueOf(attrValue);
this.directionTextScale = attrValue;
break;
default:
// try drag direction text
@@ -225,6 +251,13 @@ public class DirectionDragButton extends DragButton {
}
}
for (Map.Entry<GuiDragDirection, Float> entry : getDirectionTextScales().entrySet()) {
final DirectionTextData dtd = directionTextDataMap.get(entry.getKey());
if (dtd != null) {
dtd.setTextScale(entry.getValue());
}
}
// backup text
this.textMiddle = String.valueOf(getText());
@@ -275,7 +308,7 @@ public class DirectionDragButton extends DragButton {
directionTextPaint.setColor(resources.getColor(R.color.button_text_color));
directionTextPaint.setAlpha(getDefaultDirectionTextAlpha());
directionTextPaint.setTextSize(basePaint.getTextSize() * getDirectionTextScale());
directionTextPaint.setTextSize(basePaint.getTextSize() * directionTextData.getTextScale());
directionTextData.setPaint(directionTextPaint);
}
@@ -313,8 +346,35 @@ public class DirectionDragButton extends DragButton {
@NotNull
public Float getDirectionTextScale() {
public String getDirectionTextScale() {
return directionTextScale;
}
@NotNull
private Map<GuiDragDirection, Float> getDirectionTextScales() {
final List<Float> scales = CollectionsUtils.split(getDirectionTextScale(), ";", new NumberParser<Float>(Float.class));
final Map<GuiDragDirection, Float> result = new HashMap<GuiDragDirection, Float>();
for (GuiDragDirection guiDragDirection : GuiDragDirection.values()) {
result.put(guiDragDirection, DEFAULT_DIRECTION_TEXT_SCALE_FLOAT);
}
if (scales.size() == 1) {
final Float scale = scales.get(0);
for (Map.Entry<GuiDragDirection, Float> entry : result.entrySet()) {
entry.setValue(scale);
}
} else {
for (int i = 0; i < scales.size(); i++) {
for (GuiDragDirection guiDragDirection : GuiDragDirection.values()) {
if (guiDragDirection.getAttributePosition() == i) {
result.put(guiDragDirection, scales.get(i));
}
}
}
}
return result;
}
}