CIntObjectClasses.cpp 42 KB

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