LCOV - code coverage report
Current view: top level - source/graphics/scripting - JSInterface_GameView.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 16 55 29.1 %
Date: 2022-06-14 00:41:00 Functions: 2 16 12.5 %

          Line data    Source code
       1             : /* Copyright (C) 2021 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "JSInterface_GameView.h"
      21             : 
      22             : #include "graphics/Camera.h"
      23             : #include "graphics/GameView.h"
      24             : #include "graphics/Terrain.h"
      25             : #include "maths/FixedVector3D.h"
      26             : #include "ps/Game.h"
      27             : #include "ps/World.h"
      28             : #include "ps/CLogger.h"
      29             : #include "scriptinterface/FunctionWrapper.h"
      30             : #include "simulation2/helpers/Position.h"
      31             : 
      32             : namespace JSI_GameView
      33             : {
      34             : #define IMPLEMENT_BOOLEAN_SCRIPT_SETTING(NAME) \
      35             : bool Get##NAME##Enabled() \
      36             : { \
      37             :     if (!g_Game || !g_Game->GetView()) \
      38             :     { \
      39             :         LOGERROR("Trying to get a setting from GameView when it's not initialized!"); \
      40             :         return false; \
      41             :     } \
      42             :     return g_Game->GetView()->Get##NAME##Enabled(); \
      43             : } \
      44             : \
      45             : void Set##NAME##Enabled(bool Enabled) \
      46             : { \
      47             :     if (!g_Game || !g_Game->GetView()) \
      48             :     { \
      49             :         LOGERROR("Trying to set a setting of GameView when it's not initialized!"); \
      50             :         return; \
      51             :     } \
      52             :     g_Game->GetView()->Set##NAME##Enabled(Enabled); \
      53             : }
      54             : 
      55           0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(Culling);
      56           0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(LockCullCamera);
      57           0 : IMPLEMENT_BOOLEAN_SCRIPT_SETTING(ConstrainCamera);
      58             : 
      59             : #undef IMPLEMENT_BOOLEAN_SCRIPT_SETTING
      60             : 
      61             : 
      62             : #define REGISTER_BOOLEAN_SCRIPT_SETTING(NAME) \
      63             :     ScriptFunction::Register<&Get##NAME##Enabled>(rq, "GameView_Get" #NAME "Enabled"); \
      64             :     ScriptFunction::Register<&Set##NAME##Enabled>(rq, "GameView_Set" #NAME "Enabled");
      65             : 
      66          11 : void RegisterScriptFunctions_Settings(const ScriptRequest& rq)
      67             : {
      68          11 :     REGISTER_BOOLEAN_SCRIPT_SETTING(Culling);
      69          11 :     REGISTER_BOOLEAN_SCRIPT_SETTING(LockCullCamera);
      70          11 :     REGISTER_BOOLEAN_SCRIPT_SETTING(ConstrainCamera);
      71          11 : }
      72             : 
      73             : #undef REGISTER_BOOLEAN_SCRIPT_SETTING
      74             : 
      75           0 : JS::Value GetCameraPivot(const ScriptRequest& rq)
      76             : {
      77           0 :     CVector3D pivot(-1, -1, -1);
      78           0 :     if (g_Game && g_Game->GetView())
      79           0 :         pivot = g_Game->GetView()->GetCameraPivot();
      80             : 
      81           0 :     JS::RootedValue pivotValue(rq.cx);
      82           0 :     Script::CreateObject(rq, &pivotValue, "x", pivot.X, "z", pivot.Z);
      83           0 :     return pivotValue;
      84             : }
      85             : 
      86             : /**
      87             :  * Move camera to a 2D location.
      88             :  */
      89           0 : void CameraMoveTo(entity_pos_t x, entity_pos_t z)
      90             : {
      91           0 :     if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView() || !g_Game->GetWorld()->GetTerrain())
      92           0 :         return;
      93             : 
      94           0 :     CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
      95             : 
      96           0 :     CVector3D target;
      97           0 :     target.X = x.ToFloat();
      98           0 :     target.Z = z.ToFloat();
      99           0 :     target.Y = terrain->GetExactGroundLevel(target.X, target.Z);
     100             : 
     101           0 :     g_Game->GetView()->MoveCameraTarget(target);
     102             : }
     103             : 
     104             : /**
     105             :  * Set the camera to look at the given location.
     106             :  */
     107           0 : void SetCameraTarget(float x, float y, float z)
     108             : {
     109           0 :     if (!g_Game || !g_Game->GetView())
     110             :         return;
     111           0 :     g_Game->GetView()->MoveCameraTarget(CVector3D(x, y, z));
     112             : }
     113             : 
     114             : /**
     115             :  * Set the data (position, orientation and zoom) of the camera.
     116             :  */
     117           0 : void SetCameraData(entity_pos_t x, entity_pos_t y, entity_pos_t z, entity_pos_t rotx, entity_pos_t roty, entity_pos_t zoom)
     118             : {
     119           0 :     if (!g_Game || !g_Game->GetView())
     120           0 :         return;
     121             : 
     122           0 :     CVector3D pos(x.ToFloat(), y.ToFloat(), z.ToFloat());
     123             : 
     124           0 :     g_Game->GetView()->SetCamera(pos, rotx.ToFloat(), roty.ToFloat(), zoom.ToFloat());
     125             : }
     126             : 
     127             : /**
     128             :  * Start / stop camera following mode.
     129             :  * @param entityid unit id to follow. If zero, stop following mode
     130             :  */
     131           0 : void CameraFollow(entity_id_t entityid)
     132             : {
     133           0 :     if (!g_Game || !g_Game->GetView())
     134             :         return;
     135             : 
     136           0 :     g_Game->GetView()->FollowEntity(entityid, false);
     137             : }
     138             : 
     139             : /**
     140             :  * Start / stop first-person camera following mode.
     141             :  * @param entityid unit id to follow. If zero, stop following mode.
     142             :  */
     143           0 : void CameraFollowFPS(entity_id_t entityid)
     144             : {
     145           0 :     if (!g_Game || !g_Game->GetView())
     146             :         return;
     147             : 
     148           0 :     g_Game->GetView()->FollowEntity(entityid, true);
     149             : }
     150             : 
     151           0 : entity_id_t GetFollowedEntity()
     152             : {
     153           0 :     if (!g_Game || !g_Game->GetView())
     154             :         return INVALID_ENTITY;
     155             : 
     156           0 :     return g_Game->GetView()->GetFollowedEntity();
     157             : }
     158             : 
     159           0 : CFixedVector3D GetTerrainAtScreenPoint(int x, int y)
     160             : {
     161           0 :     CVector3D pos = g_Game->GetView()->GetCamera()->GetWorldCoordinates(x, y, true);
     162           0 :     return CFixedVector3D(fixed::FromFloat(pos.X), fixed::FromFloat(pos.Y), fixed::FromFloat(pos.Z));
     163             : }
     164             : 
     165          11 : void RegisterScriptFunctions(const ScriptRequest& rq)
     166             : {
     167          11 :     RegisterScriptFunctions_Settings(rq);
     168             : 
     169          11 :     ScriptFunction::Register<&GetCameraPivot>(rq, "GetCameraPivot");
     170          11 :     ScriptFunction::Register<&CameraMoveTo>(rq, "CameraMoveTo");
     171          11 :     ScriptFunction::Register<&SetCameraTarget>(rq, "SetCameraTarget");
     172          11 :     ScriptFunction::Register<&SetCameraData>(rq, "SetCameraData");
     173          11 :     ScriptFunction::Register<&CameraFollow>(rq, "CameraFollow");
     174          11 :     ScriptFunction::Register<&CameraFollowFPS>(rq, "CameraFollowFPS");
     175          11 :     ScriptFunction::Register<&GetFollowedEntity>(rq, "GetFollowedEntity");
     176          11 :     ScriptFunction::Register<&GetTerrainAtScreenPoint>(rq, "GetTerrainAtScreenPoint");
     177          11 : }
     178             : }

Generated by: LCOV version 1.13