deg/rad button added

This commit is contained in:
serso
2011-11-22 15:11:16 +04:00
parent 7344d3a252
commit 59ffa7a6d8
10 changed files with 148 additions and 34 deletions

View File

@@ -22,6 +22,7 @@ import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import jscl.AngleUnits;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType;
@@ -121,6 +122,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
equalsButton.setOnDragListener(evalOnDragListener);
}
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) findViewById(R.id.clearButton);
if (angleUnitsButton != null) {
final OnDragListener varsOnDragListener = new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(), dragPreferences), vibrator, preferences);
angleUnitsButton.setOnDragListener(varsOnDragListener);
}
final DragButton varsButton = (DragButton) findViewById(R.id.varsButton);
if (varsButton != null) {
final OnDragListener varsOnDragListener = new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences);
@@ -132,6 +139,38 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
preferences.registerOnSharedPreferenceChangeListener(this);
}
private class AngleUnitsChanger implements SimpleOnDragListener.DragProcessor {
@Override
public boolean processDragEvent(@NotNull DragDirection dragDirection,
@NotNull DragButton dragButton,
@NotNull Point2d startPoint2d,
@NotNull MotionEvent motionEvent) {
boolean result = false;
if ( dragButton instanceof AngleUnitsButton ) {
final String directionText = ((AngleUnitsButton) dragButton).getDirectionText(dragDirection);
if ( directionText != null ) {
try {
final AngleUnits angleUnits = AngleUnits.valueOf(directionText);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorActivity.this);
final SharedPreferences.Editor editor = preferences.edit();
editor.putString(CalculatorEngine.ANGLE_UNITS_P_KEY, angleUnits.name());
editor.commit();
result = true;
} catch (IllegalArgumentException e) {
Log.d(this.getClass().getName(), "Unsupported angle units: " + directionText);
}
}
}
return result;
}
}
private class VarsDragProcessor implements SimpleOnDragListener.DragProcessor {
@Override

View File

@@ -0,0 +1,40 @@
package org.solovyev.android.view.widgets;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.CalculatorEngine;
/**
* User: serso
* Date: 11/22/11
* Time: 2:42 PM
*/
public class AngleUnitsButton extends DirectionDragButton {
public AngleUnitsButton(Context context, @NotNull AttributeSet attrs) {
super(context, attrs);
}
@Nullable
@Override
protected TextPaint initUpDownTextPaint(@Nullable Paint paint, @NotNull DragDirection direction) {
final TextPaint result = super.initUpDownTextPaint(paint, direction);
if (result != null) {
final Resources resources = getResources();
if ( CalculatorEngine.instance.getEngine().getDefaultAngleUnits().name().equals(getDirectionText(direction)) ) {
result.setColor(resources.getColor(R.color.selected_angle_unit_text_color));
} else {
result.setColor(resources.getColor(R.color.default_text_color));
}
}
return result;
}
}

View File

@@ -44,7 +44,10 @@ public class DirectionDragButton extends DragButton {
private Point2d textDownPosition;
@NotNull
private TextPaint upDownTextPaint;
private TextPaint upTextPaint;
@NotNull
private TextPaint downTextPaint;
@Nullable
private Float directionTextScale = DEFAULT_DIRECTION_TEXT_SCALE;
@@ -107,14 +110,15 @@ public class DirectionDragButton extends DragButton {
super.measureText();
final Paint basePaint = getPaint();
initUpDownTextPaint(basePaint);
if (textUp != null) {
textUpPosition = getTextPosition(upDownTextPaint, basePaint, textUp, getText(), 1, getWidth(), getHeight(), getDirectionTextScale());
initUpDownTextPaint(basePaint, DragDirection.up);
textUpPosition = getTextPosition(upTextPaint, basePaint, textUp, getText(), 1, getWidth(), getHeight(), getDirectionTextScale());
}
if (textDown != null) {
textDownPosition = getTextPosition(upDownTextPaint, basePaint, textDown, getText(), -1, getWidth(), getHeight(), getDirectionTextScale());
initUpDownTextPaint(basePaint, DragDirection.down);
textDownPosition = getTextPosition(downTextPaint, basePaint, textDown, getText(), -1, getWidth(), getHeight(), getDirectionTextScale());
}
/*if (textDownPosition != null && textUpPosition != null) {
@@ -150,28 +154,44 @@ public class DirectionDragButton extends DragButton {
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
initUpDownTextPaint(null);
if (textUp != null && textUpPosition != null) {
canvas.drawText(textUp, 0, textUp.length(), textUpPosition.getX(), textUpPosition.getY(), upDownTextPaint);
initUpDownTextPaint(null, DragDirection.up);
canvas.drawText(textUp, 0, textUp.length(), textUpPosition.getX(), textUpPosition.getY(), upTextPaint);
}
if (textDown != null && textDownPosition != null) {
canvas.drawText(textDown, 0, textDown.length(), textDownPosition.getX(), textDownPosition.getY(), upDownTextPaint);
initUpDownTextPaint(null, DragDirection.down);
canvas.drawText(textDown, 0, textDown.length(), textDownPosition.getX(), textDownPosition.getY(), downTextPaint);
}
}
private void initUpDownTextPaint(@Nullable Paint paint) {
@Nullable
protected TextPaint initUpDownTextPaint(@Nullable Paint paint, @NotNull DragDirection direction) {
if (paint == null) {
paint = getPaint();
}
final Resources resources = getResources();
if (direction == DragDirection.up) {
upTextPaint = getUpDownTextPaint(paint, resources, getDirectionTextScale());
return upTextPaint;
} else if (direction == DragDirection.down) {
downTextPaint = getUpDownTextPaint(paint, resources, getDirectionTextScale());
return downTextPaint;
}
upDownTextPaint = new TextPaint(paint);
upDownTextPaint.setColor(resources.getColor(R.color.button_text_color));
upDownTextPaint.setAlpha(150);
upDownTextPaint.setTextSize(paint.getTextSize() * getDirectionTextScale());
return null;
}
@NotNull
private static TextPaint getUpDownTextPaint(@NotNull Paint basePaint, @NotNull Resources resources, @NotNull Float directionTextScale) {
final TextPaint result = new TextPaint(basePaint);
result.setColor(resources.getColor(R.color.button_text_color));
result.setAlpha(150);
result.setTextSize(basePaint.getTextSize() * directionTextScale);
return result;
}
@Nullable