Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodeSmile-0000011110110111/6b03259d9651d99fddb3ff1177d3f747 to your computer and use it in GitHub Desktop.
Save CodeSmile-0000011110110111/6b03259d9651d99fddb3ff1177d3f747 to your computer and use it in GitHub Desktop.
On*GUI should only switch on EventType and call methods to reduce code complexity
using CodeSmile.UnityEditor;
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace CodeSmile.Tile.UnityEditor
{
[CustomEditor(typeof(TileWorld))]
public class TileWorldDrawer : Editor
{
private Vector3 m_CursorWorldPosition;
private Vector3 m_CursorGridPosition;
private bool m_LeftMouseButtonDown;
private void OnSceneGUI()
{
switch (Event.current.type)
{
case EventType.MouseMove:
break;
case EventType.MouseDown:
if (m_InputState.IsButtonDown(MouseButton.LeftMouse))
Event.current.Use();
break;
case EventType.MouseDrag:
UpdateTileCursorPosition(Event.current.mousePosition);
TryDrawBrush();
DrawTileCursor();
if (m_InputState.IsButtonDown(MouseButton.LeftMouse))
Event.current.Use();
break;
case EventType.ScrollWheel:
break;
case EventType.Repaint:
UpdateTileCursorPosition(Event.current.mousePosition);
DrawTileCursor();
break;
case EventType.Layout:
AddDefaultControl();
break;
case EventType.DragUpdated:
Debug.Log("drag update");
break;
case EventType.DragPerform:
Debug.Log("drag perform");
break;
case EventType.DragExited:
Debug.Log("drag exit");
break;
case EventType.Ignore:
break;
case EventType.Used:
break;
case EventType.ValidateCommand:
break;
case EventType.ExecuteCommand:
break;
case EventType.ContextClick:
Debug.Log("context click");
break;
case EventType.MouseEnterWindow:
break;
case EventType.MouseLeaveWindow:
break;
case EventType.TouchMove:
break;
case EventType.TouchEnter:
break;
case EventType.TouchLeave:
break;
case EventType.TouchStationary:
break;
}
}
}
private void TryDrawBrush()
{
if (m_LeftMouseButtonDown)
{
var tileWorld = (TileWorld)target;
tileWorld.DrawTile(m_CursorGridPosition);
}
}
private void UpdateTileCursorPosition(Vector2 mousePos)
{
var ray = HandleUtility.GUIPointToWorldRay(mousePos);
if (Ray.IntersectsVirtualPlane(ray, out m_CursorWorldPosition))
{
var gridSize = ((TileWorld)target).Grid.Size;
m_CursorGridPosition = HandlesExt.SnapPointToGrid(m_CursorWorldPosition, gridSize);
}
}
private void DrawTileCursor()
{
var tileWorld = (TileWorld)target;
var gridSize = ((TileWorld)target).Grid.Size;
var gridPos = m_CursorGridPosition;
var renderPos = Vector3.zero;
switch (tileWorld.TilePivot)
{
case TilePivot.Center:
renderPos = new Vector3(gridPos.x * gridSize.x + gridSize.x * .5f,
gridPos.y * gridSize.y + gridSize.y * .5f,
gridPos.z * gridSize.z + gridSize.z * .5f);
break;
default:
throw new ArgumentOutOfRangeException();
}
Handles.DrawWireCube(renderPos, gridSize);
}
private void AddDefaultControl()
{
var controlId = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);
HandleUtility.AddDefaultControl(controlId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment