CIntObjectClasses.cpp 43 KB

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