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

@@ -6,6 +6,7 @@
#include "render/VertexCache.h" #include "render/VertexCache.h"
#include "render/gui/TowerPlacePopup.h" #include "render/gui/TowerPlacePopup.h"
#include "render/gui/TowerUpgradePopup.h"
#include "render/gui/MobTooltip.h" #include "render/gui/MobTooltip.h"
#include "render/gui/CastleTooltip.h" #include "render/gui/CastleTooltip.h"
@@ -38,6 +39,7 @@ private:
VertexCache m_TowersCache; VertexCache m_TowersCache;
std::unique_ptr<gui::TowerPlacePopup> m_TowerPlacePopup; std::unique_ptr<gui::TowerPlacePopup> m_TowerPlacePopup;
std::unique_ptr<gui::TowerUpgradePopup> m_TowerUpgradePopup;
std::unique_ptr<gui::MobTooltip> m_MobTooltip; std::unique_ptr<gui::MobTooltip> m_MobTooltip;
std::unique_ptr<gui::CastleTooltip> m_CastleTooltip; std::unique_ptr<gui::CastleTooltip> m_CastleTooltip;
public: public:
@@ -67,7 +69,6 @@ private:
void RenderMobs() const; void RenderMobs() const;
void RenderTileSelect() const; void RenderTileSelect() const;
void RenderPopups(); void RenderPopups();
void RenderTowerUpgradePopup();
void RenderMobTooltip() const; void RenderMobTooltip() const;
void RenderCastleTooltip() const; void RenderCastleTooltip() const;
void DetectClick(); void DetectClick();

View File

@@ -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

View File

@@ -35,6 +35,7 @@ WorldRenderer::WorldRenderer(game::World* world, client::ClientGame* client) : m
m_Renderer->SetZoom(m_Zoom); m_Renderer->SetZoom(m_Zoom);
m_Renderer->SetCamMovement({}); m_Renderer->SetCamMovement({});
m_TowerPlacePopup = std::make_unique<gui::TowerPlacePopup>(m_Client->GetClient()); 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_MobTooltip = std::make_unique<gui::MobTooltip>(m_Client->GetClient());
m_CastleTooltip = std::make_unique<gui::CastleTooltip>(m_Client->GetClient()); m_CastleTooltip = std::make_unique<gui::CastleTooltip>(m_Client->GetClient());
m_Client->GetWorldClient().GetWorldNotifier().BindListener(this); m_Client->GetWorldClient().GetWorldNotifier().BindListener(this);
@@ -48,7 +49,7 @@ void WorldRenderer::Update() {
if (m_WorldVao == nullptr) if (m_WorldVao == nullptr)
return; return;
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
if (io.MouseDown[0] && !ImGui::IsAnyItemActive()) { if (io.MouseDown[0] && !ImGui::IsAnyItemActive() && !ImGui::IsAnyItemHovered()) {
ImVec2 mouseDelta = ImGui::GetIO().MouseDelta; ImVec2 mouseDelta = ImGui::GetIO().MouseDelta;
const float relativeX = mouseDelta.x / (float)Display::GetWindowWidth() * 2; const float relativeX = mouseDelta.x / (float)Display::GetWindowWidth() * 2;
const float relativeY = mouseDelta.y / (float)Display::GetWindowHeight() * 2; const float relativeY = mouseDelta.y / (float)Display::GetWindowHeight() * 2;
@@ -59,10 +60,8 @@ void WorldRenderer::Update() {
} }
UpdateCursorPos(); UpdateCursorPos();
if (ImGui::IsMouseClicked(0)) { if (ImGui::IsMouseClicked(0)) {
if (!m_PopupOpened) { if (!m_TowerUpgradePopup->IsPopupOpened()) {
m_HoldCursorPos = { io.MousePos.x, io.MousePos.y }; m_HoldCursorPos = { io.MousePos.x, io.MousePos.y };
} else {
m_PopupOpened = false;
} }
} }
@@ -111,7 +110,7 @@ void WorldRenderer::RenderTileSelect() const {
void WorldRenderer::RenderPopups() { void WorldRenderer::RenderPopups() {
m_TowerPlacePopup->Render(); m_TowerPlacePopup->Render();
RenderTowerUpgradePopup(); m_TowerUpgradePopup->Render();
} }
void WorldRenderer::Render() { void WorldRenderer::Render() {
@@ -159,6 +158,7 @@ void WorldRenderer::ChangeZoom(float zoomStep) {
void WorldRenderer::Click() { void WorldRenderer::Click() {
const game::TowerPtr tower = m_Client->GetWorld().GetTower(GetClickWorldPos()); const game::TowerPtr tower = m_Client->GetWorld().GetTower(GetClickWorldPos());
m_TowerPlacePopup->SetClickPos(GetClickWorldPos()); m_TowerPlacePopup->SetClickPos(GetClickWorldPos());
m_TowerUpgradePopup->SetClickPos(GetClickWorldPos());
if (tower != nullptr) { // there is a tower here if (tower != nullptr) { // there is a tower here
ImGui::OpenPopup("TowerUpgrade"); ImGui::OpenPopup("TowerUpgrade");
} else if (m_Client->GetWorld().CanPlaceLittleTower(GetClickWorldPos(), m_Client->GetPlayer()->GetID())) { } 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); 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() { void WorldRenderer::DetectClick() {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
if (ImGui::IsMouseReleased(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemFocused()) { 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