Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework UI #110

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions code/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ set(FRAMEWORK_CLIENT_SRC
src/external/imgui/wrapper.cpp
src/external/sdl2/wrapper.cpp

src/external/imgui/widgets/ui_base.cpp

src/external/imgui/widgets/console.cpp

src/integrations/client/instance.cpp
Expand Down
439 changes: 186 additions & 253 deletions code/framework/src/external/imgui/widgets/console.cpp

Large diffs are not rendered by default.

62 changes: 31 additions & 31 deletions code/framework/src/external/imgui/widgets/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,58 @@

#pragma once

#include <input/input.h>
#include <utils/command_processor.h>
#include "ui_base.h"

#include <spdlog/spdlog.h>
#include <utils/command_processor.h>

#include <function2.hpp>
#include <memory>
#include <spdlog/spdlog.h>
#include <vector>

namespace Framework::External::ImGUI::Widgets {
class Console {
class Console: virtual public UIBase {
public:
using MenuBarProc = fu2::function<void() const>;

protected:
bool _shouldDisplayWidget = true;
bool _autoScroll = true;
bool _isOpen = false;
bool _updateInputText = false;
bool _focusOnInput = false;
bool _isMultiline = false;
bool _consoleControl = false;
float _consoleUnfocusedAlpha = 0.25f;
std::string _autocompleteWord;
std::shared_ptr<Utils::CommandProcessor> _commandProcessor;
std::shared_ptr<Input::IInput> _input;
std::vector<MenuBarProc> _menuBarDrawers;
std::vector<std::string> _history;

spdlog::logger *_logger;

bool _autoScroll = true;

bool _focusOnInput = false;

bool _updateInputText = false;

std::string _tempInputText;

std::vector<std::string> _history;

int _historyPos = -1;
spdlog::logger *_logger;
static void FormatLog(std::string log);
void SendCommand(const std::string &command) const;

public:
explicit Console(std::shared_ptr<Utils::CommandProcessor> commandProcessor, std::shared_ptr<Input::IInput> input);
~Console() = default;
std::string _autocompleteWord;

void Toggle();
bool Update();
std::vector<MenuBarProc> _menuBarDrawers;

virtual void OnOpen() override;

bool Open();
bool Close();
virtual void OnClose() override;

virtual void LockControls(bool lock) = 0;
virtual void OnUpdate() override;

void SendCommand(const std::string &command) const;

static void FormatLog(std::string log);

public:
explicit Console(std::shared_ptr<Utils::CommandProcessor> commandProcessor);

~Console() = default;

void RegisterMenuBarDrawer(const MenuBarProc &proc) {
_menuBarDrawers.push_back(proc);
}

bool IsOpen() const {
return _isOpen;
}
};
} // namespace Framework::External::ImGUI::Widgets
95 changes: 95 additions & 0 deletions code/framework/src/external/imgui/widgets/ui_base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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::PopFontShadow();

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);
ImGui::PushFontShadow(_styleFontShadowWhenControlsAreUnlocked);

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
82 changes: 82 additions & 0 deletions code/framework/src/external/imgui/widgets/ui_base.h
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
8 changes: 8 additions & 0 deletions code/framework/src/external/imgui/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ namespace Framework::External::ImGUI {
return InputState::ERROR_MISMATCH;
}

if (!_processEventEnabled) {
return InputState::PASS;
}

if (ImGui_ImplSDL2_ProcessEvent(event)) {
return InputState::BLOCK;
}
Expand All @@ -191,6 +195,10 @@ namespace Framework::External::ImGUI {
return InputState::ERROR_MISMATCH;
}

if (!_processEventEnabled) {
return InputState::PASS;
}

if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) {
return InputState::BLOCK;
}
Expand Down
16 changes: 15 additions & 1 deletion code/framework/src/external/imgui/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ namespace Framework::External::ImGUI {

static inline std::atomic_bool isContextInitialized = false;

bool _processEventEnabled = true;

public:
Error Init(Config &config);
Error Shutdown();
Expand All @@ -70,6 +72,18 @@ namespace Framework::External::ImGUI {

bool IsInitialized() const {
return _initialized;
};
}

/**
* This function allows you to enable/disable mouse and keyboard inputs.
* By default, process event is enable.
*/
void SetProcessEventEnabled(bool enable) {
_processEventEnabled = enable;
}

bool IsProcessEventEnabled() const {
return _processEventEnabled;
}
};
} // namespace Framework::External::ImGUI
Loading