From 5c52afe1f40f542dcf259a8b2ac4e8afb4ff00a4 Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Sat, 25 Oct 2025 19:19:02 +0100 Subject: [PATCH] [Add] Better scrolling/snap to botom for terminal. --- client/src/terminal.cpp | 35 ++++++++++++++++++++++++++++------- client/src/terminal.h | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/client/src/terminal.cpp b/client/src/terminal.cpp index a02f12f..ac04b0b 100644 --- a/client/src/terminal.cpp +++ b/client/src/terminal.cpp @@ -62,7 +62,7 @@ uint32_t Terminal::get_session_id(void) const { return _session_id; } -void Terminal::_on_ret_press(void) { +void Terminal::_on_ret_press(int content_height) { std::string command = _input_buffer.get_line(0); if(!command.empty()) { _command_history.push_back(command); @@ -90,6 +90,13 @@ void Terminal::_on_ret_press(void) { _history.push_back(_prompt + "> " + command); _game_state->send_network_command(_session_id, command); + + /* After processing a command, always snap to the bottom. */ + float line_height = 20.0f; + int visible_lines = content_height / line_height; + int max_scroll = (_history.size()+1) - visible_lines; + if(max_scroll < 0) max_scroll = 0; + _scroll_offset = max_scroll; } void Terminal::handle_input(SDL_Event* event, int window_x, int window_y, int window_gl_y, @@ -115,10 +122,25 @@ void Terminal::handle_input(SDL_Event* event, int window_x, int window_y, int wi } break; default: - if(_input_view->handle_event(event, content_height)) { _on_ret_press(); } + float line_height = 20.0f; + int visible_lines = content_height / line_height; + int max_scroll = (_history.size()+1) - visible_lines; + if(max_scroll < 0) max_scroll = 0; + + if(_input_view->handle_event(event, content_height)) { _on_ret_press(content_height); } + else { + _scroll_offset = max_scroll; /* Always snap to bottom on key press. */ + } + } + } else if(event->type == SDL_EVENT_TEXT_INPUT) { + if(_input_view->handle_event(event, content_height)) { _on_ret_press(content_height); } + else { + float line_height = 20.0f; + int visible_lines = content_height / line_height; + int max_scroll = (_history.size()+1) - visible_lines; + if(max_scroll < 0) max_scroll = 0; + _scroll_offset = max_scroll; /* Always snap to bottom on text input. */ } - } else { - if(_input_view->handle_event(event, content_height)) { _on_ret_press(); } } } @@ -186,12 +208,11 @@ void Terminal::render(const RenderContext& context, int x, int y_screen, int y_g context.ui_renderer->flush_text(); - /* Disable scissor test. */ - glDisable(GL_SCISSOR_TEST); - if(context.show_cursor) { context.ui_renderer->begin_shapes(); _input_view->render_cursor(context.ui_renderer, theme, input_x_pos, prompt_line_y, input_width, line_height); context.ui_renderer->flush_shapes(); } + /* Disable scissor test. */ + glDisable(GL_SCISSOR_TEST); } diff --git a/client/src/terminal.h b/client/src/terminal.h index f841b8a..4fa8f1d 100644 --- a/client/src/terminal.h +++ b/client/src/terminal.h @@ -31,7 +31,7 @@ public: uint32_t get_session_id(void) const; private: - void _on_ret_press(void); + void _on_ret_press(int content_height); bool _should_close; std::vector _command_history;