1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-22 23:34:18 +00:00
better-tee/Assets/Drawing/Scripts/PencilTool.cs

153 lines
5.3 KiB
C#
Raw Normal View History

2019-09-10 15:01:29 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PencilTool : DrawTool
{
2019-09-10 16:44:25 +00:00
public Color selectedColor = Color.black;
2019-09-10 15:01:29 +00:00
public float size = 1f;
2019-09-10 16:44:25 +00:00
protected Color[] colors;
private Vector2Int? previousPixel;
private Vector2Int? pixel;
2019-09-10 15:01:29 +00:00
2019-09-11 13:51:05 +00:00
protected Vector2Int? GetPixelAtScreenPosition(Vector2 screenPosition) {
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(screenPosition);
2019-09-10 15:01:29 +00:00
if(frame.Bounds.Contains(worldPoint)) {
Vector2 normalized = Rect.PointToNormalized(frame.Bounds, worldPoint);
Vector2Int result = new Vector2Int(Mathf.FloorToInt(normalized.x * frame.resolution.x), Mathf.FloorToInt(normalized.y * frame.resolution.y));
return result;
}
return null;
}
protected override void Start() {
base.Start();
2019-09-11 13:51:05 +00:00
colors = frame.GetPixels();
Input.simulateMouseWithTouches = false;
2019-09-10 15:01:29 +00:00
}
protected void Update() {
2019-09-11 13:51:05 +00:00
bool hasChanged = false;
// Touch
foreach(Touch t in Input.touches) {
pixel = GetPixelAtScreenPosition(t.position);
previousPixel = GetPixelAtScreenPosition(t.position - t.deltaPosition);
if(pixel.HasValue) {
if(previousPixel.HasValue) {
DrawBresenhamsThickLine(pixel.Value, previousPixel.Value, size, selectedColor);
}
else {
DrawFilledCircle(pixel.Value, size, selectedColor);
}
}
hasChanged = true;
}
// Mouse
2019-09-10 16:44:25 +00:00
previousPixel = pixel;
2019-09-10 15:01:29 +00:00
if(Input.GetMouseButton(0)) {
2019-09-11 13:51:05 +00:00
pixel = GetPixelAtScreenPosition(Input.mousePosition);
2019-09-10 15:01:29 +00:00
if(pixel.HasValue) {
2019-09-10 16:44:25 +00:00
if(previousPixel.HasValue) {
DrawBresenhamsThickLine(previousPixel.Value, pixel.Value, size, selectedColor);
}
else {
DrawFilledCircle(pixel.Value, size, selectedColor);
}
}
2019-09-11 13:51:05 +00:00
hasChanged = true;
2019-09-10 16:44:25 +00:00
}
else {
pixel = null;
}
2019-09-11 13:51:05 +00:00
if(hasChanged) {
frame.SetPixels(colors);
}
2019-09-10 16:44:25 +00:00
}
protected void DrawPixel(int x, int y, Color color) {
if(x < 0 || x >= frame.resolution.x || y < 0 || y >= frame.resolution.y) return;
colors[x + y*frame.resolution.x] = color;
}
protected void DrawCircleEightPoints(Vector2Int center, int x_radius, int y_radius, Color color) {
DrawPixel(center.x+x_radius, center.y+y_radius, color);
DrawPixel(center.x-x_radius, center.y+y_radius, color);
DrawPixel(center.x+x_radius, center.y-y_radius, color);
DrawPixel(center.x-x_radius, center.y-y_radius, color);
DrawPixel(center.x+y_radius, center.y+x_radius, color);
DrawPixel(center.x-y_radius, center.y+x_radius, color);
DrawPixel(center.x+y_radius, center.y-x_radius, color);
DrawPixel(center.x-y_radius, center.y-x_radius, color);
}
// No idea on how does it work
// https://www.geeksforgeeks.org/bresenhams-circle-drawing-algorithm/
protected void DrawBresenhamEmptyCircle(Vector2Int center, int radius, Color color) {
int x = 0;
int y = radius;
int d = 3 - 2 * radius;
DrawCircleEightPoints(center, x, y, color);
while(y >= x) {
x++;
if(d > 0) {
y--;
d = d + 4 * (x - y) + 10;
}
else {
d = d + 4 * x + 6;
}
DrawCircleEightPoints(center, x, y, color);
}
}
protected void DrawFilledCircle(Vector2Int center, float radius, Color color) {
int x_start = Mathf.CeilToInt((float)center.x - radius);
int x_end = Mathf.CeilToInt((float)center.x + radius);
int y_start = Mathf.CeilToInt((float)center.y - radius);
int y_end = Mathf.CeilToInt((float)center.y + radius);
for(int x = x_start; x < x_end; x++) {
for(int y = y_start; y < y_end; y++) {
if(Vector2Int.Distance(new Vector2Int(x, y), center) < radius) {
DrawPixel(x, y, color);
2019-09-10 15:01:29 +00:00
}
}
}
2019-09-10 16:44:25 +00:00
}
// http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm
protected void DrawBresenhamsThickLine(Vector2Int start, Vector2Int end, float radius, Color color) {
int start_x = start.x, start_y = start.y, end_x = end.x, end_y = end.y;
bool steep = Mathf.Abs(end_y - start_y) > Mathf.Abs(end_x - start_x);
if (steep) {
Utils.Swap<int>(ref start_x, ref start_y);
Utils.Swap<int>(ref end_x, ref end_y);
}
if (start_x > end_x) {
Utils.Swap<int>(ref start_x, ref end_x);
Utils.Swap<int>(ref start_y, ref end_y);
}
int dX = (end_x - start_x), dY = Mathf.Abs(end_y - start_y), err = (dX / 2), ystep = (start_y < end_y ? 1 : -1), y = start_y;
for (int x = start_x; x <= end_x; ++x)
{
if(steep) {
DrawFilledCircle(new Vector2Int(y, x), radius, color);
}
else {
DrawFilledCircle(new Vector2Int(x, y), radius, color);
}
err = err - dY;
if (err < 0) { y += ystep; err += dX; }
}
2019-09-10 15:01:29 +00:00
}
protected void Apply() {
}
}