[Add] Multi-line rendering in TextView.
This commit is contained in:
parent
d34c13ef8b
commit
324cc795f0
@ -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. */
|
||||
|
||||
@ -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,20 +51,35 @@ 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) {
|
||||
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.
|
||||
@ -72,6 +91,7 @@ void TextView::render(TextRenderer* renderer, int x, int y, bool show_cursor) {
|
||||
line.insert(cursor_pos.col, 1, '_');
|
||||
}
|
||||
}
|
||||
|
||||
renderer->render_text(line.c_str(), x, y, 1.0f, text_color);
|
||||
renderer->render_text(line.c_str(), x, current_y, 1.0f, text_color);
|
||||
current_y -= line_height;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user