From b27f3d89029ee6262cca0321fb1ac5b489976bbe Mon Sep 17 00:00:00 2001 From: niansa Date: Sat, 8 Apr 2023 14:21:30 +0200 Subject: [PATCH] Added cancellation and scrolling --- Client.cpp | 13 +++++++++++-- NDSUI.cpp | 39 ++++++++++++++++++++++++++++----------- NDSUI.hpp | 3 +++ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/Client.cpp b/Client.cpp index b7eb3dc..8541099 100644 --- a/Client.cpp +++ b/Client.cpp @@ -48,8 +48,6 @@ Client::Client(const std::string& addr, unsigned port, AsyncManager& asyncManage } basiccoro::AwaitableTask Client::ask(std::string_view prompt, const std::function (unsigned progress)>& on_progress, const std::function (std::string_view token)>& on_token) { - std::string fres; - // Send prompt length uint8_t len = prompt.length(); if (co_await connection->writeObject(len, true) == AsyncResult::Error) { @@ -98,6 +96,17 @@ basiccoro::AwaitableTask Client::ask(std::string_view prompt, const // Run on_token callback co_await on_token(token); + + // Pass cancellation request + int keys = keysCurrent(); + if (keys & KEY_START) { + co_await connection->writeObject(0xAB); // Abort + co_return AsyncResult::Success; + } else if (keys & KEY_SELECT) { + co_await connection->writeObject(0xCA); // Cancel + } else { + co_await connection->writeObject(0xC0); // Continue + } } // No error diff --git a/NDSUI.cpp b/NDSUI.cpp index 07c7a12..db4a246 100644 --- a/NDSUI.cpp +++ b/NDSUI.cpp @@ -46,7 +46,7 @@ void NDSUI::renderMessageLog() { // Log messages renderer auto renderLine = [this, y = int(boxDims.bottomRight.y - FONT_PADDING - FONT_HEIGHT), boxDims] (std::string_view line) mutable { // Skip empty lines - if (line.empty()) return; + if (line.empty()) return true; // Check if line is bold bool bold = line[0] == '\t'; if (bold) { @@ -56,20 +56,21 @@ void NDSUI::renderMessageLog() { print(line, {boxDims.topLeft.x + FONT_PADDING, unsigned(y)}, RGB15(28, 28, 28), bold); // Go to next line y -= FONT_PADDING * LOG_LINE_PADDING + FONT_HEIGHT; - if (y < 0) { - throw std::exception(); // Stop rendering any more lines - } + // Stop rendering any more lines + return y >= 0; }; // Display line by line - try { - for (auto message = messages.rbegin(); message != messages.rend(); message++) { - const auto& lines = message->getLines(); - for (auto line = lines.rbegin(); line != lines.rend(); line++) { - renderLine(*line); - } + unsigned lineCount = 0; + for (auto message = messages.rbegin(); message != messages.rend(); message++) { + const auto& lines = message->getLines(); + for (auto line = lines.rbegin(); line != lines.rend(); line++) { + if (lineCount >= scrollOff) + if (!renderLine(*line)) + return; + lineCount++; } - } catch (...) {} + } } consteval @@ -145,6 +146,7 @@ void NDSUI::handleInput(char c) { addLogMessage(std::move(msg)); userInputHandler->set(inputLineBuf.data()); resetInputLine(); + scrollOff = 0; //userInputHandler = nullptr; } } break; @@ -158,6 +160,19 @@ void NDSUI::handleInput(char c) { } } +void NDSUI::scanButtons() { + scanKeys(); + int keys = keysDownRepeat(); + // Scrolling + if (keys & KEY_DOWN) { + if (scrollOff != 0) { + scrollOff--; + } + } else if (keys & KEY_UP) { + scrollOff++; + } +} + NDSUI::NDSUI() { resetInputLine(); glScreen2D(); @@ -168,6 +183,8 @@ NDSUI::NDSUI() { void NDSUI::run() { currentInstance = this; + // Update buttons + scanButtons(); // Update keyboard keyboardUpdate(); // Draw diff --git a/NDSUI.hpp b/NDSUI.hpp index 1d6bfcb..34ac05c 100644 --- a/NDSUI.hpp +++ b/NDSUI.hpp @@ -61,6 +61,7 @@ class NDSUI { unsigned inputLineBufHead = 0; std::vector messages; std::unique_ptr> userInputHandler = nullptr; + unsigned scrollOff = 0; static void kbCallback(int key); @@ -82,6 +83,8 @@ class NDSUI { void handleInput(char); + void scanButtons(); + void resetInputLine() { inputLineBufHead = 0; inputLineBuf[0] = '\0';