CIntObjectClasses.cpp 33 KB

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