userCreateScr.cpp

00001 /*
00002   ユーザ作成・削除画面の管理
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "userCreateScr.h"
00008 #include "userCreateScrUnicode.h"
00009 #include "nettyping.h"
00010 
00011 
00012 UserCreateScr::UserCreateScr(void)
00013   : focus_changed(false),
00014     title(NULL), back_normal(NULL), back_focused(NULL),
00015     list_back(NULL), input_box(NULL),
00016     del_menu(NULL), new_user(NULL) {
00017 
00018   for (unsigned int i = 0; i < UserMax; ++i) {
00019     delList_normal[i] = NULL;
00020     delList_focused[i] = NULL;
00021   }
00022 }
00023 
00024 
00025 UserCreateScr::~UserCreateScr(void) {
00026   deleteObject();
00027 }
00028 
00029 
00030 void UserCreateScr::deleteObject(void) {
00031   for (unsigned int i = 0; i < UserMax; ++i) {
00032     delete delList_normal[i];
00033     delList_normal[i] = NULL;
00034 
00035     delete delList_focused[i];
00036     delList_focused[i] = NULL;
00037   }
00038 
00039   delete title;
00040   delete back_normal;
00041   delete back_focused;
00042   delete new_normal;
00043   delete new_focused;
00044   delete del_normal;
00045   delete del_focused;
00046   delete list_back;
00047   delete input_box;
00048 }
00049 
00050 
00051 void UserCreateScr::recreate(void) {
00052   win->lock();
00053   win->clear(ForeDepth);
00054   deleteObject();
00055   Initialize(0, NULL);
00056   win->unlock();
00057 }
00058 
00059 
00060 int UserCreateScr::Initialize(int argc, char *argv[]) {
00061   enum {
00062     ListTop = 80,
00063   };
00064   win->clear(ForeDepth);
00065 
00066   // タイトル
00067   int top = ListTop;
00068   title = new DrawSurface(ttf->createText(UserCreate_title, NormalSize,
00069                                          Fore, Back), true);
00070   win->add((new LabelComponent(title))
00071            ->setPosition(Grid(win->w/2, top), Center | Bottom), ForeDepth);
00072 
00073   // 背景
00074   top += 8;
00075   list_back = new FillSurface(Rect(NormalSize * (UserNameLength /2 +2),
00076                                    NormalSize * (UserMax +1)), Black);
00077   win->add((new LabelComponent(list_back))
00078            ->setPosition(Grid(win->w/2, top), Center), ForeDepth);
00079   int list_top = top;
00080   top += NormalSize * (UserMax +1) +8;
00081 
00082   // 新規ボタン
00083   new_normal = new DrawSurface(ttf->createText(UserCreate_new, NormalSize,
00084                                                Fore, Back), true);
00085   new_focused = new DrawSurface(ttf->createText(UserCreate_new, NormalSize,
00086                                                 Focused, Back), true);
00087   new_btn = new ButtonComponent(new_normal);
00088   new_btn->setFocusedSurface(new_focused);
00089 
00090   // 削除ボタン
00091   del_normal = new DrawSurface(ttf->createText(UserCreate_del, NormalSize,
00092                                                Fore, Back), true);
00093   del_focused = new DrawSurface(ttf->createText(UserCreate_del, NormalSize,
00094                                                 Focused, Back), true);
00095   del_btn = new ButtonComponent(del_normal);
00096   del_btn->setFocusedSurface(del_focused);
00097 
00098   // 戻るボタン
00099   back_normal = new DrawSurface(ttf->createText(UserCreate_back, NormalSize,
00100                                                Fore, Back), true);
00101   back_focused = new DrawSurface(ttf->createText(UserCreate_back, NormalSize,
00102                                                 Focused, Back), true);
00103   back_btn = new ButtonComponent(back_normal);
00104   back_btn->setFocusedSurface(back_focused);
00105 
00106   // 操作のメニュー管理
00107   menu = new MenuComponent();
00108   menu->add(new_btn);
00109   menu->add(del_btn);
00110   menu->add(back_btn);
00111   menu->setOffset(VXV::Rect((NormalSize + 8) * 2, 0));
00112   menu->changeKeySettings(false);
00113   menu->defaultSelection(true);
00114 
00115   menu->activate(true);
00116   menu->setPosition(Grid(win->w/2, top), Center);
00117   win->add(menu, ForeDepth);
00118 
00119   // 存在するユーザ情報の表示
00120   int user_y = 0;
00121   unsigned int users_size = property->users.size();
00122 
00123   if (users_size < UserMax) {
00124     // !!! 「新規」の選択を disable にする
00125   }
00126 
00127   // ユーザ一覧のメニュー作成
00128   del_menu = new MenuComponent();
00129   for (unsigned int i = 0; i < users_size; ++i) {
00130     delList_normal[i] =
00131       new DrawSurface(ttf->createText(property->users[i].name.c_str(),
00132                                       NormalSize, White, Black), true);
00133     delList_focused[i] =
00134       new DrawSurface(ttf->createText(property->users[i].name.c_str(),
00135                                       NormalSize, White, Focused), false);
00136     delList_btn[i] = new ButtonComponent(delList_normal[i]);
00137     delList_btn[i]->setFocusedSurface(delList_focused[i]);
00138     del_menu->add(delList_btn[i]);
00139   }
00140   del_menu->setOffset(VXV::Rect(0, NormalSize));
00141   del_menu->defaultSelection(false);
00142   del_menu->activate(true);
00143   del_menu->setPosition(Grid((win->w -list_back->w +NormalSize)/2,list_top+8));
00144   user_y += del_menu->h;
00145   del_menu->endFocus();
00146   win->add(del_menu, ForeDepth);
00147 
00148   // 区切り線
00149   input_box = NULL;
00150   if (users_size < UserMax) {
00151     input_box =
00152       new FillSurface(VXV::Rect(NormalSize * (UserNameLength/2 +1)+2,
00153                                 NormalSize +2), Gray8);
00154     win->add((new LabelComponent(input_box))
00155              ->setPosition(Grid(win->w/2, list_top +user_y +7), Center),
00156              ForeDepth);
00157   }
00158 
00159   // 新規ユーザ名の入力欄を作成
00160   new_user = NULL;
00161   if (users_size < UserMax) {
00162     new_user =
00163       new TextInputComponent(*ttf, NormalSize,
00164                              VXV::Rect(NormalSize * (UserNameLength/2 +1),
00165                                        NormalSize), White, Black);
00166     new_user->setPosition(Grid(win->w/2, user_y + list_top + 8), Center);
00167     new_user->setBufferMax(UserNameLength);
00168     new_user->disable(true);
00169     win->add(new_user, ForeDepth);
00170   }
00171   return 0;
00172 }
00173 
00174 
00175 // 新規ユーザの入力が確定した場合の処理
00176 void UserCreateScr::handleNewUserCreation(void) {
00177   if (!new_user || !new_user->isDecided()) {
00178     return;
00179   }
00180 
00181   std::vector<char> char_name;
00182   char_name.assign(new_user->inputed.begin(), new_user->inputed.end());
00183 
00184   // 前後の空白を取り除く
00185   for (std::vector<char>::iterator it = char_name.begin();
00186        (it != char_name.end()) && (isspace(*it));) {
00187     it = char_name.erase(it);
00188   }
00189   for (std::vector<char>::iterator it = char_name.end() -1;
00190        !char_name.empty() && it != char_name.begin();) {
00191     if  (isspace(*it)) {
00192       it = char_name.erase(it);
00193     }
00194     --it;
00195   }
00196 
00197   char_name.push_back('\0');
00198   int ret = property->addUser(&char_name[0]);
00199   if (ret < 0) {
00200     switch (ret) {
00201     case Property::AlreadyExist:
00202       // 同じ名前のユーザが既に存在する
00203       // !!!
00204       break;
00205 
00206     case Property::InvalidName:
00207       // !!!
00208       break;
00209     }
00210   } else {
00211     property->setUserIndex(property->users.size() -1);
00212   }
00213   win->unlock();
00214   recreate();
00215   win->lock();
00216   if (ret >= 0) {
00217     menu->setNowIndex(2);
00218   }
00219 }
00220 
00221 
00222 // ユーザの削除が確定したときの処理
00223 void UserCreateScr::handleUserDeleteMenu(void) {
00224   if (!del_menu || !del_menu->isDecided()) {
00225     return;
00226   }
00227 
00228   int index = del_menu->getNowIndex();
00229   if (index < 1) {
00230     // guest は削除させない
00231     // !!!
00232   } else {
00233     // !!! 削除しますか? というサブウィンドウを作るとか
00234     property->delUser(index);
00235   }
00236   win->unlock();
00237   recreate();
00238   win->lock();
00239   menu->setNowIndex(0);
00240 }
00241 
00242 
00243 void UserCreateScr::setFocusToMenu(void) {
00244   if (new_user) {
00245     new_user->endFocus();
00246   }
00247   del_menu->endFocus();
00248   del_menu->defaultSelection(false);
00249   menu->beginFocus();
00250   focus_changed = false;
00251 }
00252 
00253 
00254 int UserCreateScr::MainLoop(void) {
00255 
00256   bool quit = false;
00257   do {
00258     win->lock();
00259 
00260     handleNewUserCreation();
00261     handleUserDeleteMenu();
00262 
00263     if (menu->isDecided()) {
00264       int index = menu->getNowIndex();
00265       if (index == 0) {
00266         if (new_user) {
00267           // 新規
00268           menu->defaultSelection(false);
00269           menu->endFocus();
00270           new_user->beginFocus();
00271           focus_changed = true;
00272         } else {
00273           // !!! ユーザ数がいっぱいです、とか表示
00274           menu->setNowIndex(1);
00275           menu->activate(true);
00276         }
00277 
00278       } else if (index == 1) {
00279         // 削除
00280         menu->defaultSelection(false);
00281         menu->endFocus();
00282         del_menu->defaultSelection(true);
00283         del_menu->beginFocus();
00284         focus_changed = true;
00285 
00286       } else if (index == 2) {
00287         // 戻る
00288         quit = true;
00289       }
00290     }
00291 
00292     if (back_btn->isPressed()) {
00293       quit = true;
00294     }
00295     UserInput::userInput_t ui = UserInput::getInputed();
00296     if (ui.isPressedCode(SDLK_ESCAPE)) {
00297       if (focus_changed) {
00298         setFocusToMenu();
00299       } else {
00300         quit = true;
00301       }
00302     }
00303     if (ui.left_released && focus_changed) {
00304       setFocusToMenu();
00305     }
00306     quit |= ui.quit;
00307     win->unlock();
00308 
00309     SDL_Delay(10);
00310   } while (!quit);
00311 
00312   return 0;
00313 }
00314 

Generated on Mon Apr 13 22:52:03 2009 by  doxygen 1.5.7.1