Skip to content

Commit

Permalink
Add Config Comments Display
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamepiaynmo committed Apr 17, 2021
1 parent d4bcf11 commit db823ba
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 9 deletions.
109 changes: 107 additions & 2 deletions BMLMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,10 @@ GuiModMenu::GuiModMenu(IMod* mod) : GuiList() {
BGui::Label* desc = AddTextLabel("M_Opt_ModMenu_Description", mod->GetDescription(), ExecuteBB::GAMEFONT_03, 0.35f, 0.20f, 0.3f, 0.1f);
desc->SetTextFlags(TEXT_SCREEN | TEXT_WORDWRAP);
desc->SetAlignment(ALIGN_TOP);
m_comment_bg = AddPanel("M_Opt_ModMenu_Comment_Bg", VxColor(0, 0, 0, 110), 0.725f, 0.4f, 0.25f, 0.2f);
m_comment = AddTextLabel("M_Opt_ModMenu_Comment", "", ExecuteBB::GAMEFONT_03, 0.725f, 0.4f, 0.25f, 0.2f);
m_comment->SetTextFlags(TEXT_SCREEN | TEXT_WORDWRAP);
m_comment->SetAlignment(ALIGN_TOP);

m_config = ModLoader::m_instance->GetConfig(mod);
if (m_config) {
Expand All @@ -1304,6 +1308,49 @@ GuiModMenu::GuiModMenu(IMod* mod) : GuiList() {
SetVisible(false);
}

void GuiModMenu::Process() {
bool show_cmt = false;
if (m_curpage >= 0 && m_curpage < m_maxpage) {
CKRenderContext* rc = ModLoader::m_instance->GetRenderContext();
InputHook* input = ModLoader::m_instance->GetInputManager();
Vx2DVector mousePos;
input->GetMousePosition(mousePos, false);
int size = (std::min)(m_maxsize, m_size - m_curpage * m_maxsize);
for (int i = 0; i < size; i++) {
if (Intersect(mousePos.x / rc->GetWidth(), mousePos.y / rc->GetHeight(), m_buttons[i])) {
if (m_curcmt != i) {
m_comment_bg->SetVisible(true);
m_comment->SetVisible(true);
m_comment->SetText(m_config->m_data[i + m_curpage * m_maxsize].comment.c_str());
m_curcmt = i;
}

show_cmt = true;
break;
}
}
}

if (!show_cmt) {
if (m_curcmt >= 0) {
m_comment_bg->SetVisible(false);
m_comment->SetVisible(false);
m_curcmt = -1;
}
}

GuiList::Process();
}

void GuiModMenu::SetVisible(bool visible) {
GuiList::SetVisible(visible);
if (visible) {
m_comment_bg->SetVisible(false);
m_comment->SetVisible(false);
m_curcmt = -1;
}
}

BGui::Button* GuiModMenu::CreateButton(int index) {
BGui::Button* button = AddLevelButton(("M_Opt_ModMenu_" + std::to_string(index)).c_str(), "", 0.45f + 0.06f * index);
button->SetFont(ExecuteBB::GAMEFONT_03);
Expand Down Expand Up @@ -1441,6 +1488,7 @@ GuiModCategory::GuiModCategory(GuiModMenu* parent, Config* config, std::string c
for (Property* prop : config->GetCategory(category.c_str()).props) {
Property* newprop = new Property(nullptr, category, prop->m_key);
newprop->CopyValue(prop);
newprop->SetComment(prop->GetComment());
m_data.push_back(newprop);
}

Expand All @@ -1457,11 +1505,16 @@ GuiModCategory::GuiModCategory(GuiModMenu* parent, Config* config, std::string c
AddBackButton("M_Opt_Category_Back")->SetCallback([this]() { SaveAndExit(); });
m_exit = AddBackButton("M_Opt_Category_Back");
m_exit->SetCallback([this]() { Exit(); });
m_comment_bg = AddPanel("M_Opt_Comment_Bg", VxColor(0, 0, 0, 110), 0.725f, 0.4f, 0.25f, 0.2f);
m_comment = AddTextLabel("M_Opt_Comment", "", ExecuteBB::GAMEFONT_03, 0.725f, 0.4f, 0.25f, 0.2f);
m_comment->SetTextFlags(TEXT_SCREEN | TEXT_WORDWRAP);
m_comment->SetAlignment(ALIGN_TOP);

Vx2DVector offset(0.0f, ModLoader::m_instance->GetRenderContext()->GetHeight() * 0.015f);

float cnt = 0.25f, page = 0;
std::vector<BGui::Element*> elements;
std::vector<std::pair<Property*, BGui::Element*>> comments;
for (Property* prop : m_data) {
std::string name = prop->m_key;
switch (prop->GetType()) {
Expand All @@ -1479,6 +1532,7 @@ GuiModCategory::GuiModCategory(GuiModMenu* parent, Config* config, std::string c
BGui::Panel* panel = AddPanel(name.c_str(), VxColor(0, 0, 0, 110), 0.43f, cnt + 0.05f, 0.18f, 0.025f);
elements.push_back(panel);
m_inputs.push_back({ input, nullptr });
comments.push_back({ prop, bg });
cnt += 0.12f;
break;
}
Expand All @@ -1496,6 +1550,7 @@ GuiModCategory::GuiModCategory(GuiModMenu* parent, Config* config, std::string c
BGui::Panel* panel = AddPanel(name.c_str(), VxColor(0, 0, 0, 110), 0.43f, cnt + 0.05f, 0.18f, 0.025f);
elements.push_back(panel);
m_inputs.push_back({ input, nullptr });
comments.push_back({ prop, bg });
cnt += 0.12f;
break;
}
Expand All @@ -1507,12 +1562,15 @@ GuiModCategory::GuiModCategory(GuiModMenu* parent, Config* config, std::string c
bg->SetOffset(offset);
elements.push_back(bg);
BGui::Input* input = AddTextInput(name.c_str(), ExecuteBB::GAMEFONT_03, 0.43f, cnt + 0.05f, 0.18f, 0.025f);
input->SetText(std::to_string(prop->GetFloat()).c_str());
std::ostringstream stream;
stream << prop->GetFloat();
input->SetText(stream.str().c_str());
input->SetCallback([this, prop, input](CKDWORD) { prop->SetFloat((float) atof(input->GetText())); });
elements.push_back(input);
BGui::Panel* panel = AddPanel(name.c_str(), VxColor(0, 0, 0, 110), 0.43f, cnt + 0.05f, 0.18f, 0.025f);
elements.push_back(panel);
m_inputs.push_back({ input, nullptr });
comments.push_back({ prop, bg });
cnt += 0.12f;
break;
}
Expand All @@ -1523,6 +1581,7 @@ GuiModCategory::GuiModCategory(GuiModMenu* parent, Config* config, std::string c
elements.push_back(key.first);
elements.push_back(key.second);
m_inputs.push_back({ key.second, nullptr });
comments.push_back({ prop, key.first });
cnt += 0.06f;
break;
}
Expand All @@ -1540,6 +1599,7 @@ GuiModCategory::GuiModCategory(GuiModMenu* parent, Config* config, std::string c
elements.push_back(yesno.first);
elements.push_back(yesno.second);
m_inputs.push_back(yesno);
comments.push_back({ prop, bg });
cnt += 0.12f;
break;
}
Expand All @@ -1550,18 +1610,54 @@ GuiModCategory::GuiModCategory(GuiModMenu* parent, Config* config, std::string c
page++;
m_elements.push_back(elements);
elements.clear();
m_comments.push_back(comments);
comments.clear();
}
}

if (cnt > 0.25f) {
m_elements.push_back(elements);
m_comments.push_back(comments);
}

m_maxpage = m_elements.size();

SetVisible(false);
}

void GuiModCategory::Process() {
bool show_cmt = false;
if (m_curpage >= 0 && m_curpage < m_comments.size()) {
CKRenderContext* rc = ModLoader::m_instance->GetRenderContext();
InputHook* input = ModLoader::m_instance->GetInputManager();
Vx2DVector mousePos;
input->GetMousePosition(mousePos, false);
for (auto& pair : m_comments[m_curpage]) {
if (Intersect(mousePos.x / rc->GetWidth(), mousePos.y / rc->GetHeight(), pair.second)) {
if (m_curcmt != pair.first) {
m_comment_bg->SetVisible(true);
m_comment->SetVisible(true);
m_comment->SetText(pair.first->GetComment());
m_curcmt = pair.first;
}

show_cmt = true;
break;
}
}
}

if (!show_cmt) {
if (m_curcmt != nullptr) {
m_comment_bg->SetVisible(false);
m_comment->SetVisible(false);
m_curcmt = nullptr;
}
}

Gui::Process();
}

void GuiModCategory::SetVisible(bool visible) {
Gui::SetVisible(visible);
if (visible) {
Expand All @@ -1573,7 +1669,12 @@ void GuiModCategory::SetVisible(bool visible) {
switch (prop->GetType()) {
case IProperty::STRING: reinterpret_cast<BGui::Input*>(input.first)->SetText(prop->GetString()); break;
case IProperty::INTEGER: reinterpret_cast<BGui::Input*>(input.first)->SetText(std::to_string(prop->GetInteger()).c_str()); break;
case IProperty::FLOAT: reinterpret_cast<BGui::Input*>(input.first)->SetText(std::to_string(prop->GetFloat()).c_str()); break;
case IProperty::FLOAT: {
std::ostringstream stream;
stream << prop->GetFloat();
reinterpret_cast<BGui::Input*>(input.first)->SetText(stream.str().c_str());
break;
}
case IProperty::KEY: reinterpret_cast<BGui::KeyInput*>(input.first)->SetKey(prop->GetKey()); break;
case IProperty::BOOLEAN: {
reinterpret_cast<BGui::Button*>(input.first)->SetActive(prop->GetBoolean());
Expand All @@ -1583,6 +1684,10 @@ void GuiModCategory::SetVisible(bool visible) {
}
}

m_comment_bg->SetVisible(false);
m_comment->SetVisible(false);
m_curcmt = nullptr;

SetPage(m_curpage);
}
}
Expand Down
12 changes: 12 additions & 0 deletions BMLMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class GuiModMenu : public GuiList {
public:
GuiModMenu(IMod* mod);

virtual void Process() override;
virtual void SetVisible(bool visible) override;
virtual BGui::Button* CreateButton(int index) override;
virtual std::string GetButtonText(int index) override;
virtual BGui::Gui* CreateSubGui(int index) override;
Expand All @@ -58,6 +60,10 @@ class GuiModMenu : public GuiList {
private:
Config* m_config;
std::vector<std::string> m_categories;

BGui::Panel* m_comment_bg;
BGui::Label* m_comment;
int m_curcmt = -1;
};

class GuiCustomMap : public GuiList {
Expand Down Expand Up @@ -91,6 +97,7 @@ class GuiModCategory : public BGui::Gui {
public:
GuiModCategory(GuiModMenu* parent, Config* config, std::string category);

virtual void Process() override;
virtual void SetVisible(bool visible) override;
void SetPage(int page);
void PreviousPage() { if (m_curpage > 0) SetPage(m_curpage - 1); }
Expand All @@ -110,6 +117,11 @@ class GuiModCategory : public BGui::Gui {
Config* m_config;
std::string m_category;
BGui::Button* m_exit;

std::vector<std::vector<std::pair<Property*, BGui::Element*>>> m_comments;
BGui::Panel* m_comment_bg;
BGui::Label* m_comment;
Property* m_curcmt = nullptr;
};

class ScreenModeHook : public BGui::Gui {
Expand Down
2 changes: 1 addition & 1 deletion BuildVer.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define BML_MAJOR_VER 0
#define BML_MINOR_VER 3
#define BML_BUILD_VER 35
#define BML_BUILD_VER 37
4 changes: 4 additions & 0 deletions Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ Property* Config::Category::GetProperty(CKSTRING name) {
return props.back();
}

CKSTRING Config::GetCategoryComment(CKSTRING category) {
return GetCategory(category).comment.c_str();
}

void Config::SetCategoryComment(CKSTRING category, CKSTRING comment) {
GetCategory(category).comment = comment;
}
Expand Down
1 change: 1 addition & 0 deletions Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Config : public IConfig {

virtual IProperty* GetProperty(CKSTRING category, CKSTRING key) override;

CKSTRING GetCategoryComment(CKSTRING category);
virtual void SetCategoryComment(CKSTRING category, CKSTRING comment) override;

void Load();
Expand Down
8 changes: 4 additions & 4 deletions Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ namespace BGui {
if (key == CK_MOUSEBUTTON_LEFT) {
bool success = false;
for (Button* button : m_buttons) {
if (intersect(x, y, button)) {
if (Intersect(x, y, button)) {
button->InvokeCallback();
success = true;
}
Expand All @@ -671,7 +671,7 @@ namespace BGui {
else {
SetFocus(nullptr);
for (Input* input : m_inputs) {
if (intersect(x, y, input)) {
if (Intersect(x, y, input)) {
SetFocus(input);
success = true;
}
Expand All @@ -693,7 +693,7 @@ namespace BGui {
void Gui::OnMouseMove(float x, float y, float lx, float ly) {
}

bool Gui::intersect(float x, float y, Element* element) {
bool Gui::Intersect(float x, float y, Element* element) {
Vx2DVector pos = element->GetPosition(), size = element->GetSize();
return element->IsVisible() && x >= pos.x && x <= pos.x + size.x && y >= pos.y && y <= pos.y + size.y;
}
Expand Down Expand Up @@ -904,7 +904,7 @@ namespace BGui {
for (Button* button : m_buttons)
button->OnMouseLeave();
for (Button* button : m_buttons)
if (intersect(mousePos.x / rc->GetWidth(), mousePos.y / rc->GetHeight(), button)) button->OnMouseEnter();
if (Intersect(mousePos.x / rc->GetWidth(), mousePos.y / rc->GetHeight(), button)) button->OnMouseEnter();

VxVector relPos;
input->GetMouseRelativePosition(relPos);
Expand Down
3 changes: 1 addition & 2 deletions Gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ namespace BGui {
bool CanBeBlocked();
void SetCanBeBlocked(bool block);
void SetFocus(Input* input);
bool Intersect(float x, float y, Element* element);

static void InitMaterials();

Expand All @@ -211,8 +212,6 @@ namespace BGui {
Button* m_back = nullptr;
bool m_block = true;
int m_width = 0, m_height = 0;

bool intersect(float x, float y, Element* element);
};
}

Expand Down

0 comments on commit db823ba

Please sign in to comment.