CIntObjectClasses.cpp 40 KB

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