#include "LayerManagmentContext.h" BackgroundMode LayerManagementContext::Background = BackgroundMode::COLOR; float LayerManagementContext::BackgroundColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; std::vector LayerManagementContext::Layers; int LayerManagementContext::FinalWidth, LayerManagementContext::FinalHeight; LayerNode* LayerManagementContext::SelectedLayer; std::unordered_map LayerManagementContext::Registry; #include "../LayerNodes/PaintLayerNode.h" #include "../Tools/Tool.h" #include "../App.h" // App::History #include "../Commands/LayerManage.h" std::vector LayerManagementContext::ToIds() { std::vector ids; ids.reserve(Layers.size()); for (auto* l : Layers) ids.push_back(l->ID); return ids; } uint32_t LayerManagementContext::ToId(LayerNode* layer) { return layer->ID; } LayerNode* LayerManagementContext::FromId(uint32_t id) { return Registry.at(id); } void LayerManagementContext::SyncFromIds(const std::vector& ids) { Layers.clear(); for (uint32_t id : ids) Layers.push_back(FromId(id)); } void LayerManagementContext::Clear() { for (auto* l : Layers) { delete l; } Layers.clear(); Registry.clear(); SelectedLayer = nullptr; LayerNode::SetIdCounter(0); } void LayerManagementContext::InitCanvas(int width, int height) // init with 2 layers for now { Clear(); PaintLayerNode* initialLayer = new PaintLayerNode(); SDL_GPUTextureCreateInfo creationInfo = {}; creationInfo.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; // TODO: make this configurable creationInfo.width = width; creationInfo.height = height; creationInfo.layer_count_or_depth = 1; creationInfo.num_levels = 1; creationInfo.sample_count = SDL_GPU_SAMPLECOUNT_1; creationInfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER; initialLayer->Target = new Texture(creationInfo); // clear to #ffffffff initialLayer->Target->Clear({ 0.0f,0.0f, 0.0f, 0.0f }); Tool::SetTarget(initialLayer); FinalWidth = width; FinalHeight = height; Layers.push_back(initialLayer); SelectedLayer = initialLayer; Registry[ToId(initialLayer)] = initialLayer; } void LayerManagementContext::SelectLayer(LayerNode* layer) { SelectedLayer = layer; auto* paintLayer = dynamic_cast(SelectedLayer); if (!paintLayer) return; Tool::SetTarget(paintLayer); } void LayerManagementContext::AddLayer() { PaintLayerNode* newLayer = new PaintLayerNode(); SDL_GPUTextureCreateInfo creationInfo = {}; creationInfo.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; // TODO: make this configurable creationInfo.width = FinalWidth; creationInfo.height = FinalHeight; creationInfo.layer_count_or_depth = 1; creationInfo.num_levels = 1; creationInfo.sample_count = SDL_GPU_SAMPLECOUNT_1; creationInfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER; newLayer->Target = new Texture(creationInfo); newLayer->Target->Clear({ 0.0f,0.0f, 0.0f, 0.0f }); //Tool::SetTarget(newLayer->Target); //Layers.insert(std::find(Layers.begin(), Layers.end(), SelectedLayer) + 1, newLayer); //SelectLayer(newLayer); auto after = ToIds(); Registry[ToId(newLayer)] = newLayer; after.insert(std::find(after.begin(), after.end(), ToId(SelectedLayer)) + 1, ToId(newLayer)); App::History->Execute(std::make_unique( "Add Layer", ToIds(), after, ToId(SelectedLayer), ToId(newLayer) )); } void LayerManagementContext::RemoveLayer(LayerNode* layer) { if (Layers.size() < 2) return; auto after = ToIds(); auto it = std::find(after.begin(), after.end(), ToId(layer)); after.erase(it); uint32_t nextSelected = (it == after.begin()) ? *(it + 1) : *(it - 1); App::History->Execute(std::make_unique( "Remove Layer", ToIds(), after, ToId(SelectedLayer), nextSelected )); } void LayerManagementContext::SwapLayers(LayerNode* layer1, LayerNode* layer2) { assert(layer1 != layer2); // fuck it we ball auto after = ToIds(); auto it1 = std::find(after.begin(), after.end(), ToId(layer1)); auto it2 = std::find(after.begin(), after.end(), ToId(layer2)); std::iter_swap(it1, it2); App::History->Execute(std::make_unique( "Reorder Layer", ToIds(), after, ToId(SelectedLayer), ToId(SelectedLayer) )); } void LayerManagementContext::SetBackground(BackgroundMode mode, float* color) { LayerManagementContext::Background = mode; memcpy(LayerManagementContext::BackgroundColor, color, sizeof(float) * 4); }