2019-09-29 10:00:16 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace cylvester
|
|
|
|
{
|
|
|
|
interface IRectangularSelection
|
|
|
|
{
|
|
|
|
void Start(Vector2 mousePosition);
|
2019-09-29 18:24:16 +00:00
|
|
|
void Update(Vector2 mousePosition, ref Rect paintSpace, ref Rect selectionRect);
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class RectangularSelection : IRectangularSelection
|
|
|
|
{
|
|
|
|
private readonly Rect paintSpace_;
|
|
|
|
|
|
|
|
private Rect selectedArea_;
|
|
|
|
private readonly int textureWidth_;
|
|
|
|
private readonly int textureHeight_;
|
|
|
|
|
|
|
|
|
2019-09-29 17:47:17 +00:00
|
|
|
public RectangularSelection(int textureWidth, int textureHeight)
|
2019-09-29 10:00:16 +00:00
|
|
|
{
|
|
|
|
textureWidth_ = textureWidth;
|
|
|
|
textureHeight_ = textureHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(Vector2 mousePosition)
|
|
|
|
{
|
|
|
|
selectedArea_.x = mousePosition.x;
|
|
|
|
selectedArea_.y = mousePosition.y;
|
|
|
|
}
|
|
|
|
|
2019-09-29 18:24:16 +00:00
|
|
|
public void Update(Vector2 mousePosition, ref Rect paintSpace, ref Rect selectionRect)
|
2019-09-29 10:00:16 +00:00
|
|
|
{
|
|
|
|
selectedArea_.width = mousePosition.x - selectedArea_.x;
|
|
|
|
selectedArea_.height = mousePosition.y - selectedArea_.y;
|
2019-09-29 17:47:17 +00:00
|
|
|
var xPos = (selectedArea_.x - paintSpace.x) / paintSpace.width;
|
|
|
|
var yPos = (selectedArea_.y - paintSpace.y) / paintSpace.height;
|
|
|
|
var width = selectedArea_.width / paintSpace.width;
|
|
|
|
var height = selectedArea_.height / paintSpace.height;
|
2019-09-29 10:00:16 +00:00
|
|
|
|
2019-09-29 18:24:16 +00:00
|
|
|
selectionRect.x = xPos * textureWidth_;
|
|
|
|
selectionRect.y = yPos * textureHeight_;
|
|
|
|
selectionRect.width = width * textureWidth_;
|
|
|
|
selectionRect.height = height * textureHeight_;
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|