From 70dbffca112f7deb26a470c12ae56d11137aae86 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 14 Oct 2024 19:09:14 +0200 Subject: WIP keyboard shortcuts --- View.cpp | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'View.cpp') diff --git a/View.cpp b/View.cpp index fd80b79..c53ad8f 100644 --- a/View.cpp +++ b/View.cpp @@ -1,10 +1,14 @@ #include +#include #include +#include #include "View.h" #include "ViewController.h" -View::View(Museum & m) : controller(m) { +using namespace std; + +View::View(Museum & m) : controller(m, *this) { this->worker = new std::thread(&View::work, this); } @@ -24,18 +28,27 @@ void View::work() { for (SDL_Event e; SDL_PollEvent(&e);) { if (e.type == SDL_EVENT_QUIT) { this->open = false; - return; + continue; + } + if (e.type == SDL_EVENT_KEY_DOWN) { + this->ev_keydown(e.key); + continue; } } this->draw_begin(); - this->controller.update(*this); + this->controller.update(); this->draw_end(); } this->window_deinit(); } +void View::ev_keydown(SDL_KeyboardEvent ev) { + if (ev.repeat) return; + this->controller.keypress(static_cast(ev.scancode)); +} + void View::window_init() { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); this->window = SDL_CreateWindow( @@ -60,7 +73,7 @@ void View::window_deinit() { SDL_Quit(); } -void View::set_size(unsigned int width, unsigned int height) { +void View::window_size(unsigned int width, unsigned int height) { if (this->width == width && this->height == height) return; SDL_SetWindowSize(this->window, width, height); @@ -88,3 +101,23 @@ void View::draw_rect(Rectangle r, Color c) { SDL_RenderFillRect(this->renderer, &rect); } +typedef struct { + bool open; + vector out; +} dialog_file_tmp_data; +vector View::dialog_file() { + dialog_file_tmp_data data = { + .open = true, + .out = {}, + }; + SDL_DialogFileCallback callback = [](void * userdata, const char * const * files, int) -> void { + dialog_file_tmp_data * data = static_cast(userdata); + for (; *files != NULL; files++) + data->out.push_back(std::string(*files)); + data->open = false; + }; + SDL_ShowOpenFileDialog(callback, &data, this->window, NULL, 0, NULL, true); + while (data.open); + return data.out; +} + -- cgit v1.2.3