deg/rad button added

This commit is contained in:
Sergey Solovyev 2011-11-22 15:11:16 +04:00
parent 18dbd4c9df
commit b140551487
10 changed files with 148 additions and 34 deletions

View File

@ -52,12 +52,14 @@
a:layout_height="fill_parent" a:layout_height="fill_parent"
a:layout_weight="4"/> a:layout_weight="4"/>
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.AngleUnitsButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/clearButton" a:id="@+id/clearButton"
calc:textUp="deg"
a:text="@string/c_clear" a:text="@string/c_clear"
calc:textDown="rad"
a:textStyle="bold" a:textStyle="bold"
style="?controlImageButtonStyle" style="?controlButtonStyle"
a:onClick="clearButtonClickHandler" a:onClick="clearButtonClickHandler"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent" a:layout_height="fill_parent"

View File

@ -46,12 +46,14 @@
a:layout_height="fill_parent" a:layout_height="fill_parent"
a:layout_weight="4"/> a:layout_weight="4"/>
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.AngleUnitsButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/clearButton" a:id="@+id/clearButton"
calc:textUp="deg"
a:text="@string/c_clear" a:text="@string/c_clear"
calc:textDown="rad"
a:textStyle="bold" a:textStyle="bold"
style="?controlImageButtonStyle" style="?controlButtonStyle"
a:onClick="clearButtonClickHandler" a:onClick="clearButtonClickHandler"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent" a:layout_height="fill_parent"

View File

@ -6,10 +6,12 @@
~ or visit http://se.solovyev.org ~ or visit http://se.solovyev.org
--> -->
<org.solovyev.android.view.widgets.ColorButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.AngleUnitsButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/clearButton" a:id="@+id/clearButton"
calc:textUp="deg"
a:text="@string/c_clear" a:text="@string/c_clear"
calc:textDown="rad"
a:textStyle="bold" a:textStyle="bold"
style="?controlImageButtonStyle" style="?controlButtonStyle"
a:onClick="clearButtonClickHandler"/> a:onClick="clearButtonClickHandler"/>

View File

@ -90,6 +90,8 @@
<string name="c_calc_angle_units">Единицы измерения углов</string> <string name="c_calc_angle_units">Единицы измерения углов</string>
<string name="p_deg">Градусы</string> <string name="p_deg">Градусы</string>
<string name="p_rad">Радианы</string> <string name="p_rad">Радианы</string>
<string name="p_grad">Грады</string>
<string name="p_turns">Обороты</string>
<string name="c_angle_units_summary">Определяет единицы измерения углов.</string> <string name="c_angle_units_summary">Определяет единицы измерения углов.</string>
<string name="c_calc_theme">Тема оформления</string> <string name="c_calc_theme">Тема оформления</string>

View File

@ -50,10 +50,14 @@
<string-array name="p_angle_units_names"> <string-array name="p_angle_units_names">
<item>@string/p_deg</item> <item>@string/p_deg</item>
<item>@string/p_rad</item> <item>@string/p_rad</item>
<item>@string/p_grad</item>
<item>@string/p_turns</item>
</string-array> </string-array>
<string-array name="p_angle_units"> <string-array name="p_angle_units">
<item>deg</item> <item>deg</item>
<item>rad</item> <item>rad</item>
<item>grad</item>
<item>turns</item>
</string-array> </string-array>
</resources> </resources>

View File

@ -10,5 +10,6 @@
<color name="button_operator_text_color">#ffffff99</color> <color name="button_operator_text_color">#ffffff99</color>
<color name="button_ce_text_color">#ffffffff</color> <color name="button_ce_text_color">#ffffffff</color>
<color name="default_text_color">#ffffffff</color> <color name="default_text_color">#ffffffff</color>
<color name="selected_angle_unit_text_color">#ffffff99</color>
<color name="default_background">#ff000000</color> <color name="default_background">#ff000000</color>
</resources> </resources>

View File

@ -91,6 +91,8 @@
<string name="c_calc_angle_units">Angle Units</string> <string name="c_calc_angle_units">Angle Units</string>
<string name="p_deg">Degrees</string> <string name="p_deg">Degrees</string>
<string name="p_rad">Radians</string> <string name="p_rad">Radians</string>
<string name="p_grad">Gradians</string>
<string name="p_turns">Turns</string>
<string name="c_angle_units_summary">Defines the default units for angles.</string> <string name="c_angle_units_summary">Defines the default units for angles.</string>
<string name="c_calc_theme">Theme</string> <string name="c_calc_theme">Theme</string>

View File

@ -22,6 +22,7 @@ import android.view.*;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import jscl.AngleUnits;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.math.MathType;
@ -121,6 +122,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
equalsButton.setOnDragListener(evalOnDragListener); 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); final DragButton varsButton = (DragButton) findViewById(R.id.varsButton);
if (varsButton != null) { if (varsButton != null) {
final OnDragListener varsOnDragListener = new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences); 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); 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 { private class VarsDragProcessor implements SimpleOnDragListener.DragProcessor {
@Override @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; private Point2d textDownPosition;
@NotNull @NotNull
private TextPaint upDownTextPaint; private TextPaint upTextPaint;
@NotNull
private TextPaint downTextPaint;
@Nullable @Nullable
private Float directionTextScale = DEFAULT_DIRECTION_TEXT_SCALE; private Float directionTextScale = DEFAULT_DIRECTION_TEXT_SCALE;
@ -107,14 +110,15 @@ public class DirectionDragButton extends DragButton {
super.measureText(); super.measureText();
final Paint basePaint = getPaint(); final Paint basePaint = getPaint();
initUpDownTextPaint(basePaint);
if (textUp != null) { 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) { 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) { /*if (textDownPosition != null && textUpPosition != null) {
@ -150,28 +154,44 @@ public class DirectionDragButton extends DragButton {
public void onDraw(Canvas canvas) { public void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
initUpDownTextPaint(null);
if (textUp != null && textUpPosition != 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) { 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) { if (paint == null) {
paint = getPaint(); paint = getPaint();
} }
final Resources resources = getResources(); 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); return null;
upDownTextPaint.setColor(resources.getColor(R.color.button_text_color)); }
upDownTextPaint.setAlpha(150);
upDownTextPaint.setTextSize(paint.getTextSize() * getDirectionTextScale()); @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 @Nullable