PocketPutr/Desktop_Test/tft_handler.cpp
2026-01-18 20:08:39 +01:00

92 lines
2.8 KiB
C++

#include "GLOBALS.h"
#include "tft_handler.h"
#include <Arduino.h>
void TFT_Handler::init(DISPLAY_STATE* display_state) {
_display_state = display_state;
tft.setColorDepth(16);
tft.init();
tft.setRotation(3);
//tft.fillScreen(0x0000);
sprite.setColorDepth(16);
sprite.setPsram(true);
sprite.createSprite(480, 320);
sprite.fillScreen(0x0000);
}
void TFT_Handler::draw_box(short x, short y, short size_x, short size_y, short color) {
sprite.fillRect(x, y, size_x, size_y, color);
}
void TFT_Handler::draw_rect(short x, short y, short size_x, short size_y, short thickness, short color) {
sprite.drawRect(x, y, size_x, size_y, color);
}
void TFT_Handler::draw_line(short x1, short y1, short x2, short y2, short color) {
sprite.drawLine(x1, y1, x2, y2, color);
}
void TFT_Handler::draw_text(short x, short y, const std::string& str, short color, short size) {
sprite.setTextColor(color);
sprite.setTextSize(size);
sprite.drawString(str.c_str(), x, y);
}
void TFT_Handler::draw_button(short x, short y, short size_x, short size_y, short color, bool pressed, std::string str) {
if (pressed) {
sprite.fillRect(x - 2, y - 2, size_x + 2, size_y + 2, COL_BLACK);
sprite.fillRect(x - 1, y - 1, size_x + 1, size_y + 1, COL_DARK_GREY);
sprite.fillRect(x, y, size_x, size_y, color);
sprite.setTextColor(COL_BLACK);
sprite.setTextSize(1);
sprite.drawString(str.c_str(), x, y);
} else {
sprite.fillRect(x, y, size_x + 2, size_y + 2, COL_BLACK);
sprite.fillRect(x, y, size_x + 1, size_y + 1, COL_DARK_GREY);
sprite.fillRect(x, y, size_x, size_y, color);
sprite.setTextColor(COL_BLACK);
sprite.setTextSize(1);
sprite.drawString(str.c_str(), x, y);
}
}
void TFT_Handler::fill_screen(short color) {
sprite.fillScreen(color);
}
void TFT_Handler::push_sprite() {
sprite.pushSprite(0, 0);
}
void TFT_Handler::get_text_bounds(const std::string& str, short size, short& width, short& height) {
sprite.setTextSize(size);
width = sprite.textWidth(str.c_str());
height = sprite.fontHeight() * size;
}
void TFT_Handler::draw_scrollable_textbox(const ScrollableTextBox& box) {
// Draw box background
draw_box(box.x, box.y, box.width, box.height, COL_WHITE);
if (box.selected) {
draw_rect(box.x, box.y, box.width, box.height, 2, COL_BLACK);
}
// Set clipping region to prevent text from drawing outside box
sprite.setClipRect(box.x, box.y, box.width, box.height);
// Draw visible lines
for (const auto& line : box.lines) {
short render_y = box.y + line.local_y - box.scroll_offset;
// Only render if line is visible within the box
if (render_y >= box.y - box.line_height && render_y <= box.y + box.height) {
draw_text(box.x + 5, render_y, line.text, COL_BLACK, box.text_size);
}
}
// Clear clipping
sprite.clearClipRect();
}