Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions source/Gui/AlienDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "AlienDialog.h"

#include <Fonts/IconsFontAwesome5.h>

AlienDialog::AlienDialog(std::string const& title, RealVector2D const& defaultSize)
AlienDialog::AlienDialog(std::string const& title, RealVector2D const& defaultSize, bool maximizable)
: _title(title)
, _defaultSize(defaultSize)
, _isMaximizable(maximizable)
{}

void AlienDialog::init()
Expand Down Expand Up @@ -63,20 +65,32 @@ void AlienDialog::processDialog()
ImGui::SetNextWindowSize({scale(_defaultSize.x), scale(_defaultSize.y)}, ImGuiCond_FirstUseEver);
ImGui::OpenPopup(_title.c_str());
_state = DialogState::Open;
_windowState = DialogWindowState::Normal;
}
auto& style = ImGui::GetStyle();
auto origWindowMinSize = style.WindowMinSize;
style.WindowMinSize.x = scale(350.0f);
style.WindowMinSize.y = scale(150.0f);

if (ImGui::BeginPopupModal(_title.c_str(), NULL, 0)) {
ImGuiWindowFlags flags = ImGuiWindowFlags_None;
if (_isMaximizable && _windowState == DialogWindowState::Maximized) {
auto viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always);
ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always);
flags |= ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
}

if (ImGui::BeginPopupModal(_title.c_str(), NULL, flags)) {
if (!_sizeInitialized) {
auto size = ImGui::GetWindowSize();
auto factor = WindowController::get().getContentScaleFactor() / WindowController::get().getLastContentScaleFactor();
ImGui::SetWindowSize({size.x * factor, size.y * factor});
_sizeInitialized = true;
}

if (_isMaximizable) {
processMaximizeButton();
}

ImGui::PushID(_title.c_str());
processIntern();
Expand All @@ -88,6 +102,46 @@ void AlienDialog::processDialog()
style.WindowMinSize = origWindowMinSize;
}

void AlienDialog::processMaximizeButton()
{
auto titlebarHeight = ImGui::GetFrameHeight();
auto windowPos = ImGui::GetWindowPos();
auto windowSize = ImGui::GetWindowSize();
auto iconSize = ImGui::GetFontSize();
auto iconPos = ImVec2(windowPos.x + windowSize.x - scale(24.0f), windowPos.y + (titlebarHeight - iconSize) * 0.5f);
auto iconCenter = ImVec2(iconPos.x + iconSize * 0.5f, iconPos.y + iconSize * 0.5f);

ImGui::SetCursorScreenPos(iconPos);
if (ImGui::InvisibleButton("MaximizeButton", ImVec2(iconSize, iconSize))) {
if (_windowState == DialogWindowState::Maximized) {
ImGui::SetWindowPos(_savedPos);
ImGui::SetWindowSize(_savedSize);
_windowState = DialogWindowState::Normal;
} else {
_savedPos = windowPos;
_savedSize = windowSize;
_windowState = DialogWindowState::Maximized;
}
}

auto pressed = ImGui::IsItemActive();
auto hovered = ImGui::IsItemHovered();
auto drawList = ImGui::GetWindowDrawList();
if (hovered || pressed) {
auto bgColor = hovered && pressed ? ImGui::GetColorU32(ImGuiCol_ButtonActive) : ImGui::GetColorU32(ImGuiCol_ButtonHovered);
auto radius = iconSize * 0.6f;
drawList->AddCircleFilled(iconCenter, radius, bgColor, 12);
}

auto icon = _windowState == DialogWindowState::Maximized ? ICON_FA_COMPRESS_ARROWS_ALT : ICON_FA_EXPAND_ARROWS_ALT;
drawList->AddText(
StyleRepository::get().getIconFont(),
iconSize * 0.7f,
ImVec2(iconCenter.x - iconSize * 0.31f, iconCenter.y - iconSize * 0.22f),
ImGui::GetColorU32(ImGuiCol_Text),
icon);
}

void AlienDialog::shutdown()
{
shutdownIntern();
Expand Down
13 changes: 12 additions & 1 deletion source/Gui/AlienDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class AlienDialog : public MainLoopEntity
{
public:
AlienDialog(std::string const& title, RealVector2D const& defaultSize = RealVector2D(450.0f, 150.0f));
AlienDialog(std::string const& title, RealVector2D const& defaultSize = RealVector2D(450.0f, 150.0f), bool maximizable = false);

virtual void open();
void processNested();
Expand All @@ -32,6 +32,7 @@ class AlienDialog : public MainLoopEntity
void init() override;
void process() override;
void processDialog();
void processMaximizeButton();
void shutdown() override;

bool _sizeInitialized = false;
Expand All @@ -45,4 +46,14 @@ class AlienDialog : public MainLoopEntity
DialogState _state = DialogState::Closed;
std::string _title;
RealVector2D _defaultSize;

bool _isMaximizable = false;
enum class DialogWindowState
{
Normal,
Maximized
};
DialogWindowState _windowState = DialogWindowState::Normal;
ImVec2 _savedPos;
ImVec2 _savedSize;
};
2 changes: 1 addition & 1 deletion source/Gui/MutationRatesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ namespace
}

MutationRatesDialog::MutationRatesDialog()
: AlienDialog("Mutation rates", {800.0f, 400.0f})
: AlienDialog("Mutation rates", {800.0f, 400.0f}, true)
{}

void MutationRatesDialog::initIntern() {}
Expand Down
Loading