CIntObjectClasses.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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 "../battle/CBattleInterface.h"
  13. #include "../battle/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 = nullptr;
  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, nullptr, color);
  143. freeSurf = true;
  144. }
  145. void CPicture::colorizeAndConvert(PlayerColor player)
  146. {
  147. assert(bg);
  148. colorize(player);
  149. convertToScreenBPP();
  150. }
  151. void CPicture::colorize(PlayerColor 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 = nullptr;
  179. text = nullptr;
  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 /*= nullptr*/, 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]) : nullptr);
  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 (auto & elem : *add)
  352. imageNames.push_back(elem);
  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(PlayerColor 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 /*= nullptr*/, 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 /*= nullptr*/, 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, nullptr, 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. assert(!buttons.empty());
  474. CHighlightableButton *bt = buttons.front();
  475. if(mode)
  476. {
  477. for(auto btn : buttons)
  478. if (btn->ID == id)
  479. bt = btn;
  480. }
  481. else
  482. {
  483. bt = buttons[id];
  484. }
  485. bt->select(true);
  486. selectionChanged(bt->ID);
  487. }
  488. void CHighlightableButtonsGroup::selectionChanged(int to)
  489. {
  490. for(auto & elem : buttons)
  491. if(elem->ID!=to && elem->isHighlighted())
  492. elem->select(false);
  493. onChange(to);
  494. if (parent)
  495. parent->redraw();
  496. }
  497. void CHighlightableButtonsGroup::show(SDL_Surface * to)
  498. {
  499. if (musicLike)
  500. {
  501. for(auto & elem : buttons)
  502. if(elem->isHighlighted())
  503. elem->show(to);
  504. }
  505. else
  506. CIntObject::show(to);
  507. }
  508. void CHighlightableButtonsGroup::showAll(SDL_Surface * to)
  509. {
  510. if (musicLike)
  511. {
  512. for(auto & elem : buttons)
  513. if(elem->isHighlighted())
  514. elem->showAll(to);
  515. }
  516. else
  517. CIntObject::showAll(to);
  518. }
  519. void CHighlightableButtonsGroup::block( ui8 on )
  520. {
  521. for(auto & elem : buttons)
  522. {
  523. elem->block(on);
  524. }
  525. }
  526. void CSlider::sliderClicked()
  527. {
  528. if(!(active & MOVE))
  529. addUsedEvents(MOVE);
  530. }
  531. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  532. {
  533. double v = 0;
  534. if(horizontal)
  535. {
  536. 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 )
  537. return;
  538. v = sEvent.x - pos.x - 24;
  539. v *= positions;
  540. v /= (pos.w - 48);
  541. }
  542. else
  543. {
  544. 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 )
  545. return;
  546. v = sEvent.y - pos.y - 24;
  547. v *= positions;
  548. v /= (pos.h - 48);
  549. }
  550. v += 0.5;
  551. if(v!=value)
  552. {
  553. moveTo(v);
  554. redrawSlider();
  555. }
  556. }
  557. void CSlider::redrawSlider()
  558. {
  559. //slider->show(screenBuf);
  560. }
  561. void CSlider::moveLeft()
  562. {
  563. moveTo(value-1);
  564. }
  565. void CSlider::moveRight()
  566. {
  567. moveTo(value+1);
  568. }
  569. void CSlider::moveTo(int to)
  570. {
  571. vstd::amax(to, 0);
  572. vstd::amin(to, positions);
  573. //same, old position?
  574. if(value == to)
  575. return;
  576. value = to;
  577. if(horizontal)
  578. {
  579. if(positions)
  580. {
  581. double part = static_cast<double>(to) / positions;
  582. part*=(pos.w-48);
  583. int newPos = part + pos.x + 16 - slider->pos.x;
  584. slider->moveBy(Point(newPos, 0));
  585. }
  586. else
  587. slider->moveTo(Point(pos.x+16, pos.y));
  588. }
  589. else
  590. {
  591. if(positions)
  592. {
  593. double part = static_cast<double>(to) / positions;
  594. part*=(pos.h-48);
  595. int newPos = part + pos.y + 16 - slider->pos.y;
  596. slider->moveBy(Point(0, newPos));
  597. }
  598. else
  599. slider->moveTo(Point(pos.x, pos.y+16));
  600. }
  601. if(moved)
  602. moved(to);
  603. }
  604. void CSlider::clickLeft(tribool down, bool previousState)
  605. {
  606. if(down && !slider->isBlocked())
  607. {
  608. double pw = 0;
  609. double rw = 0;
  610. if(horizontal)
  611. {
  612. pw = GH.current->motion.x-pos.x-25;
  613. rw = pw / static_cast<double>(pos.w - 48);
  614. }
  615. else
  616. {
  617. pw = GH.current->motion.y-pos.y-24;
  618. rw = pw / (pos.h-48);
  619. }
  620. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  621. return;
  622. // if (rw>1) return;
  623. // if (rw<0) return;
  624. slider->clickLeft(true, slider->pressedL);
  625. moveTo(rw * positions + 0.5);
  626. return;
  627. }
  628. if(active & MOVE)
  629. removeUsedEvents(MOVE);
  630. }
  631. CSlider::~CSlider()
  632. {
  633. }
  634. CSlider::CSlider(int x, int y, int totalw, std::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style)
  635. :capacity(Capacity),amount(Amount),horizontal(Horizontal), moved(Moved)
  636. {
  637. OBJ_CONSTRUCTION_CAPTURING_ALL;
  638. setAmount(amount);
  639. addUsedEvents(LCLICK | KEYBOARD | WHEEL);
  640. strongInterest = true;
  641. left = new CAdventureMapButton();
  642. right = new CAdventureMapButton();
  643. slider = new CAdventureMapButton();
  644. pos.x += x;
  645. pos.y += y;
  646. if(horizontal)
  647. {
  648. left->pos.y = slider->pos.y = right->pos.y = pos.y;
  649. left->pos.x = pos.x;
  650. right->pos.x = pos.x + totalw - 16;
  651. }
  652. else
  653. {
  654. left->pos.x = slider->pos.x = right->pos.x = pos.x;
  655. left->pos.y = pos.y;
  656. right->pos.y = pos.y + totalw - 16;
  657. }
  658. left->callback = boost::bind(&CSlider::moveLeft,this);
  659. right->callback = boost::bind(&CSlider::moveRight,this);
  660. slider->callback = boost::bind(&CSlider::sliderClicked,this);
  661. left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
  662. if(horizontal)
  663. {
  664. pos.h = 16;
  665. pos.w = totalw;
  666. }
  667. else
  668. {
  669. pos.w = 16;
  670. pos.h = totalw;
  671. }
  672. if(style == 0)
  673. {
  674. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  675. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  676. //use source def to create custom animations. Format "name.def:123" will load this frame from def file
  677. auto animLeft = new CAnimation();
  678. animLeft->setCustom(name + ":0", 0);
  679. animLeft->setCustom(name + ":1", 1);
  680. left->setImage(animLeft);
  681. auto animRight = new CAnimation();
  682. animRight->setCustom(name + ":2", 0);
  683. animRight->setCustom(name + ":3", 1);
  684. right->setImage(animRight);
  685. auto animSlider = new CAnimation();
  686. animSlider->setCustom(name + ":4", 0);
  687. slider->setImage(animSlider);
  688. }
  689. else
  690. {
  691. left->setImage(new CAnimation(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF"));
  692. right->setImage(new CAnimation(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF"));
  693. slider->setImage(new CAnimation("SCNRBSL.DEF"));
  694. }
  695. slider->actOnDown = true;
  696. slider->soundDisabled = true;
  697. left->soundDisabled = true;
  698. right->soundDisabled = true;
  699. value = -1;
  700. moveTo(Value);
  701. }
  702. void CSlider::block( bool on )
  703. {
  704. left->block(on);
  705. right->block(on);
  706. slider->block(on);
  707. }
  708. void CSlider::setAmount( int to )
  709. {
  710. amount = to;
  711. positions = to - capacity;
  712. vstd::amax(positions, 0);
  713. }
  714. void CSlider::showAll(SDL_Surface * to)
  715. {
  716. CSDL_Ext::fillRect(to, &pos, 0);
  717. CIntObject::showAll(to);
  718. }
  719. void CSlider::wheelScrolled(bool down, bool in)
  720. {
  721. moveTo(value + 3 * (down ? +1 : -1));
  722. }
  723. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  724. {
  725. if(key.state != SDL_PRESSED) return;
  726. int moveDest = 0;
  727. switch(key.keysym.sym)
  728. {
  729. case SDLK_UP:
  730. case SDLK_LEFT:
  731. moveDest = value - 1;
  732. break;
  733. case SDLK_DOWN:
  734. case SDLK_RIGHT:
  735. moveDest = value + 1;
  736. break;
  737. case SDLK_PAGEUP:
  738. moveDest = value - capacity + 1;
  739. break;
  740. case SDLK_PAGEDOWN:
  741. moveDest = value + capacity - 1;
  742. break;
  743. case SDLK_HOME:
  744. moveDest = 0;
  745. break;
  746. case SDLK_END:
  747. moveDest = amount - capacity;
  748. break;
  749. default:
  750. return;
  751. }
  752. moveTo(moveDest);
  753. }
  754. void CSlider::moveToMax()
  755. {
  756. moveTo(amount);
  757. }
  758. static void intDeleter(CIntObject* object)
  759. {
  760. delete object;
  761. }
  762. CObjectList::CObjectList(CreateFunc create, DestroyFunc destroy):
  763. createObject(create),
  764. destroyObject(destroy)
  765. {
  766. if (!destroyObject)
  767. destroyObject = intDeleter;
  768. }
  769. void CObjectList::deleteItem(CIntObject* item)
  770. {
  771. if (!item)
  772. return;
  773. removeChild(item);
  774. destroyObject(item);
  775. }
  776. CIntObject* CObjectList::createItem(size_t index)
  777. {
  778. OBJ_CONSTRUCTION_CAPTURING_ALL;
  779. CIntObject * item = createObject(index);
  780. if (item == nullptr)
  781. item = new CIntObject();
  782. item->recActions = defActions;
  783. addChild(item);
  784. return item;
  785. }
  786. CTabbedInt::CTabbedInt(CreateFunc create, DestroyFunc destroy, Point position, size_t ActiveID):
  787. CObjectList(create, destroy),
  788. activeTab(nullptr),
  789. activeID(ActiveID)
  790. {
  791. pos += position;
  792. reset();
  793. }
  794. void CTabbedInt::setActive(size_t which)
  795. {
  796. if (which != activeID)
  797. {
  798. activeID = which;
  799. reset();
  800. }
  801. }
  802. void CTabbedInt::reset()
  803. {
  804. deleteItem(activeTab);
  805. activeTab = createItem(activeID);
  806. activeTab->moveTo(pos.topLeft());
  807. if (active)
  808. redraw();
  809. }
  810. CIntObject * CTabbedInt::getItem()
  811. {
  812. return activeTab;
  813. }
  814. CListBox::CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  815. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos):
  816. CObjectList(create, destroy),
  817. first(InitialPos),
  818. totalSize(TotalSize),
  819. itemOffset(ItemOffset),
  820. slider(nullptr)
  821. {
  822. pos += Pos;
  823. items.resize(VisibleSize, nullptr);
  824. if (Slider & 1)
  825. {
  826. OBJ_CONSTRUCTION_CAPTURING_ALL;
  827. slider = new CSlider(SliderPos.x, SliderPos.y, SliderPos.w, boost::bind(&CListBox::moveToPos, this, _1),
  828. VisibleSize, TotalSize, InitialPos, Slider & 2, Slider & 4);
  829. }
  830. reset();
  831. }
  832. // Used to move active items after changing list position
  833. void CListBox::updatePositions()
  834. {
  835. Point itemPos = pos.topLeft();
  836. for (auto & elem : items)
  837. {
  838. (elem)->moveTo(itemPos);
  839. itemPos += itemOffset;
  840. }
  841. if (active)
  842. {
  843. redraw();
  844. if (slider)
  845. slider->moveTo(first);
  846. }
  847. }
  848. void CListBox::reset()
  849. {
  850. size_t current = first;
  851. for (auto & elem : items)
  852. {
  853. deleteItem(elem);
  854. elem = createItem(current++);
  855. }
  856. updatePositions();
  857. }
  858. void CListBox::resize(size_t newSize)
  859. {
  860. totalSize = newSize;
  861. if (slider)
  862. slider->setAmount(totalSize);
  863. reset();
  864. }
  865. size_t CListBox::size()
  866. {
  867. return totalSize;
  868. }
  869. CIntObject * CListBox::getItem(size_t which)
  870. {
  871. if (which < first || which > first + items.size() || which > totalSize)
  872. return nullptr;
  873. size_t i=first;
  874. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  875. if( i == which)
  876. return *iter;
  877. return nullptr;
  878. }
  879. size_t CListBox::getIndexOf(CIntObject *item)
  880. {
  881. size_t i=first;
  882. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  883. if(*iter == item)
  884. return i;
  885. return size_t(-1);
  886. }
  887. void CListBox::scrollTo(size_t which)
  888. {
  889. //scroll up
  890. if (first > which)
  891. moveToPos(which);
  892. //scroll down
  893. else if (first + items.size() <= which && which < totalSize)
  894. moveToPos(which - items.size() + 1);
  895. }
  896. void CListBox::moveToPos(size_t which)
  897. {
  898. //Calculate new position
  899. size_t maxPossible;
  900. if (totalSize > items.size())
  901. maxPossible = totalSize - items.size();
  902. else
  903. maxPossible = 0;
  904. size_t newPos = std::min(which, maxPossible);
  905. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  906. if (first - newPos == 1)
  907. moveToPrev();
  908. else if (newPos - first == 1)
  909. moveToNext();
  910. else if (newPos != first)
  911. {
  912. first = newPos;
  913. reset();
  914. }
  915. }
  916. void CListBox::moveToNext()
  917. {
  918. //Remove front item and insert new one to end
  919. if (first + items.size() < totalSize)
  920. {
  921. first++;
  922. deleteItem(items.front());
  923. items.pop_front();
  924. items.push_back(createItem(first+items.size()));
  925. updatePositions();
  926. }
  927. }
  928. void CListBox::moveToPrev()
  929. {
  930. //Remove last item and insert new one at start
  931. if (first)
  932. {
  933. first--;
  934. deleteItem(items.back());
  935. items.pop_back();
  936. items.push_front(createItem(first));
  937. updatePositions();
  938. }
  939. }
  940. size_t CListBox::getPos()
  941. {
  942. return first;
  943. }
  944. const std::list<CIntObject *> &CListBox::getItems()
  945. {
  946. return items;
  947. }
  948. void CSimpleWindow::show(SDL_Surface * to)
  949. {
  950. if(bitmap)
  951. blitAt(bitmap,pos.x,pos.y,to);
  952. }
  953. CSimpleWindow::~CSimpleWindow()
  954. {
  955. if (bitmap)
  956. {
  957. SDL_FreeSurface(bitmap);
  958. bitmap=nullptr;
  959. }
  960. }
  961. void CHoverableArea::hover (bool on)
  962. {
  963. if (on)
  964. GH.statusbar->print(hoverText);
  965. else if (GH.statusbar->getCurrent()==hoverText)
  966. GH.statusbar->clear();
  967. }
  968. CHoverableArea::CHoverableArea()
  969. {
  970. addUsedEvents(HOVER);
  971. }
  972. CHoverableArea::~CHoverableArea()
  973. {
  974. }
  975. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  976. {
  977. if(!down && previousState)
  978. {
  979. LOCPLINT->showInfoDialog(text);
  980. }
  981. }
  982. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  983. {
  984. adventureInt->handleRightClick(text, down);
  985. }
  986. LRClickableAreaWText::LRClickableAreaWText()
  987. {
  988. init();
  989. }
  990. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  991. {
  992. init();
  993. pos = Pos + pos;
  994. hoverText = HoverText;
  995. text = ClickText;
  996. }
  997. LRClickableAreaWText::~LRClickableAreaWText()
  998. {
  999. }
  1000. void LRClickableAreaWText::init()
  1001. {
  1002. addUsedEvents(LCLICK | RCLICK | HOVER);
  1003. }
  1004. void CLabel::showAll(SDL_Surface * to)
  1005. {
  1006. CIntObject::showAll(to);
  1007. std::string toPrint = visibleText();
  1008. if(!toPrint.length())
  1009. return;
  1010. //blitLine(to, pos.topLeft()/2 + pos.bottomRight()/2, toPrint);
  1011. blitLine(to, pos.topLeft() + textOffset, toPrint);
  1012. }
  1013. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::WHITE*/, const std::string &Text /*= ""*/)
  1014. :CTextContainer(Align, Font, Color), text(Text)
  1015. {
  1016. autoRedraw = true;
  1017. pos.x += x;
  1018. pos.y += y;
  1019. pos.w = pos.h = 0;
  1020. bg = nullptr;
  1021. ignoreLeadingWhitespace = false;
  1022. if (alignment == TOPLEFT) // causes issues for MIDDLE
  1023. {
  1024. pos.w = graphics->fonts[font]->getStringWidth(text.c_str());
  1025. pos.h = graphics->fonts[font]->getLineHeight();
  1026. }
  1027. }
  1028. std::string CLabel::visibleText()
  1029. {
  1030. std::string ret = text;
  1031. if(ignoreLeadingWhitespace)
  1032. boost::trim_left(ret);
  1033. return ret;
  1034. }
  1035. void CLabel::setTxt(const std::string &Txt)
  1036. {
  1037. text = Txt;
  1038. if(autoRedraw)
  1039. {
  1040. if(bg || !parent)
  1041. redraw();
  1042. else
  1043. parent->redraw();
  1044. }
  1045. }
  1046. void CBoundedLabel::setBounds(int limitW, int limitH)
  1047. {
  1048. pos.h = limitH;
  1049. pos.w = limitW;
  1050. recalculateLines(text);
  1051. }
  1052. void CBoundedLabel::setTxt(const std::string &Txt)
  1053. {
  1054. recalculateLines(Txt);
  1055. CLabel::setTxt(Txt);
  1056. }
  1057. void CTextContainer::blitLine(SDL_Surface *to, Point where, std::string what)
  1058. {
  1059. const IFont * f = graphics->fonts[font];
  1060. if (alignment == CENTER)
  1061. {
  1062. where.x -= f->getStringWidth(what) / 2;
  1063. where.y -= f->getLineHeight() / 2;
  1064. }
  1065. if (alignment == BOTTOMRIGHT)
  1066. {
  1067. where.x -= f->getStringWidth(what);
  1068. where.y -= f->getLineHeight();
  1069. }
  1070. size_t begin = 0;
  1071. size_t end;
  1072. std::string delimeters = "{}";
  1073. size_t currDelimeter = 0;
  1074. do
  1075. {
  1076. end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  1077. if (begin != end)
  1078. {
  1079. std::string toPrint = what.substr(begin, end-1);
  1080. if (currDelimeter % 2) // Enclosed in {} text - set to yellow
  1081. f->renderTextLeft(to, toPrint, Colors::YELLOW, where);
  1082. else // Non-enclosed text
  1083. f->renderTextLeft(to, toPrint, color, where);
  1084. begin = end;
  1085. where.x += f->getStringWidth(toPrint);
  1086. }
  1087. currDelimeter++;
  1088. }
  1089. while (begin++ != std::string::npos);
  1090. }
  1091. CTextContainer::CTextContainer(EAlignment alignment, EFonts font, SDL_Color color):
  1092. alignment(alignment),
  1093. font(font),
  1094. color(color)
  1095. {}
  1096. void CBoundedLabel::showAll(SDL_Surface * to)
  1097. {
  1098. CIntObject::showAll(to);
  1099. const IFont * f = graphics->fonts[font];
  1100. int lineHeight = f->getLineHeight();
  1101. int lineCapacity = pos.h / lineHeight;
  1102. int dy = f->getLineHeight(); //line height
  1103. int base_y = pos.y;
  1104. if(alignment == CENTER)
  1105. base_y += std::max((pos.h - maxH)/2,0);
  1106. for (int i = 0; i < lineCapacity; i++)
  1107. {
  1108. const std::string &line = lines[i];
  1109. if ( !(lines.size() && line.size())) //empty message or empty line
  1110. continue;
  1111. int x = pos.x;
  1112. if(alignment == CENTER)
  1113. {
  1114. x += pos.w - f->getStringWidth(line.c_str()) / 2;
  1115. }
  1116. blitLine(to, Point(x, base_y + i * dy), line);
  1117. }
  1118. }
  1119. void CBoundedLabel::recalculateLines(const std::string &Txt)
  1120. {
  1121. lines.clear();
  1122. const IFont * f = graphics->fonts[font];
  1123. int lineHeight = f->getLineHeight();
  1124. lines = CMessage::breakText(Txt, pos.w, font);
  1125. maxH = lineHeight * lines.size();
  1126. maxW = 0;
  1127. for(const std::string &line : lines)
  1128. vstd::amax(maxW, f->getStringWidth(line.c_str()));
  1129. }
  1130. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  1131. font(Font), align(Align), color(Color)
  1132. {}
  1133. void CLabelGroup::add(int x, int y, const std::string &text)
  1134. {
  1135. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1136. new CLabel(x, y, font, align, color, text);
  1137. }
  1138. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::WHITE*/)
  1139. :CBoundedLabel(rect.x, rect.y, Font, Align, Color, Text), sliderStyle(SliderStyle), slider(nullptr)
  1140. {
  1141. type |= REDRAW_PARENT;
  1142. autoRedraw = false;
  1143. pos.h = rect.h;
  1144. pos.w = rect.w;
  1145. assert(Align == TOPLEFT || Align == CENTER); //TODO: support for other alignments
  1146. assert(pos.w >= 40); //we need some space
  1147. setTxt(Text);
  1148. }
  1149. void CTextBox::recalculateLines(const std::string &Txt)
  1150. {
  1151. //TODO: merge with CBoundedlabel::recalculateLines
  1152. vstd::clear_pointer(slider);
  1153. lines.clear();
  1154. const IFont * f = graphics->fonts[font];
  1155. int lineHeight = f->getLineHeight();
  1156. int lineCapacity = pos.h / lineHeight;
  1157. lines = CMessage::breakText(Txt, pos.w, font);
  1158. if (lines.size() > lineCapacity) //we need to add a slider
  1159. {
  1160. lines = CMessage::breakText(Txt, pos.w - 32 - 10, font);
  1161. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1162. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1), lineCapacity, lines.size(), 0, false, sliderStyle);
  1163. if(active)
  1164. slider->activate();
  1165. }
  1166. maxH = lineHeight * lines.size();
  1167. maxW = 0;
  1168. for(const std::string &line : lines)
  1169. vstd::amax(maxW, f->getStringWidth(line));
  1170. }
  1171. void CTextBox::showAll(SDL_Surface * to)
  1172. {
  1173. CIntObject::showAll(to);
  1174. const IFont * f = graphics->fonts[font];
  1175. int dy = f->getLineHeight(); //line height
  1176. int base_y = pos.y;
  1177. if (alignment == CENTER)
  1178. base_y += std::max((pos.h - maxH)/2,0);
  1179. int howManyLinesToPrint = slider ? slider->capacity : lines.size();
  1180. int firstLineToPrint = slider ? slider->value : 0;
  1181. for (int i = 0; i < howManyLinesToPrint; i++)
  1182. {
  1183. const std::string &line = lines[i + firstLineToPrint];
  1184. if(!line.size()) continue;
  1185. int width = pos.w + (slider ? (slider->pos.w) : 0);
  1186. int x = pos.x + int(alignment) * width / 2;
  1187. blitLine(to, Point(x, base_y + i * dy), line);
  1188. }
  1189. }
  1190. void CTextBox::sliderMoved(int to)
  1191. {
  1192. if(!slider)
  1193. return;
  1194. redraw();
  1195. }
  1196. const Point CGStatusBar::edgeOffset = Point(5, 1);
  1197. void CGStatusBar::print(const std::string & Text)
  1198. {
  1199. if(!textLock)
  1200. setTxt(Text);
  1201. }
  1202. void CGStatusBar::clear()
  1203. {
  1204. if(!textLock)
  1205. setTxt("");
  1206. }
  1207. std::string CGStatusBar::getCurrent()
  1208. {
  1209. return text;
  1210. }
  1211. //CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::WHITE*/, const std::string &Text /*= ""*/)
  1212. //: CLabel(x, y, Font, Align, Color, Text)
  1213. //{
  1214. // init();
  1215. //}
  1216. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::WHITE*/)
  1217. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  1218. {
  1219. init();
  1220. bg = BG;
  1221. addChild(bg);
  1222. pos = bg->pos;
  1223. calcOffset();
  1224. textLock = false;
  1225. }
  1226. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  1227. : CLabel(x, y, FONT_SMALL, CENTER)
  1228. {
  1229. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1230. init();
  1231. bg = new CPicture(name);
  1232. pos = bg->pos;
  1233. if((unsigned int)maxw < pos.w)
  1234. {
  1235. vstd::amin(pos.w, maxw);
  1236. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  1237. }
  1238. calcOffset();
  1239. textLock = false;
  1240. }
  1241. CGStatusBar::~CGStatusBar()
  1242. {
  1243. GH.statusbar = oldStatusBar;
  1244. }
  1245. void CGStatusBar::show(SDL_Surface * to)
  1246. {
  1247. showAll(to);
  1248. }
  1249. void CGStatusBar::init()
  1250. {
  1251. oldStatusBar = GH.statusbar;
  1252. GH.statusbar = this;
  1253. }
  1254. void CGStatusBar::calcOffset()
  1255. {
  1256. switch(alignment)
  1257. {
  1258. case TOPLEFT:
  1259. textOffset = Point(edgeOffset.x, edgeOffset.y);
  1260. break;
  1261. case CENTER:
  1262. textOffset = Point(pos.w/2, pos.h/2);
  1263. break;
  1264. case BOTTOMRIGHT:
  1265. textOffset = Point(pos.w - edgeOffset.x, pos.h - edgeOffset.y);
  1266. break;
  1267. }
  1268. }
  1269. void CGStatusBar::lock(bool shouldLock)
  1270. {
  1271. textLock = shouldLock;
  1272. }
  1273. CTextInput::CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB):
  1274. CLabel(Pos.x, Pos.y, font, CENTER),
  1275. cb(CB)
  1276. {
  1277. type |= REDRAW_PARENT;
  1278. focus = false;
  1279. pos.h = Pos.h;
  1280. pos.w = Pos.w;
  1281. textOffset = Point(pos.w/2, pos.h/2);
  1282. captureAllKeys = true;
  1283. bg = nullptr;
  1284. addUsedEvents(LCLICK | KEYBOARD);
  1285. giveFocus();
  1286. }
  1287. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1288. :cb(CB)
  1289. {
  1290. focus = false;
  1291. pos += Pos;
  1292. captureAllKeys = true;
  1293. OBJ_CONSTRUCTION;
  1294. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1295. addUsedEvents(LCLICK | KEYBOARD);
  1296. giveFocus();
  1297. }
  1298. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  1299. {
  1300. focus = false;
  1301. pos += Pos;
  1302. captureAllKeys = true;
  1303. OBJ_CONSTRUCTION;
  1304. bg = new CPicture(Pos, 0, true);
  1305. Rect hlp = Pos;
  1306. if(srf)
  1307. CSDL_Ext::blitSurface(srf, &hlp, *bg, nullptr);
  1308. else
  1309. SDL_FillRect(*bg, nullptr, 0);
  1310. pos.w = bg->pos.w;
  1311. pos.h = bg->pos.h;
  1312. bg->pos = pos;
  1313. addUsedEvents(LCLICK | KEYBOARD);
  1314. giveFocus();
  1315. }
  1316. std::string CTextInput::visibleText()
  1317. {
  1318. return focus ? text + "_" : text;
  1319. }
  1320. void CTextInput::clickLeft( tribool down, bool previousState )
  1321. {
  1322. if(down && !focus)
  1323. giveFocus();
  1324. }
  1325. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1326. {
  1327. if(!focus || key.state != SDL_PRESSED)
  1328. return;
  1329. if(key.keysym.sym == SDLK_TAB)
  1330. {
  1331. moveFocus();
  1332. GH.breakEventHandling();
  1333. return;
  1334. }
  1335. std::string oldText = text;
  1336. switch(key.keysym.sym)
  1337. {
  1338. case SDLK_BACKSPACE:
  1339. if(!text.empty())
  1340. text.resize(text.size()-1);
  1341. break;
  1342. default:
  1343. if (key.keysym.unicode < ' ')
  1344. return;
  1345. else
  1346. text += key.keysym.unicode; //TODO 16-/>8
  1347. break;
  1348. }
  1349. filters(text, oldText);
  1350. if (text != oldText)
  1351. {
  1352. redraw();
  1353. cb(text);
  1354. }
  1355. }
  1356. void CTextInput::setTxt( const std::string &nText, bool callCb )
  1357. {
  1358. CLabel::setTxt(nText);
  1359. if(callCb)
  1360. cb(text);
  1361. }
  1362. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  1363. {
  1364. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER)
  1365. return false;
  1366. //this should allow all non-printable keys to go through (for example arrows)
  1367. if (key.keysym.unicode < ' ')
  1368. return false;
  1369. return true;
  1370. }
  1371. void CTextInput::filenameFilter(std::string & text, const std::string &)
  1372. {
  1373. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  1374. size_t pos;
  1375. while ((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  1376. text.erase(pos, 1);
  1377. }
  1378. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  1379. {
  1380. assert(minValue < maxValue);
  1381. if (text.empty())
  1382. text = "0";
  1383. size_t pos = 0;
  1384. if (text[0] == '-') //allow '-' sign as first symbol only
  1385. pos++;
  1386. while (pos < text.size())
  1387. {
  1388. if (text[pos] < '0' || text[pos] > '9')
  1389. {
  1390. text = oldText;
  1391. return; //new text is not number.
  1392. }
  1393. pos++;
  1394. }
  1395. try
  1396. {
  1397. int value = boost::lexical_cast<int>(text);
  1398. if (value < minValue)
  1399. text = boost::lexical_cast<std::string>(minValue);
  1400. else if (value > maxValue)
  1401. text = boost::lexical_cast<std::string>(maxValue);
  1402. }
  1403. catch(boost::bad_lexical_cast &)
  1404. {
  1405. //Should never happen. Unless I missed some cases
  1406. logGlobal->warnStream() << "Warning: failed to convert "<< text << " to number!";
  1407. text = oldText;
  1408. }
  1409. }
  1410. CFocusable::CFocusable()
  1411. {
  1412. focusables.push_back(this);
  1413. }
  1414. CFocusable::~CFocusable()
  1415. {
  1416. if(inputWithFocus == this)
  1417. inputWithFocus = nullptr;
  1418. focusables -= this;
  1419. }
  1420. void CFocusable::giveFocus()
  1421. {
  1422. if(inputWithFocus)
  1423. {
  1424. inputWithFocus->focus = false;
  1425. inputWithFocus->redraw();
  1426. }
  1427. focus = true;
  1428. inputWithFocus = this;
  1429. redraw();
  1430. }
  1431. void CFocusable::moveFocus()
  1432. {
  1433. auto i = vstd::find(focusables, this),
  1434. ourIt = i;
  1435. for(i++; i != ourIt; i++)
  1436. {
  1437. if(i == focusables.end())
  1438. i = focusables.begin();
  1439. if((*i)->active)
  1440. {
  1441. (*i)->giveFocus();
  1442. break;;
  1443. }
  1444. }
  1445. }
  1446. CWindowObject::CWindowObject(int options_, std::string imageName, Point centerAt):
  1447. CIntObject(getUsedEvents(options_), Point()),
  1448. shadow(nullptr),
  1449. options(options_),
  1450. background(createBg(imageName, options & PLAYER_COLORED))
  1451. {
  1452. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1453. if (options & RCLICK_POPUP)
  1454. CCS->curh->hide();
  1455. if (background)
  1456. pos = background->center(centerAt);
  1457. else
  1458. center(centerAt);
  1459. if (!(options & SHADOW_DISABLED))
  1460. setShadow(true);
  1461. }
  1462. CWindowObject::CWindowObject(int options_, std::string imageName):
  1463. CIntObject(getUsedEvents(options_), Point()),
  1464. shadow(nullptr),
  1465. options(options_),
  1466. background(createBg(imageName, options & PLAYER_COLORED))
  1467. {
  1468. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1469. if (options & RCLICK_POPUP)
  1470. CCS->curh->hide();
  1471. if (background)
  1472. pos = background->center();
  1473. else
  1474. center(Point(screen->w/2, screen->h/2));
  1475. if (!(options & SHADOW_DISABLED))
  1476. setShadow(true);
  1477. }
  1478. CWindowObject::~CWindowObject()
  1479. {
  1480. setShadow(false);
  1481. }
  1482. CPicture * CWindowObject::createBg(std::string imageName, bool playerColored)
  1483. {
  1484. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1485. if (imageName.empty())
  1486. return nullptr;
  1487. auto image = new CPicture(imageName);
  1488. if (playerColored)
  1489. image->colorize(LOCPLINT->playerID);
  1490. return image;
  1491. }
  1492. void CWindowObject::setBackground(std::string filename)
  1493. {
  1494. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1495. delete background;
  1496. background = createBg(filename, options & PLAYER_COLORED);
  1497. if (background)
  1498. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  1499. updateShadow();
  1500. }
  1501. int CWindowObject::getUsedEvents(int options)
  1502. {
  1503. if (options & RCLICK_POPUP)
  1504. return RCLICK;
  1505. return 0;
  1506. }
  1507. void CWindowObject::updateShadow()
  1508. {
  1509. setShadow(false);
  1510. if (!(options & SHADOW_DISABLED))
  1511. setShadow(true);
  1512. }
  1513. void CWindowObject::setShadow(bool on)
  1514. {
  1515. //size of shadow
  1516. static const int size = 8;
  1517. if (on == bool(shadow))
  1518. return;
  1519. vstd::clear_pointer(shadow);
  1520. //object too small to cast shadow
  1521. if (pos.h <= size || pos.w <= size)
  1522. return;
  1523. if (on)
  1524. {
  1525. //helper to set last row
  1526. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  1527. {
  1528. Uint8 * ptr = (Uint8*)surf->pixels + surf->pitch * (row);
  1529. for (size_t i=0; i< surf->w; i++)
  1530. {
  1531. Channels::px<4>::a.set(ptr, 128);
  1532. ptr+=4;
  1533. }
  1534. };
  1535. // helper to set last column
  1536. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  1537. {
  1538. Uint8 * ptr = (Uint8*)surf->pixels + 4 * (col);
  1539. for (size_t i=0; i< surf->h; i++)
  1540. {
  1541. Channels::px<4>::a.set(ptr, 128);
  1542. ptr+= surf->pitch;
  1543. }
  1544. };
  1545. static SDL_Surface * shadowCornerTempl = nullptr;
  1546. static SDL_Surface * shadowBottomTempl = nullptr;
  1547. static SDL_Surface * shadowRightTempl = nullptr;
  1548. //one-time initialization
  1549. if (!shadowCornerTempl)
  1550. {
  1551. //create "template" surfaces
  1552. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  1553. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  1554. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  1555. Uint32 shadowColor = SDL_MapRGBA(shadowCornerTempl->format, 0, 0, 0, 192);
  1556. //fill with shadow body color
  1557. SDL_FillRect(shadowCornerTempl, nullptr, shadowColor);
  1558. SDL_FillRect(shadowBottomTempl, nullptr, shadowColor);
  1559. SDL_FillRect(shadowRightTempl, nullptr, shadowColor);
  1560. //fill last row and column with more transparent color
  1561. blitAlphaCol(shadowRightTempl , size-1);
  1562. blitAlphaCol(shadowCornerTempl, size-1);
  1563. blitAlphaRow(shadowBottomTempl, size-1);
  1564. blitAlphaRow(shadowCornerTempl, size-1);
  1565. }
  1566. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1567. //FIXME: do something with this points
  1568. Point shadowStart;
  1569. if (options & BORDERED)
  1570. shadowStart = Point(size - 14, size - 14);
  1571. else
  1572. shadowStart = Point(size, size);
  1573. Point shadowPos;
  1574. if (options & BORDERED)
  1575. shadowPos = Point(pos.w + 14, pos.h + 14);
  1576. else
  1577. shadowPos = Point(pos.w, pos.h);
  1578. Point fullsize;
  1579. if (options & BORDERED)
  1580. fullsize = Point(pos.w + 28, pos.h + 29);
  1581. else
  1582. fullsize = Point(pos.w, pos.h);
  1583. //create base 8x8 piece of shadow
  1584. SDL_Surface * shadowCorner = CSDL_Ext::copySurface(shadowCornerTempl);
  1585. SDL_Surface * shadowBottom = CSDL_Ext::scaleSurfaceFast(shadowBottomTempl, fullsize.x - size, size);
  1586. SDL_Surface * shadowRight = CSDL_Ext::scaleSurfaceFast(shadowRightTempl, size, fullsize.y - size);
  1587. blitAlphaCol(shadowBottom, 0);
  1588. blitAlphaRow(shadowRight, 0);
  1589. //generate "shadow" object with these 3 pieces in it
  1590. shadow = new CIntObject;
  1591. shadow->addChild(new CPicture(shadowCorner, shadowPos.x, shadowPos.y));
  1592. shadow->addChild(new CPicture(shadowRight, shadowPos.x, shadowStart.y));
  1593. shadow->addChild(new CPicture(shadowBottom, shadowStart.x, shadowPos.y));
  1594. }
  1595. }
  1596. void CWindowObject::showAll(SDL_Surface *to)
  1597. {
  1598. CIntObject::showAll(to);
  1599. if ((options & BORDERED) && (pos.h != to->h || pos.w != to->w))
  1600. CMessage::drawBorder(LOCPLINT ? LOCPLINT->playerID : PlayerColor(1), to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  1601. }
  1602. void CWindowObject::close()
  1603. {
  1604. GH.popIntTotally(this);
  1605. }
  1606. void CWindowObject::clickRight(tribool down, bool previousState)
  1607. {
  1608. close();
  1609. CCS->curh->show();
  1610. }