234 lines
6.6 KiB
C

//
// Created by rov on 12/27/25.
//
#include "gui.h"
#include <float.h>
#include "dcimgui.h"
#include "dcimgui_impl_sdl3.h"
#include "dcimgui_impl_sdlrenderer3.h"
#include "window.h"
#include "config.h"
void Gui_Init(Gui_t *gui) {
ImGui_CreateContext(NULL);
ImGuiIO *io = ImGui_GetIO();
io->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui_StyleColorsLight(NULL);
SDL_Window *window = Window_GetWindow();
SDL_Renderer *renderer = Window_GetRenderer();
cImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
cImGui_ImplSDLRenderer3_Init(renderer);
gui->isPreferencesDialogOpen = false;
}
void Gui_ProcessEvent(const SDL_Event *event) {
cImGui_ImplSDL3_ProcessEvent(event);
}
void Gui_StartRender() {
cImGui_ImplSDLRenderer3_NewFrame();
cImGui_ImplSDL3_NewFrame();
ImGui_NewFrame();
}
void Gui_Render(Gui_t *gui, ConfigParams_t *configParams, const bool inGame) {
Gui_RenderMainMenu(gui, inGame);
Gui_RenderPreferences_Dialog(gui, configParams);
}
void Gui_FinishRender() {
SDL_Renderer *renderer = Window_GetRenderer();
ImGui_Render();
cImGui_ImplSDLRenderer3_RenderDrawData(ImGui_GetDrawData(), renderer);
}
void Gui_Shutdown() {
cImGui_ImplSDLRenderer3_Shutdown();
cImGui_ImplSDL3_Shutdown();
ImGui_DestroyContext(NULL);
}
void Gui_RenderMainMenu(Gui_t *gui, const bool inGame) {
if (!ImGui_BeginMainMenuBar()) {
return;
}
if (ImGui_BeginMenu("File")) {
if (ImGui_MenuItem("Preferences")) {
gui->isPreferencesDialogOpen = true;
}
ImGui_Separator();
if (ImGui_MenuItem("Bye Bye")) {
}
ImGui_EndMenu();
}
if (ImGui_BeginMenu("Game")) {
if (ImGui_MenuItem("New Game")) {
}
if (ImGui_MenuItem("Journey Onward")) {
}
if (ImGui_MenuItemEx("End Game", NULL, NULL, inGame)) {
}
ImGui_EndMenu();
}
if (ImGui_BeginMenu("Info")) {
if (ImGui_MenuItemEx("Change Data", NULL, NULL, inGame)) {
}
if (ImGui_MenuItemEx("Userlist", NULL, NULL, inGame)) {
}
if (ImGui_MenuItemEx("Comments", NULL, NULL, inGame)) {
}
ImGui_EndMenu();
}
const char *label = "Help";
// 1. Calculate text size
const float textWidth = ImGui_CalcTextSize(label).x;
// 2. Add the internal padding ImGui adds to menus (Style.FramePadding.x * 2)
const float menuWidth = textWidth + ImGui_GetStyle()->FramePadding.x * 2.0f + 2;
// 3. Set Cursor (Window Width - Menu Width)
ImGui_SetCursorPosX(ImGui_GetWindowWidth() - menuWidth);
// 4. Draw the menu
if (ImGui_BeginMenu(label)) {
if (ImGui_MenuItem("Version")) {
}
ImGui_EndMenu();
}
ImGui_EndMainMenuBar();
}
void Gui_RenderPreferences_Dialog(Gui_t *gui, ConfigParams_t *configParams) {
if (!gui->isPreferencesDialogOpen) {
return;
}
// Set the dialog position to the center of the screen, and let it grow as necessary
const ImGuiViewport *viewport = ImGui_GetMainViewport();
const ImVec2 viewportCenter = {
viewport->Pos.x + viewport->Size.x * 0.5f, viewport->Pos.y + viewport->Size.y * 0.5f
};
ImGui_SetNextWindowPosEx(viewportCenter, ImGuiCond_Appearing, (ImVec2){0.5f, 0.5f});
ImGui_SetNextWindowSize((ImVec2){450, 0}, ImGuiCond_Always);
if (!ImGui_Begin("Preferences", &gui->isPreferencesDialogOpen,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) {
ImGui_End();
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Gui_RenderPreferences_Dialog: couldn't render.");
}
ImGui_Text("Server Data:");
if (ImGui_BeginTable("ServerTable", 2, ImGuiTableFlags_SizingFixedFit)) {
ImGui_TableSetupColumnEx("Label", ImGuiTableColumnFlags_WidthFixed, 180.0f, 0);
ImGui_TableSetupColumn("Input", ImGuiTableColumnFlags_WidthStretch);
// Server Address
ImGui_TableNextRow();
ImGui_TableSetColumnIndex(0);
ImGui_AlignTextToFramePadding();
ImGui_Text("Tibia-Server Address:");
ImGui_TableSetColumnIndex(1);
ImGui_SetNextItemWidth(-FLT_MIN);
ImGui_InputText("##Address", configParams->serverAddress, IM_ARRAYSIZE(configParams->serverAddress), 0);
// Server Address
ImGui_TableNextRow();
ImGui_TableSetColumnIndex(0);
ImGui_AlignTextToFramePadding();
ImGui_Text("Tibia-Server Portnumber:");
ImGui_TableSetColumnIndex(1);
ImGui_SetNextItemWidth(-FLT_MIN);
ImGui_InputScalar("##Port", ImGuiDataType_U16, &configParams->serverPort);
ImGui_EndTable();
}
ImGui_Spacing();
ImGui_Separator();
ImGui_Spacing();
if (ImGui_BeginTable("BottomSplit", 2, ImGuiTableFlags_BordersInnerV)) {
ImGui_TableSetupColumn("PlayerData", ImGuiTableColumnFlags_WidthStretch);
ImGui_TableSetupColumnEx("Actions", ImGuiTableColumnFlags_WidthFixed, 100.0f, 0);
ImGui_TableNextRow();
// Player Data
ImGui_TableSetColumnIndex(0);
ImGui_Text("Player Data:");
if (ImGui_BeginTable("PlayerDataTable", 2, ImGuiTableFlags_SizingFixedFit)) {
ImGui_TableSetupColumnEx("Label", ImGuiTableColumnFlags_WidthFixed, 120.0f, 0);
ImGui_TableSetupColumn("Input", ImGuiTableColumnFlags_WidthStretch);
// Player Name
ImGui_TableNextRow();
ImGui_TableSetColumnIndex(0);
ImGui_AlignTextToFramePadding();
ImGui_Text("Player Name:");
ImGui_TableSetColumnIndex(1);
ImGui_SetNextItemWidth(-FLT_MIN);
ImGui_InputText("##PName", configParams->lastAccount, IM_ARRAYSIZE(configParams->lastAccount), 0);
// Player Password
ImGui_TableNextRow();
ImGui_TableSetColumnIndex(0);
ImGui_AlignTextToFramePadding();
ImGui_Text("Player Password:");
ImGui_TableSetColumnIndex(1);
ImGui_SetNextItemWidth(-FLT_MIN);
ImGui_InputText("##PPass", configParams->lastPassword, IM_ARRAYSIZE(configParams->lastPassword),
ImGuiInputTextFlags_Password);
ImGui_EndTable();
}
// Buttons
ImGui_TableSetColumnIndex(1);
ImGui_Dummy((ImVec2){0.0f, 2.25f});
if (ImGui_ButtonEx("Cancel", (ImVec2){-FLT_MIN, 0})) {
gui->isPreferencesDialogOpen = false;
}
ImGui_Dummy((ImVec2){0.0f, 10.0f});
if (ImGui_ButtonEx("Save", (ImVec2){-FLT_MIN, 0})) {
Config_Save_Settings(configParams);
gui->isPreferencesDialogOpen = false;
}
ImGui_EndTable();
}
ImGui_End();
}