diff --git a/source/client/StarClientApplication.cpp b/source/client/StarClientApplication.cpp index 5f0c22e..250445c 100644 --- a/source/client/StarClientApplication.cpp +++ b/source/client/StarClientApplication.cpp @@ -22,6 +22,7 @@ #include "StarInterfaceLuaBindings.hpp" #include "StarInputLuaBindings.hpp" #include "StarVoiceLuaBindings.hpp" +#include "StarCameraLuaBindings.hpp" #include "StarClipboardLuaBindings.hpp" #if defined STAR_SYSTEM_WINDOWS @@ -541,6 +542,7 @@ void ClientApplication::changeState(MainAppState newState) { m_universeClient->setLuaCallbacks("input", LuaBindings::makeInputCallbacks()); m_universeClient->setLuaCallbacks("voice", LuaBindings::makeVoiceCallbacks()); + m_universeClient->setLuaCallbacks("camera", LuaBindings::makeCameraCallbacks(&m_worldPainter->camera())); if (!m_root->configuration()->get("safeScripts").toBool()) m_universeClient->setLuaCallbacks("clipboard", LuaBindings::makeClipboardCallbacks(appController())); diff --git a/source/game/CMakeLists.txt b/source/game/CMakeLists.txt index 3b85e2a..affe2b3 100644 --- a/source/game/CMakeLists.txt +++ b/source/game/CMakeLists.txt @@ -162,6 +162,7 @@ SET (star_game_HEADERS StarWeatherTypes.hpp StarWireProcessor.hpp StarWiring.hpp + StarWorldCamera.hpp StarWorldClient.hpp StarWorldClientState.hpp StarWorldGeneration.hpp @@ -231,6 +232,7 @@ SET (star_game_HEADERS objects/StarPhysicsObject.hpp objects/StarTeleporterObject.hpp + scripting/StarCameraLuaBindings.hpp scripting/StarCelestialLuaBindings.hpp scripting/StarBehaviorLuaBindings.hpp scripting/StarConfigLuaBindings.hpp @@ -421,6 +423,7 @@ SET (star_game_SOURCES StarWeatherTypes.cpp StarWireProcessor.cpp StarWiring.cpp + StarWorldCamera.cpp StarWorldClient.cpp StarWorldClientState.cpp StarWorldGeneration.cpp @@ -470,6 +473,7 @@ SET (star_game_SOURCES objects/StarPhysicsObject.cpp objects/StarTeleporterObject.cpp + scripting/StarCameraLuaBindings.cpp scripting/StarCelestialLuaBindings.cpp scripting/StarBehaviorLuaBindings.cpp scripting/StarConfigLuaBindings.cpp diff --git a/source/rendering/StarWorldCamera.cpp b/source/game/StarWorldCamera.cpp similarity index 95% rename from source/rendering/StarWorldCamera.cpp rename to source/game/StarWorldCamera.cpp index de23f3e..a27f84a 100644 --- a/source/rendering/StarWorldCamera.cpp +++ b/source/game/StarWorldCamera.cpp @@ -2,7 +2,7 @@ namespace Star { -void WorldCamera::setCenterWorldPosition(Vec2F const& position, bool force) { +void WorldCamera::setCenterWorldPosition(Vec2F position, bool force) { m_rawWorldCenter = position; // Only actually move the world center if a half pixel distance has been // moved in any direction. This is sort of arbitrary, but helps prevent diff --git a/source/rendering/StarWorldCamera.hpp b/source/game/StarWorldCamera.hpp similarity index 92% rename from source/rendering/StarWorldCamera.hpp rename to source/game/StarWorldCamera.hpp index aa0713b..abc018a 100644 --- a/source/rendering/StarWorldCamera.hpp +++ b/source/game/StarWorldCamera.hpp @@ -20,7 +20,7 @@ public: // Set the camera center position (in world space) to as close to the given // location as possible while keeping the screen within world bounds. - void setCenterWorldPosition(Vec2F const& position, bool force = false); + void setCenterWorldPosition(Vec2F position, bool force = false); // Returns the actual camera position. Vec2F centerWorldPosition() const; @@ -28,10 +28,10 @@ public: // the world is non-euclidean, one world coordinate can transform to // potentially an infinite number of screen coordinates. This will retrun // the closest to the center of the screen. - Vec2F worldToScreen(Vec2F const& worldCoord) const; + Vec2F worldToScreen(Vec2F worldCoord) const; // Assumes top left corner of screen is (0, 0) in screen coordinates. - Vec2F screenToWorld(Vec2F const& screen) const; + Vec2F screenToWorld(Vec2F screen) const; // Returns screen dimensions in world space. RectF worldScreenRect() const; @@ -86,7 +86,7 @@ inline Vec2F WorldCamera::centerWorldPosition() const { return Vec2F(m_worldCenter); } -inline Vec2F WorldCamera::worldToScreen(Vec2F const& worldCoord) const { +inline Vec2F WorldCamera::worldToScreen(Vec2F worldCoord) const { Vec2F wrappedCoord = m_worldGeometry.nearestTo(Vec2F(m_worldCenter), worldCoord); return Vec2F( (wrappedCoord[0] - m_worldCenter[0]) * (TilePixels * m_pixelRatio) + (float)m_screenSize[0] / 2.0, @@ -94,7 +94,7 @@ inline Vec2F WorldCamera::worldToScreen(Vec2F const& worldCoord) const { ); } -inline Vec2F WorldCamera::screenToWorld(Vec2F const& screen) const { +inline Vec2F WorldCamera::screenToWorld(Vec2F screen) const { return Vec2F( (screen[0] - (float)m_screenSize[0] / 2.0) / (TilePixels * m_pixelRatio) + m_worldCenter[0], (screen[1] - (float)m_screenSize[1] / 2.0) / (TilePixels * m_pixelRatio) + m_worldCenter[1] diff --git a/source/game/scripting/StarCameraLuaBindings.cpp b/source/game/scripting/StarCameraLuaBindings.cpp new file mode 100644 index 0000000..b7128ea --- /dev/null +++ b/source/game/scripting/StarCameraLuaBindings.cpp @@ -0,0 +1,31 @@ +#include "StarCameraLuaBindings.hpp" +#include "StarLuaConverters.hpp" +#include "StarWorldCamera.hpp" +#include "StarRoot.hpp" + +namespace Star { + +LuaCallbacks LuaBindings::makeCameraCallbacks(WorldCamera* camera) { + LuaCallbacks callbacks; + + callbacks.registerCallbackWithSignature("position", bind(&WorldCamera::centerWorldPosition, camera)); + callbacks.registerCallbackWithSignature("pixelRatio", bind(&WorldCamera::pixelRatio, camera)); + callbacks.registerCallback("setPixelRatio", [camera](float pixelRatio, Maybe smooth) { + if (smooth.value()) + camera->setTargetPixelRatio(pixelRatio); + else + camera->setPixelRatio(pixelRatio); + Root::singleton().configuration()->set("zoomLevel", pixelRatio); + }); + + callbacks.registerCallbackWithSignature("screenSize", bind(&WorldCamera::screenSize, camera)); + callbacks.registerCallbackWithSignature("worldScreenRect", bind(&WorldCamera::worldScreenRect, camera)); + callbacks.registerCallbackWithSignature("worldTileRect", bind(&WorldCamera::worldTileRect, camera)); + callbacks.registerCallbackWithSignature("tileMinScreen", bind(&WorldCamera::tileMinScreen, camera)); + callbacks.registerCallbackWithSignature("screenToWorld", bind(&WorldCamera::screenToWorld, camera, _1)); + callbacks.registerCallbackWithSignature("worldToScreen", bind(&WorldCamera::worldToScreen, camera, _1)); + + return callbacks; +} + +} diff --git a/source/game/scripting/StarCameraLuaBindings.hpp b/source/game/scripting/StarCameraLuaBindings.hpp new file mode 100644 index 0000000..b944b3d --- /dev/null +++ b/source/game/scripting/StarCameraLuaBindings.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "StarLua.hpp" + +namespace Star { + +STAR_CLASS(WorldCamera); + +namespace LuaBindings { + LuaCallbacks makeCameraCallbacks(WorldCamera* camera); +} +} diff --git a/source/rendering/CMakeLists.txt b/source/rendering/CMakeLists.txt index 401f16e..f44f109 100644 --- a/source/rendering/CMakeLists.txt +++ b/source/rendering/CMakeLists.txt @@ -16,7 +16,6 @@ SET (star_rendering_HEADERS StarFontTextureGroup.hpp StarTextPainter.hpp StarTilePainter.hpp - StarWorldCamera.hpp StarWorldPainter.hpp ) @@ -28,7 +27,6 @@ SET (star_rendering_SOURCES StarFontTextureGroup.cpp StarTextPainter.cpp StarTilePainter.cpp - StarWorldCamera.cpp StarWorldPainter.cpp )