2014-09-20 14:12:35 +02:00
|
|
|
/* Copyright (C) 2014 Wildfire Games.
|
2010-01-09 20:20:14 +01:00
|
|
|
* 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 "scriptinterface/ScriptInterface.h"
|
|
|
|
|
2010-08-22 01:58:08 +02:00
|
|
|
#include "gui/IGUIObject.h"
|
2012-01-13 00:32:27 +01:00
|
|
|
#include "lib/external_libraries/libsdl.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "ps/Hotkey.h"
|
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
#define SET(obj, name, value) STMT(JS::RootedValue v_(cx); AssignOrToJSVal(cx, &v_, (value)); JS_SetProperty(cx, obj, (name), v_))
|
2010-01-09 20:20:14 +01:00
|
|
|
// ignore JS_SetProperty return value, because errors should be impossible
|
|
|
|
// and we can't do anything useful in the case of errors anyway
|
|
|
|
|
2014-07-14 21:52:35 +02:00
|
|
|
template<> void ScriptInterface::ToJSVal<SDL_Event_>(JSContext* cx, JS::MutableHandleValue ret, SDL_Event_ const& val)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2014-03-28 21:26:32 +01:00
|
|
|
JSAutoRequest rq(cx);
|
2010-01-09 20:20:14 +01:00
|
|
|
const char* typeName;
|
|
|
|
|
|
|
|
switch (val.ev.type)
|
|
|
|
{
|
2012-02-06 23:47:35 +01:00
|
|
|
#if SDL_VERSION_ATLEAST(2, 0, 0)
|
|
|
|
case SDL_WINDOWEVENT: typeName = "windowevent"; break;
|
|
|
|
#else
|
2010-01-09 20:20:14 +01:00
|
|
|
case SDL_ACTIVEEVENT: typeName = "activeevent"; break;
|
2012-02-06 23:47:35 +01:00
|
|
|
case SDL_VIDEOEXPOSE: typeName = "videoexpose"; break;
|
|
|
|
case SDL_VIDEORESIZE: typeName = "videoresize"; break;
|
|
|
|
#endif
|
2010-01-09 20:20:14 +01:00
|
|
|
case SDL_KEYDOWN: typeName = "keydown"; break;
|
|
|
|
case SDL_KEYUP: typeName = "keyup"; break;
|
|
|
|
case SDL_MOUSEMOTION: typeName = "mousemotion"; break;
|
|
|
|
case SDL_MOUSEBUTTONDOWN: typeName = "mousebuttondown"; break;
|
|
|
|
case SDL_MOUSEBUTTONUP: typeName = "mousebuttonup"; break;
|
2011-11-09 14:09:01 +01:00
|
|
|
case SDL_QUIT: typeName = "quit"; break;
|
2010-01-09 20:20:14 +01:00
|
|
|
case SDL_HOTKEYDOWN: typeName = "hotkeydown"; break;
|
|
|
|
case SDL_HOTKEYUP: typeName = "hotkeyup"; break;
|
|
|
|
default: typeName = "(unknown)"; break;
|
|
|
|
}
|
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
|
2014-07-14 21:52:35 +02:00
|
|
|
if (!obj)
|
2014-03-28 21:26:32 +01:00
|
|
|
{
|
2014-07-14 21:52:35 +02:00
|
|
|
ret.setUndefined();
|
2014-03-28 21:26:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
SET(obj, "type", typeName);
|
|
|
|
|
|
|
|
switch (val.ev.type)
|
|
|
|
{
|
2012-02-06 23:47:35 +01:00
|
|
|
#if !SDL_VERSION_ATLEAST(2, 0, 0)
|
2010-01-09 20:20:14 +01:00
|
|
|
case SDL_ACTIVEEVENT:
|
|
|
|
{
|
|
|
|
SET(obj, "gain", (int)val.ev.active.gain);
|
|
|
|
SET(obj, "state", (int)val.ev.active.state);
|
|
|
|
break;
|
|
|
|
}
|
2012-02-06 23:47:35 +01:00
|
|
|
#endif
|
2010-01-09 20:20:14 +01:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
|
|
|
{
|
|
|
|
// SET(obj, "which", (int)val.ev.key.which); // (not in wsdl.h)
|
|
|
|
// SET(obj, "state", (int)val.ev.key.state); // (not in wsdl.h)
|
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
JS::RootedObject keysym(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr()));
|
2010-01-09 20:20:14 +01:00
|
|
|
if (! keysym)
|
2014-03-28 21:26:32 +01:00
|
|
|
{
|
2014-07-14 21:52:35 +02:00
|
|
|
ret.setUndefined();
|
2014-03-28 21:26:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
JS::RootedValue keysymVal(cx, JS::ObjectValue(*keysym));
|
2015-01-24 15:46:52 +01:00
|
|
|
JS_SetProperty(cx, obj, "keysym", keysymVal);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
// SET(keysym, "scancode", (int)val.ev.key.keysym.scancode); // (not in wsdl.h)
|
|
|
|
SET(keysym, "sym", (int)val.ev.key.keysym.sym);
|
|
|
|
// SET(keysym, "mod", (int)val.ev.key.keysym.mod); // (not in wsdl.h)
|
2014-09-20 14:12:35 +02:00
|
|
|
#if !SDL_VERSION_ATLEAST(2, 0, 0)
|
2010-01-09 20:20:14 +01:00
|
|
|
if (val.ev.key.keysym.unicode)
|
|
|
|
{
|
|
|
|
std::wstring unicode(1, (wchar_t)val.ev.key.keysym.unicode);
|
|
|
|
SET(keysym, "unicode", unicode);
|
|
|
|
}
|
|
|
|
else
|
2014-09-20 14:12:35 +02:00
|
|
|
#endif
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2015-01-24 15:46:52 +01:00
|
|
|
SET(keysym, "unicode", JS::UndefinedHandleValue);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
// TODO: scripts have no idea what all the key/mod enum values are;
|
|
|
|
// we should probably expose them as constants if we expect scripts to use them
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
{
|
|
|
|
// SET(obj, "which", (int)val.ev.motion.which); // (not in wsdl.h)
|
|
|
|
// SET(obj, "state", (int)val.ev.motion.state); // (not in wsdl.h)
|
|
|
|
SET(obj, "x", (int)val.ev.motion.x);
|
|
|
|
SET(obj, "y", (int)val.ev.motion.y);
|
|
|
|
// SET(obj, "xrel", (int)val.ev.motion.xrel); // (not in wsdl.h)
|
|
|
|
// SET(obj, "yrel", (int)val.ev.motion.yrel); // (not in wsdl.h)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
{
|
|
|
|
// SET(obj, "which", (int)val.ev.button.which); // (not in wsdl.h)
|
|
|
|
SET(obj, "button", (int)val.ev.button.button);
|
|
|
|
SET(obj, "state", (int)val.ev.button.state);
|
|
|
|
SET(obj, "x", (int)val.ev.button.x);
|
|
|
|
SET(obj, "y", (int)val.ev.button.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_HOTKEYDOWN:
|
|
|
|
case SDL_HOTKEYUP:
|
|
|
|
{
|
2010-10-23 04:37:00 +02:00
|
|
|
SET(obj, "hotkey", static_cast<const char*>(val.ev.user.data1));
|
2010-01-09 20:20:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 21:52:35 +02:00
|
|
|
ret.setObject(*obj);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
2010-08-22 01:58:08 +02:00
|
|
|
|
2014-07-14 21:52:35 +02:00
|
|
|
template<> void ScriptInterface::ToJSVal<IGUIObject*>(JSContext* UNUSED(cx), JS::MutableHandleValue ret, IGUIObject* const& val)
|
2010-08-22 01:58:08 +02:00
|
|
|
{
|
|
|
|
if (val == NULL)
|
2014-07-14 21:52:35 +02:00
|
|
|
ret.setNull();
|
2014-03-28 21:26:32 +01:00
|
|
|
else
|
2014-07-14 21:52:35 +02:00
|
|
|
ret.setObject(*val->GetJSObject());
|
2010-08-22 01:58:08 +02:00
|
|
|
}
|