add container:clearInterval

This commit is contained in:
MihailRis 2026-10-31 17:57:17 +03:00
parent 1492bb093e
commit c444e266ba
4 changed files with 49 additions and 8 deletions

View file

@ -92,10 +92,18 @@ void Container::act(float delta) {
// TODO: make it interval-based
gui.getWindow().setShouldRefresh();
}
for (IntervalEvent& event : intervalEvents) {
event.timer += delta;
if (event.timer > event.interval) {
event.callback();
for (size_t i = 0; i < intervalEvents.size(); i++) {
if (intervalEvents[i].repeat == 0) {
continue;
}
intervalEvents[i].timer += delta;
if (intervalEvents[i].timer > intervalEvents[i].interval) {
auto callback = intervalEvents[i].callback;
callback();
IntervalEvent& event = intervalEvents[i];
if (event.repeat == 0) {
continue;
}
event.timer = fmod(event.timer, event.interval);
if (event.repeat > 0) {
event.repeat--;
@ -232,8 +240,19 @@ void Container::clear() {
refresh();
}
void Container::listenInterval(float interval, OnTimeOut callback, int repeat) {
intervalEvents.push_back({std::move(callback), interval, 0.0f, repeat});
int Container::listenInterval(float interval, OnTimeOut callback, int repeat) {
int id = nextIntervalId++;
intervalEvents.push_back({id, std::move(callback), interval, 0.0f, repeat});
return id;
}
void Container::cancelInterval(int id) {
for (auto& event : intervalEvents) {
if (event.id == id) {
event.repeat = 0;
return;
}
}
}
void Container::setSize(const glm::vec2& size) {

View file

@ -13,6 +13,7 @@ namespace gui {
protected:
std::vector<std::shared_ptr<UINode>> nodes;
std::vector<IntervalEvent> intervalEvents;
int nextIntervalId = 1;
int scrollY = 0;
int scrollX = 0;
int scrollStep = 40;
@ -41,7 +42,8 @@ namespace gui {
virtual void remove(const std::string& id);
virtual void scrolled(int value) override;
virtual void setScrollable(bool flag);
void listenInterval(float interval, OnTimeOut callback, int repeat=-1);
int listenInterval(float interval, OnTimeOut callback, int repeat=-1);
void cancelInterval(int id);
virtual glm::vec2 getContentOffset() const override {
return glm::vec2(scrollX, scrollY);
};

View file

@ -8,6 +8,7 @@ namespace gui {
using OnTimeOut = std::function<void()>;
struct IntervalEvent {
int id;
OnTimeOut callback;
float interval;
float timer;