This commit is contained in:
serso
2011-12-08 02:43:55 +04:00
parent 3c07912ce5
commit 462906c237
26 changed files with 362 additions and 255 deletions

View File

@@ -12,7 +12,6 @@ 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;
@@ -27,21 +26,18 @@ public class AngleUnitsButton extends DirectionDragButton {
super(context, attrs);
}
@Nullable
@Override
protected TextPaint initUpDownTextPaint(@Nullable Paint paint, @NotNull DragDirection direction) {
final TextPaint result = super.initUpDownTextPaint(paint, direction);
protected void initDirectionTextPaint(@NotNull Paint basePaint,
@NotNull DirectionTextData directionTextData,
@NotNull Resources resources) {
super.initDirectionTextPaint(basePaint, directionTextData, resources);
if (result != null) {
final Resources resources = getResources();
if ( CalculatorEngine.instance.getEngine().getAngleUnits().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));
result.setAlpha(getDefaultDirectionTextAlpha());
}
final TextPaint directionTextPaint = directionTextData.getPaint();
if (CalculatorEngine.instance.getEngine().getAngleUnits().name().equals(directionTextData.getText())) {
directionTextPaint.setColor(resources.getColor(R.color.selected_angle_unit_text_color));
} else {
directionTextPaint.setColor(resources.getColor(R.color.default_text_color));
directionTextPaint.setAlpha(getDefaultDirectionTextAlpha());
}
return result;
}
}

View File

@@ -18,6 +18,9 @@ import org.solovyev.android.calculator.R;
import org.solovyev.common.utils.Point2d;
import org.solovyev.common.utils.StringUtils;
import java.util.EnumMap;
import java.util.Map;
/**
* User: serso
* Date: 7/17/11
@@ -31,7 +34,10 @@ public class DirectionDragButton extends DragButton {
@Nullable
private String textMiddle;
private static class DirectionTextData {
protected static class DirectionTextData {
@NotNull
private final GuiDragDirection guiDragDirection;
@NotNull
private String text;
@@ -42,10 +48,16 @@ public class DirectionDragButton extends DragButton {
@NotNull
private TextPaint paint;
private DirectionTextData(@NotNull String text) {
private DirectionTextData(@NotNull GuiDragDirection guiDragDirection, @NotNull String text) {
this.guiDragDirection = guiDragDirection;
this.text = text;
}
@NotNull
public GuiDragDirection getGuiDragDirection() {
return guiDragDirection;
}
@NotNull
public String getText() {
return text;
@@ -74,36 +86,106 @@ public class DirectionDragButton extends DragButton {
}
}
@Nullable
protected static enum GuiDragDirection {
up(DragDirection.up) {
@Override
public int getAttributeId() {
return R.styleable.DragButton_textUp;
}
@NotNull
@Override
public Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, int w, int h) {
return getUpDownTextPosition(paint, basePaint, text, baseText, 1, w, h);
}
},
down(DragDirection.down) {
@Override
public int getAttributeId() {
return R.styleable.DragButton_textDown;
}
@NotNull
@Override
public Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, int w, int h) {
return getUpDownTextPosition(paint, basePaint, text, baseText, -1, w, h);
}
},
left(DragDirection.left) {
@Override
public int getAttributeId() {
return R.styleable.DragButton_textLeft;
}
@NotNull
@Override
public Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, int w, int h) {
throw new UnsupportedOperationException("Not implemented yet!");
}
}/*,
right(DragDirection.right) {
@Override
public int getAttributeId() {
return 0;
}
}*/;
@NotNull
private final DragDirection dragDirection;
GuiDragDirection(@NotNull DragDirection dragDirection) {
this.dragDirection = dragDirection;
}
public abstract int getAttributeId();
@NotNull
public abstract Point2d getTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, int w, int h);
@NotNull
private static Point2d getUpDownTextPosition(@NotNull Paint paint, @NotNull Paint basePaint, @NotNull CharSequence text, CharSequence baseText, float direction, int w, int h) {
final Point2d result = new Point2d();
float width = paint.measureText(text.toString() + " ");
result.setX(w - width);
float selfHeight = paint.ascent() + paint.descent();
basePaint.measureText(StringUtils.getNotEmpty(baseText, "|"));
if (direction < 0) {
result.setY(h / 2 + h / 3 - selfHeight / 2);
} else {
result.setY(h / 2 - h / 3 - selfHeight / 2);
}
return result;
}
@Nullable
public static GuiDragDirection valueOf ( @NotNull DragDirection dragDirection ) {
for (GuiDragDirection guiDragDirection : values()) {
if ( guiDragDirection.dragDirection == dragDirection ) {
return guiDragDirection;
}
}
return null;
}
}
@NotNull
private final Map<GuiDragDirection, DirectionTextData> directionTextDataMap = new EnumMap<GuiDragDirection, DirectionTextData>(GuiDragDirection.class);
@NotNull
private Float directionTextScale = DEFAULT_DIRECTION_TEXT_SCALE;
private boolean initialized = false;
public DirectionDragButton(Context context, @NotNull AttributeSet attrs) {
super(context, attrs, false);
init(context, attrs);
}
@Nullable
public String getDirectionText(@NotNull DragDirection direction) {
final String result;
switch (direction) {
case up:
result = this.getTextUp();
break;
case down:
result = this.getTextDown();
break;
default:
result = null;
break;
}
return result;
}
private void init(@NotNull Context context, @NotNull AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, org.solovyev.android.calculator.R.styleable.DragButton);
@@ -111,16 +193,24 @@ public class DirectionDragButton extends DragButton {
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.DragButton_textUp:
this.textUp = a.getString(attr);
break;
case R.styleable.DragButton_textDown:
this.textDown = a.getString(attr);
break;
case R.styleable.DragButton_directionTextScale:
this.directionTextScale = Float.valueOf(a.getString(attr));
break;
final String attrValue = a.getString(attr);
if (!StringUtils.isEmpty(attrValue)) {
switch (attr) {
case R.styleable.DragButton_directionTextScale:
this.directionTextScale = Float.valueOf(attrValue);
break;
default:
// try drag direction text
for (GuiDragDirection guiDragDirection : GuiDragDirection.values()) {
if ( guiDragDirection.getAttributeId() == attr ) {
this.directionTextDataMap.put(guiDragDirection, new DirectionTextData(guiDragDirection, attrValue));
break;
}
}
break;
}
}
}
@@ -128,95 +218,55 @@ public class DirectionDragButton extends DragButton {
this.textMiddle = String.valueOf(getText());
super.init(context);
initialized = true;
}
@Override
protected void measureText() {
super.measureText();
final Paint basePaint = getPaint();
if (initialized) {
final Paint basePaint = getPaint();
final Resources resources = getResources();
if (textUp != null) {
initUpDownTextPaint(basePaint, DragDirection.up);
textUpPosition = getTextPosition(upTextPaint, basePaint, textUp, getText(), 1, getWidth(), getHeight(), getDirectionTextScale());
}
for (DirectionTextData directionTextData : directionTextDataMap.values()) {
initDirectionTextPaint(basePaint, directionTextData, resources);
if (textDown != null) {
initUpDownTextPaint(basePaint, DragDirection.down);
textDownPosition = getTextPosition(downTextPaint, basePaint, textDown, getText(), -1, getWidth(), getHeight(), getDirectionTextScale());
}
final GuiDragDirection guiDragDirection = directionTextData.getGuiDragDirection();
final String directionText = directionTextData.getText();
final Paint directionPaint = directionTextData.getPaint();
/*if (textDownPosition != null && textUpPosition != null) {
if (textDownPosition.getX() > textUpPosition.getX()) {
textDownPosition.setX(textUpPosition.getX());
} else {
textUpPosition.setX(textDownPosition.getX());
directionTextData.setPosition(guiDragDirection.getTextPosition(directionPaint, basePaint, directionText, getText(), getWidth(), getHeight()));
}
}*/
}
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();
float width = paint.measureText(text.toString() + " ");
result.setX(w - width);
float selfHeight = paint.ascent() + paint.descent();
basePaint.measureText(StringUtils.getNotEmpty(baseText, "|"));
if (direction < 0) {
result.setY(h / 2 + h / 3 - selfHeight / 2);
} else {
result.setY(h / 2 - h / 3 - selfHeight / 2);
}
return result;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (textUp != null && textUpPosition != null) {
initUpDownTextPaint(null, DragDirection.up);
canvas.drawText(textUp, 0, textUp.length(), textUpPosition.getX(), textUpPosition.getY(), upTextPaint);
}
if (textDown != null && textDownPosition != null) {
initUpDownTextPaint(null, DragDirection.down);
canvas.drawText(textDown, 0, textDown.length(), textDownPosition.getX(), textDownPosition.getY(), downTextPaint);
}
}
@Nullable
protected TextPaint initUpDownTextPaint(@Nullable Paint paint, @NotNull DragDirection direction) {
if (paint == null) {
paint = getPaint();
}
final TextPaint 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;
}
return null;
for (DirectionTextData directionTextData : directionTextDataMap.values()) {
initDirectionTextPaint(paint, directionTextData, resources);
final String text = directionTextData.getText();
final Point2d position = directionTextData.getPosition();
canvas.drawText(text, 0, text.length(), position.getX(), position.getY(), directionTextData.getPaint());
}
}
@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(getDefaultDirectionTextAlpha());
result.setTextSize(basePaint.getTextSize() * directionTextScale);
protected void initDirectionTextPaint(@NotNull Paint basePaint,
@NotNull DirectionTextData directionTextData,
@NotNull Resources resources) {
final TextPaint directionTextPaint = new TextPaint(basePaint);
return result;
directionTextPaint.setColor(resources.getColor(R.color.button_text_color));
directionTextPaint.setAlpha(getDefaultDirectionTextAlpha());
directionTextPaint.setTextSize(basePaint.getTextSize() * getDirectionTextScale());
directionTextData.setPaint(directionTextPaint);
}
protected static int getDefaultDirectionTextAlpha() {
@@ -225,12 +275,12 @@ public class DirectionDragButton extends DragButton {
@Nullable
public String getTextUp() {
return textUp;
return getText(GuiDragDirection.up);
}
@Nullable
public String getTextDown() {
return textDown;
return getText(GuiDragDirection.down);
}
@Nullable
@@ -240,22 +290,20 @@ public class DirectionDragButton extends DragButton {
@Nullable
public String getText(@NotNull DragDirection direction) {
final String result;
if (direction == DragDirection.up) {
result = getTextUp();
} else if (direction == DragDirection.down) {
result = getTextDown();
} else {
result = null;
}
return result;
final GuiDragDirection guiDragDirection = GuiDragDirection.valueOf(direction);
return guiDragDirection == null ? null : getText(guiDragDirection);
}
@Nullable
private String getText(@NotNull GuiDragDirection direction) {
DirectionTextData directionTextData = this.directionTextDataMap.get(direction);
return directionTextData == null ? null : directionTextData.getText();
}
@NotNull
public Float getDirectionTextScale() {
return directionTextScale == null ? DEFAULT_DIRECTION_TEXT_SCALE : directionTextScale;
return directionTextScale;
}
}

View File

@@ -20,6 +20,7 @@ import org.solovyev.common.NumberMapper;
public class VibratorContainer implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String HAPTIC_FEEDBACK_P_KEY = "org.solovyev.android.calculator.CalculatorModel_haptic_feedback";
public static final boolean HAPTIC_FEEDBACK_DEFAULT = false;
public static final String HAPTIC_FEEDBACK_DURATION_P_KEY = "org.solovyev.android.calculator.CalculatorActivity_calc_haptic_feedback_duration_key";
private final static NumberMapper<Long> mapper = new NumberMapper<Long>(Long.class);
@@ -50,7 +51,7 @@ public class VibratorContainer implements SharedPreferences.OnSharedPreferenceCh
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
if (preferences.getBoolean(HAPTIC_FEEDBACK_P_KEY, false)) {
if (preferences.getBoolean(HAPTIC_FEEDBACK_P_KEY, VibratorContainer.HAPTIC_FEEDBACK_DEFAULT)) {
//noinspection ConstantConditions
this.time = getScaledValue(mapper.parseValue(preferences.getString(HAPTIC_FEEDBACK_DURATION_P_KEY, mapper.formatValue(getScaledValue(defaultVibrationTime)))));
} else {