Skip to content

Commit

Permalink
Support RuleOverrideTile for RuleTile subtypes (#66)
Browse files Browse the repository at this point in the history
And some bug fix:
- HexagonalRuleTile rule transform right click error when index < 0
- RuleOverrideTile missing GameObject field
  • Loading branch information
johnsoncodehk authored and ChuanXin-Unity committed Jan 7, 2019
1 parent cb7fab5 commit 22f7c3b
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 238 deletions.
Expand Up @@ -19,19 +19,11 @@ internal class HexagonalRuleTileEditor : RuleTileEditor
};
private static readonly int[] s_FlatTopArrows = {1, 2, 8, 7, 6, 0};

private static Texture2D[] arrows
{
get { return RuleTile.arrows; }
}

protected override void DoRuleMatrixOnGUI(RuleTile ruleTile, Rect rect, RuleTile.TilingRule tilingRule)
internal override void RuleMatrixOnGUI(RuleTile ruleTile, Rect rect, RuleTile.TilingRule tilingRule)
{
var hexTile = (HexagonalRuleTile) ruleTile;
RuleMatrixOnGUI(hexTile, rect, tilingRule, hexTile.m_FlatTop);
}
bool flatTop = hexTile.m_FlatTop;

private static void RuleMatrixOnGUI(HexagonalRuleTile hexTile, Rect rect, RuleTile.TilingRule tilingRule, bool flatTop)
{
Handles.color = EditorGUIUtility.isProSkin ? new Color(1f, 1f, 1f, 0.2f) : new Color(0f, 0f, 0f, 0.2f);
float w = rect.width / 3f;
float h = rect.height / 3f;
Expand Down Expand Up @@ -83,47 +75,15 @@ private static void RuleMatrixOnGUI(HexagonalRuleTile hexTile, Rect rect, RuleTi
Vector2 position = flatTop ? s_FlatTopPositions[index] : s_PointedTopPositions[index];
int arrowIndex = flatTop ? s_FlatTopArrows[index] : s_PointedTopArrows[index];
Rect r = new Rect(rect.xMin + position.x * w, rect.yMin + position.y * h, w - 1, h - 1);
hexTile.RuleOnGUI(r, arrowIndex, tilingRule.m_Neighbors[index]);
if (Event.current.type == EventType.MouseDown && r.Contains(Event.current.mousePosition))
{
var allConsts = hexTile.m_NeighborType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
var neighbors = allConsts.Select(c => (int)c.GetValue(null)).ToList();
neighbors.Sort();

int oldIndex = neighbors.IndexOf(tilingRule.m_Neighbors[index]);
int newIndex = (int)Mathf.Repeat(oldIndex + GetMouseChange(), neighbors.Count);
tilingRule.m_Neighbors[index] = neighbors[newIndex];
GUI.changed = true;
Event.current.Use();
}
RuleOnGUI(r, arrowIndex, tilingRule.m_Neighbors[index]);
RuleNeighborUpdate(r, tilingRule, index);
}
// Center
{
Rect r = new Rect(rect.xMin + w, rect.yMin + h, w - 1, h - 1);
switch (tilingRule.m_RuleTransform)
{
case RuleTile.TilingRule.Transform.Rotated:
GUI.DrawTexture(r, autoTransforms[0]);
break;
case RuleTile.TilingRule.Transform.MirrorX:
GUI.DrawTexture(r, autoTransforms[1]);
break;
case RuleTile.TilingRule.Transform.MirrorY:
GUI.DrawTexture(r, autoTransforms[2]);
break;
}
if (Event.current.type == EventType.MouseDown && r.Contains(Event.current.mousePosition))
{
tilingRule.m_RuleTransform = (RuleTile.TilingRule.Transform)(((int)tilingRule.m_RuleTransform + GetMouseChange()) % 4);
GUI.changed = true;
Event.current.Use();
}
RuleTransformOnGUI(r, tilingRule.m_RuleTransform);
RuleTransformUpdate(r, tilingRule);
}
}

private static int GetMouseChange()
{
return Event.current.button == 1 ? -1 : 1;
}
}
}
Expand Up @@ -16,14 +16,10 @@ internal class IsometricRuleTileEditor : RuleTileEditor
{ 7, 8, 5 },
};

protected override void DoRuleMatrixOnGUI(RuleTile ruleTile, Rect rect, RuleTile.TilingRule tilingRule)
internal override void RuleMatrixOnGUI(RuleTile ruleTile, Rect rect, RuleTile.TilingRule tilingRule)
{
var isoTile = (IsometricRuleTile) ruleTile;
RuleMatrixOnGUI(isoTile, rect, tilingRule);
}

private static void RuleMatrixOnGUI(IsometricRuleTile isoTile, Rect rect, RuleTile.TilingRule tilingRule)
{
Handles.color = EditorGUIUtility.isProSkin ? new Color(1f, 1f, 1f, 0.2f) : new Color(0f, 0f, 0f, 0.2f);
int index = 0;
float w = rect.width / 3f;
Expand Down Expand Up @@ -59,62 +55,29 @@ private static void RuleMatrixOnGUI(IsometricRuleTile isoTile, Rect rect, RuleTi
w - 1, h - 1);
if (x != 1 || y != 1)
{
isoTile.RuleOnGUI(r, s_Arrows[y, x], tilingRule.m_Neighbors[index]);
if (Event.current.type == EventType.MouseDown && r.Contains(Event.current.mousePosition))
{
var allConsts = isoTile.m_NeighborType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
var neighbors = allConsts.Select(c => (int)c.GetValue(null)).ToList();
neighbors.Sort();

int oldIndex = neighbors.IndexOf(tilingRule.m_Neighbors[index]);
int newIndex = (int)Mathf.Repeat(oldIndex + GetMouseChange(), neighbors.Count);
tilingRule.m_Neighbors[index] = neighbors[newIndex];
GUI.changed = true;
Event.current.Use();
}
RuleOnGUI(r, s_Arrows[y, x], tilingRule.m_Neighbors[index]);
RuleNeighborUpdate(r, tilingRule, index);

index++;
}
else
{
switch (tilingRule.m_RuleTransform)
{
case RuleTile.TilingRule.Transform.Rotated:
GUI.DrawTexture(r, autoTransforms[0]);
break;
case RuleTile.TilingRule.Transform.MirrorX:
GUI.DrawTexture(r, autoTransforms[1]);
break;
case RuleTile.TilingRule.Transform.MirrorY:
GUI.DrawTexture(r, autoTransforms[2]);
break;
}

if (Event.current.type == EventType.MouseDown && ContainsMousePosition(r))
{
tilingRule.m_RuleTransform = (RuleTile.TilingRule.Transform)(((int)tilingRule.m_RuleTransform + 1) % 4);
GUI.changed = true;
Event.current.Use();
}
RuleTransformOnGUI(r, tilingRule.m_RuleTransform);
RuleTransformUpdate(r, tilingRule);
}
}
}
}

private static bool ContainsMousePosition(Rect r)
internal override bool ContainsMousePosition(Rect rect)
{
var center = r.center;
var halfWidth = r.width / 2;
var halfHeight = r.height / 2;
var center = rect.center;
var halfWidth = rect.width / 2;
var halfHeight = rect.height / 2;
var mouseFromCenter = Event.current.mousePosition - center;
var xAbs = Mathf.Abs(Vector2.Dot(mouseFromCenter, Vector2.right));
var yAbs = Mathf.Abs(Vector2.Dot(mouseFromCenter, Vector2.up));
return (xAbs / halfWidth + yAbs / halfHeight) <= 1;
}

private static int GetMouseChange()
{
return Event.current.button == 1 ? -1 : 1;
}
}
}
Expand Up @@ -167,12 +167,16 @@ public void DrawOriginalRuleElement(Rect rect, RuleTile.TilingRule originalRule,
Rect matrixRect = new Rect(rect.xMax - matrixWidth * 2f - 10f, yPos, matrixWidth, k_DefaultElementHeight);
Rect spriteRect = new Rect(rect.xMax - matrixWidth - 5f, yPos, matrixWidth, k_DefaultElementHeight);

RuleTileEditor ruleTileEditor = Editor.CreateEditor(overrideTile.m_Tile) as RuleTileEditor;

if (!isDefault)
RuleTileEditor.RuleInspectorOnGUI(inspectorRect, originalRule);
else
RuleOriginalDefaultInspectorOnGUI(inspectorRect, originalRule);
RuleTileEditor.RuleMatrixOnGUI(overrideTile.m_Tile, matrixRect, originalRule);
ruleTileEditor.RuleMatrixOnGUI(overrideTile.m_Tile, matrixRect, originalRule);
RuleTileEditor.SpriteOnGUI(spriteRect, originalRule);

DestroyImmediate(ruleTileEditor);
}
}
private void DrawOverrideElement(Rect rect, RuleTile.TilingRule originalRule)
Expand Down Expand Up @@ -224,6 +228,9 @@ private void RuleOverrideInspectorOnGUI(Rect rect, RuleTile.TilingRule originalR
if (overrideRule == null)
return;

GUI.Label(new Rect(rect.xMin, y, k_LabelWidth, k_SingleLineHeight), "Game Object");
overrideRule.m_GameObject = (GameObject)EditorGUI.ObjectField(new Rect(rect.xMin + k_LabelWidth, y, rect.width - k_LabelWidth, k_SingleLineHeight), "", overrideRule.m_GameObject, typeof(GameObject), false);
y += k_SingleLineHeight;
GUI.Label(new Rect(rect.xMin, y, k_LabelWidth, k_SingleLineHeight), "Collider");
overrideRule.m_ColliderType = (Tile.ColliderType)EditorGUI.EnumPopup(new Rect(rect.xMin + k_LabelWidth, y, rect.width - k_LabelWidth, k_SingleLineHeight), overrideRule.m_ColliderType);
y += k_SingleLineHeight;
Expand Down
Expand Up @@ -20,10 +20,4 @@ public class #SCRIPTNAME# : RuleTile<#SCRIPTNAME#.Neighbor> {
}
return base.RuleMatch(neighbor, tile);
}

#if UNITY_EDITOR
public override void RuleOnGUI(Rect rect, Vector2Int pos, int neighbor) {
base.RuleOnGUI(rect, pos, neighbor);
}
#endif
}

0 comments on commit 22f7c3b

Please sign in to comment.