Skip to content

Commit

Permalink
Merge pull request #110 from MafiaHub/rework-ui
Browse files Browse the repository at this point in the history
Rework UI
  • Loading branch information
Segfaultd authored Dec 28, 2024
2 parents 0bcabe9 + f72c6ae commit 42374d7
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 278 deletions.
2 changes: 2 additions & 0 deletions code/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ set(FRAMEWORK_CLIENT_SRC
src/external/discord/wrapper.cpp
src/external/imgui/wrapper.cpp

src/external/imgui/widgets/ui_base.cpp

src/external/imgui/widgets/console.cpp

src/integrations/client/instance.cpp
Expand Down
430 changes: 184 additions & 246 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
93 changes: 93 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,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
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
4 changes: 4 additions & 0 deletions code/framework/src/external/imgui/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,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 @@ -50,6 +50,8 @@ namespace Framework::External::ImGUI {

static inline std::atomic_bool isContextInitialized = false;

bool _processEventEnabled = true;

public:
Error Init(Config &config);
Error Shutdown();
Expand All @@ -66,6 +68,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

0 comments on commit 42374d7

Please sign in to comment.