CIntObjectClasses.cpp 34 KB

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