CIntObjectClasses.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  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 "../../lib/CConfigHandler.h"
  12. #include "../battle/CBattleInterface.h"
  13. #include "../battle/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 = nullptr;
  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, nullptr, color);
  143. freeSurf = true;
  144. }
  145. void CPicture::colorizeAndConvert(PlayerColor player)
  146. {
  147. assert(bg);
  148. colorize(player);
  149. convertToScreenBPP();
  150. }
  151. void CPicture::colorize(PlayerColor 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 = nullptr;
  179. text = nullptr;
  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 /*= nullptr*/, 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]) : nullptr);
  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->setText(*name);
  330. else if ( GH.statusbar->getText()==(*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 (auto & elem : *add)
  352. imageNames.push_back(elem);
  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(PlayerColor 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. borderEnabled = true;
  389. setState(HIGHLIGHTED);
  390. callback();
  391. }
  392. else
  393. {
  394. borderEnabled = false;
  395. setState(NORMAL);
  396. callback2();
  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 /*= nullptr*/, 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 /*= nullptr*/, 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, nullptr, x, y, key);
  456. if(musicLike)
  457. {
  458. bt->setOffset(buttons.size()-3);
  459. }
  460. bt->ID = uid;
  461. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  462. bt->onlyOn = true;
  463. buttons.push_back(bt);
  464. }
  465. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
  466. : onChange(OnChange), musicLike(musicLikeButtons)
  467. {}
  468. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  469. {
  470. }
  471. void CHighlightableButtonsGroup::select(int id, bool mode)
  472. {
  473. assert(!buttons.empty());
  474. CHighlightableButton *bt = buttons.front();
  475. if(mode)
  476. {
  477. for(auto btn : buttons)
  478. if (btn->ID == id)
  479. bt = btn;
  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(auto & elem : buttons)
  491. if(elem->ID!=to && elem->isHighlighted())
  492. elem->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(auto & elem : buttons)
  502. if(elem->isHighlighted())
  503. elem->show(to);
  504. }
  505. else
  506. CIntObject::show(to);
  507. }
  508. void CHighlightableButtonsGroup::showAll(SDL_Surface * to)
  509. {
  510. if (musicLike)
  511. {
  512. for(auto & elem : buttons)
  513. if(elem->isHighlighted())
  514. elem->showAll(to);
  515. }
  516. else
  517. CIntObject::showAll(to);
  518. }
  519. void CHighlightableButtonsGroup::block( ui8 on )
  520. {
  521. for(auto & elem : buttons)
  522. {
  523. elem->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, std::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style):
  635. capacity(Capacity),
  636. amount(Amount),
  637. scrollStep(1),
  638. horizontal(Horizontal),
  639. moved(Moved)
  640. {
  641. OBJ_CONSTRUCTION_CAPTURING_ALL;
  642. setAmount(amount);
  643. addUsedEvents(LCLICK | KEYBOARD | WHEEL);
  644. strongInterest = true;
  645. left = new CAdventureMapButton();
  646. right = new CAdventureMapButton();
  647. slider = new CAdventureMapButton();
  648. pos.x += x;
  649. pos.y += y;
  650. if(horizontal)
  651. {
  652. left->pos.y = slider->pos.y = right->pos.y = pos.y;
  653. left->pos.x = pos.x;
  654. right->pos.x = pos.x + totalw - 16;
  655. }
  656. else
  657. {
  658. left->pos.x = slider->pos.x = right->pos.x = pos.x;
  659. left->pos.y = pos.y;
  660. right->pos.y = pos.y + totalw - 16;
  661. }
  662. left->callback = boost::bind(&CSlider::moveLeft,this);
  663. right->callback = boost::bind(&CSlider::moveRight,this);
  664. slider->callback = boost::bind(&CSlider::sliderClicked,this);
  665. left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
  666. if(horizontal)
  667. {
  668. pos.h = 16;
  669. pos.w = totalw;
  670. }
  671. else
  672. {
  673. pos.w = 16;
  674. pos.h = totalw;
  675. }
  676. if(style == 0)
  677. {
  678. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  679. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  680. //use source def to create custom animations. Format "name.def:123" will load this frame from def file
  681. auto animLeft = new CAnimation();
  682. animLeft->setCustom(name + ":0", 0);
  683. animLeft->setCustom(name + ":1", 1);
  684. left->setImage(animLeft);
  685. auto animRight = new CAnimation();
  686. animRight->setCustom(name + ":2", 0);
  687. animRight->setCustom(name + ":3", 1);
  688. right->setImage(animRight);
  689. auto animSlider = new CAnimation();
  690. animSlider->setCustom(name + ":4", 0);
  691. slider->setImage(animSlider);
  692. }
  693. else
  694. {
  695. left->setImage(new CAnimation(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF"));
  696. right->setImage(new CAnimation(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF"));
  697. slider->setImage(new CAnimation("SCNRBSL.DEF"));
  698. }
  699. slider->actOnDown = true;
  700. slider->soundDisabled = true;
  701. left->soundDisabled = true;
  702. right->soundDisabled = true;
  703. value = -1;
  704. moveTo(Value);
  705. }
  706. void CSlider::block( bool on )
  707. {
  708. left->block(on);
  709. right->block(on);
  710. slider->block(on);
  711. }
  712. void CSlider::setAmount( int to )
  713. {
  714. amount = to;
  715. positions = to - capacity;
  716. vstd::amax(positions, 0);
  717. }
  718. void CSlider::showAll(SDL_Surface * to)
  719. {
  720. CSDL_Ext::fillRect(to, &pos, 0);
  721. CIntObject::showAll(to);
  722. }
  723. void CSlider::wheelScrolled(bool down, bool in)
  724. {
  725. moveTo(value + 3 * (down ? +scrollStep : -scrollStep));
  726. }
  727. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  728. {
  729. if(key.state != SDL_PRESSED) return;
  730. int moveDest = 0;
  731. switch(key.keysym.sym)
  732. {
  733. case SDLK_UP:
  734. case SDLK_LEFT:
  735. moveDest = value - scrollStep;
  736. break;
  737. case SDLK_DOWN:
  738. case SDLK_RIGHT:
  739. moveDest = value + scrollStep;
  740. break;
  741. case SDLK_PAGEUP:
  742. moveDest = value - capacity + scrollStep;
  743. break;
  744. case SDLK_PAGEDOWN:
  745. moveDest = value + capacity - scrollStep;
  746. break;
  747. case SDLK_HOME:
  748. moveDest = 0;
  749. break;
  750. case SDLK_END:
  751. moveDest = amount - capacity;
  752. break;
  753. default:
  754. return;
  755. }
  756. moveTo(moveDest);
  757. }
  758. void CSlider::moveToMax()
  759. {
  760. moveTo(amount);
  761. }
  762. static void intDeleter(CIntObject* object)
  763. {
  764. delete object;
  765. }
  766. CObjectList::CObjectList(CreateFunc create, DestroyFunc destroy):
  767. createObject(create),
  768. destroyObject(destroy)
  769. {
  770. if (!destroyObject)
  771. destroyObject = intDeleter;
  772. }
  773. void CObjectList::deleteItem(CIntObject* item)
  774. {
  775. if (!item)
  776. return;
  777. removeChild(item);
  778. destroyObject(item);
  779. }
  780. CIntObject* CObjectList::createItem(size_t index)
  781. {
  782. OBJ_CONSTRUCTION_CAPTURING_ALL;
  783. CIntObject * item = createObject(index);
  784. if (item == nullptr)
  785. item = new CIntObject();
  786. item->recActions = defActions;
  787. addChild(item);
  788. return item;
  789. }
  790. CTabbedInt::CTabbedInt(CreateFunc create, DestroyFunc destroy, Point position, size_t ActiveID):
  791. CObjectList(create, destroy),
  792. activeTab(nullptr),
  793. activeID(ActiveID)
  794. {
  795. pos += position;
  796. reset();
  797. }
  798. void CTabbedInt::setActive(size_t which)
  799. {
  800. if (which != activeID)
  801. {
  802. activeID = which;
  803. reset();
  804. }
  805. }
  806. void CTabbedInt::reset()
  807. {
  808. deleteItem(activeTab);
  809. activeTab = createItem(activeID);
  810. activeTab->moveTo(pos.topLeft());
  811. if (active)
  812. redraw();
  813. }
  814. CIntObject * CTabbedInt::getItem()
  815. {
  816. return activeTab;
  817. }
  818. CListBox::CListBox(CreateFunc create, DestroyFunc destroy, Point Pos, Point ItemOffset, size_t VisibleSize,
  819. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos):
  820. CObjectList(create, destroy),
  821. first(InitialPos),
  822. totalSize(TotalSize),
  823. itemOffset(ItemOffset),
  824. slider(nullptr)
  825. {
  826. pos += Pos;
  827. items.resize(VisibleSize, nullptr);
  828. if (Slider & 1)
  829. {
  830. OBJ_CONSTRUCTION_CAPTURING_ALL;
  831. slider = new CSlider(SliderPos.x, SliderPos.y, SliderPos.w, boost::bind(&CListBox::moveToPos, this, _1),
  832. VisibleSize, TotalSize, InitialPos, Slider & 2, Slider & 4);
  833. }
  834. reset();
  835. }
  836. // Used to move active items after changing list position
  837. void CListBox::updatePositions()
  838. {
  839. Point itemPos = pos.topLeft();
  840. for (auto & elem : items)
  841. {
  842. (elem)->moveTo(itemPos);
  843. itemPos += itemOffset;
  844. }
  845. if (active)
  846. {
  847. redraw();
  848. if (slider)
  849. slider->moveTo(first);
  850. }
  851. }
  852. void CListBox::reset()
  853. {
  854. size_t current = first;
  855. for (auto & elem : items)
  856. {
  857. deleteItem(elem);
  858. elem = createItem(current++);
  859. }
  860. updatePositions();
  861. }
  862. void CListBox::resize(size_t newSize)
  863. {
  864. totalSize = newSize;
  865. if (slider)
  866. slider->setAmount(totalSize);
  867. reset();
  868. }
  869. size_t CListBox::size()
  870. {
  871. return totalSize;
  872. }
  873. CIntObject * CListBox::getItem(size_t which)
  874. {
  875. if (which < first || which > first + items.size() || which > totalSize)
  876. return nullptr;
  877. size_t i=first;
  878. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  879. if( i == which)
  880. return *iter;
  881. return nullptr;
  882. }
  883. size_t CListBox::getIndexOf(CIntObject *item)
  884. {
  885. size_t i=first;
  886. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  887. if(*iter == item)
  888. return i;
  889. return size_t(-1);
  890. }
  891. void CListBox::scrollTo(size_t which)
  892. {
  893. //scroll up
  894. if (first > which)
  895. moveToPos(which);
  896. //scroll down
  897. else if (first + items.size() <= which && which < totalSize)
  898. moveToPos(which - items.size() + 1);
  899. }
  900. void CListBox::moveToPos(size_t which)
  901. {
  902. //Calculate new position
  903. size_t maxPossible;
  904. if (totalSize > items.size())
  905. maxPossible = totalSize - items.size();
  906. else
  907. maxPossible = 0;
  908. size_t newPos = std::min(which, maxPossible);
  909. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  910. if (first - newPos == 1)
  911. moveToPrev();
  912. else if (newPos - first == 1)
  913. moveToNext();
  914. else if (newPos != first)
  915. {
  916. first = newPos;
  917. reset();
  918. }
  919. }
  920. void CListBox::moveToNext()
  921. {
  922. //Remove front item and insert new one to end
  923. if (first + items.size() < totalSize)
  924. {
  925. first++;
  926. deleteItem(items.front());
  927. items.pop_front();
  928. items.push_back(createItem(first+items.size()));
  929. updatePositions();
  930. }
  931. }
  932. void CListBox::moveToPrev()
  933. {
  934. //Remove last item and insert new one at start
  935. if (first)
  936. {
  937. first--;
  938. deleteItem(items.back());
  939. items.pop_back();
  940. items.push_front(createItem(first));
  941. updatePositions();
  942. }
  943. }
  944. size_t CListBox::getPos()
  945. {
  946. return first;
  947. }
  948. const std::list<CIntObject *> &CListBox::getItems()
  949. {
  950. return items;
  951. }
  952. void CSimpleWindow::show(SDL_Surface * to)
  953. {
  954. if(bitmap)
  955. blitAt(bitmap,pos.x,pos.y,to);
  956. }
  957. CSimpleWindow::~CSimpleWindow()
  958. {
  959. if (bitmap)
  960. {
  961. SDL_FreeSurface(bitmap);
  962. bitmap=nullptr;
  963. }
  964. }
  965. void CHoverableArea::hover (bool on)
  966. {
  967. if (on)
  968. GH.statusbar->setText(hoverText);
  969. else if (GH.statusbar->getText()==hoverText)
  970. GH.statusbar->clear();
  971. }
  972. CHoverableArea::CHoverableArea()
  973. {
  974. addUsedEvents(HOVER);
  975. }
  976. CHoverableArea::~CHoverableArea()
  977. {
  978. }
  979. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  980. {
  981. if(!down && previousState)
  982. {
  983. LOCPLINT->showInfoDialog(text);
  984. }
  985. }
  986. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  987. {
  988. adventureInt->handleRightClick(text, down);
  989. }
  990. LRClickableAreaWText::LRClickableAreaWText()
  991. {
  992. init();
  993. }
  994. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  995. {
  996. init();
  997. pos = Pos + pos;
  998. hoverText = HoverText;
  999. text = ClickText;
  1000. }
  1001. LRClickableAreaWText::~LRClickableAreaWText()
  1002. {
  1003. }
  1004. void LRClickableAreaWText::init()
  1005. {
  1006. addUsedEvents(LCLICK | RCLICK | HOVER);
  1007. }
  1008. void CLabel::showAll(SDL_Surface * to)
  1009. {
  1010. CIntObject::showAll(to);
  1011. if(!text.empty())
  1012. blitLine(to, pos, text);
  1013. }
  1014. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::WHITE*/, const std::string &Text /*= ""*/)
  1015. :CTextContainer(Align, Font, Color), text(Text)
  1016. {
  1017. type |= REDRAW_PARENT;
  1018. autoRedraw = true;
  1019. pos.x += x;
  1020. pos.y += y;
  1021. pos.w = pos.h = 0;
  1022. bg = nullptr;
  1023. if (alignment == TOPLEFT) // causes issues for MIDDLE
  1024. {
  1025. pos.w = graphics->fonts[font]->getStringWidth(text.c_str());
  1026. pos.h = graphics->fonts[font]->getLineHeight();
  1027. }
  1028. }
  1029. Point CLabel::getBorderSize()
  1030. {
  1031. return Point(0, 0);
  1032. }
  1033. std::string CLabel::getText()
  1034. {
  1035. return text;
  1036. }
  1037. void CLabel::setText(const std::string &Txt)
  1038. {
  1039. text = Txt;
  1040. if(autoRedraw)
  1041. {
  1042. if(bg || !parent)
  1043. redraw();
  1044. else
  1045. parent->redraw();
  1046. }
  1047. }
  1048. CMultiLineLabel::CMultiLineLabel(int x, int y, EFonts Font, EAlignment Align, const SDL_Color &Color, const std::string &Text):
  1049. CLabel(x, y, Font, Align, Color, Text),
  1050. visibleSize(0, 0, 0, 0)
  1051. {
  1052. splitText(Text);
  1053. }
  1054. void CMultiLineLabel::setVisibleSize(Rect visibleSize)
  1055. {
  1056. this->visibleSize = visibleSize;
  1057. redraw();
  1058. }
  1059. void CMultiLineLabel::scrollTextBy(int distance)
  1060. {
  1061. scrollTextTo(visibleSize.y + distance);
  1062. }
  1063. void CMultiLineLabel::scrollTextTo(int distance)
  1064. {
  1065. Rect size = visibleSize;
  1066. size.y = distance;
  1067. setVisibleSize(size);
  1068. }
  1069. void CMultiLineLabel::setText(const std::string &Txt)
  1070. {
  1071. splitText(Txt);
  1072. CLabel::setText(Txt);
  1073. }
  1074. void CTextContainer::blitLine(SDL_Surface *to, Rect destRect, std::string what)
  1075. {
  1076. const IFont * f = graphics->fonts[font];
  1077. Point where = destRect.topLeft();
  1078. // input is rect in which given text should be placed
  1079. // calculate proper position for top-left corner of the text
  1080. if (alignment == TOPLEFT)
  1081. {
  1082. where.x += getBorderSize().x;
  1083. where.y += getBorderSize().y;
  1084. }
  1085. if (alignment == CENTER)
  1086. {
  1087. where.x += (destRect.w - f->getStringWidth(what)) / 2;
  1088. where.y += (destRect.h - f->getLineHeight()) / 2;
  1089. }
  1090. if (alignment == BOTTOMRIGHT)
  1091. {
  1092. where.x += getBorderSize().x + destRect.w - f->getStringWidth(what);
  1093. where.y += getBorderSize().y + destRect.h - f->getLineHeight();
  1094. }
  1095. size_t begin = 0;
  1096. size_t end;
  1097. std::string delimeters = "{}";
  1098. size_t currDelimeter = 0;
  1099. do
  1100. {
  1101. end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  1102. if (begin != end)
  1103. {
  1104. std::string toPrint = what.substr(begin, end-1);
  1105. if (currDelimeter % 2) // Enclosed in {} text - set to yellow
  1106. f->renderTextLeft(to, toPrint, Colors::YELLOW, where);
  1107. else // Non-enclosed text, use default color
  1108. f->renderTextLeft(to, toPrint, color, where);
  1109. begin = end;
  1110. destRect.x += f->getStringWidth(toPrint);
  1111. }
  1112. currDelimeter++;
  1113. }
  1114. while (begin++ != std::string::npos);
  1115. }
  1116. CTextContainer::CTextContainer(EAlignment alignment, EFonts font, SDL_Color color):
  1117. alignment(alignment),
  1118. font(font),
  1119. color(color)
  1120. {}
  1121. void CMultiLineLabel::showAll(SDL_Surface * to)
  1122. {
  1123. CIntObject::showAll(to);
  1124. const IFont * f = graphics->fonts[font];
  1125. // calculate which lines should be visible
  1126. size_t totalLines = lines.size();
  1127. size_t beginLine = visibleSize.y / f->getLineHeight();
  1128. size_t endLine = (getTextLocation().h + visibleSize.y) / f->getLineHeight() + 1;
  1129. // and where they should be displayed
  1130. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * f->getLineHeight());
  1131. Point lineSize = Point(getTextLocation().w, f->getLineHeight());
  1132. CSDL_Ext::CClipRectGuard guard(to, getTextLocation()); // to properly trim text that is too big to fit
  1133. for (size_t i = beginLine; i < std::min(totalLines, endLine); i++)
  1134. {
  1135. if (!lines[i].empty()) //non-empty line
  1136. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  1137. lineStart.y += f->getLineHeight();
  1138. }
  1139. }
  1140. void CMultiLineLabel::splitText(const std::string &Txt)
  1141. {
  1142. lines.clear();
  1143. const IFont * f = graphics->fonts[font];
  1144. int lineHeight = f->getLineHeight();
  1145. lines = CMessage::breakText(Txt, pos.w, font);
  1146. textSize.y = lineHeight * lines.size();
  1147. textSize.x = 0;
  1148. for(const std::string &line : lines)
  1149. vstd::amax( textSize.x, f->getStringWidth(line.c_str()));
  1150. redraw();
  1151. }
  1152. Rect CMultiLineLabel::getTextLocation()
  1153. {
  1154. // this method is needed for vertical alignment alignment of text
  1155. // when height of available text is smaller than height of widget
  1156. // in this case - we should add proper offset to display text at required position
  1157. if (pos.h <= textSize.y)
  1158. return pos;
  1159. Point textSize(pos.w, graphics->fonts[font]->getLineHeight() * lines.size());
  1160. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  1161. switch(alignment)
  1162. {
  1163. case TOPLEFT: return Rect(pos.topLeft(), textSize);
  1164. case CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  1165. case BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  1166. }
  1167. assert(0);
  1168. return Rect();
  1169. }
  1170. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  1171. font(Font), align(Align), color(Color)
  1172. {}
  1173. void CLabelGroup::add(int x, int y, const std::string &text)
  1174. {
  1175. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1176. new CLabel(x, y, font, align, color, text);
  1177. }
  1178. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::WHITE*/):
  1179. sliderStyle(SliderStyle),
  1180. slider(nullptr)
  1181. {
  1182. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1183. label = new CMultiLineLabel(rect.x, rect.y, Font, Align, Color);
  1184. type |= REDRAW_PARENT;
  1185. label->pos.h = pos.h = rect.h;
  1186. label->pos.w = pos.w = rect.w;
  1187. assert(pos.w >= 40); //we need some space
  1188. setText(Text);
  1189. }
  1190. void CTextBox::sliderMoved(int to)
  1191. {
  1192. label->scrollTextTo(to);
  1193. }
  1194. void CTextBox::resize(Point newSize)
  1195. {
  1196. pos.w = newSize.x;
  1197. pos.h = newSize.y;
  1198. label->pos.w = pos.w;
  1199. label->pos.h = pos.h;
  1200. if (slider)
  1201. vstd::clear_pointer(slider); // will be recreated if needed later
  1202. setText(label->getText()); // force refresh
  1203. }
  1204. void CTextBox::setText(const std::string &text)
  1205. {
  1206. label->setText(text);
  1207. if (label->textSize.y <= label->pos.h && slider)
  1208. {
  1209. // slider is no longer needed
  1210. vstd::clear_pointer(slider);
  1211. label->pos.w = pos.w;
  1212. label->setText(text);
  1213. }
  1214. else if (label->textSize.y > label->pos.h && !slider)
  1215. {
  1216. // create slider and update widget
  1217. label->pos.w = pos.w - 32;
  1218. label->setText(text);
  1219. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1220. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1),
  1221. label->pos.h, label->textSize.y, 0, false, sliderStyle);
  1222. slider->scrollStep = graphics->fonts[label->font]->getLineHeight();
  1223. }
  1224. }
  1225. void CGStatusBar::setText(const std::string & Text)
  1226. {
  1227. if(!textLock)
  1228. CLabel::setText(Text);
  1229. }
  1230. void CGStatusBar::clear()
  1231. {
  1232. setText("");
  1233. }
  1234. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::WHITE*/)
  1235. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  1236. {
  1237. init();
  1238. bg = BG;
  1239. addChild(bg);
  1240. pos = bg->pos;
  1241. getBorderSize();
  1242. textLock = false;
  1243. }
  1244. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  1245. : CLabel(x, y, FONT_SMALL, CENTER)
  1246. {
  1247. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1248. init();
  1249. bg = new CPicture(name);
  1250. pos = bg->pos;
  1251. if((unsigned int)maxw < pos.w)
  1252. {
  1253. vstd::amin(pos.w, maxw);
  1254. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  1255. }
  1256. textLock = false;
  1257. }
  1258. CGStatusBar::~CGStatusBar()
  1259. {
  1260. GH.statusbar = oldStatusBar;
  1261. }
  1262. void CGStatusBar::show(SDL_Surface * to)
  1263. {
  1264. showAll(to);
  1265. }
  1266. void CGStatusBar::init()
  1267. {
  1268. oldStatusBar = GH.statusbar;
  1269. GH.statusbar = this;
  1270. }
  1271. Point CGStatusBar::getBorderSize()
  1272. {
  1273. //Width of borders where text should not be printed
  1274. static const Point borderSize(5,1);
  1275. switch(alignment)
  1276. {
  1277. case TOPLEFT: return Point(borderSize.x, borderSize.y);
  1278. case CENTER: return Point(pos.w/2, pos.h/2);
  1279. case BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  1280. }
  1281. assert(0);
  1282. return Point();
  1283. }
  1284. void CGStatusBar::lock(bool shouldLock)
  1285. {
  1286. textLock = shouldLock;
  1287. }
  1288. CTextInput::CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB):
  1289. CLabel(Pos.x, Pos.y, font, CENTER),
  1290. cb(CB)
  1291. {
  1292. type |= REDRAW_PARENT;
  1293. focus = false;
  1294. pos.h = Pos.h;
  1295. pos.w = Pos.w;
  1296. captureAllKeys = true;
  1297. bg = nullptr;
  1298. addUsedEvents(LCLICK | KEYBOARD);
  1299. giveFocus();
  1300. }
  1301. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1302. :cb(CB)
  1303. {
  1304. focus = false;
  1305. pos += Pos;
  1306. captureAllKeys = true;
  1307. OBJ_CONSTRUCTION;
  1308. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1309. addUsedEvents(LCLICK | KEYBOARD);
  1310. giveFocus();
  1311. }
  1312. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  1313. {
  1314. focus = false;
  1315. pos += Pos;
  1316. captureAllKeys = true;
  1317. OBJ_CONSTRUCTION;
  1318. bg = new CPicture(Pos, 0, true);
  1319. Rect hlp = Pos;
  1320. if(srf)
  1321. CSDL_Ext::blitSurface(srf, &hlp, *bg, nullptr);
  1322. else
  1323. SDL_FillRect(*bg, nullptr, 0);
  1324. pos.w = bg->pos.w;
  1325. pos.h = bg->pos.h;
  1326. bg->pos = pos;
  1327. addUsedEvents(LCLICK | KEYBOARD);
  1328. giveFocus();
  1329. }
  1330. std::string CTextInput::visibleText()
  1331. {
  1332. return focus ? text + "_" : text;
  1333. }
  1334. void CTextInput::clickLeft( tribool down, bool previousState )
  1335. {
  1336. if(down && !focus)
  1337. giveFocus();
  1338. }
  1339. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1340. {
  1341. if(!focus || key.state != SDL_PRESSED)
  1342. return;
  1343. if(key.keysym.sym == SDLK_TAB)
  1344. {
  1345. moveFocus();
  1346. GH.breakEventHandling();
  1347. return;
  1348. }
  1349. std::string oldText = text;
  1350. switch(key.keysym.sym)
  1351. {
  1352. case SDLK_BACKSPACE:
  1353. if(!text.empty())
  1354. text.resize(text.size()-1);
  1355. break;
  1356. default:
  1357. if (key.keysym.unicode < ' ')
  1358. return;
  1359. else
  1360. text += key.keysym.unicode; //TODO 16-/>8
  1361. break;
  1362. }
  1363. filters(text, oldText);
  1364. if (text != oldText)
  1365. {
  1366. redraw();
  1367. cb(text);
  1368. }
  1369. }
  1370. void CTextInput::setText( const std::string &nText, bool callCb )
  1371. {
  1372. CLabel::setText(nText);
  1373. if(callCb)
  1374. cb(text);
  1375. }
  1376. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  1377. {
  1378. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER)
  1379. return false;
  1380. //this should allow all non-printable keys to go through (for example arrows)
  1381. if (key.keysym.unicode < ' ')
  1382. return false;
  1383. return true;
  1384. }
  1385. void CTextInput::filenameFilter(std::string & text, const std::string &)
  1386. {
  1387. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  1388. size_t pos;
  1389. while ((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  1390. text.erase(pos, 1);
  1391. }
  1392. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  1393. {
  1394. assert(minValue < maxValue);
  1395. if (text.empty())
  1396. text = "0";
  1397. size_t pos = 0;
  1398. if (text[0] == '-') //allow '-' sign as first symbol only
  1399. pos++;
  1400. while (pos < text.size())
  1401. {
  1402. if (text[pos] < '0' || text[pos] > '9')
  1403. {
  1404. text = oldText;
  1405. return; //new text is not number.
  1406. }
  1407. pos++;
  1408. }
  1409. try
  1410. {
  1411. int value = boost::lexical_cast<int>(text);
  1412. if (value < minValue)
  1413. text = boost::lexical_cast<std::string>(minValue);
  1414. else if (value > maxValue)
  1415. text = boost::lexical_cast<std::string>(maxValue);
  1416. }
  1417. catch(boost::bad_lexical_cast &)
  1418. {
  1419. //Should never happen. Unless I missed some cases
  1420. logGlobal->warnStream() << "Warning: failed to convert "<< text << " to number!";
  1421. text = oldText;
  1422. }
  1423. }
  1424. CFocusable::CFocusable()
  1425. {
  1426. focusables.push_back(this);
  1427. }
  1428. CFocusable::~CFocusable()
  1429. {
  1430. if(inputWithFocus == this)
  1431. inputWithFocus = nullptr;
  1432. focusables -= this;
  1433. }
  1434. void CFocusable::giveFocus()
  1435. {
  1436. if(inputWithFocus)
  1437. {
  1438. inputWithFocus->focus = false;
  1439. inputWithFocus->redraw();
  1440. }
  1441. focus = true;
  1442. inputWithFocus = this;
  1443. redraw();
  1444. }
  1445. void CFocusable::moveFocus()
  1446. {
  1447. auto i = vstd::find(focusables, this),
  1448. ourIt = i;
  1449. for(i++; i != ourIt; i++)
  1450. {
  1451. if(i == focusables.end())
  1452. i = focusables.begin();
  1453. if((*i)->active)
  1454. {
  1455. (*i)->giveFocus();
  1456. break;;
  1457. }
  1458. }
  1459. }
  1460. CWindowObject::CWindowObject(int options_, std::string imageName, Point centerAt):
  1461. CIntObject(getUsedEvents(options_), Point()),
  1462. shadow(nullptr),
  1463. options(options_),
  1464. background(createBg(imageName, options & PLAYER_COLORED))
  1465. {
  1466. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1467. if (options & RCLICK_POPUP)
  1468. CCS->curh->hide();
  1469. if (background)
  1470. pos = background->center(centerAt);
  1471. else
  1472. center(centerAt);
  1473. if (!(options & SHADOW_DISABLED))
  1474. setShadow(true);
  1475. }
  1476. CWindowObject::CWindowObject(int options_, std::string imageName):
  1477. CIntObject(getUsedEvents(options_), Point()),
  1478. shadow(nullptr),
  1479. options(options_),
  1480. background(createBg(imageName, options & PLAYER_COLORED))
  1481. {
  1482. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1483. if (options & RCLICK_POPUP)
  1484. CCS->curh->hide();
  1485. if (background)
  1486. pos = background->center();
  1487. else
  1488. center(Point(screen->w/2, screen->h/2));
  1489. if (!(options & SHADOW_DISABLED))
  1490. setShadow(true);
  1491. }
  1492. CWindowObject::~CWindowObject()
  1493. {
  1494. setShadow(false);
  1495. }
  1496. CPicture * CWindowObject::createBg(std::string imageName, bool playerColored)
  1497. {
  1498. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1499. if (imageName.empty())
  1500. return nullptr;
  1501. auto image = new CPicture(imageName);
  1502. if (playerColored)
  1503. image->colorize(LOCPLINT->playerID);
  1504. return image;
  1505. }
  1506. void CWindowObject::setBackground(std::string filename)
  1507. {
  1508. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1509. delete background;
  1510. background = createBg(filename, options & PLAYER_COLORED);
  1511. if (background)
  1512. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  1513. updateShadow();
  1514. }
  1515. int CWindowObject::getUsedEvents(int options)
  1516. {
  1517. if (options & RCLICK_POPUP)
  1518. return RCLICK;
  1519. return 0;
  1520. }
  1521. void CWindowObject::updateShadow()
  1522. {
  1523. setShadow(false);
  1524. if (!(options & SHADOW_DISABLED))
  1525. setShadow(true);
  1526. }
  1527. void CWindowObject::setShadow(bool on)
  1528. {
  1529. //size of shadow
  1530. static const int size = 8;
  1531. if (on == bool(shadow))
  1532. return;
  1533. vstd::clear_pointer(shadow);
  1534. //object too small to cast shadow
  1535. if (pos.h <= size || pos.w <= size)
  1536. return;
  1537. if (on)
  1538. {
  1539. //helper to set last row
  1540. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  1541. {
  1542. Uint8 * ptr = (Uint8*)surf->pixels + surf->pitch * (row);
  1543. for (size_t i=0; i< surf->w; i++)
  1544. {
  1545. Channels::px<4>::a.set(ptr, 128);
  1546. ptr+=4;
  1547. }
  1548. };
  1549. // helper to set last column
  1550. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  1551. {
  1552. Uint8 * ptr = (Uint8*)surf->pixels + 4 * (col);
  1553. for (size_t i=0; i< surf->h; i++)
  1554. {
  1555. Channels::px<4>::a.set(ptr, 128);
  1556. ptr+= surf->pitch;
  1557. }
  1558. };
  1559. static SDL_Surface * shadowCornerTempl = nullptr;
  1560. static SDL_Surface * shadowBottomTempl = nullptr;
  1561. static SDL_Surface * shadowRightTempl = nullptr;
  1562. //one-time initialization
  1563. if (!shadowCornerTempl)
  1564. {
  1565. //create "template" surfaces
  1566. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  1567. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  1568. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  1569. Uint32 shadowColor = SDL_MapRGBA(shadowCornerTempl->format, 0, 0, 0, 192);
  1570. //fill with shadow body color
  1571. SDL_FillRect(shadowCornerTempl, nullptr, shadowColor);
  1572. SDL_FillRect(shadowBottomTempl, nullptr, shadowColor);
  1573. SDL_FillRect(shadowRightTempl, nullptr, shadowColor);
  1574. //fill last row and column with more transparent color
  1575. blitAlphaCol(shadowRightTempl , size-1);
  1576. blitAlphaCol(shadowCornerTempl, size-1);
  1577. blitAlphaRow(shadowBottomTempl, size-1);
  1578. blitAlphaRow(shadowCornerTempl, size-1);
  1579. }
  1580. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1581. //FIXME: do something with this points
  1582. Point shadowStart;
  1583. if (options & BORDERED)
  1584. shadowStart = Point(size - 14, size - 14);
  1585. else
  1586. shadowStart = Point(size, size);
  1587. Point shadowPos;
  1588. if (options & BORDERED)
  1589. shadowPos = Point(pos.w + 14, pos.h + 14);
  1590. else
  1591. shadowPos = Point(pos.w, pos.h);
  1592. Point fullsize;
  1593. if (options & BORDERED)
  1594. fullsize = Point(pos.w + 28, pos.h + 29);
  1595. else
  1596. fullsize = Point(pos.w, pos.h);
  1597. //create base 8x8 piece of shadow
  1598. SDL_Surface * shadowCorner = CSDL_Ext::copySurface(shadowCornerTempl);
  1599. SDL_Surface * shadowBottom = CSDL_Ext::scaleSurfaceFast(shadowBottomTempl, fullsize.x - size, size);
  1600. SDL_Surface * shadowRight = CSDL_Ext::scaleSurfaceFast(shadowRightTempl, size, fullsize.y - size);
  1601. blitAlphaCol(shadowBottom, 0);
  1602. blitAlphaRow(shadowRight, 0);
  1603. //generate "shadow" object with these 3 pieces in it
  1604. shadow = new CIntObject;
  1605. shadow->addChild(new CPicture(shadowCorner, shadowPos.x, shadowPos.y));
  1606. shadow->addChild(new CPicture(shadowRight, shadowPos.x, shadowStart.y));
  1607. shadow->addChild(new CPicture(shadowBottom, shadowStart.x, shadowPos.y));
  1608. }
  1609. }
  1610. void CWindowObject::showAll(SDL_Surface *to)
  1611. {
  1612. CIntObject::showAll(to);
  1613. if ((options & BORDERED) && (pos.h != to->h || pos.w != to->w))
  1614. CMessage::drawBorder(LOCPLINT ? LOCPLINT->playerID : PlayerColor(1), to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  1615. }
  1616. void CWindowObject::close()
  1617. {
  1618. GH.popIntTotally(this);
  1619. }
  1620. void CWindowObject::clickRight(tribool down, bool previousState)
  1621. {
  1622. close();
  1623. CCS->curh->show();
  1624. }