CIntObjectClasses.cpp 42 KB

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