renderbug-cpp/lib/Figments/Figment.h

60 lines
1.6 KiB
C++

#pragma once
#include <Arduino.h>
#include <functional>
#include <ArduinoLog.h>
class Display;
class InputEvent;
class InputSource;
struct Loopable {
virtual void handleEvent(const InputEvent& event) {}
virtual void loop() = 0;
};
struct Task : public virtual Loopable {
virtual void onStart() {};
virtual void onStop() {};
enum State {
Running,
Stopped,
};
Task() {}
explicit Task(State initialState) : Task(0, initialState) {}
explicit Task(const char* name) : Task(name, Running) {}
Task(const char* name, State initialState) : name(name), state(initialState) {}
void start() { state = Running; onStart(); }
void stop() { onStop(); state = Stopped; }
virtual bool isFigment() const { return false; }
const char* name = "";
State state = Running;
};
struct TaskFunc: public Task {
TaskFunc(std::function<void()> func) : Task("lambda"), func(func) {}
void loop() override {func();}
std::function<void()> func;
};
struct Figment: public Task {
Figment() : Task() {}
explicit Figment(State initialState) : Task(initialState) {}
explicit Figment(const char* name) : Task(name) {}
Figment(const char* name, State initialState) : Task(name, initialState) {}
virtual void render(Display* dpy) const = 0;
bool isFigment() const override { return true; }
};
struct FigmentFunc: public Figment {
FigmentFunc(std::function<void(Display*)> func) : Figment("lambda"), func(func) {}
void loop() override {}
void render(Display* dpy) const override {
func(dpy);
}
std::function<void(Display*)> func;
};