RIT VEXU Core API
Loading...
Searching...
No Matches
screen_book.h
1#pragma once
2
3#include <initializer_list>
4#include <vector>
5
6#include "vex/vex.h"
7#include "core/screen/screen_pages.h"
8#include "core/screen/screen_controller.h"
9
10class ScreenBook {
11public:
12
13 ScreenBook(vex::brain::lcd screen, std::initializer_list<ScreenPage *> pages);
14 ScreenBook(vex::brain::lcd screen, std::vector<ScreenPage *> pages);
15 ScreenBook() = default;
16
17 inline std::function<void()> handle() {
18 return [this]() {
19 if(this->pages.empty()) {
20 this->screen.clearScreen(vex::color::red);
21 return;
22 }
23
24 ScreenPage *front_page = this->pages[this->index];
25 std::optional<Point2d> pressing;
26
27 if((pressing = ScreenController::get_press_pos()).has_value()) {
28 this->x_press = pressing->x();
29 this->y_press = pressing->y();
30 }
31 bool just_pressed = pressing.has_value() && !this->was_pressed;
32
33 if (just_pressed && this->x_press < 40) {
34 this->index--;
35 if (this->index < 0) {
36 this->index += this->pages.size();
37 }
38 }
39 if (just_pressed && this->x_press > 440) {
40 this->index++;
41 this->index %= this->pages.size();
42 }
43
44 // Update all pages
45 for (auto page : this->pages) {
46 if (page == front_page) {
47 page->update(this->was_pressed, this->x_press, this->y_press);
48 } else {
49 page->update(false, 0, 0);
50 }
51 }
52
53 // Draw First Page
54 int frame = ScreenController::get_frame_count();
55 if (frame % 2 == 0) {
56 this->screen.clearScreen(vex::color::black);
57 this->screen.setPenColor("#FFFFFF");
58 this->screen.setFillColor("#000000");
59 front_page->draw(this->screen, false, frame / 5);
60
61 // Draw side boxes
62 this->screen.setPenColor("#202020");
63 this->screen.setFillColor("#202020");
64 this->screen.drawRectangle(0, 0, 40, 240);
65 this->screen.drawRectangle(440, 0, 40, 240);
66 this->screen.setPenColor("#FFFFFF");
67 // left arrow
68 this->screen.drawLine(30, 100, 15, 120);
69 this->screen.drawLine(30, 140, 15, 120);
70 // right arrow
71 this->screen.drawLine(450, 100, 465, 120);
72 this->screen.drawLine(450, 140, 465, 120);
73 }
74
75 this->was_pressed = pressing.has_value();
76 };
77 }
78
86 static inline std::function<void()> pre_initialize(vex::brain& brain, Initializer& initializer, ScreenBook* page, std::function<void()> o = nullptr) {
87 return [&, o, page]() {
88 if(o) o();
89
90 std::vector<ScreenPage*> pages; size_t initializations = 0;
91 do {
92 pages.push_back(new InitializerPage(initializer, initializations));
93 initializations += 8;
94 } while(initializations < initializer.initialization_count());
95
96 *page = ScreenBook(brain.Screen, pages);
97 ScreenController::set(page->handle());
98 };
99 }
100
101private:
102
103 bool was_pressed;
104 int index;
105 int x_press;
106 int y_press;
107
108 std::vector<ScreenPage *> pages;
109 vex::brain::lcd screen;
110};
InitializerPage(const Initializer &initializer, size_t starting_index=0)
Creates an InitializerPage.
Definition screen_pages.cpp:313
const std::size_t initialization_count() const
Definition initializer.cpp:48
virtual void draw(vex::brain::lcd &screen, bool first_draw, unsigned int frame_number)
draw stored data to the screen (runs at 10 hz and only runs if this page is in front)