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. if (buttons.size() > 3)
  459. bt->setOffset(buttons.size()-3);
  460. }
  461. bt->ID = uid;
  462. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  463. bt->onlyOn = true;
  464. buttons.push_back(bt);
  465. }
  466. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
  467. : onChange(OnChange), musicLike(musicLikeButtons)
  468. {}
  469. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  470. {
  471. }
  472. void CHighlightableButtonsGroup::select(int id, bool mode)
  473. {
  474. CHighlightableButton *bt = NULL;
  475. if(mode)
  476. {
  477. for(size_t i=0;i<buttons.size() && !bt; ++i)
  478. if (buttons[i]->ID == id)
  479. bt = buttons[i];
  480. }
  481. else
  482. {
  483. bt = buttons[id];
  484. }
  485. bt->select(true);
  486. selectionChanged(bt->ID);
  487. }
  488. void CHighlightableButtonsGroup::selectionChanged(int to)
  489. {
  490. for(size_t i=0;i<buttons.size(); ++i)
  491. if(buttons[i]->ID!=to && buttons[i]->isHighlighted())
  492. buttons[i]->select(false);
  493. onChange(to);
  494. if (parent)
  495. parent->redraw();
  496. }
  497. void CHighlightableButtonsGroup::show(SDL_Surface * to)
  498. {
  499. if (musicLike)
  500. {
  501. for(size_t i=0;i<buttons.size(); ++i)
  502. if(buttons[i]->isHighlighted())
  503. buttons[i]->show(to);
  504. }
  505. else
  506. CIntObject::show(to);
  507. }
  508. void CHighlightableButtonsGroup::showAll(SDL_Surface * to)
  509. {
  510. if (musicLike)
  511. {
  512. for(size_t i=0;i<buttons.size(); ++i)
  513. if(buttons[i]->isHighlighted())
  514. buttons[i]->showAll(to);
  515. }
  516. else
  517. CIntObject::showAll(to);
  518. }
  519. void CHighlightableButtonsGroup::block( ui8 on )
  520. {
  521. for(size_t i=0;i<buttons.size(); ++i)
  522. {
  523. buttons[i]->block(on);
  524. }
  525. }
  526. void CSlider::sliderClicked()
  527. {
  528. if(!(active & MOVE))
  529. addUsedEvents(MOVE);
  530. }
  531. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  532. {
  533. double v = 0;
  534. if(horizontal)
  535. {
  536. if( std::abs(sEvent.y-(pos.y+pos.h/2)) > pos.h/2+40 || std::abs(sEvent.x-(pos.x+pos.w/2)) > pos.w/2 )
  537. return;
  538. v = sEvent.x - pos.x - 24;
  539. v *= positions;
  540. v /= (pos.w - 48);
  541. }
  542. else
  543. {
  544. if(std::abs(sEvent.x-(pos.x+pos.w/2)) > pos.w/2+40 || std::abs(sEvent.y-(pos.y+pos.h/2)) > pos.h/2 )
  545. return;
  546. v = sEvent.y - pos.y - 24;
  547. v *= positions;
  548. v /= (pos.h - 48);
  549. }
  550. v += 0.5;
  551. if(v!=value)
  552. {
  553. moveTo(v);
  554. redrawSlider();
  555. }
  556. }
  557. void CSlider::redrawSlider()
  558. {
  559. //slider->show(screenBuf);
  560. }
  561. void CSlider::moveLeft()
  562. {
  563. moveTo(value-1);
  564. }
  565. void CSlider::moveRight()
  566. {
  567. moveTo(value+1);
  568. }
  569. void CSlider::moveTo(int to)
  570. {
  571. vstd::amax(to, 0);
  572. vstd::amin(to, positions);
  573. //same, old position?
  574. if(value == to)
  575. return;
  576. value = to;
  577. if(horizontal)
  578. {
  579. if(positions)
  580. {
  581. double part = static_cast<double>(to) / positions;
  582. part*=(pos.w-48);
  583. int newPos = part + pos.x + 16 - slider->pos.x;
  584. slider->moveBy(Point(newPos, 0));
  585. }
  586. else
  587. slider->moveTo(Point(pos.x+16, pos.y));
  588. }
  589. else
  590. {
  591. if(positions)
  592. {
  593. double part = static_cast<double>(to) / positions;
  594. part*=(pos.h-48);
  595. int newPos = part + pos.y + 16 - slider->pos.y;
  596. slider->moveBy(Point(0, newPos));
  597. }
  598. else
  599. slider->moveTo(Point(pos.x, pos.y+16));
  600. }
  601. if(moved)
  602. moved(to);
  603. }
  604. void CSlider::clickLeft(tribool down, bool previousState)
  605. {
  606. if(down && !slider->isBlocked())
  607. {
  608. double pw = 0;
  609. double rw = 0;
  610. if(horizontal)
  611. {
  612. pw = GH.current->motion.x-pos.x-25;
  613. rw = pw / static_cast<double>(pos.w - 48);
  614. }
  615. else
  616. {
  617. pw = GH.current->motion.y-pos.y-24;
  618. rw = pw / (pos.h-48);
  619. }
  620. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  621. return;
  622. // if (rw>1) return;
  623. // if (rw<0) return;
  624. slider->clickLeft(true, slider->pressedL);
  625. moveTo(rw * positions + 0.5);
  626. return;
  627. }
  628. if(active & MOVE)
  629. removeUsedEvents(MOVE);
  630. }
  631. CSlider::~CSlider()
  632. {
  633. }
  634. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style)
  635. :capacity(Capacity),amount(Amount),horizontal(Horizontal), moved(Moved)
  636. {
  637. OBJ_CONSTRUCTION_CAPTURING_ALL;
  638. setAmount(amount);
  639. addUsedEvents(LCLICK | KEYBOARD | WHEEL);
  640. strongInterest = true;
  641. left = new CAdventureMapButton();
  642. right = new CAdventureMapButton();
  643. slider = new CAdventureMapButton();
  644. pos.x += x;
  645. pos.y += y;
  646. if(horizontal)
  647. {
  648. left->pos.y = slider->pos.y = right->pos.y = pos.y;
  649. left->pos.x = pos.x;
  650. right->pos.x = pos.x + totalw - 16;
  651. }
  652. else
  653. {
  654. left->pos.x = slider->pos.x = right->pos.x = pos.x;
  655. left->pos.y = pos.y;
  656. right->pos.y = pos.y + totalw - 16;
  657. }
  658. left->callback = boost::bind(&CSlider::moveLeft,this);
  659. right->callback = boost::bind(&CSlider::moveRight,this);
  660. slider->callback = boost::bind(&CSlider::sliderClicked,this);
  661. left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
  662. if(horizontal)
  663. {
  664. pos.h = 16;
  665. pos.w = totalw;
  666. }
  667. else
  668. {
  669. pos.w = 16;
  670. pos.h = totalw;
  671. }
  672. if(style == 0)
  673. {
  674. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  675. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  676. //use source def to create custom animations. Format "name.def:123" will load this frame from def file
  677. CAnimation *animLeft = new CAnimation();
  678. animLeft->setCustom(name + ":0", 0);
  679. animLeft->setCustom(name + ":1", 1);
  680. left->setImage(animLeft);
  681. CAnimation *animRight = new CAnimation();
  682. animRight->setCustom(name + ":2", 0);
  683. animRight->setCustom(name + ":3", 1);
  684. right->setImage(animRight);
  685. CAnimation *animSlider = new CAnimation();
  686. animSlider->setCustom(name + ":4", 0);
  687. slider->setImage(animSlider);
  688. }
  689. else
  690. {
  691. left->setImage(new CAnimation(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF"));
  692. right->setImage(new CAnimation(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF"));
  693. slider->setImage(new CAnimation("SCNRBSL.DEF"));
  694. }
  695. slider->actOnDown = true;
  696. slider->soundDisabled = true;
  697. left->soundDisabled = true;
  698. right->soundDisabled = true;
  699. value = -1;
  700. moveTo(Value);
  701. }
  702. void CSlider::block( bool on )
  703. {
  704. left->block(on);
  705. right->block(on);
  706. slider->block(on);
  707. }
  708. void CSlider::setAmount( int to )
  709. {
  710. amount = to;
  711. positions = to - capacity;
  712. vstd::amax(positions, 0);
  713. }
  714. void CSlider::showAll(SDL_Surface * to)
  715. {
  716. CSDL_Ext::fillRect(to, &pos, 0);
  717. CIntObject::showAll(to);
  718. }
  719. void CSlider::wheelScrolled(bool down, bool in)
  720. {
  721. moveTo(value + 3 * (down ? +1 : -1));
  722. }
  723. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  724. {
  725. if(key.state != SDL_PRESSED) return;
  726. int moveDest = 0;
  727. switch(key.keysym.sym)
  728. {
  729. case SDLK_UP:
  730. case SDLK_LEFT:
  731. moveDest = value - 1;
  732. break;
  733. case SDLK_DOWN:
  734. case SDLK_RIGHT:
  735. moveDest = value + 1;
  736. break;
  737. case SDLK_PAGEUP:
  738. moveDest = value - capacity + 1;
  739. break;
  740. case SDLK_PAGEDOWN:
  741. moveDest = value + capacity - 1;
  742. break;
  743. case SDLK_HOME:
  744. moveDest = 0;
  745. break;
  746. case SDLK_END:
  747. moveDest = amount - capacity;
  748. break;
  749. default:
  750. return;
  751. }
  752. moveTo(moveDest);
  753. }
  754. void CSlider::moveToMax()
  755. {
  756. moveTo(amount);
  757. }
  758. static void intDeleter(CIntObject* object)
  759. {
  760. delete object;
  761. }
  762. CObjectList::CObjectList(CreateFunc create, DestroyFunc destroy):
  763. createObject(create),
  764. destroyObject(destroy)
  765. {
  766. if (!destroyObject)
  767. destroyObject = intDeleter;
  768. }
  769. void CObjectList::deleteItem(CIntObject* item)
  770. {
  771. if (!item)
  772. return;
  773. removeChild(item);
  774. destroyObject(item);
  775. }
  776. CIntObject* CObjectList::createItem(size_t index)
  777. {
  778. OBJ_CONSTRUCTION_CAPTURING_ALL;
  779. CIntObject * item = createObject(index);
  780. if (item == NULL)
  781. item = new CIntObject();
  782. item->recActions = defActions;
  783. addChild(item);
  784. return item;
  785. }
  786. CTabbedInt::CTabbedInt(CreateFunc create, DestroyFunc destroy, Point position, size_t ActiveID):
  787. CObjectList(create, destroy),
  788. activeTab(NULL),
  789. activeID(ActiveID)
  790. {
  791. pos += position;
  792. reset();
  793. }
  794. void CTabbedInt::setActive(size_t which)
  795. {
  796. if (which != activeID)
  797. {
  798. activeID = which;
  799. reset();
  800. }
  801. }
  802. void CTabbedInt::reset()
  803. {
  804. deleteItem(activeTab);
  805. activeTab = createItem(activeID);
  806. activeTab->moveTo(pos.topLeft());
  807. if (active)
  808. redraw();
  809. }
  810. CIntObject * CTabbedInt::getItem()
  811. {
  812. return activeTab;
  813. }
  814. CListBox::CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  815. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos):
  816. CObjectList(create, destroy),
  817. first(InitialPos),
  818. totalSize(TotalSize),
  819. itemOffset(ItemOffset),
  820. slider(nullptr)
  821. {
  822. pos += Pos;
  823. items.resize(VisibleSize, nullptr);
  824. if (Slider & 1)
  825. {
  826. OBJ_CONSTRUCTION_CAPTURING_ALL;
  827. slider = new CSlider(SliderPos.x, SliderPos.y, SliderPos.w, boost::bind(&CListBox::moveToPos, this, _1),
  828. VisibleSize, TotalSize, InitialPos, Slider & 2, Slider & 4);
  829. }
  830. reset();
  831. }
  832. // Used to move active items after changing list position
  833. void CListBox::updatePositions()
  834. {
  835. Point itemPos = pos.topLeft();
  836. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  837. {
  838. (*it)->moveTo(itemPos);
  839. itemPos += itemOffset;
  840. }
  841. if (active)
  842. {
  843. redraw();
  844. if (slider)
  845. slider->moveTo(first);
  846. }
  847. }
  848. void CListBox::reset()
  849. {
  850. size_t current = first;
  851. for (std::list<CIntObject*>::iterator it = items.begin(); it!=items.end(); it++)
  852. {
  853. deleteItem(*it);
  854. *it = createItem(current++);
  855. }
  856. updatePositions();
  857. }
  858. void CListBox::resize(size_t newSize)
  859. {
  860. totalSize = newSize;
  861. if (slider)
  862. slider->setAmount(totalSize);
  863. reset();
  864. }
  865. size_t CListBox::size()
  866. {
  867. return totalSize;
  868. }
  869. CIntObject * CListBox::getItem(size_t which)
  870. {
  871. if (which < first || which > first + items.size() || which > totalSize)
  872. return nullptr;
  873. size_t i=first;
  874. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  875. if( i == which)
  876. return *iter;
  877. return nullptr;
  878. }
  879. size_t CListBox::getIndexOf(CIntObject *item)
  880. {
  881. size_t i=first;
  882. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  883. if(*iter == item)
  884. return i;
  885. return size_t(-1);
  886. }
  887. void CListBox::scrollTo(size_t which)
  888. {
  889. //scroll up
  890. if (first > which)
  891. moveToPos(which);
  892. //scroll down
  893. else if (first + items.size() <= which && which < totalSize)
  894. moveToPos(which - items.size() + 1);
  895. }
  896. void CListBox::moveToPos(size_t which)
  897. {
  898. //Calculate new position
  899. size_t maxPossible;
  900. if (totalSize > items.size())
  901. maxPossible = totalSize - items.size();
  902. else
  903. maxPossible = 0;
  904. size_t newPos = std::min(which, maxPossible);
  905. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  906. if (first - newPos == 1)
  907. moveToPrev();
  908. else if (newPos - first == 1)
  909. moveToNext();
  910. else if (newPos != first)
  911. {
  912. first = newPos;
  913. reset();
  914. }
  915. }
  916. void CListBox::moveToNext()
  917. {
  918. //Remove front item and insert new one to end
  919. if (first + items.size() < totalSize)
  920. {
  921. first++;
  922. deleteItem(items.front());
  923. items.pop_front();
  924. items.push_back(createItem(first+items.size()));
  925. updatePositions();
  926. }
  927. }
  928. void CListBox::moveToPrev()
  929. {
  930. //Remove last item and insert new one at start
  931. if (first)
  932. {
  933. first--;
  934. deleteItem(items.back());
  935. items.pop_back();
  936. items.push_front(createItem(first));
  937. updatePositions();
  938. }
  939. }
  940. size_t CListBox::getPos()
  941. {
  942. return first;
  943. }
  944. const std::list<CIntObject *> &CListBox::getItems()
  945. {
  946. return items;
  947. }
  948. void CSimpleWindow::show(SDL_Surface * to)
  949. {
  950. if(bitmap)
  951. blitAt(bitmap,pos.x,pos.y,to);
  952. }
  953. CSimpleWindow::~CSimpleWindow()
  954. {
  955. if (bitmap)
  956. {
  957. SDL_FreeSurface(bitmap);
  958. bitmap=NULL;
  959. }
  960. }
  961. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  962. {
  963. bg=BitmapHandler::loadBitmap(name);
  964. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  965. pos.x += x;
  966. pos.y += y;
  967. if(maxw >= 0)
  968. pos.w = std::min(bg->w,maxw);
  969. else
  970. pos.w=bg->w;
  971. pos.h=bg->h;
  972. middlex=(pos.w/2)+pos.x;
  973. middley=(bg->h/2)+pos.y;
  974. }
  975. CStatusBar::~CStatusBar()
  976. {
  977. SDL_FreeSurface(bg);
  978. }
  979. void CStatusBar::clear()
  980. {
  981. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  982. {
  983. current="";
  984. redraw();
  985. }
  986. }
  987. void CStatusBar::print(const std::string & text)
  988. {
  989. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  990. {
  991. current=text;
  992. redraw();
  993. }
  994. }
  995. void CStatusBar::showAll(SDL_Surface * to)
  996. {
  997. show(to);
  998. }
  999. void CStatusBar::show(SDL_Surface * to)
  1000. {
  1001. SDL_Rect srcRect = genRect(pos.h,pos.w,0,0);
  1002. SDL_Rect dstRect = genRect(pos.h,pos.w,pos.x,pos.y);
  1003. CSDL_Ext::blitSurface(bg,&srcRect,to,&dstRect);
  1004. CSDL_Ext::printAtMiddle(current,middlex,middley,FONT_SMALL,Colors::Cornsilk,to);
  1005. }
  1006. std::string CStatusBar::getCurrent()
  1007. {
  1008. return current;
  1009. }
  1010. void CHoverableArea::hover (bool on)
  1011. {
  1012. if (on)
  1013. GH.statusbar->print(hoverText);
  1014. else if (GH.statusbar->getCurrent()==hoverText)
  1015. GH.statusbar->clear();
  1016. }
  1017. CHoverableArea::CHoverableArea()
  1018. {
  1019. addUsedEvents(HOVER);
  1020. }
  1021. CHoverableArea::~CHoverableArea()
  1022. {
  1023. }
  1024. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  1025. {
  1026. if(!down && previousState)
  1027. {
  1028. LOCPLINT->showInfoDialog(text);
  1029. }
  1030. }
  1031. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  1032. {
  1033. adventureInt->handleRightClick(text, down);
  1034. }
  1035. LRClickableAreaWText::LRClickableAreaWText()
  1036. {
  1037. init();
  1038. }
  1039. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  1040. {
  1041. init();
  1042. pos = Pos + pos;
  1043. hoverText = HoverText;
  1044. text = ClickText;
  1045. }
  1046. LRClickableAreaWText::~LRClickableAreaWText()
  1047. {
  1048. }
  1049. void LRClickableAreaWText::init()
  1050. {
  1051. addUsedEvents(LCLICK | RCLICK | HOVER);
  1052. }
  1053. void CLabel::showAll(SDL_Surface * to)
  1054. {
  1055. CIntObject::showAll(to);
  1056. std::string toPrint = visibleText();
  1057. if(!toPrint.length())
  1058. return;
  1059. 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
  1060. printer[alignment](toPrint, pos.x + textOffset.x, pos.y + textOffset.y, font, color, to);
  1061. }
  1062. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
  1063. :alignment(Align), font(Font), color(Color), text(Text)
  1064. {
  1065. autoRedraw = true;
  1066. pos.x += x;
  1067. pos.y += y;
  1068. pos.w = pos.h = 0;
  1069. bg = NULL;
  1070. ignoreLeadingWhitespace = false;
  1071. pos.w = graphics->fonts[font]->getWidth(text.c_str());
  1072. pos.h = graphics->fonts[font]->height;
  1073. }
  1074. std::string CLabel::visibleText()
  1075. {
  1076. std::string ret = text;
  1077. if(ignoreLeadingWhitespace)
  1078. boost::trim_left(ret);
  1079. return ret;
  1080. }
  1081. void CLabel::setTxt(const std::string &Txt)
  1082. {
  1083. text = Txt;
  1084. if(autoRedraw)
  1085. {
  1086. if(bg || !parent)
  1087. redraw();
  1088. else
  1089. parent->redraw();
  1090. }
  1091. }
  1092. void CBoundedLabel::setBounds(int limitW, int limitH)
  1093. {
  1094. pos.h = limitH;
  1095. pos.w = limitW;
  1096. recalculateLines(text);
  1097. }
  1098. void CBoundedLabel::setTxt(const std::string &Txt)
  1099. {
  1100. recalculateLines(Txt);
  1101. CLabel::setTxt(Txt);
  1102. }
  1103. void CBoundedLabel::showAll(SDL_Surface * to)
  1104. {
  1105. CIntObject::showAll(to);
  1106. const Font &f = *graphics->fonts[font];
  1107. int lineHeight = f.height;
  1108. int lineCapacity = pos.h / lineHeight;
  1109. int dy = f.height; //line height
  1110. int base_y = pos.y;
  1111. if(alignment == CENTER)
  1112. base_y += std::max((pos.h - maxH)/2,0);
  1113. for (int i = 0; i < lineCapacity; i++)
  1114. {
  1115. const std::string &line = lines[i];
  1116. if(!line.size()) 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. }