Added Last Roll Highlighting

hightlights last roll base on custom color picked from the new color
picker pref

Change-Id: Iab790cd5103f955d0c8c8a0a83bcec0e7ca3a062
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-02-10 11:04:15 -05:00
parent c2ef86ea54
commit f58616cfc8
5 changed files with 240 additions and 1 deletions

View File

@@ -32,5 +32,7 @@
<string name="tens_count_twice">Tens count twice</string>
<string name="roll_mod">Roll Modifier</string>
<string name="roll_mod_msg">Calculate Roll + Modifier</string>
<string name="higlight_color">Highlight Color</string>
<string name="higlight_color_msg">Highlights your last roll in a custom color</string>
</resources>

View File

@@ -12,6 +12,11 @@
android:summary="@string/roll_mod_msg"
android:title="@string/roll_mod" />
<com.TwentyCode.android.ExaltedDice.ColorDialogPreference
android:key="color"
android:summary="@string/higlight_color_msg"
android:title="@string/higlight_color" />
<PreferenceCategory android:title="@string/successes" >
</PreferenceCategory>

View File

@@ -0,0 +1,217 @@
/**
* ColorDialogPreference.java
* @date Feb 6, 2012
* @author Casper Wakkers
*/
package com.TwentyCode.android.ExaltedDice;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* The ColorDialogPreference class is responsible for displaying a color
* palet where the user can select a color from.
* @author Casper Wakkers
*/
public class ColorDialogPreference extends DialogPreference {
/**
* Inner class representing the color chooser.
*/
private class ColorPickerView extends View {
private static final int CENTER_X = 100;
private static final int CENTER_Y = 100;
private static final int CENTER_RADIUS = 32;
private Paint paint = null;
private Paint centerPaint = null;
private boolean trackingCenter = false;
private final int[] colors = new int[] {
0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
0xFFFFFF00, 0xFFFF0000 };
/**
* @param context
* @param listener
* @param color
*/
ColorPickerView(Context context, OnColorChangedListener listener,
int color) {
super(context);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setShader(new SweepGradient(0, 0, colors, null));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(32);
centerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
centerPaint.setColor(color);
centerPaint.setStrokeWidth(5);
}
/**
* @param s
* @param d
* @param p
* @return
*/
private int ave(int s, int d, float p) {
return s + Math.round(p * (d - s));
}
/**
* @param colors
* @param unit
* @return
*/
private int interpColor(int colors[], float unit) {
if (unit <= 0) {
return colors[0];
}
if (unit >= 1) {
return colors[colors.length - 1];
}
float p = unit * (colors.length - 1);
int i = (int) p;
p -= i;
// now p is just the fractional part [0...1) and i is the index
int c0 = colors[i];
int c1 = colors[i + 1];
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);
return Color.argb(a, r, g, b);
}
/**
* {@inheritDoc}
*/
protected void onDraw(Canvas canvas) {
int centerX = getRootView().getWidth()/2 -
(int)(paint.getStrokeWidth()/2);
float r = CENTER_X - paint.getStrokeWidth()*0.5f;
canvas.translate(centerX, CENTER_Y);
canvas.drawOval(new RectF(-r, -r, r, r), paint);
canvas.drawCircle(0, 0, CENTER_RADIUS, centerPaint);
if (trackingCenter) {
int c = centerPaint.getColor();
centerPaint.setStyle(Paint.Style.STROKE);
centerPaint.setAlpha(0x80);
canvas.drawCircle(0, 0, CENTER_RADIUS +
centerPaint.getStrokeWidth(), centerPaint);
centerPaint.setStyle(Paint.Style.FILL);
centerPaint.setColor(c);
}
}
/**
* {@inheritDoc}
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getRootView().getWidth();
if (width == 0) {
width = CENTER_X*2 + 50;
}
setMeasuredDimension(width, CENTER_Y*2);
}
/**
* {@inheritDoc}
*/
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX() - getRootView().getWidth()/2;
float y = event.getY() - CENTER_Y;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
float angle = (float) java.lang.Math.atan2(y, x);
// need to turn angle [-PI ... PI] into unit [0....1]
float unit = (float)(angle / (2 * Math.PI));
if (unit < 0) {
unit += 1;
}
centerPaint.setColor(interpColor(colors, unit));
invalidate();
break;
case MotionEvent.ACTION_UP:
ColorDialogPreference.this.color = centerPaint.getColor();
break;
}
return true;
}
}
/**
* Interface describing a color change listener.
*/
public interface OnColorChangedListener {
/**
* Method colorChanged is called when a new color is selected.
* @param color new color.
*/
void colorChanged(int color);
}
private int color = 0;
// Color change listener.
private OnColorChangedListener dialogColorChangedListener = null;
/**
* ColorDialogPreference constructor.
* @param context of this class.
* @param attrs custom xml attributes.
*/
public ColorDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* {@inheritDoc}
*/
protected void onDialogClosed(boolean positiveResult) {
// Persist the color after the ok button is clicked.
if (positiveResult && color != 0) {
persistInt(color);
}
super.onDialogClosed(positiveResult);
}
/**
* {@inheritDoc}
*/
protected void onPrepareDialogBuilder(Builder builder) {
dialogColorChangedListener = new OnColorChangedListener() {
/**
* {@inheritDoc}
*/
public void colorChanged(int c) {
ColorDialogPreference.this.color = c;
}
};
int color = getSharedPreferences().getInt(getKey(), Color.WHITE);
builder.setView(new ColorPickerView(getContext(),
dialogColorChangedListener, color));
super.onPrepareDialogBuilder(builder);
}
}

View File

@@ -8,6 +8,8 @@ package com.TwentyCode.android.ExaltedDice;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -25,6 +27,7 @@ public class RollHistoryDatabaseAdapter extends BaseAdapter {
private String mGameName;
private LayoutInflater mInflater;
private int mCount;
private SharedPreferences mSettings;
/**
* Creates a new RollHistoryDatabaseAdapter
@@ -36,6 +39,7 @@ public class RollHistoryDatabaseAdapter extends BaseAdapter {
mDb = db;
mInflater = LayoutInflater.from(context);
mCount = mDb.getGameRollCount(mGameId);
mSettings = context.getSharedPreferences(Settings.SETTINGS, Context.MODE_WORLD_WRITEABLE);
}
/**
@@ -91,6 +95,17 @@ public class RollHistoryDatabaseAdapter extends BaseAdapter {
holder.mRoll.setText("Rolled: "+roll.getAsInteger(Database.KEY_NUMBER) + " "+roll.getAsString(Database.KEY_D_TYPE) +" "+ roll.getAsString(Database.KEY_MOD).replace("'", ""));
holder.mStats.setText(roll.getAsString(Database.KEY_LOG));
holder.mRolled.setText(roll.getAsString(Database.KEY_ROLLED));
if(position + 1 == mCount){
holder.mRoll.setTextColor(mSettings.getInt(Settings.KEY_COLOR, Color.WHITE));
holder.mStats.setTextColor(mSettings.getInt(Settings.KEY_COLOR, Color.WHITE));
holder.mRolled.setTextColor(mSettings.getInt(Settings.KEY_COLOR, Color.WHITE));
} else {
holder.mRoll.setTextColor(Color.WHITE);
holder.mStats.setTextColor(Color.WHITE);
holder.mRolled.setTextColor(Color.WHITE);
}
return convertView;
}

View File

@@ -34,7 +34,7 @@ public class Settings extends PreferenceActivity implements OnPreferenceClickLis
public static final String KEY_CALC_SUCCESSES = "calc_successes";
public static final String KEY_TENS_COUNT_TWICE = "tens_count_twice";
public static final String KEY_ROLL_MOD = "roll_mod";
// public static final String KEY_ = "";
public static final String KEY_COLOR = "color";
// public static final String KEY_ = "";
// public static final String KEY_ = "";
// public static final String KEY_ = "";