CIntObjectClasses.cpp 42 KB

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