1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.maps; 18 19 import com.badlogic.gdx.assets.AssetManager; 20 import com.badlogic.gdx.graphics.Texture; 21 import com.badlogic.gdx.graphics.g2d.TextureAtlas; 22 import com.badlogic.gdx.graphics.g2d.TextureRegion; 23 import com.badlogic.gdx.utils.ObjectMap; 24 25 /** Resolves an image by a string, wrapper around a Map or AssetManager to load maps either directly or via AssetManager. 26 * @author mzechner */ 27 public interface ImageResolver { 28 /** @param name 29 * @return the Texture for the given image name or null. */ 30 public TextureRegion getImage (String name); 31 32 public static class DirectImageResolver implements ImageResolver { 33 private final ObjectMap<String, Texture> images; 34 35 public DirectImageResolver (ObjectMap<String, Texture> images) { 36 this.images = images; 37 } 38 39 @Override 40 public TextureRegion getImage (String name) { 41 return new TextureRegion(images.get(name)); 42 } 43 } 44 45 public static class AssetManagerImageResolver implements ImageResolver { 46 private final AssetManager assetManager; 47 48 public AssetManagerImageResolver (AssetManager assetManager) { 49 this.assetManager = assetManager; 50 } 51 52 @Override 53 public TextureRegion getImage (String name) { 54 return new TextureRegion(assetManager.get(name, Texture.class)); 55 } 56 } 57 58 public static class TextureAtlasImageResolver implements ImageResolver { 59 private final TextureAtlas atlas; 60 61 public TextureAtlasImageResolver (TextureAtlas atlas) { 62 this.atlas = atlas; 63 } 64 65 @Override 66 public TextureRegion getImage (String name) { 67 return atlas.findRegion(name); 68 } 69 } 70 } 71