CIntObjectClasses.cpp 43 KB

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