2
0

CIntObjectClasses.cpp 44 KB

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