moved TowerUpgradePopop into its own file

This commit is contained in:
2023-01-02 15:56:20 +01:00
parent 512fb23d0e
commit 1200a6e087
4 changed files with 139 additions and 69 deletions

View File

@@ -35,6 +35,7 @@ WorldRenderer::WorldRenderer(game::World* world, client::ClientGame* client) : m
m_Renderer->SetZoom(m_Zoom);
m_Renderer->SetCamMovement({});
m_TowerPlacePopup = std::make_unique<gui::TowerPlacePopup>(m_Client->GetClient());
m_TowerUpgradePopup = std::make_unique<gui::TowerUpgradePopup>(m_Client->GetClient());
m_MobTooltip = std::make_unique<gui::MobTooltip>(m_Client->GetClient());
m_CastleTooltip = std::make_unique<gui::CastleTooltip>(m_Client->GetClient());
m_Client->GetWorldClient().GetWorldNotifier().BindListener(this);
@@ -48,7 +49,7 @@ void WorldRenderer::Update() {
if (m_WorldVao == nullptr)
return;
ImGuiIO& io = ImGui::GetIO();
if (io.MouseDown[0] && !ImGui::IsAnyItemActive()) {
if (io.MouseDown[0] && !ImGui::IsAnyItemActive() && !ImGui::IsAnyItemHovered()) {
ImVec2 mouseDelta = ImGui::GetIO().MouseDelta;
const float relativeX = mouseDelta.x / (float)Display::GetWindowWidth() * 2;
const float relativeY = mouseDelta.y / (float)Display::GetWindowHeight() * 2;
@@ -59,10 +60,8 @@ void WorldRenderer::Update() {
}
UpdateCursorPos();
if (ImGui::IsMouseClicked(0)) {
if (!m_PopupOpened) {
if (!m_TowerUpgradePopup->IsPopupOpened()) {
m_HoldCursorPos = { io.MousePos.x, io.MousePos.y };
} else {
m_PopupOpened = false;
}
}
@@ -111,7 +110,7 @@ void WorldRenderer::RenderTileSelect() const {
void WorldRenderer::RenderPopups() {
m_TowerPlacePopup->Render();
RenderTowerUpgradePopup();
m_TowerUpgradePopup->Render();
}
void WorldRenderer::Render() {
@@ -159,6 +158,7 @@ void WorldRenderer::ChangeZoom(float zoomStep) {
void WorldRenderer::Click() {
const game::TowerPtr tower = m_Client->GetWorld().GetTower(GetClickWorldPos());
m_TowerPlacePopup->SetClickPos(GetClickWorldPos());
m_TowerUpgradePopup->SetClickPos(GetClickWorldPos());
if (tower != nullptr) { // there is a tower here
ImGui::OpenPopup("TowerUpgrade");
} else if (m_Client->GetWorld().CanPlaceLittleTower(GetClickWorldPos(), m_Client->GetPlayer()->GetID())) {
@@ -171,69 +171,6 @@ void WorldRenderer::SetCamPos(float camX, float camY) {
m_Renderer->SetCamPos(m_CamPos);
}
void WorldRenderer::RenderTowerUpgradePopup() {
if (ImGui::BeginPopup("TowerUpgrade")) {
m_PopupOpened = true;
game::TowerPtr tower = m_Client->GetWorld().GetTower(GetClickWorldPos());
if (tower == nullptr) {
ImGui::EndPopup();
return;
}
ImGui::Text("Tower : %s", game::TowerFactory::GetTowerName(tower->GetType()).c_str());
for (int y = 0; y < 3; y++) { // path: 0 -> top 1 -> middle 2 -> bottom
for (int x = 0; x < 4; x++) { // level: 1, 2, 3, 4
if (x > 0)
ImGui::SameLine();
std::uint8_t currentLevel = x + 1;
game::TowerPath currentPath = game::TowerPath(y);
const game::TowerStats* towerStats = game::GetTowerStats(tower->GetType(), { currentLevel, currentPath });
game::TowerPath towerPath = tower->GetLevel().GetPath();
bool disabled = towerStats == nullptr;
int towerLevel = tower->GetLevel().GetLevel();
bool alreadyUpgraded = currentLevel <= towerLevel;
bool canUpgrade = (towerLevel + 1) == currentLevel;
if (canUpgrade && towerPath != game::TowerPath::Base) {
if (currentPath != towerPath) {
canUpgrade = false;
}
}
ImGui::PushID(x * 4 + y);
if (disabled) {
ImGui::BeginDisabled();
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::Button("", ImVec2(100, 100));
ImGui::PopStyleColor();
ImGui::EndDisabled();
} else if (alreadyUpgraded) {
ImGui::BeginDisabled();
ImGui::Button("Already", ImVec2(100, 100));
ImGui::EndDisabled();
} else if (canUpgrade) {
if (ImGui::Button("Upgrade", ImVec2(100, 100))) {
m_Client->GetClient()->UpgradeTower(tower->GetID(), { currentLevel, currentPath });
}
} else {
ImGui::BeginDisabled();
ImGui::Button("Locked", ImVec2(100, 100));
ImGui::EndDisabled();
}
ImGui::PopID();
}
}
ImGui::EndPopup();
}
}
void WorldRenderer::DetectClick() {
ImGuiIO& io = ImGui::GetIO();
if (ImGui::IsMouseReleased(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemFocused()) {

View File

@@ -0,0 +1,100 @@
#include "render/gui/TowerUpgradePopup.h"
#include "render/gui/imgui/imgui.h"
#include "game/Towers.h"
#include "game/client/Client.h"
namespace td {
namespace gui {
TowerUpgradePopup::TowerUpgradePopup(client::Client* client) : GuiWidget(client), m_ShouldBeClosed(false), m_Opened(false) {
}
void TowerUpgradePopup::SetClickPos(const Vec2f& worldPos) {
m_ClickWorldPos = worldPos;
}
bool TowerUpgradePopup::IsPopupOpened() {
return m_Opened;
}
void TowerUpgradePopup::Render() {
if (ImGui::BeginPopup("TowerUpgrade")) {
ImGui::BeginChild("TowerUpgradePopupChild", { 450, 350 });
if (m_ShouldBeClosed) {
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
return;
}
game::TowerPtr tower = m_Client->GetGame().GetWorld().GetTower(m_ClickWorldPos);
if (tower == nullptr) {
ImGui::EndPopup();
return;
}
ImGui::Text("Tower : %s", game::TowerFactory::GetTowerName(tower->GetType()).c_str());
for (int y = 0; y < 3; y++) { // path: 0 -> top 1 -> middle 2 -> bottom
for (int x = 0; x < 4; x++) { // level: 1, 2, 3, 4
if (x > 0)
ImGui::SameLine();
std::uint8_t currentLevel = x + 1;
game::TowerPath currentPath = game::TowerPath(y);
const game::TowerStats* towerStats = game::GetTowerStats(tower->GetType(), { currentLevel, currentPath });
game::TowerPath towerPath = tower->GetLevel().GetPath();
bool disabled = towerStats == nullptr;
int towerLevel = tower->GetLevel().GetLevel();
bool alreadyUpgraded = currentLevel <= towerLevel;
bool canUpgrade = (towerLevel + 1) == currentLevel;
if (canUpgrade && towerPath != game::TowerPath::Base) {
if (currentPath != towerPath) {
canUpgrade = false;
}
}
ImGui::PushID(x * 4 + y);
if (disabled) {
ImGui::BeginDisabled();
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::Button("", ImVec2(100, 100));
ImGui::PopStyleColor();
ImGui::EndDisabled();
} else if (alreadyUpgraded) {
ImGui::BeginDisabled();
ImGui::Button("Already", ImVec2(100, 100));
ImGui::EndDisabled();
} else if (canUpgrade) {
if (ImGui::Button("Upgrade", ImVec2(100, 100))) {
m_Client->UpgradeTower(tower->GetID(), { currentLevel, currentPath });
}
} else {
ImGui::BeginDisabled();
ImGui::Button("Locked", ImVec2(100, 100));
ImGui::EndDisabled();
}
ImGui::PopID();
}
}
ImGui::EndChild();
ImGui::EndPopup();
m_Opened = true;
return;
}
m_Opened = false;
}
} // namespace gui
} // namespace td