[Add] Multi-line rendering in TextView.

This commit is contained in:
Ritchie Cunningham 2025-09-29 21:26:35 +01:00
parent d34c13ef8b
commit 324cc795f0
3 changed files with 42 additions and 19 deletions

View File

@ -46,6 +46,7 @@ bool Terminal::close(void) { return _should_close; }
void Terminal::_on_ret_press(void) {
std::string command = _input_buffer.get_line(0);
_input_buffer.newline(); /* Add newline to buffer for histroy. */
_input_buffer.clear();
/* Add the command to history. */

View File

@ -4,7 +4,7 @@
#include "gfx/types.h"
#include "ui/text_buffer.h"
TextView::TextView(TextBuffer* buffer) : _buffer(buffer) {}
TextView::TextView(TextBuffer* buffer) : _buffer(buffer), _scroll_offset(0) {}
TextView::~TextView(void) {}
@ -19,7 +19,11 @@ bool TextView::handle_event(SDL_Event* event) {
_buffer->backspace();
break;
case SDLK_RETURN:
/* Instead of adding newline, signal that input was submitted. */
/*
* For editor, we want a real newline.
* For terminal, we just want to submit. No newline.
* We'll return true and let the parent component do the do'ing.
*/
return true;
break;
case SDLK_LEFT:
@ -47,31 +51,47 @@ bool TextView::handle_event(SDL_Event* event) {
return false;
}
void TextView::scroll(int amount, int content_height) {
_scroll_offset += amount;
if(_scroll_offset < 0) {
_scroll_offset = 0;
}
float line_height = 20.0f;
int visible_lines = content_height / line_height;
int max_scroll = _buffer->get_line_count() - visible_lines;
if(max_scroll < 0) max_scroll = 0;
if(_scroll_offset > max_scroll) {
_scroll_offset = max_scroll;
}
}
void TextView::render(TextRenderer* renderer, int x, int y, bool show_cursor) {
if(!_buffer) return;
const Color text_color = { 1.0f, 1.0f, 1.0f };
float line_height = 20.0f; /* TODO: Get font metrics? */
float padding = 5.0f;
/*
* Just render first line (for term input) for now.
* The code editor would need to iterate through _buffer->get_line_count()
*/
std::string line = _buffer->get_line(0);
Point cursor_pos = _buffer->get_cursor_pos();
float current_y = y;
if(show_cursor) {
/*
* This hacky. we should calculate the text width
* up to the cursor pos to draw correctly.
* For now, just append an underscore.
*/
if(cursor_pos.col == line.length()) {
line += "_";
} else {
line.insert(cursor_pos.col, 1, '_');
for(size_t i = _scroll_offset; i < _buffer->get_line_count(); ++i) {
std::string line = _buffer->get_line(i);
if(show_cursor && (int)i == cursor_pos.row) {
/*
* This hacky. we should calculate the text width
* up to the cursor pos to draw correctly.
* For now, just append an underscore.
*/
if(cursor_pos.col == line.length()) {
line += "_";
} else {
line.insert(cursor_pos.col, 1, '_');
}
}
renderer->render_text(line.c_str(), x, current_y, 1.0f, text_color);
current_y -= line_height;
}
renderer->render_text(line.c_str(), x, y, 1.0f, text_color);
}

View File

@ -13,7 +13,9 @@ public:
bool handle_event(SDL_Event* event);
void render(TextRenderer* renderer, int x, int y, bool show_cursor);
void scroll(int amount, int content_height);
private:
TextBuffer* _buffer;
int _scroll_offset;
};