def _loadObjects(self):
		# get fifedit objects
		self.model = self._engine.getModel()
		self.all_action_sets = ActionSetLoader.get_sets()

		TileSetLoader.load()
		ActionSetLoader.load()
		self.animationloader = SQLiteAnimationLoader()

		self._loadGroundTiles()
		self._loadBuildings()
class UHObjectLoader(scripts.plugin.Plugin):
	""" The B{UHObjectLoader} allows to load the UH objects into the FIFEdit object selector """

	def __init__(self):
		# Editor instance
		self._editor = None
		# Plugin variables
		self._enabled = False
		# Current mapview
		self._mapview = None


	#--- Plugin functions ---#
	def enable(self):
		""" Enable plugin """
		if self._enabled is True:
			return

		# Fifedit plugin data
		self._editor = scripts.editor.getEditor()
		self._engine = self._editor.getEngine()

		# load UH objects
		self._fixupFife()
		self._fixupHorizons()
		self._loadObjects()

	def disable(self):
		""" Disable plugin """
		if self._enabled is False:
			return

	def isEnabled(self):
		""" Returns True if plugin is enabled """
		return self._enabled;

	def getName(self):
		""" Return plugin name """
		return u"UHObjectLoader"

	#--- End plugin functions ---#

	def _fixupHorizons(self):
		"""Fixes some UH quirks that have to do with globals"""
		class PatchedFife:
			imagemanager = self._engine.getImageManager()
			use_atlases = False
			pass
		horizons.main.fife = PatchedFife()
		uh_path = util.getUHPath()
		horizons.main.PATHS.TILE_SETS_DIRECTORY = os.path.join(uh_path, horizons.main.PATHS.TILE_SETS_DIRECTORY)
		horizons.main.PATHS.ACTION_SETS_DIRECTORY = os.path.join(uh_path, horizons.main.PATHS.ACTION_SETS_DIRECTORY)
		horizons.main.PATHS.DB_FILES = map(lambda file: os.path.join(uh_path, file), horizons.main.PATHS.DB_FILES)

	def _fixupFife(self):
		"""Fixes some FIFE quirks that have to do with VFS"""
		vfs = self._engine.getVFS()
		vfs.addNewSource(util.getUHPath())
		vfs.addNewSource("/")

	def _loadObjects(self):
		# get fifedit objects
		self.model = self._engine.getModel()
		self.all_action_sets = ActionSetLoader.get_sets()

		TileSetLoader.load()
		ActionSetLoader.load()
		self.animationloader = SQLiteAnimationLoader()

		self._loadGroundTiles()
		self._loadBuildings()

	def _loadGroundTiles(self):
		print("loading UH ground tiles...")
		tile_sets = TileSetLoader.get_sets()

		for tile_set_id in tile_sets:
			tile_set = tile_sets[tile_set_id]
			object = self.model.createObject(str(tile_set_id), util.GROUND_NAMESPACE)
			fife.ObjectVisual.create(object)

			# load animations
			for action_id in tile_sets[tile_set_id].iterkeys():
				action = object.createAction(action_id+"_"+str(tile_set_id))
				fife.ActionVisual.create(action)
				for rotation in tile_sets[tile_set_id][action_id].iterkeys():
					anim = self.animationloader.loadResource( \
						str(tile_set_id)+"+"+str(action_id)+"+"+ \
						str(rotation) + ':shift:center+0,bottom+8')
					action.get2dGfxVisual().addAnimation(int(rotation), anim)
					action.setDuration(anim.getDuration())

	def _loadBuildings(self):
		print("loading UH buildings...")
		for root, dirnames, filenames in os.walk(util.getUHPath() + '/content/objects/buildings'):
			for filename in fnmatch.filter(filenames, '*.yaml'):
				# This is needed for dict lookups! Do not convert to os.join!
				full_file = root + "/" + filename
				result = YamlCache.get_file(full_file)
				result['yaml_file'] = full_file
				self._loadBuilding(result)

		print("finished loading UH objects")

	def _loadBuilding(self, yaml):
		id = int(yaml['id'])
		name = yaml['name'][2:]
		size_x = yaml['size_x']
		size_y = yaml['size_y']
		action_sets = yaml['actionsets']
		object = self.model.createObject(str(name), util.BUILDING_NAMESPACE)
		fife.ObjectVisual.create(object)

		main_action = 'idle'
		for action_set_id in action_sets.iterkeys():
			for action_id in self.all_action_sets[action_set_id].iterkeys():
				main_action = action_id+"_"+str(action_set_id)
				action = object.createAction(main_action)
				fife.ActionVisual.create(action)
				for rotation in self.all_action_sets[action_set_id][action_id].iterkeys():
					if rotation == 45:
						command = 'left-32,bottom+' + str(size_x * 16)
					elif rotation == 135:
						command = 'left-' + str(size_y * 32) + ',bottom+16'
					elif rotation == 225:
						command = 'left-' + str((size_x + size_y - 1) * 32) + ',bottom+' + str(size_y * 16)
					elif rotation == 315:
						command = 'left-' + str(size_x * 32) + ',bottom+' + str((size_x + size_y - 1) * 16)
					else:
						assert False, "Bad rotation for action_set %(id)s: %(rotation)s for action: %(action_id)s" % \
							   { 'id': action_set_id, 'rotation': rotation, 'action_id': action_id }
					anim = self.animationloader.loadResource(str(action_set_id)+"+"+str(action_id)+"+"+str(rotation) + ':shift:' + command)
					action.get2dGfxVisual().addAnimation(int(rotation), anim)
					action.setDuration(anim.getDuration())

		util.addBuilding(id, name, main_action)
示例#3
0
    def init(self):
        """
		"""
        logToPrompt, logToFile, debugPychan = True, True, False
        if self._gotInited:
            return
        #start modules
        self.log = fifelog.LogManager(self.engine, 1 if logToPrompt else 0,
                                      1 if logToFile else 0)
        #self.log.setVisibleModules('all')

        self.engine.init()

        #temporarily select a random music file to play. TODO: Replace with proper playlist
        self.ingame_music = glob.glob('content/audio/music/*.ogg')
        self.menu_music = glob.glob('content/audio/music/menu/*.ogg')
        self.initial_menu_music_element = None
        self.next_menu_music_element = None
        self.menu_music_played = False

        #init stuff
        self.eventmanager = self.engine.getEventManager()
        #self.eventmanager.setNonConsumableKeys([fife.Key.ESCAPE, fife.Key.F10])
        self.guimanager = self.engine.getGuiManager()
        self.console = self.guimanager.getConsole()
        self.soundmanager = self.engine.getSoundManager()
        self.soundmanager.init()
        self.setup_sound()
        self.imagepool = self.engine.getImagePool()
        self.animationpool = self.engine.getAnimationPool()
        self.animationloader = SQLiteAnimationLoader()
        self.animationpool.addResourceLoader(self.animationloader)

        #Set game cursor
        self.cursor = self.engine.getCursor()
        self.default_cursor_image = self.imagepool.addResourceFromFile(
            'content/gui/images/cursors/cursor.png')
        self.tearing_cursor_image = self.imagepool.addResourceFromFile(
            'content/gui/images/cursors/cursor_tear.png')
        self.cursor.set(fife.CURSOR_IMAGE, self.default_cursor_image)

        #init pychan
        self.pychan.init(self.engine, debugPychan)
        self.pychan.setupModalExecution(self.loop, self.breakLoop)

        from gui.widgets.inventory import Inventory
        from gui.widgets.imagefillstatusbutton import ImageFillStatusButton
        from gui.widgets.progressbar import ProgressBar
        from gui.widgets.toggleimagebutton import ToggleImageButton
        from gui.widgets.tooltip import TooltipIcon, TooltipButton, TooltipLabel, TooltipProgressBar
        from gui.widgets.imagebutton import CancelButton, DeleteButton, OkButton
        from gui.widgets.icongroup import TabBG
        from gui.widgets.stepslider import StepSlider

        pychan.widgets.registerWidget(CancelButton)
        pychan.widgets.registerWidget(DeleteButton)
        pychan.widgets.registerWidget(Inventory)
        pychan.widgets.registerWidget(ImageFillStatusButton)
        pychan.widgets.registerWidget(OkButton)
        pychan.widgets.registerWidget(ProgressBar)
        pychan.widgets.registerWidget(TabBG)
        pychan.widgets.registerWidget(ToggleImageButton)
        pychan.widgets.registerWidget(TooltipIcon)
        pychan.widgets.registerWidget(TooltipButton)
        pychan.widgets.registerWidget(TooltipLabel)
        pychan.widgets.registerWidget(TooltipProgressBar)
        pychan.widgets.registerWidget(StepSlider)

        for name, stylepart in horizons.gui.style.STYLES.iteritems():
            self.pychan.manager.addStyle(name, stylepart)
        self.pychan.loadFonts("content/fonts/libertine.fontdef")

        self._gotInited = True
        self.setup_setting_extras()