269 lines
7.9 KiB
C
269 lines
7.9 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;
|
|
gui->statusBarText = "Welcome to Tibia!";
|
|
}
|
|
|
|
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) {
|
|
Gui_RenderMainMenu(gui);
|
|
Gui_RenderStatusBar(gui);
|
|
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) {
|
|
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_MenuItem("End Game")) {
|
|
}
|
|
|
|
ImGui_EndMenu();
|
|
}
|
|
|
|
if (ImGui_BeginMenu("Info")) {
|
|
if (ImGui_MenuItem("Change Data")) {
|
|
}
|
|
|
|
if (ImGui_MenuItem("Userlist")) {
|
|
}
|
|
|
|
if (ImGui_MenuItem("Comments")) {
|
|
}
|
|
|
|
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_RenderStatusBar(const Gui_t* gui) {
|
|
const ImGuiViewport *viewport = ImGui_GetMainViewport();
|
|
|
|
const float height = ImGui_GetFrameHeight();
|
|
const ImVec2 position = {viewport->Pos.x, viewport->Pos.y + viewport->Size.y - height};
|
|
const ImVec2 size = {viewport->Size.x, height};
|
|
|
|
ImGui_SetNextWindowPosEx(position, ImGuiCond_Always, (ImVec2){0.0f, 0.0f});
|
|
ImGui_SetNextWindowSize(size, ImGuiCond_Always);
|
|
|
|
// Remove rounding, borders and reduce padding
|
|
ImGui_PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
|
|
ImGui_PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
|
ImGui_PushStyleVarImVec2(ImGuiStyleVar_WindowPadding, (ImVec2){8.0f, 2.0f});
|
|
|
|
// Set background and text colors
|
|
ImGui_PushStyleColorImVec4(ImGuiCol_WindowBg, (ImVec4){0.85f, 0.85f, 0.85f, 1.0f});
|
|
ImGui_PushStyleColorImVec4(ImGuiCol_Text, (ImVec4){0.0f, 0.0f, 0.0f, 1.0f});
|
|
|
|
const ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
|
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings |
|
|
ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNav;
|
|
|
|
if (ImGui_Begin("StatusBar", NULL, windowFlags)) {
|
|
ImGui_Text(gui->statusBarText);
|
|
}
|
|
|
|
ImGui_End();
|
|
|
|
ImGui_PopStyleColorEx(2);
|
|
ImGui_PopStyleVarEx(3);
|
|
}
|
|
|
|
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();
|
|
}
|