递归菜单系统

时间:2016-09-30 11:45:15

标签: c# recursion xna

我试图编写一个递归菜单类,可以使用箭头键导航,这几乎是完整的,就是这样,在上/下后,情况似乎有些小问题有点(菜单包装)它变得不稳定,甚至在该组关闭时奇怪地开始转到子项目。

这是我的输入功能:

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

1 个答案:

答案 0 :(得分:0)

没关系,问题仅在于GoUp函数缺少一条简单的行

#include <pthread.h>
#include <stdio.h> 
#include <unistd.h>
#include <stdlib.h> 

#define BUFFER_SIZE 65536
#define NUMBER_OF_THREADS 8 
#define NUM_CHARS 127


int charCount[NUM_CHARS + 1][8]; 

void* countChar(void *arg); 

struct bufferPartition {
    unsigned char* start; 
    int size;
    int index;  
};

int main(int argc, char *argv[]){
    pthread_t tid[NUMBER_OF_THREADS]; 
    pthread_attr_t attr[NUMBER_OF_THREADS]; 
    size_t fileSize;
    unsigned char* buffer = (unsigned char *) malloc(BUFFER_SIZE); 
    unsigned int bufferPartitionSize;

    printf("%i", argc); 
    if(argc != 2){
        fprintf(stderr,"usage: a.out <integer value>\n");
        return -1; 
    }

    FILE* fp = fopen(argv[1], "r+");

    if(fp == NULL){
        printf("Error! Could not open the file.");
        return -1; 
    }
    fileSize = fread(buffer, 1, BUFFER_SIZE,fp); 

    fclose(fp);

    if(fileSize % 8 != 0){
        bufferPartitionSize = ((8 - (fileSize % 8)) + fileSize) / 8;
    }else{
        bufferPartitionSize = fileSize / 8;
    }

    for(int index = 0; index < NUMBER_OF_THREADS; index++){

        struct bufferPartition* bufferPartition = (struct bufferPartition*)malloc(sizeof(struct bufferPartition)); 

        bufferPartition -> size = bufferPartitionSize;
        bufferPartition -> start = buffer + (index * (bufferPartition -> size));
        bufferPartition -> index = index + 1; 

        pthread_attr_init(&attr[index]);
        pthread_create(&tid[index], &attr[index], countChar, bufferPartition);
    }

    for(int index = 0; index <= NUMBER_OF_THREADS; index++){
        pthread_join(tid[index], NULL); 
    }

    for(unsigned int i = 0; i <= 128; i++){

        for(unsigned int k = 1; k <= NUMBER_OF_THREADS; k++){
            charCount[i][0] += charCount[i][k]; 
        }
        if(i < 32){
            printf("%i occurrences of 0x%x\n", charCount[i][0], i);
        }else{
            printf("%i occurrences of %c\n",charCount[i][0], i);
        }
    }
    return 0;
} 

void* countChar(void *arg){

    struct bufferPartition* bufferPartition = (struct bufferPartition*) arg; 
    unsigned int character;
    int threadNumber = bufferPartition->index;

    for(int index = 0; index < bufferPartition -> size; index++){

        character = bufferPartition -> start[index];
        (charCount[character][threadNumber])++; 
    }   
}

新的GoUp功能:

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