CIntObjectClasses.cpp 37 KB

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