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 "../CConfigHandler.h"
  12. #include "../BattleInterface/CBattleInterface.h"
  13. #include "../BattleInterface/CBattleInterfaceClasses.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../CMessage.h"
  16. #include "../CMusicHandler.h"
  17. #include "../GUIClasses.h"
  18. #include "CGuiHandler.h"
  19. #include "../CAdvmapInterface.h"
  20. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  21. {
  22. init();
  23. bg = BG;
  24. freeSurf = Free;
  25. pos.x += x;
  26. pos.y += y;
  27. pos.w = BG->w;
  28. pos.h = BG->h;
  29. }
  30. CPicture::CPicture( const std::string &bmpname, int x, int y )
  31. {
  32. init();
  33. bg = BitmapHandler::loadBitmap(bmpname);
  34. freeSurf = true;;
  35. pos.x += x;
  36. pos.y += y;
  37. if(bg)
  38. {
  39. pos.w = bg->w;
  40. pos.h = bg->h;
  41. }
  42. else
  43. {
  44. pos.w = pos.h = 0;
  45. }
  46. }
  47. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  48. {
  49. init();
  50. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  51. }
  52. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  53. {
  54. init();
  55. createSimpleRect(r, screenFormat, color);
  56. }
  57. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  58. {
  59. needRefresh = false;
  60. srcRect = new Rect(SrcRect);
  61. pos.x += x;
  62. pos.y += y;
  63. pos.w = srcRect->w;
  64. pos.h = srcRect->h;
  65. bg = BG;
  66. freeSurf = free;
  67. }
  68. void CPicture::setSurface(SDL_Surface *to)
  69. {
  70. bg = to;
  71. if (srcRect)
  72. {
  73. pos.w = srcRect->w;
  74. pos.h = srcRect->h;
  75. }
  76. else
  77. {
  78. pos.w = bg->w;
  79. pos.h = bg->h;
  80. }
  81. }
  82. CPicture::~CPicture()
  83. {
  84. if(freeSurf)
  85. SDL_FreeSurface(bg);
  86. delete srcRect;
  87. }
  88. void CPicture::init()
  89. {
  90. needRefresh = false;
  91. srcRect = NULL;
  92. }
  93. void CPicture::show(SDL_Surface * to)
  94. {
  95. if (needRefresh)
  96. showAll(to);
  97. }
  98. void CPicture::showAll(SDL_Surface * to)
  99. {
  100. if(bg)
  101. {
  102. if(srcRect)
  103. {
  104. SDL_Rect srcRectCpy = *srcRect;
  105. SDL_Rect dstRect = srcRectCpy;
  106. dstRect.x = pos.x;
  107. dstRect.y = pos.y;
  108. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  109. }
  110. else
  111. blitAt(bg, pos, to);
  112. }
  113. }
  114. void CPicture::convertToScreenBPP()
  115. {
  116. SDL_Surface *hlp = bg;
  117. bg = SDL_ConvertSurface(hlp,screen->format,0);
  118. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  119. SDL_FreeSurface(hlp);
  120. }
  121. void CPicture::setAlpha(int value)
  122. {
  123. SDL_SetAlpha(bg, SDL_SRCALPHA, value);
  124. }
  125. void CPicture::scaleTo(Point size)
  126. {
  127. SDL_Surface * scaled = CSDL_Ext::scaleSurface(bg, size.x, size.y);
  128. if(freeSurf)
  129. SDL_FreeSurface(bg);
  130. setSurface(scaled);
  131. freeSurf = false;
  132. }
  133. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  134. {
  135. pos += r;
  136. pos.w = r.w;
  137. pos.h = r.h;
  138. if(screenFormat)
  139. bg = CSDL_Ext::newSurface(r.w, r.h);
  140. else
  141. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  142. SDL_FillRect(bg, NULL, color);
  143. freeSurf = true;
  144. }
  145. void CPicture::colorizeAndConvert(int player)
  146. {
  147. assert(bg);
  148. colorize(player);
  149. convertToScreenBPP();
  150. }
  151. void CPicture::colorize(int player)
  152. {
  153. assert(bg);
  154. assert(bg->format->BitsPerPixel == 8);
  155. graphics->blueToPlayersAdv(bg, player);
  156. }
  157. CFilledTexture::CFilledTexture(std::string imageName, Rect position):
  158. CIntObject(0, position.topLeft()),
  159. texture(BitmapHandler::loadBitmap(imageName))
  160. {
  161. pos.w = position.w;
  162. pos.h = position.h;
  163. }
  164. CFilledTexture::~CFilledTexture()
  165. {
  166. SDL_FreeSurface(texture);
  167. }
  168. void CFilledTexture::showAll(SDL_Surface *to)
  169. {
  170. CSDL_Ext::CClipRectGuard guard(to, pos);
  171. CSDL_Ext::fillTexture(to, texture);
  172. }
  173. CButtonBase::CButtonBase()
  174. {
  175. swappedImages = keepFrame = false;
  176. bitmapOffset = 0;
  177. state=NORMAL;
  178. image = NULL;
  179. text = NULL;
  180. }
  181. CButtonBase::~CButtonBase()
  182. {
  183. }
  184. void CButtonBase::update()
  185. {
  186. if (text)
  187. {
  188. if (state == PRESSED)
  189. text->moveTo(Point(pos.x+pos.w/2+1, pos.y+pos.h/2+1));
  190. else
  191. text->moveTo(Point(pos.x+pos.w/2, pos.y+pos.h/2));
  192. }
  193. int newPos = (int)state + bitmapOffset;
  194. if (newPos < 0)
  195. newPos = 0;
  196. if (state == HIGHLIGHTED && image->size() < 4)
  197. newPos = image->size()-1;
  198. if (swappedImages)
  199. {
  200. if (newPos == 0) newPos = 1;
  201. else if (newPos == 1) newPos = 0;
  202. }
  203. if (!keepFrame)
  204. image->setFrame(newPos);
  205. if (active)
  206. redraw();
  207. }
  208. void CButtonBase::addTextOverlay( const std::string &Text, EFonts font, SDL_Color color)
  209. {
  210. OBJ_CONSTRUCTION_CAPTURING_ALL;
  211. delete text;
  212. text = new CLabel(pos.w/2, pos.h/2, font, CENTER, color, Text);
  213. update();
  214. }
  215. void CButtonBase::setOffset(int newOffset)
  216. {
  217. if (bitmapOffset == newOffset)
  218. return;
  219. bitmapOffset = newOffset;
  220. update();
  221. }
  222. void CButtonBase::setState(ButtonState newState)
  223. {
  224. if (state == newState)
  225. return;
  226. state = newState;
  227. update();
  228. }
  229. CButtonBase::ButtonState CButtonBase::getState()
  230. {
  231. return state;
  232. }
  233. bool CButtonBase::isBlocked()
  234. {
  235. return state == BLOCKED;
  236. }
  237. bool CButtonBase::isHighlighted()
  238. {
  239. return state == HIGHLIGHTED;
  240. }
  241. void CButtonBase::block(bool on)
  242. {
  243. setState(on?BLOCKED:NORMAL);
  244. }
  245. CAdventureMapButton::CAdventureMapButton ()
  246. {
  247. hoverable = actOnDown = borderEnabled = soundDisabled = false;
  248. borderColor.unused = 1; // represents a transparent color, used for HighlightableButton
  249. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  250. }
  251. CAdventureMapButton::CAdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName,int key, std::vector<std::string> * add, bool playerColoredButton )
  252. {
  253. std::map<int,std::string> pom;
  254. pom[0] = Name;
  255. init(Callback, pom, HelpBox, playerColoredButton, defName, add, x, y, key);
  256. }
  257. CAdventureMapButton::CAdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key/*=0*/ )
  258. {
  259. std::map<int,std::string> pom;
  260. pom[0] = Name;
  261. init(Callback, pom, HelpBox, info->playerColoured, info->defName, &info->additionalDefs, info->x, info->y, key);
  262. }
  263. CAdventureMapButton::CAdventureMapButton( const std::pair<std::string, std::string> &help, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
  264. {
  265. std::map<int,std::string> pom;
  266. pom[0] = help.first;
  267. init(Callback, pom, help.second, playerColoredButton, defName, add, x, y, key);
  268. }
  269. void CAdventureMapButton::clickLeft(tribool down, bool previousState)
  270. {
  271. if(isBlocked())
  272. return;
  273. if (down)
  274. {
  275. if (!soundDisabled)
  276. CCS->soundh->playSound(soundBase::button);
  277. setState(PRESSED);
  278. }
  279. else if(hoverable && hovered)
  280. setState(HIGHLIGHTED);
  281. else
  282. setState(NORMAL);
  283. if (actOnDown && down)
  284. {
  285. callback();
  286. }
  287. else if (!actOnDown && previousState && (down==false))
  288. {
  289. callback();
  290. }
  291. }
  292. void CAdventureMapButton::clickRight(tribool down, bool previousState)
  293. {
  294. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  295. CRClickPopup::createAndPush(helpBox);
  296. }
  297. void CAdventureMapButton::hover (bool on)
  298. {
  299. if(hoverable)
  300. {
  301. if(on)
  302. setState(HIGHLIGHTED);
  303. else
  304. setState(NORMAL);
  305. }
  306. if(pressedL && on)
  307. setState(PRESSED);
  308. std::string *name = (vstd::contains(hoverTexts,getState()))
  309. ? (&hoverTexts[getState()])
  310. : (vstd::contains(hoverTexts,0) ? (&hoverTexts[0]) : NULL);
  311. if(name && name->size() && !isBlocked()) //if there is no name, there is nohing to display also
  312. {
  313. if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
  314. {
  315. if(on && LOCPLINT->battleInt->console->alterTxt == "")
  316. {
  317. LOCPLINT->battleInt->console->alterTxt = *name;
  318. LOCPLINT->battleInt->console->whoSetAlter = 1;
  319. }
  320. else if (LOCPLINT->battleInt->console->alterTxt == *name)
  321. {
  322. LOCPLINT->battleInt->console->alterTxt = "";
  323. LOCPLINT->battleInt->console->whoSetAlter = 0;
  324. }
  325. }
  326. else if(GH.statusbar) //for other buttons
  327. {
  328. if (on)
  329. GH.statusbar->print(*name);
  330. else if ( GH.statusbar->getCurrent()==(*name) )
  331. GH.statusbar->clear();
  332. }
  333. }
  334. }
  335. void CAdventureMapButton::init(const CFunctionList<void()> &Callback, const std::map<int,std::string> &Name, const std::string &HelpBox, bool playerColoredButton, const std::string &defName, std::vector<std::string> * add, int x, int y, int key)
  336. {
  337. currentImage = -1;
  338. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  339. callback = Callback;
  340. hoverable = actOnDown = borderEnabled = soundDisabled = false;
  341. borderColor.unused = 1; // represents a transparent color, used for HighlightableButton
  342. hoverTexts = Name;
  343. helpBox=HelpBox;
  344. if (key != SDLK_UNKNOWN)
  345. assignedKeys.insert(key);
  346. pos.x += x;
  347. pos.y += y;
  348. if (!defName.empty())
  349. imageNames.push_back(defName);
  350. if (add)
  351. for (size_t i=0; i<add->size();i++ )
  352. imageNames.push_back(add->at(i));
  353. setIndex(0, playerColoredButton);
  354. }
  355. void CAdventureMapButton::setIndex(size_t index, bool playerColoredButton)
  356. {
  357. if (index == currentImage || index>=imageNames.size())
  358. return;
  359. currentImage = index;
  360. setImage(new CAnimation(imageNames[index]), playerColoredButton);
  361. }
  362. void CAdventureMapButton::setImage(CAnimation* anim, bool playerColoredButton, int animFlags)
  363. {
  364. OBJ_CONSTRUCTION_CAPTURING_ALL;
  365. delete image;
  366. image = new CAnimImage(anim, getState(), 0, 0, 0, animFlags);
  367. if (playerColoredButton)
  368. image->playerColored(LOCPLINT->playerID);
  369. pos.w = image->pos.w;
  370. pos.h = image->pos.h;
  371. }
  372. void CAdventureMapButton::setPlayerColor(int player)
  373. {
  374. if (image)
  375. image->playerColored(player);
  376. }
  377. void CAdventureMapButton::showAll(SDL_Surface * to)
  378. {
  379. CIntObject::showAll(to);
  380. if (borderEnabled && borderColor.unused == 0)
  381. CSDL_Ext::drawBorder(to, pos.x - 1, pos.y - 1, pos.w + 2, pos.h + 2, int3(borderColor.r, borderColor.g, borderColor.b));
  382. }
  383. void CHighlightableButton::select(bool on)
  384. {
  385. selected = on;
  386. if (on)
  387. {
  388. setState(HIGHLIGHTED);
  389. callback();
  390. borderEnabled = true;
  391. }
  392. else
  393. {
  394. setState(NORMAL);
  395. callback2();
  396. borderEnabled = false;
  397. }
  398. if(hoverTexts.size()>1)
  399. {
  400. hover(false);
  401. hover(true);
  402. }
  403. }
  404. void CHighlightableButton::clickLeft(tribool down, bool previousState)
  405. {
  406. if(isBlocked())
  407. return;
  408. if (down && !(onlyOn && isHighlighted()))
  409. {
  410. CCS->soundh->playSound(soundBase::button);
  411. setState(PRESSED);
  412. }
  413. if(previousState)//mouse up
  414. {
  415. if(down == false && getState() == PRESSED)
  416. select(!selected);
  417. else
  418. setState(selected?HIGHLIGHTED:NORMAL);
  419. }
  420. }
  421. CHighlightableButton::CHighlightableButton( const CFunctionList<void()> &onSelect, const CFunctionList<void()> &onDeselect, const std::map<int,std::string> &Name, const std::string &HelpBox, bool playerColoredButton, const std::string &defName, std::vector<std::string> * add, int x, int y, int key)
  422. : onlyOn(false), selected(false), callback2(onDeselect)
  423. {
  424. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  425. }
  426. CHighlightableButton::CHighlightableButton( const std::pair<std::string, std::string> &help, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
  427. : onlyOn(false), selected(false) // TODO: callback2(???)
  428. {
  429. ID = myid;
  430. std::map<int,std::string> pom;
  431. pom[0] = help.first;
  432. init(onSelect, pom, help.second, playerColoredButton, defName, add, x, y, key);
  433. }
  434. CHighlightableButton::CHighlightableButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
  435. : onlyOn(false), selected(false) // TODO: callback2(???)
  436. {
  437. ID = myid;
  438. std::map<int,std::string> pom;
  439. pom[0] = Name;
  440. init(onSelect, pom,HelpBox, playerColoredButton, defName, add, x, y, key);
  441. }
  442. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  443. {
  444. if (bt->parent)
  445. bt->parent->removeChild(bt);
  446. addChild(bt);
  447. bt->recActions = defActions;//FIXME: not needed?
  448. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  449. bt->onlyOn = true;
  450. buttons.push_back(bt);
  451. }
  452. void CHighlightableButtonsGroup::addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid, const CFunctionList<void()> &OnSelect, int key)
  453. {
  454. OBJ_CONSTRUCTION_CAPTURING_ALL;
  455. CHighlightableButton *bt = new CHighlightableButton(OnSelect, 0, tooltip, HelpBox, false, defName, 0, x, y, key);
  456. if(musicLike)
  457. {
  458. 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. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  961. {
  962. bg=BitmapHandler::loadBitmap(name);
  963. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  964. pos.x += x;
  965. pos.y += y;
  966. if(maxw >= 0)
  967. pos.w = std::min(bg->w,maxw);
  968. else
  969. pos.w=bg->w;
  970. pos.h=bg->h;
  971. middlex=(pos.w/2)+pos.x;
  972. middley=(bg->h/2)+pos.y;
  973. }
  974. CStatusBar::~CStatusBar()
  975. {
  976. SDL_FreeSurface(bg);
  977. }
  978. void CStatusBar::clear()
  979. {
  980. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  981. {
  982. current="";
  983. redraw();
  984. }
  985. }
  986. void CStatusBar::print(const std::string & text)
  987. {
  988. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  989. {
  990. current=text;
  991. redraw();
  992. }
  993. }
  994. void CStatusBar::showAll(SDL_Surface * to)
  995. {
  996. show(to);
  997. }
  998. void CStatusBar::show(SDL_Surface * to)
  999. {
  1000. SDL_Rect srcRect = genRect(pos.h,pos.w,0,0);
  1001. SDL_Rect dstRect = genRect(pos.h,pos.w,pos.x,pos.y);
  1002. CSDL_Ext::blitSurface(bg,&srcRect,to,&dstRect);
  1003. CSDL_Ext::printAtMiddle(current,middlex,middley,FONT_SMALL,Colors::Cornsilk,to);
  1004. }
  1005. std::string CStatusBar::getCurrent()
  1006. {
  1007. return current;
  1008. }
  1009. void CHoverableArea::hover (bool on)
  1010. {
  1011. if (on)
  1012. GH.statusbar->print(hoverText);
  1013. else if (GH.statusbar->getCurrent()==hoverText)
  1014. GH.statusbar->clear();
  1015. }
  1016. CHoverableArea::CHoverableArea()
  1017. {
  1018. addUsedEvents(HOVER);
  1019. }
  1020. CHoverableArea::~CHoverableArea()
  1021. {
  1022. }
  1023. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  1024. {
  1025. if(!down && previousState)
  1026. {
  1027. LOCPLINT->showInfoDialog(text);
  1028. }
  1029. }
  1030. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  1031. {
  1032. adventureInt->handleRightClick(text, down);
  1033. }
  1034. LRClickableAreaWText::LRClickableAreaWText()
  1035. {
  1036. init();
  1037. }
  1038. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  1039. {
  1040. init();
  1041. pos = Pos + pos;
  1042. hoverText = HoverText;
  1043. text = ClickText;
  1044. }
  1045. LRClickableAreaWText::~LRClickableAreaWText()
  1046. {
  1047. }
  1048. void LRClickableAreaWText::init()
  1049. {
  1050. addUsedEvents(LCLICK | RCLICK | HOVER);
  1051. }
  1052. void CLabel::showAll(SDL_Surface * to)
  1053. {
  1054. CIntObject::showAll(to);
  1055. std::string toPrint = visibleText();
  1056. if(!toPrint.length())
  1057. return;
  1058. static void (*printer[3])(const std::string &, int, int, EFonts, SDL_Color, SDL_Surface *) = {&CSDL_Ext::printAt, &CSDL_Ext::printAtMiddle, &CSDL_Ext::printTo}; //array of printing functions
  1059. printer[alignment](toPrint, pos.x + textOffset.x, pos.y + textOffset.y, font, color, to);
  1060. }
  1061. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
  1062. :alignment(Align), font(Font), color(Color), text(Text)
  1063. {
  1064. autoRedraw = true;
  1065. pos.x += x;
  1066. pos.y += y;
  1067. pos.w = pos.h = 0;
  1068. bg = NULL;
  1069. ignoreLeadingWhitespace = false;
  1070. pos.w = graphics->fonts[font]->getWidth(text.c_str());
  1071. pos.h = graphics->fonts[font]->height;
  1072. }
  1073. std::string CLabel::visibleText()
  1074. {
  1075. std::string ret = text;
  1076. if(ignoreLeadingWhitespace)
  1077. boost::trim_left(ret);
  1078. return ret;
  1079. }
  1080. void CLabel::setTxt(const std::string &Txt)
  1081. {
  1082. text = Txt;
  1083. if(autoRedraw)
  1084. {
  1085. if(bg || !parent)
  1086. redraw();
  1087. else
  1088. parent->redraw();
  1089. }
  1090. }
  1091. void CBoundedLabel::setBounds(int limitW, int limitH)
  1092. {
  1093. pos.h = limitH;
  1094. pos.w = limitW;
  1095. recalculateLines(text);
  1096. }
  1097. void CBoundedLabel::setTxt(const std::string &Txt)
  1098. {
  1099. recalculateLines(Txt);
  1100. CLabel::setTxt(Txt);
  1101. }
  1102. void CBoundedLabel::showAll(SDL_Surface * to)
  1103. {
  1104. CIntObject::showAll(to);
  1105. const Font &f = *graphics->fonts[font];
  1106. int lineHeight = f.height;
  1107. int lineCapacity = pos.h / lineHeight;
  1108. int dy = f.height; //line height
  1109. int base_y = pos.y;
  1110. if(alignment == CENTER)
  1111. base_y += std::max((pos.h - maxH)/2,0);
  1112. for (int i = 0; i < lineCapacity; i++)
  1113. {
  1114. const std::string &line = lines[i];
  1115. if(!line.size()) continue;
  1116. int x = pos.x;
  1117. if(alignment == CENTER)
  1118. {
  1119. x += (pos.w - f.getWidth(line.c_str())) / 2;
  1120. }
  1121. if(line[0] == '{' && line[line.size()-1] == '}')
  1122. CSDL_Ext::printAt(line, x, base_y + i*dy, font, Colors::Jasmine, to);
  1123. else
  1124. CSDL_Ext::printAt(line, x, base_y + i*dy, font, color, to);
  1125. }
  1126. }
  1127. void CBoundedLabel::recalculateLines(const std::string &Txt)
  1128. {
  1129. lines.clear();
  1130. const Font &f = *graphics->fonts[font];
  1131. int lineHeight = f.height;
  1132. lines = CMessage::breakText(Txt, pos.w, font);
  1133. maxH = lineHeight * lines.size();
  1134. maxW = 0;
  1135. BOOST_FOREACH(const std::string &line, lines)
  1136. vstd::amax(maxW, f.getWidth(line.c_str()));
  1137. }
  1138. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  1139. font(Font), align(Align), color(Color)
  1140. {};
  1141. void CLabelGroup::add(int x, int y, const std::string &text)
  1142. {
  1143. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1144. new CLabel(x, y, font, align, color, text);
  1145. };
  1146. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::Cornsilk*/)
  1147. :CBoundedLabel(rect.x, rect.y, Font, Align, Color, Text), sliderStyle(SliderStyle), slider(NULL)
  1148. {
  1149. type |= REDRAW_PARENT;
  1150. autoRedraw = false;
  1151. pos.h = rect.h;
  1152. pos.w = rect.w;
  1153. assert(Align == TOPLEFT || Align == CENTER); //TODO: support for other alignments
  1154. assert(pos.w >= 40); //we need some space
  1155. setTxt(Text);
  1156. }
  1157. void CTextBox::recalculateLines(const std::string &Txt)
  1158. {
  1159. //TODO: merge with CBoundedlabel::recalculateLines
  1160. vstd::clear_pointer(slider);
  1161. lines.clear();
  1162. const Font &f = *graphics->fonts[font];
  1163. int lineHeight = f.height;
  1164. int lineCapacity = pos.h / lineHeight;
  1165. lines = CMessage::breakText(Txt, pos.w, font);
  1166. if (lines.size() > lineCapacity) //we need to add a slider
  1167. {
  1168. lines = CMessage::breakText(Txt, pos.w - 32 - 10, font);
  1169. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1170. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1), lineCapacity, lines.size(), 0, false, sliderStyle);
  1171. if(active)
  1172. slider->activate();
  1173. }
  1174. maxH = lineHeight * lines.size();
  1175. maxW = 0;
  1176. BOOST_FOREACH(const std::string &line, lines)
  1177. vstd::amax(maxW, f.getWidth(line.c_str()));
  1178. }
  1179. void CTextBox::showAll(SDL_Surface * to)
  1180. {
  1181. CIntObject::showAll(to);
  1182. const Font &f = *graphics->fonts[font];
  1183. int dy = f.height; //line height
  1184. int base_y = pos.y;
  1185. if(alignment == CENTER)
  1186. base_y += std::max((pos.h - maxH)/2,0);
  1187. int howManyLinesToPrint = slider ? slider->capacity : lines.size();
  1188. int firstLineToPrint = slider ? slider->value : 0;
  1189. for (int i = 0; i < howManyLinesToPrint; i++)
  1190. {
  1191. const std::string &line = lines[i + firstLineToPrint];
  1192. if(!line.size()) continue;
  1193. int x = pos.x;
  1194. if(alignment == CENTER)
  1195. {
  1196. x += (pos.w - f.getWidth(line.c_str())) / 2;
  1197. if(slider)
  1198. x -= slider->pos.w / 2 + 5;
  1199. }
  1200. if(line[0] == '{' && line[line.size()-1] == '}')
  1201. CSDL_Ext::printAt(line, x, base_y + i*dy, font, Colors::Jasmine, to);
  1202. else
  1203. CSDL_Ext::printAt(line, x, base_y + i*dy, font, color, to);
  1204. }
  1205. }
  1206. void CTextBox::sliderMoved(int to)
  1207. {
  1208. if(!slider)
  1209. return;
  1210. redraw();
  1211. }
  1212. void CGStatusBar::print(const std::string & Text)
  1213. {
  1214. setTxt(Text);
  1215. }
  1216. void CGStatusBar::clear()
  1217. {
  1218. setTxt("");
  1219. }
  1220. std::string CGStatusBar::getCurrent()
  1221. {
  1222. return text;
  1223. }
  1224. //CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::Cornsilk*/, const std::string &Text /*= ""*/)
  1225. //: CLabel(x, y, Font, Align, Color, Text)
  1226. //{
  1227. // init();
  1228. //}
  1229. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::Cornsilk*/)
  1230. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  1231. {
  1232. init();
  1233. bg = BG;
  1234. addChild(bg);
  1235. pos = bg->pos;
  1236. calcOffset();
  1237. }
  1238. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  1239. : CLabel(x, y, FONT_SMALL, CENTER)
  1240. {
  1241. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1242. init();
  1243. bg = new CPicture(name);
  1244. pos = bg->pos;
  1245. if(maxw < pos.w)
  1246. {
  1247. vstd::amin(pos.w, maxw);
  1248. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  1249. }
  1250. calcOffset();
  1251. }
  1252. CGStatusBar::~CGStatusBar()
  1253. {
  1254. GH.statusbar = oldStatusBar;
  1255. }
  1256. void CGStatusBar::show(SDL_Surface * to)
  1257. {
  1258. }
  1259. void CGStatusBar::init()
  1260. {
  1261. oldStatusBar = GH.statusbar;
  1262. GH.statusbar = this;
  1263. }
  1264. void CGStatusBar::calcOffset()
  1265. {
  1266. switch(alignment)
  1267. {
  1268. case CENTER:
  1269. textOffset = Point(pos.w/2, pos.h/2);
  1270. break;
  1271. case BOTTOMRIGHT:
  1272. textOffset = Point(pos.w, pos.h);
  1273. break;
  1274. }
  1275. }
  1276. CTextInput::CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB):
  1277. CLabel(Pos.x, Pos.y, font, CENTER),
  1278. cb(CB)
  1279. {
  1280. type |= REDRAW_PARENT;
  1281. focus = false;
  1282. pos.h = Pos.h;
  1283. pos.w = Pos.w;
  1284. textOffset = Point(pos.w/2, pos.h/2);
  1285. captureAllKeys = true;
  1286. bg = nullptr;
  1287. addUsedEvents(LCLICK | KEYBOARD);
  1288. giveFocus();
  1289. }
  1290. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1291. :cb(CB)
  1292. {
  1293. focus = false;
  1294. pos += Pos;
  1295. captureAllKeys = true;
  1296. OBJ_CONSTRUCTION;
  1297. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1298. addUsedEvents(LCLICK | KEYBOARD);
  1299. giveFocus();
  1300. }
  1301. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  1302. {
  1303. focus = false;
  1304. pos += Pos;
  1305. captureAllKeys = true;
  1306. OBJ_CONSTRUCTION;
  1307. bg = new CPicture(Pos, 0, true);
  1308. Rect hlp = Pos;
  1309. if(srf)
  1310. CSDL_Ext::blitSurface(srf, &hlp, *bg, NULL);
  1311. else
  1312. SDL_FillRect(*bg, NULL, 0);
  1313. pos.w = bg->pos.w;
  1314. pos.h = bg->pos.h;
  1315. bg->pos = pos;
  1316. addUsedEvents(LCLICK | KEYBOARD);
  1317. giveFocus();
  1318. }
  1319. std::string CTextInput::visibleText()
  1320. {
  1321. return focus ? text + "_" : text;
  1322. }
  1323. void CTextInput::clickLeft( tribool down, bool previousState )
  1324. {
  1325. if(down && !focus)
  1326. giveFocus();
  1327. }
  1328. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1329. {
  1330. if(!focus || key.state != SDL_PRESSED)
  1331. return;
  1332. if(key.keysym.sym == SDLK_TAB)
  1333. {
  1334. moveFocus();
  1335. GH.breakEventHandling();
  1336. return;
  1337. }
  1338. std::string oldText = text;
  1339. switch(key.keysym.sym)
  1340. {
  1341. case SDLK_BACKSPACE:
  1342. if(!text.empty())
  1343. text.resize(text.size()-1);
  1344. break;
  1345. default:
  1346. if (key.keysym.unicode < ' ')
  1347. return;
  1348. else
  1349. text += key.keysym.unicode; //TODO 16-/>8
  1350. break;
  1351. }
  1352. filters(text, oldText);
  1353. if (text != oldText)
  1354. {
  1355. redraw();
  1356. cb(text);
  1357. }
  1358. }
  1359. void CTextInput::setTxt( const std::string &nText, bool callCb )
  1360. {
  1361. CLabel::setTxt(nText);
  1362. if(callCb)
  1363. cb(text);
  1364. }
  1365. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  1366. {
  1367. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER)
  1368. return false;
  1369. //this should allow all non-printable keys to go through (for example arrows)
  1370. if (key.keysym.unicode < ' ')
  1371. return false;
  1372. return true;
  1373. }
  1374. void CTextInput::filenameFilter(std::string & text, const std::string &)
  1375. {
  1376. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  1377. size_t pos;
  1378. while ((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  1379. text.erase(pos, 1);
  1380. }
  1381. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  1382. {
  1383. assert(minValue < maxValue);
  1384. if (text.empty())
  1385. text = "0";
  1386. size_t pos = 0;
  1387. if (text[0] == '-') //allow '-' sign as first symbol only
  1388. pos++;
  1389. while (pos < text.size())
  1390. {
  1391. if (text[pos] < '0' || text[pos] > '9')
  1392. {
  1393. text = oldText;
  1394. return; //new text is not number.
  1395. }
  1396. pos++;
  1397. }
  1398. try
  1399. {
  1400. int value = boost::lexical_cast<int>(text);
  1401. if (value < minValue)
  1402. text = boost::lexical_cast<std::string>(minValue);
  1403. else if (value > maxValue)
  1404. text = boost::lexical_cast<std::string>(maxValue);
  1405. }
  1406. catch(boost::bad_lexical_cast &)
  1407. {
  1408. //Should never happen. Unless I missed some cases
  1409. tlog0 << "Warning: failed to convert "<< text << " to number!\n";
  1410. text = oldText;
  1411. }
  1412. }
  1413. CFocusable::CFocusable()
  1414. {
  1415. focusables.push_back(this);
  1416. }
  1417. CFocusable::~CFocusable()
  1418. {
  1419. if(inputWithFocus == this)
  1420. inputWithFocus = NULL;
  1421. focusables -= this;
  1422. }
  1423. void CFocusable::giveFocus()
  1424. {
  1425. if(inputWithFocus)
  1426. {
  1427. inputWithFocus->focus = false;
  1428. inputWithFocus->redraw();
  1429. }
  1430. focus = true;
  1431. inputWithFocus = this;
  1432. redraw();
  1433. }
  1434. void CFocusable::moveFocus()
  1435. {
  1436. std::list<CFocusable*>::iterator i = vstd::find(focusables, this),
  1437. ourIt = i;
  1438. for(i++; i != ourIt; i++)
  1439. {
  1440. if(i == focusables.end())
  1441. i = focusables.begin();
  1442. if((*i)->active)
  1443. {
  1444. (*i)->giveFocus();
  1445. break;;
  1446. }
  1447. }
  1448. }
  1449. CWindowObject::CWindowObject(int options_, std::string imageName, Point centerAt):
  1450. CIntObject(getUsedEvents(options_), Point()),
  1451. shadow(nullptr),
  1452. options(options_),
  1453. background(createBg(imageName, options & PLAYER_COLORED))
  1454. {
  1455. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1456. if (options & RCLICK_POPUP)
  1457. CCS->curh->hide();
  1458. if (background)
  1459. pos = background->center(centerAt);
  1460. else
  1461. center(centerAt);
  1462. if (!(options & SHADOW_DISABLED))
  1463. setShadow(true);
  1464. }
  1465. CWindowObject::CWindowObject(int options_, std::string imageName):
  1466. CIntObject(getUsedEvents(options_), Point()),
  1467. shadow(nullptr),
  1468. options(options_),
  1469. background(createBg(imageName, options & PLAYER_COLORED))
  1470. {
  1471. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  1472. if (options & RCLICK_POPUP)
  1473. CCS->curh->hide();
  1474. if (background)
  1475. pos = background->center();
  1476. else
  1477. center(Point(screen->w/2, screen->h/2));
  1478. if (!(options & SHADOW_DISABLED))
  1479. setShadow(true);
  1480. }
  1481. CWindowObject::~CWindowObject()
  1482. {
  1483. setShadow(false);
  1484. }
  1485. CPicture * CWindowObject::createBg(std::string imageName, bool playerColored)
  1486. {
  1487. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1488. if (imageName.empty())
  1489. return nullptr;
  1490. auto image = new CPicture(imageName);
  1491. if (playerColored)
  1492. image->colorize(LOCPLINT->playerID);
  1493. return image;
  1494. }
  1495. void CWindowObject::setBackground(std::string filename)
  1496. {
  1497. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1498. delete background;
  1499. background = createBg(filename, options & PLAYER_COLORED);
  1500. if (background)
  1501. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  1502. updateShadow();
  1503. }
  1504. int CWindowObject::getUsedEvents(int options)
  1505. {
  1506. if (options & RCLICK_POPUP)
  1507. return RCLICK;
  1508. return 0;
  1509. }
  1510. void CWindowObject::updateShadow()
  1511. {
  1512. setShadow(false);
  1513. if (!(options & SHADOW_DISABLED))
  1514. setShadow(true);
  1515. }
  1516. void CWindowObject::setShadow(bool on)
  1517. {
  1518. vstd::clear_pointer(shadow);
  1519. //size of shadow
  1520. static const int size = 8;
  1521. if (on == bool(shadow))
  1522. return;
  1523. //object too small to cast shadow
  1524. if (pos.h <= size || pos.w <= size)
  1525. return;
  1526. if (on)
  1527. {
  1528. //helper to set last row
  1529. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  1530. {
  1531. Uint8 * ptr = (Uint8*)surf->pixels + surf->pitch * (row);
  1532. for (size_t i=0; i< surf->w; i++)
  1533. {
  1534. Channels::px<4>::a.set(ptr, 128);
  1535. ptr+=4;
  1536. }
  1537. };
  1538. // helper to set last column
  1539. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  1540. {
  1541. Uint8 * ptr = (Uint8*)surf->pixels + 4 * (col);
  1542. for (size_t i=0; i< surf->h; i++)
  1543. {
  1544. Channels::px<4>::a.set(ptr, 128);
  1545. ptr+= surf->pitch;
  1546. }
  1547. };
  1548. static SDL_Surface * shadowCornerTempl = nullptr;
  1549. static SDL_Surface * shadowBottomTempl = nullptr;
  1550. static SDL_Surface * shadowRightTempl = nullptr;
  1551. //one-time initialization
  1552. if (!shadowCornerTempl)
  1553. {
  1554. //create "template" surfaces
  1555. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  1556. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  1557. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  1558. Uint32 shadowColor = SDL_MapRGBA(shadowCornerTempl->format, 0, 0, 0, 192);
  1559. //fill with shadow body color
  1560. SDL_FillRect(shadowCornerTempl, NULL, shadowColor);
  1561. SDL_FillRect(shadowBottomTempl, NULL, shadowColor);
  1562. SDL_FillRect(shadowRightTempl, NULL, shadowColor);
  1563. //fill last row and column with more transparent color
  1564. blitAlphaCol(shadowRightTempl , size-1);
  1565. blitAlphaCol(shadowCornerTempl, size-1);
  1566. blitAlphaRow(shadowBottomTempl, size-1);
  1567. blitAlphaRow(shadowCornerTempl, size-1);
  1568. }
  1569. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1570. //FIXME: do something with this points
  1571. Point shadowStart;
  1572. if (options & BORDERED)
  1573. shadowStart = Point(size - 14, size - 14);
  1574. else
  1575. shadowStart = Point(size, size);
  1576. Point shadowPos;
  1577. if (options & BORDERED)
  1578. shadowPos = Point(pos.w + 14, pos.h + 14);
  1579. else
  1580. shadowPos = Point(pos.w, pos.h);
  1581. Point fullsize;
  1582. if (options & BORDERED)
  1583. fullsize = Point(pos.w + 28, pos.h + 29);
  1584. else
  1585. fullsize = Point(pos.w, pos.h);
  1586. //create base 8x8 piece of shadow
  1587. SDL_Surface * shadowCorner = CSDL_Ext::copySurface(shadowCornerTempl);
  1588. SDL_Surface * shadowBottom = CSDL_Ext::scaleSurfaceFast(shadowBottomTempl, fullsize.x - size, size);
  1589. SDL_Surface * shadowRight = CSDL_Ext::scaleSurfaceFast(shadowRightTempl, size, fullsize.y - size);
  1590. blitAlphaCol(shadowBottom, 0);
  1591. blitAlphaRow(shadowRight, 0);
  1592. //generate "shadow" object with these 3 pieces in it
  1593. shadow = new CIntObject;
  1594. shadow->addChild(new CPicture(shadowCorner, shadowPos.x, shadowPos.y));
  1595. shadow->addChild(new CPicture(shadowRight, shadowPos.x, shadowStart.y));
  1596. shadow->addChild(new CPicture(shadowBottom, shadowStart.x, shadowPos.y));
  1597. }
  1598. }
  1599. void CWindowObject::showAll(SDL_Surface *to)
  1600. {
  1601. CIntObject::showAll(to);
  1602. if ((options & BORDERED) && (pos.h != to->h || pos.w != to->w))
  1603. CMessage::drawBorder(LOCPLINT ? LOCPLINT->playerID : 1, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  1604. }
  1605. void CWindowObject::close()
  1606. {
  1607. GH.popIntTotally(this);
  1608. }
  1609. void CWindowObject::clickRight(tribool down, bool previousState)
  1610. {
  1611. close();
  1612. CCS->curh->show();
  1613. }