Some refactoring and change make text more legible
Refactoring to hopefully increase performance (should switch to OpenGL some day) Emotion labels are now uglier, but more legible`
This commit is contained in:
parent
4738fda939
commit
0b98db8e39
4 changed files with 112 additions and 84 deletions
|
@ -37,7 +37,7 @@
|
|||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package com.rubenvandeven.emotionhero;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
/**
|
||||
* Created by ruben on 16/06/17.
|
||||
* see: http://blog.danielnadeau.io/2012/01/android-canvas-beginners-tutorial.html
|
||||
*/
|
||||
public class PanelThread extends Thread {
|
||||
private SurfaceHolder _surfaceHolder;
|
||||
private ScenarioView _panel;
|
||||
private boolean _run = false;
|
||||
|
||||
|
||||
public PanelThread(SurfaceHolder surfaceHolder, ScenarioView panel) {
|
||||
_surfaceHolder = surfaceHolder;
|
||||
_panel = panel;
|
||||
}
|
||||
|
||||
|
||||
public void setRunning(boolean run) { //Allow us to stop the thread
|
||||
_run = run;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
Canvas c;
|
||||
while (_run) { //When setRunning(false) occurs, _run is
|
||||
c = null; //set to false and loop ends, stopping thread
|
||||
|
||||
try {
|
||||
c = _surfaceHolder.lockCanvas(null);
|
||||
synchronized (_surfaceHolder) {
|
||||
//Insert methods to modify positions of items in onDraw()
|
||||
_panel.postInvalidate();
|
||||
}
|
||||
} finally {
|
||||
if (c != null) {
|
||||
_surfaceHolder.unlockCanvasAndPost(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -42,6 +42,7 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
private Map<Emotion, Paint> emoPaints = new HashMap<>();
|
||||
private Map<Emotion, Paint> emoOutlinePaints = new HashMap<>();
|
||||
private Map<Emotion, Paint> emoScoredPaints = new HashMap<>();
|
||||
private Map<Emotion, Path> emoNamePaths = new HashMap<>();
|
||||
private Paint mainPaint = new Paint();
|
||||
private Paint attrScorePaint = new Paint();
|
||||
private Paint attrScoreLinePaint = new Paint();
|
||||
|
@ -61,48 +62,10 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
|
||||
public boolean noFace = false;
|
||||
|
||||
// see: http://blog.danielnadeau.io/2012/01/android-canvas-beginners-tutorial.html
|
||||
class PanelThread extends Thread {
|
||||
private SurfaceHolder _surfaceHolder;
|
||||
private ScenarioView _panel;
|
||||
private boolean _run = false;
|
||||
|
||||
|
||||
public PanelThread(SurfaceHolder surfaceHolder, ScenarioView panel) {
|
||||
_surfaceHolder = surfaceHolder;
|
||||
_panel = panel;
|
||||
}
|
||||
|
||||
|
||||
public void setRunning(boolean run) { //Allow us to stop the thread
|
||||
_run = run;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
Canvas c;
|
||||
while (_run) { //When setRunning(false) occurs, _run is
|
||||
c = null; //set to false and loop ends, stopping thread
|
||||
|
||||
try {
|
||||
c = _surfaceHolder.lockCanvas(null);
|
||||
synchronized (_surfaceHolder) {
|
||||
//Insert methods to modify positions of items in onDraw()
|
||||
postInvalidate();
|
||||
}
|
||||
} finally {
|
||||
if (c != null) {
|
||||
_surfaceHolder.unlockCanvasAndPost(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ScenarioView(Context context, Scenario s) {
|
||||
super(context);
|
||||
// setLayerType(LAYER_TYPE_HARDWARE, null); // doesn't seem to make a difference?
|
||||
getHolder().addCallback(this);
|
||||
_scenario = s;
|
||||
|
||||
|
@ -124,7 +87,7 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
|
||||
linePaint.setColor(Color.GRAY);
|
||||
linePaint.setStrokeWidth(5);
|
||||
attrScorePaint.setColor(Color.GRAY);
|
||||
attrScorePaint.setColor(Color.rgb(190,190,190));
|
||||
missedPaint.setColor(Color.DKGRAY);
|
||||
|
||||
attrScoreLinePaint.setColor(Color.DKGRAY);
|
||||
|
@ -160,25 +123,22 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
// emoScoredPaint.setShadowLayer(10,0,0,Color.BLACK);
|
||||
emoScoredPaints.put(emotion, emoScoredPaint);
|
||||
|
||||
setLayerType(LAYER_TYPE_SOFTWARE, emoPaint);
|
||||
setLayerType(LAYER_TYPE_SOFTWARE, emoPaintOutline);
|
||||
setLayerType(LAYER_TYPE_SOFTWARE, emoScoredPaint);
|
||||
emoNamePaths.put(emotion, new Path());
|
||||
|
||||
// was probably here for anti-aliasing? can't quite remember (I should make more notes!)
|
||||
// setLayerType(LAYER_TYPE_SOFTWARE, emoPaint);
|
||||
// setLayerType(LAYER_TYPE_SOFTWARE, emoPaintOutline);
|
||||
// setLayerType(LAYER_TYPE_SOFTWARE, emoScoredPaint);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas) {
|
||||
if(drawOverlay) {
|
||||
canvas.drawColor(0x770000FF);
|
||||
}
|
||||
|
||||
if(matrix != null) {
|
||||
canvas.concat(matrix);
|
||||
}
|
||||
|
||||
|
||||
//do drawing stuff here.
|
||||
|
||||
if (noFace)
|
||||
|
@ -187,6 +147,43 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
float height = canvas.getHeight();
|
||||
float width = canvas.getWidth();
|
||||
|
||||
if(_scenario != null)
|
||||
{
|
||||
|
||||
float hitWidth = width*(_scenario.getHitPercentage()/100);
|
||||
float misWidth = width*(_scenario.getMissedPercentage()/100);
|
||||
float bonusWidth = width*(_scenario.getBonusPercentage()/100);
|
||||
float bonus =_scenario.getBonusTotalValue();
|
||||
|
||||
String scoreText = String.format("%1$.0f", _scenario.getHitTotalValue() + bonus);
|
||||
Rect scoreTextBounds = new Rect();
|
||||
scorePaint.getTextBounds(scoreText, 0, scoreText.length(), scoreTextBounds);
|
||||
canvas.drawText(scoreText, hitWidth, height*0.95f-scoreTextBounds.height(), scorePaint);
|
||||
|
||||
|
||||
/*if(bonus > 0) {
|
||||
String bonusText = String.format("+ %1$.0f", bonus);
|
||||
Rect bonusTextBounds = new Rect();
|
||||
bonusScorePaint.getTextBounds(bonusText, 0, bonusText.length(), bonusTextBounds);
|
||||
canvas.drawText(bonusText, hitWidth+scoreTextBounds.width()+3, height*0.95f-bonusTextBounds.height()-10, bonusScorePaint);
|
||||
}*/
|
||||
|
||||
|
||||
canvas.drawRect(0, height*0.95f, width, height, mainPaint);
|
||||
float stepSize = width / _scenario.targets.size();
|
||||
for(int i = _scenario.targets.size(); i > 0; i--) {
|
||||
canvas.drawLine(stepSize*i, height*0.95f, stepSize*i, height, attrScoreLinePaint);
|
||||
}
|
||||
canvas.drawRect(0, height*0.95f, hitWidth, height, emoPaints.get(Emotion.JOY)); // paint: yellow
|
||||
canvas.drawRect(hitWidth, height*0.95f, hitWidth+misWidth, height, missedPaint);
|
||||
|
||||
// canvas.drawRect(0, height*0.95f, bonusWidth, height*0.975f, bonusBarPaint);
|
||||
}
|
||||
|
||||
if(matrix != null) {
|
||||
canvas.concat(matrix);
|
||||
}
|
||||
|
||||
// bottom at 60%;
|
||||
float bottomline_height = height * BAR_POSITION;
|
||||
|
||||
|
@ -220,14 +217,15 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
// canvas.drawCircle(cx, cy, max_ball_radius * value/100, emoPaints.get(emotion.ordinal()));
|
||||
canvas.drawCircle(cx, cy, max_ball_radius * value/100, attrScorePaint);
|
||||
|
||||
Path emoNamePath = new Path();
|
||||
emoNamePath.moveTo(cx, cy + max_ball_radius * 1.55f);
|
||||
Path emoNamePath = emoNamePaths.get(emotion);
|
||||
emoNamePath.moveTo(cx - max_ball_radius * 1.25f, cy + max_ball_radius * 1.60f);
|
||||
// more curly line to draw on:
|
||||
// emoNamePath.rCubicTo(width*0.1f,0, width*0.1f, height*0.2f,width*0.2f,height*0.2f);
|
||||
emoNamePath.rLineTo(1000,1000);
|
||||
|
||||
// canvas.drawText(emotion.toString(), cx, cy + max_ball_radius * (float) 1.3, emoPaint);
|
||||
canvas.drawTextOnPath(emotion.toString(), emoNamePath, 0, 0, emoPaints.get(emotion));
|
||||
|
||||
}
|
||||
|
||||
// Draw targets:
|
||||
|
@ -262,36 +260,8 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
|
||||
}
|
||||
|
||||
canvas.restore();
|
||||
// canvas.restore(); // triggers error that there are less saves then restores?
|
||||
|
||||
float hitWidth = width*(_scenario.getHitPercentage()/100);
|
||||
float misWidth = width*(_scenario.getMissedPercentage()/100);
|
||||
float bonusWidth = width*(_scenario.getBonusPercentage()/100);
|
||||
float bonus =_scenario.getBonusTotalValue();
|
||||
|
||||
String scoreText = String.format("%1$.0f", _scenario.getHitTotalValue() + bonus);
|
||||
Rect scoreTextBounds = new Rect();
|
||||
scorePaint.getTextBounds(scoreText, 0, scoreText.length(), scoreTextBounds);
|
||||
canvas.drawText(scoreText, hitWidth, height*0.95f-scoreTextBounds.height(), scorePaint);
|
||||
|
||||
|
||||
/*if(bonus > 0) {
|
||||
String bonusText = String.format("+ %1$.0f", bonus);
|
||||
Rect bonusTextBounds = new Rect();
|
||||
bonusScorePaint.getTextBounds(bonusText, 0, bonusText.length(), bonusTextBounds);
|
||||
canvas.drawText(bonusText, hitWidth+scoreTextBounds.width()+3, height*0.95f-bonusTextBounds.height()-10, bonusScorePaint);
|
||||
}*/
|
||||
|
||||
|
||||
canvas.drawRect(0, height*0.95f, width, height, mainPaint);
|
||||
float stepSize = width / _scenario.targets.size();
|
||||
for(int i = _scenario.targets.size(); i > 0; i--) {
|
||||
canvas.drawLine(stepSize*i, height*0.95f, stepSize*i, height, attrScoreLinePaint);
|
||||
}
|
||||
canvas.drawRect(0, height*0.95f, hitWidth, height, emoPaints.get(Emotion.JOY)); // paint: yellow
|
||||
canvas.drawRect(hitWidth, height*0.95f, hitWidth+misWidth, height, missedPaint);
|
||||
|
||||
// canvas.drawRect(0, height*0.95f, bonusWidth, height*0.975f, bonusBarPaint);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -305,9 +275,16 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called when surface is created or size is changed (eg. when switching landscape/portrait)
|
||||
* @param holder
|
||||
* @param format
|
||||
* @param width
|
||||
* @param height
|
||||
*/
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
// canvas has exact same size as surface :-)
|
||||
// define matrix that skews, providing a sense of depth for the targets to come down
|
||||
mLeftTop = new Point((int) (width*0.2), height/2);
|
||||
mRightTop = new Point((int) (width*0.8), height/2);
|
||||
mLeftBot = new Point(0, height);
|
||||
|
@ -327,6 +304,11 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
, 0, 4);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SurfaceHolder getHolder() {
|
||||
return super.getHolder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
|
||||
|
@ -335,7 +317,6 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback
|
|||
// holder.setFormat(PixelFormat.TRANSPARENT);
|
||||
// holder.setFormat(PixelFormat.TRANSLUCENT);
|
||||
|
||||
// Log.e("TEST2", "Jaa2!");
|
||||
_thread = new PanelThread(getHolder(), this); //Start the thread that
|
||||
_thread.setRunning(true); //will make calls to
|
||||
_thread.start(); //onDraw()
|
||||
|
|
|
@ -7,6 +7,6 @@
|
|||
<dimen name="highscore_textsize">15sp</dimen>
|
||||
<dimen name="gametitle_textsize">12sp</dimen>
|
||||
<dimen name="scenario_scorevalue">20sp</dimen>
|
||||
<dimen name="scenario_emolabel">12sp</dimen>
|
||||
<dimen name="scenario_emolabel">20sp</dimen>
|
||||
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue