Refactoring of TileManager that loads image tiles.

git-svn-id: https://andors-trail.googlecode.com/svn/trunk@201 08aca716-68be-ccc6-4d58-36f5abd142ac
This commit is contained in:
oskar.wiksten
2011-11-05 13:24:59 +00:00
parent e54b0a3fb8
commit a337a02f47
7 changed files with 84 additions and 26 deletions

View File

@@ -174,12 +174,11 @@ public final class MainActivity extends Activity {
L.log("onResume");
if (!AndorsTrailApplication.getApplicationFromActivity(this).setup.isSceneReady) return;
if (world.model.uiSelections.isInCombat) {
view.combatController.setCombatSelection(world.model.uiSelections.selectedMonster, world.model.uiSelections.selectedPosition);
view.combatController.enterCombat(CombatController.BEGIN_TURN_CONTINUE);
}
view.gameRoundController.resume();
if (world.model.uiSelections.isInCombat) {
view.combatController.setCombatSelection(world.model.uiSelections.selectedMonster, world.model.uiSelections.selectedPosition);
view.combatController.enterCombat(CombatController.BEGIN_TURN_CONTINUE);
}
}
@Override

View File

@@ -75,7 +75,7 @@ public final class ShopActivity extends TabActivity implements OnContainerItemCl
HashSet<Integer> iconIDs = world.tileManager.getTileIDsFor(container_buy);
iconIDs.addAll(world.tileManager.getTileIDsFor(player.inventory));
TileCollection tiles = world.tileManager.tileCache.loadTilesFor(iconIDs, res);
TileCollection tiles = world.tileManager.loadTilesFor(iconIDs, res);
buyListAdapter = new ShopItemContainerAdapter(this, tiles, world.tileManager, player, container_buy, this, false);
sellListAdapter = new ShopItemContainerAdapter(this, tiles, world.tileManager, player, player.inventory, this, true);
shoplist_buy.setAdapter(buyListAdapter);

View File

@@ -265,11 +265,15 @@ public final class MovementController implements TimedMessageTask.Callback {
}
public static void cacheCurrentMapData(final Resources res, final WorldContext world, final PredefinedMap nextMap) {
L.log("-> cacheCurrentMapData");
long start = System.currentTimeMillis();
LayeredTileMap mapTiles = TMXMapTranslator.readLayeredTileMap(res, world.tileManager.tileCache, nextMap);
TileCollection cachedTiles = world.tileManager.loadTilesFor(nextMap, mapTiles, world, res);
world.model.currentTileMap = mapTiles;
world.tileManager.currentMapTiles = cachedTiles;
world.tileManager.cacheAdjacentMaps(res, world, nextMap);
long duration = System.currentTimeMillis() - start;
L.log(" <- cacheCurrentMapData " + duration + "ms");
}

View File

@@ -61,7 +61,7 @@ public final class TileCache {
public TileCollection loadTilesFor(Collection<Integer> iconIDs, Resources r) { return loadTilesFor(iconIDs, r, null); }
public TileCollection loadTilesFor(Collection<Integer> iconIDs, Resources r, TileCollection result) {
cleanQueue();
if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) L.log("TileCache::loadTilesFor({" + iconIDs.size() + " items})");
int maxTileID = 0;
HashMap<ResourceFileTileset, HashMap<Integer, ResourceFileTile>> tilesToLoadPerSourceFile = new HashMap<ResourceFileTileset, HashMap<Integer, ResourceFileTile>>();
for(int tileID : iconIDs) {
@@ -75,6 +75,7 @@ public final class TileCache {
maxTileID = Math.max(maxTileID, tileID);
}
boolean hasLoadedTiles = false;
if (result == null) result = new TileCollection(maxTileID);
for(Entry<ResourceFileTileset, HashMap<Integer, ResourceFileTile>> e : tilesToLoadPerSourceFile.entrySet()) {
TileCutter cutter = null;
@@ -91,7 +92,9 @@ public final class TileCache {
if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) {
L.log("Loading tiles from tileset " + e.getKey().tilesetName);
}
if (!hasLoadedTiles) cleanQueue();
cutter = new TileCutter(e.getKey(), r);
hasLoadedTiles = true;
}
bitmap = cutter.createTile(tile.localID);
@@ -102,7 +105,7 @@ public final class TileCache {
if (cutter != null) cutter.recycle();
}
cleanQueue();
if (hasLoadedTiles) cleanQueue();
return result;
}

View File

@@ -1,5 +1,6 @@
package com.gpl.rpg.AndorsTrail.resource.tiles;
import java.util.HashMap;
import java.util.HashSet;
import android.content.res.Resources;
@@ -24,6 +25,7 @@ import com.gpl.rpg.AndorsTrail.model.map.MapObject;
import com.gpl.rpg.AndorsTrail.model.map.MonsterSpawnArea;
import com.gpl.rpg.AndorsTrail.model.map.PredefinedMap;
import com.gpl.rpg.AndorsTrail.model.map.TMXMapTranslator;
import com.gpl.rpg.AndorsTrail.util.L;
public final class TileManager {
public static final int CHAR_HERO = 1;
@@ -46,13 +48,17 @@ public final class TileManager {
public float scale;
public final TileCache tileCache = new TileCache();
public final TileCache tileCache = new TileCache();
public final TileCollection preloadedTiles = new TileCollection(72);
public TileCollection currentMapTiles;
public TileCollection adjacentMapTiles;
private final HashSet<Integer> preloadedTileIDs = new HashSet<Integer>();
public TileCollection loadTilesFor(HashSet<Integer> tileIDs, Resources r) {
return tileCache.loadTilesFor(tileIDs, r);
}
public TileCollection loadTilesFor(ItemContainer container, Resources r) {
return tileCache.loadTilesFor(getTileIDsFor(container), r);
}
@@ -142,10 +148,24 @@ public final class TileManager {
tileCache.loadTilesFor(preloadedTileIDs, r, preloadedTiles);
}
private HashMap<String, HashSet<Integer>> tileIDsPerMap = new HashMap<String, HashSet<Integer>>();
private void addTileIDsFor(HashSet<Integer> dest, String mapName, final Resources res, final WorldContext world) {
HashSet<Integer> cachedTileIDs = tileIDsPerMap.get(mapName);
if (cachedTileIDs == null) {
PredefinedMap adjacentMap = world.maps.findPredefinedMap(mapName);
if (adjacentMap == null) return;
LayeredTileMap adjacentMapTiles = TMXMapTranslator.readLayeredTileMap(res, tileCache, adjacentMap);
cachedTileIDs = getTileIDsFor(adjacentMap, adjacentMapTiles, world);
tileIDsPerMap.put(mapName, cachedTileIDs);
}
dest.addAll(cachedTileIDs);
}
public void cacheAdjacentMaps(final Resources res, final WorldContext world, final PredefinedMap nextMap) {
(new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... arg0) {
L.log("-> cacheAdjacentMaps");
long start = System.currentTimeMillis();
adjacentMapTiles = null;
HashSet<String> adjacentMapNames = new HashSet<String>();
@@ -157,13 +177,14 @@ public final class TileManager {
HashSet<Integer> tileIDs = new HashSet<Integer>();
for (String mapName : adjacentMapNames) {
PredefinedMap adjacentMap = world.maps.findPredefinedMap(mapName);
if (adjacentMap == null) continue;
LayeredTileMap adjacentMapTiles = TMXMapTranslator.readLayeredTileMap(res, tileCache, adjacentMap);
tileIDs.addAll(getTileIDsFor(adjacentMap, adjacentMapTiles, world));
addTileIDsFor(tileIDs, mapName, res, world);
}
long duration = System.currentTimeMillis() - start;
L.log(" -- cacheAdjacentMaps " + duration + "ms");
adjacentMapTiles = tileCache.loadTilesFor(tileIDs, res);
duration = System.currentTimeMillis() - start;
L.log(" <- cacheAdjacentMaps " + duration + "ms");
return null;
}
}).execute();

View File

@@ -23,12 +23,13 @@ public final class TimedMessageTask extends Handler {
if (!isAlive) return;
if (!hasQueuedTick) return;
hasQueuedTick = false;
if (tick()) queueAnotherTick();
tick();
}
private boolean tick() {
private void tick() {
nextTickTime = System.currentTimeMillis() + interval;
return callback.onTick(this);
boolean continueTicking = callback.onTick(this);
if (continueTicking) queueAnotherTick();
}
private void sleep(long delayMillis) {
@@ -56,7 +57,7 @@ public final class TimedMessageTask extends Handler {
public void start() {
isAlive = true;
if (shouldCauseTickOnStart()) tick();
queueAnotherTick();
else queueAnotherTick();
}
public void stop() {

View File

@@ -1,5 +1,7 @@
package com.gpl.rpg.AndorsTrail.view;
import java.util.HashSet;
import android.R.color;
import android.content.Context;
import android.content.res.Resources;
@@ -15,6 +17,7 @@ import com.gpl.rpg.AndorsTrail.activity.MainActivity;
import com.gpl.rpg.AndorsTrail.context.ViewContext;
import com.gpl.rpg.AndorsTrail.context.WorldContext;
import com.gpl.rpg.AndorsTrail.model.item.ItemType;
import com.gpl.rpg.AndorsTrail.resource.tiles.TileCollection;
import com.gpl.rpg.AndorsTrail.resource.tiles.TileManager;
public class QuickitemView extends LinearLayout implements OnClickListener {
@@ -22,7 +25,9 @@ public class QuickitemView extends LinearLayout implements OnClickListener {
private final WorldContext world;
private final ViewContext view;
private final QuickButton[] items = new QuickButton[NUM_QUICK_SLOTS];
private final QuickButton[] buttons = new QuickButton[NUM_QUICK_SLOTS];
private final HashSet<Integer> loadedTileIDs = new HashSet<Integer>();
private TileCollection tiles = null;
public QuickitemView(Context context, AttributeSet attrs) {
super(context, attrs);
@@ -37,9 +42,9 @@ public class QuickitemView extends LinearLayout implements OnClickListener {
this.setBackgroundColor(res.getColor(color.transparent));
TypedArray quickButtons = res.obtainTypedArray(R.array.quick_buttons);
for(int i = 0; i <items.length ; ++i) {
items[i] = (QuickButton)findViewById(quickButtons.getResourceId(i, -1));
QuickButton item = items[i];
for(int i = 0; i < buttons.length; ++i) {
buttons[i] = (QuickButton)findViewById(quickButtons.getResourceId(i, -1));
QuickButton item = buttons[i];
item.setIndex(i);
world.tileManager.setImageViewTileForUIIcon(item, TileManager.iconID_shop);
item.setOnClickListener(this);
@@ -48,7 +53,7 @@ public class QuickitemView extends LinearLayout implements OnClickListener {
}
public boolean isQuickButtonId(int id){
for(QuickButton item: items){
for(QuickButton item: buttons){
if(item.getId()==id)
return true;
}
@@ -71,21 +76,46 @@ public class QuickitemView extends LinearLayout implements OnClickListener {
}
public void refreshQuickitems() {
loadItemTypeImages();
for (int i = 0; i < NUM_QUICK_SLOTS; ++i){
QuickButton item = items[i];
QuickButton item = buttons[i];
ItemType type = world.model.player.inventory.quickitem[i];
if(type==null) {
if (type == null) {
world.tileManager.setImageViewTileForUIIcon(item, TileManager.iconID_shop);
item.setEmpty(true);
} else {
world.tileManager.setImageViewTileForSingleItemType(item, type, getResources());
world.tileManager.setImageViewTile(item, type, tiles);
item.setEmpty(!world.model.player.inventory.hasItem(type.id));
}
}
}
private void loadItemTypeImages() {
boolean shouldLoadImages = false;
for (ItemType type : world.model.player.inventory.quickitem) {
if (type == null) continue;
if (!loadedTileIDs.contains(type.iconID)) {
shouldLoadImages = true;
break;
}
}
if (!shouldLoadImages) return;
HashSet<Integer> iconIDs = new HashSet<Integer>();
for (ItemType type : world.model.player.inventory.quickitem) {
if (type == null) continue;
iconIDs.add(type.iconID);
}
loadedTileIDs.clear();
loadedTileIDs.addAll(iconIDs);
tiles = world.tileManager.loadTilesFor(iconIDs, getResources());
}
public void registerForContextMenu(MainActivity mainActivity) {
for(QuickButton item: items)
for(QuickButton item: buttons)
mainActivity.registerForContextMenu(item);
}
}