GUI/scripting updates

This was SVN commit r706.
This commit is contained in:
Ykkrosh 2004-07-11 16:22:35 +00:00
parent c62940201f
commit cf9d8b9797
12 changed files with 150 additions and 107 deletions

View file

@ -64,55 +64,8 @@ void CButton::HandleMessage(const SGUIMessage &Message)
switch (Message.type)
{
case GUIM_PREPROCESS:
break;
case GUIM_POSTPROCESS:
break;
case GUIM_MOUSE_OVER:
ScriptEvent("MouseOver");
break;
case GUIM_MOUSE_ENTER:
ScriptEvent("MouseEnter");
break;
case GUIM_MOUSE_LEAVE:
ScriptEvent("MouseLeave");
break;
case GUIM_MOUSE_PRESS_LEFT:
ScriptEvent("MouseLeftPress");
break;
case GUIM_MOUSE_RELEASE_LEFT:
ScriptEvent("MouseLeftRelease");
break;
case GUIM_MOUSE_DOWN_LEFT:
ScriptEvent("MouseLeftDown");
break;
case GUIM_MOUSE_PRESS_RIGHT:
ScriptEvent("MouseRightPress");
break;
case GUIM_MOUSE_RELEASE_RIGHT:
ScriptEvent("MouseRightRelease");
break;
case GUIM_MOUSE_DOWN_RIGHT:
ScriptEvent("MouseRightDown");
break;
case GUIM_PRESSED:
GetGUI()->TEMPmessage = "Button " + string((const TCHAR*)m_Name) + " was pressed!";
ScriptEvent("Press");
break;
case GUIM_LOAD:
ScriptEvent("Load");
// GetGUI()->TEMPmessage = "Button " + string((const TCHAR*)m_Name) + " was pressed!";
break;
default:

View file

@ -92,9 +92,6 @@ void CCheckBox::HandleMessage(const SGUIMessage &Message)
checked = !checked;
GUI<bool>::SetSetting(this, "checked", checked);
//GetGUI()->TEMPmessage = "Check box " + string((const TCHAR*)m_Name) + " was " + (m_Settings.m_Checked?"checked":"unchecked");
ScriptEvent("Press");
} break;
default:

View file

@ -67,7 +67,6 @@ int CGUI::HandleEvent(const SDL_Event* ev)
{
m_MousePos = CPos(ev->motion.x, ev->motion.y);
// pNearest will after this point at the hovered object, possibly NULL
GUI<SGUIMessage>::RecurseObject(GUIRR_HIDDEN | GUIRR_GHOST, m_BaseObject,
&IGUIObject::HandleMessage,
SGUIMessage(GUIM_MOUSE_MOTION));
@ -100,7 +99,10 @@ int CGUI::HandleEvent(const SDL_Event* ev)
GUI<IGUIObject*>::RecurseObject(GUIRR_HIDDEN | GUIRR_GHOST, m_BaseObject,
&IGUIObject::ChooseMouseOverAndClosest,
pNearest);
if (ev->type == SDL_MOUSEMOTION && pNearest)
pNearest->ScriptEvent("mousemove");
// Now we'll call UpdateMouseOver on *all* objects,
// we'll input the one hovered, and they will each
// update their own data and send messages accordingly
@ -116,26 +118,15 @@ int CGUI::HandleEvent(const SDL_Event* ev)
if (pNearest)
{
pNearest->HandleMessage(SGUIMessage(GUIM_MOUSE_PRESS_LEFT));
pNearest->ScriptEvent("mouseleftpress");
}
{
// some temp
/* CClientArea ca;
bool hidden;
GUI<CClientArea>::GetSetting(*this, CStr("backdrop43"), CStr("size"), ca);
//hidden = !hidden;
ca.pixel.right -= 3;
GUI<CClientArea>::SetSetting(*this, CStr("backdrop43"), CStr("size"), ca);
*/ }
break;
break;
case SDL_BUTTON_WHEELDOWN: // wheel down
if (pNearest)
{
pNearest->HandleMessage(SGUIMessage(GUIM_MOUSE_WHEEL_DOWN));
pNearest->ScriptEvent("mousewheeldown");
}
break;
@ -143,22 +134,10 @@ int CGUI::HandleEvent(const SDL_Event* ev)
if (pNearest)
{
pNearest->HandleMessage(SGUIMessage(GUIM_MOUSE_WHEEL_UP));
pNearest->ScriptEvent("mousewheelup");
}
break;
// TODO Gee: Just temp
case SDL_BUTTON_RIGHT:
{
CClientArea ca;
GUI<CClientArea>::GetSetting(*this, CStr("backdrop43"), CStr("size"), ca);
//hidden = !hidden;
ca.pixel.right -= 3;
GUI<CClientArea>::SetSetting(*this, CStr("backdrop43"), CStr("size"), ca);
}
break;
default:
break;
}
@ -170,7 +149,10 @@ int CGUI::HandleEvent(const SDL_Event* ev)
if (ev->button.button == SDL_BUTTON_LEFT)
{
if (pNearest)
{
pNearest->HandleMessage(SGUIMessage(GUIM_MOUSE_RELEASE_LEFT));
pNearest->ScriptEvent("mouseleftrelease");
}
}
// Reset all states on all visible objects
@ -485,6 +467,7 @@ void CGUI::AddObject(IGUIObject* pObject)
// Loaded
GUI<SGUIMessage>::RecurseObject(0, pObject, &IGUIObject::HandleMessage, SGUIMessage(GUIM_LOAD));
GUI<CStr>::RecurseObject(0, pObject, &IGUIObject::ScriptEvent, "load");
}
catch (PS_RESULT e)
{
@ -871,9 +854,14 @@ void CGUI::LoadXMLFile(const string &Filename)
m_Errors = 0;
CXeromyces XeroFile;
XeroFile.Load(Filename.c_str());
bool ParseFailed = false;
try
{
XeroFile.Load(Filename.c_str());
}
catch (...) {
// Failed
return;
}
XMBElement node = XeroFile.getRoot();
@ -922,16 +910,22 @@ void CGUI::LoadXMLFile(const string &Filename)
void CGUI::Xeromyces_ReadRootObjects(XMBElement Element, CXeromyces* pFile)
{
int el_script = pFile->getElementID("script");
// Iterate main children
// they should all be <object> elements
// they should all be <object> or <script> elements
XMBElementList children = Element.getChildNodes();
for (int i=0; i<children.Count; ++i)
{
//debug_out("Object %d\n", i);
XMBElement child = children.item(i);
// Read in this whole object into the GUI
Xeromyces_ReadObject(child, pFile, m_BaseObject);
if (child.getNodeName() == el_script)
// Execute the inline script
Xeromyces_ReadScript(child, pFile);
else
// Read in this whole object into the GUI
Xeromyces_ReadObject(child, pFile, m_BaseObject);
}
}
@ -1021,6 +1015,7 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
ATTR(name);
ATTR(z);
ATTR(on);
ATTR(file);
//
// Read Style and set defaults
@ -1138,9 +1133,39 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
}
else if (element_name == elmt_action)
{
// Scripted <action> element
// Check for a 'file' parameter
CStr file (Element.getAttributes().getNamedItem(attr_file));
CStr code;
// If there is a file, open it and use it as the code
if (file.Length())
{
Handle h = vfs_open(file);
if (h <= 0)
// TODO: Error handling
assert(! "File open failed");
else
{
void* data;
size_t len;
int err = vfs_map(h, 0, data, len);
assert(err == 0);
code = (char*)data;
vfs_unmap(h);
vfs_close(h);
}
}
// Read the inline code (concatenating to the file code, if both are specified)
code += (CStr)Element.getText();
CStr action = (CStr)child.getAttributes().getNamedItem(attr_on);
CStr code = (CStr)child.getText();
object->RegisterScriptHandler(action, code, this);
object->RegisterScriptHandler(action.LowerCase(), code, this);
}
}
@ -1191,6 +1216,41 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
}
}
void CGUI::Xeromyces_ReadScript(XMBElement Element, CXeromyces* pFile)
{
// Check for a 'file' parameter
CStr file (Element.getAttributes().getNamedItem( pFile->getAttributeID("file") ));
// If there is a file, open and execute it
if (file.Length())
{
Handle h = vfs_open(file);
if (h <= 0)
// TODO: Error handling
assert(! "File open failed");
else
{
void* data;
size_t len;
int err = vfs_map(h, 0, data, len);
assert(err == 0);
jsval result;
JS_EvaluateScript(g_ScriptingHost.getContext(), (JSObject*)m_ScriptObject, (const char*)data, (int)len, file, 0, &result);
vfs_unmap(h);
vfs_close(h);
}
}
// Execute inline scripts
CStr code (Element.getText());
jsval result;
JS_EvaluateScript(g_ScriptingHost.getContext(), (JSObject*)m_ScriptObject, code.c_str(), (int)code.Length(), "", 0, &result);
}
void CGUI::Xeromyces_ReadSprite(XMBElement Element, CXeromyces* pFile)
{
// Sprite object we're adding

View file

@ -205,6 +205,13 @@ public:
SGUIText GenerateText(const CGUIString &Text, /*const CColor &Color, */
const CStr& Font, const int &Width, const int &BufferZone);
/**
* Returns the JSObject* associated with the GUI
*
* @return A JSobject* (as a void* to avoid #including all of JS)
*/
void* GetScriptObject() { return m_ScriptObject; }
private:
/**
* Updates the object pointers, needs to be called each
@ -261,6 +268,8 @@ private:
Xeromyces_* functions tree
<code>
\<objects\> (ReadRootObjects)
|
+-\<script\> (ReadScript)
|
+-\<object\> (ReadObject)
|
@ -365,6 +374,18 @@ private:
*/
void Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent);
/**
* Reads in the element \<script\> (the DOMElement) and executes
* the script's code.
*
* @param Element The Xeromyces object that represents
* the sprite-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXMLFile()
*/
void Xeromyces_ReadScript(XMBElement Element, CXeromyces* pFile);
/**
* Reads in the element \<sprite\> (the DOMElement) and stores the
* result in a new CGUISprite.
@ -416,7 +437,6 @@ private:
//@}
private:
// Variables

View file

@ -30,8 +30,6 @@ void CRadioButton::HandleMessage(const SGUIMessage &Message)
GUI<bool>::SetSetting(this, "checked", true);
//GetGUI()->TEMPmessage = "Check box " + string((const TCHAR*)m_Name) + " was " + (m_Settings.m_Checked?"checked":"unchecked");
ScriptEvent("Press");
break;
}

View file

@ -49,6 +49,7 @@ void IGUIButtonBehavior::HandleMessage(const SGUIMessage &Message)
m_Pressed = false;
// BUTTON WAS CLICKED
HandleMessage(GUIM_PRESSED);
ScriptEvent("press");
}
} break;

View file

@ -197,6 +197,7 @@ void IGUIObject::UpdateMouseOver(IGUIObject * const &pMouseOver)
{
// It wasn't hovering, so that must mean it just entered
HandleMessage(GUIM_MOUSE_ENTER);
ScriptEvent("mouseenter");
}
// Either way, set to true
@ -204,6 +205,7 @@ void IGUIObject::UpdateMouseOver(IGUIObject * const &pMouseOver)
// call mouse over
HandleMessage(GUIM_MOUSE_OVER);
ScriptEvent("mouseover");
}
else // Some other object (or none) is hovered
{
@ -211,6 +213,7 @@ void IGUIObject::UpdateMouseOver(IGUIObject * const &pMouseOver)
{
m_MouseHovering = false;
HandleMessage(GUIM_MOUSE_LEAVE);
ScriptEvent("mouseleave");
}
}
}
@ -401,6 +404,7 @@ void IGUIObject::CheckSettingsValidity()
{
// Send message to itself
HandleMessage(GUIM_SETTINGS_UPDATED);
ScriptEvent("update");
}
catch (...)
{

View file

@ -1,4 +1,4 @@
// $Id: JSInterface_GUITypes.cpp,v 1.1 2004/07/08 15:19:45 philip Exp $
// $Id: JSInterface_GUITypes.cpp,v 1.2 2004/07/11 16:21:52 philip Exp $
#include "precompiled.h"
@ -65,11 +65,11 @@ JSBool JSI_GUISize::toString(JSContext* cx, JSObject* obj, uintN argc, jsval* ar
}
/**** GUIColour ****/
/**** GUIColor ****/
JSClass JSI_GUIColour::JSI_class = {
"GUIColour", 0,
JSClass JSI_GUIColor::JSI_class = {
"GUIColor", 0,
JS_PropertyStub, JS_PropertyStub,
JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub,
@ -77,7 +77,7 @@ JSClass JSI_GUIColour::JSI_class = {
NULL, NULL, NULL, NULL
};
JSPropertySpec JSI_GUIColour::JSI_props[] =
JSPropertySpec JSI_GUIColor::JSI_props[] =
{
{ "r", 0, JSPROP_ENUMERATE},
{ "g", 1, JSPROP_ENUMERATE},
@ -86,13 +86,13 @@ JSPropertySpec JSI_GUIColour::JSI_props[] =
{ 0 }
};
JSFunctionSpec JSI_GUIColour::JSI_methods[] =
JSFunctionSpec JSI_GUIColor::JSI_methods[] =
{
{ "toString", JSI_GUIColour::toString, 0, 0, 0 },
{ "toString", JSI_GUIColor::toString, 0, 0, 0 },
{ 0 }
};
JSBool JSI_GUIColour::construct(JSContext* cx, JSObject* obj, unsigned int argc, jsval* argv, jsval* rval)
JSBool JSI_GUIColor::construct(JSContext* cx, JSObject* obj, unsigned int argc, jsval* argv, jsval* rval)
{
if (argc == 4)
{
@ -116,7 +116,7 @@ JSBool JSI_GUIColour::construct(JSContext* cx, JSObject* obj, unsigned int argc,
return JS_TRUE;
}
JSBool JSI_GUIColour::toString(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval)
JSBool JSI_GUIColor::toString(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval)
{
char buffer[256];
// Convert to integers, to be compatible with the GUI's string SetSetting
@ -189,6 +189,6 @@ JSBool JSI_GUIMouse::toString(JSContext* cx, JSObject* obj, uintN argc, jsval* a
void JSI_GUITypes::init()
{
g_ScriptingHost.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 1, JSI_GUISize::JSI_props, JSI_GUISize::JSI_methods, NULL, NULL);
g_ScriptingHost.DefineCustomObjectType(&JSI_GUIColour::JSI_class, JSI_GUIColour::construct, 1, JSI_GUIColour::JSI_props, JSI_GUIColour::JSI_methods, NULL, NULL);
g_ScriptingHost.DefineCustomObjectType(&JSI_GUIColor::JSI_class, JSI_GUIColor::construct, 1, JSI_GUIColor::JSI_props, JSI_GUIColor::JSI_methods, NULL, NULL);
g_ScriptingHost.DefineCustomObjectType(&JSI_GUIMouse::JSI_class, JSI_GUIMouse::construct, 1, JSI_GUIMouse::JSI_props, JSI_GUIMouse::JSI_methods, NULL, NULL);
}

View file

@ -1,4 +1,4 @@
// $Id: JSInterface_GUITypes.h,v 1.1 2004/07/08 15:19:45 philip Exp $
// $Id: JSInterface_GUITypes.h,v 1.2 2004/07/11 16:21:52 philip Exp $
#include "scripting/ScriptingHost.h"
@ -17,7 +17,7 @@
}
GUISTDTYPE(Size)
GUISTDTYPE(Colour)
GUISTDTYPE(Color)
GUISTDTYPE(Mouse)
#undef GUISTDTYPE // avoid unnecessary pollution

View file

@ -1,4 +1,4 @@
// $Id: JSInterface_IGUIObject.cpp,v 1.2 2004/07/10 21:23:06 philip Exp $
// $Id: JSInterface_IGUIObject.cpp,v 1.3 2004/07/11 16:21:52 philip Exp $
#include "precompiled.h"
@ -109,7 +109,7 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval
{
CColor colour;
GUI<CColor>::GetSetting(e, propName, colour);
JSObject* obj = JS_NewObject(cx, &JSI_GUIColour::JSI_class, NULL, NULL);
JSObject* obj = JS_NewObject(cx, &JSI_GUIColor::JSI_class, NULL, NULL);
jsval r = DOUBLE_TO_JSVAL(JS_NewDouble(cx, colour.r));
jsval g = DOUBLE_TO_JSVAL(JS_NewDouble(cx, colour.g));
jsval b = DOUBLE_TO_JSVAL(JS_NewDouble(cx, colour.b));
@ -263,7 +263,7 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval
{
e->SetSetting(propName, JS_GetStringBytes(JS_ValueToString(cx, *vp)) );
}
else if (JSVAL_IS_OBJECT(*vp) && JS_GetClass(JSVAL_TO_OBJECT(*vp)) == &JSI_GUIColour::JSI_class)
else if (JSVAL_IS_OBJECT(*vp) && JS_GetClass(JSVAL_TO_OBJECT(*vp)) == &JSI_GUIColor::JSI_class)
{
CColor colour;
JSObject* obj = JSVAL_TO_OBJECT(*vp);
@ -278,7 +278,7 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval
}
else
{
JS_ReportError(cx, "Color only accepts strings or GUIColour objects");
JS_ReportError(cx, "Color only accepts strings or GUIColor objects");
}
break;
}

View file

@ -30,6 +30,7 @@ JSFunctionSpec ScriptFunctionTable[] =
{"getEntityTemplate", getEntityTemplate, 1, 0, 0 },
{"getGUIObjectByName", JSI_IGUIObject::getByName, 1, 0, 0 },
{"getGlobal", getGlobal, 0, 0, 0 },
{"getGUIGlobal", getGUIGlobal, 0, 0, 0 },
{"exit", exitProgram, 0, 0, 0 },
{0, 0, 0, 0, 0},
};
@ -124,6 +125,12 @@ JSBool getEntityTemplate( JSContext* context, JSObject* globalObject, unsigned i
return( JS_TRUE );
}
JSBool getGUIGlobal( JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval )
{
*rval = OBJECT_TO_JSVAL( g_GUI.GetScriptObject() );
return( JS_TRUE );
}
JSBool getGlobal( JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval )
{
*rval = OBJECT_TO_JSVAL( globalObject );

View file

@ -12,6 +12,9 @@ JSBool writeConsole( JSContext* context, JSObject* globalObject, unsigned int ar
JSBool getEntityByHandle( JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval );
JSBool getEntityTemplate( JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval );
// Returns the sort-of-global object associated with the current GUI
JSBool getGUIGlobal(JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval);
// Returns the global object, e.g. for setting global variables.
JSBool getGlobal(JSContext* context, JSObject* globalObject, unsigned int argc, jsval* argv, jsval* rval);