-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
OSS13 Client/Sources/Graphics/UI/Widget/SpawnWindow.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include "SpawnWindow.h" | ||
|
||
#include <imgui.h> | ||
#include <imgui-SFML.h> | ||
#include <imgui_stdlib.h> | ||
#include <imgui_internal.h> | ||
#include <imgui_extended.h> | ||
|
||
#include <Client.hpp> | ||
#include <Network.hpp> | ||
#include <Graphics/Sprite.hpp> | ||
|
||
#include <Shared/Network/Protocol/ClientToServer/Commands.h> | ||
|
||
|
||
void SpawnWindow::Update(sf::Time timeElapsed) { | ||
std::unique_lock<std::mutex> lock(guard); | ||
|
||
ImGui::SetNextWindowPos(ImVec2(60, 60), ImGuiCond_Once); | ||
ImGui::SetNextWindowSize(ImVec2(300, 300), ImGuiCond_Once); | ||
|
||
if (!ImGui::Begin("Object Spawner")) { | ||
ImGui::End(); | ||
return; | ||
} | ||
|
||
drawHeader(); | ||
drawBody(); | ||
|
||
ImGui::End(); | ||
} | ||
|
||
void SpawnWindow::UpdateTypes(std::vector<network::protocol::ObjectType> &&types) { | ||
std::unique_lock<std::mutex> lock(guard); | ||
|
||
this->types = std::forward<std::vector<network::protocol::ObjectType>>(types); | ||
noQueriesYet = false; | ||
} | ||
|
||
void searchTypesAndDropBuffer(std::string &searchBuffer) { | ||
auto command = std::make_unique<network::protocol::client::SpawnWindowSearchCommand>(); | ||
std::swap(command->searchBuffer, searchBuffer); | ||
Connection::commandQueue.Push(command.release()); | ||
} | ||
|
||
void drawHeaderInput(std::string &searchBuffer) { | ||
if (ImGui::InputTextWithHint("", "all object types", &searchBuffer, ImGuiInputTextFlags_EnterReturnsTrue)) { | ||
searchTypesAndDropBuffer(searchBuffer); | ||
} | ||
} | ||
|
||
void drawHeaderSearchButton(std::string &searchBuffer) { | ||
ImGui::SameLine(); | ||
if (ImGui::Button("Search")) { | ||
searchTypesAndDropBuffer(searchBuffer); | ||
} | ||
} | ||
|
||
void SpawnWindow::drawHeader() { | ||
drawHeaderInput(searchBuffer); | ||
drawHeaderSearchButton(searchBuffer); | ||
} | ||
|
||
void drawTypesListItemTooltip(const network::protocol::ObjectType &type) { | ||
if (type.sprite) { | ||
auto sprite = CC::Get()->RM.CreateSprite(type.sprite); | ||
ImGui::Image(sprite.GetSfmlSprite()); | ||
ImGui::SameLine(); | ||
} | ||
|
||
ImGui::BeginGroup(); | ||
ImGui::Text(type.name.c_str()); | ||
ImGui::Text(type.typeKey.c_str()); | ||
ImGui::Text(type.description.c_str()); | ||
ImGui::EndGroup(); | ||
} | ||
|
||
void drawTypesListItem(const network::protocol::ObjectType &type, bool &selected) { | ||
ImGui::Selectable(type.typeKey.c_str(), &selected); | ||
|
||
if (ImGui::IsItemHovered()) { | ||
ImGui::BeginTooltip(); | ||
drawTypesListItemTooltip(type); | ||
ImGui::EndTooltip(); | ||
} | ||
} | ||
|
||
void drawTypesListEmptySearchResultText() { | ||
ImGui::Text("No types were found!"); | ||
} | ||
|
||
void drawTypesListContent(const std::vector<network::protocol::ObjectType> &types, size_t &selectedIndex) { | ||
size_t counter = 0; | ||
for (auto &type : types) { | ||
counter++; | ||
bool selected = (counter == selectedIndex); | ||
drawTypesListItem(type, selected); | ||
if (selected) | ||
selectedIndex = counter; | ||
} | ||
} | ||
|
||
void SpawnWindow::drawTypesList() { | ||
const float footerHeightToReserve = ImGui::GetFrameHeightWithSpacing(); | ||
ImGui::BeginChild("types list", ImVec2(0, -footerHeightToReserve), true); | ||
|
||
if (!types.size() && !noQueriesYet) | ||
drawTypesListEmptySearchResultText(); | ||
else | ||
drawTypesListContent(types, selectedIndex); | ||
|
||
ImGui::EndChild(); | ||
} | ||
|
||
void spawnObject(const std::string &typeKey) { | ||
auto command = std::make_unique<network::protocol::client::SpawnWindowSpawnCommand>(); | ||
command->typeKey = typeKey; | ||
Connection::commandQueue.Push(command.release()); | ||
} | ||
|
||
void SpawnWindow::drawSpawnButton() { | ||
bool disabled = (selectedIndex == 0); | ||
if (ImGui::Button("Spawn", disabled)) { | ||
spawnObject(types[selectedIndex - 1].typeKey); | ||
} | ||
} | ||
|
||
void SpawnWindow::drawBody() { | ||
drawTypesList(); | ||
drawSpawnButton(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <mutex> | ||
|
||
#include <Graphics/UI/Widget/ImGuiWidget.h> | ||
|
||
#include <Shared/Network/Protocol/ServerToClient/WorldInfo.h> | ||
|
||
class SpawnWindow : public ImGuiWidget { | ||
public: | ||
void Update(sf::Time timeElapsed) final; | ||
|
||
void UpdateTypes(std::vector<network::protocol::ObjectType> &&types); | ||
|
||
private: | ||
void drawHeader(); | ||
void drawBody(); | ||
void drawTypesList(); | ||
void drawSpawnButton(); | ||
|
||
private: | ||
std::vector<network::protocol::ObjectType> types; | ||
size_t selectedIndex{0}; | ||
std::string searchBuffer; | ||
std::mutex guard; | ||
bool noQueriesYet{true}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters