1
0
Fork 0
mirror of https://gitlab.com/niansa/llama_nds.git synced 2025-03-06 20:53:28 +01:00

Added cancellation and scrolling

This commit is contained in:
niansa 2023-04-08 14:21:30 +02:00
parent b16c140e70
commit b27f3d8902
3 changed files with 42 additions and 13 deletions

View file

@ -48,8 +48,6 @@ Client::Client(const std::string& addr, unsigned port, AsyncManager& asyncManage
} }
basiccoro::AwaitableTask<AsyncResult> Client::ask(std::string_view prompt, const std::function<basiccoro::AwaitableTask<void> (unsigned progress)>& on_progress, const std::function<basiccoro::AwaitableTask<void> (std::string_view token)>& on_token) { basiccoro::AwaitableTask<AsyncResult> Client::ask(std::string_view prompt, const std::function<basiccoro::AwaitableTask<void> (unsigned progress)>& on_progress, const std::function<basiccoro::AwaitableTask<void> (std::string_view token)>& on_token) {
std::string fres;
// Send prompt length // Send prompt length
uint8_t len = prompt.length(); uint8_t len = prompt.length();
if (co_await connection->writeObject(len, true) == AsyncResult::Error) { if (co_await connection->writeObject(len, true) == AsyncResult::Error) {
@ -98,6 +96,17 @@ basiccoro::AwaitableTask<AsyncResult> Client::ask(std::string_view prompt, const
// Run on_token callback // Run on_token callback
co_await on_token(token); co_await on_token(token);
// Pass cancellation request
int keys = keysCurrent();
if (keys & KEY_START) {
co_await connection->writeObject<uint8_t>(0xAB); // Abort
co_return AsyncResult::Success;
} else if (keys & KEY_SELECT) {
co_await connection->writeObject<uint8_t>(0xCA); // Cancel
} else {
co_await connection->writeObject<uint8_t>(0xC0); // Continue
}
} }
// No error // No error

View file

@ -46,7 +46,7 @@ void NDSUI::renderMessageLog() {
// Log messages renderer // Log messages renderer
auto renderLine = [this, y = int(boxDims.bottomRight.y - FONT_PADDING - FONT_HEIGHT), boxDims] (std::string_view line) mutable { auto renderLine = [this, y = int(boxDims.bottomRight.y - FONT_PADDING - FONT_HEIGHT), boxDims] (std::string_view line) mutable {
// Skip empty lines // Skip empty lines
if (line.empty()) return; if (line.empty()) return true;
// Check if line is bold // Check if line is bold
bool bold = line[0] == '\t'; bool bold = line[0] == '\t';
if (bold) { if (bold) {
@ -56,20 +56,21 @@ void NDSUI::renderMessageLog() {
print(line, {boxDims.topLeft.x + FONT_PADDING, unsigned(y)}, RGB15(28, 28, 28), bold); print(line, {boxDims.topLeft.x + FONT_PADDING, unsigned(y)}, RGB15(28, 28, 28), bold);
// Go to next line // Go to next line
y -= FONT_PADDING * LOG_LINE_PADDING + FONT_HEIGHT; y -= FONT_PADDING * LOG_LINE_PADDING + FONT_HEIGHT;
if (y < 0) { // Stop rendering any more lines
throw std::exception(); // Stop rendering any more lines return y >= 0;
}
}; };
// Display line by line // Display line by line
try { unsigned lineCount = 0;
for (auto message = messages.rbegin(); message != messages.rend(); message++) { for (auto message = messages.rbegin(); message != messages.rend(); message++) {
const auto& lines = message->getLines(); const auto& lines = message->getLines();
for (auto line = lines.rbegin(); line != lines.rend(); line++) { for (auto line = lines.rbegin(); line != lines.rend(); line++) {
renderLine(*line); if (lineCount >= scrollOff)
} if (!renderLine(*line))
return;
lineCount++;
} }
} catch (...) {} }
} }
consteval consteval
@ -145,6 +146,7 @@ void NDSUI::handleInput(char c) {
addLogMessage(std::move(msg)); addLogMessage(std::move(msg));
userInputHandler->set(inputLineBuf.data()); userInputHandler->set(inputLineBuf.data());
resetInputLine(); resetInputLine();
scrollOff = 0;
//userInputHandler = nullptr; //userInputHandler = nullptr;
} }
} break; } 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() { NDSUI::NDSUI() {
resetInputLine(); resetInputLine();
glScreen2D(); glScreen2D();
@ -168,6 +183,8 @@ NDSUI::NDSUI() {
void NDSUI::run() { void NDSUI::run() {
currentInstance = this; currentInstance = this;
// Update buttons
scanButtons();
// Update keyboard // Update keyboard
keyboardUpdate(); keyboardUpdate();
// Draw // Draw

View file

@ -61,6 +61,7 @@ class NDSUI {
unsigned inputLineBufHead = 0; unsigned inputLineBufHead = 0;
std::vector<LineWrappedString> messages; std::vector<LineWrappedString> messages;
std::unique_ptr<basiccoro::SingleEvent<std::string>> userInputHandler = nullptr; std::unique_ptr<basiccoro::SingleEvent<std::string>> userInputHandler = nullptr;
unsigned scrollOff = 0;
static void kbCallback(int key); static void kbCallback(int key);
@ -82,6 +83,8 @@ class NDSUI {
void handleInput(char); void handleInput(char);
void scanButtons();
void resetInputLine() { void resetInputLine() {
inputLineBufHead = 0; inputLineBufHead = 0;
inputLineBuf[0] = '\0'; inputLineBuf[0] = '\0';