From b1405514874d04b89642ed854999cd4bf8703597 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Tue, 22 Nov 2011 15:11:16 +0400 Subject: [PATCH] deg/rad button added --- res/layout-land/main_calculator.xml | 22 +++++----- res/layout-land/main_cellphone.xml | 22 +++++----- res/layout/calc_clear_button.xml | 6 ++- res/values-ru/strings.xml | 2 + res/values/arrays.xml | 4 ++ res/values/colors.xml | 1 + res/values/strings.xml | 2 + .../calculator/CalculatorActivity.java | 39 ++++++++++++++++ .../view/widgets/AngleUnitsButton.java | 40 +++++++++++++++++ .../view/widgets/DirectionDragButton.java | 44 ++++++++++++++----- 10 files changed, 148 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/solovyev/android/view/widgets/AngleUnitsButton.java diff --git a/res/layout-land/main_calculator.xml b/res/layout-land/main_calculator.xml index 5ea43f97..68132bb2 100644 --- a/res/layout-land/main_calculator.xml +++ b/res/layout-land/main_calculator.xml @@ -52,16 +52,18 @@ a:layout_height="fill_parent" a:layout_weight="4"/> - + - + - \ No newline at end of file diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index f1cec660..adbd788e 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -90,6 +90,8 @@ Единицы измерения углов Градусы Радианы + Грады + Обороты Определяет единицы измерения углов. Тема оформления diff --git a/res/values/arrays.xml b/res/values/arrays.xml index 09678b12..889dad94 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -50,10 +50,14 @@ @string/p_deg @string/p_rad + @string/p_grad + @string/p_turns deg rad + grad + turns \ No newline at end of file diff --git a/res/values/colors.xml b/res/values/colors.xml index a81127cf..b1e5576f 100644 --- a/res/values/colors.xml +++ b/res/values/colors.xml @@ -10,5 +10,6 @@ #ffffff99 #ffffffff #ffffffff + #ffffff99 #ff000000 \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index cc6ff1ff..d5191d3e 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -91,6 +91,8 @@ Angle Units Degrees Radians + Gradians + Turns Defines the default units for angles. Theme diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index fb443cd4..52ab6839 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -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 diff --git a/src/main/java/org/solovyev/android/view/widgets/AngleUnitsButton.java b/src/main/java/org/solovyev/android/view/widgets/AngleUnitsButton.java new file mode 100644 index 00000000..4b5723e7 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/widgets/AngleUnitsButton.java @@ -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; + } +} diff --git a/src/main/java/org/solovyev/android/view/widgets/DirectionDragButton.java b/src/main/java/org/solovyev/android/view/widgets/DirectionDragButton.java index 7a6a74fd..801ee237 100644 --- a/src/main/java/org/solovyev/android/view/widgets/DirectionDragButton.java +++ b/src/main/java/org/solovyev/android/view/widgets/DirectionDragButton.java @@ -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