summaryrefslogtreecommitdiff
path: root/source/gui.c
blob: 45b0817a99ca4f0751bd262efb76508b97eabcc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
   ds81 - Nintendo DS ZX81 emulator.

   Copyright (C) 2006  Ian Cowburn
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
   $Id$
*/

#include <nds.h>
#include <string.h>

#include "framebuffer.h"


/* ---------------------------------------- PUBLIC INTERFACES
*/
int GUI_Menu(const char *opts[])
{
    int x,y;
    int h;
    int w;
    int no;
    int sel;
    int f;
    bool done;
    bool defer;

    w=0;
    h=0;
    sel=0;
    done=false;
    defer=false;

    for(no=0;opts[no];no++)
    {
    	h+=16;

	if (strlen(opts[no])>w)
	{
	    w=strlen(opts[no]);
	}
    }

    w=w*8+16;

    x=SCREEN_WIDTH/2-w/2;
    y=SCREEN_HEIGHT/2-h/2;

    while(!done)
    {
	uint32 key=0;

	FB_FillBox(x,y,w,h,FB_RGB(0,0,0));
	FB_Box(x,y,w,h,FB_RGB(31,31,31));
	FB_FillBox(x+1,y+sel*16+1,w-2,14,FB_RGB(8,8,31));

    	for(f=0;f<no;f++)
	{
	    FB_Centre(opts[f],y+4+f*16,FB_RGB(31,31,31),-1);
	}

	do
	{
	    swiWaitForVBlank();
	} while(!defer && !(key=keysDownRepeat()));

	if (defer)
	{
	    do
	    {
	    	swiWaitForVBlank();
	    } while (keysHeld()&KEY_TOUCH);
	    done=true;
	}
	else
	{
	    if (key & (KEY_A|KEY_B|KEY_X|KEY_Y))
	    {
		done=true;
	    }
	    else if ((key & KEY_UP) && sel)
	    {
		sel--;
	    }
	    else if ((key & KEY_DOWN) && sel<no-1)
	    {
		sel++;
	    }
	    else if (key & KEY_TOUCH)
	    {
		touchPosition tp=touchReadXY();

		if (tp.px>=x && tp.px<(w+w) && tp.py>=y && tp.py<(y+h))
		{
		    defer=true;
		    sel=(tp.py-y)/16;
		}
	    }
	}
    }

    return sel;
}