Conversation code cleaned up a lot.
This commit is contained in:
80
src/display/animation/queue.c
Normal file
80
src/display/animation/queue.c
Normal file
@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
void queueInit(queue_t *queue) {
|
||||
queue->timeline = 0;
|
||||
queue->count = 0;
|
||||
queue->current = ANIMATION_QUEUE_START;
|
||||
}
|
||||
|
||||
queueaction_t * queueNext(queue_t *queue) {
|
||||
queueaction_t *action;
|
||||
|
||||
// Is there a currently running action? If so, end it.
|
||||
if(queue->current != ANIMATION_QUEUE_START) {
|
||||
action = queue->items + queue->current;
|
||||
if(action->onEnd != NULL) action->onEnd(queue, action, queue->current);
|
||||
}
|
||||
|
||||
// Prepare to go to next action if there is a next action.
|
||||
queue->current++;
|
||||
queue->actionStarted = queue->timeline;
|
||||
if(queue->current >= queue->count) return NULL;
|
||||
|
||||
// Go to next action, start it.
|
||||
action = queue->items + queue->current;
|
||||
if(action->onStart != NULL) action->onStart(queue, action, queue->current);
|
||||
return action;
|
||||
}
|
||||
|
||||
queueaction_t * queueAdd(queue_t *queue) {
|
||||
queueaction_t *action;
|
||||
action = queue->items + queue->count;
|
||||
|
||||
action->index = queue->count;
|
||||
action->data = NULL;
|
||||
action->onStart = NULL;
|
||||
action->onUpdate = NULL;
|
||||
action->onEnd = NULL;
|
||||
|
||||
queue->count++;
|
||||
return action;
|
||||
}
|
||||
|
||||
void queueUpdate(queue_t *queue, engine_t *engine) {
|
||||
queueaction_t *action;
|
||||
queue->timeline += engine->time.delta;
|
||||
if(queue->current >= queue->count) return;
|
||||
|
||||
action = queue->items + queue->current;
|
||||
if(action->onUpdate != NULL) {
|
||||
action->onUpdate(queue, action, queue->current);
|
||||
}
|
||||
}
|
||||
|
||||
void queueDispose(queue_t *queue) {
|
||||
queueaction_t *action;
|
||||
if(queue->current >= queue->count) return;
|
||||
action = queue->items + queue->current;
|
||||
if(action->onEnd != NULL) action->onEnd(queue, action, queue->current);
|
||||
}
|
||||
|
||||
|
||||
void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i) {
|
||||
float n = queue->timeline - queue->actionStarted;
|
||||
if(n < queue->delays[i]) return;
|
||||
queueNext(queue);
|
||||
}
|
||||
|
||||
queueaction_t * queueDelay(queue_t *queue, float delay) {
|
||||
queueaction_t *action = queueAdd(queue);
|
||||
queue->delays[action->index] = delay;
|
||||
action->onUpdate = &_queueDelayUpdate;
|
||||
return action;
|
||||
}
|
54
src/display/animation/queue.h
Normal file
54
src/display/animation/queue.h
Normal file
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
|
||||
/**
|
||||
* Initialize the queue set.
|
||||
* @param queue Queue to initialize.
|
||||
*/
|
||||
void queueInit(queue_t *queue);
|
||||
|
||||
/**
|
||||
* Goes to the next action, or start the queue if this is the first action.
|
||||
* @param queue Queue to skip to the next action.
|
||||
* @returns Action that was just started.
|
||||
*/
|
||||
queueaction_t * queueNext(queue_t *queue);
|
||||
|
||||
/**
|
||||
* Add a queue action to the queue.
|
||||
* @param convo Queue to add to.
|
||||
* @return Pointer to the queue action that was added.
|
||||
*/
|
||||
queueaction_t * queueAdd(queue_t *queue);
|
||||
|
||||
/**
|
||||
* Updates the queue logic.
|
||||
* @param convo Queue to update.
|
||||
* @param engine Engine used to update.
|
||||
*/
|
||||
void queueUpdate(queue_t *queue, engine_t *engine);
|
||||
|
||||
/**
|
||||
* Dispose the queue when finished.
|
||||
* @param queue Queue to dispose.
|
||||
*/
|
||||
void queueDispose(queue_t *queue);
|
||||
|
||||
|
||||
/** Callbacks for Queue Delay Action */
|
||||
void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i);
|
||||
|
||||
/**
|
||||
* Adds a delay action to a queue.
|
||||
* @param queue Queue to add to.
|
||||
* @param delay Delay time (in seconds) to have.
|
||||
* @return Pointer to the action added to the queue.
|
||||
*/
|
||||
queueaction_t * queueDelay(queue_t *queue, float delay);
|
Reference in New Issue
Block a user