From dac3ce35844b60d4a89c1c17a85f8b419d6ce958 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Fri, 18 Nov 2022 19:24:45 +0000 Subject: [PATCH] Add JS bindings for in game camera attributes. Patch By: edoput Differential Revision: https://code.wildfiregames.com/D4667 This was SVN commit r27230. --- .../public/gui/credits/texts/programming.json | 1 + .../scripting/JSInterface_GameView.cpp | 40 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/binaries/data/mods/public/gui/credits/texts/programming.json b/binaries/data/mods/public/gui/credits/texts/programming.json index 068cd461c6..76db2746d1 100644 --- a/binaries/data/mods/public/gui/credits/texts/programming.json +++ b/binaries/data/mods/public/gui/credits/texts/programming.json @@ -86,6 +86,7 @@ { "nick": "DynamoFox" }, { "nick": "Echelon9", "name": "Rhys Kidd" }, { "nick": "echotangoecho" }, + { "nick": "edoput", "name": "Edoardo Putti"}, { "nick": "eihrul", "name": "Lee Salzman" }, { "nick": "elexis", "name": "Alexander Heinsius" }, { "nick": "EmjeR", "name": "Matthijs de Rijk" }, diff --git a/source/graphics/scripting/JSInterface_GameView.cpp b/source/graphics/scripting/JSInterface_GameView.cpp index b973e06903..5a9354ccf6 100644 --- a/source/graphics/scripting/JSInterface_GameView.cpp +++ b/source/graphics/scripting/JSInterface_GameView.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -72,17 +72,46 @@ void RegisterScriptFunctions_Settings(const ScriptRequest& rq) #undef REGISTER_BOOLEAN_SCRIPT_SETTING +JS::Value GetCameraRotation(const ScriptRequest& rq) +{ + if (!g_Game || !g_Game->GetView()) + return JS::UndefinedValue(); + + const CVector3D rotation = g_Game->GetView()->GetCameraRotation(); + JS::RootedValue val(rq.cx); + Script::CreateObject(rq, &val, "x", rotation.X, "y", rotation.Y); + return val; +} + +JS::Value GetCameraZoom() +{ + if (!g_Game || !g_Game->GetView()) + return JS::UndefinedValue(); + return JS::NumberValue(g_Game->GetView()->GetCameraZoom()); +} + JS::Value GetCameraPivot(const ScriptRequest& rq) { - CVector3D pivot(-1, -1, -1); - if (g_Game && g_Game->GetView()) - pivot = g_Game->GetView()->GetCameraPivot(); + if (!g_Game || !g_Game->GetView()) + return JS::UndefinedValue(); + const CVector3D pivot = g_Game->GetView()->GetCameraPivot(); JS::RootedValue pivotValue(rq.cx); Script::CreateObject(rq, &pivotValue, "x", pivot.X, "z", pivot.Z); return pivotValue; } +JS::Value GetCameraPosition(const ScriptRequest& rq) +{ + if (!g_Game || !g_Game->GetView()) + return JS::UndefinedValue(); + + const CVector3D position = g_Game->GetView()->GetCameraPosition(); + JS::RootedValue positionValue(rq.cx); + Script::CreateObject(rq, &positionValue, "x", position.X, "y", position.Y, "z", position.Z); + return positionValue; +} + /** * Move camera to a 2D location. */ @@ -166,7 +195,10 @@ void RegisterScriptFunctions(const ScriptRequest& rq) { RegisterScriptFunctions_Settings(rq); + ScriptFunction::Register<&GetCameraRotation>(rq, "GetCameraRotation"); + ScriptFunction::Register<&GetCameraZoom>(rq, "GetCameraZoom"); ScriptFunction::Register<&GetCameraPivot>(rq, "GetCameraPivot"); + ScriptFunction::Register<&GetCameraPosition>(rq, "GetCameraPosition"); ScriptFunction::Register<&CameraMoveTo>(rq, "CameraMoveTo"); ScriptFunction::Register<&SetCameraTarget>(rq, "SetCameraTarget"); ScriptFunction::Register<&SetCameraData>(rq, "SetCameraData");