Something went wrong. Try again.
art software for the rest of us
drawing paint image sdl art
Something went wrong. Try again.
4.8 kB · 147 lines
C++
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148#include "LayerManagmentContext.h"
BackgroundMode LayerManagementContext::Background = BackgroundMode::COLOR;float LayerManagementContext::BackgroundColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };std::vector<LayerNode*> LayerManagementContext::Layers;int LayerManagementContext::FinalWidth, LayerManagementContext::FinalHeight;LayerNode* LayerManagementContext::SelectedLayer;std::unordered_map<uint32_t, LayerNode*> LayerManagementContext::Registry;
#include "../LayerNodes/PaintLayerNode.h"#include "../Tools/Tool.h"#include "../App.h" // App::History#include "../Commands/LayerManage.h"
std::vector<uint32_t> LayerManagementContext::ToIds() { std::vector<uint32_t> 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<uint32_t>& 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<PaintLayerNode*>(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<LayerManage>( "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<LayerManage>( "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<LayerManage>( "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);}