CIntObjectClasses.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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 && down == false && getState() == PRESSED)
  373. {
  374. //if(!onlyOn || !isHighlighted())
  375. select(!selected);
  376. }
  377. }
  378. 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)
  379. : onlyOn(false), selected(false), callback2(onDeselect)
  380. {
  381. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  382. }
  383. 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 */ )
  384. : onlyOn(false), selected(false) // TODO: callback2(???)
  385. {
  386. ID = myid;
  387. std::map<int,std::string> pom;
  388. pom[0] = help.first;
  389. init(onSelect, pom, help.second, playerColoredButton, defName, add, x, y, key);
  390. }
  391. 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 */ )
  392. : onlyOn(false), selected(false) // TODO: callback2(???)
  393. {
  394. ID = myid;
  395. std::map<int,std::string> pom;
  396. pom[0] = Name;
  397. init(onSelect, pom,HelpBox, playerColoredButton, defName, add, x, y, key);
  398. }
  399. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  400. {
  401. if (bt->parent)
  402. bt->parent->removeChild(bt);
  403. addChild(bt);
  404. bt->recActions = defActions;//FIXME: not needed?
  405. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  406. bt->onlyOn = true;
  407. buttons.push_back(bt);
  408. }
  409. 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)
  410. {
  411. OBJ_CONSTRUCTION_CAPTURING_ALL;
  412. CHighlightableButton *bt = new CHighlightableButton(OnSelect, 0, tooltip, HelpBox, false, defName, 0, x, y, key);
  413. if(musicLike)
  414. {
  415. if (buttons.size() > 3)
  416. bt->setOffset(buttons.size()-3);
  417. }
  418. bt->ID = uid;
  419. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  420. bt->onlyOn = true;
  421. buttons.push_back(bt);
  422. }
  423. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
  424. : onChange(OnChange), musicLike(musicLikeButtons)
  425. {}
  426. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  427. {
  428. }
  429. void CHighlightableButtonsGroup::select(int id, bool mode)
  430. {
  431. CHighlightableButton *bt = NULL;
  432. if(mode)
  433. {
  434. for(size_t i=0;i<buttons.size() && !bt; ++i)
  435. if (buttons[i]->ID == id)
  436. bt = buttons[i];
  437. }
  438. else
  439. {
  440. bt = buttons[id];
  441. }
  442. bt->select(true);
  443. selectionChanged(bt->ID);
  444. }
  445. void CHighlightableButtonsGroup::selectionChanged(int to)
  446. {
  447. for(size_t i=0;i<buttons.size(); ++i)
  448. if(buttons[i]->ID!=to && buttons[i]->isHighlighted())
  449. buttons[i]->select(false);
  450. onChange(to);
  451. if (parent)
  452. parent->redraw();
  453. }
  454. void CHighlightableButtonsGroup::show(SDL_Surface * to)
  455. {
  456. if (musicLike)
  457. {
  458. for(size_t i=0;i<buttons.size(); ++i)
  459. if(buttons[i]->isHighlighted())
  460. buttons[i]->show(to);
  461. }
  462. else
  463. CIntObject::show(to);
  464. }
  465. void CHighlightableButtonsGroup::showAll(SDL_Surface * to)
  466. {
  467. if (musicLike)
  468. {
  469. for(size_t i=0;i<buttons.size(); ++i)
  470. if(buttons[i]->isHighlighted())
  471. buttons[i]->showAll(to);
  472. }
  473. else
  474. CIntObject::showAll(to);
  475. }
  476. void CHighlightableButtonsGroup::block( ui8 on )
  477. {
  478. for(size_t i=0;i<buttons.size(); ++i)
  479. {
  480. buttons[i]->block(on);
  481. }
  482. }
  483. void CSlider::sliderClicked()
  484. {
  485. if(!(active & MOVE))
  486. {
  487. activateMouseMove();
  488. used |= MOVE;
  489. }
  490. }
  491. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  492. {
  493. double v = 0;
  494. if(horizontal)
  495. {
  496. 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 )
  497. return;
  498. v = sEvent.x - pos.x - 24;
  499. v *= positions;
  500. v /= (pos.w - 48);
  501. }
  502. else
  503. {
  504. 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 )
  505. return;
  506. v = sEvent.y - pos.y - 24;
  507. v *= positions;
  508. v /= (pos.h - 48);
  509. }
  510. v += 0.5;
  511. if(v!=value)
  512. {
  513. moveTo(v);
  514. redrawSlider();
  515. }
  516. }
  517. void CSlider::redrawSlider()
  518. {
  519. //slider->show(screenBuf);
  520. }
  521. void CSlider::moveLeft()
  522. {
  523. moveTo(value-1);
  524. }
  525. void CSlider::moveRight()
  526. {
  527. moveTo(value+1);
  528. }
  529. void CSlider::moveTo(int to)
  530. {
  531. vstd::amax(to, 0);
  532. vstd::amin(to, positions);
  533. //same, old position?
  534. if(value == to)
  535. return;
  536. value = to;
  537. if(horizontal)
  538. {
  539. if(positions)
  540. {
  541. double part = static_cast<double>(to) / positions;
  542. part*=(pos.w-48);
  543. int newPos = part + pos.x + 16 - slider->pos.x;
  544. slider->moveBy(Point(newPos, 0));
  545. }
  546. else
  547. slider->moveTo(Point(pos.x+16, pos.y));
  548. }
  549. else
  550. {
  551. if(positions)
  552. {
  553. double part = static_cast<double>(to) / positions;
  554. part*=(pos.h-48);
  555. int newPos = part + pos.y + 16 - slider->pos.y;
  556. slider->moveBy(Point(0, newPos));
  557. }
  558. else
  559. slider->moveTo(Point(pos.x, pos.y+16));
  560. }
  561. if(moved)
  562. moved(to);
  563. }
  564. void CSlider::clickLeft(tribool down, bool previousState)
  565. {
  566. if(down && !slider->isBlocked())
  567. {
  568. double pw = 0;
  569. double rw = 0;
  570. if(horizontal)
  571. {
  572. pw = GH.current->motion.x-pos.x-25;
  573. rw = pw / static_cast<double>(pos.w - 48);
  574. }
  575. else
  576. {
  577. pw = GH.current->motion.y-pos.y-24;
  578. rw = pw / (pos.h-48);
  579. }
  580. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  581. return;
  582. // if (rw>1) return;
  583. // if (rw<0) return;
  584. slider->clickLeft(true, slider->pressedL);
  585. moveTo(rw * positions + 0.5);
  586. return;
  587. }
  588. if(active & MOVE)
  589. {
  590. deactivateMouseMove();
  591. used &= ~MOVE;
  592. }
  593. }
  594. CSlider::~CSlider()
  595. {
  596. }
  597. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style)
  598. :capacity(Capacity),amount(Amount),horizontal(Horizontal), moved(Moved)
  599. {
  600. OBJ_CONSTRUCTION_CAPTURING_ALL;
  601. setAmount(amount);
  602. used = LCLICK;
  603. strongInterest = true;
  604. left = new CAdventureMapButton();
  605. right = new CAdventureMapButton();
  606. slider = new CAdventureMapButton();
  607. pos.x += x;
  608. pos.y += y;
  609. if(horizontal)
  610. {
  611. left->pos.y = slider->pos.y = right->pos.y = pos.y;
  612. left->pos.x = pos.x;
  613. right->pos.x = pos.x + totalw - 16;
  614. }
  615. else
  616. {
  617. left->pos.x = slider->pos.x = right->pos.x = pos.x;
  618. left->pos.y = pos.y;
  619. right->pos.y = pos.y + totalw - 16;
  620. }
  621. left->callback = boost::bind(&CSlider::moveLeft,this);
  622. right->callback = boost::bind(&CSlider::moveRight,this);
  623. slider->callback = boost::bind(&CSlider::sliderClicked,this);
  624. left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
  625. if(horizontal)
  626. {
  627. pos.h = 16;
  628. pos.w = totalw;
  629. }
  630. else
  631. {
  632. pos.w = 16;
  633. pos.h = totalw;
  634. }
  635. if(style == 0)
  636. {
  637. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  638. CAnimation *animLeft = new CAnimation(name);
  639. left->setImage(animLeft);
  640. left->setOffset(0);
  641. CAnimation *animRight = new CAnimation(name);
  642. right->setImage(animRight);
  643. right->setOffset(2);
  644. CAnimation *animSlider = new CAnimation(name);
  645. slider->setImage(animSlider);
  646. slider->setOffset(4);
  647. }
  648. else
  649. {
  650. left->setImage(new CAnimation(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF"));
  651. right->setImage(new CAnimation(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF"));
  652. slider->setImage(new CAnimation("SCNRBSL.DEF"));
  653. }
  654. slider->actOnDown = true;
  655. slider->soundDisabled = true;
  656. left->soundDisabled = true;
  657. right->soundDisabled = true;
  658. value = -1;
  659. moveTo(Value);
  660. }
  661. void CSlider::block( bool on )
  662. {
  663. left->block(on);
  664. right->block(on);
  665. slider->block(on);
  666. }
  667. void CSlider::setAmount( int to )
  668. {
  669. amount = to;
  670. positions = to - capacity;
  671. vstd::amax(positions, 0);
  672. }
  673. void CSlider::showAll(SDL_Surface * to)
  674. {
  675. CSDL_Ext::fillRect(to, &pos, 0);
  676. CIntObject::showAll(to);
  677. }
  678. void CSlider::wheelScrolled(bool down, bool in)
  679. {
  680. moveTo(value + 3 * (down ? +1 : -1));
  681. }
  682. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  683. {
  684. if(key.state != SDL_PRESSED) return;
  685. int moveDest = 0;
  686. switch(key.keysym.sym)
  687. {
  688. case SDLK_UP:
  689. moveDest = value - 1;
  690. break;
  691. case SDLK_DOWN:
  692. moveDest = value + 1;
  693. break;
  694. case SDLK_PAGEUP:
  695. moveDest = value - capacity + 1;
  696. break;
  697. case SDLK_PAGEDOWN:
  698. moveDest = value + capacity - 1;
  699. break;
  700. case SDLK_HOME:
  701. moveDest = 0;
  702. break;
  703. case SDLK_END:
  704. moveDest = amount - capacity;
  705. break;
  706. default:
  707. return;
  708. }
  709. moveTo(moveDest);
  710. }
  711. void CSlider::moveToMax()
  712. {
  713. moveTo(amount);
  714. }
  715. static void intDeleter(CIntObject* object)
  716. {
  717. delete object;
  718. }
  719. CObjectList::CObjectList(CreateFunc create, DestroyFunc destroy):
  720. createObject(create),
  721. destroyObject(destroy)
  722. {
  723. if (!destroyObject)
  724. destroyObject = intDeleter;
  725. }
  726. void CObjectList::deleteItem(CIntObject* item)
  727. {
  728. if (!item)
  729. return;
  730. if (active)
  731. item->deactivate();
  732. removeChild(item);
  733. destroyObject(item);
  734. }
  735. CIntObject* CObjectList::createItem(size_t index)
  736. {
  737. OBJ_CONSTRUCTION_CAPTURING_ALL;
  738. CIntObject * item = createObject(index);
  739. if (item == NULL)
  740. item = new CIntObject();
  741. item->recActions = defActions;
  742. //May happen if object was created before call to getObject()
  743. if(item->parent != this)
  744. {
  745. if (item->parent)
  746. CGuiHandler::moveChild(item, item->parent, this);
  747. else
  748. addChild(item);
  749. }
  750. if (item && active)
  751. item->activate();
  752. return item;
  753. }
  754. CTabbedInt::CTabbedInt(CreateFunc create, DestroyFunc destroy, Point position, size_t ActiveID):
  755. CObjectList(create, destroy),
  756. activeTab(NULL),
  757. activeID(ActiveID)
  758. {
  759. pos += position;
  760. reset();
  761. }
  762. void CTabbedInt::setActive(size_t which)
  763. {
  764. if (which != activeID)
  765. {
  766. activeID = which;
  767. reset();
  768. }
  769. }
  770. void CTabbedInt::reset()
  771. {
  772. deleteItem(activeTab);
  773. activeTab = createItem(activeID);
  774. activeTab->moveTo(pos.topLeft());
  775. if (active)
  776. redraw();
  777. }
  778. CIntObject * CTabbedInt::getItem()
  779. {
  780. return activeTab;
  781. }
  782. CListBox::CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  783. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos):
  784. CObjectList(create, destroy),
  785. first(InitialPos),
  786. totalSize(TotalSize),
  787. itemOffset(ItemOffset)
  788. {
  789. pos += Pos;
  790. items.resize(VisibleSize, NULL);
  791. if (Slider & 1)
  792. {
  793. OBJ_CONSTRUCTION_CAPTURING_ALL;
  794. slider = new CSlider(SliderPos.x, SliderPos.y, SliderPos.w, boost::bind(&CListBox::moveToPos, this, _1),
  795. VisibleSize, TotalSize, InitialPos, Slider & 2, Slider & 4);
  796. }
  797. reset();
  798. }
  799. // Used to move active items after changing list position
  800. void CListBox::updatePositions()
  801. {
  802. Point itemPos = pos.topLeft();
  803. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  804. {
  805. (*it)->moveTo(itemPos);
  806. itemPos += itemOffset;
  807. }
  808. if (active)
  809. {
  810. redraw();
  811. if (slider)
  812. slider->moveTo(first);
  813. }
  814. }
  815. void CListBox::reset()
  816. {
  817. size_t current = first;
  818. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  819. {
  820. deleteItem(*it);
  821. *it = createItem(current++);
  822. }
  823. updatePositions();
  824. }
  825. void CListBox::scrollTo(size_t which)
  826. {
  827. //scroll up
  828. if (first > which)
  829. moveToPos(which);
  830. //scroll down
  831. else if (first + items.size() <= which)
  832. moveToPos(which - items.size());
  833. }
  834. void CListBox::moveToPos(size_t which)
  835. {
  836. //Calculate new position
  837. size_t maxPossible;
  838. if (totalSize > items.size())
  839. maxPossible = totalSize - items.size();
  840. else
  841. maxPossible = 0;
  842. size_t newPos = std::min(which, maxPossible);
  843. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  844. if (first - newPos == 1)
  845. moveToPrev();
  846. else if (newPos - first == 1)
  847. moveToNext();
  848. else if (newPos != first)
  849. {
  850. first = newPos;
  851. reset();
  852. }
  853. }
  854. void CListBox::moveToNext()
  855. {
  856. //Remove front item and insert new one to end
  857. if (first + items.size() < totalSize)
  858. {
  859. first++;
  860. deleteItem(items.front());
  861. items.pop_front();
  862. items.push_back(createItem(first+items.size()));
  863. updatePositions();
  864. }
  865. }
  866. void CListBox::moveToPrev()
  867. {
  868. //Remove last item and insert new one at start
  869. if (first)
  870. {
  871. first--;
  872. deleteItem(items.back());
  873. items.pop_back();
  874. items.push_front(createItem(first));
  875. updatePositions();
  876. }
  877. }
  878. std::list<CIntObject*> CListBox::getItems()
  879. {
  880. return items;
  881. }
  882. void CSimpleWindow::show(SDL_Surface * to)
  883. {
  884. if(bitmap)
  885. blitAt(bitmap,pos.x,pos.y,to);
  886. }
  887. CSimpleWindow::~CSimpleWindow()
  888. {
  889. if (bitmap)
  890. {
  891. SDL_FreeSurface(bitmap);
  892. bitmap=NULL;
  893. }
  894. }
  895. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  896. {
  897. bg=BitmapHandler::loadBitmap(name);
  898. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  899. pos.x += x;
  900. pos.y += y;
  901. if(maxw >= 0)
  902. pos.w = std::min(bg->w,maxw);
  903. else
  904. pos.w=bg->w;
  905. pos.h=bg->h;
  906. middlex=(pos.w/2)+pos.x;
  907. middley=(bg->h/2)+pos.y;
  908. }
  909. CStatusBar::~CStatusBar()
  910. {
  911. SDL_FreeSurface(bg);
  912. }
  913. void CStatusBar::clear()
  914. {
  915. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  916. {
  917. current="";
  918. redraw();
  919. }
  920. }
  921. void CStatusBar::print(const std::string & text)
  922. {
  923. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  924. {
  925. current=text;
  926. redraw();
  927. }
  928. }
  929. void CStatusBar::show(SDL_Surface * to)
  930. {
  931. SDL_Rect srcRect = genRect(pos.h,pos.w,0,0);
  932. SDL_Rect dstRect = genRect(pos.h,pos.w,pos.x,pos.y);
  933. CSDL_Ext::blitSurface(bg,&srcRect,to,&dstRect);
  934. CSDL_Ext::printAtMiddle(current,middlex,middley,FONT_SMALL,Colors::Cornsilk,to);
  935. }
  936. std::string CStatusBar::getCurrent()
  937. {
  938. return current;
  939. }
  940. void CList::activate()
  941. {
  942. activateLClick();
  943. activateRClick();
  944. activateHover();
  945. activateKeys();
  946. activateMouseMove();
  947. };
  948. void CList::deactivate()
  949. {
  950. deactivateLClick();
  951. deactivateRClick();
  952. deactivateHover();
  953. deactivateKeys();
  954. deactivateMouseMove();
  955. };
  956. void CList::clickLeft(tribool down, bool previousState)
  957. {
  958. };
  959. CList::CList(int Size)
  960. :SIZE(Size)
  961. {
  962. }
  963. void CList::fixPos()
  964. {
  965. if(selected < 0) //no selection, do nothing
  966. return;
  967. if(selected < from) //scroll up
  968. from = selected;
  969. else if(from + SIZE <= selected)
  970. from = selected - SIZE + 1;
  971. vstd::amin(from, size() - SIZE);
  972. vstd::amax(from, 0);
  973. draw(screen);
  974. }
  975. void CHoverableArea::hover (bool on)
  976. {
  977. if (on)
  978. GH.statusbar->print(hoverText);
  979. else if (GH.statusbar->getCurrent()==hoverText)
  980. GH.statusbar->clear();
  981. }
  982. CHoverableArea::CHoverableArea()
  983. {
  984. used |= HOVER;
  985. }
  986. CHoverableArea::~CHoverableArea()
  987. {
  988. }
  989. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  990. {
  991. if(!down && previousState)
  992. {
  993. LOCPLINT->showInfoDialog(text);
  994. }
  995. }
  996. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  997. {
  998. adventureInt->handleRightClick(text, down);
  999. }
  1000. LRClickableAreaWText::LRClickableAreaWText()
  1001. {
  1002. init();
  1003. }
  1004. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  1005. {
  1006. init();
  1007. pos = Pos + pos;
  1008. hoverText = HoverText;
  1009. text = ClickText;
  1010. }
  1011. LRClickableAreaWText::~LRClickableAreaWText()
  1012. {
  1013. }
  1014. void LRClickableAreaWText::init()
  1015. {
  1016. used = LCLICK | RCLICK | HOVER;
  1017. }
  1018. void CLabel::showAll(SDL_Surface * to)
  1019. {
  1020. CIntObject::showAll(to);
  1021. std::string *hlpText = NULL; //if NULL, text field will be used
  1022. if(ignoreLeadingWhitespace)
  1023. {
  1024. hlpText = new std::string(text);
  1025. boost::trim_left(*hlpText);
  1026. }
  1027. std::string &toPrint = hlpText ? *hlpText : text;
  1028. if(!toPrint.length())
  1029. return;
  1030. 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
  1031. printer[alignment](toPrint, pos.x + textOffset.x, pos.y + textOffset.y, font, color, to);
  1032. }
  1033. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
  1034. :alignment(Align), font(Font), color(Color), text(Text)
  1035. {
  1036. autoRedraw = true;
  1037. pos.x += x;
  1038. pos.y += y;
  1039. pos.w = pos.h = 0;
  1040. bg = NULL;
  1041. ignoreLeadingWhitespace = false;
  1042. pos.w = graphics->fonts[font]->getWidth(text.c_str());
  1043. pos.h = graphics->fonts[font]->height;
  1044. }
  1045. void CLabel::setTxt(const std::string &Txt)
  1046. {
  1047. text = Txt;
  1048. if(autoRedraw)
  1049. {
  1050. if(bg || !parent)
  1051. redraw();
  1052. else
  1053. parent->redraw();
  1054. }
  1055. }
  1056. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  1057. font(Font), align(Align), color(Color)
  1058. {};
  1059. void CLabelGroup::add(int x, int y, const std::string &text)
  1060. {
  1061. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1062. new CLabel(x, y, font, align, color, text);
  1063. };
  1064. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::Cornsilk*/)
  1065. :CLabel(rect.x, rect.y, Font, Align, Color, Text), sliderStyle(SliderStyle), slider(NULL)
  1066. {
  1067. type |= REDRAW_PARENT;
  1068. autoRedraw = false;
  1069. pos.h = rect.h;
  1070. pos.w = rect.w;
  1071. assert(Align == TOPLEFT || Align == CENTER); //TODO: support for other alignments
  1072. assert(pos.w >= 80); //we need some space
  1073. setTxt(Text);
  1074. }
  1075. void CTextBox::showAll(SDL_Surface * to)
  1076. {
  1077. CIntObject::showAll(to);
  1078. const Font &f = *graphics->fonts[font];
  1079. int dy = f.height; //line height
  1080. int base_y = pos.y;
  1081. if(alignment == CENTER)
  1082. base_y += std::max((pos.h - maxH)/2,0);
  1083. int howManyLinesToPrint = slider ? slider->capacity : lines.size();
  1084. int firstLineToPrint = slider ? slider->value : 0;
  1085. for (int i = 0; i < howManyLinesToPrint; i++)
  1086. {
  1087. const std::string &line = lines[i + firstLineToPrint];
  1088. if(!line.size()) continue;
  1089. int x = pos.x;
  1090. if(alignment == CENTER)
  1091. {
  1092. x += (pos.w - f.getWidth(line.c_str())) / 2;
  1093. if(slider)
  1094. x -= slider->pos.w / 2 + 5;
  1095. }
  1096. if(line[0] == '{' && line[line.size()-1] == '}')
  1097. CSDL_Ext::printAt(line, x, base_y + i*dy, font, Colors::Jasmine, to);
  1098. else
  1099. CSDL_Ext::printAt(line, x, base_y + i*dy, font, color, to);
  1100. }
  1101. }
  1102. void CTextBox::setTxt(const std::string &Txt)
  1103. {
  1104. recalculateLines(Txt);
  1105. CLabel::setTxt(Txt);
  1106. }
  1107. void CTextBox::sliderMoved(int to)
  1108. {
  1109. if(!slider)
  1110. return;
  1111. redraw();
  1112. }
  1113. void CTextBox::setBounds(int limitW, int limitH)
  1114. {
  1115. pos.h = limitH;
  1116. pos.w = limitW;
  1117. recalculateLines(text);
  1118. }
  1119. void CTextBox::recalculateLines(const std::string &Txt)
  1120. {
  1121. delChildNUll(slider, true);
  1122. lines.clear();
  1123. const Font &f = *graphics->fonts[font];
  1124. int lineHeight = f.height;
  1125. int lineCapacity = pos.h / lineHeight;
  1126. lines = CMessage::breakText(Txt, pos.w, font);
  1127. if(lines.size() > lineCapacity) //we need to add a slider
  1128. {
  1129. lines = CMessage::breakText(Txt, pos.w - 32 - 10, font);
  1130. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1131. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1), lineCapacity, lines.size(), 0, false, sliderStyle);
  1132. if(active)
  1133. slider->activate();
  1134. }
  1135. maxH = lineHeight * lines.size();
  1136. maxW = 0;
  1137. BOOST_FOREACH(const std::string &line, lines)
  1138. vstd::amax(maxW, f.getWidth(line.c_str()));
  1139. }
  1140. void CGStatusBar::print(const std::string & Text)
  1141. {
  1142. setTxt(Text);
  1143. }
  1144. void CGStatusBar::clear()
  1145. {
  1146. setTxt("");
  1147. }
  1148. std::string CGStatusBar::getCurrent()
  1149. {
  1150. return text;
  1151. }
  1152. CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
  1153. : CLabel(x, y, Font, Align, Color, Text)
  1154. {
  1155. init();
  1156. }
  1157. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::Cornsilk*/)
  1158. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  1159. {
  1160. init();
  1161. bg = BG;
  1162. CGuiHandler::moveChild(bg, bg->parent, this);
  1163. pos = bg->pos;
  1164. calcOffset();
  1165. }
  1166. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  1167. : CLabel(x, y, FONT_SMALL, CENTER)
  1168. {
  1169. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1170. init();
  1171. bg = new CPicture(name);
  1172. pos = bg->pos;
  1173. if(maxw < pos.w)
  1174. {
  1175. vstd::amin(pos.w, maxw);
  1176. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  1177. }
  1178. calcOffset();
  1179. }
  1180. CGStatusBar::~CGStatusBar()
  1181. {
  1182. GH.statusbar = oldStatusBar;
  1183. }
  1184. void CGStatusBar::show(SDL_Surface * to)
  1185. {
  1186. }
  1187. void CGStatusBar::init()
  1188. {
  1189. oldStatusBar = GH.statusbar;
  1190. GH.statusbar = this;
  1191. }
  1192. void CGStatusBar::calcOffset()
  1193. {
  1194. switch(alignment)
  1195. {
  1196. case CENTER:
  1197. textOffset = Point(pos.w/2, pos.h/2);
  1198. break;
  1199. case BOTTOMRIGHT:
  1200. textOffset = Point(pos.w, pos.h);
  1201. break;
  1202. }
  1203. }
  1204. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1205. :cb(CB)
  1206. {
  1207. focus = false;
  1208. pos += Pos;
  1209. captureAllKeys = true;
  1210. OBJ_CONSTRUCTION;
  1211. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1212. used = LCLICK | KEYBOARD;
  1213. giveFocus();
  1214. }
  1215. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  1216. {
  1217. focus = false;
  1218. pos += Pos;
  1219. captureAllKeys = true;
  1220. OBJ_CONSTRUCTION;
  1221. bg = new CPicture(Pos, 0, true);
  1222. Rect hlp = Pos;
  1223. if(srf)
  1224. CSDL_Ext::blitSurface(srf, &hlp, *bg, NULL);
  1225. else
  1226. SDL_FillRect(*bg, NULL, 0);
  1227. pos.w = bg->pos.w;
  1228. pos.h = bg->pos.h;
  1229. bg->pos = pos;
  1230. used = LCLICK | KEYBOARD;
  1231. giveFocus();
  1232. }
  1233. void CTextInput::showAll(SDL_Surface * to)
  1234. {
  1235. CIntObject::showAll(to);
  1236. const std::string toPrint = focus ? text + "_" : text;
  1237. CSDL_Ext::printAt(toPrint, pos.x, pos.y, FONT_SMALL, Colors::Cornsilk, to);
  1238. }
  1239. void CTextInput::clickLeft( tribool down, bool previousState )
  1240. {
  1241. if(down && !focus)
  1242. giveFocus();
  1243. }
  1244. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1245. {
  1246. if(!focus || key.state != SDL_PRESSED)
  1247. return;
  1248. if(key.keysym.sym == SDLK_TAB)
  1249. {
  1250. moveFocus();
  1251. GH.breakEventHandling();
  1252. return;
  1253. }
  1254. switch(key.keysym.sym)
  1255. {
  1256. case SDLK_BACKSPACE:
  1257. if(text.size())
  1258. text.resize(text.size()-1);
  1259. break;
  1260. default:
  1261. char c = key.keysym.unicode; //TODO 16-/>8
  1262. static const std::string forbiddenChars = "<>:\"/\\|?*"; //if we are entering a filename, some special characters won't be allowed
  1263. if(!vstd::contains(forbiddenChars,c) && std::isprint(c))
  1264. text += c;
  1265. break;
  1266. }
  1267. redraw();
  1268. cb(text);
  1269. }
  1270. void CTextInput::setText( const std::string &nText, bool callCb )
  1271. {
  1272. text = nText;
  1273. redraw();
  1274. if(callCb)
  1275. cb(text);
  1276. }
  1277. CTextInput::~CTextInput()
  1278. {
  1279. }
  1280. CFocusable::CFocusable()
  1281. {
  1282. focusables.push_back(this);
  1283. }
  1284. CFocusable::~CFocusable()
  1285. {
  1286. if(inputWithFocus == this)
  1287. inputWithFocus = NULL;
  1288. focusables -= this;
  1289. }
  1290. void CFocusable::giveFocus()
  1291. {
  1292. if(inputWithFocus)
  1293. {
  1294. inputWithFocus->focus = false;
  1295. inputWithFocus->redraw();
  1296. }
  1297. focus = true;
  1298. inputWithFocus = this;
  1299. redraw();
  1300. }
  1301. void CFocusable::moveFocus()
  1302. {
  1303. std::list<CFocusable*>::iterator i = vstd::find(focusables, this),
  1304. ourIt = i;
  1305. for(i++; i != ourIt; i++)
  1306. {
  1307. if(i == focusables.end())
  1308. i = focusables.begin();
  1309. if((*i)->active)
  1310. {
  1311. (*i)->giveFocus();
  1312. break;;
  1313. }
  1314. }
  1315. }