6#include "TinyGPU/Drivers/DisplayDriver.h"
7#include "TinyGPU/Input/GestureDetector.h"
8#include "TinyGPU/Surface/WindowedSurface.h"
10#include "TinyMaterialDesign/Core/Container.h"
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
75 Screen() {
this->setTheme(ownedTheme_); }
79 explicit Screen(
const MaterialTheme<RGB_T>& theme) : ownedTheme_(theme) {
this->setTheme(ownedTheme_); }
84 this->addChild(widget);
92 typename Container<RGB_T>::ChildAtFn at) {
93 this->setChildProvider(std::move(count), std::move(at));
101 fixedWidgets_.push_back(&widget);
102 widget.setTheme(ownedTheme_);
113 for (Widget<RGB_T>* w : fixedWidgets_) {
114 if (w !=
nullptr) w->setTheme(ownedTheme_);
116 if (dialog_ !=
nullptr) dialog_->setTheme(ownedTheme_);
130 backgroundColor_ = color;
131 hasBackgroundColor_ =
true;
165 void draw(tinygpu::ISurface<RGB_T>& target)
override {
166 bounds.w =
static_cast<int32_t>(target.width());
167 bounds.h =
static_cast<int32_t>(target.height());
169 target.clear(hasBackgroundColor_ ? backgroundColor_ : ownedTheme_.colors.background);
170 Container<RGB_T>::draw(target);
171 for (Widget<RGB_T>* widget : fixedWidgets_) {
172 if (widget->visible) widget->draw(target);
174 if (dialog_ !=
nullptr && dialog_->visible) {
175 dialog_->draw(target);
220 void drawDirect(tinygpu::DisplayDriver<RGB_T>& driver, tinygpu::ISurface<RGB_T>& scratch,
221 int32_t viewportWidth, int32_t viewportHeight) {
222 bounds.w = viewportWidth;
223 bounds.h = viewportHeight;
226 if (dialog_ !=
nullptr && dialog_->visible) {
227 drawModalDirect(driver, scratch, viewportWidth, viewportHeight);
232 const RGB_T background = hasBackgroundColor_ ? backgroundColor_ : ownedTheme_.colors.background;
242 driver.writeColor(
static_cast<size_t>(viewportWidth),
static_cast<size_t>(viewportHeight),
245 const int scrollCount =
this->effectiveCount();
246 for (
int i = 0; i < scrollCount; ++i) {
247 Widget<RGB_T>* widget =
this->effectiveChild(i);
248 if (!widget->visible)
continue;
249 Bounds screenBounds = widget->bounds;
250 screenBounds
.y -=
this->scrollOffset_;
251 if (!intersectsViewport(screenBounds, viewportWidth, viewportHeight))
continue;
252 widget->bounds.y -=
this->scrollOffset_;
253 drawWidgetDirect(driver, scratch, *widget, background);
254 widget->bounds.y +=
this->scrollOffset_;
256 drawScrollbarDirect(driver, scratch, viewportWidth, viewportHeight);
257 for (Widget<RGB_T>* widget : fixedWidgets_) {
258 if (!widget->visible)
continue;
259 if (!intersectsViewport(widget->bounds, viewportWidth, viewportHeight))
continue;
260 drawWidgetDirect(driver, scratch, *widget, background);
268 bool changed =
Container<RGB_T>::update(nowMs);
269 for (Widget<RGB_T>* widget : fixedWidgets_) changed |= widget->update(nowMs);
270 if (dialog_ !=
nullptr) changed |= dialog_->update(nowMs);
271 if (changed) dirty_ =
true;
281 if (dialog_ !=
nullptr)
return false;
282 Widget<RGB_T>* fixed = hitTestFixed(x, y);
283 if (fixed !=
nullptr)
return fixed->isDraggableAt(x, y);
284 return Container<RGB_T>::isDraggableAt(x, y);
288 void handleGesture(
const tinygpu::GestureEvent& event) { onGesture(event); }
290 bool onGesture(
const tinygpu::GestureEvent& event)
override {
291 using tinygpu::GesturePhase;
292 using tinygpu::GestureType;
300 if (dialog_ !=
nullptr) {
303 const bool handled = dialog_->enabled ? dialog_->onGesture(event) :
false;
304 if (Container<RGB_T>::isContinuousType(event.type) && event.phase == GesturePhase::kEnded) {
305 activeFixedWidget_ =
nullptr;
306 usingFixedWidget_ =
false;
307 this->activeChild_ =
nullptr;
308 this->scrollDragActive_ =
false;
313 if (Container<RGB_T>::isContinuousType(event.type)) {
324 if (event.phase == GesturePhase::kBegan) {
325 usingFixedWidget_ =
false;
326 activeFixedWidget_ =
nullptr;
327 if (event.type != GestureType::kPan && event.type != GestureType::kScroll) {
328 Widget<RGB_T>* hit = hitTestFixed(event.startPoint.x, event.startPoint.y);
329 if (hit !=
nullptr) {
330 usingFixedWidget_ =
true;
331 activeFixedWidget_ = hit;
336 bool handled =
false;
337 if (usingFixedWidget_) {
338 if (activeFixedWidget_ !=
nullptr && activeFixedWidget_->enabled) {
339 handled = activeFixedWidget_->onGesture(event);
342 handled = Container<RGB_T>::onGesture(event);
345 if (event.phase == GesturePhase::kEnded) {
346 activeFixedWidget_ =
nullptr;
347 usingFixedWidget_ =
false;
358 Widget<RGB_T>* hit = hitTestFixed(event.point.x, event.point.y);
359 if (hit !=
nullptr && hit->enabled)
return hit->onGesture(event);
360 return Container<RGB_T>::onGesture(event);
368 dialog.setTheme(ownedTheme_);
383 static bool intersectsViewport(
const Bounds& bounds, int32_t viewportWidth, int32_t viewportHeight) {
385 bounds
.x < viewportWidth;
391 Widget<RGB_T>* hitTestFixed(int32_t x, int32_t y)
const {
392 for (
auto it = fixedWidgets_.rbegin(); it != fixedWidgets_.rend(); ++it) {
393 Widget<RGB_T>* widget = *it;
394 if (widget->visible && widget->enabled && widget->bounds.contains(x, y))
return widget;
403 void drawWidgetDirect(tinygpu::DisplayDriver<RGB_T>& driver, tinygpu::ISurface<RGB_T>& scratch,
404 Widget<RGB_T>& widget, RGB_T background) {
405 const int32_t x = widget.bounds.x;
406 const int32_t y = widget.bounds.y;
407 const int32_t w = widget.bounds.w;
408 const int32_t h = widget.bounds.h;
409 if (w <= 0 || h <= 0)
return;
410 if (!scratch.resize(
static_cast<size_t>(w),
static_cast<size_t>(h)))
return;
412 tinygpu::WindowedSurface<RGB_T> window(scratch, x, y,
static_cast<size_t>(w),
413 static_cast<size_t>(h), scratch.font());
414 window.clear(background);
416 driver.writeData(scratch, x, y);
424 void drawScrollbarDirect(tinygpu::DisplayDriver<RGB_T>& driver, tinygpu::ISurface<RGB_T>& scratch,
425 int32_t viewportWidth, int32_t viewportHeight) {
426 const int32_t content =
this->contentHeight();
427 if (content <= viewportHeight)
return;
429 constexpr int32_t kBarWidth = 4;
430 const int32_t trackX = viewportWidth - kBarWidth;
431 if (!scratch.resize(
static_cast<size_t>(kBarWidth),
static_cast<size_t>(viewportHeight)))
return;
433 tinygpu::WindowedSurface<RGB_T> window(scratch, trackX, 0,
static_cast<size_t>(kBarWidth),
434 static_cast<size_t>(viewportHeight), scratch.font());
435 window.fillRect(trackX, 0, kBarWidth, viewportHeight,
436 blend(ownedTheme_.colors.surface, ownedTheme_.colors.outline, 0.25f));
438 const int32_t maxScroll = content - viewportHeight;
441 const int32_t thumbHeight =
this->scrollThumbHeight(content);
442 const int32_t thumbY = maxScroll > 0
443 ? (
this->scrollOffset_ * (viewportHeight - thumbHeight)) / maxScroll
445 window.fillRoundRect(trackX, thumbY, kBarWidth, thumbHeight, kBarWidth / 2, ownedTheme_.colors.primary);
446 driver.writeData(scratch, trackX, 0);
453 void drawModalDirect(tinygpu::DisplayDriver<RGB_T>& driver, tinygpu::ISurface<RGB_T>& scratch,
454 int32_t viewportWidth, int32_t viewportHeight) {
455 const RGB_T background = hasBackgroundColor_ ? backgroundColor_ : ownedTheme_.colors.background;
457 constexpr int32_t kModalBandHeight = 40;
458 if (scratch.resize(
static_cast<size_t>(viewportWidth),
static_cast<size_t>(kModalBandHeight))) {
459 for (int32_t bandY = 0; bandY < viewportHeight; bandY += kModalBandHeight) {
460 tinygpu::WindowedSurface<RGB_T> window(scratch, 0, bandY,
461 static_cast<size_t>(viewportWidth),
462 static_cast<size_t>(viewportHeight),
472 window.clear(background);
473 dialog_->drawBackground(window);
474 driver.writeData(scratch, 0, bandY);
478 const int childCount = dialog_->childCount();
479 for (
int i = 0; i < childCount; ++i) {
480 Widget<RGB_T>* child = dialog_->child(i);
481 if (child !=
nullptr && child->visible) {
482 drawWidgetDirect(driver, scratch, *child, background);
487 std::vector<Widget<RGB_T>*> fixedWidgets_;
488 Widget<RGB_T>* activeFixedWidget_ =
nullptr;
489 bool usingFixedWidget_ =
false;
491 Widget<RGB_T>* dialog_ =
nullptr;
492 RGB_T backgroundColor_{};
493 bool hasBackgroundColor_ =
false;
A widget that holds child widgets, draws/dispatches gestures to them, and scrolls its content vertica...
Definition: Container.h:56
Owns a set of widgets, draws them, and routes gesture events to whichever one should handle each even...
Definition: Screen.h:71
bool isDirty() const
Definition: Screen.h:158
void addFixedWidget(Widget< RGB_T > &widget)
Definition: Screen.h:100
void setBackgroundColor(RGB_T color)
Definition: Screen.h:129
Screen()
Definition: Screen.h:75
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Screen.h:290
void dismissDialog()
Definition: Screen.h:371
bool hasDialog() const
True while a dialog (or menu, ...) is presented.
Definition: Screen.h:377
void drawDirect(tinygpu::DisplayDriver< RGB_T > &driver, tinygpu::ISurface< RGB_T > &scratch, int32_t viewportWidth, int32_t viewportHeight)
Definition: Screen.h:220
void invalidate()
Definition: Screen.h:163
void presentDialog(Widget< RGB_T > &dialog)
Definition: Screen.h:366
bool isDraggableAt(int32_t x, int32_t y) const
Definition: Screen.h:280
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Screen.h:165
void setContentProvider(typename Container< RGB_T >::ChildCountFn count, typename Container< RGB_T >::ChildAtFn at)
Definition: Screen.h:91
Screen(const MaterialTheme< RGB_T > &theme)
Definition: Screen.h:79
void addWidget(Widget< RGB_T > &widget)
Definition: Screen.h:83
bool update(uint32_t nowMs) override
Definition: Screen.h:267
const MaterialTheme< RGB_T > & theme() const
Definition: Screen.h:125
bool isDialogPresented() const
Definition: Screen.h:378
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: Screen.h:110
void handleGesture(const tinygpu::GestureEvent &event)
Wire this to GestureDetector::onGesture.
Definition: Screen.h:288
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30
int32_t bottom() const
Definition: Bounds.h:31
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50