NXEngine v1.0.0.2
[NXEngine.git] / TextBox / StageSelect.cpp
blobbf35f2b028177421c39d7d4643b0c034cc12c20b
2 /*
3 The stage-select dialog when using the
4 teleporter in Arthur's House.
5 */
7 #include "../nx.h"
8 #include "StageSelect.h"
9 #include "StageSelect.fdh"
11 #define WARP_X 128
12 #define WARP_Y 46
14 #define WARP_Y_START (WARP_Y + 8)
15 #define WARP_Y_SPEED 1
17 #define LOCS_Y (WARP_Y + 16)
18 #define LOCS_SPACING 8
20 TB_StageSelect::TB_StageSelect()
22 ClearSlots();
26 void c------------------------------() {}
29 void TB_StageSelect::ResetState()
31 fVisible = false;
34 void TB_StageSelect::SetVisible(bool enable)
36 fVisible = enable;
37 fWarpY = WARP_Y_START;
39 game.frozen = enable;
40 textbox.SetFlags(TB_CURSOR_NEVER_SHOWN, enable);
41 textbox.SetFlags(TB_LINE_AT_ONCE, enable);
42 textbox.SetFlags(TB_VARIABLE_WIDTH_CHARS, enable);
44 fSelectionIndex = 0;
45 fLastButtonDown = true;
47 if (enable)
49 fMadeSelection = false;
50 textbox.ClearText();
51 UpdateText();
55 bool TB_StageSelect::IsVisible()
57 return fVisible;
61 void c------------------------------() {}
64 void TB_StageSelect::Draw(void)
66 if (!fVisible)
67 return;
69 // handle user input
70 HandleInput();
72 // draw "- WARP -" text
73 fWarpY -= WARP_Y_SPEED;
74 if (fWarpY < WARP_Y) fWarpY = WARP_Y;
76 draw_sprite(WARP_X, fWarpY, SPR_TEXT_WARP, 0);
78 // draw teleporter locations
79 int nslots = CountActiveSlots();
80 int total_spacing = ((nslots - 1) * LOCS_SPACING);
81 int total_width = total_spacing + (nslots * sprites[SPR_STAGEIMAGE].w);
82 int x = (SCREEN_WIDTH / 2) - (total_width / 2);
84 for(int i=0;i<nslots;i++)
86 int sprite;
87 GetSlotByIndex(i, &sprite, NULL);
89 draw_sprite(x, LOCS_Y, SPR_STAGEIMAGE, sprite);
91 if (i == fSelectionIndex)
93 fSelectionFrame ^= 1;
94 draw_sprite(x, LOCS_Y, SPR_SELECTOR_ITEMS, fSelectionFrame);
97 x += (sprites[SPR_STAGEIMAGE].w + LOCS_SPACING);
102 void c------------------------------() {}
105 void TB_StageSelect::HandleInput()
107 bool button_down;
109 if (textbox.YesNoPrompt.IsVisible() || fMadeSelection)
110 return;
112 if (justpushed(LEFTKEY))
114 MoveSelection(LEFT);
116 else if (justpushed(RIGHTKEY))
118 MoveSelection(RIGHT);
121 // when user picks a location return the new script to execute
122 button_down = buttondown();
123 if (button_down && !fLastButtonDown)
125 int scriptno;
126 if (!GetSlotByIndex(fSelectionIndex, NULL, &scriptno))
128 stat("StageSelect: starting activation script %d", scriptno);
129 JumpScript(scriptno, SP_MAP);
131 else
132 { // dismiss "no permission to teleport"
133 StopScripts();
136 fMadeSelection = true;
139 fLastButtonDown = button_down;
142 void TB_StageSelect::MoveSelection(int dir)
144 int numslots = CountActiveSlots();
145 if (numslots == 0) return;
147 if (dir == RIGHT)
149 if (++fSelectionIndex >= numslots)
150 fSelectionIndex = 0;
152 else
154 if (--fSelectionIndex < 0)
155 fSelectionIndex = (numslots - 1);
158 sound(SND_MENU_MOVE);
159 UpdateText();
162 // updates the text by running the appropriate script
163 // from StageSelect.tsc
164 void TB_StageSelect::UpdateText()
166 int scriptno;
168 if (GetSlotByIndex(fSelectionIndex, NULL, &scriptno))
169 { // no permission to teleport
170 scriptno = 0;
172 else
174 scriptno %= 1000;
177 JumpScript(scriptno + 1000, SP_STAGESELECT);
181 void c------------------------------() {}
184 // set teleporter slot "slotno" to run script "scriptno" when selected.
185 // this adds the slot to the menu if scriptno is nonzero and removes it if zero.
186 // the parameters here map directory to the <PS+ in the script.
187 void TB_StageSelect::SetSlot(int slotno, int scriptno)
189 if (slotno >= 0 && slotno < NUM_TELEPORTER_SLOTS)
191 fSlots[slotno] = scriptno;
193 else
195 stat("StageSelect::SetSlot: invalid slotno %d", slotno);
199 void TB_StageSelect::ClearSlots()
201 for(int i=0;i<NUM_TELEPORTER_SLOTS;i++)
202 fSlots[i] = -1;
205 // return the slotno and scriptno associated with the n'th enabled teleporter slot,
206 // where n = index.
207 // i.e. passing 1 for index returns the 2nd potential teleporter destination.
208 // if index is higher than the number of active teleporter slots, returns nonzero.
209 bool TB_StageSelect::GetSlotByIndex(int index, int *slotno_out, int *scriptno_out)
211 if (index >= 0)
213 int slots_found = 0;
215 for(int i=0;i<NUM_TELEPORTER_SLOTS;i++)
217 if (fSlots[i] != -1)
219 if (++slots_found > index)
221 if (slotno_out) *slotno_out = i;
222 if (scriptno_out) *scriptno_out = fSlots[i];
223 return 0;
229 if (slotno_out) *slotno_out = -1;
230 if (scriptno_out) *scriptno_out = -1;
231 return 1;
234 int TB_StageSelect::CountActiveSlots()
236 int count = 0;
238 for(int i=0;i<NUM_TELEPORTER_SLOTS;i++)
240 if (fSlots[i] != -1)
241 count++;
244 return count;