changes: themes support, scale for direction text

This commit is contained in:
serso 2011-10-15 21:11:11 +04:00
parent 91acb760f1
commit c3817e417a
28 changed files with 276 additions and 103 deletions

View File

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <shape xmlns:a="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#0f2e47" android:endColor="#3a5366" android:angle="90"/> <gradient a:startColor="#002761"
<corners android:radius="0dp" /> a:endColor="#005ce5"
a:angle="90"/>
<corners a:radius="0dp" />
</shape> </shape>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org
-->
<shape xmlns:a="http://schemas.android.com/apk/res/android">
<gradient a:startColor="#520546"
a:endColor="#8a307c"
a:angle="90"/>
<corners a:radius="0dp"/>
</shape>

View File

@ -10,5 +10,6 @@
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/divisionButton" a:text="/" a:id="@+id/divisionButton" a:text="/"
calc:textUp="√" calc:textUp="√"
style="@style/digit_button_style" calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/eightDigitButton" a:text="8" a:id="@+id/eightDigitButton" a:text="8"
calc:textUp="ln" calc:textUp="ln"
calc:textDown="" calc:textDown=""
style="@style/digit_button_style" calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,5 @@
a:text="5" a:text="5"
calc:textUp="cosh" calc:textUp="cosh"
calc:textDown="acosh" calc:textDown="acosh"
style="@style/digit_button_style" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,5 @@
a:text="4" a:text="4"
calc:textUp="sinh" calc:textUp="sinh"
calc:textDown="asinh" calc:textDown="asinh"
style="@style/digit_button_style" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -12,5 +12,7 @@
a:text="×" a:text="×"
calc:textUp="^" calc:textUp="^"
calc:textDown="^2" calc:textDown="^2"
style="@style/digit_button_style" style="?digitButtonStyle"
calc:directionTextScale="0.5"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/nineDigitButton" a:text="9" a:id="@+id/nineDigitButton" a:text="9"
calc:textDown="e" calc:textDown="e"
calc:textUp="π" calc:textUp="π"
style="@style/digit_button_style" calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -12,5 +12,5 @@
a:text="1" a:text="1"
calc:textUp="sin" calc:textUp="sin"
calc:textDown="asin" calc:textDown="asin"
style="@style/digit_button_style" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -7,6 +7,9 @@
--> -->
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.DirectionDragButton 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/plusButton" a:text="+" a:id="@+id/plusButton"
style="@style/digit_button_style" calc:directionTextScale="0.5"
a:text="+"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/roundBracketsButton" a:text="()" a:id="@+id/roundBracketsButton" a:text="()"
calc:textUp="(" calc:textUp="("
calc:textDown=")" calc:textDown=")"
style="@style/digit_button_style" calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/sevenDigitButton" a:text="7" a:id="@+id/sevenDigitButton" a:text="7"
calc:textUp="i" calc:textUp="i"
calc:textDown="" calc:textDown=""
style="@style/digit_button_style" calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,5 @@
a:id="@+id/sixDigitButton" a:text="6" a:id="@+id/sixDigitButton" a:text="6"
calc:textUp="tanh" calc:textUp="tanh"
calc:textDown="atanh" calc:textDown="atanh"
style="@style/digit_button_style" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/squareBracketsButton" a:text="[]" a:id="@+id/squareBracketsButton" a:text="[]"
calc:textUp="[" calc:textUp="["
calc:textDown="]" calc:textDown="]"
style="@style/digit_button_style" calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -10,5 +10,7 @@
a:id="@+id/subtractionButton" a:text="-" a:id="@+id/subtractionButton" a:text="-"
calc:textUp="{" calc:textUp="{"
calc:textDown="}" calc:textDown="}"
style="@style/digit_button_style" calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -12,5 +12,5 @@
a:text="3" a:text="3"
calc:textUp="tan" calc:textUp="tan"
calc:textDown="atan" calc:textDown="atan"
style="@style/digit_button_style" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -12,5 +12,5 @@
a:text="2" a:text="2"
calc:textUp="cos" calc:textUp="cos"
calc:textDown="acos" calc:textDown="acos"
style="@style/digit_button_style" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -11,5 +11,7 @@
a:id="@+id/zeroDigitButton" a:text="0" a:id="@+id/zeroDigitButton" a:text="0"
calc:textUp="." calc:textUp="."
calc:textDown="," calc:textDown=","
style="@style/digit_button_style" calc:directionTextScale="0.5"
style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -27,7 +27,7 @@
calc:textDown="@string/c_down" calc:textDown="@string/c_down"
a:layout_width="80dp" a:layout_width="80dp"
a:layout_height="80dp" a:layout_height="80dp"
style="@style/digit_button_style"/> style="?digitButtonStyle"/>
<org.solovyev.android.view.widgets.DirectionDragButton a:id="@+id/calibrationButtonRight" <org.solovyev.android.view.widgets.DirectionDragButton a:id="@+id/calibrationButtonRight"
a:text="" a:text=""
@ -35,7 +35,7 @@
calc:textDown="@string/c_down" calc:textDown="@string/c_down"
a:layout_width="80dp" a:layout_width="80dp"
a:layout_height="80dp" a:layout_height="80dp"
style="@style/digit_button_style"/> style="?digitButtonStyle"/>
<ImageView a:id="@+id/calibrationArrowRight" <ImageView a:id="@+id/calibrationArrowRight"
a:layout_gravity="right" a:layout_gravity="right"

18
res/values/arrays.xml Normal file
View File

@ -0,0 +1,18 @@
<!--
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
~ For more information, please, contact se.solovyev@gmail.com
~ or visit http://se.solovyev.org
-->
<resources>
<string-array name="p_theme_names">
<item>Default</item>
<item>Violet</item>
<item>Light blue</item>
</string-array>
<string-array name="p_theme_values">
<item>default_theme</item>
<item>violet_theme</item>
<item>light_blue_theme</item>
</string-array>
</resources>

View File

@ -3,6 +3,7 @@
<declare-styleable name="DragButton"> <declare-styleable name="DragButton">
<attr name="textUp" format="string"/> <attr name="textUp" format="string"/>
<attr name="textDown" format="string"/> <attr name="textDown" format="string"/>
<attr name="directionTextScale" format="float"/>
</declare-styleable> </declare-styleable>
<declare-styleable name="NumberRangeSeekBar"> <declare-styleable name="NumberRangeSeekBar">
@ -10,4 +11,6 @@
<attr name="steps" format="integer"/> <attr name="steps" format="integer"/>
</declare-styleable> </declare-styleable>
<attr name="digitButtonStyle" format="reference" />
</resources> </resources>

View File

@ -20,4 +20,8 @@
<string name="p_calc_color_display">true</string> <string name="p_calc_color_display">true</string>
<string name="p_calc_vars">org.solovyev.android.calculator.CalculatorModel_vars</string> <string name="p_calc_vars">org.solovyev.android.calculator.CalculatorModel_vars</string>
<string name="p_calc_theme_key">org.solovyev.android.calculator.CalculatorActivity_calc_theme</string>
<string name="p_calc_theme">default_theme</string>
</resources> </resources>

View File

@ -75,4 +75,5 @@
<string name="c_history_is_empty">History is empty!</string> <string name="c_history_is_empty">History is empty!</string>
<string name="c_app_history">History</string> <string name="c_app_history">History</string>
<string name="c_char_is_not_accepted">Character \'%s\' is not accepted in variable name!</string> <string name="c_char_is_not_accepted">Character \'%s\' is not accepted in variable name!</string>
<string name="c_calc_theme">Theme</string>
</resources> </resources>

View File

@ -15,13 +15,23 @@
<item name="android:textSize">20dp</item> <item name="android:textSize">20dp</item>
<item name="android:background">@drawable/button</item> <item name="android:background">@drawable/button</item>
<item name="android:layout_marginLeft">1dp</item> <item name="android:layout_marginLeft">1dp</item>
<item name="android:layout_marginBottom">1dp</item>
</style> </style>
<style name="digit_button_style" parent="button_style"> <style name="default_digit_button_style" parent="button_style">
<item name="android:background">@drawable/blue_button</item> <item name="android:background">@drawable/blue_button</item>
</style> </style>
<style name="violet_digit_button_style" parent="button_style">
<item name="android:background">@drawable/violet_button</item>
</style>
<style name="light_blue_digit_button_style" parent="button_style">
<item name="android:background">@drawable/light_blue_button</item>
</style>
<style name="control_button_style" parent="button_style"/> <style name="control_button_style" parent="button_style"/>
<style name="control_image_button_style" parent="control_button_style"> <style name="control_image_button_style" parent="control_button_style">
<item name="android:padding">6dp</item> <item name="android:padding">6dp</item>
</style> </style>
@ -56,5 +66,16 @@
<item name="android:layout_height">match_parent</item> <item name="android:layout_height">match_parent</item>
</style> </style>
<style name="default_theme">
<item name="digitButtonStyle">@style/default_digit_button_style</item>
</style>
<style name="violet_theme">
<item name="digitButtonStyle">@style/violet_digit_button_style</item>
</style>
<style name="light_blue_theme">
<item name="digitButtonStyle">@style/light_blue_digit_button_style</item>
</style>
</resources> </resources>

View File

@ -11,33 +11,42 @@
range:boundaries="0;10"/> range:boundaries="0;10"/>
<android.preference.CheckBoxPreference <android.preference.CheckBoxPreference
a:key="@string/p_calc_color_display_key" a:key="@string/p_calc_color_display_key"
a:title="@string/c_calc_color_display_title" a:title="@string/c_calc_color_display_title"
a:defaultValue="true"/> a:defaultValue="true"/>
<!-- <org.solovyev.android.view.prefs.FloatRangeSeekBarPreference <ListPreference a:key="@string/p_calc_theme_key"
a:key="@string/p_drag_distance_key" a:title="@string/c_calc_theme"
a:title="Distance of drag event" a:entries="@array/p_theme_names"
a:text=" pxs" a:entryValues="@array/p_theme_values"
a:defaultValue="15;350" a:persistent="true"
range:steps="10" a:defaultValue="2"
range:boundaries="10;500"/> a:shouldDisableView="false"/>
<org.solovyev.android.view.prefs.FloatRangeSeekBarPreference
a:key="@string/p_drag_duration_key"
a:title="Duration of drag event"
a:text=" ms"
a:defaultValue="40;2500"
range:steps="10"
range:boundaries="5;4000"/>
<org.solovyev.android.view.prefs.FloatRangeSeekBarPreference <!-- <org.solovyev.android.view.prefs.FloatRangeSeekBarPreference
a:key="@string/p_drag_angle_key" a:key="@string/p_drag_distance_key"
a:title="Angle of drag event" a:title="Distance of drag event"
a:text=" degrees" a:text=" pxs"
a:defaultValue="0;45" a:defaultValue="15;350"
range:steps="5" range:steps="10"
range:boundaries="0;45"/>--> range:boundaries="10;500"/>
<org.solovyev.android.view.prefs.FloatRangeSeekBarPreference
a:key="@string/p_drag_duration_key"
a:title="Duration of drag event"
a:text=" ms"
a:defaultValue="40;2500"
range:steps="10"
range:boundaries="5;4000"/>
<org.solovyev.android.view.prefs.FloatRangeSeekBarPreference
a:key="@string/p_drag_angle_key"
a:title="Angle of drag event"
a:text=" degrees"
a:defaultValue="0;45"
range:steps="5"
range:boundaries="0;45"/>-->
</PreferenceCategory> </PreferenceCategory>

View File

@ -31,6 +31,10 @@ import org.solovyev.common.utils.history.HistoryAction;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener {
@ -54,11 +58,26 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private volatile boolean initialized; private volatile boolean initialized;
@NotNull
private String themeName;
// key: style name, value: id of style in R.class
private Map<String, Integer> styles = null;
// ids of drag buttons in R.class
private List<Integer> dragButtonIds = null;
/** /**
* Called when the activity is first created. * Called when the activity is first created.
*/ */
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
Log.d(this.getClass().getName(), "org.solovyev.android.calculator.CalculatorActivity.onCreate()");
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(preferences);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.main); setContentView(R.layout.main);
@ -68,28 +87,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
dpclRegister.clear(); dpclRegister.clear();
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(this); final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this);
final SimpleOnDragListener onDragListener = new SimpleOnDragListener(new DigitButtonDragProcessor(calculatorView), dragPreferences); setOnDragListeners(dragPreferences);
dpclRegister.addListener(onDragListener);
// todo serso: check if there is more convenient method for doing this
final R.id ids = new R.id();
for (Field field : R.id.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try {
final View view = findViewById(field.getInt(ids));
if (view instanceof DragButton) {
((DragButton) view).setOnDragListener(onDragListener);
}
} catch (IllegalArgumentException e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage());
} catch (IllegalAccessException e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage());
}
}
}
final SimpleOnDragListener historyOnDragListener = new SimpleOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(this.calculatorView), dragPreferences); final SimpleOnDragListener historyOnDragListener = new SimpleOnDragListener(new HistoryDragProcessor<CalculatorHistoryState>(this.calculatorView), dragPreferences);
((DragButton) findViewById(R.id.historyButton)).setOnDragListener(historyOnDragListener); ((DragButton) findViewById(R.id.historyButton)).setOnDragListener(historyOnDragListener);
@ -100,10 +100,68 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener); ((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);
dpclRegister.addListener(toPositionOnDragListener); dpclRegister.addListener(toPositionOnDragListener);
final SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); preferences.registerOnSharedPreferenceChangeListener(this);
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this);
this.onSharedPreferenceChanged(defaultSharedPreferences, null); this.onSharedPreferenceChanged(preferences, null);
}
private synchronized void setOnDragListeners(@NotNull SimpleOnDragListener.Preferences dragPreferences) {
final SimpleOnDragListener onDragListener = new SimpleOnDragListener(new DigitButtonDragProcessor(calculatorView), dragPreferences);
dpclRegister.addListener(onDragListener);
if (dragButtonIds == null) {
dragButtonIds = new ArrayList<Integer>();
for (Field field : R.id.class.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try {
int dragButtonId = field.getInt(R.id.class);
final View view = findViewById(dragButtonId);
if (view instanceof DragButton) {
dragButtonIds.add(dragButtonId);
}
} catch (IllegalAccessException e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage());
}
}
}
}
for (Integer dragButtonId : dragButtonIds) {
((DragButton) findViewById(dragButtonId)).setOnDragListener(onDragListener);
}
}
private synchronized void setTheme(@NotNull SharedPreferences preferences) {
if (styles == null) {
styles = new HashMap<String, Integer>();
for (Field themeField : R.style.class.getDeclaredFields()) {
int modifiers = themeField.getModifiers();
if (Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) {
try {
Log.d(this.getClass().getName(), "Style found: " + themeField.getName());
int styleId = themeField.getInt(R.style.class);
Log.d(this.getClass().getName(), "Style id: " + styleId);
styles.put(themeField.getName(), styleId);
} catch (IllegalAccessException e) {
Log.e(CalculatorActivity.class.getName(), e.getMessage());
}
}
}
}
themeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme));
Integer styleId = styles.get(themeName);
if (styleId == null) {
Log.d(this.getClass().getName(), "No saved theme found => applying default theme: " + R.style.default_theme);
styleId = R.style.default_theme;
} else {
Log.d(this.getClass().getName(), "Saved them found: " + styleId);
}
setTheme(styleId);
} }
private void init() { private void init() {
@ -130,6 +188,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@Override @Override
public void doOperation(@NotNull EditText editor) { public void doOperation(@NotNull EditText editor) {
editor.setText(s); editor.setText(s);
calculatorView.setCursorOnEnd();
} }
}); });
} }
@ -146,13 +205,13 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
try { try {
CalculatorModel.instance.init(this); CalculatorModel.instance.init(this);
} catch (EvalError evalError) { } catch (EvalError evalError) {
// todo serso: create serso runtime exception
throw new RuntimeException("Could not initialize interpreter!"); throw new RuntimeException("Could not initialize interpreter!");
} }
initialized = true; initialized = true;
} }
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
unregisterReceiver(textReceiver); unregisterReceiver(textReceiver);
@ -332,15 +391,39 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio); view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio);
} }
public void restart() {
final Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Log.d(this.getClass().getName(), "Finishing current activity!");
finish();
overridePendingTransition(0, 0);
Log.d(this.getClass().getName(), "Starting new activity!");
startActivity(intent);
}
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, @Nullable String s) { protected void onResume() {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(CalculatorActivity.this)); super.onResume();
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final String newThemeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme));
if (!newThemeName.equals(themeName)) {
restart();
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String s) {
dpclRegister.announce().onDragPreferencesChange(SimpleOnDragListener.getPreferences(preferences, this));
CalculatorModel.instance.reset(this); CalculatorModel.instance.reset(this);
final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display)); final Boolean colorExpressionsInBracketsDefault = new BooleanMapper().parseValue(this.getString(R.string.p_calc_color_display));
assert colorExpressionsInBracketsDefault != null; assert colorExpressionsInBracketsDefault != null;
this.calculatorView.getEditor().setHighlightText(sharedPreferences.getBoolean(this.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault)); this.calculatorView.getEditor().setHighlightText(preferences.getBoolean(this.getString(R.string.p_calc_color_display_key), colorExpressionsInBracketsDefault));
this.calculatorView.evaluate(); this.calculatorView.evaluate();
} }

View File

@ -13,6 +13,7 @@ import android.text.TextPaint;
import android.util.AttributeSet; import android.util.AttributeSet;
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.R;
import org.solovyev.common.utils.Point2d; import org.solovyev.common.utils.Point2d;
import org.solovyev.common.utils.StringUtils; import org.solovyev.common.utils.StringUtils;
@ -23,6 +24,9 @@ import org.solovyev.common.utils.StringUtils;
*/ */
public class DirectionDragButton extends DragButton { public class DirectionDragButton extends DragButton {
@NotNull
private final static Float DEFAULT_DIRECTION_TEXT_SCALE = 0.33f;
@Nullable @Nullable
private String textUp; private String textUp;
@ -41,6 +45,9 @@ public class DirectionDragButton extends DragButton {
@NotNull @NotNull
private TextPaint upDownTextPaint; private TextPaint upDownTextPaint;
@Nullable
private Float directionTextScale = DEFAULT_DIRECTION_TEXT_SCALE;
public DirectionDragButton(Context context, @NotNull AttributeSet attrs) { public DirectionDragButton(Context context, @NotNull AttributeSet attrs) {
super(context, attrs, false); super(context, attrs, false);
init(context, attrs); init(context, attrs);
@ -76,12 +83,15 @@ public class DirectionDragButton extends DragButton {
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
int attr = a.getIndex(i); int attr = a.getIndex(i);
switch (attr) { switch (attr) {
case org.solovyev.android.calculator.R.styleable.DragButton_textUp: case R.styleable.DragButton_textUp:
this.textUp = a.getString(attr); this.textUp = a.getString(attr);
break; break;
case org.solovyev.android.calculator.R.styleable.DragButton_textDown: case R.styleable.DragButton_textDown:
this.textDown = a.getString(attr); this.textDown = a.getString(attr);
break; break;
case R.styleable.DragButton_directionTextScale:
this.directionTextScale = Float.valueOf(a.getString(attr));
break;
} }
} }
@ -99,15 +109,15 @@ public class DirectionDragButton extends DragButton {
initUpDownTextPaint(basePaint); initUpDownTextPaint(basePaint);
if (textUp != null) { if (textUp != null) {
textUpPosition = getTextPosition(upDownTextPaint, basePaint, textUp, getText(), 1, getWidth(), getHeight()); textUpPosition = getTextPosition(upDownTextPaint, basePaint, textUp, getText(), 1, getWidth(), getHeight(), getDirectionTextScale());
} }
if (textDown != null) { if (textDown != null) {
textDownPosition = getTextPosition(upDownTextPaint, basePaint, textDown, getText(), -1, getWidth(), getHeight()); textDownPosition = getTextPosition(upDownTextPaint, basePaint, textDown, getText(), -1, getWidth(), getHeight(), getDirectionTextScale());
} }
if ( textDownPosition != null && textUpPosition != null ) { if (textDownPosition != null && textUpPosition != null) {
if ( textDownPosition.getX() > textUpPosition.getX() ) { if (textDownPosition.getX() > textUpPosition.getX()) {
textDownPosition.setX(textUpPosition.getX()); textDownPosition.setX(textUpPosition.getX());
} else { } else {
textUpPosition.setX(textDownPosition.getX()); textUpPosition.setX(textDownPosition.getX());
@ -116,7 +126,7 @@ public class DirectionDragButton extends DragButton {
} }
public static Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, float direction, int w, int h) { public static Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, float direction, int w, int h, float scale) {
final Point2d result = new Point2d(); final Point2d result = new Point2d();
float width = paint.measureText(text.toString() + " "); float width = paint.measureText(text.toString() + " ");
@ -157,15 +167,7 @@ public class DirectionDragButton extends DragButton {
upDownTextPaint = new TextPaint(paint); upDownTextPaint = new TextPaint(paint);
upDownTextPaint.setAlpha(150); upDownTextPaint.setAlpha(150);
upDownTextPaint.setTextSize(paint.getTextSize() / 3); upDownTextPaint.setTextSize(paint.getTextSize() * getDirectionTextScale());
}
private String getStyledUpDownText(@Nullable String text) {
return StringUtils.getNotEmpty(text, "&nbsp;");
}
public void setTextUp(@Nullable String textUp) {
this.textUp = textUp;
} }
@Nullable @Nullable
@ -173,19 +175,11 @@ public class DirectionDragButton extends DragButton {
return textUp; return textUp;
} }
public void setTextDown(@Nullable String textDown) {
this.textDown = textDown;
}
@Nullable @Nullable
public String getTextDown() { public String getTextDown() {
return textDown; return textDown;
} }
public void setTextMiddle(@Nullable String textMiddle) {
this.textMiddle = textMiddle;
}
@Nullable @Nullable
public String getTextMiddle() { public String getTextMiddle() {
return textMiddle; return textMiddle;
@ -197,7 +191,7 @@ public class DirectionDragButton extends DragButton {
if (direction == DragDirection.up) { if (direction == DragDirection.up) {
result = getTextUp(); result = getTextUp();
} else if ( direction == DragDirection.down ) { } else if (direction == DragDirection.down) {
result = getTextDown(); result = getTextDown();
} else { } else {
result = null; result = null;
@ -205,4 +199,10 @@ public class DirectionDragButton extends DragButton {
return result; return result;
} }
@NotNull
public Float getDirectionTextScale() {
return directionTextScale == null ? DEFAULT_DIRECTION_TEXT_SCALE : directionTextScale;
}
} }

View File

@ -7,7 +7,6 @@ package org.solovyev.android.view.widgets;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -146,8 +145,7 @@ public class SimpleOnDragListener implements OnDragListener, DragPreferencesChan
} }
@NotNull @NotNull
public static Preferences getPreferences(@NotNull Context context) { public static Preferences getPreferences(@NotNull final SharedPreferences preferences, @NotNull Context context) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final Mapper<Interval<Float>> mapper = new NumberIntervalMapper<Float>(Float.class); final Mapper<Interval<Float>> mapper = new NumberIntervalMapper<Float>(Float.class);