Skip to content

Instantly share code, notes, and snippets.

@Hyperparticle
Last active November 28, 2017 15:59
Show Gist options
  • Save Hyperparticle/e54433fb470af12bae282e57e7063ab3 to your computer and use it in GitHub Desktop.
Save Hyperparticle/e54433fb470af12bae282e57e7063ab3 to your computer and use it in GitHub Desktop.
Draw Controller - Version 0
public class DrawController : MonoBehaviour
{
public DrawRectangle RectanglePrefab;
private readonly List<DrawRectangle> _allShapes = new List<DrawRectangle>();
private DrawRectangle CurrentShapeToDraw { get; set; }
private bool IsDrawingShape { get; set; }
private void Update()
{
var mousePos = (Vector2) Camera.main.ScreenToWorldPoint(Input.mousePosition);
var click = Input.GetKeyUp(KeyCode.Mouse0);
var canUpdateShape = CurrentShapeToDraw != null && IsDrawingShape;
if (click) {
AddShapeVertex(mousePos);
} else if (canUpdateShape) {
UpdateShapeVertex(mousePos);
}
}
/// <summary>
/// Adds a new vertex to the current shape at the given position,
/// or creates a new shape if it doesn't exist
/// </summary>
private void AddShapeVertex(Vector2 position)
{
if (CurrentShapeToDraw == null) {
// No current shape -> instantiate a new shape and add two vertices:
// one for the initial position, and the other for the current cursor
var prefab = RectanglePrefab;
CurrentShapeToDraw = Instantiate(prefab);
CurrentShapeToDraw.name = "Shape " + _allShapes.Count;
CurrentShapeToDraw.AddVertex(position);
CurrentShapeToDraw.AddVertex(position);
IsDrawingShape = true;
_allShapes.Add(CurrentShapeToDraw);
} else {
// Current shape exists -> add vertex if finished and reset reference
IsDrawingShape = !CurrentShapeToDraw.ShapeFinished;
if (IsDrawingShape) {
CurrentShapeToDraw.AddVertex(position);
} else {
CurrentShapeToDraw = null;
}
}
}
/// <summary>
/// Updates the current shape's latest vertex position to allow
/// a shape to be updated with the mouse cursor and redrawn
/// </summary>
private void UpdateShapeVertex(Vector2 position)
{
if (CurrentShapeToDraw == null) {
return;
}
CurrentShapeToDraw.UpdateShape(position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment