GridManager now works on net objects
This commit is contained in:
parent
67507a9a39
commit
ceafbf8bb6
20 changed files with 214 additions and 222 deletions
|
|
@ -1,4 +1,5 @@
|
|||
#include "GridManager.hpp"
|
||||
#include "Helpers/Networking/NetLibrary.hpp"
|
||||
#include "Server/CommandList.hpp"
|
||||
#include "SharedTypes.hpp"
|
||||
#include "World.hpp"
|
||||
|
|
@ -6,166 +7,87 @@
|
|||
#include <algorithm>
|
||||
|
||||
GridManager::GridManager(World& w):
|
||||
world(w) {}
|
||||
|
||||
void GridManager::init_client_callbacks() {
|
||||
world.con.client_add_recv_callback(CLIENT_SET_GRID, [&](cereal::PortableBinaryInputArchive& message) {
|
||||
ServerClientID gID;
|
||||
message(gID);
|
||||
auto& g = grids[gID];
|
||||
message(g);
|
||||
changed = true;
|
||||
});
|
||||
world.con.client_add_recv_callback(CLIENT_REMOVE_GRID, [&](cereal::PortableBinaryInputArchive& message) {
|
||||
ServerClientID gID;
|
||||
message(gID);
|
||||
grids.erase(gID);
|
||||
changed = true;
|
||||
});
|
||||
world(w) {
|
||||
if(w.netObjMan.is_server())
|
||||
grids = w.netObjMan.make_obj<NetworkingObjects::NetObjOrderedList<WorldGrid>>();
|
||||
}
|
||||
|
||||
ServerClientID GridManager::add_default_grid(const std::string& newName) {
|
||||
ServerClientID newID = world.get_new_id();
|
||||
WorldGrid g;
|
||||
g.name = newName;
|
||||
g.color = color_mul_alpha(convert_vec4<Vector4f>(world.canvasTheme.toolFrontColor), 0.4f);
|
||||
g.size = world.drawData.cam.c.inverseScale * WorldScalar(WorldGrid::GRID_UNIT_PIXEL_SIZE);
|
||||
g.offset = world.drawData.cam.c.pos + world.drawData.cam.c.dir_from_space(world.main.window.size.cast<float>() * 0.5f);
|
||||
grids[newID] = g;
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_SET_GRID, newID, g);
|
||||
void GridManager::add_default_grid(const std::string& newName) {
|
||||
if(grids) {
|
||||
WorldGrid g;
|
||||
g.name = newName;
|
||||
g.color = color_mul_alpha(convert_vec4<Vector4f>(world.canvasTheme.toolFrontColor), 0.4f);
|
||||
g.size = world.drawData.cam.c.inverseScale * WorldScalar(WorldGrid::GRID_UNIT_PIXEL_SIZE);
|
||||
g.offset = world.drawData.cam.c.pos + world.drawData.cam.c.dir_from_space(world.main.window.size.cast<float>() * 0.5f);
|
||||
NetworkingObjects::NetObjPtr<WorldGrid> gridPtr = grids->emplace_back_direct(grids, g);
|
||||
|
||||
changed = true;
|
||||
|
||||
world.undo.push({[&world = world, &changed = changed, &grids = grids, newID]() {
|
||||
grids.erase(newID);
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_REMOVE_GRID, newID);
|
||||
changed = true;
|
||||
return true;
|
||||
},
|
||||
[&world = world, &changed = changed, &grids = grids, g, newID]() {
|
||||
grids[newID] = g;
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_SET_GRID, newID, g);
|
||||
changed = true;
|
||||
return true;
|
||||
}});
|
||||
|
||||
return newID;
|
||||
}
|
||||
|
||||
void GridManager::send_grid_info(WorldGrid oldGrid, ServerClientID gridID) {
|
||||
auto gridFoundIt = grids.find(gridID);
|
||||
if(gridFoundIt != grids.end()) {
|
||||
WorldGrid& g = gridFoundIt->second;
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_SET_GRID, gridID, g);
|
||||
|
||||
world.undo.push({[&world = world, &changed = changed, &grids = grids, gridID, oldGrid]() {
|
||||
auto gridFoundIt = grids.find(gridID);
|
||||
if(gridFoundIt == grids.end())
|
||||
return false;
|
||||
gridFoundIt->second = oldGrid;
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_SET_GRID, gridID, oldGrid);
|
||||
changed = true;
|
||||
uint32_t pos = grids->size() - 1;
|
||||
world.undo.push({[&grids = grids, gridPtr](){
|
||||
grids->erase(grids, gridPtr);
|
||||
return true;
|
||||
},
|
||||
[&world = world, &changed = changed, &grids = grids, gridID, g]() {
|
||||
auto gridFoundIt = grids.find(gridID);
|
||||
if(gridFoundIt == grids.end())
|
||||
return false;
|
||||
gridFoundIt->second = g;
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_SET_GRID, gridID, g);
|
||||
changed = true;
|
||||
[&grids = grids, gridPtr, pos]() {
|
||||
grids->insert_and_send_create(grids, pos, gridPtr);
|
||||
return true;
|
||||
}});
|
||||
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
void GridManager::remove_grid(ServerClientID idToRemove) {
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_REMOVE_GRID, idToRemove);
|
||||
changed = true;
|
||||
auto it = grids.find(idToRemove);
|
||||
if(it == grids.end())
|
||||
return;
|
||||
|
||||
WorldGrid g = it->second;
|
||||
grids.erase(it);
|
||||
|
||||
world.undo.push({[&world = world, &changed = changed, &grids = grids, idToRemove, g]() {
|
||||
grids[idToRemove] = g;
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_SET_GRID, idToRemove, g);
|
||||
changed = true;
|
||||
return true;
|
||||
},
|
||||
[&world = world, &changed = changed, &grids = grids, idToRemove]() {
|
||||
grids.erase(idToRemove);
|
||||
world.con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_REMOVE_GRID, idToRemove);
|
||||
changed = true;
|
||||
return true;
|
||||
}});
|
||||
}
|
||||
|
||||
const std::vector<ServerClientID>& GridManager::sorted_grid_ids() {
|
||||
if(changed) {
|
||||
sortedGridIDs.clear();
|
||||
for(auto& [k, v] : grids)
|
||||
sortedGridIDs.emplace_back(k);
|
||||
std::sort(sortedGridIDs.begin(), sortedGridIDs.end(), [&](ServerClientID a, ServerClientID b) {
|
||||
std::string nameA = grids[a].get_display_name();
|
||||
std::string nameB = grids[b].get_display_name();
|
||||
bool namesEqual = nameA == nameB;
|
||||
return (!namesEqual && std::lexicographical_compare(nameA.begin(), nameA.end(), nameB.begin(), nameB.end())) || (namesEqual && a < b);
|
||||
});
|
||||
changed = false;
|
||||
void GridManager::remove_grid(uint32_t indexToRemove) {
|
||||
if(grids) {
|
||||
NetworkingObjects::NetObjPtr<WorldGrid> gridPtr = grids->erase(grids, indexToRemove);
|
||||
world.undo.push({[&grids = grids, gridPtr, indexToRemove]() {
|
||||
grids->insert_and_send_create(grids, indexToRemove, gridPtr);
|
||||
return true;
|
||||
},
|
||||
[&grids = grids, gridPtr](){
|
||||
grids->erase(grids, gridPtr);
|
||||
return true;
|
||||
}});
|
||||
}
|
||||
return sortedGridIDs;
|
||||
}
|
||||
|
||||
ClientPortionID GridManager::get_max_id(ServerPortionID serverID) const {
|
||||
ClientPortionID maxClientID = 0;
|
||||
for(auto& p : grids) {
|
||||
if(p.first.first == serverID)
|
||||
maxClientID = std::max(maxClientID, p.first.second);
|
||||
}
|
||||
return maxClientID;
|
||||
}
|
||||
|
||||
void GridManager::draw_back(SkCanvas* canvas, const DrawData& drawData) {
|
||||
auto& sortedGridIDS = sorted_grid_ids();
|
||||
for(ServerClientID id : sortedGridIDS) {
|
||||
if(!grids[id].displayInFront)
|
||||
grids[id].draw(*this, canvas, drawData);
|
||||
if(grids) {
|
||||
for(uint32_t i = 0; i < grids->size(); i++) {
|
||||
if(!grids->at(i)->displayInFront)
|
||||
grids->at(i)->draw(*this, canvas, drawData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GridManager::draw_front(SkCanvas* canvas, const DrawData& drawData) {
|
||||
auto& sortedGridIDS = sorted_grid_ids();
|
||||
for(ServerClientID id : sortedGridIDS) {
|
||||
if(grids[id].displayInFront)
|
||||
grids[id].draw(*this, canvas, drawData);
|
||||
if(grids) {
|
||||
for(uint32_t i = 0; i < grids->size(); i++) {
|
||||
if(grids->at(i)->displayInFront)
|
||||
grids->at(i)->draw(*this, canvas, drawData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GridManager::draw_coordinates(SkCanvas* canvas, const DrawData& drawData) {
|
||||
auto& sortedGridIDS = sorted_grid_ids();
|
||||
Vector2f axisOffset{0.0f, 0.0f};
|
||||
for(ServerClientID id : sortedGridIDS) {
|
||||
auto& g = grids[id];
|
||||
if(g.coordinatesWillBeDrawn && g.coordinatesAxisOnBounds) {
|
||||
grids[id].draw_coordinates(canvas, drawData, axisOffset);
|
||||
g.coordinatesWillBeDrawn = false;
|
||||
if(grids) {
|
||||
Vector2f axisOffset{0.0f, 0.0f};
|
||||
for(uint32_t i = 0; i < grids->size(); i++) {
|
||||
auto& g = grids->at(i);
|
||||
if(g->coordinatesWillBeDrawn && g->coordinatesAxisOnBounds) {
|
||||
g->draw_coordinates(canvas, drawData, axisOffset);
|
||||
g->coordinatesWillBeDrawn = false;
|
||||
}
|
||||
}
|
||||
for(uint32_t i = 0; i < grids->size(); i++) {
|
||||
auto& g = grids->at(i);
|
||||
if(g->coordinatesWillBeDrawn && !g->coordinatesAxisOnBounds)
|
||||
g->draw_coordinates(canvas, drawData, axisOffset);
|
||||
g->coordinatesWillBeDrawn = false;
|
||||
}
|
||||
}
|
||||
for(ServerClientID id : sortedGridIDS) {
|
||||
auto& g = grids[id];
|
||||
if(g.coordinatesWillBeDrawn && !g.coordinatesAxisOnBounds)
|
||||
grids[id].draw_coordinates(canvas, drawData, axisOffset);
|
||||
g.coordinatesWillBeDrawn = false;
|
||||
}
|
||||
}
|
||||
|
||||
void GridManager::scale_up(const WorldScalar& scaleUpAmount) {
|
||||
for(auto& [id, g] : grids) {
|
||||
g.scale_up(scaleUpAmount);
|
||||
if(grids) {
|
||||
for(uint32_t i = 0; i < grids->size(); i++) {
|
||||
grids->at(i)->scale_up(scaleUpAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue