This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
298 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include "modal.hpp" | ||
|
||
#include <utility> | ||
#include "gui.hpp" | ||
|
||
namespace openhack::gui { | ||
void Modal::create(const std::string &title, std::function<void(Modal *)> onDraw, bool canEscapeClose) { | ||
// Check if popup with the same title already exists | ||
auto &popups = getPopups(); | ||
for (auto popup: popups) { | ||
if (popup->getTitle() == title) { | ||
return; | ||
} | ||
} | ||
|
||
// Create new popup | ||
auto *popup = new Modal(title, std::move(onDraw), canEscapeClose); | ||
popups.push_back(popup); | ||
} | ||
|
||
void Modal::create(const std::string &title, const std::string &message) { | ||
create(title, [message](Modal *popup) { | ||
gui::text("%s", message.c_str()); | ||
if (gui::button("OK")) { | ||
popup->close(); | ||
} | ||
}); | ||
} | ||
|
||
void Modal::draw() { | ||
bool isOpen = false; | ||
ImGui::OpenPopup(m_title.c_str(), ImGuiPopupFlags_NoOpenOverExistingPopup | ImGuiPopupFlags_NoOpenOverItems); | ||
|
||
auto scale = config::getGlobal<float>("UIScale"); | ||
ImGui::SetNextWindowSizeConstraints({MIN_SIZE.x * scale, MIN_SIZE.y * scale}, | ||
{MAX_SIZE.x * scale, MAX_SIZE.y * scale}); | ||
|
||
auto font = gui::getFont(); | ||
ImGui::PushFont(font.title); | ||
|
||
if (ImGui::BeginPopupModal(m_title.c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { | ||
isOpen = true; | ||
if (m_canEscapeClose && utils::isKeyPressed("Esc")) { | ||
close(); | ||
return ImGui::EndPopup(); | ||
} | ||
ImGui::PushFont(font.normal); | ||
m_onDraw(this); | ||
ImGui::PopFont(); | ||
ImGui::EndPopup(); | ||
} | ||
|
||
ImGui::PopFont(); | ||
|
||
// check if the popup was closed | ||
if (!isOpen) { | ||
auto &popups = getPopups(); | ||
popups.erase(std::remove(popups.begin(), popups.end(), this), popups.end()); | ||
} | ||
} | ||
|
||
const std::string &Modal::getTitle() const { | ||
return m_title; | ||
} | ||
|
||
void Modal::setTitle(const std::string &title) { | ||
m_title = title; | ||
} | ||
|
||
void Modal::close() { | ||
ImGui::CloseCurrentPopup(); | ||
auto &popups = getPopups(); | ||
popups.erase(std::remove(popups.begin(), popups.end(), this), popups.end()); | ||
} | ||
|
||
std::vector<Modal *> &getPopups() { | ||
static std::vector<Modal *> popups; | ||
return popups; | ||
} | ||
|
||
void drawPopups() { | ||
for (auto popup: getPopups()) { | ||
popup->draw(); | ||
} | ||
} | ||
|
||
void closePopups() { | ||
for (auto popup: getPopups()) { | ||
popup->close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
#include <imgui.h> | ||
#include <string> | ||
#include <functional> | ||
#include <vector> | ||
|
||
namespace openhack::gui { | ||
class Modal { | ||
public: | ||
inline static const ImVec2 MIN_SIZE{400, 1}; // Minimum popup size | ||
inline static const ImVec2 MAX_SIZE{600, 800}; // Maximum popup size | ||
|
||
/// @brief Create a new popup with a custom draw function | ||
/// @param title The title of the popup | ||
/// @param onDraw The function that draws the popup | ||
/// @param canEscapeClose Whether the popup can be closed by pressing the Escape key | ||
static void create(const std::string &title, std::function<void(Modal *)> onDraw, bool canEscapeClose = true); | ||
|
||
/// @brief Create a new popup with a message and an OK button | ||
/// @param title The title of the popup | ||
/// @param message The message to display | ||
static void create(const std::string &title, const std::string &message); | ||
|
||
public: | ||
Modal(std::string title, std::function<void(Modal *)> onDraw, bool canEscapeClose) | ||
: m_title(std::move(title)), m_onDraw(std::move(onDraw)), m_canEscapeClose(canEscapeClose) {} | ||
|
||
/// @brief Draws the popup | ||
void draw(); | ||
|
||
/// @brief Get the title of the popup | ||
[[nodiscard]] const std::string &getTitle() const; | ||
|
||
/// @brief Set the title of the popup | ||
void setTitle(const std::string &title); | ||
|
||
/// @brief Close the popup | ||
void close(); | ||
|
||
private: | ||
std::string m_title; | ||
std::function<void(Modal *)> m_onDraw; | ||
bool m_canEscapeClose = true; // Whether the popup can be closed by pressing the Escape key | ||
}; | ||
|
||
/// @brief Get all popups | ||
[[nodiscard]] std::vector<Modal *> &getPopups(); | ||
|
||
/// @brief Draw all popups | ||
void drawPopups(); | ||
|
||
/// @brief Close all popups | ||
void closePopups(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.