Initial work on queueing up UI events for processing. Key events excepted for now.

This commit is contained in:
Henrik Rydgård 2023-05-01 14:23:47 +02:00
parent 2675d6ea43
commit cca613e785
2 changed files with 89 additions and 29 deletions

View File

@ -60,6 +60,41 @@ void UIScreen::DoRecreateViews() {
}
}
void UIScreen::touch(const TouchInput &touch) {
if (root_) {
if (ClickDebug && (touch.flags & TOUCH_DOWN)) {
INFO_LOG(SYSTEM, "Touch down!");
std::vector<UI::View *> views;
root_->Query(touch.x, touch.y, views);
for (auto view : views) {
INFO_LOG(SYSTEM, "%s", view->DescribeLog().c_str());
}
}
}
std::lock_guard<std::mutex> guard(eventQueueLock_);
QueuedEvent ev{};
ev.type = QueuedEventType::TOUCH;
ev.touch = touch;
eventQueue_.push_back(ev);
}
void UIScreen::axis(const AxisInput &axis) {
std::lock_guard<std::mutex> guard(eventQueueLock_);
QueuedEvent ev{};
ev.type = QueuedEventType::AXIS;
ev.axis = axis;
eventQueue_.push_back(ev);
}
bool UIScreen::key(const KeyInput &key) {
if (root_ && !ignoreInput_) {
// TODO: Make key events async too. The return value is troublesome, though.
return UI::KeyEvent(key, root_);
}
return false;
}
void UIScreen::update() {
bool vertical = UseVerticalLayout();
if (vertical != lastVertical_) {
@ -71,6 +106,39 @@ void UIScreen::update() {
if (root_) {
UpdateViewHierarchy(root_);
while (true) {
QueuedEvent ev{};
{
std::lock_guard<std::mutex> guard(eventQueueLock_);
if (!eventQueue_.empty()) {
ev = eventQueue_.front();
eventQueue_.pop_front();
} else {
break;
}
}
if (ignoreInput_) {
continue;
}
switch (ev.type) {
case QueuedEventType::KEY:
break;
case QueuedEventType::TOUCH:
if (ClickDebug && (ev.touch.flags & TOUCH_DOWN)) {
INFO_LOG(SYSTEM, "Touch down!");
std::vector<UI::View *> views;
root_->Query(ev.touch.x, ev.touch.y, views);
for (auto view : views) {
INFO_LOG(SYSTEM, "%s", view->DescribeLog().c_str());
}
}
UI::TouchEvent(ev.touch, root_);
break;
case QueuedEventType::AXIS:
UI::AxisEvent(ev.axis, root_);
break;
}
}
}
}
@ -148,34 +216,6 @@ TouchInput UIScreen::transformTouch(const TouchInput &touch) {
return updated;
}
void UIScreen::touch(const TouchInput &touch) {
if (root_ && !ignoreInput_) {
if (ClickDebug && (touch.flags & TOUCH_DOWN)) {
INFO_LOG(SYSTEM, "Touch down!");
std::vector<UI::View *> views;
root_->Query(touch.x, touch.y, views);
for (auto view : views) {
INFO_LOG(SYSTEM, "%s", view->DescribeLog().c_str());
}
}
UI::TouchEvent(touch, root_);
}
}
bool UIScreen::key(const KeyInput &key) {
if (root_ && !ignoreInput_) {
return UI::KeyEvent(key, root_);
}
return false;
}
void UIScreen::axis(const AxisInput &axis) {
if (root_ && !ignoreInput_) {
UI::AxisEvent(axis, root_);
}
}
void UIScreen::TriggerFinish(DialogResult result) {
// From here on, this dialog cannot receive input.
ignoreInput_ = true;

View File

@ -1,6 +1,8 @@
#pragma once
#include <set>
#include <mutex>
#include <string>
#include <deque>
#include "Common/Math/lin/vec3.h"
#include "Common/UI/Screen.h"
@ -13,6 +15,21 @@ namespace Draw {
class DrawContext;
}
enum class QueuedEventType : u8 {
KEY,
AXIS,
TOUCH,
};
struct QueuedEvent {
QueuedEventType type;
union {
TouchInput touch;
KeyInput key;
AxisInput axis;
};
};
class UIScreen : public Screen {
public:
UIScreen();
@ -57,6 +74,9 @@ private:
bool recreateViews_ = true;
bool lastVertical_;
std::mutex eventQueueLock_;
std::deque<QueuedEvent> eventQueue_;
};
class UIDialogScreen : public UIScreen {