diff --git a/include/render/WorldRenderer.h b/include/render/WorldRenderer.h index 9c2a70b..bbdc38d 100644 --- a/include/render/WorldRenderer.h +++ b/include/render/WorldRenderer.h @@ -6,6 +6,7 @@ #include "render/VertexCache.h" #include "render/gui/TowerPlacePopup.h" +#include "render/gui/TowerUpgradePopup.h" #include "render/gui/MobTooltip.h" #include "render/gui/CastleTooltip.h" @@ -38,6 +39,7 @@ private: VertexCache m_TowersCache; std::unique_ptr m_TowerPlacePopup; + std::unique_ptr m_TowerUpgradePopup; std::unique_ptr m_MobTooltip; std::unique_ptr m_CastleTooltip; public: @@ -67,7 +69,6 @@ private: void RenderMobs() const; void RenderTileSelect() const; void RenderPopups(); - void RenderTowerUpgradePopup(); void RenderMobTooltip() const; void RenderCastleTooltip() const; void DetectClick(); diff --git a/include/render/gui/TowerUpgradePopup.h b/include/render/gui/TowerUpgradePopup.h new file mode 100644 index 0000000..3031d3c --- /dev/null +++ b/include/render/gui/TowerUpgradePopup.h @@ -0,0 +1,32 @@ +#pragma once + +#include "GuiWidget.h" + +#include "Defines.h" + +namespace td { +namespace gui { + +class TowerUpgradePopup : public GuiWidget { +private: + Vec2f m_ClickWorldPos; + bool m_ShouldBeClosed; + bool m_Opened; +public: + TowerUpgradePopup(client::Client* client); + + virtual void Render(); + + void SetClickPos(const Vec2f& worldPos); + + bool IsPopupOpened(); +private: + static constexpr float m_TowerPopupTileWidth = 200.0f; + static constexpr float m_TowerPopupTileHeight = 200.0f; + + static constexpr float m_PlaceTowerButtonWidth = 150.0f; + static constexpr float m_PlaceTowerButtonHeight = 35.0f; +}; + +} // namespace gui +} // namespace td diff --git a/src/render/WorldRenderer.cpp b/src/render/WorldRenderer.cpp index be03ebf..b822fc5 100644 --- a/src/render/WorldRenderer.cpp +++ b/src/render/WorldRenderer.cpp @@ -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(m_Client->GetClient()); + m_TowerUpgradePopup = std::make_unique(m_Client->GetClient()); m_MobTooltip = std::make_unique(m_Client->GetClient()); m_CastleTooltip = std::make_unique(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()) { diff --git a/src/render/gui/TowerUpgradePopup.cpp b/src/render/gui/TowerUpgradePopup.cpp new file mode 100644 index 0000000..16a3392 --- /dev/null +++ b/src/render/gui/TowerUpgradePopup.cpp @@ -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 \ No newline at end of file