1
0
forked from mirrors/0ad

Changes GameView to expose global functions to scripts instead of using CJSObject.

Fixes #2126
Refs #1886

This was SVN commit r13826.
This commit is contained in:
Yves
2013-09-12 12:40:05 +00:00
parent 05422ad545
commit 5304bc9d6a
8 changed files with 142 additions and 40 deletions
@@ -290,7 +290,7 @@
<object size="0 128 100%-18 144" type="text" style="devCommandsText">Restrict camera</object>
<object size="100%-16 128 100% 144" type="checkbox" style="StoneCrossBox" checked="true">
<action on="Press">gameView.constrainCamera = this.checked;</action>
<action on="Press">Engine.GameView_SetConstrainCameraEnabled(this.checked);</action>
</object>
<object size="0 144 100%-18 160" type="text" style="devCommandsText">Reveal map</object>
+19 -24
View File
@@ -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<CGameViewImpl>
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<CGameViewImpl>::ScriptingInit("GameView");
}
int CGameView::Initialize()
{
CFG_GET_VAL("view.scroll.speed", Float, m->ViewScrollSpeed);
+10 -2
View File
@@ -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
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<bool, &JSI_GameView::Get##NAME##Enabled>("GameView_Get" #NAME "Enabled"); \
scriptInterface.RegisterFunction<void, bool, &JSI_GameView::Set##NAME##Enabled>("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
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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
+3
View File
@@ -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<CScriptVal, &GetActiveGui>("GetActiveGui");
scriptInterface.RegisterFunction<void, std::wstring, CScriptVal, &PushGuiPage>("PushGuiPage");
-3
View File
@@ -321,9 +321,6 @@ static void RegisterJavascriptInterfaces()
// maths
JSI_Vector3D::init();
// graphics
CGameView::ScriptingInit();
// renderer
CRenderer::ScriptingInit();
-10
View File
@@ -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