[Add] Better scrolling/snap to botom for terminal.
This commit is contained in:
		
							parent
							
								
									bb8591a803
								
							
						
					
					
						commit
						5c52afe1f4
					
				@ -62,7 +62,7 @@ uint32_t Terminal::get_session_id(void) const {
 | 
				
			|||||||
  return _session_id;
 | 
					  return _session_id;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Terminal::_on_ret_press(void) {
 | 
					void Terminal::_on_ret_press(int content_height) {
 | 
				
			||||||
  std::string command = _input_buffer.get_line(0);
 | 
					  std::string command = _input_buffer.get_line(0);
 | 
				
			||||||
  if(!command.empty()) {
 | 
					  if(!command.empty()) {
 | 
				
			||||||
    _command_history.push_back(command);
 | 
					    _command_history.push_back(command);
 | 
				
			||||||
@ -90,6 +90,13 @@ void Terminal::_on_ret_press(void) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  _history.push_back(_prompt + "> " + command);
 | 
					  _history.push_back(_prompt + "> " + command);
 | 
				
			||||||
  _game_state->send_network_command(_session_id, command);
 | 
					  _game_state->send_network_command(_session_id, command);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /* After processing a command, always snap to the bottom. */
 | 
				
			||||||
 | 
					  float line_height = 20.0f;
 | 
				
			||||||
 | 
					  int visible_lines = content_height / line_height;
 | 
				
			||||||
 | 
					  int max_scroll = (_history.size()+1) - visible_lines;
 | 
				
			||||||
 | 
					  if(max_scroll < 0) max_scroll = 0;
 | 
				
			||||||
 | 
					  _scroll_offset = max_scroll;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Terminal::handle_input(SDL_Event* event, int window_x, int window_y, int window_gl_y,
 | 
					void Terminal::handle_input(SDL_Event* event, int window_x, int window_y, int window_gl_y,
 | 
				
			||||||
@ -115,10 +122,25 @@ void Terminal::handle_input(SDL_Event* event, int window_x, int window_y, int wi
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      default:
 | 
					      default:
 | 
				
			||||||
        if(_input_view->handle_event(event, content_height)) { _on_ret_press(); }
 | 
					      float line_height = 20.0f;
 | 
				
			||||||
 | 
					      int visible_lines = content_height / line_height;
 | 
				
			||||||
 | 
					      int max_scroll = (_history.size()+1) - visible_lines;
 | 
				
			||||||
 | 
					      if(max_scroll < 0) max_scroll = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if(_input_view->handle_event(event, content_height)) { _on_ret_press(content_height); }
 | 
				
			||||||
 | 
					      else {
 | 
				
			||||||
 | 
					        _scroll_offset = max_scroll; /* Always snap to bottom on key press. */
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  } else if(event->type == SDL_EVENT_TEXT_INPUT) {
 | 
				
			||||||
 | 
					    if(_input_view->handle_event(event, content_height)) { _on_ret_press(content_height); }
 | 
				
			||||||
 | 
					    else {
 | 
				
			||||||
 | 
					      float line_height = 20.0f;
 | 
				
			||||||
 | 
					      int visible_lines = content_height / line_height;
 | 
				
			||||||
 | 
					      int max_scroll = (_history.size()+1) - visible_lines;
 | 
				
			||||||
 | 
					      if(max_scroll < 0) max_scroll = 0;
 | 
				
			||||||
 | 
					      _scroll_offset = max_scroll; /* Always snap to bottom on text input. */
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    if(_input_view->handle_event(event, content_height)) { _on_ret_press(); }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -186,12 +208,11 @@ void Terminal::render(const RenderContext& context, int x, int y_screen, int y_g
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  context.ui_renderer->flush_text();
 | 
					  context.ui_renderer->flush_text();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /* Disable scissor test. */
 | 
					 | 
				
			||||||
  glDisable(GL_SCISSOR_TEST);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if(context.show_cursor) {
 | 
					  if(context.show_cursor) {
 | 
				
			||||||
    context.ui_renderer->begin_shapes();
 | 
					    context.ui_renderer->begin_shapes();
 | 
				
			||||||
    _input_view->render_cursor(context.ui_renderer, theme, input_x_pos, prompt_line_y, input_width, line_height);
 | 
					    _input_view->render_cursor(context.ui_renderer, theme, input_x_pos, prompt_line_y, input_width, line_height);
 | 
				
			||||||
    context.ui_renderer->flush_shapes();
 | 
					    context.ui_renderer->flush_shapes();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  /* Disable scissor test. */
 | 
				
			||||||
 | 
					  glDisable(GL_SCISSOR_TEST);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -31,7 +31,7 @@ public:
 | 
				
			|||||||
  uint32_t get_session_id(void) const;
 | 
					  uint32_t get_session_id(void) const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
  void _on_ret_press(void);
 | 
					  void _on_ret_press(int content_height);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool _should_close;
 | 
					  bool _should_close;
 | 
				
			||||||
  std::vector<std::string> _command_history;
 | 
					  std::vector<std::string> _command_history;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user