aboutsummaryrefslogtreecommitdiff
path: root/View.cpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-14 19:09:14 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-14 19:09:14 +0200
commit70dbffca112f7deb26a470c12ae56d11137aae86 (patch)
tree5216b0914f0ce3adbebdfbac26aee91218270da8 /View.cpp
parent0fc9eb828c0236d759698186dcbdefb2370e61b7 (diff)
WIP keyboard shortcuts
Diffstat (limited to 'View.cpp')
-rw-r--r--View.cpp41
1 files changed, 37 insertions, 4 deletions
diff --git a/View.cpp b/View.cpp
index fd80b79..c53ad8f 100644
--- a/View.cpp
+++ b/View.cpp
@@ -1,10 +1,14 @@
#include <SDL3/SDL.h>
+#include <SDL3/SDL_events.h>
#include <thread>
+#include <vector>
#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<Scancode>(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<string> out;
+} dialog_file_tmp_data;
+vector<string> 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<dialog_file_tmp_data*>(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;
+}
+