static void Init() { window = (PixelKit)EditorWindow.GetWindow(typeof(PixelKit)); window.titleContent = new GUIContent(appName); window.minSize = new Vector2(680, 710); // default size, currently locked window.maxSize = new Vector2(681, 711); }
void SaveToSelectedImage() { if (selectedTexture == null) { return; } string path = AssetDatabase.GetAssetPath(selectedTexture); var bytes = canvas.EncodeToPNG(); //Debug.Log(path); File.WriteAllBytes(path, bytes); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); wasModified = false; if (!window) { window = (PixelKit)EditorWindow.GetWindow(typeof(PixelKit)); } window.titleContent = new GUIContent(appName); }
} // OnGUI void GUIEvents() { Event current = Event.current; bool altDown = false; // bool ctrlDown=false; // bool shiftDown=false; if (Event.current.alt) { altDown = true; } //if (Event.current.shift) shiftDown = true; //if (Event.current.control) ctrlDown = true; if (current.type == EventType.ScrollWheel) { if (altDown) // adjust alpha only { paintColor1.a -= Mathf.Sign(current.delta.y) * mouseWheelSpeed; paintColor1.a = Mathf.Clamp01(paintColor1.a); } else // darken/lighten color { paintColor1.r -= Mathf.Sign(current.delta.y) * mouseWheelSpeed; paintColor1.g -= Mathf.Sign(current.delta.y) * mouseWheelSpeed; paintColor1.b -= Mathf.Sign(current.delta.y) * mouseWheelSpeed; paintColor1.r = Mathf.Clamp01(paintColor1.r); paintColor1.g = Mathf.Clamp01(paintColor1.g); paintColor1.b = Mathf.Clamp01(paintColor1.b); CurrentCellOutlineRebuild(); } HandleUtility.Repaint(); } if (current.type == EventType.KeyDown) { switch (current.keyCode) { case KeyCode.X: // x key, swap colors Color tempColor = paintColor1; paintColor1 = paintColor2; paintColor2 = tempColor; current.Use(); CurrentCellOutlineRebuild(); break; case KeyCode.D: // d key, restore default colors paintColor1 = Color.black; paintColor2 = Color.white; current.Use(); CurrentCellOutlineRebuild(); break; case KeyCode.I: // i key, invert color1 paintColor1.r = 1 - paintColor1.r; paintColor1.g = 1 - paintColor1.g; paintColor1.b = 1 - paintColor1.b; current.Use(); CurrentCellOutlineRebuild(); break; case KeyCode.B: // b key, brush toolbarMode = defaultToolBarMode; current.Use(); break; case KeyCode.F: // f key, fill toolbarMode = 2; current.Use(); break; case KeyCode.Alpha1: // 1 = quick color slot 1 paintColor1 = quickColor1; current.Use(); CurrentCellOutlineRebuild(); break; case KeyCode.Alpha2: // 2 = quick color slot 2 paintColor1 = quickColor2; current.Use(); CurrentCellOutlineRebuild(); break; case KeyCode.Alpha3: // 3 = quick color slot 3 paintColor1 = quickColor3; current.Use(); CurrentCellOutlineRebuild(); break; } } // keypress // repaint after undo if (Event.current.type == EventType.ValidateCommand) { switch (Event.current.commandName) { case "UndoRedoPerformed": HandleUtility.Repaint(); break; } } current = Event.current; // mouse buttons if (current.type == EventType.MouseDrag || current.type == EventType.MouseDown) { int x = (int)(current.mousePosition.x - leftOffsetX); int y = (int)(current.mousePosition.y - topOffsetY); int px = (int)(x / pixelSizeAdjust); int py = (int)((imageSize * pixelSizeAdjust - y) / pixelSizeAdjust); // check canvas bounds if (x >= 0 && x < imageSize * pixelSizeAdjust && y >= 0 && y < imageSize * pixelSizeAdjust) { if (toolbarMode == 1) // Brush { switch (current.button) { case 0: // left mouse button (paint) Undo.RecordObject(canvas, "Paint " + px + "," + py); canvas.SetPixel(px, py, paintColor1); if (mirrorX) { canvas.SetPixel(imageSize - mirrorXOffset - px, py, paintColor1); } if (mirrorY) { canvas.SetPixel(px, imageSize - py, paintColor1); } if (mirrorX && mirrorY) { canvas.SetPixel(imageSize - mirrorXOffset - px, imageSize - 1 - py, paintColor1); } break; case 2: // middle mouse button (color pick) paintColor1 = canvas.GetPixel(px, py); break; case 1: // right mouse button (clear) Undo.RecordObject(canvas, "Erase " + px + "," + py); if (altDown) // smart erase { if (Compare4Neighbours(px, py)) { Color32 smartEraseColor = canvas.GetPixel(px, py + 1); canvas.SetPixel(px, py, smartEraseColor); if (mirrorX) { canvas.SetPixel(imageSize - mirrorXOffset - px, py, smartEraseColor); } if (mirrorY) { canvas.SetPixel(px, imageSize - mirrorXOffset - py, smartEraseColor); } if (mirrorX && mirrorY) { canvas.SetPixel(imageSize - mirrorXOffset - px, imageSize - 1 - py, smartEraseColor); } } else // use average instead { Color32 smartEraseColor = GetAverageNeighbourColor4(px, py); canvas.SetPixel(px, py, smartEraseColor); if (mirrorX) { canvas.SetPixel(imageSize - mirrorXOffset - px, py, smartEraseColor); } if (mirrorY) { canvas.SetPixel(px, imageSize - mirrorXOffset - py, smartEraseColor); } if (mirrorX && mirrorY) { canvas.SetPixel(imageSize - mirrorXOffset - px, imageSize - 1 - py, smartEraseColor); } } } else { canvas.SetPixel(px, py, clearColor); if (mirrorX) { canvas.SetPixel(imageSize - mirrorXOffset - px, py, clearColor); } if (mirrorY) { canvas.SetPixel(px, imageSize - 1 - py, clearColor); } if (mirrorX && mirrorY) { canvas.SetPixel(imageSize - mirrorXOffset - px, imageSize - 1 - py, clearColor); } } break; } } if (toolbarMode == 2) // floodfill { switch (current.button) { case 0: // left mouse button Undo.RecordObject(canvas, "Floodfill " + px + "," + py); floodFill(px, py, canvas.GetPixel(px, py), paintColor1); break; case 2: // middle mouse button paintColor1 = canvas.GetPixel(px, py); break; case 1: // right mouse button erase Undo.RecordObject(canvas, "Erase " + px + "," + py); canvas.SetPixel(px, py, paintColor2); break; } } canvas.Apply(false); if (!wasModified) { } { if (!window) { window = (PixelKit)EditorWindow.GetWindow(typeof(PixelKit)); } window.titleContent = new GUIContent(appName + "*"); wasModified = true; } if (automaticOutline) { DrawOutline(); } HandleUtility.Repaint(); } } // show mouse cursor pixel preview if (mouseCursorPixel) { int x = (int)current.mousePosition.x - leftOffsetX; int y = (int)current.mousePosition.y - topOffsetY; int y2 = (int)current.mousePosition.y; if (x >= 0 && x < imageSize * pixelSizeAdjust && y >= 0 && y < imageSize * pixelSizeAdjust) { // get pixel coords mouseX = x / pixelSizeAdjust; mouseY = (imageSize * pixelSizeAdjust - y) / pixelSizeAdjust; if (x != mouseCursorPixelPosX || y != mouseCursorPixelPosY || paintColor1.ToString() != oldpaintColor1.ToString()) { HandleUtility.Repaint(); } mouseCursorPixelPosX = x; mouseCursorPixelPosY = y; oldpaintColor1 = paintColor1; int gridX = (x) / pixelSizeAdjust * pixelSizeAdjust + leftOffsetX; int gridY = (y2 / pixelSizeAdjust) * pixelSizeAdjust; GUI.DrawTextureWithTexCoords(new Rect(gridX - 1, gridY - 1, pixelSizeAdjust + 2, pixelSizeAdjust + 2), gridCellOutLine, new Rect(0f, 0f, 1f, 1f), true); } else { // TODO: repaint only once..? HandleUtility.Repaint(); } } } // GUIEvents()