/*
Copyright (C) 2026 wanp
This file is part of Wavy.
Wavy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wavy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wavy. If not, see .
*/
#include "popup.h"
#include "app.h"
#include "mem.h"
#include "files.h"
#include
void popup_mouse_down(Uint8 button, SDL_FPoint pos, App *app) {
if (button != SDL_BUTTON_LEFT || button != SDL_BUTTON_RIGHT) {
return;
}
for (size_t i = 0; i < app->popup.options_len; ++i) {
PopupOption *option = &app->popup.options[i];
if (!option->is_disabled && SDL_PointInRectFloat(&pos, &option->rect)) {
option->is_held = true;
return;
}
}
}
void popup_mouse_motion(SDL_MouseButtonFlags state, SDL_FPoint pos, App *app) {
(void)state;
for (size_t i = 0; i < app->popup.options_len; ++i) {
PopupOption *option = &app->popup.options[i];
option->is_hovered = false;
if (SDL_PointInRectFloat(&pos, &option->rect)) {
option->is_hovered = true;
}
}
}
void popup_mouse_up(Uint8 button, SDL_FPoint pos, App *app) {
if (button != SDL_BUTTON_LEFT && button != SDL_BUTTON_RIGHT) {
return;
}
for (size_t i = 0; i < app->popup.options_len; ++i) {
PopupOption *option = &app->popup.options[i];
if (SDL_PointInRectFloat(&pos, &option->rect)) {
SDL_HideWindow(app->popup.window);
option->on_up(option, app);
}
option->is_held = false;
}
}
static void SDLCALL on_file_dialog(void *userdata, const char *const *filelist, int filter) {
(void)filter;
App *app = userdata;
app->core.is_file_dialog_open = false;
if (filelist == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "File dialog error: %s", SDL_GetError());
return;
} else if (*filelist == NULL) {
/* no file selected */
return;
}
/* TODO: multiple files */
const char *path = *filelist;
if (path_exists(path)) {
stop(&app->playback);
play(path, &app->playback);
}
}
static void on_open_file(PopupOption *option, App *app) {
(void)option, (void)app;
if (app->core.is_file_dialog_open) {
return;
}
SDL_ShowOpenFileDialog(on_file_dialog,
app,
app->core.window,
app->core.file_filters, app->core.file_filters_len,
NULL,
0);
app->core.is_file_dialog_open = true;
}
static void on_quit(PopupOption *option, App *app) {
(void)option, (void)app;
SDL_Event e = { .type = SDL_EVENT_QUIT };
SDL_PushEvent(&e);
}
void show_popup(SDL_FPoint pos, App *app) {
PopupOption options[] = {
{ .label = "open file...", .on_up = (PopupOptionHandler)on_open_file },
{ .label = "quit", .on_up = (PopupOptionHandler)on_quit },
};
int popup_height = 0;
for (size_t i = 0; i < SDL_arraysize(options); ++i) {
options[i].rect = (SDL_FRect){
POPUP_BORDER_SIZE,
POPUP_BORDER_SIZE + (POPUP_OPTION_HEIGHT * i),
POPUP_WIDTH - (POPUP_BORDER_SIZE*2),
POPUP_OPTION_HEIGHT,
};
popup_height += POPUP_OPTION_HEIGHT;
}
app->popup.options = arealloc(app->popup.options, sizeof options);
memcpy(app->popup.options, options, sizeof options);
app->popup.options_len = SDL_arraysize(options);
SDL_SetWindowSize(app->popup.window, POPUP_WIDTH*APP_SCALE, (popup_height + POPUP_BORDER_SIZE*2)*APP_SCALE);
SDL_SetRenderLogicalPresentation(app->popup.renderer,
POPUP_WIDTH, popup_height + POPUP_BORDER_SIZE*2,
SDL_LOGICAL_PRESENTATION_STRETCH);
SDL_FPoint window_pos;
SDL_RenderCoordinatesToWindow(app->core.renderer, pos.x, pos.y, &window_pos.x, &window_pos.y);
SDL_SetWindowPosition(app->popup.window, window_pos.x, window_pos.y);
SDL_ShowWindow(app->popup.window);
draw_popup(app);
}
void draw_popup(App *app) {
SDL_Renderer *renderer = app->popup.renderer;
int render_w, render_h;
SDL_GetRenderLogicalPresentation(renderer, &render_w, &render_h, NULL);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 241, 232, 255);
SDL_FRect border_rect = {
POPUP_BORDER_SIZE,
POPUP_BORDER_SIZE,
render_w - (POPUP_BORDER_SIZE*2),
render_h - (POPUP_BORDER_SIZE*2),
};
SDL_RenderFillRect(renderer, &border_rect);
for (size_t i = 0; i < app->popup.options_len; ++i) {
PopupOption *option = &app->popup.options[i];
if (option->is_hovered) {
SDL_SetRenderDrawColor(renderer, 194, 195, 199, 255);
SDL_RenderFillRect(renderer, &option->rect);
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderDebugText(renderer,
POPUP_BORDER_SIZE + 2,
2 + option->rect.y,
option->label);
}
SDL_RenderPresent(renderer);
}