########################################################################
#	Hello Worlds - Libre 3D RPG game.
#	Copyright (C) 2020  CYBERDEViL
#
#	This file is part of Hello Worlds.
#
#	Hello Worlds is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	Hello Worlds is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
########################################################################


# Global
import sys
sys.path.insert(0, "ui/lui")

# Lui
from LUISkin import LUIDefaultSkin
from LUIRegion import LUIRegion
from LUIInputHandler import LUIInputHandler

# UI
from ui.manager import UI_Manager

from core.world import World
from core.mouse import MouseHandler
from core import db # init db

# Panda
from direct.showbase.ShowBase import ShowBase
from panda3d.core import load_prc_file_data
load_prc_file_data("", """
	text-minfilter linear
	text-magfilter linear
	text-pixels-per-unit 32
	sync-video #f
	textures-power-2 none
	show-frame-rate-meter #t
	win-size 1280 720
	window-title Hello Worlds
	support-threads #t
	task-timer-verbose #t
""")


class Game(ShowBase):
	def __init__(self):
		ShowBase.__init__(self)

		# Show FPS
		self.setFrameRateMeter(True)

		# Disable default mouse look behaviour
		self.disableMouse()

		""" Db assets path
		"""
		db.AssetsPath.set('./assets/')

		""" Mouse handler
		"""
		self.mouseHandler = MouseHandler()

		""" World
		"""
		self.world = World()

		""" LUI
		"""
		# Construct a new LUIRegion
		# Note: base.luiRegion and self.luiRegion are the same
		self.luiRegion = LUIRegion.make("LUI", base.win)

		# Construct a new InputHandler to catch and process events
		self.luiInputHandler = LUIInputHandler()

		self.mouseWatcher.getParent().attach_new_node(self.luiInputHandler)
		self.luiRegion.set_input_handler(self.luiInputHandler)

		# Load the default LUI skin
		skin = LUIDefaultSkin()
		skin.load()

		""" UI manager
		"""
		self.uiManager = UI_Manager()

		""" Main shortcuts
		"""
		self.accept('escape', self.toggleStartMenu)

		""" Connections
		"""
		self.accept('UI_STARTMENU_START_PRESSED', self._startClicked)
		self.accept('UI_STARTMENU_EXIT_PRESSED', self._exitClicked)

		""" Window event task
		"""
		self._currentRatio = base.getAspectRatio()
		taskMgr.doMethodLater(1, self._windowEventCheck, 'windowEvent')

	def _startClicked(self):
		self.uiManager.startMenu.hide()
		self.world.loadMap(1)

	def _exitClicked(self):
		self.world.destroy()
		self.uiManager.close()
		self.finalizeExit()

	def toggleStartMenu(self):
		self.uiManager.startMenu.hide() if self.uiManager.startMenu.isVisible() else self.uiManager.startMenu.show()

	def _windowEventCheck(self, task):
		if self._currentRatio != base.getAspectRatio():
			self._currentRatio = base.getAspectRatio()
			messenger.send('WINDOW_RESIZED')
		return task.again

game = Game()
game.run()
