diff --git a/source/gui/CTooltip.cpp b/source/gui/CTooltip.cpp index 54ba8530e1..1c673f40f6 100644 --- a/source/gui/CTooltip.cpp +++ b/source/gui/CTooltip.cpp @@ -9,7 +9,7 @@ CTooltip::CTooltip() AddSetting(GUIST_CGUIString, "caption"); AddSetting(GUIST_CStr, "font"); AddSetting(GUIST_CGUISpriteInstance, "sprite"); - AddSetting(GUIST_float, "time"); + AddSetting(GUIST_int, "time"); AddSetting(GUIST_CColor, "textcolor"); AddSetting(GUIST_int, "maxwidth"); AddSetting(GUIST_CPos, "pos"); @@ -17,7 +17,7 @@ CTooltip::CTooltip() AddSetting(GUIST_CPos, "_mousepos"); - GUI::SetSetting(this, "time", 0.5f); + GUI::SetSetting(this, "time", 500); GUI::SetSetting(this, "anchor", EVAlign_Bottom); // Set up a blank piece of text, to be replaced with a more diff --git a/source/gui/GUITooltip.cpp b/source/gui/GUITooltip.cpp index 6995448246..fe24f37199 100644 --- a/source/gui/GUITooltip.cpp +++ b/source/gui/GUITooltip.cpp @@ -37,7 +37,7 @@ SHOWING < Start displaying the tooltip - * If the mouse leaves the object, check whether it has a tooltip + * If the mouse leaves the object, check whether the new object has a tooltip and switch to 'SHOWING' or 'COOLING' COOLING (since I can't think of a better name) @@ -88,22 +88,27 @@ void GUITooltip::ShowTooltip(IGUIObject* obj, CPos pos, CStr& style, CGUI* gui) LOG_ONCE(ERROR, "gui", "Cannot find tooltip object named '%s'", (const char*)style); return; } + // Unhide the object GUI::SetSetting(tooltipobj, "hidden", false); assert(obj); // These shouldn't fail: + // Retrieve object's 'tooltip' setting CStr text; if (GUI::GetSetting(obj, "tooltip", text) != PS_OK) debug_warn("Failed to retrieve tooltip text"); + // Set tooltip's caption if (tooltipobj->SetSetting("caption", text) != PS_OK) debug_warn("Failed to set tooltip caption"); + // Store mouse position inside the tooltip if (GUI::SetSetting(tooltipobj, "_mousepos", pos) != PS_OK) debug_warn("Failed to set tooltip mouse position"); + // Make the tooltip object regenerate its text tooltipobj->HandleMessage(SGUIMessage(GUIM_SETTINGS_UPDATED, "caption")); } @@ -118,6 +123,20 @@ static void HideTooltip(CStr& style, CGUI* gui) GUI::SetSetting(tooltipobj, "hidden", true); } +static int GetTooltipTime(CStr& style, CGUI* gui) +{ + int time = 500; // default value (in msec) + + IGUIObject* tooltipobj = gui->FindObjectByName(style); + if (! tooltipobj) + { + LOG_ONCE(ERROR, "gui", "Cannot find tooltip object named '%s'", (const char*)style); + return time; + } + GUI::GetSetting(tooltipobj, "time", time); + return time; +} + void GUITooltip::Update(IGUIObject* Nearest, CPos MousePos, CGUI* GUI) { double now = get_time(); @@ -178,12 +197,14 @@ void GUITooltip::Update(IGUIObject* Nearest, CPos MousePos, CGUI* GUI) break; } + // Handle state-entry code + if (nextstate != -1) { switch (nextstate) { case ST_STATIONARY_TOOLTIP: - m_Time = now + 0.5/* TODO: tooltip time */; + m_Time = now + (double)GetTooltipTime(*style, GUI) / 1000.; break; case ST_SHOWING: diff --git a/source/gui/GUIutil.cpp b/source/gui/GUIutil.cpp index efd29f4d01..9be79335d1 100755 --- a/source/gui/GUIutil.cpp +++ b/source/gui/GUIutil.cpp @@ -415,8 +415,10 @@ void CInternalCGUIAccessorBase::HandleMessage(IGUIObject *pObject, const SGUIMes template<> void CheckType(const IGUIObject* obj, const CStr& setting) { \ if (((IGUIObject*)obj)->m_Settings[setting].m_Type != GUIST_##T) \ { \ - debug_warn("EXCESSIVELY FATAL ERROR: Inconsistent types in GUI"); \ - throw "EXCESSIVELY FATAL ERROR: Inconsistent types in GUI"; /* TODO: better reporting */ \ + /* Abort now, to avoid corrupting everything by invalidly \ + casting pointers */ \ + debug_warn("FATAL ERROR: Inconsistent types in GUI"); \ + throw "FATAL ERROR: Inconsistent types in GUI"; /* TODO: better reporting */ \ } \ } #include "GUItypes.h" diff --git a/source/gui/IGUIObject.cpp b/source/gui/IGUIObject.cpp index 005e90baad..fd6274a3e8 100755 --- a/source/gui/IGUIObject.cpp +++ b/source/gui/IGUIObject.cpp @@ -406,7 +406,14 @@ void IGUIObject::RegisterScriptHandler(const CStr& Action, const CStr& Code, CGU // Location to report errors from CStr CodeName = GetName()+" "+Action; - JSFunction* func = JS_CompileFunction(g_ScriptingHost.getContext(), pGUI->m_ScriptObject, NULL, paramCount, paramNames, (const char*)Code, Code.Length(), CodeName, 0); + // Generate a unique name + static int x=0; + char buf[64]; + sprintf(buf, "__eventhandler%d", x++); + + 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 + m_ScriptHandlers[Action] = func; } @@ -425,20 +432,18 @@ void IGUIObject::ScriptEvent(const CStr& Action) // Make a 'this', allowing access to the IGUIObject JSObject* jsGuiObject = JS_ConstructObjectWithArguments(g_ScriptingHost.getContext(), &JSI_IGUIObject::JSI_class, NULL, m_pGUI->m_ScriptObject, 1, &guiObject); -// assert(jsGuiObject); // TODO (URGENT): Work out why this fails - - // Prevent it from being garbage-collected before - // it's passed into the function + assert(jsGuiObject); // TODO: Handle errors + + // Prevent it from being garbage-collected before it's passed into the function JS_AddRoot(g_ScriptingHost.getContext(), &jsGuiObject); - // Set up the 'mouse' parameter jsval mouseParams[3]; mouseParams[0] = INT_TO_JSVAL(m_pGUI->m_MousePos.x); mouseParams[1] = INT_TO_JSVAL(m_pGUI->m_MousePos.y); mouseParams[2] = INT_TO_JSVAL(m_pGUI->m_MouseButtons); JSObject* mouseObj = JS_ConstructObjectWithArguments(g_ScriptingHost.getContext(), &JSI_GUIMouse::JSI_class, NULL, m_pGUI->m_ScriptObject, 3, mouseParams); - assert(mouseObj); // TODO need better error handling + assert(mouseObj); // TODO: Handle errors // Don't garbage collect the mouse JS_AddRoot(g_ScriptingHost.getContext(), &mouseObj); diff --git a/source/gui/scripting/JSInterface_GUITypes.cpp b/source/gui/scripting/JSInterface_GUITypes.cpp index 1696b64033..ba9f494445 100755 --- a/source/gui/scripting/JSInterface_GUITypes.cpp +++ b/source/gui/scripting/JSInterface_GUITypes.cpp @@ -1,4 +1,4 @@ -// $Id: JSInterface_GUITypes.cpp,v 1.6 2004/09/03 14:12:43 philip Exp $ +// $Id$ #include "precompiled.h" @@ -13,7 +13,7 @@ JSClass JSI_GUISize::JSI_class = { JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, - NULL, NULL, NULL, NULL + NULL, NULL, NULL, JSI_GUISize::construct }; JSPropertySpec JSI_GUISize::JSI_props[] = @@ -121,7 +121,7 @@ JSClass JSI_GUIColor::JSI_class = { JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, - NULL, NULL, NULL, NULL + NULL, NULL, NULL, JSI_GUIColor::construct }; JSPropertySpec JSI_GUIColor::JSI_props[] = @@ -185,7 +185,7 @@ JSClass JSI_GUIMouse::JSI_class = { JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, - NULL, NULL, NULL, NULL + NULL, NULL, NULL, JSI_GUIMouse::construct }; JSPropertySpec JSI_GUIMouse::JSI_props[] = diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index dd303a8b14..49ce650634 100755 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -16,7 +16,7 @@ JSClass JSI_IGUIObject::JSI_class = { JSI_IGUIObject::getProperty, JSI_IGUIObject::setProperty, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, - NULL, NULL, NULL, NULL + NULL, NULL, NULL, JSI_IGUIObject::construct }; JSPropertySpec JSI_IGUIObject::JSI_props[] = diff --git a/source/scripting/ScriptingHost.cpp b/source/scripting/ScriptingHost.cpp index e6f571a878..fef21ec8f5 100755 --- a/source/scripting/ScriptingHost.cpp +++ b/source/scripting/ScriptingHost.cpp @@ -83,11 +83,6 @@ ScriptingHost::~ScriptingHost() JS_ShutDown(); } -JSContext* ScriptingHost::getContext() -{ - return( m_Context ); -} - void ScriptingHost::LoadScriptFromDisk(const std::string & fileName) { const char* fn = fileName.c_str(); diff --git a/source/scripting/ScriptingHost.h b/source/scripting/ScriptingHost.h index 229e6e639a..70c94f3961 100755 --- a/source/scripting/ScriptingHost.h +++ b/source/scripting/ScriptingHost.h @@ -82,9 +82,9 @@ public: // Helpers: - JSContext* getContext(); - inline JSContext *GetContext() - { return getContext(); } + // TODO: Remove one of these + inline JSContext *getContext() { return m_Context; } + inline JSContext *GetContext() { return m_Context; } void LoadScriptFromDisk(const std::string & fileName);