0
我試圖編寫一個遞歸菜單類,可用箭頭鍵導航,這幾乎是完整的,就是這樣,它似乎在情況上有小問題,在上/下一點之後(菜單包裝)它變得不穩定,並且奇怪地開始轉到子項目,即使該組關閉。遞歸菜單系統
這裏是我的輸入功能:
{
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;
}
}