public static Rect DrawRectangle(PIATexture tex, Vector2 startingPoint, Vector2 finalPoint, Color color) { // 0,0 on upper left corner Rect rectangle; TransformToLeftTop(ref startingPoint, tex.Texture.height); TransformToLeftTop(ref finalPoint, tex.Texture.height); if (startingPoint.x > finalPoint.x) { SwapX(ref startingPoint, ref finalPoint); } if (startingPoint.y < finalPoint.y) { SwapY(ref startingPoint, ref finalPoint); } rectangle = new Rect(startingPoint.x, startingPoint.y, Mathf.Abs(finalPoint.x - startingPoint.x), Mathf.Abs(finalPoint.y - startingPoint.y)); // Upper segment for (int x = (int)startingPoint.x; x <= finalPoint.x; x++) { tex.Paint(x, (int)startingPoint.y, color, true, false); } // Downer segment for (int x = (int)startingPoint.x; x <= finalPoint.x; x++) { tex.Paint(x, (int)finalPoint.y, color, true, false); } // Left segment for (int y = (int)startingPoint.y; y >= finalPoint.y; y--) { tex.Paint((int)startingPoint.x, y, color, true, false); } // Right segment for (int y = (int)startingPoint.y; y >= finalPoint.y; y--) { tex.Paint((int)finalPoint.x, y, color, true, false); } tex.Texture.Apply(); return(rectangle); }
public static void ClearRect(PIATexture tex, Rect rectangle) { for (int x = (int)rectangle.x; x <= rectangle.xMax; x++) { for (int y = (int)rectangle.y; y >= rectangle.y - rectangle.height; y--) { tex.Paint(x, y, ClearColor); } } tex.Texture.Apply(); }
public static Rect DrawFilledRectangle(PIATexture tex, Vector2 startingPoint, Vector2 finalPoint, Color color) { Rect rectangle = DrawRectangle(tex, startingPoint, finalPoint, color); for (int x = (int)rectangle.x + 1; x < rectangle.xMax; x++) { for (int y = (int)rectangle.y; y > rectangle.y - rectangle.height - 1; y--) { tex.Paint(x, y, color); } } tex.Texture.Apply(); return(rectangle); }
public void OnGUIExecute(Event e, Vector2 pixelCoordinate) { // mouse is outside the grid if (pixelCoordinate.x < 0 || pixelCoordinate.y < 0) { return; } // this is used to preview where we are going to draw PIATexture helper = PIAEditorWindow.Instance.SelectionTexture; PIAFrame frame = PIASession.Instance.ImageData.CurrentFrame; int width = PIASession.Instance.ImageData.Width; int height = PIASession.Instance.ImageData.Height; // tools state machine switch (ToolType) { case PIAToolType.Paint: if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag) { if (e.button == 0) { frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, FirstColor); Debug.Log(frame.GetCurrentImage().Texture.width + "," + frame.GetCurrentImage().Texture.height); } if (e.button == 1) { frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, SecondColor); } } else { helper.Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, new Color(Color.black.r, Color.black.g, Color.black.b, 0.2f), false, true, false); } break; case PIAToolType.Erase: if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag) { frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, ClearColor); } else { helper.Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, new Color(Color.white.r, Color.white.g, Color.white.b, 0.5f), false, true, false); } break; case PIAToolType.Rectangle: if (e.type == EventType.MouseDown) { downPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y); if (e.button == 0) { DrawRectangle(helper, downPoint, pixelCoordinate, new Color(FirstColor.r, FirstColor.g, FirstColor.b, 0.5f)); } if (e.button == 1) { DrawRectangle(helper, downPoint, pixelCoordinate, new Color(SecondColor.r, SecondColor.g, SecondColor.b, 0.5f)); } } if (e.type == EventType.MouseDrag) { if (e.button == 0) { DrawRectangle(helper, downPoint, pixelCoordinate, new Color(FirstColor.r, FirstColor.g, FirstColor.b, 0.5f)); } if (e.button == 1) { DrawRectangle(helper, downPoint, pixelCoordinate, new Color(SecondColor.r, SecondColor.g, SecondColor.b, 0.5f)); } } if (e.type == EventType.MouseUp) { upPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y); if (e.button == 0) { DrawRectangle(frame.GetCurrentImage(), downPoint, upPoint, FirstColor); } if (e.button == 1) { DrawRectangle(frame.GetCurrentImage(), downPoint, upPoint, SecondColor); } helper.ClearTexture(true); } break; case PIAToolType.RectangleFilled: if (e.type == EventType.MouseDown) { downPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y); if (e.button == 0) { DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(FirstColor.r, FirstColor.g, FirstColor.b, 0.5f)); } if (e.button == 1) { DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(SecondColor.r, SecondColor.g, SecondColor.b, 0.5f)); } } if (e.type == EventType.MouseDrag) { if (e.button == 0) { DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(FirstColor.r, FirstColor.g, FirstColor.b, 0.5f)); } if (e.button == 1) { DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(SecondColor.r, SecondColor.g, SecondColor.b, 0.5f)); } } if (e.type == EventType.MouseUp) { upPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y); if (e.button == 0) { DrawFilledRectangle(frame.GetCurrentImage(), downPoint, upPoint, FirstColor); } if (e.button == 1) { DrawFilledRectangle(frame.GetCurrentImage(), downPoint, upPoint, SecondColor); } helper.ClearTexture(true); } break; case PIAToolType.Selection: if (e.type == EventType.MouseDown) { downPoint = new Vector2((int)pixelCoordinate.x, (int)pixelCoordinate.y); DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(Color.cyan.r, Color.cyan.g, Color.cyan.b, 0.5f)); } if (e.type == EventType.MouseDrag) { selectedRect = DrawFilledRectangle(helper, downPoint, pixelCoordinate, new Color(Color.cyan.r, Color.cyan.g, Color.cyan.b, 0.5f)); } if (e.keyCode == KeyCode.Delete) { ClearRect(frame.GetCurrentImage(), selectedRect); helper.ClearTexture(true); } break; case PIAToolType.Dithering: if ((pixelCoordinate.x + pixelCoordinate.y) % 2 == 1) { helper.Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, new Color(Color.red.r, Color.red.g, Color.red.b, 0.2f), false, true, false); return; } if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag) { if (e.button == 0) { frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, FirstColor); } if (e.button == 1) { frame.GetCurrentImage().Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, SecondColor); } } else { helper.Paint((int)pixelCoordinate.x, height - (int)pixelCoordinate.y - 1, new Color(Color.black.r, Color.black.g, Color.black.b, 0.2f), false, true, false); } break; } }