69 lines
2.0 KiB
C++
69 lines
2.0 KiB
C++
#include <cstdio>
|
|
#include <SDL3/SDL.h>
|
|
#include <GL/glew.h>
|
|
|
|
#include "gfx/txt_renderer.h"
|
|
#include "terminal.h"
|
|
|
|
Terminal::Terminal(void) {
|
|
/* Placeholder welcome message to history. */
|
|
_history.push_back("Welcome to Bettola");
|
|
}
|
|
|
|
void Terminal::_on_ret_press(void) {
|
|
/* Add the command to history. */
|
|
_history.push_back("> " + _input_buffer);
|
|
|
|
/* For now, print the commmand clear buffer.
|
|
* TODO: Parse and execute commands.
|
|
*/
|
|
printf("Command entered: %s\n", _input_buffer.c_str());
|
|
|
|
if(_input_buffer == "clear") {
|
|
_history.clear();
|
|
} else if(_input_buffer == "help") {
|
|
_history.push_back("Commands: help, clear");
|
|
}
|
|
|
|
_input_buffer.clear();
|
|
}
|
|
|
|
void Terminal::handle_input(SDL_Event* event) {
|
|
if(event->type == SDL_EVENT_TEXT_INPUT) {
|
|
/* Append chars to the input buffer. */
|
|
_input_buffer += event->text.text;
|
|
} else if(event->type == SDL_EVENT_KEY_DOWN) {
|
|
/* Handle special keys. */
|
|
if(event->key.key == SDLK_BACKSPACE && _input_buffer.length() > 0) {
|
|
_input_buffer.pop_back();
|
|
} else if(event->key.key == SDLK_RETURN) {
|
|
_on_ret_press();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Terminal::render(TextRenderer* renderer, int x, int y, int width, int height) {
|
|
float white[] = { 1.0f, 1.0f, 1.0f };
|
|
float green[] = { 0.2f, 1.0f, 0.2f };
|
|
float line_height = 20.0f;
|
|
float padding = 5.0f;
|
|
|
|
/* Enable scissor test to clip rendering to the window content area. */
|
|
glEnable(GL_SCISSOR_TEST);
|
|
glScissor(x, y, width, height);
|
|
|
|
/* Draw history. */
|
|
for(size_t i = 0; i < _history.size(); ++i) {
|
|
float y_pos = (y+height) - padding - line_height - (i*line_height);
|
|
renderer->render_text(_history[i].c_str(), x+padding, y_pos, 1.0f, white);
|
|
}
|
|
|
|
/* Draw current input line. */
|
|
std::string prompt = "> " + _input_buffer;
|
|
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);
|
|
|
|
/* Disable scissor test. */
|
|
glDisable(GL_SCISSOR_TEST);
|
|
}
|