Skip to content

Commit

Permalink
Fix MessageBoard
Browse files Browse the repository at this point in the history
  • Loading branch information
doyaGu committed Sep 16, 2024
1 parent 717dc45 commit 368f9eb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 52 deletions.
74 changes: 36 additions & 38 deletions src/MessageBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "ModManager.h"

MessageBoard::MessageBoard() : Bui::Window("MessageBoard") {
MessageBoard::MessageBoard(const int size) : Bui::Window("MessageBoard"), m_Messages(size) {
SetVisibility(false);
}

Expand All @@ -23,51 +23,48 @@ ImGuiWindowFlags MessageBoard::GetFlags() {
}

void MessageBoard::OnBegin() {
const int count = std::min(MSG_MAXSIZE, m_MsgCount);
const float sy = static_cast<float>(count) * ImGui::GetTextLineHeightWithSpacing();

ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 0.0f));
ImGui::PushStyleColor(ImGuiCol_WindowBg, Bui::GetMenuColor());

const ImVec2 vpSize = ImGui::GetMainViewport()->Size;
m_WindowPos = ImVec2(vpSize.x * 0.02f, vpSize.y * 0.9f - sy);
m_WindowSize = ImVec2(vpSize.x * 0.96f, sy);

ImGui::SetNextWindowPos(m_WindowPos, ImGuiCond_Always);
ImGui::SetNextWindowSize(m_WindowSize, ImGuiCond_Always);
const float sy = static_cast<float>(m_MessageCount) * ImGui::GetTextLineHeightWithSpacing();
ImGui::SetNextWindowPos(ImVec2(vpSize.x * 0.02f, vpSize.y * 0.9f - sy), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(vpSize.x * 0.96f, sy), ImGuiCond_Always);
}

void MessageBoard::OnDraw() {
const int count = std::min(MSG_MAXSIZE, m_MsgCount);
const float ly = ImGui::GetTextLineHeightWithSpacing();
ImVec4 bgColorVec4 = Bui::GetMenuColor();

ImGui::BringWindowToDisplayFront(ImGui::GetCurrentWindow());

const float ly = ImGui::GetTextLineHeightWithSpacing();

const ImVec2 cursorPos = ImGui::GetCursorScreenPos();
const ImVec2 contentSize = ImGui::GetContentRegionMax();

ImVec2 msgPos = cursorPos;
ImVec4 bgColorVec4 = Bui::GetMenuColor();

ImDrawList *drawList = ImGui::GetWindowDrawList();

if (m_IsCommandBarVisible) {
for (int i = 0; i < count; i++) {
msgPos.y = cursorPos.y + static_cast<float>(count - i) * ly;
for (int i = 0; i < m_MessageCount; i++) {
msgPos.y = cursorPos.y + static_cast<float>(m_MessageCount - i) * ly;

drawList->AddRectFilled(msgPos, ImVec2(msgPos.x + contentSize.x, msgPos.y + ly), ImGui::GetColorU32(bgColorVec4));
drawList->AddText(msgPos, IM_COL32_WHITE, m_Msgs[i].Text);
drawList->AddText(msgPos, IM_COL32_WHITE, m_Messages[i].GetMessage());
}
} else {
for (int i = 0; i < count; i++) {
const float timer = m_Msgs[i].Timer;
for (int i = 0; i < m_MessageCount; i++) {
const float timer = m_Messages[i].GetTimer();
if (timer > 0) {
msgPos.y = cursorPos.y + static_cast<float>(count - i) * ly;
bgColorVec4.w = std::min(110.0f, (timer / 20.0f)) / 255.0f;
msgPos.y = cursorPos.y + static_cast<float>(m_MessageCount - i) * ly;
bgColorVec4.w = std::min(110.0f, timer / 20.0f) / 255.0f;

drawList->AddRectFilled(msgPos, ImVec2(msgPos.x + contentSize.x, msgPos.y + ly), ImGui::GetColorU32(bgColorVec4));
drawList->AddText(msgPos, IM_COL32_WHITE, m_Msgs[i].Text);
drawList->AddText(msgPos, IM_COL32_WHITE, m_Messages[i].GetMessage());
}
}
}
Expand All @@ -80,36 +77,37 @@ void MessageBoard::OnAfterEnd() {
CKStats stats;
BML_GetCKContext()->GetProfileStats(&stats);

const int count = std::min(MSG_MAXSIZE, m_MsgCount);
for (int i = 0; i < count; i++) {
m_Msgs[i].Timer -= stats.TotalFrameTime;
if (m_Msgs[i].Timer == 0)
--m_DisplayMsgCount;
for (int i = 0; i < m_MessageCount; i++) {
m_Messages[i].timer -= stats.TotalFrameTime;
if (m_Messages[i].timer == 0)
--m_DisplayMessageCount;
}

if (m_DisplayMsgCount == 0)
if (m_DisplayMessageCount == 0)
Hide();
}

void MessageBoard::AddMessage(const char *msg) {
for (int i = std::min(MSG_MAXSIZE - 1, m_MsgCount) - 1; i >= 0; i--) {
const char *text = m_Msgs[i].Text;
strncpy(m_Msgs[i + 1].Text, text, 256);
m_Msgs[i + 1].Timer = m_Msgs[i].Timer;
if (m_MessageCount == m_Messages.size() && m_Messages[m_MessageCount - 1].timer > 0) {
--m_DisplayMessageCount;
}

strncpy(m_Msgs[0].Text, msg, 256);
m_Msgs[0].Timer = m_MsgMaxTimer;
++m_MsgCount;
++m_DisplayMsgCount;
for (int i = std::min(m_MessageCount, static_cast<int>(m_Messages.size()) - 1) - 1; i >= 0; i--)
m_Messages[i + 1] = std::move(m_Messages[i]);
m_Messages[0] = std::move(MessageUnit(msg, m_MaxTimer));

if (m_MessageCount < static_cast<int>(m_Messages.size())) {
++m_MessageCount;
++m_DisplayMessageCount;
}
}

void MessageBoard::ClearMessages() {
m_MsgCount = 0;
m_DisplayMsgCount = 0;
for (auto &m_Msg : m_Msgs) {
m_Msg.Text[0] = '\0';
m_Msg.Timer = 0.0f;
m_MessageCount = 0;
m_DisplayMessageCount = 0;
for (auto &message : m_Messages) {
message.text.clear();
message.timer = 0.0f;
}
}

Expand Down
66 changes: 52 additions & 14 deletions src/MessageBoard.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
#ifndef BML_MESSAGEBOARD_H
#define BML_MESSAGEBOARD_H

#include "BML/Bui.h"
#include <vector>

#define MSG_MAXSIZE 35
#include "BML/Bui.h"

class MessageBoard : public Bui::Window {
public:
MessageBoard();
struct MessageUnit {
std::string text;
float timer = 0.0f;

MessageUnit() = default;
MessageUnit(const char *msg, const float timer) : text(msg), timer(timer) {}
MessageUnit(const MessageUnit &other) = default;
MessageUnit(MessageUnit &&other) noexcept : text(std::move(other.text)), timer(other.timer) {}

MessageUnit &operator=(const MessageUnit &other) {
if (this == &other)
return *this;
text = other.text;
timer = other.timer;
return *this;
}

MessageUnit &operator=(MessageUnit &&other) noexcept {
if (this == &other)
return *this;
text = std::move(other.text);
timer = other.timer;
return *this;
}

const char *GetMessage() const {
return text.c_str();
}

void SetMessage(const char *msg) {
if (msg)
text = msg;
}

float GetTimer() const {
return timer;
}

void SetTimer(const float t) {
timer = t;
}
};

explicit MessageBoard(int size = 35);
~MessageBoard() override;

ImGuiWindowFlags GetFlags() override;
Expand All @@ -20,28 +63,23 @@ class MessageBoard : public Bui::Window {
void ClearMessages();

float GetMaxTimer() const {
return m_MsgMaxTimer;
return m_MaxTimer;
}

void SetMaxTimer(float maxTimer) {
m_MsgMaxTimer = maxTimer;
m_MaxTimer = maxTimer;
}

void SetCommandBarVisible(bool visible) {
m_IsCommandBarVisible = visible;
}

private:
ImVec2 m_WindowPos;
ImVec2 m_WindowSize;
bool m_IsCommandBarVisible = false;
int m_MsgCount = 0;
int m_DisplayMsgCount = 0;
struct {
char Text[256] = {};
float Timer = 0.0f;
} m_Msgs[MSG_MAXSIZE] = {};
float m_MsgMaxTimer = 6000; // ms
int m_MessageCount = 0;
int m_DisplayMessageCount = 0;
std::vector<MessageUnit> m_Messages;
float m_MaxTimer = 6000; // ms
};

#endif // BML_MESSAGEBOARD_H

0 comments on commit 368f9eb

Please sign in to comment.