Package horizons :: Package util :: Package loaders :: Module sqliteatlasloader
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.loaders.sqliteatlasloader

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from typing import Any, List 
 23   
 24  from fife import fife 
 25   
 26  import horizons.globals 
 27  from horizons.util.loaders.actionsetloader import ActionSetLoader 
 28  from horizons.util.loaders.tilesetloader import TileSetLoader 
 29   
 30   
31 -class SQLiteAtlasLoader:
32 """Loads atlases and appropriate action sets from a JSON file and a SQLite database. 33 """
34 - def __init__(self):
35 self.atlaslib = [] # type: List[Any] 36 37 # TODO: There's something wrong with ground entities if atlas.sql 38 # is loaded only here, for now it's added to DB_FILES (empty file if no atlases are used) 39 40 #f = open('content/atlas.sql', "r") 41 #sql = "BEGIN TRANSACTION;" + f.read() + "COMMIT;" 42 #horizons.globals.db.execute_script(sql) 43 44 self.atlases = horizons.globals.db("SELECT atlas_path FROM atlas ORDER BY atlas_id ASC") 45 self.inited = False
46
47 - def init(self):
48 """Used to lazy init the loader""" 49 for (atlas,) in self.atlases: 50 # print 'creating', atlas 51 # cast explicit to str because the imagemanager is not able to handle unicode strings 52 img = horizons.globals.fife.imagemanager.create(str(atlas)) 53 self.atlaslib.append(img) 54 self.inited = True
55
56 - def loadResource(self, anim_id):
57 """ 58 @param anim_id: String identifier for the image, eg "as_hunter0+idle+135" 59 """ 60 61 if not self.inited: 62 self.init() 63 actionset, action, rotation = anim_id.split('+') 64 65 animationmanager = horizons.globals.fife.animationmanager 66 67 # if we've loaded that animation before, we can finish early 68 if animationmanager.exists(anim_id): 69 return animationmanager.getPtr(anim_id) 70 71 ani = animationmanager.create(anim_id) 72 73 # Set the correct loader based on the actionset 74 loader = self._get_loader(actionset) 75 76 frame_start, frame_end = 0.0, 0.0 77 for file in sorted(loader.get_sets()[actionset][action][int(rotation)].keys()): 78 entry = loader.get_sets()[actionset][action][int(rotation)][file] 79 # we don't need to load images at this point to query for its parameters 80 # such as width and height because we can get those from json file 81 xpos, ypos, width, height = entry[2:] 82 83 if horizons.globals.fife.imagemanager.exists(file): 84 img = horizons.globals.fife.imagemanager.get(file) 85 else: 86 img = horizons.globals.fife.imagemanager.create(file) 87 region = fife.Rect(xpos, ypos, width, height) 88 img.useSharedImage(self.atlaslib[entry[1]], region) 89 90 img.setYShift(int(img.getWidth() / 4 - img.getHeight() / 2)) 91 frame_end = entry[0] 92 ani.addFrame(img, max(1, int((frame_end - frame_start) * 1000))) 93 frame_start = frame_end 94 # currently unused. would trigger onInstanceActionFrame of 95 # fife.InstanceActionListener instance 96 ani.setActionFrame(-1) 97 return ani
98
99 - def _get_loader(self, actionset):
100 if actionset.startswith("ts_"): 101 loader = TileSetLoader 102 elif actionset.startswith("as_"): 103 loader = ActionSetLoader 104 else: 105 assert False, "Invalid set being loaded: " + actionset 106 return loader
107
108 - def load_image(self, file, actionset, action, rotation):
109 if not self.inited: 110 self.init() 111 loader = self._get_loader(actionset) 112 entry = loader.get_sets()[actionset][action][int(rotation)][file] 113 # we don't need to load images at this point to query for its parameters 114 # such as width and height because we can get those from json file 115 xpos, ypos, width, height = entry[2:] 116 117 if horizons.globals.fife.imagemanager.exists(file): 118 img = horizons.globals.fife.imagemanager.get(file) 119 else: 120 img = horizons.globals.fife.imagemanager.create(file) 121 region = fife.Rect(xpos, ypos, width, height) 122 img.useSharedImage(self.atlaslib[entry[1]], region) 123 124 return img
125