infinipaint/src/GridManager.cpp

50 lines
1.7 KiB
C++
Raw Normal View History

2025-07-06 20:43:53 +03:00
#include "GridManager.hpp"
#include "SharedTypes.hpp"
#include "World.hpp"
2025-09-17 17:17:43 +03:00
#include "MainProgram.hpp"
#include <algorithm>
2025-07-06 20:43:53 +03:00
2025-09-06 19:06:30 +03:00
GridManager::GridManager(World& w):
world(w) {}
2025-07-06 20:43:53 +03:00
2025-09-06 19:06:30 +03:00
void GridManager::init_client_callbacks() {
}
2025-07-06 20:43:53 +03:00
ServerClientID GridManager::add_default_grid(const std::string& newName) {
ServerClientID newID = world.get_new_id();
2025-09-06 19:06:30 +03:00
WorldGrid g;
g.name = newName;
g.color = color_mul_alpha(convert_vec4<Vector4f>(world.canvasTheme.toolFrontColor), 0.6f);
2025-09-17 17:17:43 +03:00
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;
2025-09-06 19:06:30 +03:00
changed = true;
return newID;
2025-08-01 10:58:45 +03:00
}
void GridManager::remove_grid(ServerClientID idToRemove) {
grids.erase(idToRemove);
2025-09-06 19:06:30 +03:00
changed = true;
2025-07-06 20:43:53 +03:00
}
const std::vector<ServerClientID>& GridManager::sorted_grid_ids() {
2025-09-06 19:06:30 +03:00
if(changed) {
sortedGridIDs.clear();
2025-09-06 19:06:30 +03:00
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);
});
2025-09-06 19:06:30 +03:00
changed = false;
2025-07-06 20:43:53 +03:00
}
return sortedGridIDs;
2025-09-06 19:06:30 +03:00
}
2025-07-06 20:43:53 +03:00
2025-09-06 19:06:30 +03:00
void GridManager::draw(SkCanvas* canvas, const DrawData& drawData) {
for(auto& [str, g] : grids)
g.draw(*this, canvas, drawData);
2025-07-06 20:43:53 +03:00
}