-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from MafiaHub/rework-ui
Rework UI
- Loading branch information
Showing
7 changed files
with
411 additions
and
278 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
430 changes: 184 additions & 246 deletions
430
code/framework/src/external/imgui/widgets/console.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
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,93 @@ | ||
/* | ||
* MafiaHub OSS license | ||
* Copyright (c) 2021-2023, MafiaHub. All rights reserved. | ||
* | ||
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework. | ||
* See LICENSE file in the source repository for information regarding licensing. | ||
*/ | ||
|
||
#include "ui_base.h" | ||
|
||
namespace Framework::External::ImGUI::Widgets { | ||
void UIBase::Open(bool lockControls) { | ||
_open = true; | ||
_wasOpen = true; | ||
|
||
if (lockControls) { | ||
_lockControlsWhenOpen = true; | ||
LockControls(true); | ||
} | ||
|
||
OnOpen(); | ||
} | ||
|
||
void UIBase::Close() { | ||
OnClose(); | ||
|
||
if (_lockControlsWhenOpen) { | ||
LockControls(false); | ||
} | ||
|
||
_lockControlsWhenOpen = false; | ||
_wasOpen = false; | ||
_open = false; | ||
} | ||
|
||
void UIBase::Toggle(bool lockControls) { | ||
if (_open) { | ||
Close(); | ||
} | ||
else { | ||
Open(lockControls); | ||
} | ||
} | ||
|
||
void UIBase::Update() { | ||
// Detect if _open has changed but Close() has not been called. | ||
// This can be the case when we use the close button of the window. | ||
if (_wasOpen && !_open) { | ||
Close(); | ||
return; | ||
} | ||
|
||
// Prevent updating if not open | ||
if (!_open) { | ||
return; | ||
} | ||
|
||
OnUpdate(); | ||
} | ||
|
||
void UIBase::CleanUpUIWindow() { | ||
if (!AreControlsLocked()) { | ||
|
||
ImGui::PopStyleColor(); | ||
ImGui::PopStyleColor(); | ||
} | ||
|
||
ImGui::End(); | ||
} | ||
|
||
void UIBase::CreateUIWindow(const char *name, WindowContent windowContent, bool *pOpen, ImGuiWindowFlags flags) { | ||
if (!AreControlsLocked()) { | ||
flags |= ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse; | ||
|
||
ImGui::SetNextWindowBgAlpha(_styleWindowBackgroundAlphaWhenControlsAreUnlocked); | ||
|
||
ImGuiStyle &style = ImGui::GetStyle(); | ||
ImGui::PushStyleColor(ImGuiCol_TitleBg, style.Colors[ImGuiCol_TitleBgCollapsed]); | ||
ImGui::PushStyleColor(ImGuiCol_TitleBgActive, style.Colors[ImGuiCol_TitleBgCollapsed]); | ||
} | ||
|
||
bool wasWindowProcessed = ImGui::Begin(name, AreControlsLocked() ? pOpen : NULL, flags); | ||
|
||
if (!wasWindowProcessed) { | ||
CleanUpUIWindow(); | ||
return; | ||
} | ||
|
||
windowContent(); | ||
|
||
CleanUpUIWindow(); | ||
} | ||
} // namespace Framework::External::ImGUI::Widgets |
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,82 @@ | ||
/* | ||
* MafiaHub OSS license | ||
* Copyright (c) 2021-2023, MafiaHub. All rights reserved. | ||
* | ||
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework. | ||
* See LICENSE file in the source repository for information regarding licensing. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <fu2/function2.hpp> | ||
#include <imgui/imgui.h> | ||
|
||
namespace Framework::External::ImGUI::Widgets { | ||
class UIBase { | ||
public: | ||
using WindowContent = fu2::function<void() const>; | ||
|
||
private: | ||
bool _lockControlsWhenOpen = false; | ||
|
||
bool _wasOpen = false; | ||
|
||
void CleanUpUIWindow(); | ||
|
||
protected: | ||
bool _open = false; | ||
|
||
float _styleWindowBackgroundAlphaWhenControlsAreUnlocked = 0.25f; | ||
|
||
unsigned int _styleFontShadowWhenControlsAreUnlocked = 0xFF000000; | ||
|
||
virtual void OnOpen() = 0; | ||
|
||
virtual void OnClose() = 0; | ||
|
||
virtual void OnUpdate() = 0; | ||
|
||
virtual bool AreControlsLocked() const = 0; | ||
|
||
virtual void LockControls(bool lock) const = 0; | ||
|
||
public: | ||
UIBase() {}; | ||
|
||
bool IsOpen() const { | ||
return _open; | ||
} | ||
|
||
/** | ||
* You probably shouldn't override `Open`, use `OnOpen` instead. | ||
*/ | ||
void Open(bool lockControls = true); | ||
|
||
/** | ||
* You probably shouldn't override `Close`, use `OnClose` instead. | ||
*/ | ||
void Close(); | ||
|
||
void Toggle(bool lockControls = true); | ||
|
||
/** | ||
* You probably shouldn't override `Update`, use `OnUpdate` instead. | ||
* | ||
* Update will prevent OnUpdate to be called if the UI is not open. | ||
*/ | ||
void Update(); | ||
|
||
/** | ||
* Create a custom ImGui window. | ||
* | ||
* When the controls are unlocked: | ||
* - window background becomes transparent (customize with _styleWindowBackgroundAlphaWhenControlsAreUnlocked) | ||
* - add font shadow (customize with _styleFontShadowWhenControlsAreUnlocked) | ||
* - set title bar to "collapse" style | ||
* - close button is removed | ||
* - resizing is disabled | ||
* - collapsing is disabled | ||
*/ | ||
void CreateUIWindow(const char *name, WindowContent windowContent, bool *pOpen = NULL, ImGuiWindowFlags flags = 0); | ||
}; | ||
} // namespace Framework::External::ImGUI::Widgets |
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