Bug fixes. (#367)

- Do not draw more frames than what is needed.
- Fix MenuEntry transform state.
- Fix Canvas animated example.
This commit is contained in:
Arthur Sonzogni 2022-03-26 07:55:52 +01:00 committed by GitHub
parent 548fa51b71
commit 62fb6298be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 57 deletions

View File

@ -167,7 +167,7 @@ int main(int argc, const char* argv[]) {
for (int x = 0; x < size; x++) { for (int x = 0; x < size; x++) {
float dx = x - mx; float dx = x - mx;
float dy = y - my; float dy = y - my;
ys[y][x] = (int)(-1.5 + 3.0 * std::exp(-0.2f * (dx * dx + dy * dy))); ys[y][x] = -1.5 + 3.0 * std::exp(-0.2f * (dx * dx + dy * dy));
} }
} }
for (int y = 0; y < size; y++) { for (int y = 0; y < size; y++) {

View File

@ -79,7 +79,7 @@ class ScreenInteractive : public Screen {
std::atomic<bool> quit_ = false; std::atomic<bool> quit_ = false;
std::thread event_listener_; std::thread event_listener_;
std::thread animation_listener_; std::thread animation_listener_;
bool animation_requested_ = true; bool animation_requested_ = false;
animation::TimePoint previous_animation_time; animation::TimePoint previous_animation_time;
int cursor_x_ = 1; int cursor_x_ = 1;

View File

@ -512,9 +512,9 @@ Component MenuEntry(ConstStringRef label, Ref<MenuEntryOption> option) {
EntryState state = { EntryState state = {
*label_, *label_,
false,
hovered_, hovered_,
focused, focused,
false,
}; };
Element element = Element element =

View File

@ -485,63 +485,64 @@ void ScreenInteractive::Main(Component component) {
Clear(); Clear();
}; };
draw(); bool attempt_draw = true;
while (!quit_) { while (!quit_) {
if (!task_receiver_->HasPending()) if (attempt_draw && !task_receiver_->HasPending()) {
draw(); draw();
attempt_draw = false;
bool continue_event_loop = true;
while (continue_event_loop) {
continue_event_loop = false;
Task task;
if (!task_receiver_->Receive(&task))
break;
std::visit(
[&](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
// Handle Event.
if constexpr (std::is_same_v<T, Event>) {
if (arg.is_cursor_reporting()) {
cursor_x_ = arg.cursor_x();
cursor_y_ = arg.cursor_y();
return;
}
if (arg.is_mouse()) {
arg.mouse().x -= cursor_x_;
arg.mouse().y -= cursor_y_;
}
arg.screen_ = this;
component->OnEvent(arg);
}
// Handle callback
if constexpr (std::is_same_v<T, Closure>) {
arg();
return;
}
// Handle Animation
if constexpr (std::is_same_v<T, AnimationTask>) {
if (!animation_requested_) {
continue_event_loop = true;
return;
}
animation_requested_ = false;
animation::TimePoint now = animation::Clock::now();
animation::Duration delta = now - previous_animation_time;
previous_animation_time = now;
animation::Params params(delta);
component->OnAnimation(params);
}
},
task);
} }
Task task;
if (!task_receiver_->Receive(&task))
break;
// clang-format off
std::visit([&](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
// Handle Event.
if constexpr (std::is_same_v<T, Event>) {
if (arg.is_cursor_reporting()) {
cursor_x_ = arg.cursor_x();
cursor_y_ = arg.cursor_y();
return;
}
if (arg.is_mouse()) {
arg.mouse().x -= cursor_x_;
arg.mouse().y -= cursor_y_;
}
arg.screen_ = this;
component->OnEvent(arg);
attempt_draw = true;
return;
}
// Handle callback
if constexpr (std::is_same_v<T, Closure>) {
arg();
return;
}
// Handle Animation
if constexpr (std::is_same_v<T, AnimationTask>) {
if (!animation_requested_)
return;
animation_requested_ = false;
animation::TimePoint now = animation::Clock::now();
animation::Duration delta = now - previous_animation_time;
previous_animation_time = now;
animation::Params params(delta);
component->OnAnimation(params);
attempt_draw = true;
return;
}
},
task);
// clang-format on
} }
} }