CIntObjectClasses.cpp 43 KB

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