GUIClasses.cpp 84 KB

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