Maybe fixed GUI event-handler function things. Other minor changes and fixes.

This was SVN commit r2096.
This commit is contained in:
Ykkrosh 2005-03-30 22:33:10 +00:00
parent 1f902dde21
commit 0eb78450c0
8 changed files with 57 additions and 24 deletions

View File

@ -143,7 +143,8 @@
<object type="progressbar" name="SESSION_GROUP_PANE_PORTRAIT_52_BAR" style="group_portrait_bar" >
<action on="Load"><![CDATA[
// Seek through ALL group portraits and set their sizes at once. (So make sure this always goes in the last menu button.)
groupPaneCounter = 1;
var groupPaneCounter = 1;
for (rowLoop = 0; rowLoop < crd_grppane_prt_row.last; rowLoop++)
{
for (colLoop = 0; colLoop < crd_grppane_prt_col.last; colLoop++)
@ -151,7 +152,11 @@
// Set size, caption, and onPress function for each button.
AddSizeGroupPane("SESSION_GROUP_PANE_PORTRAIT_" + groupPaneCounter, rowLoop, colLoop);
getGUIObjectByName("SESSION_GROUP_PANE_PORTRAIT_" + groupPaneCounter).caption = groupPaneCounter;
getGUIObjectByName("SESSION_GROUP_PANE_PORTRAIT_" + groupPaneCounter).onPress = function() { SelectUnit(groupPaneCounter); }
// This is quite evil:
getGUIObjectByName("SESSION_GROUP_PANE_PORTRAIT_" + groupPaneCounter).onPress =
function (n) { return function() { SelectUnit(n); } } (groupPaneCounter);
AddSizeGroupPaneBar("SESSION_GROUP_PANE_PORTRAIT_" + groupPaneCounter + "_BAR");
groupPaneCounter++;
}

View File

@ -55,17 +55,30 @@ IGUIObject::IGUIObject() :
IGUIObject::~IGUIObject()
{
// delete() needs to know the type of the variable - never delete a void*
#define TYPE(t) case GUIST_##t: delete (t*)it->second.m_pSetting; break;
map<CStr, SGUISetting>::iterator it;
for (it = m_Settings.begin(); it != m_Settings.end(); ++it)
switch (it->second.m_Type)
{
map<CStr, SGUISetting>::iterator it;
for (it = m_Settings.begin(); it != m_Settings.end(); ++it)
{
#include "GUItypes.h"
switch (it->second.m_Type)
{
// delete() needs to know the type of the variable - never delete a void*
#define TYPE(t) case GUIST_##t: delete (t*)it->second.m_pSetting; break;
#include "GUItypes.h"
#undef TYPE
default:
debug_warn("Invalid setting type");
}
}
#undef TYPE
}
{
std::map<CStr, JSObject**>::iterator it;
for (it = m_ScriptHandlers.begin(); it != m_ScriptHandlers.end(); ++it)
{
JS_RemoveRoot(g_ScriptingHost.getContext(), it->second);
delete it->second;
}
}
}
//-------------------------------------------------------------------
@ -413,13 +426,27 @@ void IGUIObject::RegisterScriptHandler(const CStr& Action, const CStr& Code, CGU
JSFunction* func = JS_CompileFunction(g_ScriptingHost.getContext(), pGUI->m_ScriptObject, buf, paramCount, paramNames, (const char*)Code, Code.Length(), CodeName, 0);
assert(func); // TODO: Handle errors
if (func)
SetScriptHandler(Action, JS_GetFunctionObject(func));
}
m_ScriptHandlers[Action] = func;
void IGUIObject::SetScriptHandler(const CStr& Action, JSObject* Function)
{
JSObject** obj = new JSObject*;
*obj = Function;
JS_AddRoot(g_ScriptingHost.getContext(), obj);
if (m_ScriptHandlers[Action])
{
JS_RemoveRoot(g_ScriptingHost.getContext(), m_ScriptHandlers[Action]);
delete m_ScriptHandlers[Action];
}
m_ScriptHandlers[Action] = obj;
}
void IGUIObject::ScriptEvent(const CStr& Action)
{
map<CStr, JSFunction*>::iterator it = m_ScriptHandlers.find(Action);
map<CStr, JSObject**>::iterator it = m_ScriptHandlers.find(Action);
if (it == m_ScriptHandlers.end())
return;
@ -454,7 +481,7 @@ void IGUIObject::ScriptEvent(const CStr& Action)
jsval result;
JSBool ok = JS_CallFunction(g_ScriptingHost.getContext(), jsGuiObject, it->second, 1, paramData, &result);
JSBool ok = JS_CallFunctionValue(g_ScriptingHost.getContext(), jsGuiObject, OBJECT_TO_JSVAL(*it->second), 1, paramData, &result);
if (!ok)
{
JS_ReportError(g_ScriptingHost.getContext(), "Errors executing script action \"%s\"", Action.c_str());

View File

@ -431,10 +431,7 @@ protected:
*/
void ScriptEvent(const CStr& Action);
/**
* Internal storage for registered script handlers.
*/
std::map<CStr, JSFunction*> m_ScriptHandlers;
void SetScriptHandler(const CStr& Action, JSObject* Function);
//@}
private:
@ -511,6 +508,9 @@ protected:
private:
// An object can't function stand alone
CGUI *m_pGUI;
// Internal storage for registered script handlers.
std::map<CStr, JSObject**> m_ScriptHandlers;
};

View File

@ -53,11 +53,11 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval
if (propName.Left(2) == "on")
{
CStr eventName (CStr(propName.substr(2)).LowerCase());
std::map<CStr, JSFunction*>::iterator it = e->m_ScriptHandlers.find(eventName);
std::map<CStr, JSObject**>::iterator it = e->m_ScriptHandlers.find(eventName);
if (it == e->m_ScriptHandlers.end())
*vp = JSVAL_NULL;
else
*vp = OBJECT_TO_JSVAL(JS_GetFunctionObject(it->second));
*vp = OBJECT_TO_JSVAL(*(it->second));
return JS_TRUE;
}
@ -264,15 +264,14 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval
// Use onWhatever to set event handlers
if (propName.Left(2) == "on")
{
JSFunction* func = JS_ValueToFunction(cx, *vp);
if (! func)
if (!JSVAL_IS_OBJECT(*vp) || !JS_ValueToFunction(cx, *vp))
{
JS_ReportError(cx, "on- event-handlers must be functions");
return JS_FALSE;
}
CStr eventName (CStr(propName.substr(2)).LowerCase());
e->m_ScriptHandlers[eventName] = func;
e->SetScriptHandler(eventName, JSVAL_TO_OBJECT(*vp));
return JS_TRUE;
}

View File

@ -1408,7 +1408,9 @@ static void Frame()
MICROLOG(L"render");
Render();
MICROLOG(L"finished render");
PROFILE_START( "swap buffers" );
SDL_GL_SwapBuffers();
PROFILE_END( "swap buffers" );
}
// inactive; relinquish CPU for a little while
// don't use SDL_WaitEvent: don't want the main loop to freeze until app focus is restored

View File

@ -24,7 +24,7 @@
#ifndef CCONSOLE_H
#define CCONSOLE_H
#define CONSOLE_BUFFER_SIZE 256 // for text being typed into the console
#define CONSOLE_BUFFER_SIZE 1024 // for text being typed into the console
#define CONSOLE_MESSAGE_SIZE 1024 // for messages being printed into the console
typedef void(*fptr)(void);

View File

@ -258,7 +258,7 @@ public:
}
void ImmediateCopy( IJSObject* CopyTo, IJSObject* CopyFrom, IJSProperty* CopyProperty )
{
assert( "Inheritance not supported for CJSSharedProperties" );
debug_warn( "Inheritance not supported for CJSSharedProperties" );
}
void Set( JSContext* cx, IJSObject* owner, jsval Value )
{

View File

@ -333,7 +333,7 @@ bool CEntity::DispatchEvent( CScriptEvent* evt )
else
{
CStr8 short_string( evt->m_Type );
int length = short_string.length();
size_t length = short_string.length();
data = new char[length + 9];
strcpy( data, "script: " );
strcpy( data + 8, short_string.c_str() );