From 411454683daee0fe62217b7d5b137ecd7087dae5 Mon Sep 17 00:00:00 2001 From: "oskar.wiksten@gmail.com" Date: Wed, 19 Oct 2011 22:26:38 +0000 Subject: [PATCH] Allow selection of conversation replies with keyboard. Prevent FC when changing maps while animations are running (such as regeneration) git-svn-id: https://andors-trail.googlecode.com/svn/trunk@188 08aca716-68be-ccc6-4d58-36f5abd142ac --- .../res/layout/conversation_statement.xml | 4 + AndorsTrail/res/values/authors.xml | 1 + .../activity/ConversationActivity.java | 74 +++++++++++++++++-- .../AndorsTrail/controller/Controller.java | 2 +- .../controller/MovementController.java | 4 +- .../controller/VisualEffectController.java | 5 +- .../gpl/rpg/AndorsTrail/view/MainView.java | 73 +++++++++--------- 7 files changed, 118 insertions(+), 45 deletions(-) diff --git a/AndorsTrail/res/layout/conversation_statement.xml b/AndorsTrail/res/layout/conversation_statement.xml index bb41eb7..1476643 100644 --- a/AndorsTrail/res/layout/conversation_statement.xml +++ b/AndorsTrail/res/layout/conversation_statement.xml @@ -20,5 +20,9 @@ android:layout_height="wrap_content" android:text="Actor: Text" android:textSize="@dimen/conversation_textsize" + android:shadowDx="1" + android:shadowDy="1" + android:shadowRadius="1" + android:shadowColor="#000" /> diff --git a/AndorsTrail/res/values/authors.xml b/AndorsTrail/res/values/authors.xml index f097a29..f1d5bc0 100644 --- a/AndorsTrail/res/values/authors.xml +++ b/AndorsTrail/res/values/authors.xml @@ -26,6 +26,7 @@ French translation by LeSanglier and Misty Soul<br /> German translation by Bomber and Samuel Plentz<br /> English proofreading by taws34<br /> + English proofreading and testing by Stephen Stalnaker<br /> Hebrew translation by eitanbm<br /> Forum moderated by Tim Davis<br /> Forum moderated by Josh Kloos<br /> diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/ConversationActivity.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/ConversationActivity.java index f2b10c3..a0ee817 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/ConversationActivity.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/ConversationActivity.java @@ -12,17 +12,21 @@ import android.os.Parcel; import android.os.Parcelable; import android.text.Spannable; import android.text.style.ForegroundColorSpan; +import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.View.OnClickListener; +import android.view.View.OnKeyListener; import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; +import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView.BufferType; import com.gpl.rpg.AndorsTrail.AndorsTrailApplication; @@ -39,7 +43,7 @@ import com.gpl.rpg.AndorsTrail.model.actor.Player; import com.gpl.rpg.AndorsTrail.model.item.Loot; import com.gpl.rpg.AndorsTrail.resource.tiles.TileManager; -public final class ConversationActivity extends Activity { +public final class ConversationActivity extends Activity implements OnKeyListener { public static final int ACTIVITYRESULT_ATTACK = Activity.RESULT_FIRST_USER + 1; public static final int ACTIVITYRESULT_REMOVE = Activity.RESULT_FIRST_USER + 2; private static final int playerConversationColor = Color.argb(255, 0xbb, 0x22, 0x22); @@ -56,7 +60,7 @@ public final class ConversationActivity extends Activity { private ListView statementList; private Monster npc; private RadioGroup replyGroup; - private OnClickListener radioButtonListener; + private OnCheckedChangeListener radioButtonListener; private boolean displayActors = true; private final ConversationCollection conversationCollection = new ConversationCollection(); @@ -106,9 +110,9 @@ public final class ConversationActivity extends Activity { } }); - radioButtonListener = new OnClickListener() { + radioButtonListener = new OnCheckedChangeListener() { @Override - public void onClick(View v) { + public void onCheckedChanged(CompoundButton arg0, boolean arg1) { nextButton.setEnabled(true); } }; @@ -120,8 +124,64 @@ public final class ConversationActivity extends Activity { }); setPhrase(phraseID); + + statementList.setOnKeyListener(this); } - + + private int getSelectedReplyIndex() { + for (int i = 0; i < phrase.replies.length; ++i) { + final View v = replyGroup.getChildAt(i); + if (v == null) continue; + final RadioButton rb = (RadioButton) v; + if (rb.isChecked()) return i; + } + return -1; + } + + private void setSelectedReplyIndex(int i) { + if (phrase.replies == null) return; + if (phrase.replies.length <= 0) return; + if (i < 0) i = 0; + else if (i >= phrase.replies.length) i = phrase.replies.length - 1; + + View v = replyGroup.getChildAt(i); + if (v == null) return; + RadioButton rb = (RadioButton) v; + rb.setChecked(true); + } + + @Override + public boolean onKey(View arg0, int keyCode, KeyEvent event) { + if (event.getAction() != KeyEvent.ACTION_DOWN) return false; + int selectedReplyIndex = getSelectedReplyIndex(); + + switch (keyCode) { + case KeyEvent.KEYCODE_DPAD_UP: + --selectedReplyIndex; + setSelectedReplyIndex(selectedReplyIndex); + return true; + case KeyEvent.KEYCODE_DPAD_DOWN: + ++selectedReplyIndex; + setSelectedReplyIndex(selectedReplyIndex); + return true; + case KeyEvent.KEYCODE_SPACE: + case KeyEvent.KEYCODE_ENTER: + case KeyEvent.KEYCODE_DPAD_CENTER: + if (nextButton.isEnabled()) nextButton.performClick(); + return true; + case KeyEvent.KEYCODE_1: setSelectedReplyIndex(0); return true; + case KeyEvent.KEYCODE_2: setSelectedReplyIndex(1); return true; + case KeyEvent.KEYCODE_3: setSelectedReplyIndex(2); return true; + case KeyEvent.KEYCODE_4: setSelectedReplyIndex(3); return true; + case KeyEvent.KEYCODE_5: setSelectedReplyIndex(4); return true; + case KeyEvent.KEYCODE_6: setSelectedReplyIndex(5); return true; + case KeyEvent.KEYCODE_7: setSelectedReplyIndex(6); return true; + case KeyEvent.KEYCODE_8: setSelectedReplyIndex(7); return true; + case KeyEvent.KEYCODE_9: setSelectedReplyIndex(8); return true; + default: return false; + } + } + public void setPhrase(String phraseID) { this.phraseID = phraseID; if (phraseID.equalsIgnoreCase(ConversationCollection.PHRASE_CLOSE)) { @@ -200,8 +260,9 @@ public final class ConversationActivity extends Activity { RadioButton rb = new RadioButton(this); rb.setLayoutParams(layoutParams); rb.setText(r.text); - rb.setOnClickListener(radioButtonListener); + rb.setOnCheckedChangeListener(radioButtonListener); rb.setTag(r); + rb.setShadowLayer(1, 1, 1, Color.BLACK); replyGroup.addView(rb, layoutParams); } @@ -211,6 +272,7 @@ public final class ConversationActivity extends Activity { return false; } + private Reply getSelectedReply() { for (int i = 0; i < phrase.replies.length; ++i) { final View v = replyGroup.getChildAt(i); diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/Controller.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/Controller.java index 640be4c..378daea 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/Controller.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/Controller.java @@ -72,7 +72,7 @@ public final class Controller { final MainActivity act = view.mainActivity; MovementController.respawnPlayer(act.getResources(), world); act.updateStatus(); - act.mainview.notifyMapChanged(); + act.mainview.notifyMapChanged(world.model); act.message(act.getResources().getString(R.string.combat_hero_dies, lostExp)); } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java index 79e49f1..273bba6 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java @@ -49,7 +49,7 @@ public final class MovementController implements TimedMessageTask.Callback { protected void onPostExecute(Void result) { super.onPostExecute(result); view.mainActivity.clearMessages(); - view.mainActivity.mainview.notifyMapChanged(); + view.mainActivity.mainview.notifyMapChanged(model); stopMovement(); view.gameRoundController.resume(); } @@ -213,7 +213,7 @@ public final class MovementController implements TimedMessageTask.Callback { player.lastPosition.set(player.position); player.position.set(newPosition); view.combatController.setCombatSelection(null, null); - view.mainActivity.mainview.notifyPlayerMoved(); + view.mainActivity.mainview.notifyPlayerMoved(newPosition); if (handleEvents) { MapObject o = currentMap.getEventObjectAt(newPosition); diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/VisualEffectController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/VisualEffectController.java index 506d6a1..393c459 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/VisualEffectController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/VisualEffectController.java @@ -48,7 +48,8 @@ public final class VisualEffectController { if (frame >= beginFadeAtFrame && displayText != null) { this.textPaint.setAlpha(255 * (effect.lastFrame - frame) / (effect.lastFrame - beginFadeAtFrame)); } - view.redrawAreaWithEffect(area, this, tileID, textYOffset, this.textPaint); + area.topLeft.y = position.y - 1; + view.redrawAreaWithEffect(this, tileID, textYOffset, this.textPaint); } protected void onCompleted() { @@ -68,7 +69,7 @@ public final class VisualEffectController { public final Coord position; public final String displayText; - private final CoordRect area; + public final CoordRect area; private final Paint textPaint = new Paint(); private final int beginFadeAtFrame; private final VisualEffectCompletedCallback callback; diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java index 762c80e..596b590 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java @@ -53,7 +53,10 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac private final CoordRect p1x1 = new CoordRect(new Coord(), new Size(1,1)); private boolean hasSurface = false; + private PredefinedMap currentMap; + private LayeredTileMap currentTileMap; private TileCollection tiles; + private final Coord playerPosition = new Coord(); public MainView(Context context, AttributeSet attr) { super(context, attr); @@ -113,7 +116,7 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac ); if (model.currentMap != null) { - notifyMapChanged(); + notifyMapChanged(model); } redrawAll(REDRAW_ALL_SURFACE_CHANGED); @@ -175,8 +178,7 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac if (!hasSurface) return; //if (!preferences.optimizedDrawing) area = mapViewArea; - final PredefinedMap currentMap = model.currentMap; - boolean b = currentMap.isOutside(area); + boolean b = currentMap.isOutside(area); if (b) return; calculateRedrawRect(area); @@ -204,12 +206,12 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac return true; } private final Rect redrawRect = new Rect(); - public void redrawAreaWithEffect(CoordRect area, final VisualEffectAnimation effect, int tileID, int textYOffset, Paint textPaint) { + public void redrawAreaWithEffect(final VisualEffectAnimation effect, int tileID, int textYOffset, Paint textPaint) { + CoordRect area = effect.area; if (!hasSurface) return; if (shouldRedrawEverythingForVisualEffect()) area = mapViewArea; - final PredefinedMap currentMap = model.currentMap; - if (currentMap.isOutside(area)) return; + if (currentMap.isOutside(area)) return; calculateRedrawRect(area); Canvas c = null; @@ -263,10 +265,8 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac } private void doDrawRect(Canvas canvas, CoordRect area) { - final LayeredTileMap currentTileMap = model.currentTileMap; - final PredefinedMap currentMap = model.currentMap; - - drawMapLayer(canvas, area, currentTileMap.layers[LayeredTileMap.LAYER_GROUND]); + + drawMapLayer(canvas, area, currentTileMap.layers[LayeredTileMap.LAYER_GROUND]); tryDrawMapLayer(canvas, area, currentTileMap, LayeredTileMap.LAYER_OBJECTS); for (Loot l : currentMap.groundBags) { @@ -275,7 +275,7 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac } } - drawFromMapPosition(canvas, area, model.player.position, model.player.actorTraits.iconID); + drawFromMapPosition(canvas, area, playerPosition, model.player.actorTraits.iconID); for (MonsterSpawnArea a : currentMap.spawnAreas) { for (Monster m : a.monsters) { drawFromMapPosition(canvas, area, m.rectPosition, m.actorTraits.iconID); @@ -339,39 +339,44 @@ public final class MainView extends SurfaceView implements SurfaceHolder.Callbac canvas.drawText(e.displayText, x, y, textPaint); } - public void notifyMapChanged() { - Size mapViewSize = new Size( - Math.min(screenSizeTileCount.width, model.currentMap.size.width) - ,Math.min(screenSizeTileCount.height, model.currentMap.size.height) + public void notifyMapChanged(ModelContainer model) { + synchronized (holder) { + currentMap = model.currentMap; + currentTileMap = model.currentTileMap; + Size mapViewSize = new Size( + Math.min(screenSizeTileCount.width, currentMap.size.width) + ,Math.min(screenSizeTileCount.height, currentMap.size.height) ); - mapViewArea = new CoordRect(mapTopLeft, mapViewSize); - tiles = world.tileManager.currentMapTiles; + mapViewArea = new CoordRect(mapTopLeft, mapViewSize); + + tiles = world.tileManager.currentMapTiles; + } clearCanvas(); - recalculateMapTopLeft(); + recalculateMapTopLeft(model.player.position); redrawAll(REDRAW_ALL_MAP_CHANGED); } - private void recalculateMapTopLeft() { - mapTopLeft.set(0, 0); - - final PredefinedMap currentMap = model.currentMap; - final Coord playerpos = model.player.position; - - if (currentMap.size.width > screenSizeTileCount.width) { - mapTopLeft.x = Math.max(0, playerpos.x - mapViewArea.size.width/2); - mapTopLeft.x = Math.min(mapTopLeft.x, currentMap.size.width - mapViewArea.size.width); - } - if (currentMap.size.height > screenSizeTileCount.height) { - mapTopLeft.y = Math.max(0, playerpos.y - mapViewArea.size.height/2); - mapTopLeft.y = Math.min(mapTopLeft.y, currentMap.size.height - mapViewArea.size.height); - } + private void recalculateMapTopLeft(Coord playerPosition) { + synchronized (holder) { + this.playerPosition.set(playerPosition); + mapTopLeft.set(0, 0); + + if (currentMap.size.width > screenSizeTileCount.width) { + mapTopLeft.x = Math.max(0, playerPosition.x - mapViewArea.size.width/2); + mapTopLeft.x = Math.min(mapTopLeft.x, currentMap.size.width - mapViewArea.size.width); + } + if (currentMap.size.height > screenSizeTileCount.height) { + mapTopLeft.y = Math.max(0, playerPosition.y - mapViewArea.size.height/2); + mapTopLeft.y = Math.min(mapTopLeft.y, currentMap.size.height - mapViewArea.size.height); + } + } } - public void notifyPlayerMoved() { - recalculateMapTopLeft(); + public void notifyPlayerMoved(Coord newPosition) { + recalculateMapTopLeft(newPosition); redrawAll(REDRAW_ALL_PLAYER_MOVED); } } -- 2.49.0