Changeset View
Standalone View
source/graphics/scripting/JSInterface_GameView.cpp
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2022 Wildfire Games. | ||||||||||||||||||||||||
Silier: ^ | |||||||||||||||||||||||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||||||||||||||||||||||
* | * | ||||||||||||||||||||||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||||||||||||||||||||||
* (at your option) any later version. | * (at your option) any later version. | ||||||||||||||||||||||||
* | * | ||||||||||||||||||||||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||||||||||||||||||||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||||||||||||||||||||||
{ | { | ||||||||||||||||||||||||
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); | ||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||
#undef REGISTER_BOOLEAN_SCRIPT_SETTING | #undef REGISTER_BOOLEAN_SCRIPT_SETTING | ||||||||||||||||||||||||
JS::Value GetCameraRotation(const ScriptRequest& rq) | |||||||||||||||||||||||||
Done Inline ActionsWhy it's called rotation if it returns direction (or sin/cos of that angles)? vladislavbelov: Why it's called rotation if it returns direction (or sin/cos of that angles)? | |||||||||||||||||||||||||
{ | |||||||||||||||||||||||||
if (!g_Game || !g_Game->GetView()) | |||||||||||||||||||||||||
Done Inline ActionsIt seems we use -1 logic as an error, can we replace it by returning undefined? Since -1 is a correct value for angles or vectors. vladislavbelov: It seems we use `-1` logic as an error, can we replace it by returning `undefined`? Since `-1`… | |||||||||||||||||||||||||
return JS::UndefinedValue(); | |||||||||||||||||||||||||
const CVector3D rotation = g_Game->GetView()->GetCameraRotation(); | |||||||||||||||||||||||||
Done Inline Actionsconst CVector3D rotation = .... vladislavbelov: `const CVector3D rotation = ...`. | |||||||||||||||||||||||||
JS::RootedValue val(rq.cx); | |||||||||||||||||||||||||
Script::CreateObject(rq, &val, "x", rotation.X, "y", rotation.Y); | |||||||||||||||||||||||||
Done Inline ActionsIs Y correct here? GetIn() is direction, not angles. vladislavbelov: Is `Y` correct here? `GetIn()` is direction, not angles. | |||||||||||||||||||||||||
Done Inline ActionsI am not sure what the correct approach is. I just followed what the "reset orientation" would do in C++. The current "reset orientation" uses the values from the config which is why it's returning them as x,y. I see there are more methods for getting the rotation as angles instead but SetCameraData uses two values x,y to determine the rotation. I have interpreted the pos.X and pos.Y as the rotations rotx, roty in the call to SetCameraData but I may be mistaken. Would this use-case better be implemented using GetYRotation()? I'm not sure how to proceed and if the issue is just the naming of the properties x, y in the object I can leave a comment to explain they are edoput: I am not sure what the correct approach is. I just followed what the "reset orientation" would… | |||||||||||||||||||||||||
return val; | |||||||||||||||||||||||||
} | |||||||||||||||||||||||||
JS::Value GetCameraZoom() | |||||||||||||||||||||||||
{ | |||||||||||||||||||||||||
Not Done Inline Actions
A bit less convoluted without braces. Stan: A bit less convoluted without braces. | |||||||||||||||||||||||||
if (!g_Game || !g_Game->GetView()) | |||||||||||||||||||||||||
return JS::UndefinedValue(); | |||||||||||||||||||||||||
return JS::NumberValue(g_Game->GetView()->GetCameraZoom()); | |||||||||||||||||||||||||
} | |||||||||||||||||||||||||
Not Done Inline ActionsCan't you return a float directly? wraitii: Can't you return a float directly? | |||||||||||||||||||||||||
Not Done Inline ActionsThat would be a not good practice. vladislavbelov: That would be a not good practice. | |||||||||||||||||||||||||
Not Done Inline Actionsmh, yeah I guess the answer is 'no because we haven't implemented those to avoid errors in the sim', but I'm not sure going through JS::NumberValue is the best call. wraitii: mh, yeah I guess the answer is 'no because we haven't implemented those to avoid errors in the… | |||||||||||||||||||||||||
Not Done Inline ActionsSorry, it was the answer for:
Missed the field. vladislavbelov: Sorry, it was the answer for:
> Unrelated, but this sounds like you could just read the config… | |||||||||||||||||||||||||
Not Done Inline ActionsAh, yeah, makes more sense. I asked because I was wondering if maybe edoput missed that reading the config existed or some such. wraitii: Ah, yeah, makes more sense. I asked because I was wondering if maybe edoput missed that reading… | |||||||||||||||||||||||||
Done Inline ActionsI think the config is unrelated to my problem as of now. While I can read the config values rotate.y.default and rotate.x.default from the config and I can set them I cannot read the current camera angles from the config. In the mod I am working on I also don't set the rotate.x.default and rotate.y.default because they should be limited to the session lifetime edoput: I think the config is unrelated to my problem as of now.
While I can read the config values… | |||||||||||||||||||||||||
Done Inline Actionswhat would be the best call? Would JS::Float32Value be better? edoput: what would be the best call? Would JS::Float32Value be better? | |||||||||||||||||||||||||
Not Done Inline Actionsno if you can't just return float, returning JS::NumberValue seems fine. I think perhaps an explicit<float> type would be good to avoid accidentally returning floats in the sim, but this os utterly out of scope.
Yes I was just remarking on that tbh. wraitii: no if you can't just return float, returning JS::NumberValue seems fine.
I think perhaps an… | |||||||||||||||||||||||||
Not Done Inline ActionsI assume you can return undefined there too ? Stan: I assume you can return undefined there too ? | |||||||||||||||||||||||||
JS::Value GetCameraPivot(const ScriptRequest& rq) | JS::Value GetCameraPivot(const ScriptRequest& rq) | ||||||||||||||||||||||||
{ | { | ||||||||||||||||||||||||
CVector3D pivot(-1, -1, -1); | if (!g_Game || !g_Game->GetView()) | ||||||||||||||||||||||||
if (g_Game && g_Game->GetView()) | return JS::UndefinedValue(); | ||||||||||||||||||||||||
pivot = g_Game->GetView()->GetCameraPivot(); | CVector3D pivot = g_Game->GetView()->GetCameraPivot(); | ||||||||||||||||||||||||
Not Done Inline ActionsI think it's better to return undefined value here as well. vladislavbelov: I think it's better to return undefined value here as well. | |||||||||||||||||||||||||
phositUnsubmitted Not Done Inline Actionsconst phosit: const | |||||||||||||||||||||||||
JS::RootedValue pivotValue(rq.cx); | JS::RootedValue pivotValue(rq.cx); | ||||||||||||||||||||||||
Script::CreateObject(rq, &pivotValue, "x", pivot.X, "z", pivot.Z); | Script::CreateObject(rq, &pivotValue, "x", pivot.X, "z", pivot.Z); | ||||||||||||||||||||||||
return pivotValue; | return pivotValue; | ||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||
JS::Value GetCameraPosition(const ScriptRequest& rq) | |||||||||||||||||||||||||
{ | |||||||||||||||||||||||||
if (!g_Game || !g_Game->GetView()) | |||||||||||||||||||||||||
return JS::UndefinedValue(); | |||||||||||||||||||||||||
CVector3D camera = g_Game->GetView()->GetCameraPosition(); | |||||||||||||||||||||||||
Not Done Inline ActionsI think it's better to return undefined value here as well. vladislavbelov: I think it's better to return undefined value here as well. | |||||||||||||||||||||||||
phositUnsubmitted Not Done Inline Actionsconst phosit: const | |||||||||||||||||||||||||
SilierUnsubmitted Not Done Inline ActionsAlso probably this should be called position instead camera but thats detail Silier: Also probably this should be called position instead camera but thats detail | |||||||||||||||||||||||||
JS::RootedValue positionValue(rq.cx); | |||||||||||||||||||||||||
Script::CreateObject(rq, &positionValue, "x", camera.X, "y", camera.Y, "z", camera.Z); | |||||||||||||||||||||||||
return positionValue; | |||||||||||||||||||||||||
} | |||||||||||||||||||||||||
/** | /** | ||||||||||||||||||||||||
* Move camera to a 2D location. | * Move camera to a 2D location. | ||||||||||||||||||||||||
*/ | */ | ||||||||||||||||||||||||
void CameraMoveTo(entity_pos_t x, entity_pos_t z) | void 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; | ||||||||||||||||||||||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | CFixedVector3D 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 RegisterScriptFunctions(const ScriptRequest& rq) | void RegisterScriptFunctions(const ScriptRequest& rq) | ||||||||||||||||||||||||
{ | { | ||||||||||||||||||||||||
RegisterScriptFunctions_Settings(rq); | RegisterScriptFunctions_Settings(rq); | ||||||||||||||||||||||||
ScriptFunction::Register<&GetCameraRotation>(rq, "GetCameraRotation"); | |||||||||||||||||||||||||
ScriptFunction::Register<&GetCameraZoom>(rq, "GetCameraZoom"); | |||||||||||||||||||||||||
ScriptFunction::Register<&GetCameraPivot>(rq, "GetCameraPivot"); | ScriptFunction::Register<&GetCameraPivot>(rq, "GetCameraPivot"); | ||||||||||||||||||||||||
ScriptFunction::Register<&GetCameraPosition>(rq, "GetCameraPosition"); | |||||||||||||||||||||||||
ScriptFunction::Register<&CameraMoveTo>(rq, "CameraMoveTo"); | ScriptFunction::Register<&CameraMoveTo>(rq, "CameraMoveTo"); | ||||||||||||||||||||||||
ScriptFunction::Register<&SetCameraTarget>(rq, "SetCameraTarget"); | ScriptFunction::Register<&SetCameraTarget>(rq, "SetCameraTarget"); | ||||||||||||||||||||||||
ScriptFunction::Register<&SetCameraData>(rq, "SetCameraData"); | ScriptFunction::Register<&SetCameraData>(rq, "SetCameraData"); | ||||||||||||||||||||||||
ScriptFunction::Register<&CameraFollow>(rq, "CameraFollow"); | ScriptFunction::Register<&CameraFollow>(rq, "CameraFollow"); | ||||||||||||||||||||||||
ScriptFunction::Register<&CameraFollowFPS>(rq, "CameraFollowFPS"); | ScriptFunction::Register<&CameraFollowFPS>(rq, "CameraFollowFPS"); | ||||||||||||||||||||||||
ScriptFunction::Register<&GetFollowedEntity>(rq, "GetFollowedEntity"); | ScriptFunction::Register<&GetFollowedEntity>(rq, "GetFollowedEntity"); | ||||||||||||||||||||||||
ScriptFunction::Register<&GetTerrainAtScreenPoint>(rq, "GetTerrainAtScreenPoint"); | ScriptFunction::Register<&GetTerrainAtScreenPoint>(rq, "GetTerrainAtScreenPoint"); | ||||||||||||||||||||||||
} | } | ||||||||||||||||||||||||
} | } |
^