mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-29 01:54:09 +00:00
Simplify GUI/simulation interface
This was SVN commit r7286.
This commit is contained in:
@@ -108,44 +108,6 @@ static jsval CloneValueBetweenContexts(JSContext* cxFrom, JSContext* cxTo, jsval
|
||||
return JSVAL_VOID;
|
||||
}
|
||||
|
||||
CScriptVal GetSimulationState(void* cbdata)
|
||||
{
|
||||
CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata);
|
||||
|
||||
if (!g_UseSimulation2 || !g_Game)
|
||||
return JSVAL_VOID;
|
||||
CSimulation2* sim = g_Game->GetSimulation2();
|
||||
debug_assert(sim);
|
||||
CmpPtr<ICmpGuiInterface> gui(*sim, SYSTEM_ENTITY);
|
||||
if (gui.null())
|
||||
return JSVAL_VOID;
|
||||
|
||||
int player = -1;
|
||||
if (g_Game && g_Game->GetLocalPlayer())
|
||||
player = g_Game->GetLocalPlayer()->GetPlayerID();
|
||||
|
||||
return CloneValueBetweenContexts(sim->GetScriptInterface().GetContext(), guiManager->GetScriptInterface().GetContext(), gui->GetSimulationState(player).get());
|
||||
}
|
||||
|
||||
CScriptVal GetEntityState(void* cbdata, entity_id_t ent)
|
||||
{
|
||||
CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata);
|
||||
|
||||
if (!g_UseSimulation2 || !g_Game)
|
||||
return JSVAL_VOID;
|
||||
CSimulation2* sim = g_Game->GetSimulation2();
|
||||
debug_assert(sim);
|
||||
CmpPtr<ICmpGuiInterface> gui(*sim, SYSTEM_ENTITY);
|
||||
if (gui.null())
|
||||
return JSVAL_VOID;
|
||||
|
||||
int player = -1;
|
||||
if (g_Game && g_Game->GetLocalPlayer())
|
||||
player = g_Game->GetLocalPlayer()->GetPlayerID();
|
||||
|
||||
return CloneValueBetweenContexts(sim->GetScriptInterface().GetContext(), guiManager->GetScriptInterface().GetContext(), gui->GetEntityState(player, ent).get());
|
||||
}
|
||||
|
||||
CScriptVal GuiInterfaceCall(void* cbdata, std::string name, CScriptVal data)
|
||||
{
|
||||
CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata);
|
||||
@@ -158,27 +120,16 @@ CScriptVal GuiInterfaceCall(void* cbdata, std::string name, CScriptVal data)
|
||||
if (gui.null())
|
||||
return JSVAL_VOID;
|
||||
|
||||
int player = -1;
|
||||
if (g_Game && g_Game->GetLocalPlayer())
|
||||
player = g_Game->GetLocalPlayer()->GetPlayerID();
|
||||
|
||||
JSContext* cxGui = guiManager->GetScriptInterface().GetContext();
|
||||
JSContext* cxSim = sim->GetScriptInterface().GetContext();
|
||||
CScriptVal ret = gui->ScriptCall(name, CloneValueBetweenContexts(cxGui, cxSim, data.get()));
|
||||
CScriptVal ret = gui->ScriptCall(player, name, CloneValueBetweenContexts(cxGui, cxSim, data.get()));
|
||||
return CloneValueBetweenContexts(cxSim, cxGui, ret.get());
|
||||
}
|
||||
|
||||
void SetEntitySelectionHighlight(void* UNUSED(cbdata), entity_id_t ent, CColor color)
|
||||
{
|
||||
if (!g_UseSimulation2 || !g_Game)
|
||||
return;
|
||||
CSimulation2* sim = g_Game->GetSimulation2();
|
||||
debug_assert(sim);
|
||||
CmpPtr<ICmpGuiInterface> gui(*sim, SYSTEM_ENTITY);
|
||||
if (gui.null())
|
||||
return;
|
||||
|
||||
// TODO: stop duplicating all this prolog code
|
||||
|
||||
gui->SetSelectionHighlight(ent, color);
|
||||
}
|
||||
|
||||
void PostNetworkCommand(void* cbdata, CScriptVal cmd)
|
||||
{
|
||||
CGUIManager* guiManager = static_cast<CGUIManager*> (cbdata);
|
||||
@@ -226,9 +177,6 @@ void GuiScriptingInit(ScriptInterface& scriptInterface)
|
||||
|
||||
// Simulation<->GUI interface functions:
|
||||
scriptInterface.RegisterFunction<bool, &IsNewSimulation>("IsNewSimulation");
|
||||
scriptInterface.RegisterFunction<CScriptVal, &GetSimulationState>("GetSimulationState");
|
||||
scriptInterface.RegisterFunction<CScriptVal, entity_id_t, &GetEntityState>("GetEntityState");
|
||||
scriptInterface.RegisterFunction<void, entity_id_t, CColor, &SetEntitySelectionHighlight>("SetEntitySelectionHighlight");
|
||||
scriptInterface.RegisterFunction<CScriptVal, std::string, CScriptVal, &GuiInterfaceCall>("GuiInterfaceCall");
|
||||
|
||||
scriptInterface.RegisterFunction<void, CScriptVal, &PostNetworkCommand>("PostNetworkCommand");
|
||||
|
||||
@@ -104,6 +104,12 @@ public:
|
||||
template<typename T0, typename T1, typename R>
|
||||
bool CallFunction(jsval val, const char* name, const T0& a0, const T1& a1, R& ret);
|
||||
|
||||
/**
|
||||
* Call the named property on the given object, with return type R and 3 arguments
|
||||
*/
|
||||
template<typename T0, typename T1, typename T2, typename R>
|
||||
bool CallFunction(jsval val, const char* name, const T0& a0, const T1& a1, const T2& a2, R& ret);
|
||||
|
||||
jsval GetGlobalObject();
|
||||
|
||||
/**
|
||||
@@ -273,6 +279,21 @@ bool ScriptInterface::CallFunction(jsval val, const char* name, const T0& a0, co
|
||||
return FromJSVal(GetContext(), jsRet, ret);
|
||||
}
|
||||
|
||||
template<typename T0, typename T1, typename T2, typename R>
|
||||
bool ScriptInterface::CallFunction(jsval val, const char* name, const T0& a0, const T1& a1, const T2& a2, R& ret)
|
||||
{
|
||||
LOCAL_ROOT_SCOPE;
|
||||
jsval jsRet;
|
||||
std::vector<jsval> argv;
|
||||
argv.push_back(ToJSVal(GetContext(), a0));
|
||||
argv.push_back(ToJSVal(GetContext(), a1));
|
||||
argv.push_back(ToJSVal(GetContext(), a2));
|
||||
bool ok = CallFunction_(val, name, argv, jsRet);
|
||||
if (!ok)
|
||||
return false;
|
||||
return FromJSVal(GetContext(), jsRet, ret);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool ScriptInterface::SetGlobal(const char* name, const T& value, bool replace)
|
||||
{
|
||||
|
||||
@@ -30,25 +30,9 @@ class CCmpGuiInterfaceScripted : public ICmpGuiInterface
|
||||
public:
|
||||
DEFAULT_SCRIPT_WRAPPER(GuiInterfaceScripted)
|
||||
|
||||
virtual CScriptVal GetSimulationState(int player)
|
||||
virtual CScriptVal ScriptCall(int player, std::string cmd, CScriptVal data)
|
||||
{
|
||||
return m_Script.Call<CScriptVal> ("GetSimulationState", player);
|
||||
}
|
||||
|
||||
virtual CScriptVal GetEntityState(int player, entity_id_t ent)
|
||||
{
|
||||
return m_Script.Call<CScriptVal> ("GetEntityState", player, ent);
|
||||
}
|
||||
|
||||
virtual void SetSelectionHighlight(entity_id_t ent, const CColor& color)
|
||||
{
|
||||
m_Script.Call<CScriptVal> ("SetSelectionHighlight", ent, color);
|
||||
// ignore return value
|
||||
}
|
||||
|
||||
virtual CScriptVal ScriptCall(std::string name, CScriptVal data)
|
||||
{
|
||||
return m_Script.Call<CScriptVal> ("ScriptCall", name, data);
|
||||
return m_Script.Call<CScriptVal> ("ScriptCall", player, cmd, data);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -25,14 +25,10 @@ struct CColor;
|
||||
class ICmpGuiInterface : public IComponent
|
||||
{
|
||||
public:
|
||||
virtual CScriptVal GetSimulationState(int player) = 0;
|
||||
virtual CScriptVal GetEntityState(int player, entity_id_t ent) = 0;
|
||||
virtual void SetSelectionHighlight(entity_id_t ent, const CColor& color) = 0;
|
||||
|
||||
/**
|
||||
* Generic call function, for use by GUI scripts to talk to the GuiInterface script.
|
||||
*/
|
||||
virtual CScriptVal ScriptCall(std::string name, CScriptVal data) = 0;
|
||||
virtual CScriptVal ScriptCall(int player, std::string cmd, CScriptVal data) = 0;
|
||||
// TODO: some of the earlier functions should just use ScriptCall.
|
||||
|
||||
DECLARE_INTERFACE_TYPE(GuiInterface)
|
||||
|
||||
Reference in New Issue
Block a user