/*
* InfiniPaint
* Copyright (C) 2025-2026 Yousef Khadadeh
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#pragma once
#include "DrawingProgramLayerListItem.hpp"
#include "DrawingProgramLayerManagerGUI.hpp"
#include
#include
class DrawingProgramLayerManager {
private:
DrawingProgram& drawP;
friend class DrawingProgramLayerManagerGUI;
friend class DrawingProgramLayerListItem;
friend class DrawingProgramLayer;
friend class DrawingProgramLayerFolder;
NetworkingObjects::NetObjOwnerPtr layerTreeRoot;
NetworkingObjects::NetObjWeakPtr editingLayer;
void set_initial_editing_layer();
void add_undo_erase_components(const std::unordered_map>& eraseMap, bool newUndo = true);
void add_undo_place_components(DrawingProgramLayerListItem* parent, const std::vector& placeList, bool newUndo = true);
void erase_component_map(const std::unordered_map>& eraseMap, bool newUndo = true);
bool commitUpdateOnComponentInsert = true;
bool addToCacheOnComponentInsert = true;
public:
enum class LayerSelector {
ALL_VISIBLE_LAYERS,
LAYER_BEING_EDITED
};
DrawingProgramLayerManager(DrawingProgram& drawProg);
void server_init_no_file();
void draw(SkCanvas* canvas, const DrawData& drawData);
void write_components_server(cereal::PortableBinaryOutputArchive& a);
void read_components_client(cereal::PortableBinaryInputArchive& a);
bool is_a_layer_being_edited();
void scale_up(const WorldScalar& scaleUpAmount);
template void erase_component_container(const List& compsToErase, bool newUndo = true) {
if(!compsToErase.empty()) {
std::unordered_map> idsToEraseInSpecificLayers;
for(auto& c : compsToErase)
idsToEraseInSpecificLayers[c->obj->parentLayer].emplace_back(c->obj->objInfo);
for(auto& [parent, its] : idsToEraseInSpecificLayers) {
std::sort(its.begin(), its.end(), [](auto& a, auto& b) {
return a->pos < b->pos;
});
}
erase_component_map(idsToEraseInSpecificLayers, newUndo);
}
}
uint32_t edited_layer_component_count();
CanvasComponentContainer::ObjInfoIterator get_edited_layer_end_iterator();
void push_components_to(const std::vector& objs, bool beginIfTrueEndIfFalse);
bool component_passes_layer_selector(CanvasComponentContainer::ObjInfo* c, LayerSelector layerSelector);
void disable_add_to_cache_block(const std::function& toRun);
void disable_commit_update_block(const std::function& toRun);
void disable_add_to_cache_and_commit_update_block(const std::function& toRun);
void load_file(cereal::PortableBinaryInputArchive& a, VersionNumber version);
void save_file(cereal::PortableBinaryOutputArchive& a) const;
void get_used_resources(std::unordered_set& resourceSet);
bool layer_tree_root_exists();
const DrawingProgramLayerListItem& get_layer_root();
uint32_t total_component_count();
CanvasComponentContainer::ObjInfo* add_component_to_layer_being_edited(CanvasComponentContainer* newObj);
// BOTH add_many_components_to_layer CALLS REQUIRE A SORTED INSERT LIST
std::vector add_many_components_to_layer_being_edited(const std::vector>& newObjs);
std::vector add_many_components_to_layer(DrawingProgramLayerListItem* layer, const std::vector>& newObjs, bool newUndo = true);
std::vector get_flattened_component_list() const;
std::vector get_flattened_layer_list();
void add_undo_place_component(CanvasComponentContainer::ObjInfo* objInfo);
bool can_current_peer_edit_layer(const DrawingProgramLayerListItem* layer) const;
bool client_may_modify_component(const CanvasComponentContainer* comp) const;
void ensure_editing_layer_is_editable();
/** Host: recompute fog coverage and erase/reveal (+ resources) on clients. */
void refresh_fog_client_projection();
/** Coalesce frequent mesh edits; flushed once per frame in DrawingProgram::update. */
void mark_fog_client_projection_dirty();
void flush_fog_client_projection_if_dirty();
// NetObjIDs omitted from the last client projection (host only).
std::unordered_set clientHiddenComponentIds;
// Client inserts into locked/fog layers — erase after the NetObj update finishes.
void defer_reject_component(DrawingProgramLayerListItem* layer, NetworkingObjects::NetObjID compId);
void flush_deferred_rejects();
DrawingProgramLayerManagerGUI listGUI;
private:
std::vector> deferredRejectInserts;
bool fogClientProjectionDirty = false;
};