forked from mirrors/0ad
Fix #549 (Allow units to be moved by right-clicking on the mini-map), based on patch from Badmadblacksad
This was SVN commit r8107.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2009 Wildfire Games.
|
||||
/* Copyright (C) 2010 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@@ -26,6 +26,7 @@ IGUIObject
|
||||
|
||||
#include "gui/scripting/JSInterface_IGUIObject.h"
|
||||
#include "gui/scripting/JSInterface_GUITypes.h"
|
||||
#include "scriptinterface/ScriptVal.h"
|
||||
|
||||
#include "ps/CLogger.h"
|
||||
#define LOG_CATEGORY L"gui"
|
||||
@@ -509,6 +510,8 @@ void IGUIObject::ScriptEvent(const CStr& Action)
|
||||
JSObject* jsGuiObject = JS_ConstructObjectWithArguments(g_ScriptingHost.getContext(), &JSI_IGUIObject::JSI_class, m_pGUI->m_ScriptObject, NULL, 1, &guiObject);
|
||||
debug_assert(jsGuiObject); // TODO: Handle errors
|
||||
|
||||
// TODO: why don't we use GetJSObject here?
|
||||
|
||||
// Prevent it from being garbage-collected before it's passed into the function
|
||||
JS_AddRoot(g_ScriptingHost.getContext(), &jsGuiObject);
|
||||
|
||||
@@ -537,6 +540,24 @@ void IGUIObject::ScriptEvent(const CStr& Action)
|
||||
JS_RemoveRoot(g_ScriptingHost.getContext(), &jsGuiObject);
|
||||
}
|
||||
|
||||
void IGUIObject::ScriptEvent(const CStr& Action, const CScriptValRooted& Argument)
|
||||
{
|
||||
std::map<CStr, JSObject**>::iterator it = m_ScriptHandlers.find(Action);
|
||||
if (it == m_ScriptHandlers.end())
|
||||
return;
|
||||
|
||||
JSObject* object = GetJSObject();
|
||||
|
||||
jsval arg = Argument.get();
|
||||
|
||||
jsval result;
|
||||
JSBool ok = JS_CallFunctionValue(g_ScriptingHost.getContext(), object, OBJECT_TO_JSVAL(*it->second), 1, &arg, &result);
|
||||
if (!ok)
|
||||
{
|
||||
JS_ReportError(g_ScriptingHost.getContext(), "Errors executing script action \"%s\"", Action.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
JSObject* IGUIObject::GetJSObject()
|
||||
{
|
||||
// Cache the object when somebody first asks for it, because otherwise
|
||||
|
||||
+12
-1
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2009 Wildfire Games.
|
||||
/* Copyright (C) 2010 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@@ -59,6 +59,7 @@ The base class of an object
|
||||
struct SGUISetting;
|
||||
struct SGUIStyle;
|
||||
class CGUI;
|
||||
class CScriptValRooted;
|
||||
|
||||
//--------------------------------------------------------
|
||||
// Macros
|
||||
@@ -437,11 +438,21 @@ protected:
|
||||
/**
|
||||
* Execute the script for a particular action.
|
||||
* Does nothing if no script has been registered for that action.
|
||||
* The mouse coordinates will be passed as the first argument.
|
||||
*
|
||||
* @param Action Name of action
|
||||
*/
|
||||
void ScriptEvent(const CStr& Action);
|
||||
|
||||
/**
|
||||
* Execute the script for a particular action.
|
||||
* Does nothing if no script has been registered for that action.
|
||||
*
|
||||
* @param Action Name of action
|
||||
* @param Argument Argument to pass to action
|
||||
*/
|
||||
void ScriptEvent(const CStr& Action, const CScriptValRooted& Argument);
|
||||
|
||||
void SetScriptHandler(const CStr& Action, JSObject* Function);
|
||||
|
||||
//@}
|
||||
|
||||
+12
-12
@@ -36,6 +36,7 @@
|
||||
#include "ps/World.h"
|
||||
#include "renderer/Renderer.h"
|
||||
#include "renderer/WaterManager.h"
|
||||
#include "scriptinterface/ScriptInterface.h"
|
||||
#include "simulation2/Simulation2.h"
|
||||
#include "simulation2/components/ICmpMinimap.h"
|
||||
|
||||
@@ -135,19 +136,18 @@ void CMiniMap::SetCameraPos()
|
||||
|
||||
void CMiniMap::FireWorldClickEvent(int button, int clicks)
|
||||
{
|
||||
// TODO: we ought to pass this through to the GUI system
|
||||
|
||||
//debug_printf(L"FireWorldClickEvent: button %d, clicks %d\n", button, clicks);
|
||||
|
||||
/*
|
||||
// Determine X and Z according to proportion of mouse position and minimap
|
||||
CPos MousePos = GetMousePos();
|
||||
CVector2D Destination;
|
||||
//X and Z according to proportion of mouse position and minimap
|
||||
Destination.x = CELL_SIZE * m_MapSize *
|
||||
( (MousePos.x - m_CachedActualSize.left) / m_CachedActualSize.GetWidth() );
|
||||
Destination.y = CELL_SIZE * m_MapSize * ( (m_CachedActualSize.bottom - MousePos.y) /
|
||||
m_CachedActualSize.GetHeight() );
|
||||
*/
|
||||
float x = CELL_SIZE * m_MapSize *
|
||||
((MousePos.x - m_CachedActualSize.left) / m_CachedActualSize.GetWidth());
|
||||
float z = CELL_SIZE * m_MapSize *
|
||||
((m_CachedActualSize.bottom - MousePos.y) / m_CachedActualSize.GetHeight());
|
||||
|
||||
CScriptValRooted coords;
|
||||
g_ScriptingHost.GetScriptInterface().Eval("({})", coords);
|
||||
g_ScriptingHost.GetScriptInterface().SetProperty(coords.get(), "x", x, false);
|
||||
g_ScriptingHost.GetScriptInterface().SetProperty(coords.get(), "z", z, false);
|
||||
ScriptEvent("worldclick", coords);
|
||||
|
||||
UNUSED2(button);
|
||||
UNUSED2(clicks);
|
||||
|
||||
Reference in New Issue
Block a user