Skip to content

Instantly share code, notes, and snippets.

@samizzo
Last active September 25, 2018 12:12
Show Gist options
  • Save samizzo/b4c9ce9a2b54e6ba18a1db5a3df5a2a0 to your computer and use it in GitHub Desktop.
Save samizzo/b4c9ce9a2b54e6ba18a1db5a3df5a2a0 to your computer and use it in GitHub Desktop.
DrawLevel and ProcessMouse code
private void DrawLevel()
{
var selectedChords = m_selectedLevel.GetChords(m_selectedX, m_selectedY);
EditorGUILayout.BeginVertical(GUILayout.Width(24));
for (var y = 0; y < LevelData.Y_SIZE; y++)
{
EditorGUILayout.BeginHorizontal();
for (var x = 0; x < LevelData.X_SIZE; x++)
{
var oldCol = GUI.color;
var canModify = true;
Texture2D texture = null;
var objs = m_selectedLevel.GetLevelObjects(x, y);
if (!m_editorTextures.TryGetValue(LevelObjectData.Type.Empty.ToString(), out texture))
texture = EditorGUIUtility.whiteTexture;
GUILayout.Label(texture, m_levelObjectStyle);
var rect = GUILayoutUtility.GetLastRect();
var isPartOfChord = false;
if (objs != null)
{
if (objs.Count > 0)
{
if (!m_editorTextures.TryGetValue(LevelObjectData.Type.Ground.ToString(), out texture))
texture = EditorGUIUtility.whiteTexture;
GUI.DrawTexture(rect, texture);
}
for (var i = 0; i < objs.Count; i++)
{
var obj = objs[i];
if (obj.ObjectType == LevelObjectData.Type.Crate || obj.ObjectType == LevelObjectData.Type.Target)
{
if (m_colourSet)
GUI.color = m_colourSet.GetColour(y);
}
var textureName = obj.ObjectType.ToString();
var target = obj as TargetLevelObjectData;
if (target != null && target.IsExtended)
{
textureName += "Extended";
}
else if (IsPartOfExtended(x, y))
{
textureName = "TargetExtendedContinue";
canModify = false;
}
else if (IsEndOfExtended(x, y))
{
textureName = "TargetExtendedEnd";
canModify = false;
}
if (!m_editorTextures.TryGetValue(textureName, out texture))
texture = EditorGUIUtility.whiteTexture;
GUI.DrawTexture(rect, texture);
var chord = m_selectedLevel.GetChord(obj);
isPartOfChord |= chord != null && selectedChords != null && selectedChords.Contains(chord);
}
}
var goc = m_selectedLevel.GetGoc(x, y);
if (goc != null && goc.m_gameObjects.Count > 0)
{
Texture2D dot;
if (m_editorTextures.TryGetValue("Dot", out dot))
GUI.DrawTexture(rect, dot);
}
GUI.color = oldCol;
ProcessMouse(rect, x, y, canModify);
if (IsInChord(x, y))
{
var r = new Rect(rect);
r.x += r.width * 0.5f;
r.width = 2.0f;
EditorGUI.DrawRect(r, Color.white);
}
var alpha = (Mathf.PingPong((float)EditorApplication.timeSinceStartup, 0.5f) * 2.0f);
if (x == m_hoverX && y == m_hoverY)
EditorGUI.DrawRect(rect, new Color(1.0f, 1.0f, 1.0f, alpha));
if (x == m_selectedX && y == m_selectedY || isPartOfChord)
{
oldCol = GUI.color;
GUI.color = new Color(1.0f, 1.0f, 1.0f, alpha);
GUI.DrawTexture(rect, m_selectionTexture);
GUI.color = oldCol;
}
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
}
private void ProcessMouse(Rect rect, int x, int y, bool canModify)
{
var evt = Event.current;
if (!evt.isMouse || !rect.Contains(evt.mousePosition))
return;
if (evt.button == 0 && evt.type == EventType.MouseDown)
{
if (m_selectedTool == LevelObjectData.Type.None)
{
// "Selection" tool is active.
if (evt.control)
{
// If control is down, then if current selection is a crate/target and new
// selection is a crate/target, link them in a chord.
if (x == m_selectedX)
{
// Crates/targets can only be linked if they are in the same column.
MakeChord(x, m_selectedY, y);
}
}
else
{
m_pendingSelectedX = x;
m_pendingSelectedY = y;
GUI.FocusControl("");
}
}
else if (canModify)
{
Undo.RecordObject(m_selectedLevel, string.Format("Set ({0}, {1}) to {2}", x, y, m_selectedTool.ToString()));
var type = LevelObjectData.GetTypeFromId(m_selectedTool);
var obj = (LevelObjectData)System.Activator.CreateInstance(type);
obj.ObjectType = m_selectedTool;
obj.Position = new Vector2I(x, y);
// HACK! Don't clear existing objects from the position if we're placing a target and there's a crate
// or we're placing a crate and there's a target. This way we can have crates on targets at the start
// of a level.
var placeCrate = m_selectedTool == LevelObjectData.Type.Crate;
var placeTarget = m_selectedTool == LevelObjectData.Type.Target;
var hasCrate = m_selectedLevel.HasLevelObject(x, y, LevelObjectData.Type.Crate);
var hasTarget = m_selectedLevel.HasLevelObject(x, y, LevelObjectData.Type.Target);
var dontClear = (placeCrate && hasTarget) || (placeTarget && hasCrate);
var clear = !dontClear || (hasCrate && !placeTarget) || (hasTarget && !placeCrate);
if (clear)
m_selectedLevel.ClearLevelObjects(x, y);
m_selectedLevel.AddLevelObject(x, y, obj);
EditorUtility.SetDirty(m_selectedLevel);
// If the user just placed a pivot, automatically select it.
if (m_selectedTool == LevelObjectData.Type.Pivot)
{
m_selectedTool = LevelObjectData.Type.None;
m_pendingSelectedX = x;
m_pendingSelectedY = y;
}
GUI.FocusControl("");
Repaint();
}
}
else if (evt.type == EventType.MouseMove)
{
m_hoverX = x;
m_hoverY = y;
Repaint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment