Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/scripting/JSInterface_GameView.cpp
Show All 19 Lines | |||||
#include "JSInterface_GameView.h" | #include "JSInterface_GameView.h" | ||||
#include "graphics/Camera.h" | #include "graphics/Camera.h" | ||||
#include "graphics/GameView.h" | #include "graphics/GameView.h" | ||||
#include "graphics/Terrain.h" | #include "graphics/Terrain.h" | ||||
#include "ps/Game.h" | #include "ps/Game.h" | ||||
#include "ps/World.h" | #include "ps/World.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#define IMPLEMENT_BOOLEAN_SCRIPT_SETTING(NAME) \ | #define IMPLEMENT_BOOLEAN_SCRIPT_SETTING(NAME) \ | ||||
bool JSI_GameView::Get##NAME##Enabled(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) \ | bool JSI_GameView::Get##NAME##Enabled() \ | ||||
{ \ | { \ | ||||
if (!g_Game || !g_Game->GetView()) \ | if (!g_Game || !g_Game->GetView()) \ | ||||
{ \ | { \ | ||||
LOGERROR("Trying to get a setting from GameView when it's not initialized!"); \ | LOGERROR("Trying to get a setting from GameView when it's not initialized!"); \ | ||||
return false; \ | return false; \ | ||||
} \ | } \ | ||||
return g_Game->GetView()->Get##NAME##Enabled(); \ | return g_Game->GetView()->Get##NAME##Enabled(); \ | ||||
} \ | } \ | ||||
\ | \ | ||||
void JSI_GameView::Set##NAME##Enabled(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), bool Enabled) \ | void JSI_GameView::Set##NAME##Enabled(bool Enabled) \ | ||||
{ \ | { \ | ||||
if (!g_Game || !g_Game->GetView()) \ | if (!g_Game || !g_Game->GetView()) \ | ||||
{ \ | { \ | ||||
LOGERROR("Trying to set a setting of GameView when it's not initialized!"); \ | LOGERROR("Trying to set a setting of GameView when it's not initialized!"); \ | ||||
return; \ | return; \ | ||||
} \ | } \ | ||||
g_Game->GetView()->Set##NAME##Enabled(Enabled); \ | g_Game->GetView()->Set##NAME##Enabled(Enabled); \ | ||||
} | } | ||||
IMPLEMENT_BOOLEAN_SCRIPT_SETTING(Culling); | IMPLEMENT_BOOLEAN_SCRIPT_SETTING(Culling); | ||||
IMPLEMENT_BOOLEAN_SCRIPT_SETTING(LockCullCamera); | IMPLEMENT_BOOLEAN_SCRIPT_SETTING(LockCullCamera); | ||||
IMPLEMENT_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); | IMPLEMENT_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); | ||||
#undef IMPLEMENT_BOOLEAN_SCRIPT_SETTING | #undef IMPLEMENT_BOOLEAN_SCRIPT_SETTING | ||||
#define REGISTER_BOOLEAN_SCRIPT_SETTING(NAME) \ | #define REGISTER_BOOLEAN_SCRIPT_SETTING(NAME) \ | ||||
scriptInterface.RegisterFunction<bool, &JSI_GameView::Get##NAME##Enabled>("GameView_Get" #NAME "Enabled"); \ | ScriptWrapper::WrapAndRegister<decltype(&JSI_GameView::Get##NAME##Enabled), &JSI_GameView::Get##NAME##Enabled>(scriptInterface, "GameView_Get" #NAME "Enabled"); \ | ||||
scriptInterface.RegisterFunction<void, bool, &JSI_GameView::Set##NAME##Enabled>("GameView_Set" #NAME "Enabled"); | ScriptWrapper::WrapAndRegister<decltype(&JSI_GameView::Set##NAME##Enabled), &JSI_GameView::Set##NAME##Enabled>(scriptInterface, "GameView_Set" #NAME "Enabled") | ||||
void JSI_GameView::RegisterScriptFunctions_Settings(const ScriptInterface& scriptInterface) | void JSI_GameView::RegisterScriptFunctions_Settings(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
REGISTER_BOOLEAN_SCRIPT_SETTING(Culling); | REGISTER_BOOLEAN_SCRIPT_SETTING(Culling); | ||||
REGISTER_BOOLEAN_SCRIPT_SETTING(LockCullCamera); | REGISTER_BOOLEAN_SCRIPT_SETTING(LockCullCamera); | ||||
REGISTER_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); | REGISTER_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); | ||||
} | } | ||||
Show All 10 Lines | JS::Value JSI_GameView::GetCameraPivot(ScriptInterface::CxPrivate* pCxPrivate) | ||||
JS::RootedValue pivotValue(cx); | JS::RootedValue pivotValue(cx); | ||||
ScriptInterface::CreateObject(cx, &pivotValue, "x", pivot.X, "z", pivot.Z); | ScriptInterface::CreateObject(cx, &pivotValue, "x", pivot.X, "z", pivot.Z); | ||||
return pivotValue; | return pivotValue; | ||||
} | } | ||||
/** | /** | ||||
* Move camera to a 2D location. | * Move camera to a 2D location. | ||||
*/ | */ | ||||
void JSI_GameView::CameraMoveTo(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_pos_t x, entity_pos_t z) | void JSI_GameView::CameraMoveTo(entity_pos_t x, entity_pos_t z) | ||||
{ | { | ||||
if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView() || !g_Game->GetWorld()->GetTerrain()) | if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView() || !g_Game->GetWorld()->GetTerrain()) | ||||
return; | return; | ||||
CTerrain* terrain = g_Game->GetWorld()->GetTerrain(); | CTerrain* terrain = g_Game->GetWorld()->GetTerrain(); | ||||
CVector3D target; | CVector3D target; | ||||
target.X = x.ToFloat(); | target.X = x.ToFloat(); | ||||
target.Z = z.ToFloat(); | target.Z = z.ToFloat(); | ||||
target.Y = terrain->GetExactGroundLevel(target.X, target.Z); | target.Y = terrain->GetExactGroundLevel(target.X, target.Z); | ||||
g_Game->GetView()->MoveCameraTarget(target); | g_Game->GetView()->MoveCameraTarget(target); | ||||
} | } | ||||
/** | /** | ||||
* Set the camera to look at the given location. | * Set the camera to look at the given location. | ||||
*/ | */ | ||||
void JSI_GameView::SetCameraTarget(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), float x, float y, float z) | void JSI_GameView::SetCameraTarget(float x, float y, float z) | ||||
{ | { | ||||
g_Game->GetView()->ResetCameraTarget(CVector3D(x, y, z)); | g_Game->GetView()->ResetCameraTarget(CVector3D(x, y, z)); | ||||
} | } | ||||
/** | /** | ||||
* Set the data (position, orientation and zoom) of the camera. | * Set the data (position, orientation and zoom) of the camera. | ||||
*/ | */ | ||||
void JSI_GameView::SetCameraData(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_pos_t x, entity_pos_t y, entity_pos_t z, entity_pos_t rotx, entity_pos_t roty, entity_pos_t zoom) | void JSI_GameView::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) | ||||
{ | { | ||||
if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView() || !g_Game->GetWorld()->GetTerrain()) | if (!g_Game || !g_Game->GetWorld() || !g_Game->GetView() || !g_Game->GetWorld()->GetTerrain()) | ||||
return; | return; | ||||
CVector3D Pos = CVector3D(x.ToFloat(), y.ToFloat(), z.ToFloat()); | CVector3D Pos = CVector3D(x.ToFloat(), y.ToFloat(), z.ToFloat()); | ||||
float RotX = rotx.ToFloat(); | float RotX = rotx.ToFloat(); | ||||
float RotY = roty.ToFloat(); | float RotY = roty.ToFloat(); | ||||
float Zoom = zoom.ToFloat(); | float Zoom = zoom.ToFloat(); | ||||
g_Game->GetView()->SetCamera(Pos, RotX, RotY, Zoom); | g_Game->GetView()->SetCamera(Pos, RotX, RotY, Zoom); | ||||
} | } | ||||
/** | /** | ||||
* Start / stop camera following mode. | * Start / stop camera following mode. | ||||
* @param entityid unit id to follow. If zero, stop following mode | * @param entityid unit id to follow. If zero, stop following mode | ||||
*/ | */ | ||||
void JSI_GameView::CameraFollow(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_id_t entityid) | void JSI_GameView::CameraFollow(entity_id_t entityid) | ||||
{ | { | ||||
if (!g_Game || !g_Game->GetView()) | if (!g_Game || !g_Game->GetView()) | ||||
return; | return; | ||||
g_Game->GetView()->FollowEntity(entityid, false); | g_Game->GetView()->FollowEntity(entityid, false); | ||||
} | } | ||||
/** | /** | ||||
* Start / stop first-person camera following mode. | * Start / stop first-person camera following mode. | ||||
* @param entityid unit id to follow. If zero, stop following mode. | * @param entityid unit id to follow. If zero, stop following mode. | ||||
*/ | */ | ||||
void JSI_GameView::CameraFollowFPS(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), entity_id_t entityid) | void JSI_GameView::CameraFollowFPS(entity_id_t entityid) | ||||
{ | { | ||||
if (!g_Game || !g_Game->GetView()) | if (!g_Game || !g_Game->GetView()) | ||||
return; | return; | ||||
g_Game->GetView()->FollowEntity(entityid, true); | g_Game->GetView()->FollowEntity(entityid, true); | ||||
} | } | ||||
entity_id_t JSI_GameView::GetFollowedEntity(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | entity_id_t JSI_GameView::GetFollowedEntity() | ||||
{ | { | ||||
if (!g_Game || !g_Game->GetView()) | if (!g_Game || !g_Game->GetView()) | ||||
return INVALID_ENTITY; | return INVALID_ENTITY; | ||||
return g_Game->GetView()->GetFollowedEntity(); | return g_Game->GetView()->GetFollowedEntity(); | ||||
} | } | ||||
CFixedVector3D JSI_GameView::GetTerrainAtScreenPoint(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x, int y) | CFixedVector3D JSI_GameView::GetTerrainAtScreenPoint(int x, int y) | ||||
{ | { | ||||
CVector3D pos = g_Game->GetView()->GetCamera()->GetWorldCoordinates(x, y, true); | CVector3D pos = g_Game->GetView()->GetCamera()->GetWorldCoordinates(x, y, true); | ||||
return CFixedVector3D(fixed::FromFloat(pos.X), fixed::FromFloat(pos.Y), fixed::FromFloat(pos.Z)); | return CFixedVector3D(fixed::FromFloat(pos.X), fixed::FromFloat(pos.Y), fixed::FromFloat(pos.Z)); | ||||
} | } | ||||
void JSI_GameView::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_GameView::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
RegisterScriptFunctions_Settings(scriptInterface); | RegisterScriptFunctions_Settings(scriptInterface); | ||||
scriptInterface.RegisterFunction<JS::Value, &GetCameraPivot>("GetCameraPivot"); | #define Register(name) ScriptWrapper::WrapAndRegister<decltype(&name), &name>(scriptInterface, #name) | ||||
scriptInterface.RegisterFunction<void, entity_pos_t, entity_pos_t, &CameraMoveTo>("CameraMoveTo"); | Register(GetCameraPivot); | ||||
scriptInterface.RegisterFunction<void, float, float, float, &SetCameraTarget>("SetCameraTarget"); | Register(CameraMoveTo); | ||||
scriptInterface.RegisterFunction<void, entity_pos_t, entity_pos_t, entity_pos_t, entity_pos_t, entity_pos_t, entity_pos_t, &SetCameraData>("SetCameraData"); | Register(SetCameraTarget); | ||||
scriptInterface.RegisterFunction<void, entity_id_t, &CameraFollow>("CameraFollow"); | Register(SetCameraData); | ||||
scriptInterface.RegisterFunction<void, entity_id_t, &CameraFollowFPS>("CameraFollowFPS"); | Register(CameraFollow); | ||||
scriptInterface.RegisterFunction<entity_id_t, &GetFollowedEntity>("GetFollowedEntity"); | Register(CameraFollowFPS); | ||||
scriptInterface.RegisterFunction<CFixedVector3D, int, int, &GetTerrainAtScreenPoint>("GetTerrainAtScreenPoint"); | Register(GetFollowedEntity); | ||||
Register(GetTerrainAtScreenPoint); | |||||
#undef Register | |||||
} | } |
Wildfire Games · Phabricator