[Add] Blinking terminal cursor.
This commit is contained in:
		
							parent
							
								
									2d79aba17a
								
							
						
					
					
						commit
						846b8595f4
					
				@ -75,6 +75,10 @@ int main(int argc, char** argv) {
 | 
			
		||||
  Desktop* desktop = new Desktop();
 | 
			
		||||
  desktop->add_window(test_window);
 | 
			
		||||
 | 
			
		||||
  /* timer for cursor blink. */
 | 
			
		||||
  Uint32 last_blink_time = 0;
 | 
			
		||||
  bool show_cursor = true;
 | 
			
		||||
 | 
			
		||||
  bool running = true;
 | 
			
		||||
  while(running) {
 | 
			
		||||
    /* Event handling. */
 | 
			
		||||
@ -84,11 +88,17 @@ int main(int argc, char** argv) {
 | 
			
		||||
      desktop->handle_event(&event);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Uint32 current_time = SDL_GetTicks();
 | 
			
		||||
    if(current_time - last_blink_time > 500) { /* Every 500ms. */
 | 
			
		||||
      show_cursor = !show_cursor;
 | 
			
		||||
      last_blink_time = current_time;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Clear screen. */
 | 
			
		||||
    glClearColor(0.1f, 0.1f, 0.1, 1.0f);
 | 
			
		||||
    glClear(GL_COLOR_BUFFER_BIT);
 | 
			
		||||
 | 
			
		||||
    desktop->render(shape_renderer_instance, txt_render_instance, SCREEN_HEIGHT);
 | 
			
		||||
    desktop->render(shape_renderer_instance, txt_render_instance, SCREEN_HEIGHT, show_cursor);
 | 
			
		||||
 | 
			
		||||
    /* It's really odd to call it SwapWindow now, rather than SwapBuffer. */
 | 
			
		||||
    SDL_GL_SwapWindow(window);
 | 
			
		||||
 | 
			
		||||
@ -42,7 +42,8 @@ void Terminal::handle_input(SDL_Event* event) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Terminal::render(TextRenderer* renderer, int x, int y, int width, int height) {
 | 
			
		||||
void Terminal::render(TextRenderer* renderer, int x, int y, int width, int height,
 | 
			
		||||
                      bool show_cursor) {
 | 
			
		||||
  float white[]     = { 1.0f, 1.0f, 1.0f };
 | 
			
		||||
  float green[]     = { 0.2f, 1.0f, 0.2f };
 | 
			
		||||
  float line_height = 20.0f;
 | 
			
		||||
@ -59,9 +60,13 @@ void Terminal::render(TextRenderer* renderer, int x, int y, int width, int heigh
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* Draw current input line. */
 | 
			
		||||
  std::string prompt = "> " + _input_buffer;
 | 
			
		||||
  std::string prompt = "> ";
 | 
			
		||||
  std::string line_to_render = prompt + _input_buffer;
 | 
			
		||||
  if(show_cursor) {
 | 
			
		||||
    line_to_render += "_";
 | 
			
		||||
  }
 | 
			
		||||
  float prompt_y_pos = (y+height) - padding - line_height - (_history.size()*line_height);
 | 
			
		||||
  renderer->render_text(prompt.c_str(), x+padding, prompt_y_pos, 1.0f, green);
 | 
			
		||||
  renderer->render_text(line_to_render.c_str(), x+padding, prompt_y_pos, 1.0f, green);
 | 
			
		||||
 | 
			
		||||
  /* Disable scissor test. */
 | 
			
		||||
  glDisable(GL_SCISSOR_TEST);
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ public:
 | 
			
		||||
  Terminal(void);
 | 
			
		||||
 | 
			
		||||
  void handle_input(SDL_Event* event);
 | 
			
		||||
  void render(TextRenderer* renderer, int x, int y, int width, int height);
 | 
			
		||||
  void render(TextRenderer* renderer, int x, int y, int width, int height, bool show_cursor);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
  void _on_ret_press(void);
 | 
			
		||||
 | 
			
		||||
@ -52,8 +52,8 @@ void Desktop::handle_event(SDL_Event* event) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Desktop::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
 | 
			
		||||
                     int screen_height) {
 | 
			
		||||
                     int screen_height, bool show_cursor) {
 | 
			
		||||
  for(auto win: _windows) {
 | 
			
		||||
    win->render(shape_renderer, txt_renderer, screen_height);
 | 
			
		||||
    win->render(shape_renderer, txt_renderer, screen_height, show_cursor);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -14,7 +14,8 @@ public:
 | 
			
		||||
 | 
			
		||||
  void add_window(UIWindow* window);
 | 
			
		||||
  void handle_event(SDL_Event* event);
 | 
			
		||||
  void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height);
 | 
			
		||||
  void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height,
 | 
			
		||||
              bool show_cursor);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
  std::vector<UIWindow*> _windows;
 | 
			
		||||
 | 
			
		||||
@ -32,7 +32,7 @@ bool UIWindow::is_point_inside(int x, int y) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
 | 
			
		||||
                      int screen_height) {
 | 
			
		||||
                      int screen_height, bool show_cursor) {
 | 
			
		||||
  int title_bar_height = 30;
 | 
			
		||||
 | 
			
		||||
  /* Define colours. */
 | 
			
		||||
@ -63,7 +63,7 @@ void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
 | 
			
		||||
  if(_content) {
 | 
			
		||||
    int content_y_gl = y_gl;
 | 
			
		||||
    int content_height_gl = _height - title_bar_height;
 | 
			
		||||
    _content->render(txt_renderer, _x, content_y_gl, _width, content_height_gl);
 | 
			
		||||
    _content->render(txt_renderer, _x, content_y_gl, _width, content_height_gl, show_cursor);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,8 @@ class UIWindow {
 | 
			
		||||
public:
 | 
			
		||||
  UIWindow(const char* title, int x, int y, int width, int height);
 | 
			
		||||
 | 
			
		||||
  void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height);
 | 
			
		||||
  void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height,
 | 
			
		||||
              bool show_cursor);
 | 
			
		||||
  void handle_event(SDL_Event* event);
 | 
			
		||||
  void set_focused(bool focused);
 | 
			
		||||
  bool is_point_inside(int x, int y);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user