// Draw the Pixel Art Editor. // This includes both toolbar and painting area. // TODO: Add comments void OnGUI() { if (window == null) { Init(); } if (CurrentImg == null || TemplateImage == null) { Debug.Log("one is null"); string curImgPath = EditorPrefs.GetString("currentAnimationPath", ""); string templateImgPath = EditorPrefs.GetString("templateImgPath", ""); if (curImgPath.Length != 0) { animation = UPASession.OpenAnimationsFromFolder(false, curImgPath); if (animation.Count > 0) { CurrentImg = animation[0]; //CurrentImg.initilizeAlphas(); animationIndex = 0; } } if (templateImgPath.Length != 0) { TemplateImage = UPASession.OpenImageAtPath(templateImgPath, true); TemplateImage.initilizeAlphas(); TemplateImage.loopThroughImage(); } if (CurrentImg == null) { if (GUI.Button(new Rect(window.position.width / 2f - 140, window.position.height / 2f - 25, 130, 50), "Load Animation")) { //UPASession.OpenImage(); animation = UPASession.OpenAnimationsFromFolder(false); CurrentImg.initilizeAlphas(); animationIndex = 0; } } else if (TemplateImage == null) { if (GUI.Button(new Rect(window.position.width / 2f - 140, window.position.height / 2f - 25, 130, 50), "New Template")) { UPAImageCreationWindow.Init(); } if (GUI.Button(new Rect(window.position.width / 2f + 10, window.position.height / 2f - 25, 130, 50), "Open Template")) { TemplateImage = UPASession.OpenFolder(true); TemplateImage.LoadAllTexsFromMaps(); TemplateImage.initilizeAlphas(); TemplateImage.loopThroughImage(); return; } } return; } if (gettingPreviewArmor) { if (GUI.Button(new Rect(window.position.width / 2f, window.position.height / 8f, 130, 50), "Load Head")) { pathDictionary["head"] = EditorUtility.OpenFolderPanel( "Choose Animation Folder", "Assets/Sprites/Armor/Heads/", ""); } if (GUI.Button(new Rect(window.position.width / 2f - 70, window.position.height / 8f + 60, 130, 50), "Load Arms")) { pathDictionary["arms"] = EditorUtility.OpenFolderPanel( "Choose Animation Folder", "Assets/Sprites/Armor/Arms/", ""); } if (GUI.Button(new Rect(window.position.width / 2f + 70, window.position.height / 8f + 60, 130, 50), "Load Body")) { pathDictionary["body"] = EditorUtility.OpenFolderPanel( "Choose Animation Folder", "Assets/Sprites/Armor/Bodies/", ""); } if (GUI.Button(new Rect(window.position.width / 2f, window.position.height / 8f + 120, 130, 50), "Load Legs")) { pathDictionary["legs"] = EditorUtility.OpenFolderPanel( "Choose Animation Folder", "Assets/Sprites/Legs/", ""); } if (GUI.Button(new Rect(window.position.width / 2f + 10, window.position.height / 2f - 25, 130, 50), "Done")) { UPAImage armorTemplate = UPASession.LoadImageTemplate(pathDictionary); //now we have a map to draw things from lets now map the template to each proper layer List <UPAImage> newAnimation = new List <UPAImage>(); foreach (UPAImage img in animation) { UPAImage newImage = UPASession.CreateUPAImage(img.width, img.height); for (int i = 0; i < img.layers.Count; i++) { UPALayer layer = new UPALayer(img.layers[i]); UPALayer templateLayer = armorTemplate.GetLayer(layer.name); if (templateLayer != null) { Debug.Log("layer does not == null!"); foreach (Vector2 key in layer.colorMapDictionary.Keys) { Vector2 value = layer.colorMapDictionary[key]; layer.SetPixel((int)key.x, (int)key.y, templateLayer.GetPixel((int)value.x, (int)value.y)); } } if (i != 0) { newImage.AddLayer(); } newImage.layers[i] = layer; } newAnimation.Add(newImage); } animation = newAnimation; gettingPreviewArmor = false; pathDictionary.Clear(); } return; } // Init the textures correctly, won't cost performance if nothing to load CurrentImg.LoadAllTexsFromMaps(); TemplateImage.LoadAllTexsFromMaps(); EditorGUI.DrawRect(window.position, new Color32(30, 30, 30, 255)); #region Event handling Event e = Event.current; //Init event handler //Capture mouse position Vector2 mousePos = e.mousePosition; // If key is pressed if (e.button == 0) { // Mouse buttons if (e.isMouse && mousePos.y > 40 && e.type != EventType.MouseUp) { if (!UPADrawer.GetLayerPanelRect(window.position).Contains(mousePos)) { if (selectedTool == UPATool.Eraser) { CurrentImg.SetPixelByPos(Color.clear, mousePos, CurrentImg.selectedLayer); } else if (selectedTool == UPATool.PaintBrush) { CurrentImg.SetPixelByPos(selectedColor, mousePos, CurrentImg.selectedLayer); } else if (selectedTool == UPATool.BoxBrush) { Debug.Log("TODO: Add Box Brush tool."); } else if (selectedTool == UPATool.ColorPicker) { Vector2 pCoord = CurrentImg.GetPixelCoordinate(mousePos); Color? newColor = CurrentImg.GetBlendedPixel((int)pCoord.x, (int)pCoord.y); if (newColor != null && newColor != Color.clear) { selectedColor = (Color)newColor; } selectedTool = lastTool; } else if (selectedTool == UPATool.Map) { CurrentImg.mapPixelByPos(mousePos, TemplateImage.currentPixelPosition); } } } // Key down if (e.type == EventType.KeyDown) { if (e.keyCode == KeyCode.W) { changeLayer(1); } if (e.keyCode == KeyCode.S) { changeLayer(-1); } if (e.keyCode == KeyCode.A) { TemplateImage.focusPixel(-1); } if (e.keyCode == KeyCode.D) { TemplateImage.focusPixel(1); } if (e.keyCode == KeyCode.Alpha1) { selectedTool = UPATool.PaintBrush; } if (e.keyCode == KeyCode.Alpha2) { selectedTool = UPATool.Eraser; } if (e.keyCode == KeyCode.P) { lastTool = selectedTool; selectedTool = UPATool.ColorPicker; } if (e.keyCode == KeyCode.UpArrow) { CurrentImg.layers[0].setAlpha(true); } if (e.keyCode == KeyCode.DownArrow) { CurrentImg.layers[0].setAlpha(false); } if (e.keyCode == KeyCode.LeftArrow) { changeFrame(-1); } if (e.keyCode == KeyCode.RightArrow) { changeFrame(1); } } if (e.control) { if (lastTool == UPATool.Empty) { lastTool = selectedTool; selectedTool = UPATool.Eraser; } } else if (e.type == EventType.KeyUp && e.keyCode == KeyCode.LeftControl) { if (lastTool != UPATool.Empty) { selectedTool = lastTool; lastTool = UPATool.Empty; } } } // TODO: Better way of doing this? // Why does it behave so weirdly with my mac tablet. if (e.type == EventType.ScrollWheel) { gridSpacing -= e.delta.y; } #endregion // DRAW IMAGE UPADrawer.DrawImage(CurrentImg, false); //Test draw another image UPADrawer.DrawImage(TemplateImage, true); UPADrawer.DrawToolbar(window.position, mousePos); UPADrawer.DrawLayerPanel(window.position); UPADrawer.DrawFramePanel(window.position); e.Use(); // Release event handler }
public static List <UPAImage> OpenAnimationsFromFolder(bool isTemplate, string path = "") { if (path.Length == 0) { path = EditorUtility.OpenFolderPanel( "Choose Animation Folder", "Assets/Sprites", ""); } if (path.Length != 0) { List <UPAImage> animation = new List <UPAImage>(); DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] info = dir.GetFiles("*.asset"); Debug.Log("length = " + info.Length); if (info.Length > 0) { Debug.Log("info length greator than 0"); Debug.Log(path); //We have some asset files, so lets load those in, instead of creating new ones //TODO: i dont know if these frames are loaded in order, may need to sort for (int i = 0; i < info.Length; i++) { string newPath; newPath = path + "/" + (i + 1) + ".asset"; newPath = FileUtil.GetProjectRelativePath(newPath); Debug.Log("expected path = Assets / Sprites / Front RunTest / 1.asset"); Debug.Log("new path = "); //newPath = "Assets/Sprites/Front RunTest/1"; Debug.Log(newPath); UPAImage frameImage = OpenFrameAtPath(newPath); frameImage.LoadAllTexsFromMaps(); frameImage.setAllNormalAlpha(); //frameImage.initilizeAlphas(); animation.Add(frameImage); } } else { info = dir.GetFiles("*.png"); List <FileInfo[]> frames = sortFrames(info); int frameNumber = 1; foreach (FileInfo[] frame in frames) { string newPath = path + "\\" + frameNumber + ".asset"; UPAImage i = loadImageFromFileInfo(frame, isTemplate, newPath); i.initilizeAlphas(); animation.Add(i); frameNumber += 1; } } UPAImage img = animation[0]; Debug.Log(img.GetType()); EditorPrefs.SetString("currentAnimationPath", path); UPAEditorWindow.CurrentImg = img; if (UPAEditorWindow.window != null) { UPAEditorWindow.window.Repaint(); } else { UPAEditorWindow.Init(); } return(animation); } return(null); }
// Draw the settings toolbar public static void DrawToolbar(Rect window, Vector2 mousePos) { // Draw toolbar bg EditorGUI.DrawRect(new Rect(0, 0, window.width, 40), toolbarColor); if (GUI.Button(new Rect(5, 4, 50, 30), "New")) { UPAImageCreationWindow.Init(); } if (GUI.Button(new Rect(60, 4, 50, 30), "Open")) { //CurrentImg = UPASession.OpenFolder(false); CurrentImg = UPASession.OpenImage(); CurrentImg.initilizeAlphas(); if (CurrentImg == null) { return; } } if (GUI.Button(new Rect(115, 4, 50, 30), "Export")) { UPAExportWindow.Init(CurrentImg); } if (GUI.Button(new Rect(179, 6, 25, 25), "+")) { CurrentImg.gridSpacing *= 1.2f; } if (GUI.Button(new Rect(209, 6, 25, 25), "-")) { CurrentImg.gridSpacing *= 0.8f; CurrentImg.gridSpacing -= 2; } CurrentImg.selectedColor = EditorGUI.ColorField(new Rect(250, 7, 70, 25), CurrentImg.selectedColor); EditorGUI.DrawRect(new Rect(303, 7, 20, 25), toolbarColor); //bgColor = EditorGUI.ColorField (new Rect (400, 4, 70, 25), bgColor); GUI.backgroundColor = Color.white; if (UPAEditorWindow.selectedTool == UPATool.PaintBrush) { GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f); } if (GUI.Button(new Rect(320, 4, 60, 30), "Paint")) { UPAEditorWindow.selectedTool = UPATool.PaintBrush; } GUI.backgroundColor = Color.white; if (UPAEditorWindow.selectedTool == UPATool.Map) { GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f); } if (GUI.Button(new Rect(450, 4, 60, 30), "Map")) { UPAEditorWindow.selectedTool = UPATool.Map; } GUI.backgroundColor = Color.white; if (UPAEditorWindow.selectedTool == UPATool.Eraser) { GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f); } if (GUI.Button(new Rect(385, 4, 60, 30), "Erase")) { UPAEditorWindow.selectedTool = UPATool.Eraser; } GUI.backgroundColor = Color.white; style.normal.textColor = new Color(0.7f, 0.7f, 0.7f); style.fontSize = 12; style.fontStyle = FontStyle.Normal; GUI.Label(new Rect(525, 11, 150, 30), "Use WASD to navigate.", style); if (GUI.Button(new Rect(684, 4, 55, 30), "Center")) { CurrentImg.gridOffsetX = 0; CurrentImg.gridOffsetY = 0; } CurrentImg.gridBGIndex = GUI.Toolbar(new Rect(743, 4, 130, 30), CurrentImg.gridBGIndex, gridBGStrings); if (CurrentImg.gridBGIndex == 0) { gridBGColor = Color.black; } else if (CurrentImg.gridBGIndex == 1) { gridBGColor = Color.white; } else { gridBGColor = Color.clear; } if (GUI.Button(new Rect(930, 4, 75, 30), "Save Armor")) { Armor armor = new Armor("shoulder"); armor.AddArmorPiece(new ArmorPiece("topSide")); armorSavePath = DataController.SaveArmor(armor); Debug.Log(armorSavePath); } if (GUI.Button(new Rect(1010, 4, 75, 30), "Load Armor")) { Armor armor = DataController.LoadArmor(armorSavePath); Debug.Log("armor found"); Debug.Log(armor.name); for (int i = 0; i < armor.armorPieces.Count; i++) { Debug.Log(armor.armorPieces[i].name); } } if (GUI.Button(new Rect(1090, 4, 75, 30), "Load template")) { Debug.Log("load template pressed"); UPAImage img = UPASession.OpenFolder(true); img.initilizeAlphas(); img.loopThroughImage(); } if (GUI.Button(new Rect(1175, 4, 100, 30), "Load Animations")) { UPAEditorWindow.animation = UPASession.OpenAnimationsFromFolder(false); } if (GUI.Button(new Rect(1295, 4, 100, 30), "Preview")) { UPAEditorWindow.gettingPreviewArmor = true; } if (GUI.Button(new Rect(1410, 4, 100, 30), "Save Anim")) { UPAAnimator.MakeAnimationClip(UPAEditorWindow.animation); } //Vector2 pixelCoordinate = CurrentImg.GetReadablePixelCoordinate(mousePos); //CurrentImg.GetPixelByPos(pixelCoordinate, CurrentImg.selectedLayer); //GUI.Label(new Rect(1200, 11, 200, 30), "(" + CurrentImg.GetPixelByPos(mousePos, CurrentImg.selectedLayer, true) + ")", style); //GUI.Label(new Rect(880, 11, 100, 30), "(" + (int)pixelCoordinate.x + "," + (int)pixelCoordinate.y + ")", style); if (CurrentImg.tool == UPATool.ColorPicker) { style.fontStyle = FontStyle.Bold; style.fontSize = 15; GUI.Label(new Rect(window.width / 2f - 140, 60, 100, 30), "Click on a pixel to choose a color.", style); } }