递归菜单系统
本文关键字:系统 菜单 递归 | 更新日期: 2023-09-27 18:09:34
我试图编码一个递归菜单类,可导航的箭头键,这几乎是完整的,只是,它似乎有一些小问题的情况下,上升/下降后一点(菜单包装)它变得不稳定,奇怪地开始去子项,即使该组关闭。
这里是我的输入函数:
{
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;
}
它渲染菜单很好,但为了你的缘故,这里是我的绘图代码:
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;
}
}
没关系,问题完全在于group函数缺少一个简单的行
else if (group.Index < -1) group.Index = -1;
新的组功能:
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);
}