1
2 #include "../nx.h"
3 #include "dialog.h"
4 #include "dialog.fdh"
5 using namespace Options;
6 extern FocusStack optionstack;
7
8 #define DLG_X ((SCREEN_WIDTH / 2) - 88)
9 #define DLG_Y ((SCREEN_HEIGHT / 2) - 90)
10 #define DLG_W 190
11 #define DLG_H 180
12
13 #define REPEAT_WAIT 30
14 #define REPEAT_RATE 4
15
Dialog()16 Dialog::Dialog()
17 {
18 onclear = NULL;
19 ondismiss = NULL;
20
21 fCoords.x = DLG_X;
22 fCoords.y = DLG_Y;
23 fCoords.w = DLG_W;
24 fCoords.h = DLG_H;
25 fTextX = (fCoords.x + 48);
26
27 fCurSel = 0;
28 fNumShown = 0;
29 fRepeatTimer = 0;
30
31 optionstack.AddItem(this);
32 }
33
~Dialog()34 Dialog::~Dialog()
35 {
36 ODItem *item;
37 for(int i=0; item = ItemAt(i);i++)
38 delete item;
39
40 optionstack.RemoveItem(this);
41 }
42
SetSize(int w,int h)43 void Dialog::SetSize(int w, int h)
44 {
45 fCoords.w = w;
46 fCoords.h = h;
47 fCoords.x = ((DLG_W / 2) - (w / 2)) + DLG_X;
48 fCoords.y = ((DLG_H / 2) - (h / 2)) + DLG_Y;
49 fTextX = (fCoords.x + 34);
50 }
51
offset(int xd,int yd)52 void Dialog::offset(int xd, int yd)
53 {
54 fCoords.x += xd;
55 fCoords.y += yd;
56 fTextX += xd;
57 }
58
59 /*
60 void c------------------------------() {}
61 */
62
AddItem(const char * text,void (* activate)(ODItem *,int),void (* update)(ODItem *),int id,int type)63 ODItem *Dialog::AddItem(const char *text, \
64 void (*activate)(ODItem *, int), \
65 void (*update)(ODItem *),\
66 int id, int type)
67 {
68 ODItem *item = new ODItem;
69 memset(item, 0, sizeof(ODItem));
70
71 strcpy(item->text, text);
72
73 item->activate = activate;
74 item->update = update;
75 item->id = id;
76 item->type = type;
77
78 fItems.AddItem(item);
79
80 if (update)
81 (*update)(item);
82
83 return item;
84 }
85
AddSeparator()86 ODItem *Dialog::AddSeparator()
87 {
88 return AddItem("", NULL, NULL, -1, OD_SEPARATOR);
89 }
90
AddDismissalItem(const char * text)91 ODItem *Dialog::AddDismissalItem(const char *text)
92 {
93 if (!text) text = "Return";
94 return AddItem(text, NULL, NULL, -1, OD_DISMISS);
95 }
96
97 /*
98 void c------------------------------() {}
99 */
100
Draw()101 void Dialog::Draw()
102 {
103 TextBox::DrawFrame(fCoords.x, fCoords.y, fCoords.w, fCoords.h);
104
105 int x = fTextX;
106 int y = (fCoords.y + 18);
107 for(int i=0;;i++)
108 {
109 ODItem *item = (ODItem *)fItems.ItemAt(i);
110 if (!item) break;
111
112 if (i < fNumShown)
113 DrawItem(x, y, item);
114
115 if (i == fCurSel)
116 draw_sprite(x - 16, y, SPR_WHIMSICAL_STAR, 1);
117
118 y += GetFontHeight();
119 }
120
121 if (fNumShown < 99)
122 fNumShown++;
123 }
124
DrawItem(int x,int y,ODItem * item)125 void Dialog::DrawItem(int x, int y, ODItem *item)
126 {
127 char text[132];
128
129 strcpy(text, item->text);
130 strcat(text, item->suffix);
131
132 font_draw(x, y, text, 0);
133
134 // for key remaps
135 if (item->righttext[0])
136 {
137 font_draw((fCoords.x + fCoords.w) - 62, y, item->righttext, 0);
138 }
139 }
140
141
142 /*
143 void c------------------------------() {}
144 */
145
RunInput()146 void Dialog::RunInput()
147 {
148 if (inputs[UPKEY] || inputs[DOWNKEY])
149 {
150 int dir = (inputs[DOWNKEY]) ? 1 : -1;
151
152 if (!fRepeatTimer)
153 {
154 fRepeatTimer = (lastinputs[UPKEY] || lastinputs[DOWNKEY]) ? REPEAT_RATE : REPEAT_WAIT;
155 sound(SND_MENU_MOVE);
156
157 int nitems = fItems.CountItems();
158 for(;;)
159 {
160 fCurSel += dir;
161 if (fCurSel < 0) fCurSel = (nitems - 1);
162 else fCurSel %= nitems;
163
164 ODItem *item = ItemAt(fCurSel);
165 if (item && item->type != OD_SEPARATOR) break;
166 }
167 }
168 else fRepeatTimer--;
169 }
170 else fRepeatTimer = 0;
171
172
173 if (buttonjustpushed() || justpushed(RIGHTKEY) || justpushed(LEFTKEY))
174 {
175 int dir = (!inputs[LEFTKEY] || buttonjustpushed() || justpushed(RIGHTKEY)) ? 1 : -1;
176
177 ODItem *item = ItemAt(fCurSel);
178 if (item)
179 {
180 if (item->type == OD_DISMISS)
181 {
182 if (dir > 0)
183 {
184 sound(SND_MENU_MOVE);
185 if (ondismiss) (*ondismiss)();
186 return;
187 }
188 }
189 else if (item->activate)
190 {
191 (*item->activate)(item, dir);
192
193 if (item->update)
194 (*item->update)(item);
195 }
196 }
197 }
198
199 /*if (justpushed(ESCKEY))
200 {
201 Dismiss();
202 return;
203 }*/
204 }
205
SetSelection(int sel)206 void Dialog::SetSelection(int sel)
207 {
208 if (sel < 0) sel = fItems.CountItems();
209 if (sel >= fItems.CountItems()) sel = (fItems.CountItems() - 1);
210
211 fCurSel = sel;
212 }
213
214
Dismiss()215 void Dialog::Dismiss()
216 {
217 delete this;
218 }
219
Refresh()220 void Dialog::Refresh()
221 {
222 ODItem *item;
223 for(int i=0; item = ItemAt(i);i++)
224 {
225 if (item->update)
226 (*item->update)(item);
227 }
228 }
229
Clear()230 void Dialog::Clear()
231 {
232 if (onclear)
233 (*onclear)();
234
235 ODItem *item;
236 for(int i=0; item = ItemAt(i);i++)
237 delete item;
238
239 fItems.MakeEmpty();
240 fNumShown = 0;
241 fCurSel = 0;
242 }
243
244
245
246
247