Skip to content

Commit

Permalink
Separate the HUD from BMLMod
Browse files Browse the repository at this point in the history
  • Loading branch information
doyaGu committed Sep 16, 2024
1 parent ec73d37 commit 99751f5
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 97 deletions.
109 changes: 25 additions & 84 deletions src/BMLMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ void BMLMod::OnProcess() {
OnResize();
}

OnProcess_Fps();
OnProcess_SRTimer();
OnProcess_HUD();
OnProcess_CommandBar();
OnProcess_Menu();
Expand Down Expand Up @@ -203,8 +201,12 @@ void BMLMod::OnModifyConfig(const char *category, const char *key, IProperty *pr
AdjustFrameRate(false, static_cast<float>(val));
else
AdjustFrameRate(true);
} else if (prop == m_ShowTitle) {
m_HUD.ShowTitle(m_ShowTitle->GetBoolean());
} else if (prop == m_ShowFPS) {
m_HUD.ShowFPS(m_ShowFPS->GetBoolean());
} else if (prop == m_ShowSR && m_BML->IsIngame()) {
m_SRShouldDraw = m_ShowSR->GetBoolean();
m_HUD.ShowSRTimer(m_ShowSR->GetBoolean());
} else if (prop == m_MsgDuration) {
const float timer = m_MsgDuration->GetFloat() * 1000;
m_MessageBoard.SetMaxTimer(timer);
Expand Down Expand Up @@ -243,6 +245,9 @@ void BMLMod::OnPreStartMenu() {
}

RenderHook::EnableWidescreenFix(m_WidescreenFix->GetBoolean());

m_HUD.ShowTitle(m_ShowTitle->GetBoolean());
m_HUD.ShowFPS(m_ShowFPS->GetBoolean());
}

void BMLMod::OnExitGame() {
Expand All @@ -262,32 +267,28 @@ void BMLMod::OnStartLevel() {
else
AdjustFrameRate(true);
}
m_SRTimer = 0.0f;
strcpy(m_SRScore, "00:00:00.000");
if (m_ShowSR->GetBoolean()) {
m_SRShouldDraw = true;
}
m_HUD.ResetSRTimer(m_ShowSR->GetBoolean());
SetParamValue(m_LoadCustom, FALSE);
}

void BMLMod::OnPostExitLevel() {
m_SRShouldDraw = false;
m_HUD.ShowSRTimer(false);
}

void BMLMod::OnPauseLevel() {
m_SRActivated = false;
m_HUD.ActivateSRTimer(false);
}

void BMLMod::OnUnpauseLevel() {
m_SRActivated = true;
m_HUD.ActivateSRTimer(true);
}

void BMLMod::OnCounterActive() {
m_SRActivated = true;
m_HUD.ActivateSRTimer(true);
}

void BMLMod::OnCounterInactive() {
m_SRActivated = false;
m_HUD.ActivateSRTimer(false);
}

void BMLMod::AddIngameMessage(const char *msg) {
Expand Down Expand Up @@ -342,6 +343,10 @@ void BMLMod::LoadMap(const std::wstring &path) {
m_ExitStart->Activate();
}

float BMLMod::GetSRScore() const {
return m_HUD.GetSRScore();
}

int BMLMod::GetHSScore() {
int points, lifes;
CKDataArray *energy = m_BML->GetArrayByName("Energy");
Expand Down Expand Up @@ -377,8 +382,13 @@ int BMLMod::GetHUD() {

void BMLMod::SetHUD(int mode) {
m_ShowTitle->SetBoolean((mode & HUD_TITLE) != 0);
m_HUD.ShowTitle(m_ShowTitle->GetBoolean());

m_ShowFPS->SetBoolean((mode & HUD_FPS) != 0);
m_HUD.ShowFPS(m_ShowFPS->GetBoolean());

m_ShowSR->SetBoolean((mode & HUD_SR) != 0);
m_HUD.ShowSRTimer(m_ShowSR->GetBoolean());
}

void BMLMod::InitConfigs() {
Expand Down Expand Up @@ -940,78 +950,9 @@ void BMLMod::OnEditScript_ExtraLife_Fix(CKBehavior *script) {
->SetDirectSource(CreateParamValue<float>(script, "DeltaTime", CKPGUID_FLOAT, 20.0f));
}

void BMLMod::OnProcess_Fps() {
CKStats stats;
m_CKContext->GetProfileStats(&stats);
m_FPSCount += int(1000 / stats.TotalFrameTime);
if (++m_FPSTimer == 60) {
sprintf(m_FPSText, "FPS: %d", m_FPSCount / 60);
m_FPSTimer = 0;
m_FPSCount = 0;
}
}

void BMLMod::OnProcess_SRTimer() {
if (m_SRActivated) {
m_SRTimer += m_TimeManager->GetLastDeltaTime();
int counter = int(m_SRTimer);
int ms = counter % 1000;
counter /= 1000;
int s = counter % 60;
counter /= 60;
int m = counter % 60;
counter /= 60;
int h = counter % 100;
sprintf(m_SRScore, "%02d:%02d:%02d.%03d", h, m, s, ms);
}
}

void BMLMod::OnProcess_HUD() {
const ImVec2 &vpSize = ImGui::GetMainViewport()->Size;

ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(vpSize);

constexpr ImGuiWindowFlags HUDFlags = ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoBackground |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoInputs |
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoSavedSettings;
if (ImGui::Begin("HUD", nullptr, HUDFlags)) {
ImDrawList *drawList = ImGui::GetWindowDrawList();

float oldScale = ImGui::GetFont()->Scale;
ImGui::GetFont()->Scale *= 1.2f;
ImGui::PushFont(ImGui::GetFont());

if (m_ShowTitle->GetBoolean()) {
constexpr auto TitleText = "BML Plus " BML_VERSION;
const auto titleSize = ImGui::CalcTextSize(TitleText);
drawList->AddText(ImVec2((vpSize.x - titleSize.x) / 2.0f, 0), IM_COL32_WHITE, TitleText);
}

if (m_BML->IsCheatEnabled()) {
constexpr auto CheatText = "Cheat Mode Enabled";
const auto cheatSize = ImGui::CalcTextSize(CheatText);
drawList->AddText(ImVec2((vpSize.x - cheatSize.x) / 2.0f, vpSize.y * 0.85f), IM_COL32_WHITE, CheatText);
}

if (m_ShowFPS->GetBoolean()) {
drawList->AddText(ImVec2(0, 0), IM_COL32_WHITE, m_FPSText);
}

if (m_SRShouldDraw) {
drawList->AddText(ImVec2(vpSize.x * 0.03f, vpSize.y * 0.8f), IM_COL32_WHITE, "SR Timer");
auto srSize = ImGui::CalcTextSize(m_SRScore);
drawList->AddText(ImVec2(vpSize.x * 0.05f, vpSize.y * 0.8f + srSize.y), IM_COL32_WHITE, m_SRScore);
}

ImGui::GetFont()->Scale = oldScale;
ImGui::PopFont();
}
ImGui::End();
m_HUD.OnProcess();
m_HUD.Render();
}

void BMLMod::OnProcess_CommandBar() {
Expand Down
17 changes: 4 additions & 13 deletions src/BMLMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#include "BML/Bui.h"

#include "Config.h"
#include "HUD.h"
#include "ModMenu.h"
#include "MapMenu.h"
#include "CommandBar.h"
#include "MessageBoard.h"

class BMLMod;

enum HudTypes {
HUD_TITLE = 1,
HUD_FPS = 2,
Expand Down Expand Up @@ -63,7 +62,8 @@ class BMLMod : public IMod {

void LoadMap(const std::wstring &path);

float GetSRScore() const { return m_SRTimer; }
float GetSRScore() const;

int GetHSScore();

void AdjustFrameRate(bool sync = false, float limit = 60.0f);
Expand All @@ -85,8 +85,6 @@ class BMLMod : public IMod {
void OnEditScript_Levelinit_build(CKBehavior *script);
void OnEditScript_ExtraLife_Fix(CKBehavior *script);

void OnProcess_Fps();
void OnProcess_SRTimer();
void OnProcess_HUD();
void OnProcess_CommandBar();
void OnProcess_Menu();
Expand All @@ -110,6 +108,7 @@ class BMLMod : public IMod {
VxRect m_OldWindowRect;
VxRect m_WindowRect;

HUD m_HUD;
ModMenu m_ModMenu;
MapMenu m_MapMenu;
CommandBar m_CommandBar;
Expand All @@ -119,14 +118,6 @@ class BMLMod : public IMod {
bool m_ShowImGuiDemo = false;
#endif

int m_FPSCount = 0;
int m_FPSTimer = 0;
char m_FPSText[16] = {};
float m_SRTimer = 0.0f;
char m_SRScore[16] = {};
bool m_SRActivated = false;
bool m_SRShouldDraw = false;

IProperty *m_ShowTitle = nullptr;
IProperty *m_ShowFPS = nullptr;
IProperty *m_ShowSR = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(BML_PRIVATE_HEADERS
BMLMod.h
NewBallTypeMod.h

HUD.h
ModMenu.h
MapMenu.h
CommandBar.h
Expand Down Expand Up @@ -65,6 +66,7 @@ set(BML_SOURCES
BMLMod.cpp
NewBallTypeMod.cpp

HUD.cpp
ModMenu.cpp
MapMenu.cpp
CommandBar.cpp
Expand Down
89 changes: 89 additions & 0 deletions src/HUD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "HUD.h"

#include "ModManager.h"

HUD::HUD() : Bui::Window("HUD") {
SetVisibility(true);
}

HUD::~HUD() = default;

ImGuiWindowFlags HUD::GetFlags() {
return ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoBackground |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoInputs |
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoSavedSettings;
}

void HUD::OnBegin() {
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size);
}

void HUD::OnDraw() {
const ImVec2 contentSize = ImGui::GetContentRegionAvail();
ImDrawList *drawList = ImGui::GetWindowDrawList();

float oldScale = ImGui::GetFont()->Scale;
ImGui::GetFont()->Scale *= 1.2f;
ImGui::PushFont(ImGui::GetFont());

if (m_ShowTitle) {
constexpr auto TitleText = "BML Plus " BML_VERSION;
const auto titleSize = ImGui::CalcTextSize(TitleText);
drawList->AddText(ImVec2((contentSize.x - titleSize.x) / 2.0f, 0), IM_COL32_WHITE, TitleText);
}

if (m_ShowFPS) {
drawList->AddText(ImVec2(0, 0), IM_COL32_WHITE, m_FPSText);
}

if (BML_GetModManager()->IsCheatEnabled()) {
constexpr auto CheatText = "Cheat Mode Enabled";
const auto cheatSize = ImGui::CalcTextSize(CheatText);
drawList->AddText(ImVec2((contentSize.x - cheatSize.x) / 2.0f, contentSize.y * 0.85f), IM_COL32_WHITE, CheatText);
}

if (m_ShowSRTimer) {
drawList->AddText(ImVec2(contentSize.x * 0.03f, contentSize.y * 0.8f), IM_COL32_WHITE, "SR Timer");
auto srSize = ImGui::CalcTextSize(m_SRScore);
drawList->AddText(ImVec2(contentSize.x * 0.05f, contentSize.y * 0.8f + srSize.y), IM_COL32_WHITE, m_SRScore);
}

ImGui::GetFont()->Scale = oldScale;
ImGui::PopFont();
}

void HUD::OnProcess() {
OnProcess_Fps();
OnProcess_SRTimer();
}

void HUD::OnProcess_Fps() {
CKStats stats;
BML_GetCKContext()->GetProfileStats(&stats);
m_FPSCount += int(1000 / stats.TotalFrameTime);
if (++m_FPSTimer == 60) {
sprintf(m_FPSText, "FPS: %d", m_FPSCount / 60);
m_FPSTimer = 0;
m_FPSCount = 0;
}
}

void HUD::OnProcess_SRTimer() {
if (m_SRActivated) {
m_SRTimer += BML_GetCKContext()->GetTimeManager()->GetLastDeltaTime();
int counter = int(m_SRTimer);
int ms = counter % 1000;
counter /= 1000;
int s = counter % 60;
counter /= 60;
int m = counter % 60;
counter /= 60;
int h = counter % 100;
sprintf(m_SRScore, "%02d:%02d:%02d.%03d", h, m, s, ms);
}
}
Loading

0 comments on commit 99751f5

Please sign in to comment.