[Add] Better scrolling/snap to botom for terminal.

This commit is contained in:
Ritchie Cunningham 2025-10-25 19:19:02 +01:00
parent bb8591a803
commit 5c52afe1f4
2 changed files with 29 additions and 8 deletions

View File

@ -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);
}

View File

@ -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<std::string> _command_history;