public void mapPixel(Vector2 templatePixel, Vector2 layerPixel) { if (colorMapDictionary == null) { colorMapDictionary = new VectorDictionary(); } if (originalColorDictionary == null) { originalColorDictionary = new VectorColorDictionary(); } if (colorMapDictionary.ContainsValue(templatePixel)) { Vector2 keyToSwap = Vector2.negativeInfinity; //We know that this pixel is already mapped to something on this layer //so lets go through and find which one it is foreach (Vector2 key in colorMapDictionary.Keys) { if (colorMapDictionary[key] == templatePixel) { if (key != layerPixel) { //the pixel we are mapping isn't the same as the already set pixel, so we shall replace these keyToSwap = key; } else { //We are mapping the same pixel, so lets just stop here return; } } } if (keyToSwap != Vector2.negativeInfinity) { colorMapDictionary.Remove(keyToSwap); SetPixel((int)keyToSwap.x, (int)keyToSwap.y, originalColorDictionary[keyToSwap]); originalColorDictionary.Remove(keyToSwap); } } originalColorDictionary[layerPixel] = GetPixel((int)layerPixel.x, (int)layerPixel.y); colorMapDictionary[layerPixel] = templatePixel; colorMappedPixels(templatePixel); }
// Create clone of other UPALayer public UPALayer(UPALayer original) { name = original.name; opacity = 1; mode = original.mode; map = (Color[])original.map.Clone(); tex = new Texture2D(original.parentImg.width, original.parentImg.height); Texture2D parentTex = original.tex; if (parentTex != null) { //TODO: may be bad to turn this off tex.SetPixels(original.tex.GetPixels()); } else { Debug.LogError("no parent tex!!"); } tex.filterMode = FilterMode.Point; tex.Apply(); enabled = true; locked = original.locked; parentImg = original.parentImg; colorMapDictionary = original.colorMapDictionary; // Because Unity won't record map (Color[]) as an undo, // we instead register a callback to LoadMapFromTex since undoing textures works fine Undo.undoRedoPerformed += LoadMapFromTex; // subscribe to the undo event }