2
0

CIntObjectClasses.cpp 42 KB

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