mirror of
https://github.com/edubart/otclient.git
synced 2025-10-15 12:04:55 +02:00
new useful function, cycleEvent
This commit is contained in:
@@ -44,6 +44,9 @@ void EventDispatcher::poll()
|
||||
break;
|
||||
m_scheduledEventList.pop();
|
||||
scheduledEvent->execute();
|
||||
|
||||
if(scheduledEvent->nextCycle())
|
||||
m_scheduledEventList.push(scheduledEvent);
|
||||
}
|
||||
|
||||
// execute events list up to 10 times, this is needed because some events can schedule new events that would
|
||||
@@ -75,7 +78,15 @@ void EventDispatcher::poll()
|
||||
ScheduledEventPtr EventDispatcher::scheduleEvent(const std::function<void()>& callback, int delay)
|
||||
{
|
||||
assert(delay >= 0);
|
||||
ScheduledEventPtr scheduledEvent(new ScheduledEvent(callback, delay));
|
||||
ScheduledEventPtr scheduledEvent(new ScheduledEvent(callback, delay, 1));
|
||||
m_scheduledEventList.push(scheduledEvent);
|
||||
return scheduledEvent;
|
||||
}
|
||||
|
||||
ScheduledEventPtr EventDispatcher::cycleEvent(const std::function<void()>& callback, int delay)
|
||||
{
|
||||
assert(delay > 0);
|
||||
ScheduledEventPtr scheduledEvent(new ScheduledEvent(callback, delay, 0));
|
||||
m_scheduledEventList.push(scheduledEvent);
|
||||
return scheduledEvent;
|
||||
}
|
||||
|
@@ -32,7 +32,7 @@ class Event : public LuaObject
|
||||
public:
|
||||
Event(const std::function<void()>& callback) : m_callback(callback), m_canceled(false), m_executed(false) { }
|
||||
|
||||
void execute() {
|
||||
virtual void execute() {
|
||||
if(!m_canceled && !m_executed && m_callback) {
|
||||
m_callback();
|
||||
m_executed = true;
|
||||
@@ -52,15 +52,42 @@ protected:
|
||||
class ScheduledEvent : public Event
|
||||
{
|
||||
public:
|
||||
ScheduledEvent(const std::function<void()>& callback, int delay) : Event(callback) {
|
||||
ScheduledEvent(const std::function<void()>& callback, int delay, int maxCycles) : Event(callback) {
|
||||
m_ticks = g_clock.millis() + delay;
|
||||
m_delay = delay;
|
||||
m_maxCycles = maxCycles;
|
||||
m_cyclesExecuted = 0;
|
||||
}
|
||||
|
||||
void execute() {
|
||||
if(!m_canceled && m_callback && (m_maxCycles == 0 || m_cyclesExecuted < m_maxCycles)) {
|
||||
m_callback();
|
||||
m_executed = true;
|
||||
}
|
||||
m_cyclesExecuted++;
|
||||
}
|
||||
|
||||
bool nextCycle() {
|
||||
if(m_canceled)
|
||||
return false;
|
||||
if(m_maxCycles == 0 || m_cyclesExecuted < m_maxCycles) {
|
||||
m_ticks += m_delay;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int ticks() const { return m_ticks; }
|
||||
int reamaningTicks() const { return m_ticks - g_clock.millis(); }
|
||||
int delay() const { return m_delay; }
|
||||
int cyclesExecuted() { return m_cyclesExecuted; }
|
||||
int maxCycles() { return m_maxCycles; }
|
||||
|
||||
private:
|
||||
ticks_t m_ticks;
|
||||
int m_delay;
|
||||
int m_maxCycles;
|
||||
int m_cyclesExecuted;
|
||||
};
|
||||
|
||||
struct lessScheduledEvent : std::binary_function<ScheduledEventPtr, ScheduledEventPtr&, bool> {
|
||||
@@ -77,6 +104,7 @@ public:
|
||||
|
||||
EventPtr addEvent(const std::function<void()>& callback, bool pushFront = false);
|
||||
ScheduledEventPtr scheduleEvent(const std::function<void()>& callback, int delay);
|
||||
ScheduledEventPtr cycleEvent(const std::function<void()>& callback, int delay);
|
||||
|
||||
private:
|
||||
std::list<EventPtr> m_eventList;
|
||||
|
@@ -61,8 +61,12 @@ void Application::registerLuaFunctions()
|
||||
|
||||
// ScheduledEvent
|
||||
g_lua.registerClass<ScheduledEvent, Event>();
|
||||
g_lua.bindClassMemberFunction<ScheduledEvent>("reamaningTicks", &ScheduledEvent::reamaningTicks);
|
||||
g_lua.bindClassMemberFunction<ScheduledEvent>("nextCycle", &ScheduledEvent::nextCycle);
|
||||
g_lua.bindClassMemberFunction<ScheduledEvent>("ticks", &ScheduledEvent::ticks);
|
||||
g_lua.bindClassMemberFunction<ScheduledEvent>("reamaningTicks", &ScheduledEvent::reamaningTicks);
|
||||
g_lua.bindClassMemberFunction<ScheduledEvent>("delay", &ScheduledEvent::delay);
|
||||
g_lua.bindClassMemberFunction<ScheduledEvent>("cyclesExecuted", &ScheduledEvent::cyclesExecuted);
|
||||
g_lua.bindClassMemberFunction<ScheduledEvent>("maxCycles", &ScheduledEvent::maxCycles);
|
||||
|
||||
// UIWidget
|
||||
g_lua.registerClass<UIWidget>();
|
||||
@@ -600,4 +604,5 @@ void Application::registerLuaFunctions()
|
||||
g_lua.registerStaticClass("g_eventDispatcher");
|
||||
g_lua.bindClassStaticFunction("g_eventDispatcher", "addEvent", std::bind(&EventDispatcher::addEvent, &g_eventDispatcher, std::placeholders::_1, std::placeholders::_2));
|
||||
g_lua.bindClassStaticFunction("g_eventDispatcher", "scheduleEvent", std::bind(&EventDispatcher::scheduleEvent, &g_eventDispatcher, std::placeholders::_1, std::placeholders::_2));
|
||||
g_lua.bindClassStaticFunction("g_eventDispatcher", "cycleEvent", std::bind(&EventDispatcher::cycleEvent, &g_eventDispatcher, std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
Reference in New Issue
Block a user