00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "gui.h"
00012
00013
00014
00015
00016 GuiOptionBrowser::GuiOptionBrowser(int w, int h, OptionList * l)
00017 {
00018 width = w;
00019 height = h;
00020 options = l;
00021 selectable = true;
00022 listOffset = this->FindMenuItem(-1, 1);
00023 listChanged = true;
00024 selectedItem = 0;
00025 focus = 0;
00026
00027 trigA = new GuiTrigger;
00028 trigA->SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
00029
00030 btnSoundOver = new GuiSound(button_over_pcm, button_over_pcm_size, SOUND_PCM);
00031 btnSoundClick = new GuiSound(button_click_pcm, button_click_pcm_size, SOUND_PCM);
00032
00033 bgOptions = new GuiImageData(bg_options_png);
00034 bgOptionsImg = new GuiImage(bgOptions);
00035 bgOptionsImg->SetParent(this);
00036 bgOptionsImg->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
00037
00038 bgOptionsEntry = new GuiImageData(bg_options_entry_png);
00039
00040 scrollbar = new GuiImageData(scrollbar_png);
00041 scrollbarImg = new GuiImage(scrollbar);
00042 scrollbarImg->SetParent(this);
00043 scrollbarImg->SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
00044 scrollbarImg->SetPosition(0, 30);
00045
00046 arrowDown = new GuiImageData(scrollbar_arrowdown_png);
00047 arrowDownImg = new GuiImage(arrowDown);
00048 arrowDownOver = new GuiImageData(scrollbar_arrowdown_over_png);
00049 arrowDownOverImg = new GuiImage(arrowDownOver);
00050 arrowUp = new GuiImageData(scrollbar_arrowup_png);
00051 arrowUpImg = new GuiImage(arrowUp);
00052 arrowUpOver = new GuiImageData(scrollbar_arrowup_over_png);
00053 arrowUpOverImg = new GuiImage(arrowUpOver);
00054
00055 arrowUpBtn = new GuiButton(arrowUpImg->GetWidth(), arrowUpImg->GetHeight());
00056 arrowUpBtn->SetParent(this);
00057 arrowUpBtn->SetImage(arrowUpImg);
00058 arrowUpBtn->SetImageOver(arrowUpOverImg);
00059 arrowUpBtn->SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
00060 arrowUpBtn->SetSelectable(false);
00061 arrowUpBtn->SetTrigger(trigA);
00062 arrowUpBtn->SetSoundOver(btnSoundOver);
00063 arrowUpBtn->SetSoundClick(btnSoundClick);
00064
00065 arrowDownBtn = new GuiButton(arrowDownImg->GetWidth(), arrowDownImg->GetHeight());
00066 arrowDownBtn->SetParent(this);
00067 arrowDownBtn->SetImage(arrowDownImg);
00068 arrowDownBtn->SetImageOver(arrowDownOverImg);
00069 arrowDownBtn->SetAlignment(ALIGN_RIGHT, ALIGN_BOTTOM);
00070 arrowDownBtn->SetSelectable(false);
00071 arrowDownBtn->SetTrigger(trigA);
00072 arrowDownBtn->SetSoundOver(btnSoundOver);
00073 arrowDownBtn->SetSoundClick(btnSoundClick);
00074
00075 for(int i=0; i<PAGESIZE; i++)
00076 {
00077 optionTxt[i] = new GuiText(NULL, 20, (GXColor){0, 0, 0, 0xff});
00078 optionTxt[i]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
00079 optionTxt[i]->SetPosition(8,0);
00080
00081 optionVal[i] = new GuiText(NULL, 20, (GXColor){0, 0, 0, 0xff});
00082 optionVal[i]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
00083 optionVal[i]->SetPosition(250,0);
00084
00085 optionBg[i] = new GuiImage(bgOptionsEntry);
00086
00087 optionBtn[i] = new GuiButton(512,30);
00088 optionBtn[i]->SetParent(this);
00089 optionBtn[i]->SetLabel(optionTxt[i], 0);
00090 optionBtn[i]->SetLabel(optionVal[i], 1);
00091 optionBtn[i]->SetImageOver(optionBg[i]);
00092 optionBtn[i]->SetPosition(2,30*i+3);
00093 optionBtn[i]->SetTrigger(trigA);
00094 optionBtn[i]->SetSoundClick(btnSoundClick);
00095 }
00096 }
00097
00098
00099
00100
00101 GuiOptionBrowser::~GuiOptionBrowser()
00102 {
00103 delete arrowUpBtn;
00104 delete arrowDownBtn;
00105
00106 delete bgOptionsImg;
00107 delete scrollbarImg;
00108 delete arrowDownImg;
00109 delete arrowDownOverImg;
00110 delete arrowUpImg;
00111 delete arrowUpOverImg;
00112
00113 delete bgOptions;
00114 delete bgOptionsEntry;
00115 delete scrollbar;
00116 delete arrowDown;
00117 delete arrowDownOver;
00118 delete arrowUp;
00119 delete arrowUpOver;
00120
00121 delete trigA;
00122 delete btnSoundOver;
00123 delete btnSoundClick;
00124
00125 for(int i=0; i<PAGESIZE; i++)
00126 {
00127 delete optionTxt[i];
00128 delete optionVal[i];
00129 delete optionBg[i];
00130 delete optionBtn[i];
00131 }
00132 }
00133
00134 void GuiOptionBrowser::SetCol2Position(int x)
00135 {
00136 for(int i=0; i<PAGESIZE; i++)
00137 optionVal[i]->SetPosition(x,0);
00138 }
00139
00140 void GuiOptionBrowser::SetFocus(int f)
00141 {
00142 focus = f;
00143
00144 for(int i=0; i<PAGESIZE; i++)
00145 optionBtn[i]->ResetState();
00146
00147 if(f == 1)
00148 optionBtn[selectedItem]->SetState(STATE_SELECTED);
00149 }
00150
00151 void GuiOptionBrowser::ResetState()
00152 {
00153 if(state != STATE_DISABLED)
00154 {
00155 state = STATE_DEFAULT;
00156 stateChan = -1;
00157 }
00158
00159 for(int i=0; i<PAGESIZE; i++)
00160 {
00161 optionBtn[i]->ResetState();
00162 }
00163 }
00164
00165 int GuiOptionBrowser::GetClickedOption()
00166 {
00167 int found = -1;
00168 for(int i=0; i<PAGESIZE; i++)
00169 {
00170 if(optionBtn[i]->GetState() == STATE_CLICKED)
00171 {
00172 optionBtn[i]->SetState(STATE_SELECTED);
00173 found = optionIndex[i];
00174 break;
00175 }
00176 }
00177 return found;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 int GuiOptionBrowser::FindMenuItem(int currentItem, int direction)
00187 {
00188 int nextItem = currentItem + direction;
00189
00190 if(nextItem < 0 || nextItem >= options->length)
00191 return -1;
00192
00193 if(strlen(options->name[nextItem]) > 0)
00194 return nextItem;
00195 else
00196 return FindMenuItem(nextItem, direction);
00197 }
00198
00199
00200
00201
00202 void GuiOptionBrowser::Draw()
00203 {
00204 if(!this->IsVisible())
00205 return;
00206
00207 bgOptionsImg->Draw();
00208
00209 int next = listOffset;
00210
00211 for(int i=0; i<PAGESIZE; i++)
00212 {
00213 if(next >= 0)
00214 {
00215 optionBtn[i]->Draw();
00216 next = this->FindMenuItem(next, 1);
00217 }
00218 else
00219 break;
00220 }
00221
00222 scrollbarImg->Draw();
00223 arrowUpBtn->Draw();
00224 arrowDownBtn->Draw();
00225
00226 this->UpdateEffects();
00227 }
00228
00229 void GuiOptionBrowser::TriggerUpdate()
00230 {
00231 listChanged = true;
00232 }
00233
00234 void GuiOptionBrowser::Update(GuiTrigger * t)
00235 {
00236 if(state == STATE_DISABLED || !t)
00237 return;
00238
00239 int next, prev;
00240
00241 arrowUpBtn->Update(t);
00242 arrowDownBtn->Update(t);
00243
00244 next = listOffset;
00245
00246 if(listChanged)
00247 {
00248 listChanged = false;
00249 for(int i=0; i<PAGESIZE; i++)
00250 {
00251 if(next >= 0)
00252 {
00253 if(optionBtn[i]->GetState() == STATE_DISABLED)
00254 {
00255 optionBtn[i]->SetVisible(true);
00256 optionBtn[i]->SetState(STATE_DEFAULT);
00257 }
00258
00259 optionTxt[i]->SetText(options->name[next]);
00260 optionVal[i]->SetText(options->value[next]);
00261 optionIndex[i] = next;
00262 next = this->FindMenuItem(next, 1);
00263 }
00264 else
00265 {
00266 optionBtn[i]->SetVisible(false);
00267 optionBtn[i]->SetState(STATE_DISABLED);
00268 }
00269 }
00270 }
00271
00272 for(int i=0; i<PAGESIZE; i++)
00273 {
00274 if(i != selectedItem && optionBtn[i]->GetState() == STATE_SELECTED)
00275 optionBtn[i]->ResetState();
00276 else if(focus && i == selectedItem && optionBtn[i]->GetState() == STATE_DEFAULT)
00277 optionBtn[selectedItem]->SetState(STATE_SELECTED, t->chan);
00278
00279 int currChan = t->chan;
00280
00281 if(t->wpad->ir.valid && !optionBtn[i]->IsInside(t->wpad->ir.x, t->wpad->ir.y))
00282 t->chan = -1;
00283
00284 optionBtn[i]->Update(t);
00285 t->chan = currChan;
00286
00287 if(optionBtn[i]->GetState() == STATE_SELECTED)
00288 selectedItem = i;
00289 }
00290
00291
00292 if(!focus)
00293 return;
00294
00295 if(t->Down() || arrowDownBtn->GetState() == STATE_CLICKED)
00296 {
00297 next = this->FindMenuItem(optionIndex[selectedItem], 1);
00298
00299 if(next >= 0)
00300 {
00301 if(selectedItem == PAGESIZE-1)
00302 {
00303
00304 listOffset = this->FindMenuItem(listOffset, 1);
00305 listChanged = true;
00306 }
00307 else if(optionBtn[selectedItem+1]->IsVisible())
00308 {
00309 optionBtn[selectedItem]->ResetState();
00310 optionBtn[selectedItem+1]->SetState(STATE_SELECTED, t->chan);
00311 selectedItem++;
00312 }
00313 }
00314 arrowDownBtn->ResetState();
00315 }
00316 else if(t->Up() || arrowUpBtn->GetState() == STATE_CLICKED)
00317 {
00318 prev = this->FindMenuItem(optionIndex[selectedItem], -1);
00319
00320 if(prev >= 0)
00321 {
00322 if(selectedItem == 0)
00323 {
00324
00325 listOffset = prev;
00326 listChanged = true;
00327 }
00328 else
00329 {
00330 optionBtn[selectedItem]->ResetState();
00331 optionBtn[selectedItem-1]->SetState(STATE_SELECTED, t->chan);
00332 selectedItem--;
00333 }
00334 }
00335 arrowUpBtn->ResetState();
00336 }
00337
00338 if(updateCB)
00339 updateCB(this);
00340 }