CIntObjectClasses.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. #include "StdInc.h"
  2. #include "CIntObjectClasses.h"
  3. #include "../CBitmapHandler.h"
  4. #include "SDL_Pixels.h"
  5. #include "SDL_Extensions.h"
  6. #include "../Graphics.h"
  7. #include "../CAnimation.h"
  8. #include "CCursorHandler.h"
  9. #include "../CGameInfo.h"
  10. #include "../../CCallback.h"
  11. #include "../../lib/CConfigHandler.h"
  12. #include "../BattleInterface/CBattleInterface.h"
  13. #include "../BattleInterface/CBattleInterfaceClasses.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../CMessage.h"
  16. #include "../CMusicHandler.h"
  17. #include "../GUIClasses.h"
  18. #include "CGuiHandler.h"
  19. #include "../CAdvmapInterface.h"
  20. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  21. {
  22. init();
  23. bg = BG;
  24. freeSurf = Free;
  25. pos.x += x;
  26. pos.y += y;
  27. pos.w = BG->w;
  28. pos.h = BG->h;
  29. }
  30. CPicture::CPicture( const std::string &bmpname, int x, int y )
  31. {
  32. init();
  33. bg = BitmapHandler::loadBitmap(bmpname);
  34. freeSurf = true;;
  35. pos.x += x;
  36. pos.y += y;
  37. if(bg)
  38. {
  39. pos.w = bg->w;
  40. pos.h = bg->h;
  41. }
  42. else
  43. {
  44. pos.w = pos.h = 0;
  45. }
  46. }
  47. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  48. {
  49. init();
  50. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  51. }
  52. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  53. {
  54. init();
  55. createSimpleRect(r, screenFormat, color);
  56. }
  57. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  58. {
  59. needRefresh = false;
  60. srcRect = new Rect(SrcRect);
  61. pos.x += x;
  62. pos.y += y;
  63. pos.w = srcRect->w;
  64. pos.h = srcRect->h;
  65. bg = BG;
  66. freeSurf = free;
  67. }
  68. void CPicture::setSurface(SDL_Surface *to)
  69. {
  70. bg = to;
  71. if (srcRect)
  72. {
  73. pos.w = srcRect->w;
  74. pos.h = srcRect->h;
  75. }
  76. else
  77. {
  78. pos.w = bg->w;
  79. pos.h = bg->h;
  80. }
  81. }
  82. CPicture::~CPicture()
  83. {
  84. if(freeSurf)
  85. SDL_FreeSurface(bg);
  86. delete srcRect;
  87. }
  88. void CPicture::init()
  89. {
  90. needRefresh = false;
  91. srcRect = NULL;
  92. }
  93. void CPicture::show(SDL_Surface * to)
  94. {
  95. if (needRefresh)
  96. showAll(to);
  97. }
  98. void CPicture::showAll(SDL_Surface * to)
  99. {
  100. if(bg)
  101. {
  102. if(srcRect)
  103. {
  104. SDL_Rect srcRectCpy = *srcRect;
  105. SDL_Rect dstRect = srcRectCpy;
  106. dstRect.x = pos.x;
  107. dstRect.y = pos.y;
  108. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  109. }
  110. else
  111. blitAt(bg, pos, to);
  112. }
  113. }
  114. void CPicture::convertToScreenBPP()
  115. {
  116. SDL_Surface *hlp = bg;
  117. bg = SDL_ConvertSurface(hlp,screen->format,0);
  118. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  119. SDL_FreeSurface(hlp);
  120. }
  121. void CPicture::setAlpha(int value)
  122. {
  123. SDL_SetAlpha(bg, SDL_SRCALPHA, value);
  124. }
  125. void CPicture::scaleTo(Point size)
  126. {
  127. SDL_Surface * scaled = CSDL_Ext::scaleSurface(bg, size.x, size.y);
  128. if(freeSurf)
  129. SDL_FreeSurface(bg);
  130. setSurface(scaled);
  131. freeSurf = false;
  132. }
  133. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  134. {
  135. pos += r;
  136. pos.w = r.w;
  137. pos.h = r.h;
  138. if(screenFormat)
  139. bg = CSDL_Ext::newSurface(r.w, r.h);
  140. else
  141. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  142. SDL_FillRect(bg, NULL, color);
  143. freeSurf = true;
  144. }
  145. void CPicture::colorizeAndConvert(int player)
  146. {
  147. assert(bg);
  148. colorize(player);
  149. convertToScreenBPP();
  150. }
  151. void CPicture::colorize(int player)
  152. {
  153. assert(bg);
  154. assert(bg->format->BitsPerPixel == 8);
  155. graphics->blueToPlayersAdv(bg, player);
  156. }
  157. CFilledTexture::CFilledTexture(std::string imageName, Rect position):
  158. CIntObject(0, position.topLeft()),
  159. texture(BitmapHandler::loadBitmap(imageName))
  160. {
  161. pos.w = position.w;
  162. pos.h = position.h;
  163. }
  164. CFilledTexture::~CFilledTexture()
  165. {
  166. SDL_FreeSurface(texture);
  167. }
  168. void CFilledTexture::showAll(SDL_Surface *to)
  169. {
  170. CSDL_Ext::CClipRectGuard guard(to, pos);
  171. CSDL_Ext::fillTexture(to, texture);
  172. }
  173. CButtonBase::CButtonBase()
  174. {
  175. swappedImages = keepFrame = false;
  176. bitmapOffset = 0;
  177. state=NORMAL;
  178. image = NULL;
  179. text = NULL;
  180. }
  181. CButtonBase::~CButtonBase()
  182. {
  183. }
  184. void CButtonBase::update()
  185. {
  186. if (text)
  187. {
  188. if (state == PRESSED)
  189. text->moveTo(Point(pos.x+pos.w/2+1, pos.y+pos.h/2+1));
  190. else
  191. text->moveTo(Point(pos.x+pos.w/2, pos.y+pos.h/2));
  192. }
  193. int newPos = (int)state + bitmapOffset;
  194. if (newPos < 0)
  195. newPos = 0;
  196. if (state == HIGHLIGHTED && image->size() < 4)
  197. newPos = image->size()-1;
  198. if (swappedImages)
  199. {
  200. if (newPos == 0) newPos = 1;
  201. else if (newPos == 1) newPos = 0;
  202. }
  203. if (!keepFrame)
  204. image->setFrame(newPos);
  205. if (active)
  206. redraw();
  207. }
  208. void CButtonBase::addTextOverlay( const std::string &Text, EFonts font, SDL_Color color)
  209. {
  210. OBJ_CONSTRUCTION_CAPTURING_ALL;
  211. delete text;
  212. text = new CLabel(pos.w/2, pos.h/2, font, CENTER, color, Text);
  213. update();
  214. }
  215. void CButtonBase::setOffset(int newOffset)
  216. {
  217. if (bitmapOffset == newOffset)
  218. return;
  219. bitmapOffset = newOffset;
  220. update();
  221. }
  222. void CButtonBase::setState(ButtonState newState)
  223. {
  224. if (state == newState)
  225. return;
  226. state = newState;
  227. update();
  228. }
  229. CButtonBase::ButtonState CButtonBase::getState()
  230. {
  231. return state;
  232. }
  233. bool CButtonBase::isBlocked()
  234. {
  235. return state == BLOCKED;
  236. }
  237. bool CButtonBase::isHighlighted()
  238. {
  239. return state == HIGHLIGHTED;
  240. }
  241. void CButtonBase::block(bool on)
  242. {
  243. setState(on?BLOCKED:NORMAL);
  244. }
  245. CAdventureMapButton::CAdventureMapButton ()
  246. {
  247. hoverable = actOnDown = borderEnabled = soundDisabled = false;
  248. borderColor.unused = 1; // represents a transparent color, used for HighlightableButton
  249. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  250. }
  251. 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 )
  252. {
  253. std::map<int,std::string> pom;
  254. pom[0] = Name;
  255. init(Callback, pom, HelpBox, playerColoredButton, defName, add, x, y, key);
  256. }
  257. CAdventureMapButton::CAdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key/*=0*/ )
  258. {
  259. std::map<int,std::string> pom;
  260. pom[0] = Name;
  261. init(Callback, pom, HelpBox, info->playerColoured, info->defName, &info->additionalDefs, info->x, info->y, key);
  262. }
  263. 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 */ )
  264. {
  265. std::map<int,std::string> pom;
  266. pom[0] = help.first;
  267. init(Callback, pom, help.second, playerColoredButton, defName, add, x, y, key);
  268. }
  269. void CAdventureMapButton::clickLeft(tribool down, bool previousState)
  270. {
  271. if(isBlocked())
  272. return;
  273. if (down)
  274. {
  275. if (!soundDisabled)
  276. CCS->soundh->playSound(soundBase::button);
  277. setState(PRESSED);
  278. }
  279. else if(hoverable && hovered)
  280. setState(HIGHLIGHTED);
  281. else
  282. setState(NORMAL);
  283. if (actOnDown && down)
  284. {
  285. callback();
  286. }
  287. else if (!actOnDown && previousState && (down==false))
  288. {
  289. callback();
  290. }
  291. }
  292. void CAdventureMapButton::clickRight(tribool down, bool previousState)
  293. {
  294. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  295. CRClickPopup::createAndPush(helpBox);
  296. }
  297. void CAdventureMapButton::hover (bool on)
  298. {
  299. if(hoverable)
  300. {
  301. if(on)
  302. setState(HIGHLIGHTED);
  303. else
  304. setState(NORMAL);
  305. }
  306. if(pressedL && on)
  307. setState(PRESSED);
  308. std::string *name = (vstd::contains(hoverTexts,getState()))
  309. ? (&hoverTexts[getState()])
  310. : (vstd::contains(hoverTexts,0) ? (&hoverTexts[0]) : NULL);
  311. if(name && name->size() && !isBlocked()) //if there is no name, there is nohing to display also
  312. {
  313. if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
  314. {
  315. if(on && LOCPLINT->battleInt->console->alterTxt == "")
  316. {
  317. LOCPLINT->battleInt->console->alterTxt = *name;
  318. LOCPLINT->battleInt->console->whoSetAlter = 1;
  319. }
  320. else if (LOCPLINT->battleInt->console->alterTxt == *name)
  321. {
  322. LOCPLINT->battleInt->console->alterTxt = "";
  323. LOCPLINT->battleInt->console->whoSetAlter = 0;
  324. }
  325. }
  326. else if(GH.statusbar) //for other buttons
  327. {
  328. if (on)
  329. GH.statusbar->print(*name);
  330. else if ( GH.statusbar->getCurrent()==(*name) )
  331. GH.statusbar->clear();
  332. }
  333. }
  334. }
  335. 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)
  336. {
  337. currentImage = -1;
  338. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  339. callback = Callback;
  340. hoverable = actOnDown = borderEnabled = soundDisabled = false;
  341. borderColor.unused = 1; // represents a transparent color, used for HighlightableButton
  342. hoverTexts = Name;
  343. helpBox=HelpBox;
  344. if (key != SDLK_UNKNOWN)
  345. assignedKeys.insert(key);
  346. pos.x += x;
  347. pos.y += y;
  348. if (!defName.empty())
  349. imageNames.push_back(defName);
  350. if (add)
  351. for (size_t i=0; i<add->size();i++ )
  352. imageNames.push_back(add->at(i));
  353. setIndex(0, playerColoredButton);
  354. }
  355. void CAdventureMapButton::setIndex(size_t index, bool playerColoredButton)
  356. {
  357. if (index == currentImage || index>=imageNames.size())
  358. return;
  359. currentImage = index;
  360. setImage(new CAnimation(imageNames[index]), playerColoredButton);
  361. }
  362. void CAdventureMapButton::setImage(CAnimation* anim, bool playerColoredButton, int animFlags)
  363. {
  364. OBJ_CONSTRUCTION_CAPTURING_ALL;
  365. delete image;
  366. image = new CAnimImage(anim, getState(), 0, 0, 0, animFlags);
  367. if (playerColoredButton)
  368. image->playerColored(LOCPLINT->playerID);
  369. pos.w = image->pos.w;
  370. pos.h = image->pos.h;
  371. }
  372. void CAdventureMapButton::setPlayerColor(int player)
  373. {
  374. if (image)
  375. image->playerColored(player);
  376. }
  377. void CAdventureMapButton::showAll(SDL_Surface * to)
  378. {
  379. CIntObject::showAll(to);
  380. if (borderEnabled && borderColor.unused == 0)
  381. CSDL_Ext::drawBorder(to, pos.x-1, pos.y-1, pos.w+2, pos.h+2, int3(borderColor.r, borderColor.g, borderColor.b));
  382. }
  383. void CHighlightableButton::select(bool on)
  384. {
  385. selected = on;
  386. if (on)
  387. {
  388. borderEnabled = true;
  389. setState(HIGHLIGHTED);
  390. callback();
  391. }
  392. else
  393. {
  394. borderEnabled = false;
  395. setState(NORMAL);
  396. callback2();
  397. }
  398. if(hoverTexts.size()>1)
  399. {
  400. hover(false);
  401. hover(true);
  402. }
  403. }
  404. void CHighlightableButton::clickLeft(tribool down, bool previousState)
  405. {
  406. if(isBlocked())
  407. return;
  408. if (down && !(onlyOn && isHighlighted()))
  409. {
  410. CCS->soundh->playSound(soundBase::button);
  411. setState(PRESSED);
  412. }
  413. if(previousState)//mouse up
  414. {
  415. if(down == false && getState() == PRESSED)
  416. select(!selected);
  417. else
  418. setState(selected?HIGHLIGHTED:NORMAL);
  419. }
  420. }
  421. 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)
  422. : onlyOn(false), selected(false), callback2(onDeselect)
  423. {
  424. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  425. }
  426. 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 */ )
  427. : onlyOn(false), selected(false) // TODO: callback2(???)
  428. {
  429. ID = myid;
  430. std::map<int,std::string> pom;
  431. pom[0] = help.first;
  432. init(onSelect, pom, help.second, playerColoredButton, defName, add, x, y, key);
  433. }
  434. 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 */ )
  435. : onlyOn(false), selected(false) // TODO: callback2(???)
  436. {
  437. ID = myid;
  438. std::map<int,std::string> pom;
  439. pom[0] = Name;
  440. init(onSelect, pom,HelpBox, playerColoredButton, defName, add, x, y, key);
  441. }
  442. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  443. {
  444. if (bt->parent)
  445. bt->parent->removeChild(bt);
  446. addChild(bt);
  447. bt->recActions = defActions;//FIXME: not needed?
  448. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  449. bt->onlyOn = true;
  450. buttons.push_back(bt);
  451. }
  452. 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)
  453. {
  454. OBJ_CONSTRUCTION_CAPTURING_ALL;
  455. CHighlightableButton *bt = new CHighlightableButton(OnSelect, 0, tooltip, HelpBox, false, defName, 0, x, y, key);
  456. if(musicLike)
  457. {
  458. bt->setOffset(buttons.size()-3);
  459. }
  460. bt->ID = uid;
  461. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  462. bt->onlyOn = true;
  463. buttons.push_back(bt);
  464. }
  465. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
  466. : onChange(OnChange), musicLike(musicLikeButtons)
  467. {}
  468. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  469. {
  470. }
  471. void CHighlightableButtonsGroup::select(int id, bool mode)
  472. {
  473. CHighlightableButton *bt = NULL;
  474. if(mode)
  475. {
  476. for(size_t i=0;i<buttons.size() && !bt; ++i)
  477. if (buttons[i]->ID == id)
  478. bt = buttons[i];
  479. }
  480. else
  481. {
  482. bt = buttons[id];
  483. }
  484. bt->select(true);
  485. selectionChanged(bt->ID);
  486. }
  487. void CHighlightableButtonsGroup::selectionChanged(int to)
  488. {
  489. for(size_t i=0;i<buttons.size(); ++i)
  490. if(buttons[i]->ID!=to && buttons[i]->isHighlighted())
  491. buttons[i]->select(false);
  492. onChange(to);
  493. if (parent)
  494. parent->redraw();
  495. }
  496. void CHighlightableButtonsGroup::show(SDL_Surface * to)
  497. {
  498. if (musicLike)
  499. {
  500. for(size_t i=0;i<buttons.size(); ++i)
  501. if(buttons[i]->isHighlighted())
  502. buttons[i]->show(to);
  503. }
  504. else
  505. CIntObject::show(to);
  506. }
  507. void CHighlightableButtonsGroup::showAll(SDL_Surface * to)
  508. {
  509. if (musicLike)
  510. {
  511. for(size_t i=0;i<buttons.size(); ++i)
  512. if(buttons[i]->isHighlighted())
  513. buttons[i]->showAll(to);
  514. }
  515. else
  516. CIntObject::showAll(to);
  517. }
  518. void CHighlightableButtonsGroup::block( ui8 on )
  519. {
  520. for(size_t i=0;i<buttons.size(); ++i)
  521. {
  522. buttons[i]->block(on);
  523. }
  524. }
  525. void CSlider::sliderClicked()
  526. {
  527. if(!(active & MOVE))
  528. addUsedEvents(MOVE);
  529. }
  530. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  531. {
  532. double v = 0;
  533. if(horizontal)
  534. {
  535. 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 )
  536. return;
  537. v = sEvent.x - pos.x - 24;
  538. v *= positions;
  539. v /= (pos.w - 48);
  540. }
  541. else
  542. {
  543. 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 )
  544. return;
  545. v = sEvent.y - pos.y - 24;
  546. v *= positions;
  547. v /= (pos.h - 48);
  548. }
  549. v += 0.5;
  550. if(v!=value)
  551. {
  552. moveTo(v);
  553. redrawSlider();
  554. }
  555. }
  556. void CSlider::redrawSlider()
  557. {
  558. //slider->show(screenBuf);
  559. }
  560. void CSlider::moveLeft()
  561. {
  562. moveTo(value-1);
  563. }
  564. void CSlider::moveRight()
  565. {
  566. moveTo(value+1);
  567. }
  568. void CSlider::moveTo(int to)
  569. {
  570. vstd::amax(to, 0);
  571. vstd::amin(to, positions);
  572. //same, old position?
  573. if(value == to)
  574. return;
  575. value = to;
  576. if(horizontal)
  577. {
  578. if(positions)
  579. {
  580. double part = static_cast<double>(to) / positions;
  581. part*=(pos.w-48);
  582. int newPos = part + pos.x + 16 - slider->pos.x;
  583. slider->moveBy(Point(newPos, 0));
  584. }
  585. else
  586. slider->moveTo(Point(pos.x+16, pos.y));
  587. }
  588. else
  589. {
  590. if(positions)
  591. {
  592. double part = static_cast<double>(to) / positions;
  593. part*=(pos.h-48);
  594. int newPos = part + pos.y + 16 - slider->pos.y;
  595. slider->moveBy(Point(0, newPos));
  596. }
  597. else
  598. slider->moveTo(Point(pos.x, pos.y+16));
  599. }
  600. if(moved)
  601. moved(to);
  602. }
  603. void CSlider::clickLeft(tribool down, bool previousState)
  604. {
  605. if(down && !slider->isBlocked())
  606. {
  607. double pw = 0;
  608. double rw = 0;
  609. if(horizontal)
  610. {
  611. pw = GH.current->motion.x-pos.x-25;
  612. rw = pw / static_cast<double>(pos.w - 48);
  613. }
  614. else
  615. {
  616. pw = GH.current->motion.y-pos.y-24;
  617. rw = pw / (pos.h-48);
  618. }
  619. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  620. return;
  621. // if (rw>1) return;
  622. // if (rw<0) return;
  623. slider->clickLeft(true, slider->pressedL);
  624. moveTo(rw * positions + 0.5);
  625. return;
  626. }
  627. if(active & MOVE)
  628. removeUsedEvents(MOVE);
  629. }
  630. CSlider::~CSlider()
  631. {
  632. }
  633. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style)
  634. :capacity(Capacity),amount(Amount),horizontal(Horizontal), moved(Moved)
  635. {
  636. OBJ_CONSTRUCTION_CAPTURING_ALL;
  637. setAmount(amount);
  638. addUsedEvents(LCLICK | KEYBOARD | WHEEL);
  639. strongInterest = true;
  640. left = new CAdventureMapButton();
  641. right = new CAdventureMapButton();
  642. slider = new CAdventureMapButton();
  643. pos.x += x;
  644. pos.y += y;
  645. if(horizontal)
  646. {
  647. left->pos.y = slider->pos.y = right->pos.y = pos.y;
  648. left->pos.x = pos.x;
  649. right->pos.x = pos.x + totalw - 16;
  650. }
  651. else
  652. {
  653. left->pos.x = slider->pos.x = right->pos.x = pos.x;
  654. left->pos.y = pos.y;
  655. right->pos.y = pos.y + totalw - 16;
  656. }
  657. left->callback = boost::bind(&CSlider::moveLeft,this);
  658. right->callback = boost::bind(&CSlider::moveRight,this);
  659. slider->callback = boost::bind(&CSlider::sliderClicked,this);
  660. left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
  661. if(horizontal)
  662. {
  663. pos.h = 16;
  664. pos.w = totalw;
  665. }
  666. else
  667. {
  668. pos.w = 16;
  669. pos.h = totalw;
  670. }
  671. if(style == 0)
  672. {
  673. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  674. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  675. //use source def to create custom animations. Format "name.def:123" will load this frame from def file
  676. CAnimation *animLeft = new CAnimation();
  677. animLeft->setCustom(name + ":0", 0);
  678. animLeft->setCustom(name + ":1", 1);
  679. left->setImage(animLeft);
  680. CAnimation *animRight = new CAnimation();
  681. animRight->setCustom(name + ":2", 0);
  682. animRight->setCustom(name + ":3", 1);
  683. right->setImage(animRight);
  684. CAnimation *animSlider = new CAnimation();
  685. animSlider->setCustom(name + ":4", 0);
  686. slider->setImage(animSlider);
  687. }
  688. else
  689. {
  690. left->setImage(new CAnimation(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF"));
  691. right->setImage(new CAnimation(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF"));
  692. slider->setImage(new CAnimation("SCNRBSL.DEF"));
  693. }
  694. slider->actOnDown = true;
  695. slider->soundDisabled = true;
  696. left->soundDisabled = true;
  697. right->soundDisabled = true;
  698. value = -1;
  699. moveTo(Value);
  700. }
  701. void CSlider::block( bool on )
  702. {
  703. left->block(on);
  704. right->block(on);
  705. slider->block(on);
  706. }
  707. void CSlider::setAmount( int to )
  708. {
  709. amount = to;
  710. positions = to - capacity;
  711. vstd::amax(positions, 0);
  712. }
  713. void CSlider::showAll(SDL_Surface * to)
  714. {
  715. CSDL_Ext::fillRect(to, &pos, 0);
  716. CIntObject::showAll(to);
  717. }
  718. void CSlider::wheelScrolled(bool down, bool in)
  719. {
  720. moveTo(value + 3 * (down ? +1 : -1));
  721. }
  722. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  723. {
  724. if(key.state != SDL_PRESSED) return;
  725. int moveDest = 0;
  726. switch(key.keysym.sym)
  727. {
  728. case SDLK_UP:
  729. case SDLK_LEFT:
  730. moveDest = value - 1;
  731. break;
  732. case SDLK_DOWN:
  733. case SDLK_RIGHT:
  734. moveDest = value + 1;
  735. break;
  736. case SDLK_PAGEUP:
  737. moveDest = value - capacity + 1;
  738. break;
  739. case SDLK_PAGEDOWN:
  740. moveDest = value + capacity - 1;
  741. break;
  742. case SDLK_HOME:
  743. moveDest = 0;
  744. break;
  745. case SDLK_END:
  746. moveDest = amount - capacity;
  747. break;
  748. default:
  749. return;
  750. }
  751. moveTo(moveDest);
  752. }
  753. void CSlider::moveToMax()
  754. {
  755. moveTo(amount);
  756. }
  757. static void intDeleter(CIntObject* object)
  758. {
  759. delete object;
  760. }
  761. CObjectList::CObjectList(CreateFunc create, DestroyFunc destroy):
  762. createObject(create),
  763. destroyObject(destroy)
  764. {
  765. if (!destroyObject)
  766. destroyObject = intDeleter;
  767. }
  768. void CObjectList::deleteItem(CIntObject* item)
  769. {
  770. if (!item)
  771. return;
  772. removeChild(item);
  773. destroyObject(item);
  774. }
  775. CIntObject* CObjectList::createItem(size_t index)
  776. {
  777. OBJ_CONSTRUCTION_CAPTURING_ALL;
  778. CIntObject * item = createObject(index);
  779. if (item == NULL)
  780. item = new CIntObject();
  781. item->recActions = defActions;
  782. addChild(item);
  783. return item;
  784. }
  785. CTabbedInt::CTabbedInt(CreateFunc create, DestroyFunc destroy, Point position, size_t ActiveID):
  786. CObjectList(create, destroy),
  787. activeTab(NULL),
  788. activeID(ActiveID)
  789. {
  790. pos += position;
  791. reset();
  792. }
  793. void CTabbedInt::setActive(size_t which)
  794. {
  795. if (which != activeID)
  796. {
  797. activeID = which;
  798. reset();
  799. }
  800. }
  801. void CTabbedInt::reset()
  802. {
  803. deleteItem(activeTab);
  804. activeTab = createItem(activeID);
  805. activeTab->moveTo(pos.topLeft());
  806. if (active)
  807. redraw();
  808. }
  809. CIntObject * CTabbedInt::getItem()
  810. {
  811. return activeTab;
  812. }
  813. CListBox::CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  814. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos):
  815. CObjectList(create, destroy),
  816. first(InitialPos),
  817. totalSize(TotalSize),
  818. itemOffset(ItemOffset),
  819. slider(nullptr)
  820. {
  821. pos += Pos;
  822. items.resize(VisibleSize, nullptr);
  823. if (Slider & 1)
  824. {
  825. OBJ_CONSTRUCTION_CAPTURING_ALL;
  826. slider = new CSlider(SliderPos.x, SliderPos.y, SliderPos.w, boost::bind(&CListBox::moveToPos, this, _1),
  827. VisibleSize, TotalSize, InitialPos, Slider & 2, Slider & 4);
  828. }
  829. reset();
  830. }
  831. // Used to move active items after changing list position
  832. void CListBox::updatePositions()
  833. {
  834. Point itemPos = pos.topLeft();
  835. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  836. {
  837. (*it)->moveTo(itemPos);
  838. itemPos += itemOffset;
  839. }
  840. if (active)
  841. {
  842. redraw();
  843. if (slider)
  844. slider->moveTo(first);
  845. }
  846. }
  847. void CListBox::reset()
  848. {
  849. size_t current = first;
  850. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  851. {
  852. deleteItem(*it);
  853. *it = createItem(current++);
  854. }
  855. updatePositions();
  856. }
  857. void CListBox::resize(size_t newSize)
  858. {
  859. totalSize = newSize;
  860. if (slider)
  861. slider->setAmount(totalSize);
  862. reset();
  863. }
  864. size_t CListBox::size()
  865. {
  866. return totalSize;
  867. }
  868. CIntObject * CListBox::getItem(size_t which)
  869. {
  870. if (which < first || which > first + items.size() || which > totalSize)
  871. return nullptr;
  872. size_t i=first;
  873. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  874. if( i == which)
  875. return *iter;
  876. return nullptr;
  877. }
  878. size_t CListBox::getIndexOf(CIntObject *item)
  879. {
  880. size_t i=first;
  881. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  882. if(*iter == item)
  883. return i;
  884. return size_t(-1);
  885. }
  886. void CListBox::scrollTo(size_t which)
  887. {
  888. //scroll up
  889. if (first > which)
  890. moveToPos(which);
  891. //scroll down
  892. else if (first + items.size() <= which && which < totalSize)
  893. moveToPos(which - items.size() + 1);
  894. }
  895. void CListBox::moveToPos(size_t which)
  896. {
  897. //Calculate new position
  898. size_t maxPossible;
  899. if (totalSize > items.size())
  900. maxPossible = totalSize - items.size();
  901. else
  902. maxPossible = 0;
  903. size_t newPos = std::min(which, maxPossible);
  904. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  905. if (first - newPos == 1)
  906. moveToPrev();
  907. else if (newPos - first == 1)
  908. moveToNext();
  909. else if (newPos != first)
  910. {
  911. first = newPos;
  912. reset();
  913. }
  914. }
  915. void CListBox::moveToNext()
  916. {
  917. //Remove front item and insert new one to end
  918. if (first + items.size() < totalSize)
  919. {
  920. first++;
  921. deleteItem(items.front());
  922. items.pop_front();
  923. items.push_back(createItem(first+items.size()));
  924. updatePositions();
  925. }
  926. }
  927. void CListBox::moveToPrev()
  928. {
  929. //Remove last item and insert new one at start
  930. if (first)
  931. {
  932. first--;
  933. deleteItem(items.back());
  934. items.pop_back();
  935. items.push_front(createItem(first));
  936. updatePositions();
  937. }
  938. }
  939. size_t CListBox::getPos()
  940. {
  941. return first;
  942. }
  943. const std::list<CIntObject *> &CListBox::getItems()
  944. {
  945. return items;
  946. }
  947. void CSimpleWindow::show(SDL_Surface * to)
  948. {
  949. if(bitmap)
  950. blitAt(bitmap,pos.x,pos.y,to);
  951. }
  952. CSimpleWindow::~CSimpleWindow()
  953. {
  954. if (bitmap)
  955. {
  956. SDL_FreeSurface(bitmap);
  957. bitmap=NULL;
  958. }
  959. }
  960. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  961. {
  962. bg=BitmapHandler::loadBitmap(name);
  963. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  964. pos.x += x;
  965. pos.y += y;
  966. if(maxw >= 0)
  967. pos.w = std::min(bg->w,maxw);
  968. else
  969. pos.w=bg->w;
  970. pos.h=bg->h;
  971. middlex=(pos.w/2)+pos.x;
  972. middley=(bg->h/2)+pos.y;
  973. }
  974. CStatusBar::~CStatusBar()
  975. {
  976. SDL_FreeSurface(bg);
  977. }
  978. void CStatusBar::clear()
  979. {
  980. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  981. {
  982. current="";
  983. redraw();
  984. }
  985. }
  986. void CStatusBar::print(const std::string & text)
  987. {
  988. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  989. {
  990. current=text;
  991. redraw();
  992. }
  993. }
  994. void CStatusBar::showAll(SDL_Surface * to)
  995. {
  996. show(to);
  997. }
  998. void CStatusBar::show(SDL_Surface * to)
  999. {
  1000. SDL_Rect srcRect = genRect(pos.h,pos.w,0,0);
  1001. SDL_Rect dstRect = genRect(pos.h,pos.w,pos.x,pos.y);
  1002. CSDL_Ext::blitSurface(bg,&srcRect,to,&dstRect);
  1003. graphics->fonts[FONT_SMALL]->renderTextCenter(to, current, Colors::WHITE, Point(middlex, middley));
  1004. }
  1005. std::string CStatusBar::getCurrent()
  1006. {
  1007. return current;
  1008. }
  1009. void CHoverableArea::hover (bool on)
  1010. {
  1011. if (on)
  1012. GH.statusbar->print(hoverText);
  1013. else if (GH.statusbar->getCurrent()==hoverText)
  1014. GH.statusbar->clear();
  1015. }
  1016. CHoverableArea::CHoverableArea()
  1017. {
  1018. addUsedEvents(HOVER);
  1019. }
  1020. CHoverableArea::~CHoverableArea()
  1021. {
  1022. }
  1023. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  1024. {
  1025. if(!down && previousState)
  1026. {
  1027. LOCPLINT->showInfoDialog(text);
  1028. }
  1029. }
  1030. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  1031. {
  1032. adventureInt->handleRightClick(text, down);
  1033. }
  1034. LRClickableAreaWText::LRClickableAreaWText()
  1035. {
  1036. init();
  1037. }
  1038. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  1039. {
  1040. init();
  1041. pos = Pos + pos;
  1042. hoverText = HoverText;
  1043. text = ClickText;
  1044. }
  1045. LRClickableAreaWText::~LRClickableAreaWText()
  1046. {
  1047. }
  1048. void LRClickableAreaWText::init()
  1049. {
  1050. addUsedEvents(LCLICK | RCLICK | HOVER);
  1051. }
  1052. void CLabel::showAll(SDL_Surface * to)
  1053. {
  1054. CIntObject::showAll(to);
  1055. std::string toPrint = visibleText();
  1056. if(!toPrint.length())
  1057. return;
  1058. blitLine(to, pos.topLeft()/2 + pos.bottomRight()/2, toPrint);
  1059. }
  1060. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::WHITE*/, const std::string &Text /*= ""*/)
  1061. :CTextContainer(Align, Font, Color), text(Text)
  1062. {
  1063. autoRedraw = true;
  1064. pos.x += x;
  1065. pos.y += y;
  1066. pos.w = pos.h = 0;
  1067. bg = NULL;
  1068. ignoreLeadingWhitespace = false;
  1069. if (alignment == TOPLEFT) // causes issues for MIDDLE
  1070. {
  1071. pos.w = graphics->fonts[font]->getStringWidth(text.c_str());
  1072. pos.h = graphics->fonts[font]->getLineHeight();
  1073. }
  1074. }
  1075. std::string CLabel::visibleText()
  1076. {
  1077. std::string ret = text;
  1078. if(ignoreLeadingWhitespace)
  1079. boost::trim_left(ret);
  1080. return ret;
  1081. }
  1082. void CLabel::setTxt(const std::string &Txt)
  1083. {
  1084. text = Txt;
  1085. if(autoRedraw)
  1086. {
  1087. if(bg || !parent)
  1088. redraw();
  1089. else
  1090. parent->redraw();
  1091. }
  1092. }
  1093. void CBoundedLabel::setBounds(int limitW, int limitH)
  1094. {
  1095. pos.h = limitH;
  1096. pos.w = limitW;
  1097. recalculateLines(text);
  1098. }
  1099. void CBoundedLabel::setTxt(const std::string &Txt)
  1100. {
  1101. recalculateLines(Txt);
  1102. CLabel::setTxt(Txt);
  1103. }
  1104. void CTextContainer::blitLine(SDL_Surface *to, Point where, std::string what)
  1105. {
  1106. const IFont * f = graphics->fonts[font];
  1107. auto renderer = &IFont::renderTextLeft;
  1108. switch (alignment)
  1109. {
  1110. break; case TOPLEFT: renderer = &IFont::renderTextLeft;
  1111. break; case CENTER: renderer = &IFont::renderTextCenter;
  1112. break; case BOTTOMRIGHT: renderer = &IFont::renderTextRight;
  1113. break; default: assert(0);
  1114. }
  1115. size_t begin = 0;
  1116. size_t end;
  1117. std::string delimeters = "{}";
  1118. size_t currDelimeter = 0;
  1119. do
  1120. {
  1121. end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  1122. if (begin != end)
  1123. {
  1124. std::string toPrint = what.substr(begin, end-1);
  1125. if (currDelimeter % 2) // Enclosed in {} text - set to yellow
  1126. (graphics->fonts[font]->*renderer)(to, toPrint, Colors::YELLOW, where);
  1127. else // Non-enclosed text
  1128. (graphics->fonts[font]->*renderer)(to, toPrint, color, where);
  1129. begin = end;
  1130. where.x += f->getStringWidth(toPrint.c_str());
  1131. }
  1132. currDelimeter++;
  1133. }
  1134. while (begin++ != std::string::npos);
  1135. }
  1136. CTextContainer::CTextContainer(EAlignment alignment, EFonts font, SDL_Color color):
  1137. alignment(alignment),
  1138. font(font),
  1139. color(color)
  1140. {}
  1141. void CBoundedLabel::showAll(SDL_Surface * to)
  1142. {
  1143. CIntObject::showAll(to);
  1144. const IFont * f = graphics->fonts[font];
  1145. int lineHeight = f->getLineHeight();
  1146. int lineCapacity = pos.h / lineHeight;
  1147. int dy = f->getLineHeight(); //line height
  1148. int base_y = pos.y;
  1149. if(alignment == CENTER)
  1150. base_y += std::max((pos.h - maxH)/2,0);
  1151. for (int i = 0; i < lineCapacity; i++)
  1152. {
  1153. const std::string &line = lines[i];
  1154. if ( !(lines.size() && line.size())) //empty message or empty line
  1155. continue;
  1156. int x = pos.x;
  1157. if(alignment == CENTER)
  1158. {
  1159. x += pos.w - f->getStringWidth(line.c_str()) / 2;
  1160. }
  1161. blitLine(to, Point(x, base_y + i * dy), line);
  1162. }
  1163. }
  1164. void CBoundedLabel::recalculateLines(const std::string &Txt)
  1165. {
  1166. lines.clear();
  1167. const IFont * f = graphics->fonts[font];
  1168. int lineHeight = f->getLineHeight();
  1169. lines = CMessage::breakText(Txt, pos.w, font);
  1170. maxH = lineHeight * lines.size();
  1171. maxW = 0;
  1172. BOOST_FOREACH(const std::string &line, lines)
  1173. vstd::amax(maxW, f->getStringWidth(line.c_str()));
  1174. }
  1175. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  1176. font(Font), align(Align), color(Color)
  1177. {}
  1178. void CLabelGroup::add(int x, int y, const std::string &text)
  1179. {
  1180. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1181. new CLabel(x, y, font, align, color, text);
  1182. }
  1183. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::WHITE*/)
  1184. :CBoundedLabel(rect.x, rect.y, Font, Align, Color, Text), sliderStyle(SliderStyle), slider(NULL)
  1185. {
  1186. type |= REDRAW_PARENT;
  1187. autoRedraw = false;
  1188. pos.h = rect.h;
  1189. pos.w = rect.w;
  1190. assert(Align == TOPLEFT || Align == CENTER); //TODO: support for other alignments
  1191. assert(pos.w >= 40); //we need some space
  1192. setTxt(Text);
  1193. }
  1194. void CTextBox::recalculateLines(const std::string &Txt)
  1195. {
  1196. //TODO: merge with CBoundedlabel::recalculateLines
  1197. vstd::clear_pointer(slider);
  1198. lines.clear();
  1199. const IFont * f = graphics->fonts[font];
  1200. int lineHeight = f->getLineHeight();
  1201. int lineCapacity = pos.h / lineHeight;
  1202. lines = CMessage::breakText(Txt, pos.w, font);
  1203. if (lines.size() > lineCapacity) //we need to add a slider
  1204. {
  1205. lines = CMessage::breakText(Txt, pos.w - 32 - 10, font);
  1206. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1207. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1), lineCapacity, lines.size(), 0, false, sliderStyle);
  1208. if(active)
  1209. slider->activate();
  1210. }
  1211. maxH = lineHeight * lines.size();
  1212. maxW = 0;
  1213. BOOST_FOREACH(const std::string &line, lines)
  1214. vstd::amax(maxW, f->getStringWidth(line));
  1215. }
  1216. void CTextBox::showAll(SDL_Surface * to)
  1217. {
  1218. CIntObject::showAll(to);
  1219. const IFont * f = graphics->fonts[font];
  1220. int dy = f->getLineHeight(); //line height
  1221. int base_y = pos.y;
  1222. if (alignment == CENTER)
  1223. base_y += std::max((pos.h - maxH)/2,0);
  1224. int howManyLinesToPrint = slider ? slider->capacity : lines.size();
  1225. int firstLineToPrint = slider ? slider->value : 0;
  1226. for (int i = 0; i < howManyLinesToPrint; i++)
  1227. {
  1228. const std::string &line = lines[i + firstLineToPrint];
  1229. if(!line.size()) continue;
  1230. int width = pos.w + (slider ? (slider->pos.w) : 0);
  1231. int x = pos.x + int(alignment) * width / 2;
  1232. blitLine(to, Point(x, base_y + i * dy), line);
  1233. }
  1234. }
  1235. void CTextBox::sliderMoved(int to)
  1236. {
  1237. if(!slider)
  1238. return;
  1239. redraw();
  1240. }
  1241. void CGStatusBar::print(const std::string & Text)
  1242. {
  1243. setTxt(Text);
  1244. }
  1245. void CGStatusBar::clear()
  1246. {
  1247. setTxt("");
  1248. }
  1249. std::string CGStatusBar::getCurrent()
  1250. {
  1251. return text;
  1252. }
  1253. //CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::WHITE*/, const std::string &Text /*= ""*/)
  1254. //: CLabel(x, y, Font, Align, Color, Text)
  1255. //{
  1256. // init();
  1257. //}
  1258. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::WHITE*/)
  1259. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  1260. {
  1261. init();
  1262. bg = BG;
  1263. addChild(bg);
  1264. pos = bg->pos;
  1265. calcOffset();
  1266. }
  1267. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  1268. : CLabel(x, y, FONT_SMALL, CENTER)
  1269. {
  1270. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1271. init();
  1272. bg = new CPicture(name);
  1273. pos = bg->pos;
  1274. if(maxw < pos.w)
  1275. {
  1276. vstd::amin(pos.w, maxw);
  1277. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  1278. }
  1279. calcOffset();
  1280. }
  1281. CGStatusBar::~CGStatusBar()
  1282. {
  1283. GH.statusbar = oldStatusBar;
  1284. }
  1285. void CGStatusBar::show(SDL_Surface * to)
  1286. {
  1287. }
  1288. void CGStatusBar::init()
  1289. {
  1290. oldStatusBar = GH.statusbar;
  1291. GH.statusbar = this;
  1292. }
  1293. void CGStatusBar::calcOffset()
  1294. {
  1295. switch(alignment)
  1296. {
  1297. case CENTER:
  1298. textOffset = Point(pos.w/2, pos.h/2);
  1299. break;
  1300. case BOTTOMRIGHT:
  1301. textOffset = Point(pos.w, pos.h);
  1302. break;
  1303. }
  1304. }
  1305. CTextInput::CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB):
  1306. CLabel(Pos.x, Pos.y, font, CENTER),
  1307. cb(CB)
  1308. {
  1309. type |= REDRAW_PARENT;
  1310. focus = false;
  1311. pos.h = Pos.h;
  1312. pos.w = Pos.w;
  1313. textOffset = Point(pos.w/2, pos.h/2);
  1314. captureAllKeys = true;
  1315. bg = nullptr;
  1316. addUsedEvents(LCLICK | KEYBOARD);
  1317. giveFocus();
  1318. }
  1319. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1320. :cb(CB)
  1321. {
  1322. focus = false;
  1323. pos += Pos;
  1324. captureAllKeys = true;
  1325. OBJ_CONSTRUCTION;
  1326. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1327. addUsedEvents(LCLICK | KEYBOARD);
  1328. giveFocus();
  1329. }
  1330. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  1331. {
  1332. focus = false;
  1333. pos += Pos;
  1334. captureAllKeys = true;
  1335. OBJ_CONSTRUCTION;
  1336. bg = new CPicture(Pos, 0, true);
  1337. Rect hlp = Pos;
  1338. if(srf)
  1339. CSDL_Ext::blitSurface(srf, &hlp, *bg, NULL);
  1340. else
  1341. SDL_FillRect(*bg, NULL, 0);
  1342. pos.w = bg->pos.w;
  1343. pos.h = bg->pos.h;
  1344. bg->pos = pos;
  1345. addUsedEvents(LCLICK | KEYBOARD);
  1346. giveFocus();
  1347. }
  1348. std::string CTextInput::visibleText()
  1349. {
  1350. return focus ? text + "_" : text;
  1351. }
  1352. void CTextInput::clickLeft( tribool down, bool previousState )
  1353. {
  1354. if(down && !focus)
  1355. giveFocus();
  1356. }
  1357. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1358. {
  1359. if(!focus || key.state != SDL_PRESSED)
  1360. return;
  1361. if(key.keysym.sym == SDLK_TAB)
  1362. {
  1363. moveFocus();
  1364. GH.breakEventHandling();
  1365. return;
  1366. }
  1367. std::string oldText = text;
  1368. switch(key.keysym.sym)
  1369. {
  1370. case SDLK_BACKSPACE:
  1371. if(!text.empty())
  1372. text.resize(text.size()-1);
  1373. break;
  1374. default:
  1375. if (key.keysym.unicode < ' ')
  1376. return;
  1377. else
  1378. text += key.keysym.unicode; //TODO 16-/>8
  1379. break;
  1380. }
  1381. filters(text, oldText);
  1382. if (text != oldText)
  1383. {
  1384. redraw();
  1385. cb(text);
  1386. }
  1387. }
  1388. void CTextInput::setTxt( const std::string &nText, bool callCb )
  1389. {
  1390. CLabel::setTxt(nText);
  1391. if(callCb)
  1392. cb(text);
  1393. }
  1394. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  1395. {
  1396. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER)
  1397. return false;
  1398. //this should allow all non-printable keys to go through (for example arrows)
  1399. if (key.keysym.unicode < ' ')
  1400. return false;
  1401. return true;
  1402. }
  1403. void CTextInput::filenameFilter(std::string & text, const std::string &)
  1404. {
  1405. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  1406. size_t pos;
  1407. while ((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  1408. text.erase(pos, 1);
  1409. }
  1410. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  1411. {
  1412. assert(minValue < maxValue);
  1413. if (text.empty())
  1414. text = "0";
  1415. size_t pos = 0;
  1416. if (text[0] == '-') //allow '-' sign as first symbol only
  1417. pos++;
  1418. while (pos < text.size())
  1419. {
  1420. if (text[pos] < '0' || text[pos] > '9')
  1421. {
  1422. text = oldText;
  1423. return; //new text is not number.
  1424. }
  1425. pos++;
  1426. }
  1427. try
  1428. {
  1429. int value = boost::lexical_cast<int>(text);
  1430. if (value < minValue)
  1431. text = boost::lexical_cast<std::string>(minValue);
  1432. else if (value > maxValue)
  1433. text = boost::lexical_cast<std::string>(maxValue);
  1434. }
  1435. catch(boost::bad_lexical_cast &)
  1436. {
  1437. //Should never happen. Unless I missed some cases
  1438. tlog0 << "Warning: failed to convert "<< text << " to number!\n";
  1439. text = oldText;
  1440. }
  1441. }
  1442. CFocusable::CFocusable()
  1443. {
  1444. focusables.push_back(this);
  1445. }
  1446. CFocusable::~CFocusable()
  1447. {
  1448. if(inputWithFocus == this)
  1449. inputWithFocus = NULL;
  1450. focusables -= this;
  1451. }
  1452. void CFocusable::giveFocus()
  1453. {
  1454. if(inputWithFocus)
  1455. {
  1456. inputWithFocus->focus = false;
  1457. inputWithFocus->redraw();
  1458. }
  1459. focus = true;
  1460. inputWithFocus = this;
  1461. redraw();
  1462. }
  1463. void CFocusable::moveFocus()
  1464. {
  1465. std::list<CFocusable*>::iterator i = vstd::find(focusables, this),
  1466. ourIt = i;
  1467. for(i++; i != ourIt; i++)
  1468. {
  1469. if(i == focusables.end())
  1470. i = focusables.begin();
  1471. if((*i)->active)
  1472. {
  1473. (*i)->giveFocus();
  1474. break;;
  1475. }
  1476. }
  1477. }
  1478. CWindowObject::CWindowObject(int options_, std::string imageName, Point centerAt):
  1479. CIntObject(getUsedEvents(options_), Point()),
  1480. shadow(nullptr),
  1481. options(options_),
  1482. background(createBg(imageName, options & PLAYER_COLORED))
  1483. {
  1484. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1485. if (options & RCLICK_POPUP)
  1486. CCS->curh->hide();
  1487. if (background)
  1488. pos = background->center(centerAt);
  1489. else
  1490. center(centerAt);
  1491. if (!(options & SHADOW_DISABLED))
  1492. setShadow(true);
  1493. }
  1494. CWindowObject::CWindowObject(int options_, std::string imageName):
  1495. CIntObject(getUsedEvents(options_), Point()),
  1496. shadow(nullptr),
  1497. options(options_),
  1498. background(createBg(imageName, options & PLAYER_COLORED))
  1499. {
  1500. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1501. if (options & RCLICK_POPUP)
  1502. CCS->curh->hide();
  1503. if (background)
  1504. pos = background->center();
  1505. else
  1506. center(Point(screen->w/2, screen->h/2));
  1507. if (!(options & SHADOW_DISABLED))
  1508. setShadow(true);
  1509. }
  1510. CWindowObject::~CWindowObject()
  1511. {
  1512. setShadow(false);
  1513. }
  1514. CPicture * CWindowObject::createBg(std::string imageName, bool playerColored)
  1515. {
  1516. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1517. if (imageName.empty())
  1518. return nullptr;
  1519. auto image = new CPicture(imageName);
  1520. if (playerColored)
  1521. image->colorize(LOCPLINT->playerID);
  1522. return image;
  1523. }
  1524. void CWindowObject::setBackground(std::string filename)
  1525. {
  1526. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1527. delete background;
  1528. background = createBg(filename, options & PLAYER_COLORED);
  1529. if (background)
  1530. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  1531. updateShadow();
  1532. }
  1533. int CWindowObject::getUsedEvents(int options)
  1534. {
  1535. if (options & RCLICK_POPUP)
  1536. return RCLICK;
  1537. return 0;
  1538. }
  1539. void CWindowObject::updateShadow()
  1540. {
  1541. setShadow(false);
  1542. if (!(options & SHADOW_DISABLED))
  1543. setShadow(true);
  1544. }
  1545. void CWindowObject::setShadow(bool on)
  1546. {
  1547. //size of shadow
  1548. static const int size = 8;
  1549. if (on == bool(shadow))
  1550. return;
  1551. vstd::clear_pointer(shadow);
  1552. //object too small to cast shadow
  1553. if (pos.h <= size || pos.w <= size)
  1554. return;
  1555. if (on)
  1556. {
  1557. //helper to set last row
  1558. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  1559. {
  1560. Uint8 * ptr = (Uint8*)surf->pixels + surf->pitch * (row);
  1561. for (size_t i=0; i< surf->w; i++)
  1562. {
  1563. Channels::px<4>::a.set(ptr, 128);
  1564. ptr+=4;
  1565. }
  1566. };
  1567. // helper to set last column
  1568. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  1569. {
  1570. Uint8 * ptr = (Uint8*)surf->pixels + 4 * (col);
  1571. for (size_t i=0; i< surf->h; i++)
  1572. {
  1573. Channels::px<4>::a.set(ptr, 128);
  1574. ptr+= surf->pitch;
  1575. }
  1576. };
  1577. static SDL_Surface * shadowCornerTempl = nullptr;
  1578. static SDL_Surface * shadowBottomTempl = nullptr;
  1579. static SDL_Surface * shadowRightTempl = nullptr;
  1580. //one-time initialization
  1581. if (!shadowCornerTempl)
  1582. {
  1583. //create "template" surfaces
  1584. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  1585. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  1586. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  1587. Uint32 shadowColor = SDL_MapRGBA(shadowCornerTempl->format, 0, 0, 0, 192);
  1588. //fill with shadow body color
  1589. SDL_FillRect(shadowCornerTempl, NULL, shadowColor);
  1590. SDL_FillRect(shadowBottomTempl, NULL, shadowColor);
  1591. SDL_FillRect(shadowRightTempl, NULL, shadowColor);
  1592. //fill last row and column with more transparent color
  1593. blitAlphaCol(shadowRightTempl , size-1);
  1594. blitAlphaCol(shadowCornerTempl, size-1);
  1595. blitAlphaRow(shadowBottomTempl, size-1);
  1596. blitAlphaRow(shadowCornerTempl, size-1);
  1597. }
  1598. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1599. //FIXME: do something with this points
  1600. Point shadowStart;
  1601. if (options & BORDERED)
  1602. shadowStart = Point(size - 14, size - 14);
  1603. else
  1604. shadowStart = Point(size, size);
  1605. Point shadowPos;
  1606. if (options & BORDERED)
  1607. shadowPos = Point(pos.w + 14, pos.h + 14);
  1608. else
  1609. shadowPos = Point(pos.w, pos.h);
  1610. Point fullsize;
  1611. if (options & BORDERED)
  1612. fullsize = Point(pos.w + 28, pos.h + 29);
  1613. else
  1614. fullsize = Point(pos.w, pos.h);
  1615. //create base 8x8 piece of shadow
  1616. SDL_Surface * shadowCorner = CSDL_Ext::copySurface(shadowCornerTempl);
  1617. SDL_Surface * shadowBottom = CSDL_Ext::scaleSurfaceFast(shadowBottomTempl, fullsize.x - size, size);
  1618. SDL_Surface * shadowRight = CSDL_Ext::scaleSurfaceFast(shadowRightTempl, size, fullsize.y - size);
  1619. blitAlphaCol(shadowBottom, 0);
  1620. blitAlphaRow(shadowRight, 0);
  1621. //generate "shadow" object with these 3 pieces in it
  1622. shadow = new CIntObject;
  1623. shadow->addChild(new CPicture(shadowCorner, shadowPos.x, shadowPos.y));
  1624. shadow->addChild(new CPicture(shadowRight, shadowPos.x, shadowStart.y));
  1625. shadow->addChild(new CPicture(shadowBottom, shadowStart.x, shadowPos.y));
  1626. }
  1627. }
  1628. void CWindowObject::showAll(SDL_Surface *to)
  1629. {
  1630. CIntObject::showAll(to);
  1631. if ((options & BORDERED) && (pos.h != to->h || pos.w != to->w))
  1632. CMessage::drawBorder(LOCPLINT ? LOCPLINT->playerID : 1, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  1633. }
  1634. void CWindowObject::close()
  1635. {
  1636. GH.popIntTotally(this);
  1637. }
  1638. void CWindowObject::clickRight(tribool down, bool previousState)
  1639. {
  1640. close();
  1641. CCS->curh->show();
  1642. }