2
0

CIntObjectClasses.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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 CFunctionList2<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. size_t end;
  1102. std::string delimeters = "{}";
  1103. size_t currDelimeter = 0;
  1104. do
  1105. {
  1106. end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  1107. if (begin != end)
  1108. {
  1109. std::string toPrint = what.substr(begin, end-1);
  1110. if (currDelimeter % 2) // Enclosed in {} text - set to yellow
  1111. f->renderTextLeft(to, toPrint, Colors::YELLOW, where);
  1112. else // Non-enclosed text, use default color
  1113. f->renderTextLeft(to, toPrint, color, where);
  1114. begin = end;
  1115. destRect.x += f->getStringWidth(toPrint);
  1116. }
  1117. currDelimeter++;
  1118. }
  1119. while (begin++ != std::string::npos);
  1120. }
  1121. CTextContainer::CTextContainer(EAlignment alignment, EFonts font, SDL_Color color):
  1122. alignment(alignment),
  1123. font(font),
  1124. color(color)
  1125. {}
  1126. void CMultiLineLabel::showAll(SDL_Surface * to)
  1127. {
  1128. CIntObject::showAll(to);
  1129. const IFont * f = graphics->fonts[font];
  1130. // calculate which lines should be visible
  1131. int totalLines = lines.size();
  1132. int beginLine = visibleSize.y;
  1133. int endLine = getTextLocation().h + visibleSize.y;
  1134. if (beginLine < 0)
  1135. beginLine = 0;
  1136. else
  1137. beginLine /= f->getLineHeight();
  1138. if (endLine < 0)
  1139. endLine = 0;
  1140. else
  1141. endLine /= f->getLineHeight();
  1142. endLine++;
  1143. // and where they should be displayed
  1144. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * f->getLineHeight());
  1145. Point lineSize = Point(getTextLocation().w, f->getLineHeight());
  1146. CSDL_Ext::CClipRectGuard guard(to, getTextLocation()); // to properly trim text that is too big to fit
  1147. for (int i = beginLine; i < std::min(totalLines, endLine); i++)
  1148. {
  1149. if (!lines[i].empty()) //non-empty line
  1150. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  1151. lineStart.y += f->getLineHeight();
  1152. }
  1153. }
  1154. void CMultiLineLabel::splitText(const std::string &Txt)
  1155. {
  1156. lines.clear();
  1157. const IFont * f = graphics->fonts[font];
  1158. int lineHeight = f->getLineHeight();
  1159. lines = CMessage::breakText(Txt, pos.w, font);
  1160. textSize.y = lineHeight * lines.size();
  1161. textSize.x = 0;
  1162. for(const std::string &line : lines)
  1163. vstd::amax( textSize.x, f->getStringWidth(line.c_str()));
  1164. redraw();
  1165. }
  1166. Rect CMultiLineLabel::getTextLocation()
  1167. {
  1168. // this method is needed for vertical alignment alignment of text
  1169. // when height of available text is smaller than height of widget
  1170. // in this case - we should add proper offset to display text at required position
  1171. if (pos.h <= textSize.y)
  1172. return pos;
  1173. Point textSize(pos.w, graphics->fonts[font]->getLineHeight() * lines.size());
  1174. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  1175. switch(alignment)
  1176. {
  1177. case TOPLEFT: return Rect(pos.topLeft(), textSize);
  1178. case CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  1179. case BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  1180. }
  1181. assert(0);
  1182. return Rect();
  1183. }
  1184. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  1185. font(Font), align(Align), color(Color)
  1186. {}
  1187. void CLabelGroup::add(int x, int y, const std::string &text)
  1188. {
  1189. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1190. new CLabel(x, y, font, align, color, text);
  1191. }
  1192. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::WHITE*/):
  1193. sliderStyle(SliderStyle),
  1194. slider(nullptr)
  1195. {
  1196. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1197. label = new CMultiLineLabel(rect, Font, Align, Color);
  1198. type |= REDRAW_PARENT;
  1199. pos.h = rect.h;
  1200. pos.w = rect.w;
  1201. assert(pos.w >= 40); //we need some space
  1202. setText(Text);
  1203. }
  1204. void CTextBox::sliderMoved(int to)
  1205. {
  1206. label->scrollTextTo(to);
  1207. }
  1208. void CTextBox::resize(Point newSize)
  1209. {
  1210. pos.w = newSize.x;
  1211. pos.h = newSize.y;
  1212. label->pos.w = pos.w;
  1213. label->pos.h = pos.h;
  1214. if (slider)
  1215. vstd::clear_pointer(slider); // will be recreated if needed later
  1216. setText(label->getText()); // force refresh
  1217. }
  1218. void CTextBox::setText(const std::string &text)
  1219. {
  1220. label->setText(text);
  1221. if (label->textSize.y <= label->pos.h && slider)
  1222. {
  1223. // slider is no longer needed
  1224. vstd::clear_pointer(slider);
  1225. label->pos.w = pos.w;
  1226. label->setText(text);
  1227. }
  1228. else if (label->textSize.y > label->pos.h && !slider)
  1229. {
  1230. // create slider and update widget
  1231. label->pos.w = pos.w - 32;
  1232. label->setText(text);
  1233. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1234. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1),
  1235. label->pos.h, label->textSize.y, 0, false, sliderStyle);
  1236. slider->scrollStep = graphics->fonts[label->font]->getLineHeight();
  1237. }
  1238. }
  1239. void CGStatusBar::setText(const std::string & Text)
  1240. {
  1241. if(!textLock)
  1242. CLabel::setText(Text);
  1243. }
  1244. void CGStatusBar::clear()
  1245. {
  1246. setText("");
  1247. }
  1248. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::WHITE*/)
  1249. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  1250. {
  1251. init();
  1252. bg = BG;
  1253. addChild(bg);
  1254. pos = bg->pos;
  1255. getBorderSize();
  1256. textLock = false;
  1257. }
  1258. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  1259. : CLabel(x, y, FONT_SMALL, CENTER)
  1260. {
  1261. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1262. init();
  1263. bg = new CPicture(name);
  1264. pos = bg->pos;
  1265. if((unsigned int)maxw < pos.w)
  1266. {
  1267. vstd::amin(pos.w, maxw);
  1268. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  1269. }
  1270. textLock = false;
  1271. }
  1272. CGStatusBar::~CGStatusBar()
  1273. {
  1274. GH.statusbar = oldStatusBar;
  1275. }
  1276. void CGStatusBar::show(SDL_Surface * to)
  1277. {
  1278. showAll(to);
  1279. }
  1280. void CGStatusBar::init()
  1281. {
  1282. oldStatusBar = GH.statusbar;
  1283. GH.statusbar = this;
  1284. }
  1285. Point CGStatusBar::getBorderSize()
  1286. {
  1287. //Width of borders where text should not be printed
  1288. static const Point borderSize(5,1);
  1289. switch(alignment)
  1290. {
  1291. case TOPLEFT: return Point(borderSize.x, borderSize.y);
  1292. case CENTER: return Point(pos.w/2, pos.h/2);
  1293. case BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  1294. }
  1295. assert(0);
  1296. return Point();
  1297. }
  1298. void CGStatusBar::lock(bool shouldLock)
  1299. {
  1300. textLock = shouldLock;
  1301. }
  1302. CTextInput::CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB):
  1303. CLabel(Pos.x, Pos.y, font, CENTER),
  1304. cb(CB)
  1305. {
  1306. type |= REDRAW_PARENT;
  1307. focus = false;
  1308. pos.h = Pos.h;
  1309. pos.w = Pos.w;
  1310. captureAllKeys = true;
  1311. bg = nullptr;
  1312. addUsedEvents(LCLICK | KEYBOARD);
  1313. giveFocus();
  1314. }
  1315. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1316. :cb(CB)
  1317. {
  1318. focus = false;
  1319. pos += Pos;
  1320. captureAllKeys = true;
  1321. OBJ_CONSTRUCTION;
  1322. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1323. addUsedEvents(LCLICK | KEYBOARD);
  1324. giveFocus();
  1325. }
  1326. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  1327. {
  1328. focus = false;
  1329. pos += Pos;
  1330. captureAllKeys = true;
  1331. OBJ_CONSTRUCTION;
  1332. bg = new CPicture(Pos, 0, true);
  1333. Rect hlp = Pos;
  1334. if(srf)
  1335. CSDL_Ext::blitSurface(srf, &hlp, *bg, nullptr);
  1336. else
  1337. SDL_FillRect(*bg, nullptr, 0);
  1338. pos.w = bg->pos.w;
  1339. pos.h = bg->pos.h;
  1340. bg->pos = pos;
  1341. addUsedEvents(LCLICK | KEYBOARD);
  1342. giveFocus();
  1343. }
  1344. std::string CTextInput::visibleText()
  1345. {
  1346. return focus ? text + "_" : text;
  1347. }
  1348. void CTextInput::clickLeft( tribool down, bool previousState )
  1349. {
  1350. if(down && !focus)
  1351. giveFocus();
  1352. }
  1353. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1354. {
  1355. if(!focus || key.state != SDL_PRESSED)
  1356. return;
  1357. if(key.keysym.sym == SDLK_TAB)
  1358. {
  1359. moveFocus();
  1360. GH.breakEventHandling();
  1361. return;
  1362. }
  1363. std::string oldText = text;
  1364. switch(key.keysym.sym)
  1365. {
  1366. case SDLK_DELETE: // have index > ' ' so it won't be filtered out by default section
  1367. return;
  1368. case SDLK_BACKSPACE:
  1369. if(!text.empty())
  1370. text.resize(text.size()-1);
  1371. break;
  1372. default:
  1373. if (key.keysym.unicode < ' ')
  1374. return;
  1375. else
  1376. text += key.keysym.unicode; //TODO 16-/>8
  1377. break;
  1378. }
  1379. filters(text, oldText);
  1380. if (text != oldText)
  1381. {
  1382. redraw();
  1383. cb(text);
  1384. }
  1385. }
  1386. void CTextInput::setText( const std::string &nText, bool callCb )
  1387. {
  1388. CLabel::setText(nText);
  1389. if(callCb)
  1390. cb(text);
  1391. }
  1392. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  1393. {
  1394. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER)
  1395. return false;
  1396. //this should allow all non-printable keys to go through (for example arrows)
  1397. if (key.keysym.unicode < ' ')
  1398. return false;
  1399. return true;
  1400. }
  1401. void CTextInput::filenameFilter(std::string & text, const std::string &)
  1402. {
  1403. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  1404. size_t pos;
  1405. while ((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  1406. text.erase(pos, 1);
  1407. }
  1408. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  1409. {
  1410. assert(minValue < maxValue);
  1411. if (text.empty())
  1412. text = "0";
  1413. size_t pos = 0;
  1414. if (text[0] == '-') //allow '-' sign as first symbol only
  1415. pos++;
  1416. while (pos < text.size())
  1417. {
  1418. if (text[pos] < '0' || text[pos] > '9')
  1419. {
  1420. text = oldText;
  1421. return; //new text is not number.
  1422. }
  1423. pos++;
  1424. }
  1425. try
  1426. {
  1427. int value = boost::lexical_cast<int>(text);
  1428. if (value < minValue)
  1429. text = boost::lexical_cast<std::string>(minValue);
  1430. else if (value > maxValue)
  1431. text = boost::lexical_cast<std::string>(maxValue);
  1432. }
  1433. catch(boost::bad_lexical_cast &)
  1434. {
  1435. //Should never happen. Unless I missed some cases
  1436. logGlobal->warnStream() << "Warning: failed to convert "<< text << " to number!";
  1437. text = oldText;
  1438. }
  1439. }
  1440. CFocusable::CFocusable()
  1441. {
  1442. focusables.push_back(this);
  1443. }
  1444. CFocusable::~CFocusable()
  1445. {
  1446. if(inputWithFocus == this)
  1447. inputWithFocus = nullptr;
  1448. focusables -= this;
  1449. }
  1450. void CFocusable::giveFocus()
  1451. {
  1452. if(inputWithFocus)
  1453. {
  1454. inputWithFocus->focus = false;
  1455. inputWithFocus->redraw();
  1456. }
  1457. focus = true;
  1458. inputWithFocus = this;
  1459. redraw();
  1460. }
  1461. void CFocusable::moveFocus()
  1462. {
  1463. auto i = vstd::find(focusables, this),
  1464. ourIt = i;
  1465. for(i++; i != ourIt; i++)
  1466. {
  1467. if(i == focusables.end())
  1468. i = focusables.begin();
  1469. if((*i)->active)
  1470. {
  1471. (*i)->giveFocus();
  1472. break;;
  1473. }
  1474. }
  1475. }
  1476. CWindowObject::CWindowObject(int options_, std::string imageName, Point centerAt):
  1477. CIntObject(getUsedEvents(options_), Point()),
  1478. shadow(nullptr),
  1479. options(options_),
  1480. background(createBg(imageName, options & PLAYER_COLORED))
  1481. {
  1482. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1483. if (options & RCLICK_POPUP)
  1484. CCS->curh->hide();
  1485. if (background)
  1486. pos = background->center(centerAt);
  1487. else
  1488. center(centerAt);
  1489. if (!(options & SHADOW_DISABLED))
  1490. setShadow(true);
  1491. }
  1492. CWindowObject::CWindowObject(int options_, std::string imageName):
  1493. CIntObject(getUsedEvents(options_), Point()),
  1494. shadow(nullptr),
  1495. options(options_),
  1496. background(createBg(imageName, options & PLAYER_COLORED))
  1497. {
  1498. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1499. if (options & RCLICK_POPUP)
  1500. CCS->curh->hide();
  1501. if (background)
  1502. pos = background->center();
  1503. else
  1504. center(Point(screen->w/2, screen->h/2));
  1505. if (!(options & SHADOW_DISABLED))
  1506. setShadow(true);
  1507. }
  1508. CWindowObject::~CWindowObject()
  1509. {
  1510. setShadow(false);
  1511. }
  1512. CPicture * CWindowObject::createBg(std::string imageName, bool playerColored)
  1513. {
  1514. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1515. if (imageName.empty())
  1516. return nullptr;
  1517. auto image = new CPicture(imageName);
  1518. if (playerColored)
  1519. image->colorize(LOCPLINT->playerID);
  1520. return image;
  1521. }
  1522. void CWindowObject::setBackground(std::string filename)
  1523. {
  1524. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1525. delete background;
  1526. background = createBg(filename, options & PLAYER_COLORED);
  1527. if (background)
  1528. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  1529. updateShadow();
  1530. }
  1531. int CWindowObject::getUsedEvents(int options)
  1532. {
  1533. if (options & RCLICK_POPUP)
  1534. return RCLICK;
  1535. return 0;
  1536. }
  1537. void CWindowObject::updateShadow()
  1538. {
  1539. setShadow(false);
  1540. if (!(options & SHADOW_DISABLED))
  1541. setShadow(true);
  1542. }
  1543. void CWindowObject::setShadow(bool on)
  1544. {
  1545. //size of shadow
  1546. static const int size = 8;
  1547. if (on == bool(shadow))
  1548. return;
  1549. vstd::clear_pointer(shadow);
  1550. //object too small to cast shadow
  1551. if (pos.h <= size || pos.w <= size)
  1552. return;
  1553. if (on)
  1554. {
  1555. //helper to set last row
  1556. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  1557. {
  1558. Uint8 * ptr = (Uint8*)surf->pixels + surf->pitch * (row);
  1559. for (size_t i=0; i< surf->w; i++)
  1560. {
  1561. Channels::px<4>::a.set(ptr, 128);
  1562. ptr+=4;
  1563. }
  1564. };
  1565. // helper to set last column
  1566. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  1567. {
  1568. Uint8 * ptr = (Uint8*)surf->pixels + 4 * (col);
  1569. for (size_t i=0; i< surf->h; i++)
  1570. {
  1571. Channels::px<4>::a.set(ptr, 128);
  1572. ptr+= surf->pitch;
  1573. }
  1574. };
  1575. static SDL_Surface * shadowCornerTempl = nullptr;
  1576. static SDL_Surface * shadowBottomTempl = nullptr;
  1577. static SDL_Surface * shadowRightTempl = nullptr;
  1578. //one-time initialization
  1579. if (!shadowCornerTempl)
  1580. {
  1581. //create "template" surfaces
  1582. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  1583. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  1584. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  1585. Uint32 shadowColor = SDL_MapRGBA(shadowCornerTempl->format, 0, 0, 0, 192);
  1586. //fill with shadow body color
  1587. SDL_FillRect(shadowCornerTempl, nullptr, shadowColor);
  1588. SDL_FillRect(shadowBottomTempl, nullptr, shadowColor);
  1589. SDL_FillRect(shadowRightTempl, nullptr, shadowColor);
  1590. //fill last row and column with more transparent color
  1591. blitAlphaCol(shadowRightTempl , size-1);
  1592. blitAlphaCol(shadowCornerTempl, size-1);
  1593. blitAlphaRow(shadowBottomTempl, size-1);
  1594. blitAlphaRow(shadowCornerTempl, size-1);
  1595. }
  1596. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1597. //FIXME: do something with this points
  1598. Point shadowStart;
  1599. if (options & BORDERED)
  1600. shadowStart = Point(size - 14, size - 14);
  1601. else
  1602. shadowStart = Point(size, size);
  1603. Point shadowPos;
  1604. if (options & BORDERED)
  1605. shadowPos = Point(pos.w + 14, pos.h + 14);
  1606. else
  1607. shadowPos = Point(pos.w, pos.h);
  1608. Point fullsize;
  1609. if (options & BORDERED)
  1610. fullsize = Point(pos.w + 28, pos.h + 29);
  1611. else
  1612. fullsize = Point(pos.w, pos.h);
  1613. //create base 8x8 piece of shadow
  1614. SDL_Surface * shadowCorner = CSDL_Ext::copySurface(shadowCornerTempl);
  1615. SDL_Surface * shadowBottom = CSDL_Ext::scaleSurfaceFast(shadowBottomTempl, fullsize.x - size, size);
  1616. SDL_Surface * shadowRight = CSDL_Ext::scaleSurfaceFast(shadowRightTempl, size, fullsize.y - size);
  1617. blitAlphaCol(shadowBottom, 0);
  1618. blitAlphaRow(shadowRight, 0);
  1619. //generate "shadow" object with these 3 pieces in it
  1620. shadow = new CIntObject;
  1621. shadow->addChild(new CPicture(shadowCorner, shadowPos.x, shadowPos.y));
  1622. shadow->addChild(new CPicture(shadowRight, shadowPos.x, shadowStart.y));
  1623. shadow->addChild(new CPicture(shadowBottom, shadowStart.x, shadowPos.y));
  1624. }
  1625. }
  1626. void CWindowObject::showAll(SDL_Surface *to)
  1627. {
  1628. CIntObject::showAll(to);
  1629. if ((options & BORDERED) && (pos.h != to->h || pos.w != to->w))
  1630. CMessage::drawBorder(LOCPLINT ? LOCPLINT->playerID : PlayerColor(1), to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  1631. }
  1632. void CWindowObject::close()
  1633. {
  1634. GH.popIntTotally(this);
  1635. }
  1636. void CWindowObject::clickRight(tribool down, bool previousState)
  1637. {
  1638. close();
  1639. CCS->curh->show();
  1640. }