Skip to content

Commit

Permalink
[CLT] add(SpawnWindow)
Browse files Browse the repository at this point in the history
  • Loading branch information
Insineer committed Sep 21, 2019
1 parent 4d9c045 commit d79eadc
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 1 deletion.
2 changes: 2 additions & 0 deletions OSS13 Client/OSS13 Client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
<ClCompile Include="Sources\Graphics\UI\Widget\FormattedTextField.cpp" />
<ClCompile Include="Sources\Graphics\UI\Widget\ImGuiWidget.cpp" />
<ClCompile Include="Sources\Graphics\UI\Widget\Label.cpp" />
<ClCompile Include="Sources\Graphics\UI\Widget\SpawnWindow.cpp" />
<ClCompile Include="Sources\Graphics\UI\Widget\Widget.cpp" />
<ClCompile Include="Sources\Graphics\Window.cpp" />
<ClCompile Include="Sources\Network.cpp" />
Expand Down Expand Up @@ -183,6 +184,7 @@
<ClInclude Include="Sources\Graphics\UI\Widget\ImGuiWidget.h" />
<ClInclude Include="Sources\Graphics\UI\Widget\Label.hpp" />
<ClInclude Include="Sources\Graphics\UI\Widget\LogRecordsHolder.h" />
<ClInclude Include="Sources\Graphics\UI\Widget\SpawnWindow.h" />
<ClInclude Include="Sources\Graphics\UI\Widget\Widget.hpp" />
<ClInclude Include="Sources\Graphics\Window.hpp" />
<ClInclude Include="Sources\Network.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions OSS13 Client/OSS13 Client.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
<ClCompile Include="Sources\Graphics\TileGrid\ControlUI.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
<ClCompile Include="Sources\Graphics\UI\Widget\SpawnWindow.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Sources\Graphics\TileGrid\Block.hpp">
Expand Down Expand Up @@ -191,5 +194,8 @@
<ClInclude Include="Sources\Graphics\TileGrid\ControlUI.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Sources\Graphics\UI\Widget\SpawnWindow.h">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion OSS13 Client/Sources/Graphics/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ void Sprite::updateSpriteVariables() {

sfSprite.setTexture(*texture->GetSFMLTexture());
sfSprite.setTextureRect(rect);
}
}

const sf::Sprite &Sprite::GetSfmlSprite() const {
return sfSprite;
}
1 change: 1 addition & 0 deletions OSS13 Client/Sources/Graphics/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Sprite {
bool IsValid() const;
bool IsAnimated() const;
bool PixelTransparent(uf::vec2u pixel) const;
const sf::Sprite &GetSfmlSprite() const;

private:
mutable sf::Sprite sfSprite;
Expand Down
14 changes: 14 additions & 0 deletions OSS13 Client/Sources/Graphics/UI/UIModule/GameProcessUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Graphics/TileGrid/TileGrid.hpp>
#include <Graphics/TileGrid/Object.hpp>
#include <Graphics/UI/UI.hpp>
#include <Graphics/UI/Widget/SpawnWindow.h>
#include <Network.hpp>

#include <Shared/Global.hpp>
Expand Down Expand Up @@ -115,6 +116,19 @@ void GameProcessUI::HandleEvent(sf::Event event) {
UIModule::HandleEvent(event);
}

void GameProcessUI::OpenSpawnWindow() {
for (auto &widget : widgets)
if (dynamic_cast<SpawnWindow *>(widget.get()))
return;
widgets.push_back(std::make_unique<SpawnWindow>());
}

void GameProcessUI::UpdateSpawnWindow(std::vector<network::protocol::ObjectType> &&types) {
for (auto &widget : widgets)
if (auto *spawnWidget = dynamic_cast<SpawnWindow *>(widget.get()))
spawnWidget->UpdateTypes(std::forward<std::vector<network::protocol::ObjectType>>(types));
}

InfoLabel *GameProcessUI::GetInfoLabel() const { return infoLabel.get(); }
TileGrid *GameProcessUI::GetTileGrid() const { return tileGrid; }

Expand Down
5 changes: 5 additions & 0 deletions OSS13 Client/Sources/Graphics/UI/UIModule/GameProcessUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "Graphics/UI/Widget/FormattedTextField.hpp"
#include "UIModule.hpp"

#include <Shared/Network/Protocol/ServerToClient/WorldInfo.h>

class UI;
class TileGrid;

Expand All @@ -27,6 +29,9 @@ class GameProcessUI : public UIModule {
void Update(sf::Time timeElapsed) override final;
void HandleEvent(sf::Event event) override;

void OpenSpawnWindow();
void UpdateSpawnWindow(std::vector<network::protocol::ObjectType> &&types);

InfoLabel *GetInfoLabel() const;
TileGrid *GetTileGrid() const;

Expand Down
131 changes: 131 additions & 0 deletions OSS13 Client/Sources/Graphics/UI/Widget/SpawnWindow.cpp
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();
}
27 changes: 27 additions & 0 deletions OSS13 Client/Sources/Graphics/UI/Widget/SpawnWindow.h
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};
};
14 changes: 14 additions & 0 deletions OSS13 Client/Sources/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ bool Connection::parsePacket(Packet &packet) {
return true;
}

if (auto *command = dynamic_cast<server::OpenSpawnWindowCommand *>(p.get())) {
GameProcessUI *gameProcessUI = dynamic_cast<GameProcessUI *>(CC::Get()->GetWindow()->GetUI()->GetCurrentUIModule());
EXPECT(gameProcessUI);
gameProcessUI->OpenSpawnWindow();
return true;
}

if (auto *command = dynamic_cast<server::UpdateSpawnWindowCommand *>(p.get())) {
GameProcessUI *gameProcessUI = dynamic_cast<GameProcessUI *>(CC::Get()->GetWindow()->GetUI()->GetCurrentUIModule());
EXPECT(gameProcessUI);
gameProcessUI->UpdateSpawnWindow(std::forward<std::vector<network::protocol::ObjectType>>(command->types));
return true;
}

if (auto *command = dynamic_cast<server::UpdateWindowCommand *>(p.get())) {
UIModule *uiModule = CC::Get()->GetWindow()->GetUI()->GetCurrentUIModule();
EXPECT(uiModule);
Expand Down

0 comments on commit d79eadc

Please sign in to comment.