NXEngine v1.0.0.5
[NXEngine.git] / pause / dialog.cpp
blob674bd86cf6df8fe5b37ae4d1248629dfaf1d4c61
2 #include "../nx.h"
3 #include "dialog.h"
4 #include "dialog.fdh"
5 using namespace Options;
6 extern FocusStack optionstack;
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
13 #define REPEAT_WAIT 30
14 #define REPEAT_RATE 4
16 Dialog::Dialog()
18 onclear = NULL;
19 ondismiss = NULL;
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);
27 fCurSel = 0;
28 fNumShown = 0;
29 fRepeatTimer = 0;
31 optionstack.AddItem(this);
34 Dialog::~Dialog()
36 for(int i=0;ODItem *item = ItemAt(i);i++)
37 delete item;
39 optionstack.RemoveItem(this);
42 void Dialog::SetSize(int w, int h)
44 fCoords.w = w;
45 fCoords.h = h;
46 fCoords.x = ((DLG_W / 2) - (w / 2)) + DLG_X;
47 fCoords.y = ((DLG_H / 2) - (h / 2)) + DLG_Y;
48 fTextX = (fCoords.x + 34);
51 void Dialog::offset(int xd, int yd)
53 fCoords.x += xd;
54 fCoords.y += yd;
55 fTextX += xd;
59 void c------------------------------() {}
62 ODItem *Dialog::AddItem(const char *text, \
63 void (*activate)(ODItem *, int), \
64 void (*update)(ODItem *),\
65 int id, int type)
67 ODItem *item = new ODItem;
68 memset(item, 0, sizeof(ODItem));
70 strcpy(item->text, text);
72 item->activate = activate;
73 item->update = update;
74 item->id = id;
75 item->type = type;
77 fItems.AddItem(item);
79 if (update)
80 (*update)(item);
82 return item;
85 ODItem *Dialog::AddSeparator()
87 return AddItem("", NULL, NULL, -1, OD_SEPARATOR);
90 ODItem *Dialog::AddDismissalItem(const char *text)
92 if (!text) text = "Return";
93 return AddItem(text, NULL, NULL, -1, OD_DISMISS);
97 void c------------------------------() {}
100 void Dialog::Draw()
102 TextBox::DrawFrame(fCoords.x, fCoords.y, fCoords.w, fCoords.h);
104 int x = fTextX;
105 int y = (fCoords.y + 18);
106 for(int i=0;;i++)
108 ODItem *item = (ODItem *)fItems.ItemAt(i);
109 if (!item) break;
111 if (i < fNumShown)
112 DrawItem(x, y, item);
114 if (i == fCurSel)
115 draw_sprite(x - 16, y, SPR_WHIMSICAL_STAR, 1);
117 y += GetFontHeight();
120 if (fNumShown < 99)
121 fNumShown++;
124 void Dialog::DrawItem(int x, int y, ODItem *item)
126 char text[132];
128 strcpy(text, item->text);
129 strcat(text, item->suffix);
131 // for replay times
132 // comes first because it can trim the text on the left side if needed
133 if (item->raligntext[0])
135 int rx = (fCoords.x + fCoords.w) - 10;
136 rx -= GetFontWidth(item->raligntext, 5);
137 font_draw(rx, y, item->raligntext, 5);
139 // ... ellipses if too long
140 int maxx = (rx - 4);
141 //FillRect(maxx, 0, maxx, SCREEN_HEIGHT, 0,255,0);
142 for(;;)
144 int wd = GetFontWidth(text, 0);
145 if (x+wd < maxx) break;
147 int len = strlen(text);
148 if (len <= 3) { *text = 0; break; }
150 text[len-1] = 0;
151 text[len-2] = '.';
152 text[len-3] = '.';
153 text[len-4] = '.';
157 font_draw(x, y, text, 0);
159 // for key remaps
160 if (item->righttext[0])
162 font_draw((fCoords.x + fCoords.w) - 62, y, item->righttext, 0);
168 void c------------------------------() {}
171 void Dialog::RunInput()
173 if (inputs[UPKEY] || inputs[DOWNKEY])
175 int dir = (inputs[DOWNKEY]) ? 1 : -1;
177 if (!fRepeatTimer)
179 fRepeatTimer = (lastinputs[UPKEY] || lastinputs[DOWNKEY]) ? REPEAT_RATE : REPEAT_WAIT;
180 sound(SND_MENU_MOVE);
182 int nitems = fItems.CountItems();
183 for(;;)
185 fCurSel += dir;
186 if (fCurSel < 0) fCurSel = (nitems - 1);
187 else fCurSel %= nitems;
189 ODItem *item = ItemAt(fCurSel);
190 if (item && item->type != OD_SEPARATOR) break;
193 else fRepeatTimer--;
195 else fRepeatTimer = 0;
198 if (buttonjustpushed() || justpushed(RIGHTKEY) || justpushed(LEFTKEY))
200 int dir = (!inputs[LEFTKEY] || buttonjustpushed() || justpushed(RIGHTKEY)) ? 1 : -1;
202 ODItem *item = ItemAt(fCurSel);
203 if (item)
205 if (item->type == OD_DISMISS)
207 if (dir > 0)
209 sound(SND_MENU_MOVE);
210 if (ondismiss) (*ondismiss)();
211 return;
214 else if (item->activate)
216 (*item->activate)(item, dir);
218 if (item->update)
219 (*item->update)(item);
224 /*if (justpushed(ESCKEY))
226 Dismiss();
227 return;
231 void Dialog::SetSelection(int sel)
233 if (sel < 0) sel = fItems.CountItems();
234 if (sel >= fItems.CountItems()) sel = (fItems.CountItems() - 1);
236 fCurSel = sel;
240 void Dialog::Dismiss()
242 delete this;
245 void Dialog::Refresh()
247 for(int i=0;ODItem *item = ItemAt(i);i++)
249 if (item->update)
250 (*item->update)(item);
254 void Dialog::Clear()
256 if (onclear)
257 (*onclear)();
259 for(int i=0;ODItem *item = ItemAt(i);i++)
260 delete item;
262 fItems.MakeEmpty();
263 fNumShown = 0;
264 fCurSel = 0;