GUIClasses.cpp 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236
  1. #include "GUIClasses.h"
  2. #include "SDL_Extensions.h"
  3. #include "../stdafx.h"
  4. #include "CAdvmapInterface.h"
  5. #include "CBattleInterface.h"
  6. #include "../CCallback.h"
  7. #include "CCastleInterface.h"
  8. #include "CCursorHandler.h"
  9. #include "CGameInfo.h"
  10. #include "CHeroWindow.h"
  11. #include "CMessage.h"
  12. #include "CConfigHandler.h"
  13. #include "SDL_framerate.h"
  14. #include "CConfigHandler.h"
  15. #include "CCreatureAnimation.h"
  16. #include "Graphics.h"
  17. #include "../hch/CArtHandler.h"
  18. #include "../hch/CGeneralTextHandler.h"
  19. #include "../hch/CHeroHandler.h"
  20. #include "../hch/CLodHandler.h"
  21. #include "../hch/CObjectHandler.h"
  22. #include "../hch/CSpellHandler.h"
  23. #include "../hch/CTownHandler.h"
  24. #include "../lib/CondSh.h"
  25. #include "../lib/map.h"
  26. #include "../mapHandler.h"
  27. #include "../timeHandler.h"
  28. #include <boost/lexical_cast.hpp>
  29. #include <boost/algorithm/string.hpp>
  30. #include <boost/algorithm/string/replace.hpp>
  31. #include <boost/assign/std/vector.hpp>
  32. #include <boost/assign/list_of.hpp>
  33. #include <boost/date_time/posix_time/posix_time.hpp>
  34. //#include <boost/thread.hpp>
  35. #include <cmath>
  36. #include <queue>
  37. #include <sstream>
  38. #include "../lib/NetPacks.h"
  39. /*
  40. * GUIClasses.cpp, part of VCMI engine
  41. *
  42. * Authors: listed in file AUTHORS in main folder
  43. *
  44. * License: GNU General Public License v2.0 or later
  45. * Full text of license available in license.txt file, in main folder
  46. *
  47. */
  48. using namespace boost::assign;
  49. using namespace CSDL_Ext;
  50. extern TTF_Font * GEOR16;
  51. extern std::queue<SDL_Event*> events;
  52. extern boost::mutex eventsM;
  53. static StackState* getStackState(const CGObjectInstance *obj, int pos, bool town)
  54. {
  55. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(obj);
  56. if(!h) return NULL;
  57. StackState *pom = new StackState();
  58. pom->currentHealth = 0;
  59. pom->attackBonus = h->getPrimSkillLevel(0);
  60. pom->defenseBonus = h->getPrimSkillLevel(1);
  61. pom->luck = h->getCurrentLuck();
  62. pom->morale = h->getCurrentMorale(pos,town);
  63. pom->speedBonus = h->valOfBonuses(HeroBonus::STACKS_SPEED);
  64. return pom;
  65. }
  66. void CGarrisonSlot::hover (bool on)
  67. {
  68. Hoverable::hover(on);
  69. if(on)
  70. {
  71. std::string temp;
  72. if(creature)
  73. {
  74. if(owner->highlighted)
  75. {
  76. if(owner->highlighted == this)
  77. {
  78. temp = CGI->generaltexth->tcommands[4];
  79. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  80. }
  81. else if (owner->highlighted->creature == creature)
  82. {
  83. temp = CGI->generaltexth->tcommands[2];
  84. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  85. }
  86. else if (owner->highlighted->creature)
  87. {
  88. temp = CGI->generaltexth->tcommands[7];
  89. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  90. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  91. }
  92. else
  93. {
  94. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  95. tlog2 << "Highlighted set to NULL"<<std::endl;
  96. owner->highlighted = NULL;
  97. }
  98. }
  99. else
  100. {
  101. if(upg)
  102. {
  103. temp = CGI->generaltexth->tcommands[32];
  104. }
  105. else
  106. {
  107. temp = CGI->generaltexth->tcommands[12];
  108. }
  109. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  110. };
  111. }
  112. else
  113. {
  114. if(owner->highlighted)
  115. {
  116. const CArmedInstance *highl = owner->highlighted->getObj();
  117. if( highl->needsLastStack() //we are moving stack from hero's
  118. && highl->army.slots.size() == 1 //it's only stack
  119. && owner->highlighted->upg != upg //we're moving it to the other garrison
  120. )
  121. {
  122. temp = CGI->generaltexth->tcommands[5]; //cannot move last stack!
  123. }
  124. else
  125. {
  126. temp = CGI->generaltexth->tcommands[6];
  127. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  128. }
  129. }
  130. else
  131. {
  132. temp = CGI->generaltexth->tcommands[11];
  133. }
  134. }
  135. LOCPLINT->statusbar->print(temp);
  136. }
  137. else
  138. {
  139. LOCPLINT->statusbar->clear();
  140. }
  141. }
  142. const CArmedInstance * CGarrisonSlot::getObj()
  143. {
  144. return (!upg)?(owner->oup):(owner->odown);
  145. }
  146. void CGarrisonSlot::clickRight (tribool down)
  147. {
  148. StackState *pom = getStackState(getObj(),ID, LOCPLINT->topInt() == LOCPLINT->castleInt);
  149. if(down && creature)
  150. {
  151. LOCPLINT->pushInt(new CCreInfoWindow(creature->idNumber,0,count,pom,0,0,NULL));
  152. }
  153. delete pom;
  154. }
  155. void CGarrisonSlot::clickLeft(tribool down)
  156. {
  157. if(owner->ignoreEvent)
  158. {
  159. owner->ignoreEvent = false;
  160. return;
  161. }
  162. if(down)
  163. {
  164. bool refr = false;
  165. if(owner->highlighted)
  166. {
  167. if(owner->highlighted == this) //view info
  168. {
  169. StackState *pom2 = getStackState(getObj(), ID, LOCPLINT->topInt() == LOCPLINT->castleInt);
  170. UpgradeInfo pom = LOCPLINT->cb->getUpgradeInfo(getObj(), ID);
  171. CCreInfoWindow *creWindow = NULL;
  172. if(pom.oldID>=0) //upgrade is possible
  173. {
  174. creWindow = new CCreInfoWindow(
  175. creature->idNumber,1,count,pom2,
  176. boost::bind(&CCallback::upgradeCreature,LOCPLINT->cb,getObj(),ID,pom.newID[0]), //bind upgrade function
  177. boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID),&pom);
  178. }
  179. else
  180. {
  181. creWindow = new CCreInfoWindow(
  182. creature->idNumber,1,count,pom2,0,
  183. boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID), NULL);
  184. }
  185. LOCPLINT->pushInt(creWindow);
  186. owner->highlighted = NULL;
  187. show(screen2);
  188. refr = true;
  189. delete pom2;
  190. }
  191. else if((owner->splitting || LOCPLINT->shiftPressed())
  192. && (!creature
  193. || (creature == owner->highlighted->creature))
  194. ) //we want to split
  195. {
  196. owner->p2 = ID; //store the second stack pos
  197. owner->pb = upg;//store the second stack owner (up or down army)
  198. owner->splitting = false;
  199. int totalAmount = owner->highlighted->count;
  200. if(creature)
  201. totalAmount += count;
  202. int last = -1;
  203. if(upg != owner->highlighted->upg) //not splitting within same army
  204. {
  205. if(owner->highlighted->getObj()->army.slots.size() == 1
  206. && owner->highlighted->getObj()->needsLastStack() )
  207. {
  208. last = 0;
  209. }
  210. if(getObj()->army.slots.size() == 1
  211. && getObj()->needsLastStack() )
  212. {
  213. last += 2;
  214. }
  215. }
  216. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  217. LOCPLINT->pushInt(spw);
  218. refr = true;
  219. }
  220. else if(creature != owner->highlighted->creature) //swap
  221. {
  222. LOCPLINT->cb->swapCreatures(
  223. (!upg)?(owner->oup):(owner->odown),
  224. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  225. ID,owner->highlighted->ID);
  226. }
  227. else //merge
  228. {
  229. LOCPLINT->cb->mergeStacks(
  230. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  231. (!upg)?(owner->oup):(owner->odown),
  232. owner->highlighted->ID,ID);
  233. }
  234. }
  235. else //highlight
  236. {
  237. if(creature)
  238. owner->highlighted = this;
  239. show(screen2);
  240. refr = true;
  241. }
  242. if(refr) {hover(false); hover(true); } //to refresh statusbar
  243. }
  244. }
  245. void CGarrisonSlot::activate()
  246. {
  247. if(!active) active=true;
  248. else return;
  249. ClickableL::activate();
  250. ClickableR::activate();
  251. Hoverable::activate();
  252. }
  253. void CGarrisonSlot::deactivate()
  254. {
  255. if(active) active=false;
  256. else return;
  257. ClickableL::deactivate();
  258. ClickableR::deactivate();
  259. Hoverable::deactivate();
  260. }
  261. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CCreature * Creature, int Count)
  262. {
  263. active = false;
  264. upg = Upg;
  265. count = Count;
  266. ID = IID;
  267. creature = Creature;
  268. pos.x = x;
  269. pos.y = y;
  270. pos.w = 58;
  271. pos.h = 64;
  272. owner = Owner;
  273. }
  274. CGarrisonSlot::~CGarrisonSlot()
  275. {
  276. if(active)
  277. deactivate();
  278. }
  279. void CGarrisonSlot::show(SDL_Surface * to)
  280. {
  281. if(creature)
  282. {
  283. char buf[15];
  284. SDL_itoa(count,buf,10);
  285. blitAt(graphics->bigImgs[creature->idNumber],pos,to);
  286. printToWR(buf,pos.x+56,pos.y+62,GEOR16,zwykly,to);
  287. if((owner->highlighted==this)
  288. || (owner->splitting && owner->highlighted->creature == creature))
  289. {
  290. blitAt(graphics->bigImgs[-1],pos,to);
  291. }
  292. }
  293. else //empty slot
  294. {
  295. SDL_Rect jakis1 = genRect(pos.h,pos.w,owner->offx+ID*(pos.w+owner->interx),owner->offy+upg*(pos.h+owner->intery)),
  296. jakis2 = pos;
  297. SDL_BlitSurface(owner->sur,&jakis1,to,&jakis2);
  298. if(owner->splitting)
  299. blitAt(graphics->bigImgs[-1],pos,to);
  300. }
  301. }
  302. CGarrisonInt::~CGarrisonInt()
  303. {
  304. if(sup)
  305. {
  306. for(size_t i=0;i<sup->size();i++)
  307. {
  308. delete (*sup)[i];
  309. }
  310. delete sup;
  311. }
  312. if(sdown)
  313. {
  314. for(size_t i=0;i<sdown->size();i++)
  315. {
  316. delete (*sdown)[i]; //XXX what about smartpointers? boost or auto_ptr from std
  317. }
  318. delete sdown;
  319. }
  320. }
  321. void CGarrisonInt::show(SDL_Surface * to)
  322. {
  323. if(sup)
  324. {
  325. for(size_t i = 0; i<sup->size(); i++)
  326. {
  327. if((*sup)[i])
  328. {
  329. (*sup)[i]->show(to);
  330. }
  331. }
  332. }
  333. if(sdown)
  334. {
  335. for(size_t i = 0; i<sdown->size(); i++)
  336. {
  337. if((*sdown)[i])
  338. {
  339. (*sdown)[i]->show(to);
  340. }
  341. }
  342. }
  343. }
  344. void CGarrisonInt::deactiveteSlots()
  345. {
  346. if(sup)
  347. {
  348. for(int i = 0; i<sup->size(); i++)
  349. {
  350. if((*sup)[i])
  351. {
  352. (*sup)[i]->deactivate();
  353. }
  354. }
  355. }
  356. if(sdown)
  357. {
  358. for(int i = 0; i<sdown->size(); i++)
  359. {
  360. if((*sdown)[i])
  361. {
  362. (*sdown)[i]->deactivate();
  363. }
  364. }
  365. }
  366. }
  367. void CGarrisonInt::activeteSlots()
  368. {
  369. if(sup)
  370. {
  371. for(int i = 0; i<sup->size(); i++)
  372. {
  373. if((*sup)[i])
  374. {
  375. (*sup)[i]->activate();
  376. }
  377. }
  378. }
  379. if(sdown)
  380. {
  381. for(int i = 0; i<sdown->size(); i++)
  382. {
  383. if((*sdown)[i])
  384. {
  385. (*sdown)[i]->activate();
  386. }
  387. }
  388. }
  389. }
  390. void CGarrisonInt::createSlots()
  391. {
  392. if(set1)
  393. {
  394. sup = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  395. for
  396. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set1->slots.begin();
  397. i!=set1->slots.end(); i++)
  398. {
  399. (*sup)[i->first] =
  400. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y,i->first, 0,
  401. &CGI->creh->creatures[i->second.first],i->second.second);
  402. }
  403. for(int i=0; i<sup->size(); i++)
  404. if((*sup)[i] == NULL)
  405. (*sup)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y,i,0,NULL, 0);
  406. }
  407. if(set2)
  408. {
  409. sdown = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  410. for
  411. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set2->slots.begin();
  412. i!=set2->slots.end(); i++)
  413. {
  414. (*sdown)[i->first] =
  415. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y + 64 + intery,i->first,1,
  416. &CGI->creh->creatures[i->second.first],i->second.second);
  417. }
  418. for(int i=0; i<sdown->size(); i++)
  419. if((*sdown)[i] == NULL)
  420. (*sdown)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y + 64 + intery,i,1, NULL, 0);
  421. }
  422. }
  423. void CGarrisonInt::deleteSlots()
  424. {
  425. if(sup)
  426. {
  427. for(int i = 0; i<sup->size(); i++)
  428. {
  429. if((*sup)[i])
  430. {
  431. delete (*sup)[i];
  432. }
  433. }
  434. delete sup;
  435. sup = NULL;
  436. }
  437. if(sdown)
  438. {
  439. for(int i = 0; i<sdown->size(); i++)
  440. {
  441. if((*sdown)[i])
  442. {
  443. delete (*sdown)[i];
  444. }
  445. }
  446. delete sdown;
  447. sdown = NULL;
  448. }
  449. }
  450. void CGarrisonInt::recreateSlots()
  451. {
  452. splitting = false;
  453. highlighted = NULL;
  454. if(active)
  455. {
  456. deactiveteSlots();
  457. }
  458. deleteSlots();
  459. createSlots();
  460. if(active)
  461. {
  462. //ignoreEvent = true;
  463. activeteSlots();
  464. //show(screen2);
  465. }
  466. }
  467. void CGarrisonInt::splitClick()
  468. {
  469. if(!highlighted)
  470. return;
  471. splitting = !splitting;
  472. show(screen2);
  473. }
  474. void CGarrisonInt::splitStacks(int am2)
  475. {
  476. LOCPLINT->cb->splitStack(
  477. (highlighted->upg)?(odown):(oup),
  478. (pb)?(odown):(oup),
  479. highlighted->ID,
  480. p2,
  481. am2);
  482. }
  483. CGarrisonInt::CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *&pomsur, int OX, int OY, const CArmedInstance *s1,
  484. const CArmedInstance *s2)
  485. :interx(inx),intery(iny),highlighted(NULL),sur(pomsur),offx(OX),offy(OY),sup(NULL),
  486. sdown(NULL),oup(s1),odown(s2)
  487. {
  488. active = false;
  489. splitting = false;
  490. set1 = LOCPLINT->cb->getGarrison(s1);
  491. set2 = LOCPLINT->cb->getGarrison(s2);
  492. ignoreEvent = false;
  493. update = true;
  494. pos.x=(x);
  495. pos.y=(y);
  496. pos.w=(58);
  497. pos.h=(64);
  498. createSlots();
  499. }
  500. void CGarrisonInt::activate()
  501. {
  502. active = true;
  503. if(sup)
  504. {
  505. for(int i = 0; i<sup->size(); i++)
  506. if((*sup)[i])
  507. (*sup)[i]->activate();
  508. }
  509. if(sdown)
  510. {
  511. for(int i = 0; i<sdown->size(); i++)
  512. if((*sdown)[i])
  513. (*sdown)[i]->activate();
  514. }
  515. }
  516. void CGarrisonInt::deactivate()
  517. {
  518. active = false;
  519. deactiveteSlots();
  520. }
  521. CInfoWindow::CInfoWindow(std::string text, int player, int charperline, const std::vector<SComponent*> &comps, std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, bool delComps)
  522. {
  523. ID = -1;
  524. this->delComps = delComps;
  525. for(int i=0;i<Buttons.size();i++)
  526. {
  527. buttons.push_back(new AdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first));
  528. buttons[i]->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  529. }
  530. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  531. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  532. for(int i=0;i<comps.size();i++)
  533. {
  534. components.push_back(comps[i]);
  535. }
  536. CMessage::drawIWindow(this,text,player,charperline);
  537. }
  538. CInfoWindow::CInfoWindow()
  539. {
  540. ID = -1;
  541. delComps = false;
  542. }
  543. void CInfoWindow::close()
  544. {
  545. LOCPLINT->popIntTotally(this);
  546. LOCPLINT->showingDialog->setn(false);
  547. }
  548. void CInfoWindow::show(SDL_Surface * to)
  549. {
  550. CSimpleWindow::show(to);
  551. for(int i=0;i<buttons.size();i++)
  552. buttons[i]->show(to);
  553. }
  554. CInfoWindow::~CInfoWindow()
  555. {
  556. if(delComps)
  557. {
  558. for (int i=0;i<components.size();i++)
  559. delete components[i];
  560. }
  561. for(int i=0;i<buttons.size();i++)
  562. delete buttons[i];
  563. }
  564. void CInfoWindow::activate()
  565. {
  566. for (int i=0;i<components.size();i++)
  567. components[i]->activate();
  568. for(int i=0;i<buttons.size();i++)
  569. buttons[i]->activate();
  570. }
  571. void CInfoWindow::deactivate()
  572. {
  573. for (int i=0;i<components.size();i++)
  574. components[i]->deactivate();
  575. for(int i=0;i<buttons.size();i++)
  576. buttons[i]->deactivate();
  577. }
  578. void CRClickPopup::clickRight (tribool down)
  579. {
  580. if(down)
  581. return;
  582. close();
  583. }
  584. void CRClickPopup::activate()
  585. {
  586. ClickableR::activate();
  587. }
  588. void CRClickPopup::deactivate()
  589. {
  590. ClickableR::deactivate();
  591. }
  592. void CRClickPopup::close()
  593. {
  594. LOCPLINT->popIntTotally(this);
  595. }
  596. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  597. :free(Free),bitmap(Bitmap)
  598. {
  599. pos.x = x;
  600. pos.y = y;
  601. pos.h = bitmap->h;
  602. pos.w = bitmap->w;
  603. // Put the window back on screen if necessary
  604. amax(pos.x, 0);
  605. amax(pos.y, 0);
  606. amin(pos.x, conf.cc.resx - bitmap->w);
  607. amin(pos.y, conf.cc.resy - bitmap->h);
  608. }
  609. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  610. {
  611. free=Free;
  612. bitmap=Bitmap;
  613. }
  614. void CInfoPopup::close()
  615. {
  616. if(free)
  617. SDL_FreeSurface(bitmap);
  618. LOCPLINT->popIntTotally(this);
  619. }
  620. void CInfoPopup::show(SDL_Surface * to)
  621. {
  622. blitAt(bitmap,pos.x,pos.y,to);
  623. }
  624. void SComponent::init(Etype Type, int Subtype, int Val)
  625. {
  626. std::ostringstream oss;
  627. switch (Type)
  628. {
  629. case artifact:
  630. description = CGI->arth->artifacts[Subtype].Description();
  631. subtitle = CGI->arth->artifacts[Subtype].Name();
  632. break;
  633. case primskill:
  634. oss << ((Val>0)?("+"):("-")) << Val << " ";
  635. if(Subtype < 4)
  636. {
  637. description = CGI->generaltexth->arraytxt[2+Subtype];
  638. oss << CGI->generaltexth->primarySkillNames[Subtype];
  639. }
  640. else if(Subtype == 5) //spell points
  641. {
  642. description = CGI->generaltexth->allTexts[149];
  643. oss << CGI->generaltexth->allTexts[387];
  644. }
  645. else
  646. {
  647. tlog1 << "Wrong subtype=" << Subtype << std::endl;
  648. }
  649. subtitle = oss.str();
  650. break;
  651. case secskill44: case secskill:
  652. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  653. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  654. break;
  655. case resource:
  656. description = CGI->generaltexth->allTexts[242];
  657. oss << Val;
  658. subtitle = oss.str();
  659. break;
  660. case spell:
  661. description = CGI->spellh->spells[Subtype].descriptions[Val];
  662. subtitle = CGI->spellh->spells[Subtype].name;
  663. break;
  664. case creature:
  665. subtitle = boost::lexical_cast<std::string>(Val) + " " + CGI->creh->creatures[Subtype].*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  666. break;
  667. case experience:
  668. description = CGI->generaltexth->allTexts[241];
  669. oss << Val ;
  670. if(Subtype && Val==1)
  671. {
  672. subtitle = CGI->generaltexth->allTexts[442];
  673. }
  674. else
  675. {
  676. subtitle = oss.str();
  677. }
  678. break;
  679. }
  680. type = Type;
  681. subtype = Subtype;
  682. val = Val;
  683. SDL_Surface * temp = this->getImg();
  684. if(!temp)
  685. {
  686. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  687. return;
  688. }
  689. pos.w = temp->w;
  690. pos.h = temp->h;
  691. }
  692. SComponent::SComponent(Etype Type, int Subtype, int Val)
  693. {
  694. init(Type,Subtype,Val);
  695. }
  696. SComponent::SComponent(const Component &c)
  697. {
  698. if(c.id==5)
  699. init(experience,c.subtype,c.val);
  700. else if(c.id == Component::SPELL)
  701. init(spell,c.subtype,c.val);
  702. else
  703. init((Etype)c.id,c.subtype,c.val);
  704. if(c.id==2 && c.when==-1)
  705. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  706. }
  707. void SComponent::show(SDL_Surface * to)
  708. {
  709. blitAt(getImg(),pos.x,pos.y,to);
  710. }
  711. SDL_Surface * SComponent::getImg()
  712. {
  713. switch (type)
  714. {
  715. case artifact:
  716. return graphics->artDefs->ourImages[subtype].bitmap;
  717. break;
  718. case primskill:
  719. return graphics->pskillsb->ourImages[subtype].bitmap;
  720. break;
  721. case secskill44:
  722. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  723. break;
  724. case secskill:
  725. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  726. break;
  727. case resource:
  728. return graphics->resources->ourImages[subtype].bitmap;
  729. break;
  730. case experience:
  731. return graphics->pskillsb->ourImages[4].bitmap;
  732. break;
  733. case morale:
  734. return graphics->morale82->ourImages[val+3].bitmap;
  735. break;
  736. case luck:
  737. return graphics->luck82->ourImages[val+3].bitmap;
  738. break;
  739. case spell:
  740. return graphics->spellscr->ourImages[subtype].bitmap;
  741. break;
  742. case creature:
  743. return graphics->bigImgs[subtype];
  744. }
  745. return NULL;
  746. }
  747. void SComponent::clickRight (tribool down)
  748. {
  749. if(description.size())
  750. LOCPLINT->adventureInt->handleRightClick(description,down,this);
  751. }
  752. void SComponent::activate()
  753. {
  754. ClickableR::activate();
  755. }
  756. void SComponent::deactivate()
  757. {
  758. ClickableR::deactivate();
  759. }
  760. void CSelectableComponent::clickLeft(tribool down)
  761. {
  762. if (down)
  763. {
  764. if(onSelect)
  765. onSelect();
  766. }
  767. }
  768. void CSelectableComponent::init(SDL_Surface * Border)
  769. {
  770. SDL_Surface * symb = SComponent::getImg();
  771. myBitmap = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  772. SDL_SetColorKey(myBitmap,SDL_SRCCOLORKEY,SDL_MapRGB(myBitmap->format,0,255,255));
  773. blitAt(symb,1,1,myBitmap);
  774. if (Border) //use custom border
  775. {
  776. border = Border;
  777. customB = true;
  778. }
  779. else //we need to draw border
  780. {
  781. customB = false;
  782. border = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  783. SDL_FillRect(border,NULL,0x00FFFF);
  784. for (int i=0;i<border->w;i++)
  785. {
  786. SDL_PutPixelWithoutRefresh(border,i,0,239,215,123);
  787. SDL_PutPixelWithoutRefresh(border,i,(border->h)-1,239,215,123);
  788. }
  789. for (int i=0;i<border->h;i++)
  790. {
  791. SDL_PutPixelWithoutRefresh(border,0,i,239,215,123);
  792. SDL_PutPixelWithoutRefresh(border,(border->w)-1,i,239,215,123);
  793. }
  794. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  795. }
  796. selected = false;
  797. }
  798. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect, SDL_Surface * Border)
  799. :SComponent(c),onSelect(OnSelect)
  800. {
  801. init(Border);
  802. }
  803. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect, SDL_Surface * Border)
  804. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  805. {
  806. init(Border);
  807. }
  808. CSelectableComponent::~CSelectableComponent()
  809. {
  810. SDL_FreeSurface(myBitmap);
  811. if (!customB)
  812. SDL_FreeSurface(border);
  813. }
  814. void CSelectableComponent::activate()
  815. {
  816. KeyInterested::activate();
  817. SComponent::activate();
  818. ClickableL::activate();
  819. }
  820. void CSelectableComponent::deactivate()
  821. {
  822. KeyInterested::deactivate();
  823. SComponent::deactivate();
  824. ClickableL::deactivate();
  825. }
  826. SDL_Surface * CSelectableComponent::getImg()
  827. {
  828. return myBitmap;
  829. }
  830. void CSelectableComponent::select(bool on)
  831. {
  832. if(on != selected)
  833. {
  834. SDL_FillRect(myBitmap,NULL,0x000000);
  835. blitAt(SComponent::getImg(),1,1,myBitmap);
  836. if (on)
  837. {
  838. blitAt(border,0,0,myBitmap);
  839. }
  840. selected = on;
  841. return;
  842. }
  843. else
  844. {
  845. return;
  846. }
  847. }
  848. void CSelectableComponent::show(SDL_Surface * to)
  849. {
  850. blitAt(myBitmap,pos.x,pos.y,to);
  851. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+14,GEOR13,12,zwykly,to);
  852. }
  853. void CSimpleWindow::show(SDL_Surface * to)
  854. {
  855. blitAt(bitmap,pos.x,pos.y,to);
  856. }
  857. CSimpleWindow::~CSimpleWindow()
  858. {
  859. if (bitmap)
  860. {
  861. SDL_FreeSurface(bitmap);
  862. bitmap=NULL;
  863. }
  864. }
  865. void CSelWindow::selectionChange(unsigned to)
  866. {
  867. for (unsigned i=0;i<components.size();i++)
  868. {
  869. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  870. if (!pom)
  871. continue;
  872. pom->select(i==to);
  873. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  874. }
  875. }
  876. CSelWindow::CSelWindow(const std::string &text, int player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, int askID)
  877. {
  878. ID = askID;
  879. for(int i=0;i<Buttons.size();i++)
  880. {
  881. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  882. if(!i && askID >= 0)
  883. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  884. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  885. }
  886. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  887. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  888. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  889. buttons.back()->callback += boost::bind(&ICallback::selectionMade,LOCPLINT->cb,0,askID);
  890. for(int i=0;i<comps.size();i++)
  891. {
  892. components.push_back(comps[i]);
  893. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  894. if(i<9)
  895. comps[i]->assignedKeys.insert(SDLK_1+i);
  896. }
  897. CMessage::drawIWindow(this,text,player,charperline);
  898. }
  899. void CSelWindow::madeChoice()
  900. {
  901. if(ID < 0)
  902. return;
  903. int ret = -1;
  904. for (int i=0;i<components.size();i++)
  905. {
  906. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  907. {
  908. ret = i;
  909. }
  910. }
  911. LOCPLINT->cb->selectionMade(ret+1,ID);
  912. }
  913. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  914. {
  915. bg=BitmapHandler::loadBitmap(name);
  916. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  917. pos.x=x;
  918. pos.y=y;
  919. if(maxw >= 0)
  920. pos.w = std::min(bg->w,maxw);
  921. else
  922. pos.w=bg->w;
  923. pos.h=bg->h;
  924. middlex=(pos.w/2)+x;
  925. middley=(bg->h/2)+y;
  926. }
  927. CStatusBar::~CStatusBar()
  928. {
  929. SDL_FreeSurface(bg);
  930. }
  931. void CStatusBar::clear()
  932. {
  933. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  934. {
  935. current="";
  936. show(screen);
  937. }
  938. }
  939. void CStatusBar::print(const std::string & text)
  940. {
  941. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  942. {
  943. current=text;
  944. show(LOCPLINT->topInt()==LOCPLINT->adventureInt ? screen : screen2); //if there are now windows opened, update statusbar on screen, else to cache surface
  945. }
  946. }
  947. void CStatusBar::show(SDL_Surface * to)
  948. {
  949. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  950. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),to,&pom);
  951. printAtMiddle(current,middlex,middley,GEOR13,zwykly,to);
  952. }
  953. std::string CStatusBar::getCurrent()
  954. {
  955. return current;
  956. }
  957. void CList::activate()
  958. {
  959. ClickableL::activate();
  960. ClickableR::activate();
  961. Hoverable::activate();
  962. KeyInterested::activate();
  963. MotionInterested::activate();
  964. };
  965. void CList::deactivate()
  966. {
  967. ClickableL::deactivate();
  968. ClickableR::deactivate();
  969. Hoverable::deactivate();
  970. KeyInterested::deactivate();
  971. MotionInterested::deactivate();
  972. };
  973. void CList::clickLeft(tribool down)
  974. {
  975. };
  976. CList::CList(int Size)
  977. :SIZE(Size)
  978. {
  979. }
  980. CHeroList::CHeroList(int Size)
  981. :CList(Size)
  982. {
  983. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  984. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  985. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  986. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  987. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  988. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  989. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  990. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  991. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  992. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  993. //32px per hero
  994. posmobx = pos.x+1;
  995. posmoby = pos.y+arrup->height+1;
  996. posporx = pos.x+mobile->width+2;
  997. pospory = pos.y+arrup->height;
  998. posmanx = pos.x+1+50+mobile->width;
  999. posmany = pos.y+arrup->height+1;
  1000. from = 0;
  1001. selected = -1;
  1002. pressed = indeterminate;
  1003. }
  1004. void CHeroList::init()
  1005. {
  1006. int w = pos.w+1, h = pos.h+4;
  1007. bg = CSDL_Ext::newSurface(w,h,screen);
  1008. SDL_BlitSurface(LOCPLINT->adventureInt->bg,&genRect(w,h,pos.x,pos.y),bg,&genRect(w,h,0,0));
  1009. }
  1010. void CHeroList::genList()
  1011. {
  1012. int howMany = LOCPLINT->cb->howManyHeroes();
  1013. for (int i=0;i<howMany;i++)
  1014. {
  1015. const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  1016. if(!h->inTownGarrison)
  1017. items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  1018. }
  1019. }
  1020. void CHeroList::select(int which)
  1021. {
  1022. if (which<0)
  1023. {
  1024. selected = which;
  1025. LOCPLINT->adventureInt->selection = NULL;
  1026. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  1027. draw(screen);
  1028. LOCPLINT->adventureInt->infoBar.draw(screen);
  1029. }
  1030. if (which>=items.size())
  1031. return;
  1032. selected = which;
  1033. //recalculationg path in case of something has changed on map
  1034. if(items[which].second)
  1035. {
  1036. CPath * newPath = LOCPLINT->cb->getPath(items[which].second->startPos(), items[which].second->endPos(), items[which].first);
  1037. delete items[which].second;
  1038. LOCPLINT->adventureInt->terrain.currentPath = items[which].second = newPath;
  1039. }
  1040. else
  1041. {
  1042. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  1043. }
  1044. LOCPLINT->adventureInt->select(items[which].first);
  1045. //recalculated and assigned
  1046. }
  1047. void CHeroList::clickLeft(tribool down)
  1048. {
  1049. if (down)
  1050. {
  1051. /***************************ARROWS*****************************************/
  1052. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1053. {
  1054. if(from>0)
  1055. {
  1056. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  1057. pressed = true;
  1058. }
  1059. return;
  1060. }
  1061. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1062. {
  1063. if(items.size()-from>SIZE)
  1064. {
  1065. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  1066. pressed = false;
  1067. }
  1068. return;
  1069. }
  1070. /***************************HEROES*****************************************/
  1071. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1072. hx-=pos.x;
  1073. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1074. int ny = hy/32;
  1075. if (ny>=SIZE || ny<0)
  1076. return;
  1077. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  1078. LOCPLINT->openHeroWindow(items[selected].first);//print hero screen
  1079. select(ny+from);
  1080. }
  1081. else
  1082. {
  1083. if (indeterminate(pressed))
  1084. return;
  1085. if (pressed) //up
  1086. {
  1087. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  1088. pressed = indeterminate;
  1089. if (!down)
  1090. {
  1091. from--;
  1092. if (from<0)
  1093. from=0;
  1094. draw(screen);
  1095. }
  1096. }
  1097. else if (!pressed) //down
  1098. {
  1099. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  1100. pressed = indeterminate;
  1101. if (!down)
  1102. {
  1103. from++;
  1104. //if (from<items.size()-5)
  1105. // from=items.size()-5;
  1106. draw(screen);
  1107. }
  1108. }
  1109. else
  1110. throw 0;
  1111. }
  1112. }
  1113. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1114. {
  1115. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1116. {
  1117. if (from>0)
  1118. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  1119. else
  1120. LOCPLINT->adventureInt->statusbar.clear();
  1121. return;
  1122. }
  1123. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1124. {
  1125. if ((items.size()-from) > SIZE)
  1126. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1127. else
  1128. LOCPLINT->adventureInt->statusbar.clear();
  1129. return;
  1130. }
  1131. //if not buttons then heroes
  1132. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1133. hx-=pos.x;
  1134. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1135. int ny = hy/32;
  1136. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  1137. {
  1138. LOCPLINT->adventureInt->statusbar.clear();
  1139. return;
  1140. }
  1141. std::vector<std::string> temp;
  1142. temp.push_back(items[from+ny].first->name);
  1143. temp.push_back(items[from+ny].first->type->heroClass->name);
  1144. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1145. //select(ny+from);
  1146. }
  1147. void CHeroList::clickRight(tribool down)
  1148. {
  1149. if (down)
  1150. {
  1151. /***************************ARROWS*****************************************/
  1152. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  1153. {
  1154. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1155. }
  1156. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  1157. {
  1158. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1159. }
  1160. else
  1161. {
  1162. //if not buttons then heroes
  1163. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1164. hx-=pos.x;
  1165. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1166. int ny = hy/32;
  1167. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  1168. {
  1169. return;
  1170. }
  1171. //show popup
  1172. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[items[from+ny].first->subID],
  1173. LOCPLINT->current->motion.x-graphics->heroWins[items[from+ny].first->subID]->w,
  1174. LOCPLINT->current->motion.y-graphics->heroWins[items[from+ny].first->subID]->h,
  1175. false);
  1176. LOCPLINT->pushInt(ip);
  1177. }
  1178. }
  1179. else
  1180. {
  1181. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1182. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1183. }
  1184. }
  1185. void CHeroList::hover (bool on)
  1186. {
  1187. }
  1188. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1189. {
  1190. }
  1191. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1192. {
  1193. if(toRemove) //remove specific hero
  1194. {
  1195. for (std::vector<std::pair<const CGHeroInstance*, CPath *> >::iterator i=items.begin(); i != items.end(); i++)
  1196. {
  1197. if(i->first == toRemove)
  1198. {
  1199. delete i->second;
  1200. items.erase(i);
  1201. break;
  1202. }
  1203. }
  1204. }
  1205. else
  1206. {
  1207. items.clear();
  1208. genList();
  1209. }
  1210. if(selected>=items.size())
  1211. select(items.size()-1);
  1212. if(items.size()==0)
  1213. LOCPLINT->adventureInt->townList.select(0);
  1214. else
  1215. select(selected);
  1216. }
  1217. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1218. {
  1219. int ser = -1;
  1220. for(int i=0; i<items.size() && ser<0; i++)
  1221. if(items[i].first->subID == which->subID)
  1222. ser = i;
  1223. ser -= from;
  1224. if(ser<0 || ser > SIZE) return;
  1225. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1226. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1227. }
  1228. void CHeroList::draw(SDL_Surface * to)
  1229. {
  1230. for (int iT=0+from;iT<SIZE+from;iT++)
  1231. {
  1232. int i = iT-from;
  1233. if (iT>=items.size())
  1234. {
  1235. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1236. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1237. blitAt(empty,posporx,pospory+i*32,to);
  1238. continue;
  1239. }
  1240. const CGHeroInstance *cur = items[iT].first;
  1241. int pom = cur->movement / 100;
  1242. if (pom>25) pom=25;
  1243. if (pom<0) pom=0;
  1244. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1245. pom = cur->mana / 5;
  1246. if (pom>25) pom=25;
  1247. if (pom<0) pom=0;
  1248. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1249. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1250. blitAt(temp,posporx,pospory+i*32,to);
  1251. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  1252. {
  1253. blitAt(selection,posporx,pospory+i*32,to);
  1254. }
  1255. //TODO: support for custom portraits
  1256. }
  1257. if (from>0)
  1258. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1259. else
  1260. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1261. if (items.size()-from > SIZE)
  1262. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1263. else
  1264. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1265. }
  1266. int CHeroList::getPosOfHero(const CArmedInstance* h)
  1267. {
  1268. return vstd::findPos(
  1269. items,h,
  1270. boost::bind(vstd::equal<std::pair<const CGHeroInstance*, CPath *>,
  1271. const CArmedInstance *,const CGHeroInstance*>,_1,
  1272. &std::pair<const CGHeroInstance*, CPath *>::first,_2));
  1273. }
  1274. CTownList::~CTownList()
  1275. {
  1276. delete arrup;
  1277. delete arrdo;
  1278. }
  1279. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1280. :CList(Size)
  1281. {
  1282. arrup = CDefHandler::giveDef(arrupg);
  1283. arrdo = CDefHandler::giveDef(arrdog);
  1284. pos.x = x;
  1285. pos.y = y;
  1286. pos.w = std::max(arrdo->width, arrup->width);
  1287. pos.h = arrdo->height + arrup->height + Size*32;
  1288. arrupp.x=x;
  1289. arrupp.y=y;
  1290. arrupp.w=arrup->width;
  1291. arrupp.h=arrup->height;
  1292. arrdop.x=x;
  1293. arrdop.y=y+arrup->height+32*SIZE;
  1294. arrdop.w=arrdo->width;
  1295. arrdop.h=arrdo->height;
  1296. posporx = arrdop.x;
  1297. pospory = arrupp.y + arrupp.h;
  1298. pressed = indeterminate;
  1299. from = 0;
  1300. selected = -1;
  1301. }
  1302. void CTownList::genList()
  1303. {
  1304. int howMany = LOCPLINT->cb->howManyTowns();
  1305. for (int i=0;i<howMany;i++)
  1306. {
  1307. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1308. }
  1309. }
  1310. void CTownList::select(int which)
  1311. {
  1312. if (which>=items.size())
  1313. return;
  1314. selected = which;
  1315. if(!fun.empty())
  1316. fun();
  1317. }
  1318. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1319. {
  1320. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1321. {
  1322. if (from>0)
  1323. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[306].first);
  1324. else
  1325. LOCPLINT->statusbar->clear();
  1326. return;
  1327. }
  1328. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1329. {
  1330. if ((items.size()-from) > SIZE)
  1331. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[307].first);
  1332. else
  1333. LOCPLINT->statusbar->clear();
  1334. return;
  1335. }
  1336. //if not buttons then towns
  1337. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1338. hx-=pos.x;
  1339. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1340. int ny = hy/32;
  1341. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  1342. {
  1343. LOCPLINT->statusbar->clear();
  1344. return;
  1345. };
  1346. LOCPLINT->statusbar->print(items[from+ny]->name);
  1347. }
  1348. void CTownList::clickLeft(tribool down)
  1349. {
  1350. if (down)
  1351. {
  1352. /***************************ARROWS*****************************************/
  1353. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1354. {
  1355. if(from>0)
  1356. {
  1357. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1358. pressed = true;
  1359. }
  1360. return;
  1361. }
  1362. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1363. {
  1364. if(items.size()-from > SIZE)
  1365. {
  1366. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1367. pressed = false;
  1368. }
  1369. return;
  1370. }
  1371. /***************************TOWNS*****************************************/
  1372. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1373. hx-=pos.x;
  1374. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1375. int ny = hy/32;
  1376. if (ny>SIZE || ny<0)
  1377. return;
  1378. if(LOCPLINT->topInt() == LOCPLINT->adventureInt
  1379. && (ny+from)==selected
  1380. && LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE
  1381. )
  1382. LOCPLINT->openTownWindow(items[selected]);//print town screen
  1383. else
  1384. select(ny+from);
  1385. }
  1386. else
  1387. {
  1388. if (indeterminate(pressed))
  1389. return;
  1390. if (pressed) //up
  1391. {
  1392. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1393. pressed = indeterminate;
  1394. if (!down)
  1395. {
  1396. from--;
  1397. if (from<0)
  1398. from=0;
  1399. draw(screenBuf);
  1400. }
  1401. }
  1402. else if (!pressed) //down
  1403. {
  1404. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1405. pressed = indeterminate;
  1406. if (!down)
  1407. {
  1408. from++;
  1409. //if (from<items.size()-5)
  1410. // from=items.size()-5;
  1411. draw(screenBuf);
  1412. }
  1413. }
  1414. else
  1415. throw 0;
  1416. }
  1417. }
  1418. void CTownList::clickRight(tribool down)
  1419. {
  1420. if (down)
  1421. {
  1422. /***************************ARROWS*****************************************/
  1423. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  1424. {
  1425. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1426. }
  1427. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  1428. {
  1429. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1430. }
  1431. //if not buttons then towns
  1432. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  1433. hx-=pos.x;
  1434. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1435. int ny = hy/32;
  1436. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  1437. {
  1438. return;
  1439. }
  1440. //show popup
  1441. CInfoPopup * ip = new CInfoPopup(
  1442. graphics->townWins[items[from+ny]->id],
  1443. LOCPLINT->current->motion.x-graphics->townWins[items[from+ny]->id]->w,
  1444. LOCPLINT->current->motion.y-graphics->townWins[items[from+ny]->id]->h,
  1445. false);
  1446. LOCPLINT->pushInt(ip);
  1447. }
  1448. else
  1449. {
  1450. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1451. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1452. }
  1453. }
  1454. void CTownList::hover (bool on)
  1455. {
  1456. }
  1457. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1458. {
  1459. }
  1460. void CTownList::draw(SDL_Surface * to)
  1461. {
  1462. for (int iT=0+from;iT<SIZE+from;iT++)
  1463. {
  1464. int i = iT-from;
  1465. if (iT>=items.size())
  1466. {
  1467. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1468. continue;
  1469. }
  1470. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32,to);
  1471. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  1472. {
  1473. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1474. }
  1475. }
  1476. if (from>0)
  1477. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1478. else
  1479. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1480. if (items.size()-from>SIZE)
  1481. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1482. else
  1483. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1484. }
  1485. CCreaturePic::CCreaturePic(CCreature *cre, bool Big)
  1486. :c(cre),big(Big)
  1487. {
  1488. anim = new CCreatureAnimation(cre->animDefName);
  1489. }
  1490. CCreaturePic::~CCreaturePic()
  1491. {
  1492. delete anim;
  1493. }
  1494. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  1495. {
  1496. SDL_Rect dst;
  1497. if(big)
  1498. {
  1499. blitAt(graphics->backgrounds[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1500. dst = genRect(130,100,x,y);
  1501. }
  1502. else
  1503. {
  1504. blitAt(graphics->backgroundsm[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1505. dst = genRect(120,100,x,y);
  1506. }
  1507. if(c->isDoubleWide())
  1508. x-=15;
  1509. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  1510. }
  1511. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  1512. {
  1513. //TODO: write
  1514. return NULL;
  1515. }
  1516. void CRecruitmentWindow::close()
  1517. {
  1518. LOCPLINT->popIntTotally(this);
  1519. }
  1520. void CRecruitmentWindow::Max()
  1521. {
  1522. slider->moveTo(slider->amount);
  1523. }
  1524. void CRecruitmentWindow::Buy()
  1525. {
  1526. recruit(creatures[which].ID,slider->value);
  1527. close();
  1528. }
  1529. void CRecruitmentWindow::Cancel()
  1530. {
  1531. close();
  1532. }
  1533. void CRecruitmentWindow::sliderMoved(int to)
  1534. {
  1535. buy->block(!to);
  1536. }
  1537. void CRecruitmentWindow::clickLeft(tribool down)
  1538. {
  1539. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1540. for(int i=0;i<creatures.size();i++)
  1541. {
  1542. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1543. {
  1544. which = i;
  1545. int newAmount = std::min(amounts[i],creatures[i].amount);
  1546. slider->amount = newAmount;
  1547. if(slider->value > newAmount)
  1548. slider->moveTo(newAmount);
  1549. else
  1550. slider->moveTo(slider->value);
  1551. curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1552. for(int j=0;j<creatures.size();j++)
  1553. {
  1554. if(which==j)
  1555. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  1556. else
  1557. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  1558. curx += 120;
  1559. }
  1560. break;
  1561. }
  1562. curx += 120;
  1563. }
  1564. }
  1565. void CRecruitmentWindow::clickRight( boost::logic::tribool down )
  1566. {
  1567. if(down)
  1568. {
  1569. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1570. for(int i=0;i<creatures.size();i++)
  1571. {
  1572. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  1573. {
  1574. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID,0,0,NULL,NULL,NULL,NULL);
  1575. LOCPLINT->pushInt(popup);
  1576. break;
  1577. }
  1578. curx += 120;
  1579. }
  1580. }
  1581. }
  1582. void CRecruitmentWindow::activate()
  1583. {
  1584. ClickableL::activate();
  1585. ClickableR::activate();
  1586. buy->activate();
  1587. max->activate();
  1588. cancel->activate();
  1589. slider->activate();
  1590. LOCPLINT->statusbar = bar;
  1591. }
  1592. void CRecruitmentWindow::deactivate()
  1593. {
  1594. ClickableL::deactivate();
  1595. ClickableR::deactivate();
  1596. buy->deactivate();
  1597. max->deactivate();
  1598. cancel->deactivate();
  1599. slider->deactivate();
  1600. }
  1601. void CRecruitmentWindow::show(SDL_Surface * to)
  1602. {
  1603. static char animCounter=0; //animation counter - for determining appropriate animation frame to be shown
  1604. blitAt(bitmap,pos.x,pos.y,to);
  1605. buy->show(to);
  1606. max->show(to);
  1607. cancel->show(to);
  1608. slider->show(to);
  1609. char pom[15];
  1610. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1611. printAtMiddle(pom,pos.x+205,pos.y+252,GEOR13,zwykly,to);
  1612. SDL_itoa(slider->value,pom,10); //recruit
  1613. printAtMiddle(pom,pos.x+279,pos.y+252,GEOR13,zwykly,to);
  1614. printAtMiddle(CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creatures[which].ID].namePl,pos.x+243,pos.y+32,GEOR16,tytulowy,to); //eg "Recruit Dragon flies"
  1615. int curx = pos.x+115-creatures[which].res.size()*16;
  1616. for(int i=0;i<creatures[which].res.size();i++)
  1617. {
  1618. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,to);
  1619. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,to);
  1620. SDL_itoa(creatures[which].res[i].second,pom,10);
  1621. printAtMiddle(pom,curx+12,pos.y+286,GEOR13,zwykly,to);
  1622. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1623. printAtMiddle(pom,curx+12+258,pos.y+286,GEOR13,zwykly,to);
  1624. curx+=32;
  1625. }
  1626. curx = pos.x + 192 + 102 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1627. for(int i=0; i<creatures.size(); ++i)
  1628. {
  1629. creatures[i].pic->blitPic(to, curx-50, pos.y+130-65, !(animCounter%4));
  1630. curx += 120;
  1631. }
  1632. ++animCounter;
  1633. bar->show(to);
  1634. }
  1635. CRecruitmentWindow::CRecruitmentWindow(const std::vector<std::pair<int,int> > &Creatures, const boost::function<void(int,int)> &Recruit) //creatures - pairs<creature_ID,amount>
  1636. :recruit(Recruit), which(0)
  1637. {
  1638. creatures.resize(Creatures.size());
  1639. amounts.resize(Creatures.size());
  1640. for(int i=0;i<creatures.size();i++)
  1641. {
  1642. creatures[i].amount = Creatures[i].second;
  1643. creatures[i].ID = Creatures[i].first;
  1644. for(int j=0;j<CGI->creh->creatures[Creatures[i].first].cost.size();j++)
  1645. if(CGI->creh->creatures[Creatures[i].first].cost[j])
  1646. creatures[i].res.push_back(std::make_pair(j,CGI->creh->creatures[Creatures[i].first].cost[j]));
  1647. creatures[i].pic = new CCreaturePic(&CGI->creh->creatures[Creatures[i].first]);
  1648. amounts[i] = CGI->creh->creatures[Creatures[i].first].maxAmount(LOCPLINT->cb->getResourceAmount());
  1649. }
  1650. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  1651. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1652. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo //it wouldn't work on 8bpp map
  1653. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1654. SDL_FreeSurface(hhlp);
  1655. pos.x = screen->w/2 - bitmap->w/2;
  1656. pos.y = screen->h/2 - bitmap->h/2;
  1657. pos.w = bitmap->w;
  1658. pos.h = bitmap->h;
  1659. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  1660. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  1661. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  1662. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  1663. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecruitmentWindow::sliderMoved,this, _1),1,std::min(amounts[0],creatures[0].amount),0,true);
  1664. std::string pom;
  1665. printAtMiddle(CGI->generaltexth->allTexts[346],113,231,GEOR13,zwykly,bitmap); //cost per troop t
  1666. printAtMiddle(CGI->generaltexth->allTexts[465],205,231,GEOR13,zwykly,bitmap); //available t
  1667. printAtMiddle(CGI->generaltexth->allTexts[16],279,231,GEOR13,zwykly,bitmap); //recruit t
  1668. printAtMiddle(CGI->generaltexth->allTexts[466],373,231,GEOR13,zwykly,bitmap); //total cost t
  1669. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  1670. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  1671. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  1672. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  1673. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  1674. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  1675. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  1676. //border for creatures
  1677. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  1678. for(int i=0;i<creatures.size();i++)
  1679. {
  1680. creatures[i].pos.x = curx+1;
  1681. creatures[i].pos.y = 65;
  1682. creatures[i].pos.w = 100;
  1683. creatures[i].pos.h = 130;
  1684. if(which==i)
  1685. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  1686. else
  1687. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  1688. curx += 120;
  1689. }
  1690. if(!creatures[0].amount || !amounts[0])
  1691. {
  1692. max->block(true);
  1693. slider->block(true);
  1694. }
  1695. //buy->block(true); //not needed, will be blocked by initing slider on 0
  1696. }
  1697. CRecruitmentWindow::~CRecruitmentWindow()
  1698. {
  1699. for(int i=0;i<creatures.size();i++)
  1700. {
  1701. delete creatures[i].pic;
  1702. }
  1703. delete max;
  1704. delete buy;
  1705. delete cancel;
  1706. SDL_FreeSurface(bitmap);
  1707. delete slider;
  1708. delete bar;
  1709. }
  1710. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1711. {
  1712. last = Last;
  1713. which = 1;
  1714. c=cid;
  1715. slider = NULL;
  1716. gar = Owner;
  1717. bitmap = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1718. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  1719. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1720. pos.x = screen->w/2 - bitmap->w/2;
  1721. pos.y = screen->h/2 - bitmap->h/2;
  1722. pos.w = bitmap->w;
  1723. pos.h = bitmap->h;
  1724. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1725. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1726. int sliderPositions = max - (last>=0) - (last==2);
  1727. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),1,sliderPositions,val,true);
  1728. a1 = max-val;
  1729. a2 = val;
  1730. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  1731. anim->anim->setType(1);
  1732. std::string title = CGI->generaltexth->allTexts[256];
  1733. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  1734. printAtMiddle(title,150,34,GEOR16,tytulowy,bitmap);
  1735. }
  1736. CSplitWindow::~CSplitWindow() //d-tor
  1737. {
  1738. SDL_FreeSurface(bitmap);
  1739. delete ok;
  1740. delete cancel;
  1741. delete slider;
  1742. delete anim;
  1743. }
  1744. void CSplitWindow::activate()
  1745. {
  1746. ClickableL::activate();
  1747. KeyInterested::activate();
  1748. ok->activate();
  1749. cancel->activate();
  1750. slider->activate();
  1751. }
  1752. void CSplitWindow::deactivate()
  1753. {
  1754. ClickableL::deactivate();
  1755. KeyInterested::deactivate();
  1756. ok->deactivate();
  1757. cancel->deactivate();
  1758. slider->deactivate();
  1759. }
  1760. void CSplitWindow::split()
  1761. {
  1762. gar->splitStacks(a2);
  1763. close();
  1764. }
  1765. void CSplitWindow::close()
  1766. {
  1767. LOCPLINT->popIntTotally(this);
  1768. }
  1769. void CSplitWindow::sliderMoved(int to)
  1770. {
  1771. int all = a1+a2;
  1772. a2 = to + (last==1 || last==2);
  1773. if(slider)
  1774. a1 = all - a2;
  1775. }
  1776. void CSplitWindow::show(SDL_Surface * to)
  1777. {
  1778. blitAt(bitmap,pos.x,pos.y,to);
  1779. ok->show(to);
  1780. cancel->show(to);
  1781. slider->show(to);
  1782. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,GEOR16,zwykly,to);
  1783. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,GEOR16,zwykly,to);
  1784. anim->blitPic(to,pos.x+20,pos.y+54,false);
  1785. anim->blitPic(to,pos.x+177,pos.y+54,false);
  1786. }
  1787. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1788. {
  1789. if(key.state != SDL_PRESSED)
  1790. return;
  1791. int &cur = (which ? a2 : a1),
  1792. &sec = (which ? a1 : a2),
  1793. ncur = cur;
  1794. if (key.keysym.sym == SDLK_BACKSPACE)
  1795. {
  1796. ncur /= 10;
  1797. }
  1798. else if(key.keysym.sym == SDLK_TAB)
  1799. {
  1800. which = !which;
  1801. }
  1802. else
  1803. {
  1804. int number = key.keysym.sym - SDLK_0;
  1805. if (number < 0 || number > 9) //not a number pressed
  1806. {
  1807. return;
  1808. }
  1809. ncur = cur*10 + number;
  1810. }
  1811. int delta = ncur - cur;
  1812. if(delta > sec)
  1813. {
  1814. cur += sec;
  1815. sec = 0;
  1816. }
  1817. slider->moveTo(which ? ncur : a1+a2-ncur);
  1818. }
  1819. void CSplitWindow::clickLeft( boost::logic::tribool down )
  1820. {
  1821. if(down)
  1822. {
  1823. Point click(LOCPLINT->current->motion.x,LOCPLINT->current->motion.y);
  1824. click -= pos.topLeft();
  1825. if(Rect(19,216,105,40).isIn(click)) //left picture
  1826. which = 0;
  1827. else if(Rect(175,216,105,40).isIn(click)) //right picture
  1828. which = 1;
  1829. }
  1830. }
  1831. void CCreInfoWindow::show(SDL_Surface * to)
  1832. {
  1833. char pom[15];
  1834. blitAt(bitmap,pos.x,pos.y,to);
  1835. anim->blitPic(to,pos.x+21,pos.y+48,(type) && !(anf%4));
  1836. if(++anf==4)
  1837. anf=0;
  1838. if(count.size())
  1839. printTo(count.c_str(),pos.x+114,pos.y+174,GEOR16,zwykly,to);
  1840. if(upgrade)
  1841. upgrade->show(to);
  1842. if(dismiss)
  1843. dismiss->show(to);
  1844. if(ok)
  1845. ok->show(to);
  1846. }
  1847. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  1848. :type(Type),dsm(Dsm),dismiss(0),upgrade(0),ok(0)
  1849. {
  1850. //active = false;
  1851. anf = 0;
  1852. c = &CGI->creh->creatures[Cid];
  1853. bitmap = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  1854. pos.x = screen->w/2 - bitmap->w/2;
  1855. pos.y = screen->h/2 - bitmap->h/2;
  1856. pos.w = bitmap->w;
  1857. pos.h = bitmap->h;
  1858. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  1859. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1860. anim = new CCreaturePic(c);
  1861. if(!type) anim->anim->setType(2);
  1862. char pom[75];int hlp=0;
  1863. if(creatureCount)
  1864. {
  1865. SDL_itoa(creatureCount,pom,10);
  1866. count = pom;
  1867. }
  1868. printAtMiddle(c->namePl,149,30,GEOR13,tytulowy,bitmap); //creature name
  1869. //atttack
  1870. printAt(CGI->generaltexth->primarySkillNames[0],155,48,GEOR13,zwykly,bitmap);
  1871. SDL_itoa(c->attack,pom,10);
  1872. if(State && State->attackBonus)
  1873. {
  1874. int hlp;
  1875. if(c->attack > 0)
  1876. hlp = log10f(c->attack)+2;
  1877. else
  1878. hlp = 2;
  1879. pom[hlp-1] = ' '; pom[hlp] = '(';
  1880. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  1881. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  1882. pom[hlp] = ')'; pom[hlp+1] = '\0';
  1883. }
  1884. printToWR(pom,276,61,GEOR13,zwykly,bitmap);
  1885. //defense
  1886. printAt(CGI->generaltexth->primarySkillNames[1],155,67,GEOR13,zwykly,bitmap);
  1887. SDL_itoa(c->defence,pom,10);
  1888. if(State && State->defenseBonus)
  1889. {
  1890. int hlp;
  1891. if(c->defence > 0)
  1892. hlp = log10f(c->defence)+2;
  1893. else
  1894. hlp = 2;
  1895. pom[hlp-1] = ' '; pom[hlp] = '(';
  1896. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  1897. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  1898. pom[hlp] = ')'; pom[hlp+1] = '\0';
  1899. }
  1900. printToWR(pom,276,80,GEOR13,zwykly,bitmap);
  1901. //shots
  1902. if(c->shots)
  1903. {
  1904. printAt(CGI->generaltexth->allTexts[198],155,86,GEOR13,zwykly,bitmap);
  1905. if(State)
  1906. sprintf(pom,"%d(%d)",c->shots,State->shotsLeft);
  1907. else
  1908. SDL_itoa(c->shots,pom,10);
  1909. printToWR(pom,276,99,GEOR13,zwykly,bitmap);
  1910. }
  1911. //damage
  1912. printAt(CGI->generaltexth->allTexts[199],155,105,GEOR13,zwykly,bitmap);
  1913. SDL_itoa(c->damageMin,pom,10);
  1914. if(c->damageMin > 0)
  1915. hlp = log10f(c->damageMin)+2;
  1916. else
  1917. hlp = 2;
  1918. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  1919. SDL_itoa(c->damageMax,pom+hlp+2,10);
  1920. printToWR(pom,276,118,GEOR13,zwykly,bitmap);
  1921. //health
  1922. printAt(CGI->generaltexth->allTexts[388],155,124,GEOR13,zwykly,bitmap);
  1923. SDL_itoa(c->hitPoints,pom,10);
  1924. printToWR(pom,276,137,GEOR13,zwykly,bitmap);
  1925. //remaining health
  1926. if(State && State->currentHealth)
  1927. {
  1928. printAt(CGI->generaltexth->allTexts[200],155,143,GEOR13,zwykly,bitmap);
  1929. SDL_itoa(State->currentHealth,pom,10);
  1930. printToWR(pom,276,156,GEOR13,zwykly,bitmap);
  1931. }
  1932. //speed
  1933. printAt(CGI->generaltexth->zelp[441].first,155,162,GEOR13,zwykly,bitmap);
  1934. SDL_itoa(c->speed,pom,10);
  1935. if(State && State->speedBonus)
  1936. {
  1937. int hlp;
  1938. if(c->speed > 0)
  1939. hlp = log10f(c->speed)+2;
  1940. else
  1941. hlp = 2;
  1942. pom[hlp-1] = ' '; pom[hlp] = '(';
  1943. SDL_itoa(c->speed + State->speedBonus, pom+hlp+1, 10);
  1944. hlp += 2+(int)log10f(c->speed + State->speedBonus);
  1945. pom[hlp] = ')'; pom[hlp+1] = '\0';
  1946. }
  1947. printToWR(pom,276,175,GEOR13,zwykly,bitmap);
  1948. //luck and morale
  1949. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  1950. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  1951. //print abilities text - if r-click popup
  1952. if(type)
  1953. {
  1954. if(Upg && ui)
  1955. {
  1956. bool enough = true;
  1957. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  1958. {
  1959. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  1960. enough = false;
  1961. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  1962. }
  1963. if(enough)
  1964. {
  1965. CFunctionList<void()> fs[2];
  1966. fs[0] += Upg;
  1967. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  1968. CFunctionList<void()> cfl;
  1969. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false);
  1970. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF",SDLK_u);
  1971. }
  1972. else
  1973. {
  1974. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  1975. upgrade->callback.funcs.clear();
  1976. upgrade->bitmapOffset = 2;
  1977. }
  1978. }
  1979. if(Dsm)
  1980. {
  1981. CFunctionList<void()> fs[2];
  1982. //on dismiss confirmed
  1983. fs[0] += Dsm; //dismiss
  1984. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  1985. CFunctionList<void()> cfl;
  1986. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  1987. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF",SDLK_d);
  1988. }
  1989. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF",SDLK_RETURN);
  1990. }
  1991. else
  1992. {
  1993. printAtWB(c->abilityText,17,231,GEOR13,35,zwykly,bitmap);
  1994. }
  1995. //spell effects
  1996. if(State)
  1997. {
  1998. int printed=0; //how many effect pics have been printed
  1999. for(std::set<int>::const_iterator it = State->effects.begin(); it!=State->effects.end(); ++it)
  2000. {
  2001. blitAt(graphics->spellEffectsPics->ourImages[*it + 1].bitmap, 127 + 52 * printed, 186, bitmap);
  2002. ++printed;
  2003. if(printed >= 3)
  2004. {
  2005. break;
  2006. }
  2007. }
  2008. }
  2009. }
  2010. CCreInfoWindow::~CCreInfoWindow()
  2011. {
  2012. SDL_FreeSurface(bitmap);
  2013. delete anim;
  2014. delete upgrade;
  2015. delete ok;
  2016. delete dismiss;
  2017. for(int i=0; i<upgResCost.size();i++)
  2018. delete upgResCost[i];
  2019. }
  2020. void CCreInfoWindow::activate()
  2021. {
  2022. //if(active) return;
  2023. //active = true;
  2024. if(!type)
  2025. ClickableR::activate();
  2026. if(ok)
  2027. ok->activate();
  2028. if(dismiss)
  2029. dismiss->activate();
  2030. if(upgrade)
  2031. upgrade->activate();
  2032. }
  2033. void CCreInfoWindow::close()
  2034. {
  2035. LOCPLINT->popIntTotally(this);
  2036. }
  2037. void CCreInfoWindow::clickRight(boost::logic::tribool down)
  2038. {
  2039. if(down)
  2040. return;
  2041. close();
  2042. }
  2043. void CCreInfoWindow::dismissF()
  2044. {
  2045. dsm();
  2046. close();
  2047. }
  2048. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  2049. {
  2050. }
  2051. void CCreInfoWindow::deactivate()
  2052. {
  2053. //if(!active) return;
  2054. //active = false;
  2055. if(!type)
  2056. ClickableR::deactivate();
  2057. if(ok)
  2058. ok->deactivate();
  2059. if(dismiss)
  2060. dismiss->deactivate();
  2061. if(upgrade)
  2062. upgrade->deactivate();
  2063. }
  2064. void CLevelWindow::close()
  2065. {
  2066. for(int i=0;i<comps.size();i++)
  2067. {
  2068. if(comps[i]->selected)
  2069. {
  2070. cb(i);
  2071. break;
  2072. }
  2073. }
  2074. LOCPLINT->popIntTotally(this);
  2075. LOCPLINT->showingDialog->setn(false);
  2076. }
  2077. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  2078. {
  2079. LOCPLINT->showingDialog->setn(true);
  2080. heroType = hero->subID;
  2081. cb = callback;
  2082. for(int i=0;i<skills.size();i++)
  2083. {
  2084. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  2085. comps.back()->assignedKeys.insert(SDLK_1 + i);
  2086. }
  2087. bitmap = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  2088. graphics->blueToPlayersAdv(bitmap,hero->tempOwner);
  2089. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2090. pos.x = screen->w/2 - bitmap->w/2;
  2091. pos.y = screen->h/2 - bitmap->h/2;
  2092. pos.w = bitmap->w;
  2093. pos.h = bitmap->h;
  2094. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  2095. //draw window
  2096. char buf[100], buf2[100];
  2097. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  2098. sprintf(buf,buf2,hero->name.c_str());
  2099. printAtMiddle(buf,192,35,GEOR16,zwykly,bitmap);
  2100. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  2101. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  2102. printAtMiddle(buf,192,162,GEOR16,zwykly,bitmap);
  2103. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  2104. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,252,GEOR16,zwykly,bitmap);
  2105. SDL_Surface * ort = TTF_RenderText_Blended(GEOR16,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  2106. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+ort->w) )/2;
  2107. for(int i=0;i<comps.size();i++)
  2108. {
  2109. comps[i]->pos.x = curx+pos.x;
  2110. comps[i]->pos.y = 326+pos.y;
  2111. if( i < (comps.size()-1) )
  2112. {
  2113. curx += 44 + 18; //skill width + margin to "or"
  2114. blitAt(ort,curx,346,bitmap);
  2115. curx += ort->w + 18;
  2116. }
  2117. }
  2118. if(comps.size() > 1)
  2119. {
  2120. ok->block(true);
  2121. }
  2122. else if(comps.size() == 1)
  2123. {
  2124. comps[0]->select(true);
  2125. }
  2126. SDL_FreeSurface(ort);
  2127. }
  2128. void CLevelWindow::selectionChanged(unsigned to)
  2129. {
  2130. if(ok->blocked)
  2131. ok->block(false);
  2132. for(int i=0;i<comps.size();i++)
  2133. if(i==to)
  2134. comps[i]->select(true);
  2135. else
  2136. comps[i]->select(false);
  2137. }
  2138. CLevelWindow::~CLevelWindow()
  2139. {
  2140. delete ok;
  2141. for(int i=0;i<comps.size();i++)
  2142. delete comps[i];
  2143. SDL_FreeSurface(bitmap);
  2144. }
  2145. void CLevelWindow::activate()
  2146. {
  2147. ok->activate();
  2148. for(int i=0;i<comps.size();i++)
  2149. comps[i]->activate();
  2150. }
  2151. void CLevelWindow::deactivate()
  2152. {
  2153. ok->deactivate();
  2154. for(int i=0;i<comps.size();i++)
  2155. comps[i]->deactivate();
  2156. }
  2157. void CLevelWindow::show(SDL_Surface * to)
  2158. {
  2159. blitAt(bitmap,pos.x,pos.y,to);
  2160. blitAt(graphics->portraitLarge[heroType],170+pos.x,66+pos.y,to);
  2161. ok->show(to);
  2162. for(int i=0;i<comps.size();i++)
  2163. comps[i]->show(to);
  2164. }
  2165. void CMinorResDataBar::show(SDL_Surface * to)
  2166. {
  2167. blitAt(bg,pos.x,pos.y,to);
  2168. char buf[30];
  2169. for (int i=0;i<7;i++)
  2170. {
  2171. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2172. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly,to);
  2173. }
  2174. std::vector<std::string> temp;
  2175. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  2176. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  2177. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  2178. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  2179. CGI->generaltexth->allTexts[62]
  2180. +": %s, "
  2181. + CGI->generaltexth->allTexts[63]
  2182. + ": %s, "
  2183. + CGI->generaltexth->allTexts[64]
  2184. + ": %s",temp)
  2185. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly,to);
  2186. }
  2187. CMinorResDataBar::CMinorResDataBar()
  2188. {
  2189. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  2190. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2191. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2192. pos.x = 7;
  2193. pos.y = 575;
  2194. pos.w = bg->w;
  2195. pos.h = bg->h;
  2196. }
  2197. CMinorResDataBar::~CMinorResDataBar()
  2198. {
  2199. SDL_FreeSurface(bg);
  2200. }
  2201. SDL_Surface * CCustomImgComponent::getImg()
  2202. {
  2203. return bmp;
  2204. }
  2205. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  2206. :bmp(sur), free(freeSur)
  2207. {
  2208. init(Type,Subtype,Val);
  2209. }
  2210. CCustomImgComponent::~CCustomImgComponent()
  2211. {
  2212. if(free)
  2213. SDL_FreeSurface(bmp);
  2214. }
  2215. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  2216. {
  2217. left = Left;
  2218. type = Type;
  2219. id = ID;
  2220. }
  2221. void CMarketplaceWindow::CTradeableItem::show(SDL_Surface * to)
  2222. {
  2223. SDL_Surface *hlp = getSurface();
  2224. blitAt(hlp,pos.x+19,pos.y+9,to);
  2225. }
  2226. void CMarketplaceWindow::CTradeableItem::clickLeft( boost::logic::tribool down )
  2227. {
  2228. CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(LOCPLINT->topInt());
  2229. assert(mw);
  2230. if(down)
  2231. {
  2232. if(left)
  2233. {
  2234. if(mw->hLeft != this)
  2235. mw->hLeft = this;
  2236. else
  2237. return;
  2238. }
  2239. else
  2240. {
  2241. if(mw->hRight != this)
  2242. mw->hRight = this;
  2243. else
  2244. return;
  2245. }
  2246. mw->selectionChanged(left);
  2247. }
  2248. }
  2249. void CMarketplaceWindow::CTradeableItem::activate()
  2250. {
  2251. ClickableL::activate();
  2252. }
  2253. void CMarketplaceWindow::CTradeableItem::deactivate()
  2254. {
  2255. ClickableL::deactivate();
  2256. }
  2257. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  2258. {
  2259. switch(type)
  2260. {
  2261. case 0:
  2262. return graphics->resources32->ourImages[id].bitmap;
  2263. case 1:
  2264. return graphics->artDefs->ourImages[id].bitmap;
  2265. default:
  2266. return NULL;
  2267. }
  2268. }
  2269. static void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  2270. {
  2271. i.resize(amount);
  2272. for(int j=0;j<amount;j++)
  2273. {
  2274. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  2275. i[j]->pos = p[j];
  2276. }
  2277. }
  2278. void CMarketplaceWindow::setMode( int mode )
  2279. {
  2280. std::vector<SDL_Rect> lpos, rpos;
  2281. clear();
  2282. switch(mode)
  2283. {
  2284. case 0:
  2285. {
  2286. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  2287. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  2288. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  2289. bg = SDL_ConvertSurface(bg2,screen->format,0);
  2290. SDL_FreeSurface(bg2);
  2291. lpos += genRect(66,69,39,180), genRect(66,69,122,180), genRect(66,69,204,180),
  2292. genRect(66,69,39,259), genRect(66,69,122,259), genRect(66,69,204,259),
  2293. genRect(66,69,122,338);
  2294. for(int i=0;i<lpos.size();i++)
  2295. {
  2296. lpos[i].x += pos.x;
  2297. lpos[i].y += pos.y;
  2298. rpos.push_back(lpos[i]);
  2299. rpos[rpos.size()-1].x += 288;
  2300. }
  2301. initItems(left,lpos,0,7,true,NULL);
  2302. initItems(right,rpos,0,7,false,NULL);
  2303. printAtMiddle(CGI->generaltexth->allTexts[158],303,28,GEORXX,tytulowy,bg); //title
  2304. printAtMiddle(CGI->generaltexth->allTexts[270],158,148,GEOR13,zwykly,bg); //kingdom res.
  2305. printAtMiddle(CGI->generaltexth->allTexts[168],450,148,GEOR13,zwykly,bg); //available for trade
  2306. }
  2307. }
  2308. }
  2309. void CMarketplaceWindow::clear()
  2310. {
  2311. for(int i=0;i<left.size();i++)
  2312. delete left[i];
  2313. for(int i=0;i<right.size();i++)
  2314. delete right[i];
  2315. left.clear();
  2316. right.clear();
  2317. SDL_FreeSurface(bg);
  2318. }
  2319. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  2320. {
  2321. mode = Mode;
  2322. bg = NULL;
  2323. ok = max = deal = NULL;
  2324. pos.x = screen->w/2 - 300;
  2325. pos.y = screen->h/2 - 296;
  2326. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  2327. setMode(mode);
  2328. hLeft = hRight = NULL;
  2329. ok = new AdventureMapButton("","",boost::bind(&CPlayerInterface::popIntTotally,LOCPLINT,this),pos.x+516,pos.y+520,"IOK6432.DEF",SDLK_RETURN);
  2330. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  2331. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  2332. max->block(true);
  2333. deal->block(true);
  2334. }
  2335. CMarketplaceWindow::~CMarketplaceWindow()
  2336. {
  2337. clear();
  2338. delete slider;
  2339. }
  2340. void CMarketplaceWindow::show(SDL_Surface * to)
  2341. {
  2342. blitAt(bg,pos,to);
  2343. if(hRight)
  2344. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2345. if(hLeft)
  2346. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2347. ok->show(to);
  2348. deal->show(to);
  2349. max->show(to);
  2350. slider->show(to);
  2351. for(int i=0;i<left.size();i++)
  2352. left[i]->show(to);
  2353. for(int i=0;i<right.size();i++)
  2354. right[i]->show(to);
  2355. if(mode==0)
  2356. {
  2357. char buf[15];
  2358. for(int i=0;i<left.size();i++)
  2359. {
  2360. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2361. printAtMiddle(buf,left[i]->pos.x+35,left[i]->pos.y+56,GEOR13,zwykly,to);
  2362. }
  2363. if(hLeft) //print prices
  2364. {
  2365. for(int i=0; i<right.size();i++)
  2366. {
  2367. if(right[i]->id != hLeft->id)
  2368. printAtMiddle(rSubs[i],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly,to);
  2369. else
  2370. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly,to);
  2371. }
  2372. }
  2373. if(hLeft && hRight && hLeft->id!= hRight->id)
  2374. {
  2375. blitAt(hLeft->getSurface(),pos.x+142,pos.y+457,to);
  2376. blitAt(hRight->getSurface(),pos.x+430,pos.y+457,to);
  2377. SDL_itoa(slider->value * r1,buf,10);
  2378. printAtMiddle(buf,pos.x+158,pos.y+504,GEOR13,zwykly,to);
  2379. SDL_itoa(slider->value * r2,buf,10);
  2380. printAtMiddle(buf,pos.x+446,pos.y+504,GEOR13,zwykly,to);
  2381. }
  2382. }
  2383. }
  2384. void CMarketplaceWindow::activate()
  2385. {
  2386. for(int i=0;i<left.size();i++)
  2387. left[i]->activate();
  2388. for(int i=0;i<right.size();i++)
  2389. right[i]->activate();
  2390. ok->activate();
  2391. max->activate();
  2392. deal->activate();
  2393. slider->activate();
  2394. }
  2395. void CMarketplaceWindow::deactivate()
  2396. {
  2397. for(int i=0;i<left.size();i++)
  2398. left[i]->deactivate();
  2399. for(int i=0;i<right.size();i++)
  2400. right[i]->deactivate();
  2401. ok->deactivate();
  2402. max->deactivate();
  2403. deal->deactivate();
  2404. slider->deactivate();
  2405. }
  2406. void CMarketplaceWindow::setMax()
  2407. {
  2408. slider->moveTo(slider->amount);
  2409. }
  2410. void CMarketplaceWindow::makeDeal()
  2411. {
  2412. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  2413. slider->moveTo(0);
  2414. hLeft = NULL;
  2415. selectionChanged(true);
  2416. }
  2417. void CMarketplaceWindow::sliderMoved( int to )
  2418. {
  2419. }
  2420. void CMarketplaceWindow::selectionChanged(bool side)
  2421. {
  2422. if(hLeft && hRight && hLeft->id!= hRight->id)
  2423. {
  2424. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  2425. slider->amount = LOCPLINT->cb->getResourceAmount(hLeft->id) / r1;
  2426. slider->moveTo(0);
  2427. max->block(false);
  2428. deal->block(false);
  2429. }
  2430. else
  2431. {
  2432. max->block(true);
  2433. deal->block(true);
  2434. slider->amount = 0;
  2435. slider->moveTo(0);
  2436. }
  2437. if(side && hLeft) //left selection changed, recalculate offers
  2438. {
  2439. rSubs.clear();
  2440. rSubs.resize(right.size());
  2441. int h1, h2;
  2442. for(int i=0;i<right.size();i++)
  2443. {
  2444. std::ostringstream oss;
  2445. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  2446. oss << h2;
  2447. if(h1!=1)
  2448. oss << "/" << h1;
  2449. rSubs[i] = oss.str();
  2450. }
  2451. }
  2452. }
  2453. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  2454. {
  2455. this->pos = pos;
  2456. background = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  2457. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2458. //printing texts
  2459. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 240, 32, GEOR16, tytulowy, background); //window title
  2460. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 65, GEOR16, tytulowy, background); //hero speed
  2461. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 131, GEOR16, tytulowy, background); //enemy speed
  2462. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 197, GEOR16, tytulowy, background); //map scroll speed
  2463. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 263, GEOR16, tytulowy, background); //video quality
  2464. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 348, GEOR16, tytulowy, background); //music volume
  2465. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 414, GEOR16, tytulowy, background); //effects volume
  2466. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 283, 57, GEOR16, zwykly, background); //show move path
  2467. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 283, 89, GEOR16, zwykly, background); //show hero reminder
  2468. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 283, 121, GEOR16, zwykly, background); //quick combat
  2469. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 283, 153, GEOR16, zwykly, background); //video subtitles
  2470. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 283, 185, GEOR16, zwykly, background); //town building outlines
  2471. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 283, 217, GEOR16, zwykly, background); //spell book animation
  2472. //setting up buttons
  2473. save = new AdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::bsavef, this), 516, 354, "SOSAVE.DEF", SDLK_s);
  2474. std::swap(save->imgs[0][0], save->imgs[0][1]);
  2475. quitGame = new AdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), 405, 471, "soquit.def", SDLK_q);
  2476. std::swap(quitGame->imgs[0][0], quitGame->imgs[0][1]);
  2477. backToMap = new AdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), 516, 471, "soretrn.def", SDLK_RETURN);
  2478. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  2479. std::swap(backToMap->imgs[0][0], backToMap->imgs[0][1]);
  2480. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  2481. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].first),CGI->generaltexth->zelp[349].second, "sysopb1.def", 187, 134, 1);
  2482. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].first),CGI->generaltexth->zelp[350].second, "sysopb2.def", 235, 134, 2);
  2483. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].first),CGI->generaltexth->zelp[351].second, "sysopb3.def", 283, 134, 4);
  2484. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].first),CGI->generaltexth->zelp[352].second, "sysopb4.def", 331, 134, 8);
  2485. heroMoveSpeed->select(owner->heroMoveSpeed, 1);
  2486. heroMoveSpeed->onChange = boost::bind(&CPlayerInterface::setHeroMoveSpeed, owner, _1);
  2487. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  2488. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].first),CGI->generaltexth->zelp[357].second, "sysopb9.def", 187, 267, 1);
  2489. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].first),CGI->generaltexth->zelp[358].second, "sysob10.def", 251, 267, 2);
  2490. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].first),CGI->generaltexth->zelp[359].second, "sysob11.def", 315, 267, 4);
  2491. mapScrollSpeed->select(owner->mapScrollingSpeed, 1);
  2492. mapScrollSpeed->onChange = boost::bind(&CPlayerInterface::setMapScrollingSpeed, owner, _1);
  2493. musicVolume = new CHighlightableButtonsGroup(0, true);
  2494. for(int i=0; i<10; ++i)
  2495. {
  2496. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].first),CGI->generaltexth->zelp[326+i].second, "syslb.def", 188 + 19*i, 416, i*11);
  2497. }
  2498. musicVolume->select(CGI->musich->getVolume(), 1);
  2499. musicVolume->onChange = boost::bind(&CMusicHandler::setVolume, CGI->musich, _1);
  2500. effectsVolume = new CHighlightableButtonsGroup(0, true);
  2501. for(int i=0; i<10; ++i)
  2502. {
  2503. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].first),CGI->generaltexth->zelp[336+i].second, "syslb.def", 188 + 19*i, 482, i*11);
  2504. }
  2505. effectsVolume->select(CGI->soundh->getVolume(), 1);
  2506. effectsVolume->onChange = boost::bind(&CSoundHandler::setVolume, CGI->soundh, _1);
  2507. }
  2508. CSystemOptionsWindow::~CSystemOptionsWindow()
  2509. {
  2510. SDL_FreeSurface(background);
  2511. delete save;
  2512. delete quitGame;
  2513. delete backToMap;
  2514. delete heroMoveSpeed;
  2515. delete mapScrollSpeed;
  2516. delete musicVolume;
  2517. delete effectsVolume;
  2518. }
  2519. void CSystemOptionsWindow::bquitf()
  2520. {
  2521. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(exit, 0), boost::bind(&CSystemOptionsWindow::activate, this), false);
  2522. }
  2523. void CSystemOptionsWindow::breturnf()
  2524. {
  2525. LOCPLINT->popIntTotally(this);
  2526. }
  2527. void CSystemOptionsWindow::bsavef()
  2528. {
  2529. using namespace boost::posix_time;
  2530. std::ostringstream fnameStream;
  2531. fnameStream << second_clock::local_time();
  2532. std::string fname = fnameStream.str();
  2533. boost::algorithm::replace_all(fname,":","");
  2534. boost::algorithm::replace_all(fname," ","-");
  2535. LOCPLINT->showYesNoDialog("Do you want to save current game as " + fname, std::vector<SComponent*>(), boost::bind(&CCallback::save, LOCPLINT->cb, fname), boost::bind(&CSystemOptionsWindow::activate, this), false);
  2536. }
  2537. void CSystemOptionsWindow::activate()
  2538. {
  2539. save->activate();
  2540. quitGame->activate();
  2541. backToMap->activate();
  2542. heroMoveSpeed->activate();
  2543. mapScrollSpeed->activate();
  2544. musicVolume->activate();
  2545. effectsVolume->activate();
  2546. }
  2547. void CSystemOptionsWindow::deactivate()
  2548. {
  2549. save->deactivate();
  2550. quitGame->deactivate();
  2551. backToMap->deactivate();
  2552. heroMoveSpeed->deactivate();
  2553. mapScrollSpeed->deactivate();
  2554. musicVolume->deactivate();
  2555. effectsVolume->deactivate();
  2556. }
  2557. void CSystemOptionsWindow::show(SDL_Surface *to)
  2558. {
  2559. SDL_BlitSurface(background, NULL, to, &pos);
  2560. save->show(to);
  2561. quitGame->show(to);
  2562. backToMap->show(to);
  2563. heroMoveSpeed->show(to);
  2564. mapScrollSpeed->show(to);
  2565. musicVolume->show(to);
  2566. effectsVolume->show(to);
  2567. }
  2568. CTavernWindow::CTavernWindow(const CGHeroInstance *H1, const CGHeroInstance *H2, const std::string &gossip)
  2569. :h1(selected,0,72,299,H1),h2(selected,1,162,299,H2)
  2570. {
  2571. if(H1)
  2572. selected = 0;
  2573. else
  2574. selected = -1;
  2575. oldSelected = -1;
  2576. bg = BitmapHandler::loadBitmap("TPTAVERN.bmp");
  2577. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2578. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2579. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,GEOR16,tytulowy,bg);
  2580. printAtMiddle("2500",320,328,GEOR13,zwykly,bg);
  2581. printAtMiddle(CGI->generaltexth->jktexts[38],146,283,GEOR16,tytulowy,bg);
  2582. printAtMiddleWB(gossip,200,220,GEOR13,50,zwykly,bg);
  2583. pos.w = bg->w;
  2584. pos.h = bg->h;
  2585. pos.x = (screen->w-bg->w)/2;
  2586. pos.y = (screen->h-bg->h)/2;
  2587. bar = new CStatusBar(pos.x+8, pos.y+478, "APHLFTRT.bmp", 380);
  2588. h1.pos.x += pos.x;
  2589. h2.pos.x += pos.x;
  2590. h1.pos.y += pos.y;
  2591. h2.pos.y += pos.y;
  2592. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"",boost::bind(&CTavernWindow::close,this),pos.x+310,pos.y+428,"ICANCEL.DEF",SDLK_ESCAPE);
  2593. recruit = new AdventureMapButton("","",boost::bind(&CTavernWindow::recruitb,this),pos.x+272,pos.y+355,"TPTAV01.DEF",SDLK_RETURN);
  2594. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"",0,pos.x+22,pos.y+428,"TPTAV02.DEF",SDLK_t);
  2595. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  2596. {
  2597. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  2598. recruit->block(2);
  2599. }
  2600. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  2601. {
  2602. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  2603. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  2604. recruit->block(2);
  2605. }
  2606. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  2607. {
  2608. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  2609. recruit->block(2);
  2610. }
  2611. else
  2612. {
  2613. if(!H1)
  2614. recruit->block(1);
  2615. }
  2616. }
  2617. void CTavernWindow::recruitb()
  2618. {
  2619. const CGHeroInstance *toBuy = (selected ? h2 : h1).h;
  2620. close();
  2621. LOCPLINT->cb->recruitHero(LOCPLINT->castleInt->town,toBuy);
  2622. }
  2623. CTavernWindow::~CTavernWindow()
  2624. {
  2625. SDL_FreeSurface(bg);
  2626. delete cancel;
  2627. delete thiefGuild;
  2628. delete recruit;
  2629. delete bar;
  2630. }
  2631. void CTavernWindow::activate()
  2632. {
  2633. thiefGuild->activate();
  2634. cancel->activate();
  2635. if(h1.h)
  2636. h1.activate();
  2637. if(h2.h)
  2638. h2.activate();
  2639. recruit->activate();
  2640. LOCPLINT->statusbar = bar;
  2641. }
  2642. void CTavernWindow::deactivate()
  2643. {
  2644. thiefGuild->deactivate();
  2645. cancel->deactivate();
  2646. if(h1.h)
  2647. h1.deactivate();
  2648. if(h2.h)
  2649. h2.deactivate();
  2650. recruit->deactivate();
  2651. }
  2652. void CTavernWindow::close()
  2653. {
  2654. LOCPLINT->popIntTotally(this);
  2655. }
  2656. void CTavernWindow::show(SDL_Surface * to)
  2657. {
  2658. blitAt(bg,pos.x,pos.y,to);
  2659. if(h1.h)
  2660. h1.show(to);
  2661. if(h2.h)
  2662. h2.show(to);
  2663. thiefGuild->show(to);
  2664. cancel->show(to);
  2665. recruit->show(to);
  2666. bar->show(to);
  2667. if(selected >= 0)
  2668. {
  2669. HeroPortrait *sel = selected ? &h2 : &h1;
  2670. if (selected != oldSelected && !recruit->blocked)
  2671. {
  2672. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  2673. oldSelected = selected;
  2674. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  2675. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  2676. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  2677. }
  2678. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,GEOR13,40,zwykly,to);
  2679. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  2680. }
  2681. }
  2682. void CTavernWindow::HeroPortrait::clickLeft(boost::logic::tribool down)
  2683. {
  2684. if(pressedL && !down)
  2685. as();
  2686. ClickableL::clickLeft(down);
  2687. }
  2688. void CTavernWindow::HeroPortrait::activate()
  2689. {
  2690. ClickableL::activate();
  2691. ClickableR::activate();
  2692. Hoverable::activate();
  2693. }
  2694. void CTavernWindow::HeroPortrait::deactivate()
  2695. {
  2696. ClickableL::deactivate();
  2697. ClickableR::deactivate();
  2698. Hoverable::deactivate();
  2699. }
  2700. void CTavernWindow::HeroPortrait::clickRight(boost::logic::tribool down)
  2701. {
  2702. if(down)
  2703. {
  2704. LOCPLINT->adventureInt->heroWindow->setHero(h);
  2705. LOCPLINT->pushInt(new CRClickPopupInt(LOCPLINT->adventureInt->heroWindow,false));
  2706. }
  2707. }
  2708. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  2709. :as(sel,id)
  2710. {
  2711. h = H;
  2712. pos.x = x;
  2713. pos.y = y;
  2714. pos.w = 58;
  2715. pos.h = 64;
  2716. if(H)
  2717. {
  2718. hoverName = CGI->generaltexth->tavernInfo[4];
  2719. boost::algorithm::replace_first(hoverName,"%s",H->name);
  2720. }
  2721. int artifs = h->artifWorn.size() + h->artifacts.size();
  2722. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  2723. if(vstd::contains(h->artifWorn,i))
  2724. artifs--;
  2725. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  2726. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  2727. descr[sizeof(descr)-1] = '\0';
  2728. }
  2729. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  2730. {
  2731. blitAt(graphics->portraitLarge[h->subID],pos,to);
  2732. }
  2733. void CTavernWindow::HeroPortrait::hover( bool on )
  2734. {
  2735. Hoverable::hover(on);
  2736. if(on)
  2737. LOCPLINT->statusbar->print(hoverName);
  2738. else
  2739. LOCPLINT->statusbar->clear();
  2740. }
  2741. void CInGameConsole::activate()
  2742. {
  2743. KeyInterested::activate();
  2744. }
  2745. void CInGameConsole::deactivate()
  2746. {
  2747. KeyInterested::deactivate();
  2748. }
  2749. void CInGameConsole::show(SDL_Surface * to)
  2750. {
  2751. int number = 0;
  2752. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  2753. texts_mx.lock();
  2754. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  2755. {
  2756. SDL_Color green = {0,0xff,0,0};
  2757. Point leftBottomCorner(0, screen->h);
  2758. if(LOCPLINT->battleInt)
  2759. {
  2760. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  2761. }
  2762. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, TNRB16, green);
  2763. if(SDL_GetTicks() - it->second > defaultTimeout)
  2764. {
  2765. toDel.push_back(it);
  2766. }
  2767. }
  2768. for(int it=0; it<toDel.size(); ++it)
  2769. {
  2770. texts.erase(toDel[it]);
  2771. }
  2772. texts_mx.unlock();
  2773. }
  2774. void CInGameConsole::print(const std::string &txt)
  2775. {
  2776. texts_mx.lock();
  2777. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  2778. if(texts.size() > maxDisplayedTexts)
  2779. {
  2780. texts.pop_front();
  2781. }
  2782. texts_mx.unlock();
  2783. }
  2784. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  2785. {
  2786. if(key.type != SDL_KEYDOWN) return;
  2787. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  2788. switch(key.keysym.sym)
  2789. {
  2790. case SDLK_TAB:
  2791. {
  2792. if(captureAllKeys)
  2793. {
  2794. captureAllKeys = false;
  2795. endEnteringText(false);
  2796. }
  2797. else
  2798. {
  2799. captureAllKeys = true;
  2800. startEnteringText();
  2801. }
  2802. break;
  2803. }
  2804. case SDLK_RETURN: //enter key
  2805. {
  2806. if(enteredText.size() > 0 && captureAllKeys)
  2807. {
  2808. captureAllKeys = false;
  2809. endEnteringText(true);
  2810. }
  2811. break;
  2812. }
  2813. case SDLK_BACKSPACE:
  2814. {
  2815. if(enteredText.size() > 1)
  2816. {
  2817. enteredText.resize(enteredText.size()-1);
  2818. enteredText[enteredText.size()-1] = '_';
  2819. refreshEnteredText();
  2820. }
  2821. break;
  2822. }
  2823. case SDLK_UP: //up arrow
  2824. {
  2825. if(prevEntDisp == -1)
  2826. {
  2827. prevEntDisp = previouslyEntered.size() - 1;
  2828. enteredText = previouslyEntered[prevEntDisp] + "_";
  2829. refreshEnteredText();
  2830. }
  2831. else if( prevEntDisp > 0)
  2832. {
  2833. --prevEntDisp;
  2834. enteredText = previouslyEntered[prevEntDisp] + "_";
  2835. refreshEnteredText();
  2836. }
  2837. break;
  2838. }
  2839. case SDLK_DOWN: //down arrow
  2840. {
  2841. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  2842. {
  2843. ++prevEntDisp;
  2844. enteredText = previouslyEntered[prevEntDisp] + "_";
  2845. refreshEnteredText();
  2846. }
  2847. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  2848. {
  2849. prevEntDisp = -1;
  2850. enteredText = "_";
  2851. refreshEnteredText();
  2852. }
  2853. break;
  2854. }
  2855. default:
  2856. {
  2857. if(enteredText.size() > 0)
  2858. {
  2859. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  2860. {
  2861. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  2862. enteredText += "_";
  2863. refreshEnteredText();
  2864. }
  2865. }
  2866. break;
  2867. }
  2868. }
  2869. }
  2870. void CInGameConsole::startEnteringText()
  2871. {
  2872. enteredText = "_";
  2873. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  2874. {
  2875. LOCPLINT->statusbar->print(enteredText);
  2876. }
  2877. else if(LOCPLINT->battleInt)
  2878. {
  2879. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  2880. }
  2881. }
  2882. void CInGameConsole::endEnteringText(bool printEnteredText)
  2883. {
  2884. prevEntDisp = -1;
  2885. if(printEnteredText)
  2886. {
  2887. std::string txt = enteredText.substr(0, enteredText.size()-1);
  2888. LOCPLINT->cb->sendMessage(txt);
  2889. previouslyEntered.push_back(txt);
  2890. print(txt);
  2891. }
  2892. enteredText = "";
  2893. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  2894. {
  2895. LOCPLINT->statusbar->clear();
  2896. }
  2897. else if(LOCPLINT->battleInt)
  2898. {
  2899. LOCPLINT->battleInt->console->ingcAlter = "";
  2900. }
  2901. }
  2902. void CInGameConsole::refreshEnteredText()
  2903. {
  2904. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  2905. {
  2906. LOCPLINT->statusbar->print(enteredText);
  2907. }
  2908. else if(LOCPLINT->battleInt)
  2909. {
  2910. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  2911. }
  2912. }
  2913. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  2914. {
  2915. }
  2916. void CGarrisonWindow::close()
  2917. {
  2918. LOCPLINT->popIntTotally(this);
  2919. }
  2920. void CGarrisonWindow::activate()
  2921. {
  2922. split->activate();
  2923. quit->activate();
  2924. garr->activate();
  2925. }
  2926. void CGarrisonWindow::deactivate()
  2927. {
  2928. split->deactivate();
  2929. quit->deactivate();
  2930. garr->deactivate();
  2931. }
  2932. void CGarrisonWindow::show(SDL_Surface * to)
  2933. {
  2934. blitAt(bg,pos,to);
  2935. split->show(to);
  2936. quit->show(to);
  2937. garr->show(to);
  2938. blitAt(graphics->flags->ourImages[garr->odown->getOwner()].bitmap,pos.x+29,pos.y+125,to);
  2939. blitAt(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->odown)->portrait],pos.x+29,pos.y+222,to);
  2940. printAtMiddle(CGI->generaltexth->allTexts[709],pos.x+275,pos.y+30,GEOR16,tytulowy,to);
  2941. }
  2942. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down )
  2943. {
  2944. bg = BitmapHandler::loadBitmap("GARRISON.bmp");
  2945. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2946. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2947. pos.x = screen->w/2 - bg->w/2;
  2948. pos.y = screen->h/2 - bg->h/2;
  2949. pos.w = screen->w;
  2950. pos.h = screen->h;
  2951. garr = new CGarrisonInt(pos.x+92, pos.y+129, 4, 30, bg, 92, 129, up, down);
  2952. split = new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+88,pos.y+314,"IDV6432.DEF");
  2953. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),pos.x+399,pos.y+314,"IOK6432.DEF",SDLK_RETURN);
  2954. }
  2955. CGarrisonWindow::~CGarrisonWindow()
  2956. {
  2957. SDL_FreeSurface(bg);
  2958. delete split;
  2959. delete quit;
  2960. delete garr;
  2961. }
  2962. IShowActivable::IShowActivable()
  2963. {
  2964. type = 0;
  2965. }
  2966. CWindowWithGarrison::CWindowWithGarrison()
  2967. {
  2968. type |= WITH_GARRISON;
  2969. }
  2970. void CRClickPopupInt::show(SDL_Surface * to)
  2971. {
  2972. inner->show(to);
  2973. }
  2974. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  2975. {
  2976. inner = our;
  2977. delInner = deleteInt;
  2978. }
  2979. CRClickPopupInt::~CRClickPopupInt()
  2980. {
  2981. //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  2982. //TODO? it might be better to rewrite hero window so it will bee newed/deleted on opening / closing (not effort-worthy now, but on some day...?)
  2983. if(inner == LOCPLINT->adventureInt->heroWindow)
  2984. LOCPLINT->adventureInt->heroWindow->dispose();
  2985. if(delInner)
  2986. delete inner;
  2987. }