Skip to content

Commit

Permalink
[CSM] Add function to set the FOV of the local player
Browse files Browse the repository at this point in the history
Adress reviews
  • Loading branch information
bigfoot547 committed Jun 19, 2017
1 parent 39f4a2f commit fd347f9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
13 changes: 12 additions & 1 deletion doc/client_lua_api.md
Expand Up @@ -848,10 +848,21 @@ Please do not try to access the reference until the camera is initialized, other
x = number,
y = number,
max = number,
actual = number
actual = number,
zoom = number
}
```

* `set_fov(fov_degrees, [persist])`
* Sets the FOV for the local player
* Pass true to the second argument to save the zoom fov to the setting.
* `set_zoom_fov(fov_degrees [persist])`
* Sets the FOV for the local player when zoomed in
* Pass true to the second argument to save the zoom fov to the setting.
* `save_fov()`
* Sets the "fov" setting to `get_fov().actual`
* `save_zoom_fov()`
* Sets the "zoom_fov" setting to `get_fov().zoom`
* `get_pos()`
* Returns position of camera with view bobbing
* `get_offset()`
Expand Down
25 changes: 25 additions & 0 deletions src/camera.h
Expand Up @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <ICameraSceneNode.h>
#include <ISceneNode.h>
#include <list>
#include "settings.h" // For g_settings

#include "client.h"

Expand Down Expand Up @@ -91,6 +92,30 @@ class Camera
return m_camera_offset;
}

inline void setFov(f32 fov, bool persist)
{
m_cache_fov = fov;
if (persist)
g_settings->setFloat("fov", getFov());
}

inline void setZoomFov(f32 fov, bool persist)
{
m_cache_zoom_fov = fov;
if (persist)
g_settings->setFloat("zoom_fov", getZoomFov());
}

inline f32 getFov() const
{
return m_cache_fov;
}

inline f32 getZoomFov() const
{
return m_cache_zoom_fov;
}

// Horizontal field of view
inline f32 getFovX() const
{
Expand Down
68 changes: 66 additions & 2 deletions src/script/lua_api/l_camera.cpp
Expand Up @@ -64,13 +64,75 @@ int LuaCamera::l_get_fov(lua_State *L)
lua_setfield(L, -2, "x");
lua_pushnumber(L, camera->getFovY() * core::DEGTORAD);
lua_setfield(L, -2, "y");
lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG);
lua_setfield(L, -2, "actual");
lua_pushnumber(L, camera->getFovMax() * core::RADTODEG);
lua_setfield(L, -2, "max");
lua_pushnumber(L, camera->getFov());
lua_setfield(L, -2, "actual");
lua_pushnumber(L, camera->getZoomFov());
lua_setfield(L, -2, "zoom");

return 1;
}

int LuaCamera::l_set_fov(lua_State *L)
{
Camera *camera = getobject(L, 1);

if (!camera)
return 0;

if (!lua_isnumber(L, 2))
return 0;

if (!lua_isboolean(L, 3))
bool persist = false;
else
bool persist = lua_getboolean(L, 3);

float fov = lua_tonumber(L, 2);
camera->setFov(fov, persist);
return 0;
}

int LuaCamera::l_set_zoom_fov(lua_State *L)
{
Camera *camera = getobject(L, 1);
if (!camera)
return 0;

if (!lua_isnumber(L, 2))
return 0;

if (!lua_isboolean(L, 3))
bool persist = false;
else
bool persist = lua_getboolean(L, 3);

float fov = lua_tonumber(L, 2);
camera->setZoomFov(fov, persist);
return 0;
}

int LuaCamera::l_save_fov(lua_State *L)
{
Camera *camera = getobject(L, 1);
if (!camera)
return 0;

camera->saveFov();
return 0;
}

int LuaCamera::l_save_zoom_fov(lua_State *L)
{
Camera *camera = getobject(L, 1);
if (!camera)
return 0;

camera->saveZoomFov();
return 0;
}

int LuaCamera::l_get_pos(lua_State *L)
{
Camera *camera = getobject(L, 1);
Expand Down Expand Up @@ -193,6 +255,8 @@ void LuaCamera::Register(lua_State *L)
const char LuaCamera::className[] = "Camera";
const luaL_Reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode),
luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov),
luamethod(LuaCamera, set_fov), luamethod(LuaCamera, set_zoom_fov),
luamethod(LuaCamera, save_fov), luamethod(LuaCamera, save_zoom_fov),
luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset),
luamethod(LuaCamera, get_look_dir),
luamethod(LuaCamera, get_look_vertical),
Expand Down
5 changes: 5 additions & 0 deletions src/script/lua_api/l_camera.h
Expand Up @@ -18,6 +18,11 @@ class LuaCamera : public ModApiBase
static int l_get_camera_mode(lua_State *L);

static int l_get_fov(lua_State *L);
static int l_set_fov(lua_State *L);
static int l_set_zoom_fov(lua_State *L);

static int l_save_fov(lua_State *L);
static int l_save_zoom_fov(lua_State *L);

static int l_get_pos(lua_State *L);
static int l_get_offset(lua_State *L);
Expand Down

0 comments on commit fd347f9

Please sign in to comment.