From 706565eb5dfeebc648c78c1c8d14fed86cb7a960 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 28 Jan 2019 09:44:07 +0100 Subject: [PATCH] src/core: add mouse support to scenes --- sonic-radiance.love/core/init.lua | 11 +++++++++++ sonic-radiance.love/core/modules/scenes.lua | 11 ++++++++++- sonic-radiance.love/core/scenemanager.lua | 10 ++++++++++ sonic-radiance.love/core/screen.lua | 8 ++++++++ sonic-radiance.love/main.lua | 8 ++++++++ sonic-radiance.love/scenes/test_scene/init.lua | 2 +- 6 files changed, 48 insertions(+), 2 deletions(-) diff --git a/sonic-radiance.love/core/init.lua b/sonic-radiance.love/core/init.lua index 5e7f764..63bba08 100644 --- a/sonic-radiance.love/core/init.lua +++ b/sonic-radiance.love/core/init.lua @@ -41,6 +41,17 @@ function CoreSystem:new() self.scenemanager = SceneManager(self) end +function CoreSystem:mousemoved(x, y, dx, dy) + local x, y = self.screen:project(x, y) + local dx, dy = self.screen:project(dx, dy) + self.scenemanager:mousemoved(x, y, dx, dy) +end + +function CoreSystem:mousepressed( x, y, button, istouch ) + local x, y = self.screen:project(x, y) + self.scenemanager:mousepressed( x, y, button, istouch ) +end + function CoreSystem:update(dt) self.debug:update(dt) self.input:update(dt) diff --git a/sonic-radiance.love/core/modules/scenes.lua b/sonic-radiance.love/core/modules/scenes.lua index e964b94..dfda261 100644 --- a/sonic-radiance.love/core/modules/scenes.lua +++ b/sonic-radiance.love/core/modules/scenes.lua @@ -25,7 +25,8 @@ local Scene = Object:extend() function Scene:new() - + self.mouse = {} + self.mouse.x, self.mouse.y = core.screen:getMousePosition() end function Scene:register() @@ -36,6 +37,14 @@ function Scene:update(dt) -- Empty function, is just here to avoid crash end +function Scene:mousemoved(x, y, dx, dy) + -- Empty function, is just here to avoid crash +end + +function Scene:mousepressed( x, y, button, istouch ) + -- Empty function, is just here to avoid crash +end + function Scene:draw() end diff --git a/sonic-radiance.love/core/scenemanager.lua b/sonic-radiance.love/core/scenemanager.lua index 5fac773..31398c4 100644 --- a/sonic-radiance.love/core/scenemanager.lua +++ b/sonic-radiance.love/core/scenemanager.lua @@ -36,6 +36,16 @@ function SceneManager: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) +end + +function SceneManager:mousepressed( x, y, button, istouch ) + self.currentScene:mousepressed( x, y, button, istouch ) +end + function SceneManager:clearScene() self.currentScene = nil end diff --git a/sonic-radiance.love/core/screen.lua b/sonic-radiance.love/core/screen.lua index c7dc84a..1d8d3dc 100644 --- a/sonic-radiance.love/core/screen.lua +++ b/sonic-radiance.love/core/screen.lua @@ -53,6 +53,14 @@ function ScreenManager:applySettings() CScreen.update(width, height) end +function ScreenManager:project(x, y) + return CScreen.project(x, y) +end + +function ScreenManager:getMousePosition() + return CScreen.project(love.mouse.getX(), love.mouse.getY()) +end + function ScreenManager:apply() CScreen.apply() end diff --git a/sonic-radiance.love/main.lua b/sonic-radiance.love/main.lua index 49b02af..37d4cde 100644 --- a/sonic-radiance.love/main.lua +++ b/sonic-radiance.love/main.lua @@ -43,3 +43,11 @@ end function love.draw() core:draw() end + +function love.mousemoved(x, y, dx, dy) + core:mousemoved(x, y, dx, dy) +end + +function love.mousepressed( x, y, button, istouch ) + core:mousemoved(x, y, button, istouch) +end diff --git a/sonic-radiance.love/scenes/test_scene/init.lua b/sonic-radiance.love/scenes/test_scene/init.lua index c69cae0..8ece219 100644 --- a/sonic-radiance.love/scenes/test_scene/init.lua +++ b/sonic-radiance.love/scenes/test_scene/init.lua @@ -41,7 +41,7 @@ function TestScene:draw() love.graphics.rectangle("fill", 0, 0, 424, 240) love.graphics.setColor(0, 0, 0, 1) - love.graphics.print(self.i, 16, 16) + love.graphics.print(self.i .. " ; " .. self.mouse.x .. ":" .. self.mouse.y, 16, 16) end return TestScene