mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
add container:clearInterval
This commit is contained in:
parent
1492bb093e
commit
c444e266ba
4 changed files with 49 additions and 8 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ namespace gui {
|
|||
using OnTimeOut = std::function<void()>;
|
||||
|
||||
struct IntervalEvent {
|
||||
int id;
|
||||
OnTimeOut callback;
|
||||
float interval;
|
||||
float timer;
|
||||
|
|
|
|||
|
|
@ -157,7 +157,18 @@ static int l_container_set_interval(lua::State* L) {
|
|||
if (auto container = std::dynamic_pointer_cast<Container>(node.node)) {
|
||||
lua::pushvalue(L, 3);
|
||||
auto runnable = lua::create_runnable(L);
|
||||
container->listenInterval(interval, runnable);
|
||||
return lua::pushinteger(
|
||||
L, container->listenInterval(interval, runnable)
|
||||
);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_container_clear_interval(lua::State* L) {
|
||||
auto node = get_document_node(L, 1);
|
||||
auto id = lua::tointeger(L, 2);
|
||||
if (auto container = std::dynamic_pointer_cast<Container>(node.node)) {
|
||||
container->cancelInterval(id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -502,6 +513,13 @@ static int p_set_interval(UINode* node, lua::State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int p_clear_interval(UINode* node, lua::State* L) {
|
||||
if (dynamic_cast<Container*>(node)) {
|
||||
return lua::pushcfunction(L, lua::wrap<l_container_clear_interval>);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int p_get_content_offset(UINode* node, lua::State* L) {
|
||||
return lua::pushvec(L, node->getContentOffset());
|
||||
}
|
||||
|
|
@ -667,6 +685,7 @@ static int l_gui_getattr(lua::State* L) {
|
|||
{"reposition", p_get_reposition},
|
||||
{"clear", p_get_clear},
|
||||
{"setInterval", p_set_interval},
|
||||
{"clearInterval", p_clear_interval},
|
||||
{"placeholder", p_get_placeholder},
|
||||
{"hint", p_get_hint},
|
||||
{"valid", p_is_valid},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue