Dean Reynolds Dean Reynolds - 2 months ago 6
C# Question

Recursive Menu System

I'm trying to code up a recursive menu class, navigatable with the arrow keys, which is almost complete, it's just that, it appears to have minor problems in situations, after going up/down for a bit (the menu wraps) it gets unstable and weirdly starts going to sub items even when that group is closed.

Here's my input functions:

{
if (Keyboard.Pressed(Keys.Up))
{
if (Index >= 0)
{
if (!GoUp(Groups[Index]))
{
Index--; if (Index == -1) Index = (Groups.Count - 1);
if (Groups[Index].Open) Groups[Index].Index = ((Groups[Index].Groups.Count + Groups[Index].Items.Count) - 1);
}
}
}
if (Keyboard.Pressed(Keys.Down))
{
if (Index >= 0)
{
if (!GoDown(Groups[Index])) Index++;
if (Index >= Groups.Count) Index = 0;
}
}
}
internal bool GoUp(Group group)
{
if (!group.Open) return false;
if ((group.Index >= 0) && (group.Index < group.Groups.Count) && group.Groups[group.Index].Open) if (GoUp(group.Groups[group.Index])) return true;
group.Index--;
if (group.Index == -1) return true;
if ((group.Index >= 0) && (group.Index < group.Groups.Count) && group.Groups[group.Index].Open) group.Groups[group.Index].Index = ((group.Groups[group.Index].Groups.Count + group.Groups[group.Index].Items.Count) - 1);
return (group.Index >= 0);
}
internal bool GoDown(Group group)
{
if (!group.Open) return false;
if ((group.Index >= 0) && (group.Index < group.Groups.Count) && group.Groups[group.Index].Open) if (GoDown(group.Groups[group.Index])) return true;
group.Index++;
if (group.Index >= (group.Groups.Count + group.Items.Count)) { group.Index = -1; return false; }
if ((group.Index >= 0) && (group.Index < group.Groups.Count)) group.Groups[group.Index].Index = -1;
return true;
}


It renders the menu fine, but for your sake, here's my drawing code:

public void Draw(Batch batch, Vector2 position, SpriteFont font, int width)
{
VisibleItems = Groups.Count;
for (var i = 0; i < Groups.Count; i++) DrawGroup(Groups[i], null, batch, ref position, font);
}
internal void DrawGroup(Group group, Group parent, Batch batch, ref Vector2 position, SpriteFont font)
{
const float textScale = .15f;
batch.DrawString(group.Name, font, position, (((((parent == null) && (group == Groups[Index])) || ((parent != null) &&
(parent.Index >= 0) && (parent.Groups.Count > parent.Index) && (group == parent.Groups[parent.Index]))) && (group.Index == -1)) ? GroupSelectedColor : GroupColor), new Vector2(textScale));
position.Y += (font.MeasureString(group.Name).Y * textScale);
if (group.Open)
{
var x1 = position.X;
if (group.Groups.Count > 0)
{
position.X += 10;
var x2 = position.X;
for (var i = 0; i < group.Groups.Count; i++)
{
DrawGroup(group.Groups[i], group, batch, ref position, font);
position.X = x2;
}
position.X -= 10;
}
if (group.Items.Count > 0)
{
position.X += 10;
for (var i = 0; i < group.Items.Count; i++)
{
batch.DrawString(group.Items[i].Name, font, position, (((group.Index >= group.Groups.Count) && (i == (group.Index - group.Groups.Count))) ? ItemSelectedColor : ItemColor), new Vector2(textScale));
position.Y += (font.MeasureString(group.Items[i].Name).Y*textScale);
}
position.X -= 10;
}
position.X = x1;
}
}

Answer

Never mind, the problem was solely in the GoUp function missing a simple line

else if (group.Index < -1) group.Index = -1;

the new GoUp function:

    internal bool GoUp(Group group)
    {
        if (!group.Open) return false;
        if ((group.Index >= 0) && (group.Index < group.Groups.Count) && group.Groups[group.Index].Open) if (GoUp(group.Groups[group.Index])) return true;
        group.Index--;
        if (group.Index == -1) return true;
        else if (group.Index < -1) group.Index = -1;
        if ((group.Index >= 0) && (group.Index < group.Groups.Count) && group.Groups[group.Index].Open) group.Groups[group.Index].Index = ((group.Groups[group.Index].Groups.Count + group.Groups[group.Index].Items.Count) - 1);
        return (group.Index >= 0);
    }
Comments