-- scene.lua :: a basic scene management system, that work by sending the different
-- core functions to the scene, normally without the scene itself having to manage
-- them.

--[[
  Copyright © 2019 Kazhnuz

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

local SceneManager  = Object:extend()

function SceneManager:new(controller)
  self.controller   = controller
  self.currentScene = nil
end

function SceneManager:update(dt)
  if (self.currentScene ~= nil) then
    local keys = self.controller.input.keys
    self.currentScene.keys = keys
    self.currentScene.menusystem.keys = keys
    self.currentScene.assets:update(dt)
    self.currentScene.menusystem:update(dt)
    self.currentScene:update(dt)
  end
end

function SceneManager:mousemoved(x, y, dx, dy)
  self.currentScene.mouse.x,
  self.currentScene.mouse.y = x, y
  self.currentScene:mousemoved(x, y, dx, dy)
  self.currentScene.menusystem:mousemoved(x, y, dx, dy)
end

function SceneManager:mousepressed( x, y, button, istouch )
  self.currentScene:mousepressed( x, y, button, istouch )
  self.currentScene.menusystem:mousepressed( x, y, button, istouch )
end

function SceneManager:clearScene()
  self.currentScene = nil
end

function SceneManager:draw()
  self.controller.screen:apply()
  if (self.currentScene ~= nil) then
    self.currentScene:draw(dt)
    self.currentScene.menusystem:draw()
  end
  self.controller.screen:cease()
end

return SceneManager
