CIntObjectClasses.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. #include "StdInc.h"
  2. #include "CIntObjectClasses.h"
  3. #include "../CBitmapHandler.h"
  4. #include "SDL_Extensions.h"
  5. #include "../Graphics.h"
  6. #include "../CAnimation.h"
  7. #include "../CGameInfo.h"
  8. #include "../../CCallback.h"
  9. #include "../CConfigHandler.h"
  10. #include "../BattleInterface/CBattleInterface.h"
  11. #include "../BattleInterface/CBattleInterfaceClasses.h"
  12. #include "../CPlayerInterface.h"
  13. #include "../CMessage.h"
  14. #include "../CMusicHandler.h"
  15. #include "../GUIClasses.h"
  16. #include "CGuiHandler.h"
  17. #include "../CAdvmapInterface.h"
  18. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  19. {
  20. init();
  21. bg = BG;
  22. freeSurf = Free;
  23. pos.x += x;
  24. pos.y += y;
  25. pos.w = BG->w;
  26. pos.h = BG->h;
  27. }
  28. CPicture::CPicture( const std::string &bmpname, int x, int y )
  29. {
  30. init();
  31. bg = BitmapHandler::loadBitmap(bmpname);
  32. freeSurf = true;;
  33. pos.x += x;
  34. pos.y += y;
  35. if(bg)
  36. {
  37. pos.w = bg->w;
  38. pos.h = bg->h;
  39. }
  40. else
  41. {
  42. pos.w = pos.h = 0;
  43. }
  44. }
  45. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  46. {
  47. init();
  48. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  49. }
  50. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  51. {
  52. init();
  53. createSimpleRect(r, screenFormat, color);
  54. }
  55. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  56. {
  57. needRefresh = false;
  58. srcRect = new Rect(SrcRect);
  59. pos.x += x;
  60. pos.y += y;
  61. pos.w = srcRect->w;
  62. pos.h = srcRect->h;
  63. bg = BG;
  64. freeSurf = free;
  65. }
  66. CPicture::~CPicture()
  67. {
  68. if(freeSurf)
  69. SDL_FreeSurface(bg);
  70. delete srcRect;
  71. }
  72. void CPicture::init()
  73. {
  74. needRefresh = false;
  75. srcRect = NULL;
  76. }
  77. void CPicture::show(SDL_Surface * to)
  78. {
  79. if (needRefresh)
  80. showAll(to);
  81. }
  82. void CPicture::showAll(SDL_Surface * to)
  83. {
  84. if(bg)
  85. {
  86. if(srcRect)
  87. {
  88. SDL_Rect srcRectCpy = *srcRect;
  89. SDL_Rect dstRect = srcRectCpy;
  90. dstRect.x = pos.x;
  91. dstRect.y = pos.y;
  92. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  93. }
  94. else
  95. blitAt(bg, pos, to);
  96. }
  97. }
  98. void CPicture::convertToScreenBPP()
  99. {
  100. SDL_Surface *hlp = bg;
  101. bg = SDL_ConvertSurface(hlp,screen->format,0);
  102. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  103. SDL_FreeSurface(hlp);
  104. }
  105. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  106. {
  107. pos += r;
  108. pos.w = r.w;
  109. pos.h = r.h;
  110. if(screenFormat)
  111. bg = CSDL_Ext::newSurface(r.w, r.h);
  112. else
  113. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  114. SDL_FillRect(bg, NULL, color);
  115. freeSurf = true;
  116. }
  117. void CPicture::colorizeAndConvert(int player)
  118. {
  119. assert(bg);
  120. colorize(player);
  121. convertToScreenBPP();
  122. }
  123. void CPicture::colorize(int player)
  124. {
  125. assert(bg);
  126. assert(bg->format->BitsPerPixel == 8);
  127. graphics->blueToPlayersAdv(bg, player);
  128. }
  129. CButtonBase::CButtonBase()
  130. {
  131. swappedImages = keepFrame = false;
  132. bitmapOffset = 0;
  133. state=NORMAL;
  134. image = NULL;
  135. text = NULL;
  136. }
  137. CButtonBase::~CButtonBase()
  138. {
  139. }
  140. void CButtonBase::update()
  141. {
  142. if (text)
  143. {
  144. if (state == PRESSED)
  145. text->moveTo(Point(pos.x+pos.w/2+1, pos.y+pos.h/2+1));
  146. else
  147. text->moveTo(Point(pos.x+pos.w/2, pos.y+pos.h/2));
  148. }
  149. int newPos = (int)state + bitmapOffset;
  150. if (newPos < 0)
  151. newPos = 0;
  152. if (state == HIGHLIGHTED && image->size() < 4)
  153. newPos = image->size()-1;
  154. if (swappedImages)
  155. {
  156. if (newPos == 0) newPos = 1;
  157. else if (newPos == 1) newPos = 0;
  158. }
  159. if (!keepFrame)
  160. image->setFrame(newPos);
  161. if (active)
  162. redraw();
  163. }
  164. void CButtonBase::addTextOverlay( const std::string &Text, EFonts font, SDL_Color color)
  165. {
  166. OBJ_CONSTRUCTION_CAPTURING_ALL;
  167. delChild(text);
  168. text = new CLabel(pos.w/2, pos.h/2, font, CENTER, color, Text);
  169. update();
  170. }
  171. void CButtonBase::setOffset(int newOffset)
  172. {
  173. if (bitmapOffset == newOffset)
  174. return;
  175. bitmapOffset = newOffset;
  176. update();
  177. }
  178. void CButtonBase::setState(ButtonState newState)
  179. {
  180. if (state == newState)
  181. return;
  182. state = newState;
  183. update();
  184. }
  185. CButtonBase::ButtonState CButtonBase::getState()
  186. {
  187. return state;
  188. }
  189. bool CButtonBase::isBlocked()
  190. {
  191. return state == BLOCKED;
  192. }
  193. bool CButtonBase::isHighlighted()
  194. {
  195. return state == HIGHLIGHTED;
  196. }
  197. void CButtonBase::block(bool on)
  198. {
  199. setState(on?BLOCKED:NORMAL);
  200. }
  201. CAdventureMapButton::CAdventureMapButton ()
  202. {
  203. hoverable = actOnDown = borderEnabled = soundDisabled = false;
  204. borderColor.unused = 1; // represents a transparent color, used for HighlightableButton
  205. used = LCLICK | RCLICK | HOVER | KEYBOARD;
  206. }
  207. CAdventureMapButton::CAdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName,int key, std::vector<std::string> * add, bool playerColoredButton )
  208. {
  209. std::map<int,std::string> pom;
  210. pom[0] = Name;
  211. init(Callback, pom, HelpBox, playerColoredButton, defName, add, x, y, key);
  212. }
  213. CAdventureMapButton::CAdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key/*=0*/ )
  214. {
  215. std::map<int,std::string> pom;
  216. pom[0] = Name;
  217. init(Callback, pom, HelpBox, info->playerColoured, info->defName, &info->additionalDefs, info->x, info->y, key);
  218. }
  219. CAdventureMapButton::CAdventureMapButton( const std::pair<std::string, std::string> &help, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
  220. {
  221. std::map<int,std::string> pom;
  222. pom[0] = help.first;
  223. init(Callback, pom, help.second, playerColoredButton, defName, add, x, y, key);
  224. }
  225. void CAdventureMapButton::clickLeft(tribool down, bool previousState)
  226. {
  227. if(isBlocked())
  228. return;
  229. if (down)
  230. {
  231. if (!soundDisabled)
  232. CCS->soundh->playSound(soundBase::button);
  233. setState(PRESSED);
  234. }
  235. else if(hoverable && hovered)
  236. setState(HIGHLIGHTED);
  237. else
  238. setState(NORMAL);
  239. if (actOnDown && down)
  240. {
  241. callback();
  242. }
  243. else if (!actOnDown && previousState && (down==false))
  244. {
  245. callback();
  246. }
  247. }
  248. void CAdventureMapButton::clickRight(tribool down, bool previousState)
  249. {
  250. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  251. CRClickPopup::createAndPush(helpBox);
  252. }
  253. void CAdventureMapButton::hover (bool on)
  254. {
  255. if(hoverable)
  256. {
  257. if(on)
  258. setState(HIGHLIGHTED);
  259. else
  260. setState(NORMAL);
  261. }
  262. if(pressedL && on)
  263. setState(PRESSED);
  264. std::string *name = (vstd::contains(hoverTexts,getState()))
  265. ? (&hoverTexts[getState()])
  266. : (vstd::contains(hoverTexts,0) ? (&hoverTexts[0]) : NULL);
  267. if(name && name->size() && !isBlocked()) //if there is no name, there is nohing to display also
  268. {
  269. if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
  270. {
  271. if(on && LOCPLINT->battleInt->console->alterTxt == "")
  272. {
  273. LOCPLINT->battleInt->console->alterTxt = *name;
  274. LOCPLINT->battleInt->console->whoSetAlter = 1;
  275. }
  276. else if (LOCPLINT->battleInt->console->alterTxt == *name)
  277. {
  278. LOCPLINT->battleInt->console->alterTxt = "";
  279. LOCPLINT->battleInt->console->whoSetAlter = 0;
  280. }
  281. }
  282. else if(GH.statusbar) //for other buttons
  283. {
  284. if (on)
  285. GH.statusbar->print(*name);
  286. else if ( GH.statusbar->getCurrent()==(*name) )
  287. GH.statusbar->clear();
  288. }
  289. }
  290. }
  291. void CAdventureMapButton::init(const CFunctionList<void()> &Callback, const std::map<int,std::string> &Name, const std::string &HelpBox, bool playerColoredButton, const std::string &defName, std::vector<std::string> * add, int x, int y, int key)
  292. {
  293. currentImage = -1;
  294. used = LCLICK | RCLICK | HOVER | KEYBOARD;
  295. callback = Callback;
  296. hoverable = actOnDown = borderEnabled = soundDisabled = false;
  297. borderColor.unused = 1; // represents a transparent color, used for HighlightableButton
  298. assignedKeys.insert(key);
  299. hoverTexts = Name;
  300. helpBox=HelpBox;
  301. pos.x += x;
  302. pos.y += y;
  303. if (!defName.empty())
  304. imageNames.push_back(defName);
  305. if (add)
  306. for (size_t i=0; i<add->size();i++ )
  307. imageNames.push_back(add->at(i));
  308. setIndex(0, playerColoredButton);
  309. }
  310. void CAdventureMapButton::setIndex(size_t index, bool playerColoredButton)
  311. {
  312. if (index == currentImage || index>=imageNames.size())
  313. return;
  314. currentImage = index;
  315. setImage(new CAnimation(imageNames[index]), playerColoredButton);
  316. }
  317. void CAdventureMapButton::setImage(CAnimation* anim, bool playerColoredButton, int animFlags)
  318. {
  319. OBJ_CONSTRUCTION_CAPTURING_ALL;
  320. if (image && active)
  321. image->deactivate();
  322. delChild(image);
  323. image = new CAnimImage(anim, getState(), 0, 0, 0, animFlags);
  324. if (active)
  325. image->activate();
  326. if (playerColoredButton)
  327. image->playerColored(LOCPLINT->playerID);
  328. pos.w = image->pos.w;
  329. pos.h = image->pos.h;
  330. }
  331. void CAdventureMapButton::setPlayerColor(int player)
  332. {
  333. if (image)
  334. image->playerColored(player);
  335. }
  336. void CAdventureMapButton::showAll(SDL_Surface * to)
  337. {
  338. CIntObject::showAll(to);
  339. if (borderEnabled && borderColor.unused == 0)
  340. CSDL_Ext::drawBorder(to, pos.x - 1, pos.y - 1, pos.w + 2, pos.h + 2, int3(borderColor.r, borderColor.g, borderColor.b));
  341. }
  342. void CHighlightableButton::select(bool on)
  343. {
  344. selected = on;
  345. if (on)
  346. {
  347. setState(HIGHLIGHTED);
  348. callback();
  349. borderEnabled = true;
  350. }
  351. else
  352. {
  353. setState(NORMAL);
  354. callback2();
  355. borderEnabled = false;
  356. }
  357. if(hoverTexts.size()>1)
  358. {
  359. hover(false);
  360. hover(true);
  361. }
  362. }
  363. void CHighlightableButton::clickLeft(tribool down, bool previousState)
  364. {
  365. if(isBlocked())
  366. return;
  367. if (down && !(onlyOn && isHighlighted()))
  368. {
  369. CCS->soundh->playSound(soundBase::button);
  370. setState(PRESSED);
  371. }
  372. if(previousState)//mouse up
  373. {
  374. if(down == false && getState() == PRESSED)
  375. select(!selected);
  376. else
  377. setState(selected?HIGHLIGHTED:NORMAL);
  378. }
  379. }
  380. CHighlightableButton::CHighlightableButton( const CFunctionList<void()> &onSelect, const CFunctionList<void()> &onDeselect, const std::map<int,std::string> &Name, const std::string &HelpBox, bool playerColoredButton, const std::string &defName, std::vector<std::string> * add, int x, int y, int key)
  381. : onlyOn(false), selected(false), callback2(onDeselect)
  382. {
  383. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  384. }
  385. CHighlightableButton::CHighlightableButton( const std::pair<std::string, std::string> &help, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
  386. : onlyOn(false), selected(false) // TODO: callback2(???)
  387. {
  388. ID = myid;
  389. std::map<int,std::string> pom;
  390. pom[0] = help.first;
  391. init(onSelect, pom, help.second, playerColoredButton, defName, add, x, y, key);
  392. }
  393. CHighlightableButton::CHighlightableButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
  394. : onlyOn(false), selected(false) // TODO: callback2(???)
  395. {
  396. ID = myid;
  397. std::map<int,std::string> pom;
  398. pom[0] = Name;
  399. init(onSelect, pom,HelpBox, playerColoredButton, defName, add, x, y, key);
  400. }
  401. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  402. {
  403. if (bt->parent)
  404. bt->parent->removeChild(bt);
  405. addChild(bt);
  406. bt->recActions = defActions;//FIXME: not needed?
  407. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  408. bt->onlyOn = true;
  409. buttons.push_back(bt);
  410. }
  411. void CHighlightableButtonsGroup::addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid, const CFunctionList<void()> &OnSelect, int key)
  412. {
  413. OBJ_CONSTRUCTION_CAPTURING_ALL;
  414. CHighlightableButton *bt = new CHighlightableButton(OnSelect, 0, tooltip, HelpBox, false, defName, 0, x, y, key);
  415. if(musicLike)
  416. {
  417. if (buttons.size() > 3)
  418. bt->setOffset(buttons.size()-3);
  419. }
  420. bt->ID = uid;
  421. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  422. bt->onlyOn = true;
  423. buttons.push_back(bt);
  424. }
  425. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
  426. : onChange(OnChange), musicLike(musicLikeButtons)
  427. {}
  428. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  429. {
  430. }
  431. void CHighlightableButtonsGroup::select(int id, bool mode)
  432. {
  433. CHighlightableButton *bt = NULL;
  434. if(mode)
  435. {
  436. for(size_t i=0;i<buttons.size() && !bt; ++i)
  437. if (buttons[i]->ID == id)
  438. bt = buttons[i];
  439. }
  440. else
  441. {
  442. bt = buttons[id];
  443. }
  444. bt->select(true);
  445. selectionChanged(bt->ID);
  446. }
  447. void CHighlightableButtonsGroup::selectionChanged(int to)
  448. {
  449. for(size_t i=0;i<buttons.size(); ++i)
  450. if(buttons[i]->ID!=to && buttons[i]->isHighlighted())
  451. buttons[i]->select(false);
  452. onChange(to);
  453. if (parent)
  454. parent->redraw();
  455. }
  456. void CHighlightableButtonsGroup::show(SDL_Surface * to)
  457. {
  458. if (musicLike)
  459. {
  460. for(size_t i=0;i<buttons.size(); ++i)
  461. if(buttons[i]->isHighlighted())
  462. buttons[i]->show(to);
  463. }
  464. else
  465. CIntObject::show(to);
  466. }
  467. void CHighlightableButtonsGroup::showAll(SDL_Surface * to)
  468. {
  469. if (musicLike)
  470. {
  471. for(size_t i=0;i<buttons.size(); ++i)
  472. if(buttons[i]->isHighlighted())
  473. buttons[i]->showAll(to);
  474. }
  475. else
  476. CIntObject::showAll(to);
  477. }
  478. void CHighlightableButtonsGroup::block( ui8 on )
  479. {
  480. for(size_t i=0;i<buttons.size(); ++i)
  481. {
  482. buttons[i]->block(on);
  483. }
  484. }
  485. void CSlider::sliderClicked()
  486. {
  487. if(!(active & MOVE))
  488. {
  489. activateMouseMove();
  490. used |= MOVE;
  491. }
  492. }
  493. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  494. {
  495. double v = 0;
  496. if(horizontal)
  497. {
  498. if( std::abs(sEvent.y-(pos.y+pos.h/2)) > pos.h/2+40 || std::abs(sEvent.x-(pos.x+pos.w/2)) > pos.w/2 )
  499. return;
  500. v = sEvent.x - pos.x - 24;
  501. v *= positions;
  502. v /= (pos.w - 48);
  503. }
  504. else
  505. {
  506. if(std::abs(sEvent.x-(pos.x+pos.w/2)) > pos.w/2+40 || std::abs(sEvent.y-(pos.y+pos.h/2)) > pos.h/2 )
  507. return;
  508. v = sEvent.y - pos.y - 24;
  509. v *= positions;
  510. v /= (pos.h - 48);
  511. }
  512. v += 0.5;
  513. if(v!=value)
  514. {
  515. moveTo(v);
  516. redrawSlider();
  517. }
  518. }
  519. void CSlider::redrawSlider()
  520. {
  521. //slider->show(screenBuf);
  522. }
  523. void CSlider::moveLeft()
  524. {
  525. moveTo(value-1);
  526. }
  527. void CSlider::moveRight()
  528. {
  529. moveTo(value+1);
  530. }
  531. void CSlider::moveTo(int to)
  532. {
  533. vstd::amax(to, 0);
  534. vstd::amin(to, positions);
  535. //same, old position?
  536. if(value == to)
  537. return;
  538. value = to;
  539. if(horizontal)
  540. {
  541. if(positions)
  542. {
  543. double part = static_cast<double>(to) / positions;
  544. part*=(pos.w-48);
  545. int newPos = part + pos.x + 16 - slider->pos.x;
  546. slider->moveBy(Point(newPos, 0));
  547. }
  548. else
  549. slider->moveTo(Point(pos.x+16, pos.y));
  550. }
  551. else
  552. {
  553. if(positions)
  554. {
  555. double part = static_cast<double>(to) / positions;
  556. part*=(pos.h-48);
  557. int newPos = part + pos.y + 16 - slider->pos.y;
  558. slider->moveBy(Point(0, newPos));
  559. }
  560. else
  561. slider->moveTo(Point(pos.x, pos.y+16));
  562. }
  563. if(moved)
  564. moved(to);
  565. }
  566. void CSlider::clickLeft(tribool down, bool previousState)
  567. {
  568. if(down && !slider->isBlocked())
  569. {
  570. double pw = 0;
  571. double rw = 0;
  572. if(horizontal)
  573. {
  574. pw = GH.current->motion.x-pos.x-25;
  575. rw = pw / static_cast<double>(pos.w - 48);
  576. }
  577. else
  578. {
  579. pw = GH.current->motion.y-pos.y-24;
  580. rw = pw / (pos.h-48);
  581. }
  582. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  583. return;
  584. // if (rw>1) return;
  585. // if (rw<0) return;
  586. slider->clickLeft(true, slider->pressedL);
  587. moveTo(rw * positions + 0.5);
  588. return;
  589. }
  590. if(active & MOVE)
  591. {
  592. deactivateMouseMove();
  593. used &= ~MOVE;
  594. }
  595. }
  596. CSlider::~CSlider()
  597. {
  598. }
  599. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style)
  600. :capacity(Capacity),amount(Amount),horizontal(Horizontal), moved(Moved)
  601. {
  602. OBJ_CONSTRUCTION_CAPTURING_ALL;
  603. setAmount(amount);
  604. used = LCLICK;
  605. strongInterest = true;
  606. left = new CAdventureMapButton();
  607. right = new CAdventureMapButton();
  608. slider = new CAdventureMapButton();
  609. pos.x += x;
  610. pos.y += y;
  611. if(horizontal)
  612. {
  613. left->pos.y = slider->pos.y = right->pos.y = pos.y;
  614. left->pos.x = pos.x;
  615. right->pos.x = pos.x + totalw - 16;
  616. }
  617. else
  618. {
  619. left->pos.x = slider->pos.x = right->pos.x = pos.x;
  620. left->pos.y = pos.y;
  621. right->pos.y = pos.y + totalw - 16;
  622. }
  623. left->callback = boost::bind(&CSlider::moveLeft,this);
  624. right->callback = boost::bind(&CSlider::moveRight,this);
  625. slider->callback = boost::bind(&CSlider::sliderClicked,this);
  626. left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
  627. if(horizontal)
  628. {
  629. pos.h = 16;
  630. pos.w = totalw;
  631. }
  632. else
  633. {
  634. pos.w = 16;
  635. pos.h = totalw;
  636. }
  637. if(style == 0)
  638. {
  639. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  640. CAnimation *animLeft = new CAnimation(name);
  641. left->setImage(animLeft);
  642. left->setOffset(0);
  643. CAnimation *animRight = new CAnimation(name);
  644. right->setImage(animRight);
  645. right->setOffset(2);
  646. CAnimation *animSlider = new CAnimation(name);
  647. slider->setImage(animSlider);
  648. slider->setOffset(4);
  649. }
  650. else
  651. {
  652. left->setImage(new CAnimation(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF"));
  653. right->setImage(new CAnimation(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF"));
  654. slider->setImage(new CAnimation("SCNRBSL.DEF"));
  655. }
  656. slider->actOnDown = true;
  657. slider->soundDisabled = true;
  658. left->soundDisabled = true;
  659. right->soundDisabled = true;
  660. value = -1;
  661. moveTo(Value);
  662. }
  663. void CSlider::block( bool on )
  664. {
  665. left->block(on);
  666. right->block(on);
  667. slider->block(on);
  668. }
  669. void CSlider::setAmount( int to )
  670. {
  671. amount = to;
  672. positions = to - capacity;
  673. vstd::amax(positions, 0);
  674. }
  675. void CSlider::showAll(SDL_Surface * to)
  676. {
  677. CSDL_Ext::fillRect(to, &pos, 0);
  678. CIntObject::showAll(to);
  679. }
  680. void CSlider::wheelScrolled(bool down, bool in)
  681. {
  682. moveTo(value + 3 * (down ? +1 : -1));
  683. }
  684. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  685. {
  686. if(key.state != SDL_PRESSED) return;
  687. int moveDest = 0;
  688. switch(key.keysym.sym)
  689. {
  690. case SDLK_UP:
  691. moveDest = value - 1;
  692. break;
  693. case SDLK_DOWN:
  694. moveDest = value + 1;
  695. break;
  696. case SDLK_PAGEUP:
  697. moveDest = value - capacity + 1;
  698. break;
  699. case SDLK_PAGEDOWN:
  700. moveDest = value + capacity - 1;
  701. break;
  702. case SDLK_HOME:
  703. moveDest = 0;
  704. break;
  705. case SDLK_END:
  706. moveDest = amount - capacity;
  707. break;
  708. default:
  709. return;
  710. }
  711. moveTo(moveDest);
  712. }
  713. void CSlider::moveToMax()
  714. {
  715. moveTo(amount);
  716. }
  717. static void intDeleter(CIntObject* object)
  718. {
  719. delete object;
  720. }
  721. CObjectList::CObjectList(CreateFunc create, DestroyFunc destroy):
  722. createObject(create),
  723. destroyObject(destroy)
  724. {
  725. if (!destroyObject)
  726. destroyObject = intDeleter;
  727. }
  728. void CObjectList::deleteItem(CIntObject* item)
  729. {
  730. if (!item)
  731. return;
  732. if (active)
  733. item->deactivate();
  734. removeChild(item);
  735. destroyObject(item);
  736. }
  737. CIntObject* CObjectList::createItem(size_t index)
  738. {
  739. OBJ_CONSTRUCTION_CAPTURING_ALL;
  740. CIntObject * item = createObject(index);
  741. if (item == NULL)
  742. item = new CIntObject();
  743. item->recActions = defActions;
  744. //May happen if object was created before call to getObject()
  745. if(item->parent != this)
  746. {
  747. if (item->parent)
  748. CGuiHandler::moveChild(item, item->parent, this);
  749. else
  750. addChild(item);
  751. }
  752. if (item && active)
  753. item->activate();
  754. return item;
  755. }
  756. CTabbedInt::CTabbedInt(CreateFunc create, DestroyFunc destroy, Point position, size_t ActiveID):
  757. CObjectList(create, destroy),
  758. activeTab(NULL),
  759. activeID(ActiveID)
  760. {
  761. pos += position;
  762. reset();
  763. }
  764. void CTabbedInt::setActive(size_t which)
  765. {
  766. if (which != activeID)
  767. {
  768. activeID = which;
  769. reset();
  770. }
  771. }
  772. void CTabbedInt::reset()
  773. {
  774. deleteItem(activeTab);
  775. activeTab = createItem(activeID);
  776. activeTab->moveTo(pos.topLeft());
  777. if (active)
  778. redraw();
  779. }
  780. CIntObject * CTabbedInt::getItem()
  781. {
  782. return activeTab;
  783. }
  784. CListBox::CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  785. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos):
  786. CObjectList(create, destroy),
  787. first(InitialPos),
  788. totalSize(TotalSize),
  789. itemOffset(ItemOffset)
  790. {
  791. pos += Pos;
  792. items.resize(VisibleSize, NULL);
  793. if (Slider & 1)
  794. {
  795. OBJ_CONSTRUCTION_CAPTURING_ALL;
  796. slider = new CSlider(SliderPos.x, SliderPos.y, SliderPos.w, boost::bind(&CListBox::moveToPos, this, _1),
  797. VisibleSize, TotalSize, InitialPos, Slider & 2, Slider & 4);
  798. }
  799. reset();
  800. }
  801. // Used to move active items after changing list position
  802. void CListBox::updatePositions()
  803. {
  804. Point itemPos = pos.topLeft();
  805. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  806. {
  807. (*it)->moveTo(itemPos);
  808. itemPos += itemOffset;
  809. }
  810. if (active)
  811. {
  812. redraw();
  813. if (slider)
  814. slider->moveTo(first);
  815. }
  816. }
  817. void CListBox::reset()
  818. {
  819. size_t current = first;
  820. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  821. {
  822. deleteItem(*it);
  823. *it = createItem(current++);
  824. }
  825. updatePositions();
  826. }
  827. void CListBox::scrollTo(size_t which)
  828. {
  829. //scroll up
  830. if (first > which)
  831. moveToPos(which);
  832. //scroll down
  833. else if (first + items.size() <= which)
  834. moveToPos(which - items.size());
  835. }
  836. void CListBox::moveToPos(size_t which)
  837. {
  838. //Calculate new position
  839. size_t maxPossible;
  840. if (totalSize > items.size())
  841. maxPossible = totalSize - items.size();
  842. else
  843. maxPossible = 0;
  844. size_t newPos = std::min(which, maxPossible);
  845. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  846. if (first - newPos == 1)
  847. moveToPrev();
  848. else if (newPos - first == 1)
  849. moveToNext();
  850. else if (newPos != first)
  851. {
  852. first = newPos;
  853. reset();
  854. }
  855. }
  856. void CListBox::moveToNext()
  857. {
  858. //Remove front item and insert new one to end
  859. if (first + items.size() < totalSize)
  860. {
  861. first++;
  862. deleteItem(items.front());
  863. items.pop_front();
  864. items.push_back(createItem(first+items.size()));
  865. updatePositions();
  866. }
  867. }
  868. void CListBox::moveToPrev()
  869. {
  870. //Remove last item and insert new one at start
  871. if (first)
  872. {
  873. first--;
  874. deleteItem(items.back());
  875. items.pop_back();
  876. items.push_front(createItem(first));
  877. updatePositions();
  878. }
  879. }
  880. std::list<CIntObject*> CListBox::getItems()
  881. {
  882. return items;
  883. }
  884. void CSimpleWindow::show(SDL_Surface * to)
  885. {
  886. if(bitmap)
  887. blitAt(bitmap,pos.x,pos.y,to);
  888. }
  889. CSimpleWindow::~CSimpleWindow()
  890. {
  891. if (bitmap)
  892. {
  893. SDL_FreeSurface(bitmap);
  894. bitmap=NULL;
  895. }
  896. }
  897. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  898. {
  899. bg=BitmapHandler::loadBitmap(name);
  900. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  901. pos.x += x;
  902. pos.y += y;
  903. if(maxw >= 0)
  904. pos.w = std::min(bg->w,maxw);
  905. else
  906. pos.w=bg->w;
  907. pos.h=bg->h;
  908. middlex=(pos.w/2)+pos.x;
  909. middley=(bg->h/2)+pos.y;
  910. }
  911. CStatusBar::~CStatusBar()
  912. {
  913. SDL_FreeSurface(bg);
  914. }
  915. void CStatusBar::clear()
  916. {
  917. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  918. {
  919. current="";
  920. redraw();
  921. }
  922. }
  923. void CStatusBar::print(const std::string & text)
  924. {
  925. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  926. {
  927. current=text;
  928. redraw();
  929. }
  930. }
  931. void CStatusBar::show(SDL_Surface * to)
  932. {
  933. SDL_Rect srcRect = genRect(pos.h,pos.w,0,0);
  934. SDL_Rect dstRect = genRect(pos.h,pos.w,pos.x,pos.y);
  935. CSDL_Ext::blitSurface(bg,&srcRect,to,&dstRect);
  936. CSDL_Ext::printAtMiddle(current,middlex,middley,FONT_SMALL,Colors::Cornsilk,to);
  937. }
  938. std::string CStatusBar::getCurrent()
  939. {
  940. return current;
  941. }
  942. void CList::activate()
  943. {
  944. activateLClick();
  945. activateRClick();
  946. activateHover();
  947. activateKeys();
  948. activateMouseMove();
  949. };
  950. void CList::deactivate()
  951. {
  952. deactivateLClick();
  953. deactivateRClick();
  954. deactivateHover();
  955. deactivateKeys();
  956. deactivateMouseMove();
  957. };
  958. void CList::clickLeft(tribool down, bool previousState)
  959. {
  960. };
  961. CList::CList(int Size)
  962. :SIZE(Size)
  963. {
  964. }
  965. void CList::fixPos()
  966. {
  967. if(selected < 0) //no selection, do nothing
  968. return;
  969. if(selected < from) //scroll up
  970. from = selected;
  971. else if(from + SIZE <= selected)
  972. from = selected - SIZE + 1;
  973. vstd::amin(from, size() - SIZE);
  974. vstd::amax(from, 0);
  975. draw(screen);
  976. }
  977. void CHoverableArea::hover (bool on)
  978. {
  979. if (on)
  980. GH.statusbar->print(hoverText);
  981. else if (GH.statusbar->getCurrent()==hoverText)
  982. GH.statusbar->clear();
  983. }
  984. CHoverableArea::CHoverableArea()
  985. {
  986. used |= HOVER;
  987. }
  988. CHoverableArea::~CHoverableArea()
  989. {
  990. }
  991. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  992. {
  993. if(!down && previousState)
  994. {
  995. LOCPLINT->showInfoDialog(text);
  996. }
  997. }
  998. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  999. {
  1000. adventureInt->handleRightClick(text, down);
  1001. }
  1002. LRClickableAreaWText::LRClickableAreaWText()
  1003. {
  1004. init();
  1005. }
  1006. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  1007. {
  1008. init();
  1009. pos = Pos + pos;
  1010. hoverText = HoverText;
  1011. text = ClickText;
  1012. }
  1013. LRClickableAreaWText::~LRClickableAreaWText()
  1014. {
  1015. }
  1016. void LRClickableAreaWText::init()
  1017. {
  1018. used = LCLICK | RCLICK | HOVER;
  1019. }
  1020. void CLabel::showAll(SDL_Surface * to)
  1021. {
  1022. CIntObject::showAll(to);
  1023. std::string *hlpText = NULL; //if NULL, text field will be used
  1024. if(ignoreLeadingWhitespace)
  1025. {
  1026. hlpText = new std::string(text);
  1027. boost::trim_left(*hlpText);
  1028. }
  1029. std::string &toPrint = hlpText ? *hlpText : text;
  1030. if(!toPrint.length())
  1031. return;
  1032. static void (*printer[3])(const std::string &, int, int, EFonts, SDL_Color, SDL_Surface *) = {&CSDL_Ext::printAt, &CSDL_Ext::printAtMiddle, &CSDL_Ext::printTo}; //array of printing functions
  1033. printer[alignment](toPrint, pos.x + textOffset.x, pos.y + textOffset.y, font, color, to);
  1034. }
  1035. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
  1036. :alignment(Align), font(Font), color(Color), text(Text)
  1037. {
  1038. autoRedraw = true;
  1039. pos.x += x;
  1040. pos.y += y;
  1041. pos.w = pos.h = 0;
  1042. bg = NULL;
  1043. ignoreLeadingWhitespace = false;
  1044. pos.w = graphics->fonts[font]->getWidth(text.c_str());
  1045. pos.h = graphics->fonts[font]->height;
  1046. }
  1047. void CLabel::setTxt(const std::string &Txt)
  1048. {
  1049. text = Txt;
  1050. if(autoRedraw)
  1051. {
  1052. if(bg || !parent)
  1053. redraw();
  1054. else
  1055. parent->redraw();
  1056. }
  1057. }
  1058. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  1059. font(Font), align(Align), color(Color)
  1060. {};
  1061. void CLabelGroup::add(int x, int y, const std::string &text)
  1062. {
  1063. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1064. new CLabel(x, y, font, align, color, text);
  1065. };
  1066. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::Cornsilk*/)
  1067. :CLabel(rect.x, rect.y, Font, Align, Color, Text), sliderStyle(SliderStyle), slider(NULL)
  1068. {
  1069. type |= REDRAW_PARENT;
  1070. autoRedraw = false;
  1071. pos.h = rect.h;
  1072. pos.w = rect.w;
  1073. assert(Align == TOPLEFT || Align == CENTER); //TODO: support for other alignments
  1074. assert(pos.w >= 80); //we need some space
  1075. setTxt(Text);
  1076. }
  1077. void CTextBox::showAll(SDL_Surface * to)
  1078. {
  1079. CIntObject::showAll(to);
  1080. const Font &f = *graphics->fonts[font];
  1081. int dy = f.height; //line height
  1082. int base_y = pos.y;
  1083. if(alignment == CENTER)
  1084. base_y += std::max((pos.h - maxH)/2,0);
  1085. int howManyLinesToPrint = slider ? slider->capacity : lines.size();
  1086. int firstLineToPrint = slider ? slider->value : 0;
  1087. for (int i = 0; i < howManyLinesToPrint; i++)
  1088. {
  1089. const std::string &line = lines[i + firstLineToPrint];
  1090. if(!line.size()) continue;
  1091. int x = pos.x;
  1092. if(alignment == CENTER)
  1093. {
  1094. x += (pos.w - f.getWidth(line.c_str())) / 2;
  1095. if(slider)
  1096. x -= slider->pos.w / 2 + 5;
  1097. }
  1098. if(line[0] == '{' && line[line.size()-1] == '}')
  1099. CSDL_Ext::printAt(line, x, base_y + i*dy, font, Colors::Jasmine, to);
  1100. else
  1101. CSDL_Ext::printAt(line, x, base_y + i*dy, font, color, to);
  1102. }
  1103. }
  1104. void CTextBox::setTxt(const std::string &Txt)
  1105. {
  1106. recalculateLines(Txt);
  1107. CLabel::setTxt(Txt);
  1108. }
  1109. void CTextBox::sliderMoved(int to)
  1110. {
  1111. if(!slider)
  1112. return;
  1113. redraw();
  1114. }
  1115. void CTextBox::setBounds(int limitW, int limitH)
  1116. {
  1117. pos.h = limitH;
  1118. pos.w = limitW;
  1119. recalculateLines(text);
  1120. }
  1121. void CTextBox::recalculateLines(const std::string &Txt)
  1122. {
  1123. delChildNUll(slider, true);
  1124. lines.clear();
  1125. const Font &f = *graphics->fonts[font];
  1126. int lineHeight = f.height;
  1127. int lineCapacity = pos.h / lineHeight;
  1128. lines = CMessage::breakText(Txt, pos.w, font);
  1129. if(lines.size() > lineCapacity) //we need to add a slider
  1130. {
  1131. lines = CMessage::breakText(Txt, pos.w - 32 - 10, font);
  1132. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1133. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1), lineCapacity, lines.size(), 0, false, sliderStyle);
  1134. if(active)
  1135. slider->activate();
  1136. }
  1137. maxH = lineHeight * lines.size();
  1138. maxW = 0;
  1139. BOOST_FOREACH(const std::string &line, lines)
  1140. vstd::amax(maxW, f.getWidth(line.c_str()));
  1141. }
  1142. void CGStatusBar::print(const std::string & Text)
  1143. {
  1144. setTxt(Text);
  1145. }
  1146. void CGStatusBar::clear()
  1147. {
  1148. setTxt("");
  1149. }
  1150. std::string CGStatusBar::getCurrent()
  1151. {
  1152. return text;
  1153. }
  1154. CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
  1155. : CLabel(x, y, Font, Align, Color, Text)
  1156. {
  1157. init();
  1158. }
  1159. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::Cornsilk*/)
  1160. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  1161. {
  1162. init();
  1163. bg = BG;
  1164. CGuiHandler::moveChild(bg, bg->parent, this);
  1165. pos = bg->pos;
  1166. calcOffset();
  1167. }
  1168. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  1169. : CLabel(x, y, FONT_SMALL, CENTER)
  1170. {
  1171. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1172. init();
  1173. bg = new CPicture(name);
  1174. pos = bg->pos;
  1175. if(maxw < pos.w)
  1176. {
  1177. vstd::amin(pos.w, maxw);
  1178. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  1179. }
  1180. calcOffset();
  1181. }
  1182. CGStatusBar::~CGStatusBar()
  1183. {
  1184. GH.statusbar = oldStatusBar;
  1185. }
  1186. void CGStatusBar::show(SDL_Surface * to)
  1187. {
  1188. }
  1189. void CGStatusBar::init()
  1190. {
  1191. oldStatusBar = GH.statusbar;
  1192. GH.statusbar = this;
  1193. }
  1194. void CGStatusBar::calcOffset()
  1195. {
  1196. switch(alignment)
  1197. {
  1198. case CENTER:
  1199. textOffset = Point(pos.w/2, pos.h/2);
  1200. break;
  1201. case BOTTOMRIGHT:
  1202. textOffset = Point(pos.w, pos.h);
  1203. break;
  1204. }
  1205. }
  1206. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1207. :cb(CB)
  1208. {
  1209. focus = false;
  1210. pos += Pos;
  1211. captureAllKeys = true;
  1212. OBJ_CONSTRUCTION;
  1213. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1214. used = LCLICK | KEYBOARD;
  1215. giveFocus();
  1216. }
  1217. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  1218. {
  1219. focus = false;
  1220. pos += Pos;
  1221. captureAllKeys = true;
  1222. OBJ_CONSTRUCTION;
  1223. bg = new CPicture(Pos, 0, true);
  1224. Rect hlp = Pos;
  1225. if(srf)
  1226. CSDL_Ext::blitSurface(srf, &hlp, *bg, NULL);
  1227. else
  1228. SDL_FillRect(*bg, NULL, 0);
  1229. pos.w = bg->pos.w;
  1230. pos.h = bg->pos.h;
  1231. bg->pos = pos;
  1232. used = LCLICK | KEYBOARD;
  1233. giveFocus();
  1234. }
  1235. void CTextInput::showAll(SDL_Surface * to)
  1236. {
  1237. CIntObject::showAll(to);
  1238. const std::string toPrint = focus ? text + "_" : text;
  1239. CSDL_Ext::printAt(toPrint, pos.x, pos.y, FONT_SMALL, Colors::Cornsilk, to);
  1240. }
  1241. void CTextInput::clickLeft( tribool down, bool previousState )
  1242. {
  1243. if(down && !focus)
  1244. giveFocus();
  1245. }
  1246. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1247. {
  1248. if(!focus || key.state != SDL_PRESSED)
  1249. return;
  1250. if(key.keysym.sym == SDLK_TAB)
  1251. {
  1252. moveFocus();
  1253. GH.breakEventHandling();
  1254. return;
  1255. }
  1256. switch(key.keysym.sym)
  1257. {
  1258. case SDLK_BACKSPACE:
  1259. if(text.size())
  1260. text.resize(text.size()-1);
  1261. break;
  1262. default:
  1263. char c = key.keysym.unicode; //TODO 16-/>8
  1264. static const std::string forbiddenChars = "<>:\"/\\|?*"; //if we are entering a filename, some special characters won't be allowed
  1265. if(!vstd::contains(forbiddenChars,c) && std::isprint(c))
  1266. text += c;
  1267. break;
  1268. }
  1269. redraw();
  1270. cb(text);
  1271. }
  1272. void CTextInput::setText( const std::string &nText, bool callCb )
  1273. {
  1274. text = nText;
  1275. redraw();
  1276. if(callCb)
  1277. cb(text);
  1278. }
  1279. CTextInput::~CTextInput()
  1280. {
  1281. }
  1282. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  1283. {
  1284. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER)
  1285. return false;
  1286. return true;
  1287. }
  1288. CFocusable::CFocusable()
  1289. {
  1290. focusables.push_back(this);
  1291. }
  1292. CFocusable::~CFocusable()
  1293. {
  1294. if(inputWithFocus == this)
  1295. inputWithFocus = NULL;
  1296. focusables -= this;
  1297. }
  1298. void CFocusable::giveFocus()
  1299. {
  1300. if(inputWithFocus)
  1301. {
  1302. inputWithFocus->focus = false;
  1303. inputWithFocus->redraw();
  1304. }
  1305. focus = true;
  1306. inputWithFocus = this;
  1307. redraw();
  1308. }
  1309. void CFocusable::moveFocus()
  1310. {
  1311. std::list<CFocusable*>::iterator i = vstd::find(focusables, this),
  1312. ourIt = i;
  1313. for(i++; i != ourIt; i++)
  1314. {
  1315. if(i == focusables.end())
  1316. i = focusables.begin();
  1317. if((*i)->active)
  1318. {
  1319. (*i)->giveFocus();
  1320. break;;
  1321. }
  1322. }
  1323. }