2016-09-30 13 views
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; 
     } 
    } 

答えて

0

気にしないで、問題がGoUp機能でのみでしたシンプルなラインが欠落し

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

新しいGoUp機能:

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); 
    }