bettola/client/src/ui/editor.cpp

102 lines
3.1 KiB
C++

#include "editor.h"
#include <memory>
#include "text_view.h"
#include "gfx/types.h"
#include "ui/window_action.h"
Editor::Editor(void)
: _should_close(false), _pending_action({ActionType::NONE}),
_filename("untitled.txt") {
_view = std::make_unique<TextView>(&_buffer, true, true, true);
_menu_bar = std::make_unique<MenuBar>(25);
_menu_bar->add_menu("File");
_menu_bar->add_menu_item("File", "Save", [this]() {
_pending_action = {ActionType::WRITE_FILE, _filename, _buffer.get_text()};
});
}
Editor::Editor(const std::string& filename)
: _should_close(false), _pending_action({ActionType::NONE}),
_filename(filename) {
_view = std::make_unique<TextView>(&_buffer, true, true, true);
_menu_bar = std::make_unique<MenuBar>(25);
_menu_bar->add_menu("File");
_menu_bar->add_menu_item("File", "Save", [this]() {
_pending_action = {ActionType::WRITE_FILE, _filename, _buffer.get_text()};
});
}
Editor::~Editor(void) {}
void Editor::update(void) {
/* Nothing to do yet. */
}
void Editor::handle_input(SDL_Event* event, int window_x, int window_y, int window_gl_y) {
if(!event) return;
_menu_bar->handle_event(event, window_x, window_y);
/* We don't care about the return val here. RET is just newline. */
if(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_S &&
(event->key.mod & SDL_KMOD_CTRL)) {
/* C-S pressed, create a save action. */
_pending_action = { ActionType::WRITE_FILE, _filename, _buffer.get_text() };
} else {
_view->handle_event(event);
}
}
void Editor::render(const RenderContext& context, int x, int y_screen, int y_gl,
int width, int height) {
int menu_bar_height = _menu_bar->get_height();
int content_y = y_screen + menu_bar_height;
int content_height = height - menu_bar_height;
/* Pass 1: Main Bar Background.*/
context.ui_renderer->begin_shapes();
_menu_bar->render_bar_bg(context.ui_renderer, x, y_screen, width);
context.ui_renderer->flush_shapes();
/* Pass 2: Main text view. */
context.ui_renderer->begin_text();
_view->render_text_content(context.ui_renderer, _theme, x, content_y, width, content_height);
context.ui_renderer->flush_text();
/* Pass 3: Editor cursor. */
if(context.show_cursor) {
context.ui_renderer->begin_shapes();
_view->render_cursor(context.ui_renderer, _theme, x, content_y, width, content_height);
context.ui_renderer->flush_shapes();
}
/* Pass 4: Menu bar text and dropdown. */
context.ui_renderer->begin_text();
_menu_bar->render_bar_text(context.ui_renderer, x, y_screen, width);
_menu_bar->render_dropdown(context.ui_renderer, x, y_screen, width);
context.ui_renderer->flush_text();
}
void Editor::scroll(int amount, int content_height) {
_view->scroll(amount, content_height);
}
bool Editor::should_close(void) {
return _should_close;
}
void Editor::set_buffer_content(const std::string& content) {
_buffer.set_text(content);
}
WindowAction Editor::get_pending_action(void) {
WindowAction action = _pending_action;
_pending_action.type = ActionType::NONE; /* Clear action. */
return action;
}
const std::string& Editor::get_filename(void) const {
return _filename;
}