feat: add summon menu
This commit is contained in:
82
src/render/gui/SummonMenu.cpp
Normal file
82
src/render/gui/SummonMenu.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include "render/gui/SummonMenu.h"
|
||||
#include "game/client/Client.h"
|
||||
#include "render/gui/imgui/imgui.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
SummonMenu::SummonMenu(client::Client* client) : GuiWidget(client), m_MenuOpened(true){
|
||||
m_Values.fill(0);
|
||||
}
|
||||
|
||||
void SummonMenu::render(){
|
||||
if (m_MenuOpened) {
|
||||
ImGui::Begin("Summon", &m_MenuOpened);
|
||||
ImTextureID my_tex_id = ImGui::GetIO().Fonts->TexID;
|
||||
for (int i = 0; i < m_MobTypeCount / 2; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
ImGui::Image(my_tex_id, ImVec2(100, 100));
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::PushItemWidth(m_ImageWidth);
|
||||
for (int i = 0; i < m_MobTypeCount / 2; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
if (ImGui::InputInt("", m_Values.data() + i, 1, 10)) {
|
||||
setSummonMax(i);
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::PopItemWidth();
|
||||
ImGui::Separator();
|
||||
for (int i = m_MobTypeCount / 2; i < m_MobTypeCount; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
ImGui::Image(my_tex_id, ImVec2(100, 100));
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::PushItemWidth(m_ImageWidth);
|
||||
for (int i = m_MobTypeCount / 2; i < m_MobTypeCount; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
if (ImGui::InputInt("", m_Values.data() + i, 1, m_MobTypeCount)) {
|
||||
setSummonMax(i);
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::PopItemWidth();
|
||||
if (ImGui::Button("Send")) {
|
||||
std::vector<protocol::MobSend> mobSent;
|
||||
protocol::MobSend mobSend;
|
||||
for(int i = 0; i < m_MobTypeCount; i++){
|
||||
if(m_Values[i] != 0){
|
||||
mobSend.mobCount = m_Values[i];
|
||||
mobSend.mobLevel = 1; // TODO: add mob levels
|
||||
mobSend.mobType = td::game::MobType(i);
|
||||
mobSent.push_back(mobSend);
|
||||
}
|
||||
}
|
||||
m_Client->sendMobs(mobSent);
|
||||
m_Values.fill(0);
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
}
|
||||
|
||||
void SummonMenu::setSummonMax(int valueIndex){
|
||||
int& value = m_Values[valueIndex];
|
||||
value = std::max(0, value);
|
||||
value = std::min(12, value);
|
||||
int total = 0;
|
||||
for (std::size_t i = 0; i < m_Values.size(); i++) {
|
||||
total += m_Values[i];
|
||||
}
|
||||
if (total == 13) // if the total is greater than the maximum, we substract the value
|
||||
value--;
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
@@ -18,6 +18,9 @@
|
||||
#include "render/Renderer.h"
|
||||
#include "network/Network.h"
|
||||
|
||||
#include "render/gui/GuiManager.h"
|
||||
#include "render/gui/SummonMenu.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
@@ -28,9 +31,14 @@ static SDL_GLContext gl_context;
|
||||
static std::unique_ptr<td::client::Client> client;
|
||||
static std::thread* serverThread;
|
||||
static td::render::Renderer* renderer;
|
||||
static td::gui::GuiManager guiManager;
|
||||
|
||||
bool serverShouldStop = false;
|
||||
|
||||
void initWidgets(){
|
||||
guiManager.addWidgets(std::make_shared<td::gui::SummonMenu>(client.get()));
|
||||
}
|
||||
|
||||
void init(SDL_Window* sdl_window, SDL_GLContext sdlContext, td::render::Renderer* render) {
|
||||
window = sdl_window;
|
||||
gl_context = sdlContext;
|
||||
@@ -44,6 +52,7 @@ void init(SDL_Window* sdl_window, SDL_GLContext sdlContext, td::render::Renderer
|
||||
ImGui::GetIO().Fonts->AddFontDefault(&c);
|
||||
renderer = render;
|
||||
client = std::make_unique<td::client::Client>(render);
|
||||
initWidgets();
|
||||
}
|
||||
|
||||
void beginFrame() {
|
||||
@@ -237,53 +246,7 @@ void capValues(int* values, int& value) {
|
||||
}
|
||||
|
||||
void renderSummonMenu() {
|
||||
static bool menu_open = true;
|
||||
if (menu_open) {
|
||||
|
||||
ImGui::Begin("Summon", &menu_open);
|
||||
static int width = 100;
|
||||
static int values[16]{ 0 };
|
||||
ImTextureID my_tex_id = ImGui::GetIO().Fonts->TexID;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
ImGui::Image(my_tex_id, ImVec2(100, 100));
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::PushItemWidth(width);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
if (ImGui::InputInt("", values + i, 1, 10)) {
|
||||
capValues(values, values[i]);
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::PopItemWidth();
|
||||
ImGui::Separator();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
ImGui::Image(my_tex_id, ImVec2(100, 100));
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::PushItemWidth(width);
|
||||
for (int i = 8; i < 16; i++) {
|
||||
ImGui::SameLine();
|
||||
ImGui::PushID(i);
|
||||
if (ImGui::InputInt("", values + i, 1, 10)) {
|
||||
capValues(values, values[i]);
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::PopItemWidth();
|
||||
if (ImGui::Button("Send")) {
|
||||
std::cout << "Sending Troops ...\n";
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void renderGame() {
|
||||
@@ -303,7 +266,7 @@ void renderGame() {
|
||||
showTPS();
|
||||
showStats();
|
||||
showPlayers();
|
||||
renderSummonMenu();
|
||||
guiManager.renderWidgets();
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user