The timer.ujml sample implements multiple timers and fires events via both listener objects and UJML events.
This sample shows how to maintain a list of components which implement a listener interface and fire and event to them, as well as raise a separate UJML event. See Samples, Components, Interfaces, Component Events.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ujml PUBLIC "-//UIEVOLUTION//DTD UJML 2.1//EN" "http://www.uievolution.com/dtd/ujml-2.1.dtd"[
<!ENTITY TIMER_MAX_COUNT "8">
]>
<!--
Copyright (c) 2000-2007 UIEvolution, Inc. U.S.A.
http://www.uievolution.com
All rights reserved.
Provided under the Software License Agreement for Non-commercial Use of
UIEvolution Platform Software - modification and incorporation into
applications allowed, subject to the terms thereof.
-->
<!--
timer.ujml
Timer component.
-->
<ujml>
<interfaces>
<interface name="ITimerListener"/><!-- Forward declaration. -->
<include interface="ITimerSource" file="itimer.ujmi"/>
<include interface="ITimerListener" file="itimer.ujmi"/>
</interfaces>
<component name="TimerSource">
<interfaces>
<interface name="ITimerSource" access="export"/>
<interface name="ITimerListener" access="import"/>
</interfaces>
<state-variables>
<state-var name="sTimer" type="boolean" size="&TIMER_MAX_COUNT;"/>
</state-variables>
<variables>
<var name="mListeners" type="ITimerListener" size="0"/>
<var name="mListenerCount" type="int"/>
<var name="mRunning" type="boolean"/>
<var name="mTimerCount" type="int"/>
<var name="mTimerID" type="int" size="&TIMER_MAX_COUNT;"/>
<var name="mTimerInterval" type="int" size="&TIMER_MAX_COUNT;"/>
</variables>
<functions>
<!--
Initializes the timer.
-->
<function name="ITimerSource.init" type="void">
<parameters>
<var name="maxListeners" type="int"/>
</parameters>
<variables>
<var name="i" type="int"/>
</variables>
<script>
// Set defaults.
mRunning = false;
mListenerCount = 0;
mTimerCount = 0;
for (i = 0;i &_LT; &TIMER_MAX_COUNT;;i++)
{
sTimer[i] = false;
mTimerID[i] = 0;
mTimerInterval[i] = 0;
}
// Set listener array size.
_resizeArray(mListeners, maxListeners);
</script>
</function>
<!--
Adds an ITimerListener component to the listener list.
Returns true if the listener was successfully added.
Note: How many listeners a timer can contain depends on the
maxListeners argument passed in the init() function.
-->
<function name="ITimerSource.addTimerListener" type="boolean">
<parameters>
<var name="listener" type="ITimerListener"/>
</parameters>
<variables>
<var name="result" type="boolean"/>
<var name="i" type="int"/>
</variables>
<script>
// Do we have anything to add?
result = false;
if (!_is_null(listener))
{
// Assume success.
result = true;
// Check if the listener is already there.
for (i = 0;i &_LT; mListenerCount;i++)
{
if (_isSameInstance(mListeners[i], listener))
{
// Found it. Set the result and exit loop.
result = false;
i = mListenerCount;
}
}
// Add the listener?
if (result)
{
// Do we have room?
if (_getArrayLength(mListeners, 0) &_GT; mListenerCount)
{
// Add it.
mListeners[mListenerCount] = listener;
mListenerCount++;
}
else // No room.
{
result = false;
}
}
}
</script>
<return><eval>result</eval></return>
</function>
<!--
Clears the listener list.
-->
<function name="ITimerSource.clearTimerListeners" type="void">
<variables>
<var name="cnt" type="int"/>
<var name="i" type="int"/>
</variables>
<script>
// Clear all listener references from the list.
cnt = _getArrayLength(mListeners, 0);
for (i = 0;i &_LT; cnt;i++)
{
mListeners[i] = null;
}
// Reset the count.
mListenerCount = 0;
</script>
</function>
<!--
Add a timer for a specified ID with the specified tick
time.
Returns true if the timer was successfully added.
Note: How many timers can run at a time is implementation
dependent. Attempting to start too many timers will return
false.
-->
<function name="ITimerSource.addTimer" type="boolean">
<parameters>
<var name="id" type="int"/>
<var name="tickMilliseconds" type="int"/>
</parameters>
<variables>
<var name="result" type="boolean"/>
<var name="i" type="int"/>
<var name="idx" type="int"/>
</variables>
<script>
// Assume success.
result = true;
// Check if the timer is already there.
idx = -1;
for (i = 0;i &_LT; mTimerCount;i++)
{
if (mTimerID[i] == id)
{
// Found it. Set the index and exit loop.
idx = i;
i = mTimerCount;
}
}
// If not found, add it.
if (idx &_LT; 0)
{
// Do we have room?
if (&TIMER_MAX_COUNT; &_GT; mTimerCount)
{
// Add it.
idx = mTimerCount;
mTimerID[idx] = id;
mTimerCount++;
}
else // No room.
{
result = false;
}
}
// Do we have a valid ID?
if (result)
{
// Set the interval.
mTimerInterval[idx] = tickMilliseconds;
// Start the timer?
if(tickMilliseconds &_GT; 0)
{
sTimer[idx] = mRunning;
}
else
{
sTimer[idx] = false;
}
}
</script>
<return><eval>result</eval></return>
</function>
<!--
Clears the timer list.
-->
<function name="ITimerSource.clearTimers" type="void">
<variables>
<var name="i" type="int"/>
</variables>
<script>
// Turn off any running timers.
for (i = 0;i &_LT; mTimerCount;i++)
{
sTimer[i] = false;
}
// Reset the count.
mTimerCount = 0;
</script>
</function>
<!--
Starts all timers.
-->
<function name="ITimerSource.start" type="void">
<variables>
<var name="i" type="int"/>
</variables>
<script>
// Are we already running?
if (!mRunning)
{
// We are now.
mRunning = true;
// Turn on timers.
for (i = 0;i &_LT; mTimerCount;i++)
{
if (mTimerInterval[i] &_GT; 0)
{
sTimer[i] = true;
}
else
{
sTimer[i] = false;
}
}
}
</script>
</function>
<!--
Stops all timers.
-->
<function name="ITimerSource.stop" type="void">
<script>
// Stop timers on next tick.
mRunning = false;
</script>
</function>
</functions>
<script>
// Set defaults
ITimerSource.init(1);
</script>
<states>
<state var="sTimer" index="*">
<transition value="true">
<delay><eval>mTimerInterval[_state_index()]</eval></delay>
<variables>
<var name="idx" type="int"/>
<var name="i" type="int"/>
</variables>
<script>
// Get index
idx = _state_index();
// Notify listeners
for (i = 0;i &_LT; mListenerCount;i++)
{
mListeners[i].tick(mTimerID[idx]);
}
// Fire event
ITimerSource.onTick(mTimerID[idx]);
// Do again?
if (mTimerInterval[idx] &_GT; 0)
{
_clear_state(sTimer[idx]);
sTimer[idx] = mRunning;
}
else
{
sTimer[idx] = false;
}
</script>
</transition>
</state>
</states>
</component>
</ujml>|
Copyright (c) 2000-2007 UIEvolution, Inc. All rights reserved.
|
|
What do you think about this topic? Send feedback!
|