From 129716e28f3bc0d254f1dacd412f81d66b9b4fd1 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 11 Dec 2016 15:58:31 -0500 Subject: [PATCH] Draw cursor again --- src/core/Buffer.h | 4 ++++ src/gui/BufferPane.cc | 49 ++++++++++++++++++++++++++----------------- src/gui/BufferPane.h | 2 +- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/core/Buffer.h b/src/core/Buffer.h index 572b11f..46d4773 100644 --- a/src/core/Buffer.h +++ b/src/core/Buffer.h @@ -75,6 +75,10 @@ public: { return m_iterator < other.m_iterator; } + bool operator==(const Cursor & other) const + { + return (m_line == other.m_line) && (m_column == other.m_column); + } uint32_t operator*() const { return *m_iterator; } uint8_t * address() const { return m_iterator.address(); } bool valid() const { return m_iterator.valid(); } diff --git a/src/gui/BufferPane.cc b/src/gui/BufferPane.cc index 7e545cb..75fa917 100644 --- a/src/gui/BufferPane.cc +++ b/src/gui/BufferPane.cc @@ -56,25 +56,34 @@ int BufferPane::screen_rows_above_cursor(int stop_at) void BufferPane::draw() { - update_cursor_row(); - int screen_row = m_cursor_row - m_cursor->column() / m_columns; - Buffer::Cursor iter_cursor = *m_cursor; - iter_cursor.go_start_of_line(); - while ((screen_row > 0) && iter_cursor.go_up(0u)) + if (m_cursor->valid()) { - Buffer::Cursor cursor2 = iter_cursor; - cursor2.go_end_of_line(false); - screen_row -= (1 + cursor2.column() / m_columns); - } - while (screen_row < m_rows) - { - draw_buffer_line(screen_row, iter_cursor); - iter_cursor.go_end_of_line(false); - screen_row += (1 + iter_cursor.column() / m_columns); - if (!iter_cursor.go_down(0u)) + update_cursor_row(); + int screen_row = m_cursor_row - m_cursor->column() / m_columns; + Buffer::Cursor iter_cursor = *m_cursor; + iter_cursor.go_start_of_line(); + while ((screen_row > 0) && iter_cursor.go_up(0u)) { - break; + Buffer::Cursor cursor2 = iter_cursor; + cursor2.go_end_of_line(false); + screen_row -= (1 + cursor2.column() / m_columns); } + while (screen_row < m_rows) + { + draw_buffer_line(screen_row, iter_cursor); + iter_cursor.go_end_of_line(false); + screen_row += (1 + iter_cursor.column() / m_columns); + if (!iter_cursor.go_down(0u)) + { + break; + } + } + } + else + { + int x, y; + colrow_to_xy(0, 0, &x, &y); + draw_cursor(x, y, false); } } @@ -91,6 +100,10 @@ void BufferPane::draw_buffer_line(int screen_row, const Buffer::Cursor & cursor) int draw_column = iter_cursor.column() % m_columns; int x, y; colrow_to_xy(draw_column, draw_row, &x, &y); + if (iter_cursor == *m_cursor) + { + draw_cursor(x, y, false); + } uint32_t c = *iter_cursor; if ((c != '\t') && (c != ' ')) { @@ -107,10 +120,8 @@ void BufferPane::draw_buffer_character(int screen_column, int screen_row, uint32 m_gl->draw_character(win_x(x), win_y(y), character, *m_font); } -void BufferPane::draw_cursor(int screen_column, int screen_row, bool insert_mode) +void BufferPane::draw_cursor(int x, int y, bool insert_mode) { - int x, y; - colrow_to_xy(screen_column, screen_row, &x, &y); int width = insert_mode ? 1 : m_font->get_advance(); int height = m_font->get_line_height(); m_gl->draw_rect(win_x(x), win_y(y), width, height, 1.0, 0.2, 1.0, 1.0); diff --git a/src/gui/BufferPane.h b/src/gui/BufferPane.h index 2d2d926..a5233a0 100644 --- a/src/gui/BufferPane.h +++ b/src/gui/BufferPane.h @@ -27,7 +27,7 @@ protected: void update_cursor_row(); void draw_buffer_line(int screen_row, const Buffer::Cursor & cursor); void draw_buffer_character(int screen_column, int screen_row, uint32_t character); - void draw_cursor(int screen_column, int screen_row, bool insert_mode); + void draw_cursor(int x, int y, bool insert_mode); void colrow_to_xy(int col, int row, int * x, int * y); std::shared_ptr m_buffer;