draw cursor crosshairs

This commit is contained in:
Josh Holtrop 2016-09-13 21:25:10 -04:00
parent cde71ac70a
commit 980ec3a295
2 changed files with 43 additions and 3 deletions

View File

@ -359,18 +359,34 @@ void Window::draw_text()
auto start_position = calculate_start_position();
int row = start_position.first;
PieceTable::Cursor cursor = start_position.second;
int last_vertical_crosshair_row = -1;
int crosshair_row_offset = m_cursor->column / m_columns;
int crosshair_screen_column = m_cursor->column % m_columns;
for (;;)
{
if ((last_vertical_crosshair_row < row) &&
(cursor.column >= m_cursor->column))
{
int crosshair_screen_row = row + crosshair_row_offset;
if ((crosshair_screen_row >= 0) &&
(crosshair_screen_row < m_rows))
{
draw_crosshair(crosshair_screen_column, crosshair_screen_row);
}
last_vertical_crosshair_row = row;
}
int row_offset = cursor.column / m_columns;
int screen_row = row + row_offset;
if (screen_row >= m_rows)
break;
int screen_column = cursor.column % m_columns;
if (cursor == *m_cursor)
draw_cursor(screen_column, screen_row);
if (screen_row >= 0)
{
if (cursor.line == m_cursor->line)
draw_crosshair(screen_column, screen_row);
if (cursor == *m_cursor)
draw_cursor(screen_column, screen_row);
uint32_t character = *cursor;
if ((character != 0xFFFFFFFFu) &&
(character != ' ') &&
@ -379,7 +395,17 @@ void Window::draw_text()
}
if (!cursor.go_right(1))
{
if (!cursor.go_down(1, 0))
bool last_row = !cursor.go_down(1, 0);
if (last_vertical_crosshair_row < row)
{
int crosshair_screen_row = row + crosshair_row_offset;
if (crosshair_screen_row == screen_row)
{
draw_crosshair(crosshair_screen_column, crosshair_screen_row);
}
last_vertical_crosshair_row = row;
}
if (last_row)
break;
row = screen_row + 1;
}
@ -435,3 +461,16 @@ void Window::draw_cursor(int screen_column, int screen_row)
m_shaders.flat->set_position(x, y);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
void Window::draw_crosshair(int screen_column, int screen_row)
{
int advance = m_font.get_advance();
int line_height = m_font.get_line_height();
int x = screen_column * advance;
int y = m_height - (screen_row + 1) * line_height;
m_cursor_array->bind();
m_shaders.flat->use();
m_shaders.flat->set_color(0.1, 0.1, 0.1, 1.0);
m_shaders.flat->set_position(x, y);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}

View File

@ -29,6 +29,7 @@ protected:
void resize();
void redraw();
void draw_cursor(int screen_column, int screen_row);
void draw_crosshair(int screen_column, int screen_row);
void handle_event(SDL_Event & event);
void handle_key(uint32_t scancode, uint32_t mod);
void cursor_move(int which);