CIntObjectClasses.cpp 40 KB

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