diff --git a/binaries/data/mods/public/gui/session/session.xml b/binaries/data/mods/public/gui/session/session.xml index b59627625d..9e649f5ced 100644 --- a/binaries/data/mods/public/gui/session/session.xml +++ b/binaries/data/mods/public/gui/session/session.xml @@ -290,7 +290,7 @@ Restrict camera - gameView.constrainCamera = this.checked; + Engine.GameView_SetConstrainCameraEnabled(this.checked); Reveal map diff --git a/source/graphics/GameView.cpp b/source/graphics/GameView.cpp index ed5909afa9..81e19117c0 100644 --- a/source/graphics/GameView.cpp +++ b/source/graphics/GameView.cpp @@ -34,6 +34,7 @@ #include "graphics/TerritoryTexture.h" #include "graphics/Unit.h" #include "graphics/UnitManager.h" +#include "graphics/scripting/JSInterface_GameView.h" #include "lib/input.h" #include "lib/timer.h" #include "maths/BoundingBoxAligned.h" @@ -54,7 +55,6 @@ #include "ps/World.h" #include "renderer/Renderer.h" #include "renderer/WaterManager.h" -#include "scripting/ScriptableObject.h" #include "simulation2/Simulation2.h" #include "simulation2/components/ICmpPosition.h" #include "simulation2/components/ICmpRangeManager.h" @@ -148,7 +148,7 @@ public: float m_Smoothness; }; -class CGameViewImpl : public CJSObject +class CGameViewImpl { NONCOPYABLE(CGameViewImpl); public: @@ -308,10 +308,25 @@ public: CSmoothedValue Zoom; CSmoothedValue RotateX; // inclination around x axis (relative to camera) CSmoothedValue RotateY; // rotation around y (vertical) axis - - static void ScriptingInit(); }; +#define IMPLEMENT_BOOLEAN_SETTING(NAME) \ +bool CGameView::Get##NAME##Enabled() \ +{ \ + return m->NAME; \ +} \ +\ +void CGameView::Set##NAME##Enabled(bool Enabled) \ +{ \ + m->NAME = Enabled; \ +} + +IMPLEMENT_BOOLEAN_SETTING(Culling); +IMPLEMENT_BOOLEAN_SETTING(LockCullCamera); +IMPLEMENT_BOOLEAN_SETTING(ConstrainCamera); + +#undef IMPLEMENT_BOOLEAN_SETTING + static void SetupCameraMatrixSmooth(CGameViewImpl* m, CMatrix3D* orientation) { orientation->SetIdentity(); @@ -372,16 +387,6 @@ CObjectManager& CGameView::GetObjectManager() const return m->ObjectManager; } -JSObject* CGameView::GetScript() -{ - return m->GetScript(); -} - -/*static*/ void CGameView::ScriptingInit() -{ - return CGameViewImpl::ScriptingInit(); -} - CCamera* CGameView::GetCamera() { return &m->ViewCamera; @@ -402,16 +407,6 @@ CTerritoryTexture& CGameView::GetTerritoryTexture() return m->TerritoryTexture; } - -void CGameViewImpl::ScriptingInit() -{ - AddProperty(L"culling", &CGameViewImpl::Culling); - AddProperty(L"lockCullCamera", &CGameViewImpl::LockCullCamera); - AddProperty(L"constrainCamera", &CGameViewImpl::ConstrainCamera); - - CJSObject::ScriptingInit("GameView"); -} - int CGameView::Initialize() { CFG_GET_VAL("view.scroll.speed", Float, m->ViewScrollSpeed); diff --git a/source/graphics/GameView.h b/source/graphics/GameView.h index c64f799f84..64a0e38d87 100644 --- a/source/graphics/GameView.h +++ b/source/graphics/GameView.h @@ -96,6 +96,16 @@ public: float GetFar() const; float GetFOV() const; float GetCullFOV() const; + + #define DECLARE_BOOLEAN_SETTING(NAME) \ + bool Get##NAME##Enabled(); \ + void Set##NAME##Enabled(bool Enabled); + + DECLARE_BOOLEAN_SETTING(Culling); + DECLARE_BOOLEAN_SETTING(LockCullCamera); + DECLARE_BOOLEAN_SETTING(ConstrainCamera); + + #undef DECLARE_BOOLEAN_SETTING // Set projection of current camera using near, far, and FOV values void SetCameraProjection(); @@ -104,8 +114,6 @@ public: CCinemaManager* GetCinema(); JSObject* GetScript(); - static void ScriptingInit(); }; extern InReaction game_view_handler(const SDL_Event_* ev); - #endif diff --git a/source/graphics/scripting/JSInterface_GameView.cpp b/source/graphics/scripting/JSInterface_GameView.cpp new file mode 100644 index 0000000000..80dafff81f --- /dev/null +++ b/source/graphics/scripting/JSInterface_GameView.cpp @@ -0,0 +1,68 @@ +/* Copyright (C) 2013 Wildfire Games. + * This file is part of 0 A.D. + * + * 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 + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#include "precompiled.h" + +#include "JSInterface_GameView.h" +#include "graphics/GameView.h" +#include "ps/Game.h" +#include "ps/CLogger.h" +#include "scriptinterface/ScriptInterface.h" + +#define IMPLEMENT_BOOLEAN_SCRIPT_SETTING(NAME) \ +bool JSI_GameView::Get##NAME##Enabled(void* UNUSED(cbdata)) \ +{ \ + if (!g_Game || !g_Game->GetView()) \ + { \ + LOGERROR(L"Trying to get a setting from GameView when it's not initialized!"); \ + return false; \ + } \ + return g_Game->GetView()->Get##NAME##Enabled(); \ +} \ +\ +void JSI_GameView::Set##NAME##Enabled(void* UNUSED(cbdata), bool Enabled) \ +{ \ + if (!g_Game || !g_Game->GetView()) \ + { \ + LOGERROR(L"Trying to set a setting of GameView when it's not initialized!"); \ + return; \ + } \ + g_Game->GetView()->Set##NAME##Enabled(Enabled); \ +} + +IMPLEMENT_BOOLEAN_SCRIPT_SETTING(Culling); +IMPLEMENT_BOOLEAN_SCRIPT_SETTING(LockCullCamera); +IMPLEMENT_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); + +#undef IMPLEMENT_BOOLEAN_SCRIPT_SETTING + + +#define REGISTER_BOOLEAN_SCRIPT_SETTING(NAME) \ +scriptInterface.RegisterFunction("GameView_Get" #NAME "Enabled"); \ +scriptInterface.RegisterFunction("GameView_Set" #NAME "Enabled"); + +void JSI_GameView::RegisterScriptFunctions(ScriptInterface& scriptInterface) +{ + REGISTER_BOOLEAN_SCRIPT_SETTING(Culling); + REGISTER_BOOLEAN_SCRIPT_SETTING(LockCullCamera); + REGISTER_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); +} + +#undef REGISTER_BOOLEAN_SCRIPT_SETTING + + + diff --git a/source/graphics/scripting/JSInterface_GameView.h b/source/graphics/scripting/JSInterface_GameView.h new file mode 100644 index 0000000000..da610bd5a7 --- /dev/null +++ b/source/graphics/scripting/JSInterface_GameView.h @@ -0,0 +1,41 @@ +/* Copyright (C) 2013 Wildfire Games. + * This file is part of 0 A.D. + * + * 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 + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + + +#ifndef INCLUDED_JSINTERFACE_GAMEVIEW +#define INCLUDED_JSINTERFACE_GAMEVIEW + +#include "ps/CStr.h" +class ScriptInterface; + +#define DECLARE_BOOLEAN_SCRIPT_SETTING(NAME) \ + bool Get##NAME##Enabled(void* cbdata); \ + void Set##NAME##Enabled(void* cbdata, bool Enabled); + +namespace JSI_GameView +{ + void RegisterScriptFunctions(ScriptInterface& ScriptInterface); + + DECLARE_BOOLEAN_SCRIPT_SETTING(Culling); + DECLARE_BOOLEAN_SCRIPT_SETTING(LockCullCamera); + DECLARE_BOOLEAN_SCRIPT_SETTING(ConstrainCamera); +} + +#undef DECLARE_BOOLEAN_SCRIPT_SETTING + +#endif + diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index 3fce42a152..bb7fd377c0 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -23,6 +23,7 @@ #include "graphics/GameView.h" #include "graphics/MapReader.h" #include "gui/GUIManager.h" +#include "graphics/scripting/JSInterface_GameView.h" #include "lib/timer.h" #include "lib/utf8.h" #include "lib/sysdep/sysdep.h" @@ -650,6 +651,8 @@ void SetBoundingBoxDebugOverlay(void* UNUSED(cbdata), bool enabled) void GuiScriptingInit(ScriptInterface& scriptInterface) { + JSI_GameView::RegisterScriptFunctions(scriptInterface); + // GUI manager functions: scriptInterface.RegisterFunction("GetActiveGui"); scriptInterface.RegisterFunction("PushGuiPage"); diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index d427b310cc..f01f325730 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -321,9 +321,6 @@ static void RegisterJavascriptInterfaces() // maths JSI_Vector3D::init(); - // graphics - CGameView::ScriptingInit(); - // renderer CRenderer::ScriptingInit(); diff --git a/source/scripting/ScriptGlue.cpp b/source/scripting/ScriptGlue.cpp index 264be1814d..db2a56906b 100644 --- a/source/scripting/ScriptGlue.cpp +++ b/source/scripting/ScriptGlue.cpp @@ -408,15 +408,6 @@ JSFunctionSpec ScriptFunctionTable[] = // property accessors //----------------------------------------------------------------------------- -JSBool GetGameView(JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsid UNUSED(id), jsval* vp) -{ - if (g_Game) - *vp = OBJECT_TO_JSVAL(g_Game->GetView()->GetScript()); - else - *vp = JSVAL_NULL; - return JS_TRUE; -} - JSBool GetRenderer(JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsid UNUSED(id), jsval* vp) { if (CRenderer::IsInitialised()) @@ -439,7 +430,6 @@ enum ScriptGlobalTinyIDs JSPropertySpec ScriptGlobalTable[] = { { "console" , GLOBAL_CONSOLE, JSPROP_PERMANENT|JSPROP_READONLY, JSI_Console::getConsole, 0 }, - { "gameView" , 0, JSPROP_PERMANENT|JSPROP_READONLY, GetGameView, 0 }, { "renderer" , 0, JSPROP_PERMANENT|JSPROP_READONLY, GetRenderer, 0 }, // end of table marker