forked from 0ad/0ad
Add focus() script method to GUI objects.
Add basic GUI script timer. This was SVN commit r7908.
This commit is contained in:
parent
75494ef13c
commit
54d9224918
52
binaries/data/mods/public/gui/common/timer.js
Normal file
52
binaries/data/mods/public/gui/common/timer.js
Normal file
@ -0,0 +1,52 @@
|
||||
var g_TimerID = 0;
|
||||
var g_Timers = {};
|
||||
var g_Time = Date.now();
|
||||
|
||||
/**
|
||||
* Set a timeout to call func() after 'delay' msecs.
|
||||
* Returns an id that can be passed to clearTimeout.
|
||||
*/
|
||||
function setTimeout(func, delay)
|
||||
{
|
||||
var id = ++g_TimerID;
|
||||
g_Timers[id] = [g_Time + delay, func];
|
||||
return id;
|
||||
}
|
||||
|
||||
function clearTimeout(id)
|
||||
{
|
||||
delete g_Timers[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* If you want to use timers, then you must call this function regularly
|
||||
* (e.g. in a Tick handler)
|
||||
*/
|
||||
function updateTimers()
|
||||
{
|
||||
g_Time = Date.now();
|
||||
|
||||
// Collect the timers that need to run
|
||||
// (We do this in two stages to avoid deleting from the timer list while
|
||||
// we're in the middle of iterating through it)
|
||||
var run = [];
|
||||
for (var id in g_Timers)
|
||||
{
|
||||
if (g_Timers[id][0] <= g_Time)
|
||||
run.push(id);
|
||||
}
|
||||
for each (var id in run)
|
||||
{
|
||||
var t = g_Timers[id];
|
||||
if (!t)
|
||||
continue; // an earlier timer might have cancelled this one, so skip it
|
||||
|
||||
try {
|
||||
t[1]();
|
||||
} catch (e) {
|
||||
var stack = e.stack.trimRight().replace(/^/mg, ' '); // indent the stack trace
|
||||
error("Error in timer: "+e+"\n"+stack+"\n");
|
||||
}
|
||||
delete g_Timers[id];
|
||||
}
|
||||
}
|
@ -70,6 +70,8 @@ function init(attribs)
|
||||
}
|
||||
|
||||
updatePlayerList();
|
||||
|
||||
getGUIObjectByName("chatInput").focus();
|
||||
}
|
||||
|
||||
function cancelSetup()
|
||||
|
@ -109,6 +109,9 @@ function onTick()
|
||||
// Display rally points for selected buildings
|
||||
Engine.GuiInterfaceCall("DisplayRallyPoint", { "entities": g_Selection.toList() });
|
||||
}
|
||||
|
||||
// Run timers
|
||||
updateTimers();
|
||||
}
|
||||
|
||||
function onSimulationUpdate()
|
||||
|
@ -3,6 +3,7 @@
|
||||
<objects>
|
||||
|
||||
<script file="gui/common/functions_global_object.js" />
|
||||
<script file="gui/common/timer.js"/>
|
||||
<script file="gui/session_new/session.js"/>
|
||||
<script file="gui/session_new/selection.js"/>
|
||||
<script file="gui/session_new/input.js"/>
|
||||
|
@ -620,7 +620,7 @@ void CInput::HandleMessage(const SGUIMessage &Message)
|
||||
break;
|
||||
|
||||
case GUIM_GOT_FOCUS:
|
||||
//m_iBufferPos = 0; // TODO, a keeper?
|
||||
m_iBufferPos = 0;
|
||||
|
||||
break;
|
||||
|
||||
|
@ -319,6 +319,7 @@ protected:
|
||||
*/
|
||||
virtual void Destroy();
|
||||
|
||||
public:
|
||||
/**
|
||||
* This function is called with different messages
|
||||
* for instance when the mouse enters the object.
|
||||
@ -327,6 +328,7 @@ protected:
|
||||
*/
|
||||
virtual void HandleMessage(const SGUIMessage& UNUSED(Message)) {}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Draws the object.
|
||||
*
|
||||
@ -386,11 +388,13 @@ protected:
|
||||
m_MouseHovering = false;
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Take focus!
|
||||
*/
|
||||
void SetFocus();
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Check if object is focused.
|
||||
*/
|
||||
|
@ -47,6 +47,7 @@ JSPropertySpec JSI_IGUIObject::JSI_props[] =
|
||||
JSFunctionSpec JSI_IGUIObject::JSI_methods[] =
|
||||
{
|
||||
{ "toString", JSI_IGUIObject::toString, 0, 0, 0 },
|
||||
{ "focus", JSI_IGUIObject::focus, 0, 0, 0 },
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
@ -61,7 +62,8 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval
|
||||
// access nonexistent properties.)
|
||||
if (propName == "constructor" ||
|
||||
propName == "prototype" ||
|
||||
propName == "toString"
|
||||
propName == "toString" ||
|
||||
propName == "focus"
|
||||
)
|
||||
return JS_TRUE;
|
||||
|
||||
@ -577,3 +579,12 @@ JSBool JSI_IGUIObject::toString(JSContext* cx, JSObject* obj, uintN UNUSED(argc)
|
||||
*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buffer));
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
||||
JSBool JSI_IGUIObject::focus(JSContext* cx, JSObject* obj, uintN UNUSED(argc), jsval* UNUSED(argv), jsval* rval)
|
||||
{
|
||||
IGUIObject* e = (IGUIObject*)JS_GetPrivate( cx, obj );
|
||||
|
||||
e->SetFocus();
|
||||
e->HandleMessage(SGUIMessage(GUIM_GOT_FOCUS));
|
||||
return JS_TRUE;
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ namespace JSI_IGUIObject
|
||||
JSBool setProperty(JSContext* cx, JSObject* obj, jsval id, jsval* vp);
|
||||
JSBool construct(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
|
||||
JSBool toString(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
|
||||
JSBool focus(JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* rval);
|
||||
void init();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user