GUIClasses.cpp 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327
  1. #include "GUIClasses.h"
  2. #include "SDL_Extensions.h"
  3. #include "../stdafx.h"
  4. #include "CAdvmapInterface.h"
  5. #include "CBattleInterface.h"
  6. #include "../CCallback.h"
  7. #include "CCastleInterface.h"
  8. #include "CCursorHandler.h"
  9. #include "CGameInfo.h"
  10. #include "CHeroWindow.h"
  11. #include "CMessage.h"
  12. #include "CConfigHandler.h"
  13. #include "SDL_framerate.h"
  14. #include "CConfigHandler.h"
  15. #include "CCreatureAnimation.h"
  16. #include "Graphics.h"
  17. #include "../hch/CArtHandler.h"
  18. #include "../hch/CGeneralTextHandler.h"
  19. #include "../hch/CHeroHandler.h"
  20. #include "../hch/CLodHandler.h"
  21. #include "../hch/CObjectHandler.h"
  22. #include "../hch/CSpellHandler.h"
  23. #include "../hch/CTownHandler.h"
  24. #include "../lib/CondSh.h"
  25. #include "../lib/map.h"
  26. #include "../mapHandler.h"
  27. #include "../timeHandler.h"
  28. #include <boost/lexical_cast.hpp>
  29. #include <boost/algorithm/string.hpp>
  30. #include <boost/algorithm/string/replace.hpp>
  31. #include <boost/assign/std/vector.hpp>
  32. #include <boost/assign/list_of.hpp>
  33. #include <boost/date_time/posix_time/posix_time.hpp>
  34. //#include <boost/thread.hpp>
  35. #include <cmath>
  36. #include <queue>
  37. #include <sstream>
  38. #include "../lib/NetPacks.h"
  39. #include "CSpellWindow.h"
  40. #include "CHeroWindow.h"
  41. #include "../hch/CVideoHandler.h"
  42. #include "../StartInfo.h"
  43. #include "CPreGame.h"
  44. /*
  45. * GUIClasses.cpp, part of VCMI engine
  46. *
  47. * Authors: listed in file AUTHORS in main folder
  48. *
  49. * License: GNU General Public License v2.0 or later
  50. * Full text of license available in license.txt file, in main folder
  51. *
  52. */
  53. using namespace boost::assign;
  54. using namespace CSDL_Ext;
  55. extern TTF_Font * GEOR16;
  56. extern std::queue<SDL_Event*> events;
  57. extern boost::mutex eventsM;
  58. #undef min
  59. #undef max
  60. static StackState* getStackState(const CGObjectInstance *obj, int pos, bool town)
  61. {
  62. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(obj);
  63. if(!h) return NULL;
  64. StackState *pom = new StackState();
  65. pom->shotsLeft = -1;
  66. pom->healthBonus = h->valOfBonuses(HeroBonus::STACK_HEALTH);
  67. pom->currentHealth = 0;
  68. pom->attackBonus = h->getPrimSkillLevel(0);
  69. pom->defenseBonus = h->getPrimSkillLevel(1);
  70. pom->luck = h->getCurrentLuck();
  71. pom->morale = h->getCurrentMorale(pos,town);
  72. pom->speedBonus = h->valOfBonuses(HeroBonus::STACKS_SPEED);
  73. pom->dmgMultiplier = 1;
  74. return pom;
  75. }
  76. void CGarrisonSlot::hover (bool on)
  77. {
  78. ////Hoverable::hover(on);
  79. if(on)
  80. {
  81. std::string temp;
  82. if(creature)
  83. {
  84. if(owner->highlighted)
  85. {
  86. if(owner->highlighted == this)
  87. {
  88. temp = CGI->generaltexth->tcommands[4]; //View %s
  89. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  90. }
  91. else if (owner->highlighted->creature == creature)
  92. {
  93. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  94. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  95. }
  96. else if (owner->highlighted->creature)
  97. {
  98. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  99. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  100. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  101. }
  102. else
  103. {
  104. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  105. tlog2 << "Highlighted set to NULL"<<std::endl;
  106. owner->highlighted = NULL;
  107. }
  108. }
  109. else
  110. {
  111. if(upg)
  112. {
  113. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  114. }
  115. else if(owner->oup && owner->oup->ID == TOWNI_TYPE)
  116. {
  117. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  118. }
  119. else
  120. {
  121. temp = CGI->generaltexth->allTexts[481]; //Select %s
  122. }
  123. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  124. };
  125. }
  126. else
  127. {
  128. if(owner->highlighted)
  129. {
  130. const CArmedInstance *highl = owner->highlighted->getObj();
  131. if( highl->needsLastStack() //we are moving stack from hero's
  132. && highl->army.slots.size() == 1 //it's only stack
  133. && owner->highlighted->upg != upg //we're moving it to the other garrison
  134. )
  135. {
  136. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  137. }
  138. else
  139. {
  140. temp = CGI->generaltexth->tcommands[6]; //Move %s
  141. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  142. }
  143. }
  144. else
  145. {
  146. temp = CGI->generaltexth->tcommands[11]; //Empty
  147. }
  148. }
  149. LOCPLINT->statusbar->print(temp);
  150. }
  151. else
  152. {
  153. LOCPLINT->statusbar->clear();
  154. }
  155. }
  156. const CArmedInstance * CGarrisonSlot::getObj()
  157. {
  158. return (!upg)?(owner->oup):(owner->odown);
  159. }
  160. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  161. {
  162. StackState *pom = getStackState(getObj(),ID, GH.topInt() == LOCPLINT->castleInt);
  163. if(down && creature)
  164. {
  165. GH.pushInt(new CCreInfoWindow(creature->idNumber, 0, count, pom, 0, 0, NULL));
  166. }
  167. delete pom;
  168. }
  169. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  170. {
  171. if(owner->ignoreEvent)
  172. {
  173. owner->ignoreEvent = false;
  174. return;
  175. }
  176. if(down)
  177. {
  178. bool refr = false;
  179. if(owner->highlighted)
  180. {
  181. if(owner->highlighted == this) //view info
  182. {
  183. StackState *pom2 = getStackState(getObj(), ID, GH.topInt() == LOCPLINT->castleInt);
  184. UpgradeInfo pom = LOCPLINT->cb->getUpgradeInfo(getObj(), ID);
  185. CCreInfoWindow *creWindow = NULL;
  186. if(pom.oldID>=0) //upgrade is possible
  187. {
  188. creWindow = new CCreInfoWindow(
  189. creature->idNumber, 1, count, pom2,
  190. boost::bind(&CCallback::upgradeCreature, LOCPLINT->cb, getObj(), ID, pom.newID[0]), //bind upgrade function
  191. boost::bind(&CCallback::dismissCreature, LOCPLINT->cb, getObj(), ID), &pom);
  192. }
  193. else
  194. {
  195. creWindow = new CCreInfoWindow(
  196. creature->idNumber, 1, count, pom2, 0,
  197. boost::bind(&CCallback::dismissCreature, LOCPLINT->cb, getObj(), ID), NULL);
  198. }
  199. GH.pushInt(creWindow);
  200. owner->highlighted = NULL;
  201. owner->splitting = false;
  202. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  203. owner->splitButtons[i]->block(true);
  204. show(screen2);
  205. refr = true;
  206. delete pom2;
  207. }
  208. else {
  209. // Only allow certain moves if troops aren't removable.
  210. if (owner->removableUnits
  211. || (upg == 0 && (owner->highlighted->upg == 1 && !creature))
  212. || (upg == 1 && owner->highlighted->upg == 1))
  213. {
  214. //we want to split
  215. if((owner->splitting || LOCPLINT->shiftPressed())
  216. && (!creature
  217. || (creature == owner->highlighted->creature)))
  218. {
  219. owner->p2 = ID; //store the second stack pos
  220. owner->pb = upg;//store the second stack owner (up or down army)
  221. owner->splitting = false;
  222. int totalAmount = owner->highlighted->count;
  223. if(creature)
  224. totalAmount += count;
  225. int last = -1;
  226. if(upg != owner->highlighted->upg) //not splitting within same army
  227. {
  228. if(owner->highlighted->getObj()->army.slots.size() == 1
  229. && owner->highlighted->getObj()->needsLastStack() )
  230. {
  231. last = 0;
  232. }
  233. if(getObj()->army.slots.size() == 1
  234. && getObj()->needsLastStack() )
  235. {
  236. last += 2;
  237. }
  238. }
  239. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  240. GH.pushInt(spw);
  241. refr = true;
  242. }
  243. else if(creature != owner->highlighted->creature) //swap
  244. {
  245. LOCPLINT->cb->swapCreatures(
  246. (!upg)?(owner->oup):(owner->odown),
  247. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  248. ID,owner->highlighted->ID);
  249. }
  250. else //merge
  251. {
  252. LOCPLINT->cb->mergeStacks(
  253. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  254. (!upg)?(owner->oup):(owner->odown),
  255. owner->highlighted->ID,ID);
  256. }
  257. }
  258. else { // Highlight
  259. if(creature)
  260. owner->highlighted = this;
  261. show(screen2);
  262. refr = true;
  263. }
  264. }
  265. }
  266. else //highlight
  267. {
  268. if(creature)
  269. {
  270. owner->highlighted = this;
  271. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  272. owner->splitButtons[i]->block(false);
  273. }
  274. show(screen2);
  275. refr = true;
  276. }
  277. if(refr) {hover(false); hover(true); } //to refresh statusbar
  278. }
  279. }
  280. void CGarrisonSlot::activate()
  281. {
  282. if(!active) active=true;
  283. else return;
  284. activateLClick();
  285. activateRClick();
  286. activateHover();
  287. }
  288. void CGarrisonSlot::deactivate()
  289. {
  290. if(active) active=false;
  291. else return;
  292. deactivateLClick();
  293. deactivateRClick();
  294. deactivateHover();
  295. }
  296. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CCreature * Creature, int Count)
  297. {
  298. active = false;
  299. upg = Upg;
  300. count = Count;
  301. ID = IID;
  302. creature = Creature;
  303. pos.x = x;
  304. pos.y = y;
  305. if(Owner->smallIcons)
  306. {
  307. pos.w = 32;
  308. pos.h = 32;
  309. }
  310. else
  311. {
  312. pos.w = 58;
  313. pos.h = 64;
  314. }
  315. owner = Owner;
  316. }
  317. CGarrisonSlot::~CGarrisonSlot()
  318. {
  319. if(active)
  320. deactivate();
  321. }
  322. void CGarrisonSlot::show(SDL_Surface * to)
  323. {
  324. std::map<int,SDL_Surface*> &imgs = (owner->smallIcons ? graphics->smallImgs : graphics->bigImgs);
  325. if(creature)
  326. {
  327. char buf[15];
  328. SDL_itoa(count,buf,10);
  329. blitAt(imgs[creature->idNumber],pos,to);
  330. printToWR(buf, pos.x+pos.w-2, pos.y+pos.h-2, owner->smallIcons ? GEORM : GEOR16, zwykly, to);
  331. if((owner->highlighted==this)
  332. || (owner->splitting && owner->highlighted->creature == creature))
  333. {
  334. blitAt(imgs[-1],pos,to);
  335. }
  336. }
  337. else //empty slot
  338. {
  339. Rect pos1 = pos, pos2 = pos; //positions on the garr bg sur and scren
  340. pos1.x = owner->surOffset.x;
  341. pos1.y = owner->surOffset.y;
  342. SDL_BlitSurface(owner->sur,&pos1,to,&pos2);
  343. if(owner->splitting)
  344. blitAt(imgs[-1],pos,to);
  345. }
  346. }
  347. CGarrisonInt::~CGarrisonInt()
  348. {
  349. if(sup)
  350. {
  351. for(size_t i=0;i<sup->size();i++)
  352. {
  353. delete (*sup)[i];
  354. }
  355. delete sup;
  356. }
  357. if(sdown)
  358. {
  359. for(size_t i=0;i<sdown->size();i++)
  360. {
  361. delete (*sdown)[i]; //XXX what about smartpointers? boost or auto_ptr from std
  362. }
  363. delete sdown;
  364. }
  365. for(size_t i = 0; i<splitButtons.size(); i++)
  366. delete splitButtons[i];
  367. }
  368. void CGarrisonInt::show(SDL_Surface * to)
  369. {
  370. if(sup)
  371. {
  372. for(size_t i = 0; i<sup->size(); i++)
  373. {
  374. if((*sup)[i])
  375. {
  376. (*sup)[i]->show(to);
  377. }
  378. }
  379. }
  380. if(sdown)
  381. {
  382. for(size_t i = 0; i<sdown->size(); i++)
  383. {
  384. if((*sdown)[i])
  385. {
  386. (*sdown)[i]->show(to);
  387. }
  388. }
  389. }
  390. for(size_t i = 0; i<splitButtons.size(); i++)
  391. splitButtons[i]->show(to);
  392. }
  393. void CGarrisonInt::deactiveteSlots()
  394. {
  395. if(sup)
  396. {
  397. for(int i = 0; i<sup->size(); i++)
  398. {
  399. if((*sup)[i])
  400. {
  401. (*sup)[i]->deactivate();
  402. }
  403. }
  404. }
  405. if(sdown)
  406. {
  407. for(int i = 0; i<sdown->size(); i++)
  408. {
  409. if((*sdown)[i])
  410. {
  411. (*sdown)[i]->deactivate();
  412. }
  413. }
  414. }
  415. }
  416. void CGarrisonInt::activeteSlots()
  417. {
  418. if(sup)
  419. {
  420. for(int i = 0; i<sup->size(); i++)
  421. {
  422. if((*sup)[i])
  423. {
  424. (*sup)[i]->activate();
  425. }
  426. }
  427. }
  428. if(sdown)
  429. {
  430. for(int i = 0; i<sdown->size(); i++)
  431. {
  432. if((*sdown)[i])
  433. {
  434. (*sdown)[i]->activate();
  435. }
  436. }
  437. }
  438. }
  439. void CGarrisonInt::createSlots()
  440. {
  441. int h, w; //height and width of slot
  442. if(smallIcons)
  443. {
  444. h = w = 32;
  445. }
  446. else
  447. {
  448. h = 64;
  449. w = 58;
  450. }
  451. if(set1)
  452. {
  453. sup = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  454. for
  455. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set1->slots.begin();
  456. i!=set1->slots.end(); i++)
  457. {
  458. (*sup)[i->first] =
  459. new CGarrisonSlot(this, pos.x + (i->first*(w+interx)), pos.y,i->first, 0,
  460. &CGI->creh->creatures[i->second.first],i->second.second);
  461. }
  462. for(int i=0; i<sup->size(); i++)
  463. if((*sup)[i] == NULL)
  464. (*sup)[i] = new CGarrisonSlot(this, pos.x + (i*(w+interx)), pos.y,i,0,NULL, 0);
  465. }
  466. if(set2)
  467. {
  468. sdown = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  469. for
  470. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set2->slots.begin();
  471. i!=set2->slots.end(); i++)
  472. {
  473. (*sdown)[i->first] =
  474. new CGarrisonSlot(this, pos.x + (i->first*(w+interx)) + garOffset.x, pos.y + garOffset.y,i->first,1,
  475. &CGI->creh->creatures[i->second.first],i->second.second);
  476. }
  477. for(int i=0; i<sdown->size(); i++)
  478. if((*sdown)[i] == NULL)
  479. (*sdown)[i] = new CGarrisonSlot(this, pos.x + (i*(w+interx)) + garOffset.x, pos.y + garOffset.y,i,1, NULL, 0);
  480. }
  481. }
  482. void CGarrisonInt::deleteSlots()
  483. {
  484. if(sup)
  485. {
  486. for(int i = 0; i<sup->size(); i++)
  487. {
  488. if((*sup)[i])
  489. {
  490. delete (*sup)[i];
  491. }
  492. }
  493. delete sup;
  494. sup = NULL;
  495. }
  496. if(sdown)
  497. {
  498. for(int i = 0; i<sdown->size(); i++)
  499. {
  500. if((*sdown)[i])
  501. {
  502. delete (*sdown)[i];
  503. }
  504. }
  505. delete sdown;
  506. sdown = NULL;
  507. }
  508. }
  509. void CGarrisonInt::recreateSlots()
  510. {
  511. splitting = false;
  512. highlighted = NULL;
  513. for(size_t i = 0; i<splitButtons.size(); i++)
  514. splitButtons[i]->block(true);
  515. if(active)
  516. {
  517. deactiveteSlots();
  518. }
  519. deleteSlots();
  520. createSlots();
  521. if(active)
  522. {
  523. //ignoreEvent = true;
  524. activeteSlots();
  525. //show(screen2);
  526. }
  527. }
  528. void CGarrisonInt::splitClick()
  529. {
  530. if(!highlighted)
  531. return;
  532. splitting = !splitting;
  533. show(screen2);
  534. }
  535. void CGarrisonInt::splitStacks(int am2)
  536. {
  537. LOCPLINT->cb->splitStack(
  538. (highlighted->upg)?(odown):(oup),
  539. (pb)?(odown):(oup),
  540. highlighted->ID,
  541. p2,
  542. am2);
  543. }
  544. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset, SDL_Surface *&pomsur, const Point& SurOffset,
  545. const CArmedInstance *s1, const CArmedInstance *s2, bool _removableUnits, bool smallImgs)
  546. :interx(inx),garOffset(garsOffset),highlighted(NULL),sur(pomsur),surOffset(SurOffset),sup(NULL),
  547. sdown(NULL),oup(s1),odown(s2), removableUnits(_removableUnits), smallIcons(smallImgs)
  548. {
  549. active = false;
  550. splitting = false;
  551. set1 = LOCPLINT->cb->getGarrison(s1);
  552. set2 = LOCPLINT->cb->getGarrison(s2);
  553. ignoreEvent = false;
  554. update = true;
  555. pos.x=(x);
  556. pos.y=(y);
  557. pos.w=(58);
  558. pos.h=(64);
  559. createSlots();
  560. }
  561. void CGarrisonInt::activate()
  562. {
  563. for(size_t i = 0; i<splitButtons.size(); i++)
  564. if(splitButtons[i]->blocked != !highlighted)
  565. splitButtons[i]->block(!highlighted);
  566. active = true;
  567. if(sup)
  568. {
  569. for(int i = 0; i<sup->size(); i++)
  570. if((*sup)[i])
  571. (*sup)[i]->activate();
  572. }
  573. if(sdown)
  574. {
  575. for(int i = 0; i<sdown->size(); i++)
  576. if((*sdown)[i])
  577. (*sdown)[i]->activate();
  578. }
  579. for(size_t i = 0; i<splitButtons.size(); i++)
  580. splitButtons[i]->activate();
  581. }
  582. void CGarrisonInt::deactivate()
  583. {
  584. active = false;
  585. deactiveteSlots();
  586. for(size_t i = 0; i<splitButtons.size(); i++)
  587. splitButtons[i]->deactivate();
  588. }
  589. 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)
  590. {
  591. ID = -1;
  592. this->delComps = delComps;
  593. for(int i=0;i<Buttons.size();i++)
  594. {
  595. buttons.push_back(new AdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first));
  596. buttons[i]->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  597. }
  598. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  599. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  600. for(int i=0;i<comps.size();i++)
  601. {
  602. components.push_back(comps[i]);
  603. }
  604. CMessage::drawIWindow(this,text,player,charperline);
  605. }
  606. CInfoWindow::CInfoWindow()
  607. {
  608. ID = -1;
  609. delComps = false;
  610. }
  611. void CInfoWindow::close()
  612. {
  613. GH.popIntTotally(this);
  614. LOCPLINT->showingDialog->setn(false);
  615. }
  616. void CInfoWindow::show(SDL_Surface * to)
  617. {
  618. CSimpleWindow::show(to);
  619. for(int i=0;i<buttons.size();i++)
  620. buttons[i]->show(to);
  621. }
  622. CInfoWindow::~CInfoWindow()
  623. {
  624. if(delComps)
  625. {
  626. for (int i=0;i<components.size();i++)
  627. delete components[i];
  628. }
  629. for(int i=0;i<buttons.size();i++)
  630. delete buttons[i];
  631. }
  632. void CInfoWindow::activate()
  633. {
  634. for (int i=0;i<components.size();i++)
  635. components[i]->activate();
  636. for(int i=0;i<buttons.size();i++)
  637. buttons[i]->activate();
  638. }
  639. void CInfoWindow::deactivate()
  640. {
  641. for (int i=0;i<components.size();i++)
  642. components[i]->deactivate();
  643. for(int i=0;i<buttons.size();i++)
  644. buttons[i]->deactivate();
  645. }
  646. void CInfoWindow::showAll( SDL_Surface * to )
  647. {
  648. show(to);
  649. }
  650. void CRClickPopup::clickRight(tribool down, bool previousState)
  651. {
  652. if(down)
  653. return;
  654. close();
  655. }
  656. void CRClickPopup::activate()
  657. {
  658. activateRClick();
  659. }
  660. void CRClickPopup::deactivate()
  661. {
  662. deactivateRClick();
  663. }
  664. void CRClickPopup::close()
  665. {
  666. GH.popIntTotally(this);
  667. }
  668. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  669. :free(Free),bitmap(Bitmap)
  670. {
  671. pos.x = x;
  672. pos.y = y;
  673. pos.h = bitmap->h;
  674. pos.w = bitmap->w;
  675. // Put the window back on screen if necessary
  676. amax(pos.x, 0);
  677. amax(pos.y, 0);
  678. amin(pos.x, conf.cc.resx - bitmap->w);
  679. amin(pos.y, conf.cc.resy - bitmap->h);
  680. }
  681. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  682. {
  683. free=Free;
  684. bitmap=Bitmap;
  685. if(bitmap)
  686. {
  687. pos.x = screen->w/2 - bitmap->w/2;
  688. pos.y = screen->h/2 - bitmap->h/2;
  689. pos.h = bitmap->h;
  690. pos.w = bitmap->w;
  691. }
  692. }
  693. void CInfoPopup::close()
  694. {
  695. if(free)
  696. SDL_FreeSurface(bitmap);
  697. GH.popIntTotally(this);
  698. }
  699. void CInfoPopup::show(SDL_Surface * to)
  700. {
  701. blitAt(bitmap,pos.x,pos.y,to);
  702. }
  703. void SComponent::init(Etype Type, int Subtype, int Val)
  704. {
  705. std::ostringstream oss;
  706. switch (Type)
  707. {
  708. case artifact:
  709. description = CGI->arth->artifacts[Subtype].Description();
  710. subtitle = CGI->arth->artifacts[Subtype].Name();
  711. break;
  712. case primskill:
  713. oss << std::showpos << Val << " ";
  714. if(Subtype < 4)
  715. {
  716. description = CGI->generaltexth->arraytxt[2+Subtype];
  717. oss << CGI->generaltexth->primarySkillNames[Subtype];
  718. }
  719. else if(Subtype == 5) //spell points
  720. {
  721. description = CGI->generaltexth->allTexts[149];
  722. oss << CGI->generaltexth->allTexts[387];
  723. }
  724. else
  725. {
  726. tlog1 << "Wrong subtype=" << Subtype << std::endl;
  727. }
  728. subtitle = oss.str();
  729. break;
  730. case secskill44: case secskill:
  731. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  732. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  733. break;
  734. case resource:
  735. description = CGI->generaltexth->allTexts[242];
  736. oss << Val;
  737. subtitle = oss.str();
  738. break;
  739. case spell:
  740. description = CGI->spellh->spells[Subtype].descriptions[Val];
  741. subtitle = CGI->spellh->spells[Subtype].name;
  742. break;
  743. case creature:
  744. subtitle = boost::lexical_cast<std::string>(Val) + " " + CGI->creh->creatures[Subtype].*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  745. break;
  746. case experience:
  747. description = CGI->generaltexth->allTexts[241];
  748. oss << Val ;
  749. if(Subtype && Val==1)
  750. {
  751. subtitle = CGI->generaltexth->allTexts[442];
  752. }
  753. else
  754. {
  755. subtitle = oss.str();
  756. }
  757. break;
  758. }
  759. type = Type;
  760. subtype = Subtype;
  761. val = Val;
  762. SDL_Surface * temp = this->getImg();
  763. if(!temp)
  764. {
  765. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  766. return;
  767. }
  768. pos.w = temp->w;
  769. pos.h = temp->h;
  770. }
  771. SComponent::SComponent(Etype Type, int Subtype, int Val)
  772. {
  773. init(Type,Subtype,Val);
  774. }
  775. SComponent::SComponent(const Component &c)
  776. {
  777. if(c.id==5)
  778. init(experience,c.subtype,c.val);
  779. else if(c.id == Component::SPELL)
  780. init(spell,c.subtype,c.val);
  781. else
  782. init((Etype)c.id,c.subtype,c.val);
  783. if(c.id==2 && c.when==-1)
  784. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  785. }
  786. void SComponent::show(SDL_Surface * to)
  787. {
  788. blitAt(getImg(),pos.x,pos.y,to);
  789. }
  790. SDL_Surface * SComponent::getImg()
  791. {
  792. switch (type)
  793. {
  794. case artifact:
  795. return graphics->artDefs->ourImages[subtype].bitmap;
  796. break;
  797. case primskill:
  798. return graphics->pskillsb->ourImages[subtype].bitmap;
  799. break;
  800. case secskill44:
  801. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  802. break;
  803. case secskill:
  804. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  805. break;
  806. case resource:
  807. return graphics->resources->ourImages[subtype].bitmap;
  808. break;
  809. case experience:
  810. return graphics->pskillsb->ourImages[4].bitmap;
  811. break;
  812. case morale:
  813. return graphics->morale82->ourImages[val+3].bitmap;
  814. break;
  815. case luck:
  816. return graphics->luck82->ourImages[val+3].bitmap;
  817. break;
  818. case spell:
  819. return graphics->spellscr->ourImages[subtype].bitmap;
  820. break;
  821. case creature:
  822. return graphics->bigImgs[subtype];
  823. }
  824. return NULL;
  825. }
  826. void SComponent::clickRight(tribool down, bool previousState)
  827. {
  828. if(description.size())
  829. LOCPLINT->adventureInt->handleRightClick(description,down,this);
  830. }
  831. void SComponent::activate()
  832. {
  833. activateRClick();
  834. }
  835. void SComponent::deactivate()
  836. {
  837. deactivateRClick();
  838. }
  839. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  840. {
  841. if (down)
  842. {
  843. if(onSelect)
  844. onSelect();
  845. }
  846. }
  847. void CSelectableComponent::init(SDL_Surface * Border)
  848. {
  849. SDL_Surface * symb = SComponent::getImg();
  850. myBitmap = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  851. SDL_SetColorKey(myBitmap,SDL_SRCCOLORKEY,SDL_MapRGB(myBitmap->format,0,255,255));
  852. blitAt(symb,1,1,myBitmap);
  853. if (Border) //use custom border
  854. {
  855. border = Border;
  856. customB = true;
  857. }
  858. else //we need to draw border
  859. {
  860. customB = false;
  861. border = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  862. SDL_FillRect(border,NULL,0x00FFFF);
  863. for (int i=0;i<border->w;i++)
  864. {
  865. SDL_PutPixelWithoutRefresh(border,i,0,239,215,123);
  866. SDL_PutPixelWithoutRefresh(border,i,(border->h)-1,239,215,123);
  867. }
  868. for (int i=0;i<border->h;i++)
  869. {
  870. SDL_PutPixelWithoutRefresh(border,0,i,239,215,123);
  871. SDL_PutPixelWithoutRefresh(border,(border->w)-1,i,239,215,123);
  872. }
  873. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  874. }
  875. selected = false;
  876. }
  877. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect, SDL_Surface * Border)
  878. :SComponent(c),onSelect(OnSelect)
  879. {
  880. init(Border);
  881. }
  882. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect, SDL_Surface * Border)
  883. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  884. {
  885. init(Border);
  886. }
  887. CSelectableComponent::~CSelectableComponent()
  888. {
  889. SDL_FreeSurface(myBitmap);
  890. if (!customB)
  891. SDL_FreeSurface(border);
  892. }
  893. void CSelectableComponent::activate()
  894. {
  895. activateKeys();
  896. SComponent::activate();
  897. activateLClick();
  898. }
  899. void CSelectableComponent::deactivate()
  900. {
  901. deactivateKeys();
  902. SComponent::deactivate();
  903. deactivateLClick();
  904. }
  905. SDL_Surface * CSelectableComponent::getImg()
  906. {
  907. return myBitmap;
  908. }
  909. void CSelectableComponent::select(bool on)
  910. {
  911. if(on != selected)
  912. {
  913. SDL_FillRect(myBitmap,NULL,0x000000);
  914. blitAt(SComponent::getImg(),1,1,myBitmap);
  915. if (on)
  916. {
  917. blitAt(border,0,0,myBitmap);
  918. }
  919. selected = on;
  920. return;
  921. }
  922. else
  923. {
  924. return;
  925. }
  926. }
  927. void CSelectableComponent::show(SDL_Surface * to)
  928. {
  929. blitAt(myBitmap,pos.x,pos.y,to);
  930. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+14,GEOR13,12,zwykly,to);
  931. }
  932. void CSimpleWindow::show(SDL_Surface * to)
  933. {
  934. blitAt(bitmap,pos.x,pos.y,to);
  935. }
  936. CSimpleWindow::~CSimpleWindow()
  937. {
  938. if (bitmap)
  939. {
  940. SDL_FreeSurface(bitmap);
  941. bitmap=NULL;
  942. }
  943. }
  944. void CSelWindow::selectionChange(unsigned to)
  945. {
  946. for (unsigned i=0;i<components.size();i++)
  947. {
  948. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  949. if (!pom)
  950. continue;
  951. pom->select(i==to);
  952. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  953. }
  954. }
  955. 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)
  956. {
  957. ID = askID;
  958. for(int i=0;i<Buttons.size();i++)
  959. {
  960. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  961. if(!i && askID >= 0)
  962. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  963. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  964. }
  965. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  966. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  967. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  968. buttons.back()->callback += boost::bind(&ICallback::selectionMade,LOCPLINT->cb,0,askID);
  969. for(int i=0;i<comps.size();i++)
  970. {
  971. components.push_back(comps[i]);
  972. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  973. if(i<9)
  974. comps[i]->assignedKeys.insert(SDLK_1+i);
  975. }
  976. CMessage::drawIWindow(this,text,player,charperline);
  977. }
  978. void CSelWindow::madeChoice()
  979. {
  980. if(ID < 0)
  981. return;
  982. int ret = -1;
  983. for (int i=0;i<components.size();i++)
  984. {
  985. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  986. {
  987. ret = i;
  988. }
  989. }
  990. LOCPLINT->cb->selectionMade(ret+1,ID);
  991. }
  992. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  993. {
  994. bg=BitmapHandler::loadBitmap(name);
  995. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  996. pos.x=x;
  997. pos.y=y;
  998. if(maxw >= 0)
  999. pos.w = std::min(bg->w,maxw);
  1000. else
  1001. pos.w=bg->w;
  1002. pos.h=bg->h;
  1003. middlex=(pos.w/2)+x;
  1004. middley=(bg->h/2)+y;
  1005. }
  1006. CStatusBar::~CStatusBar()
  1007. {
  1008. SDL_FreeSurface(bg);
  1009. }
  1010. void CStatusBar::clear()
  1011. {
  1012. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  1013. {
  1014. current="";
  1015. show(screen);
  1016. }
  1017. }
  1018. void CStatusBar::print(const std::string & text)
  1019. {
  1020. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  1021. {
  1022. current=text;
  1023. show(GH.topInt()==LOCPLINT->adventureInt ? screen : screen2); //if there are now windows opened, update statusbar on screen, else to cache surface
  1024. }
  1025. }
  1026. void CStatusBar::show(SDL_Surface * to)
  1027. {
  1028. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  1029. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),to,&pom);
  1030. printAtMiddle(current,middlex,middley,GEOR13,zwykly,to);
  1031. }
  1032. std::string CStatusBar::getCurrent()
  1033. {
  1034. return current;
  1035. }
  1036. void CList::activate()
  1037. {
  1038. activateLClick();
  1039. activateRClick();
  1040. activateHover();
  1041. activateKeys();
  1042. activateMouseMove();
  1043. };
  1044. void CList::deactivate()
  1045. {
  1046. deactivateLClick();
  1047. deactivateRClick();
  1048. deactivateHover();
  1049. deactivateKeys();
  1050. deactivateMouseMove();
  1051. };
  1052. void CList::clickLeft(tribool down, bool previousState)
  1053. {
  1054. };
  1055. CList::CList(int Size)
  1056. :SIZE(Size)
  1057. {
  1058. }
  1059. CHeroList::CHeroList(int Size)
  1060. :CList(Size), heroes(LOCPLINT->wanderingHeroes)
  1061. {
  1062. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  1063. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  1064. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  1065. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  1066. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  1067. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  1068. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  1069. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  1070. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  1071. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  1072. //32px per hero
  1073. posmobx = pos.x+1;
  1074. posmoby = pos.y+arrup->height+1;
  1075. posporx = pos.x+mobile->width+2;
  1076. pospory = pos.y+arrup->height;
  1077. posmanx = pos.x+1+50+mobile->width;
  1078. posmany = pos.y+arrup->height+1;
  1079. from = 0;
  1080. selected = -1;
  1081. pressed = indeterminate;
  1082. }
  1083. void CHeroList::init()
  1084. {
  1085. int w = pos.w+1, h = pos.h+4;
  1086. bg = CSDL_Ext::newSurface(w,h,screen);
  1087. SDL_BlitSurface(LOCPLINT->adventureInt->bg,&genRect(w,h,pos.x,pos.y),bg,&genRect(w,h,0,0));
  1088. }
  1089. void CHeroList::genList()
  1090. {
  1091. //int howMany = LOCPLINT->cb->howManyHeroes();
  1092. //for (int i=0;i<howMany;i++)
  1093. //{
  1094. // const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  1095. // if(!h->inTownGarrison)
  1096. // items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  1097. //}
  1098. }
  1099. void CHeroList::select(int which)
  1100. {
  1101. if (which<0)
  1102. {
  1103. selected = which;
  1104. LOCPLINT->adventureInt->selection = NULL;
  1105. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  1106. draw(screen);
  1107. LOCPLINT->adventureInt->infoBar.draw(screen);
  1108. }
  1109. if (which>=heroes.size())
  1110. return;
  1111. selected = which;
  1112. LOCPLINT->adventureInt->select(heroes[which]);
  1113. }
  1114. void CHeroList::clickLeft(tribool down, bool previousState)
  1115. {
  1116. if (down)
  1117. {
  1118. /***************************ARROWS*****************************************/
  1119. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1120. {
  1121. if(from>0)
  1122. {
  1123. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  1124. pressed = true;
  1125. }
  1126. return;
  1127. }
  1128. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1129. {
  1130. if(heroes.size()-from>SIZE)
  1131. {
  1132. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  1133. pressed = false;
  1134. }
  1135. return;
  1136. }
  1137. /***************************HEROES*****************************************/
  1138. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1139. hx-=pos.x;
  1140. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1141. int ny = hy/32;
  1142. if (ny>=SIZE || ny<0)
  1143. return;
  1144. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  1145. LOCPLINT->openHeroWindow(heroes[selected]);//print hero screen
  1146. select(ny+from);
  1147. }
  1148. else
  1149. {
  1150. if (indeterminate(pressed))
  1151. return;
  1152. if (pressed) //up
  1153. {
  1154. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  1155. pressed = indeterminate;
  1156. if (!down)
  1157. {
  1158. from--;
  1159. if (from<0)
  1160. from=0;
  1161. draw(screen);
  1162. }
  1163. }
  1164. else if (!pressed) //down
  1165. {
  1166. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  1167. pressed = indeterminate;
  1168. if (!down)
  1169. {
  1170. from++;
  1171. //if (from<items.size()-5)
  1172. // from=items.size()-5;
  1173. draw(screen);
  1174. }
  1175. }
  1176. else
  1177. throw 0;
  1178. }
  1179. }
  1180. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1181. {
  1182. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1183. {
  1184. if (from>0)
  1185. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  1186. else
  1187. LOCPLINT->adventureInt->statusbar.clear();
  1188. return;
  1189. }
  1190. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1191. {
  1192. if ((heroes.size()-from) > SIZE)
  1193. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1194. else
  1195. LOCPLINT->adventureInt->statusbar.clear();
  1196. return;
  1197. }
  1198. //if not buttons then heroes
  1199. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1200. hx-=pos.x;
  1201. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1202. int ny = hy/32;
  1203. if ((ny>SIZE || ny<0) || (from+ny>=heroes.size()))
  1204. {
  1205. LOCPLINT->adventureInt->statusbar.clear();
  1206. return;
  1207. }
  1208. std::vector<std::string> temp;
  1209. temp.push_back(heroes[from+ny]->name);
  1210. temp.push_back(heroes[from+ny]->type->heroClass->name);
  1211. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1212. //select(ny+from);
  1213. }
  1214. void CHeroList::clickRight(tribool down, bool previousState)
  1215. {
  1216. if (down)
  1217. {
  1218. /***************************ARROWS*****************************************/
  1219. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1220. {
  1221. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1222. }
  1223. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (heroes.size()-from>5))
  1224. {
  1225. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1226. }
  1227. else
  1228. {
  1229. //if not buttons then heroes
  1230. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1231. hx-=pos.x;
  1232. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1233. int ny = hy/32;
  1234. if ((ny>SIZE || ny<0) || (from+ny>=heroes.size()))
  1235. {
  1236. return;
  1237. }
  1238. //show popup
  1239. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[heroes[from+ny]->subID],
  1240. GH.current->motion.x-graphics->heroWins[heroes[from+ny]->subID]->w,
  1241. GH.current->motion.y-graphics->heroWins[heroes[from+ny]->subID]->h,
  1242. false);
  1243. GH.pushInt(ip);
  1244. }
  1245. }
  1246. else
  1247. {
  1248. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1249. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1250. }
  1251. }
  1252. void CHeroList::hover (bool on)
  1253. {
  1254. }
  1255. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1256. {
  1257. }
  1258. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1259. {
  1260. if(toRemove) //remove specific hero
  1261. heroes -= toRemove;
  1262. else
  1263. LOCPLINT->recreateWanderingHeroes();
  1264. if(selected >= heroes.size())
  1265. select(heroes.size()-1);
  1266. if(toRemove)
  1267. {
  1268. if(heroes.size() == 0)
  1269. LOCPLINT->adventureInt->townList.select(0);
  1270. else
  1271. select(selected);
  1272. }
  1273. }
  1274. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1275. {
  1276. int ser = -1;
  1277. for(int i=0; i<heroes.size() && ser<0; i++)
  1278. if(heroes[i]->subID == which->subID)
  1279. ser = i;
  1280. ser -= from;
  1281. if(ser<0 || ser > SIZE) return;
  1282. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1283. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1284. }
  1285. void CHeroList::draw(SDL_Surface * to)
  1286. {
  1287. for (int iT=0+from;iT<SIZE+from;iT++)
  1288. {
  1289. int i = iT-from;
  1290. if (iT>=heroes.size())
  1291. {
  1292. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1293. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1294. blitAt(empty,posporx,pospory+i*32,to);
  1295. continue;
  1296. }
  1297. const CGHeroInstance *cur = heroes[iT];
  1298. int pom = cur->movement / 100;
  1299. if (pom>25) pom=25;
  1300. if (pom<0) pom=0;
  1301. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1302. pom = cur->mana / 5;
  1303. if (pom>25) pom=25;
  1304. if (pom<0) pom=0;
  1305. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1306. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1307. blitAt(temp,posporx,pospory+i*32,to);
  1308. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  1309. {
  1310. blitAt(selection,posporx,pospory+i*32,to);
  1311. }
  1312. //TODO: support for custom portraits
  1313. }
  1314. if (from>0)
  1315. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1316. else
  1317. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1318. if (heroes.size()-from > SIZE)
  1319. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1320. else
  1321. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1322. }
  1323. int CHeroList::getPosOfHero(const CGHeroInstance* h)
  1324. {
  1325. return vstd::findPos(heroes, h, std::equal_to<const CGHeroInstance*>());
  1326. }
  1327. void CHeroList::show( SDL_Surface * to )
  1328. {
  1329. }
  1330. CTownList::~CTownList()
  1331. {
  1332. delete arrup;
  1333. delete arrdo;
  1334. }
  1335. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1336. :CList(Size)
  1337. {
  1338. arrup = CDefHandler::giveDef(arrupg);
  1339. arrdo = CDefHandler::giveDef(arrdog);
  1340. pos.x = x;
  1341. pos.y = y;
  1342. pos.w = std::max(arrdo->width, arrup->width);
  1343. pos.h = arrdo->height + arrup->height + Size*32;
  1344. arrupp.x=x;
  1345. arrupp.y=y;
  1346. arrupp.w=arrup->width;
  1347. arrupp.h=arrup->height;
  1348. arrdop.x=x;
  1349. arrdop.y=y+arrup->height+32*SIZE;
  1350. arrdop.w=arrdo->width;
  1351. arrdop.h=arrdo->height;
  1352. posporx = arrdop.x;
  1353. pospory = arrupp.y + arrupp.h;
  1354. pressed = indeterminate;
  1355. from = 0;
  1356. selected = -1;
  1357. }
  1358. void CTownList::genList()
  1359. {
  1360. items.clear();
  1361. int howMany = LOCPLINT->cb->howManyTowns();
  1362. for (int i=0;i<howMany;i++)
  1363. {
  1364. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1365. }
  1366. }
  1367. void CTownList::select(int which)
  1368. {
  1369. if (which>=items.size())
  1370. return;
  1371. selected = which;
  1372. if(!fun.empty())
  1373. fun();
  1374. }
  1375. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1376. {
  1377. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1378. {
  1379. if (from>0)
  1380. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[306].first);
  1381. else
  1382. LOCPLINT->statusbar->clear();
  1383. return;
  1384. }
  1385. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1386. {
  1387. if ((items.size()-from) > SIZE)
  1388. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[307].first);
  1389. else
  1390. LOCPLINT->statusbar->clear();
  1391. return;
  1392. }
  1393. //if not buttons then towns
  1394. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1395. hx-=pos.x;
  1396. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1397. int ny = hy/32;
  1398. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  1399. {
  1400. LOCPLINT->statusbar->clear();
  1401. return;
  1402. };
  1403. LOCPLINT->statusbar->print(items[from+ny]->name);
  1404. }
  1405. void CTownList::clickLeft(tribool down, bool previousState)
  1406. {
  1407. if (down)
  1408. {
  1409. /***************************ARROWS*****************************************/
  1410. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1411. {
  1412. if(from>0)
  1413. {
  1414. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1415. pressed = true;
  1416. }
  1417. return;
  1418. }
  1419. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1420. {
  1421. if(items.size()-from > SIZE)
  1422. {
  1423. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1424. pressed = false;
  1425. }
  1426. return;
  1427. }
  1428. /***************************TOWNS*****************************************/
  1429. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1430. hx-=pos.x;
  1431. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1432. int ny = hy/32;
  1433. if (ny>SIZE || ny<0)
  1434. return;
  1435. if(GH.topInt() == LOCPLINT->adventureInt
  1436. && (ny+from)==selected
  1437. && LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE
  1438. )
  1439. LOCPLINT->openTownWindow(items[selected]);//print town screen
  1440. else
  1441. select(ny+from);
  1442. }
  1443. else
  1444. {
  1445. if (indeterminate(pressed))
  1446. return;
  1447. if (pressed) //up
  1448. {
  1449. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1450. pressed = indeterminate;
  1451. if (!down)
  1452. {
  1453. from--;
  1454. if (from<0)
  1455. from=0;
  1456. draw(screenBuf);
  1457. }
  1458. }
  1459. else if (!pressed) //down
  1460. {
  1461. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1462. pressed = indeterminate;
  1463. if (!down)
  1464. {
  1465. from++;
  1466. //if (from<items.size()-5)
  1467. // from=items.size()-5;
  1468. draw(screenBuf);
  1469. }
  1470. }
  1471. else
  1472. throw 0;
  1473. }
  1474. }
  1475. void CTownList::clickRight(tribool down, bool previousState)
  1476. {
  1477. if (down)
  1478. {
  1479. /***************************ARROWS*****************************************/
  1480. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1481. {
  1482. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1483. }
  1484. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (items.size()-from>5))
  1485. {
  1486. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1487. }
  1488. //if not buttons then towns
  1489. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1490. hx-=pos.x;
  1491. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1492. int ny = hy/32;
  1493. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  1494. {
  1495. return;
  1496. }
  1497. //show popup
  1498. CInfoPopup * ip = new CInfoPopup(
  1499. graphics->townWins[items[from+ny]->id],
  1500. GH.current->motion.x-graphics->townWins[items[from+ny]->id]->w,
  1501. GH.current->motion.y-graphics->townWins[items[from+ny]->id]->h,
  1502. false);
  1503. GH.pushInt(ip);
  1504. }
  1505. else
  1506. {
  1507. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1508. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1509. }
  1510. }
  1511. void CTownList::hover (bool on)
  1512. {
  1513. }
  1514. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1515. {
  1516. }
  1517. void CTownList::draw(SDL_Surface * to)
  1518. {
  1519. for (int iT=0+from;iT<SIZE+from;iT++)
  1520. {
  1521. int i = iT-from;
  1522. if (iT>=items.size())
  1523. {
  1524. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1525. continue;
  1526. }
  1527. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32,to);
  1528. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  1529. {
  1530. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1531. }
  1532. }
  1533. if (from>0)
  1534. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1535. else
  1536. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1537. if (items.size()-from>SIZE)
  1538. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1539. else
  1540. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1541. }
  1542. void CTownList::show( SDL_Surface * to )
  1543. {
  1544. }
  1545. CCreaturePic::CCreaturePic(const CCreature *cre, bool Big)
  1546. :c(cre),big(Big)
  1547. {
  1548. anim = new CCreatureAnimation(cre->animDefName);
  1549. }
  1550. CCreaturePic::~CCreaturePic()
  1551. {
  1552. delete anim;
  1553. }
  1554. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  1555. {
  1556. SDL_Rect dst;
  1557. if(big)
  1558. {
  1559. blitAt(graphics->backgrounds[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1560. dst = genRect(130,100,x,y);
  1561. }
  1562. else
  1563. {
  1564. blitAt(graphics->backgroundsm[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1565. dst = genRect(120,100,x,y);
  1566. }
  1567. if(c->isDoubleWide())
  1568. x-=15;
  1569. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  1570. }
  1571. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  1572. {
  1573. //TODO: write
  1574. return NULL;
  1575. }
  1576. void CRecruitmentWindow::close()
  1577. {
  1578. GH.popIntTotally(this);
  1579. }
  1580. void CRecruitmentWindow::Max()
  1581. {
  1582. slider->moveTo(slider->amount);
  1583. }
  1584. void CRecruitmentWindow::Buy()
  1585. {
  1586. int crid = creatures[which].ID,
  1587. dstslot = dst->army.getSlotFor(crid);
  1588. if(dstslot < 0) //no available slot
  1589. {
  1590. std::string txt;
  1591. if(dst->ID == HEROI_TYPE)
  1592. {
  1593. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1594. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid].namePl : CGI->creh->creatures[crid].nameSing);
  1595. }
  1596. else
  1597. {
  1598. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1599. }
  1600. LOCPLINT->showInfoDialog(txt);
  1601. return;
  1602. }
  1603. recruit(crid, slider->value);
  1604. if(level >= 0)
  1605. close();
  1606. else
  1607. slider->moveTo(0);
  1608. }
  1609. void CRecruitmentWindow::Cancel()
  1610. {
  1611. close();
  1612. }
  1613. void CRecruitmentWindow::sliderMoved(int to)
  1614. {
  1615. buy->block(!to);
  1616. }
  1617. void CRecruitmentWindow::clickLeft(tribool down, bool previousState)
  1618. {
  1619. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1620. for(int i=0;i<creatures.size();i++)
  1621. {
  1622. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1623. if(isItIn(&genRect(132,sCREATURE_WIDTH,pos.x+curx,pos.y+64),GH.current->motion.x,GH.current->motion.y))
  1624. {
  1625. which = i;
  1626. int newAmount = std::min(amounts[i],creatures[i].amount);
  1627. slider->setAmount(newAmount);
  1628. if(slider->value > newAmount)
  1629. slider->moveTo(newAmount);
  1630. else
  1631. slider->moveTo(slider->value);
  1632. curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1633. for(int j=0;j<creatures.size();j++)
  1634. {
  1635. if(which==j)
  1636. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(255,0,0));
  1637. else
  1638. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(239,215,123));
  1639. curx += TOTAL_CREATURE_WIDTH;
  1640. }
  1641. break;
  1642. }
  1643. curx += TOTAL_CREATURE_WIDTH;
  1644. }
  1645. }
  1646. void CRecruitmentWindow::clickRight(tribool down, bool previousState)
  1647. {
  1648. if(down)
  1649. {
  1650. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1651. for(int i=0;i<creatures.size();i++)
  1652. {
  1653. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1654. if(isItIn(&genRect(132,sCREATURE_WIDTH,pos.x+curx,pos.y+64),GH.current->motion.x,GH.current->motion.y))
  1655. {
  1656. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID, 0, 0, NULL, NULL, NULL, NULL);
  1657. GH.pushInt(popup);
  1658. break;
  1659. }
  1660. curx += TOTAL_CREATURE_WIDTH;
  1661. }
  1662. }
  1663. }
  1664. void CRecruitmentWindow::activate()
  1665. {
  1666. activateLClick();
  1667. activateRClick();
  1668. buy->activate();
  1669. max->activate();
  1670. cancel->activate();
  1671. slider->activate();
  1672. LOCPLINT->statusbar = bar;
  1673. }
  1674. void CRecruitmentWindow::deactivate()
  1675. {
  1676. deactivateLClick();
  1677. deactivateRClick();
  1678. buy->deactivate();
  1679. max->deactivate();
  1680. cancel->deactivate();
  1681. slider->deactivate();
  1682. }
  1683. void CRecruitmentWindow::show(SDL_Surface * to)
  1684. {
  1685. static char animCounter=0; //animation counter - for determining appropriate animation frame to be shown
  1686. blitAt(bitmap,pos.x,pos.y,to);
  1687. buy->show(to);
  1688. max->show(to);
  1689. cancel->show(to);
  1690. slider->show(to);
  1691. char pom[15];
  1692. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1693. printAtMiddle(pom,pos.x+205,pos.y+252,GEOR13,zwykly,to);
  1694. SDL_itoa(slider->value,pom,10); //recruit
  1695. printAtMiddle(pom,pos.x+279,pos.y+252,GEOR13,zwykly,to);
  1696. 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"
  1697. int curx = pos.x+115-creatures[which].res.size()*16;
  1698. for(int i=0;i<creatures[which].res.size();i++)
  1699. {
  1700. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,to);
  1701. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,to);
  1702. SDL_itoa(creatures[which].res[i].second,pom,10);
  1703. printAtMiddle(pom,curx+12,pos.y+286,GEOR13,zwykly,to);
  1704. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1705. printAtMiddle(pom,curx+12+258,pos.y+286,GEOR13,zwykly,to);
  1706. curx+=32;
  1707. }
  1708. curx = pos.x + 192 + CREATURE_WIDTH - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1709. for(int i=0; i<creatures.size(); ++i)
  1710. {
  1711. creatures[i].pic->blitPic(to, curx-50, pos.y+130-65, !(animCounter%4));
  1712. curx += TOTAL_CREATURE_WIDTH;
  1713. }
  1714. ++animCounter;
  1715. bar->show(to);
  1716. }
  1717. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> &Recruit)
  1718. :recruit(Recruit), dwelling(Dwelling), dst(Dst), level(Level)
  1719. {
  1720. which = 0;
  1721. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  1722. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1723. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1724. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1725. SDL_FreeSurface(hhlp);
  1726. pos.x = screen->w/2 - bitmap->w/2;
  1727. pos.y = screen->h/2 - bitmap->h/2;
  1728. pos.w = bitmap->w;
  1729. pos.h = bitmap->h;
  1730. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  1731. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  1732. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  1733. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  1734. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecruitmentWindow::sliderMoved,this, _1),1,0,0,true);
  1735. initCres();
  1736. printAtMiddle(CGI->generaltexth->allTexts[346],113,231,GEOR13,zwykly,bitmap); //cost per troop t
  1737. printAtMiddle(CGI->generaltexth->allTexts[465],205,231,GEOR13,zwykly,bitmap); //available t
  1738. printAtMiddle(CGI->generaltexth->allTexts[16],279,231,GEOR13,zwykly,bitmap); //recruit t
  1739. printAtMiddle(CGI->generaltexth->allTexts[466],373,231,GEOR13,zwykly,bitmap); //total cost t
  1740. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  1741. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  1742. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  1743. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  1744. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  1745. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  1746. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  1747. //border for creatures
  1748. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1749. for(int i=0;i<creatures.size();i++)
  1750. {
  1751. creatures[i].pos.x = curx+1;
  1752. creatures[i].pos.y = 65;
  1753. creatures[i].pos.w = 100;
  1754. creatures[i].pos.h = 130;
  1755. if(which==i)
  1756. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(255,0,0));
  1757. else
  1758. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(239,215,123));
  1759. curx += TOTAL_CREATURE_WIDTH;
  1760. }
  1761. if(!creatures[0].amount || !amounts[0])
  1762. {
  1763. max->block(true);
  1764. slider->block(true);
  1765. }
  1766. //buy->block(true); //not needed, will be blocked by initing slider on 0
  1767. }
  1768. CRecruitmentWindow::~CRecruitmentWindow()
  1769. {
  1770. cleanCres();
  1771. delete max;
  1772. delete buy;
  1773. delete cancel;
  1774. SDL_FreeSurface(bitmap);
  1775. delete slider;
  1776. delete bar;
  1777. }
  1778. void CRecruitmentWindow::initCres()
  1779. {
  1780. cleanCres();
  1781. for(int i=0; i<dwelling->creatures.size(); i++)
  1782. {
  1783. if(level >= 0 && i != level)
  1784. continue;
  1785. for(int j = dwelling->creatures[i].second.size() - 1; j >= 0 ; j--)
  1786. {
  1787. creatures.resize(creatures.size()+1);
  1788. creinfo &cur = creatures.back();
  1789. cur.amount = dwelling->creatures[i].first;
  1790. cur.ID = dwelling->creatures[i].second[j];
  1791. const CCreature *cre = &CGI->creh->creatures[cur.ID];
  1792. cur.pic = new CCreaturePic(cre);
  1793. for(int k=0; k<cre->cost.size(); k++)
  1794. if(cre->cost[k])
  1795. cur.res.push_back(std::make_pair(k,cre->cost[k]));
  1796. amounts.push_back(cre->maxAmount(LOCPLINT->cb->getResourceAmount()));
  1797. }
  1798. }
  1799. slider->setAmount(std::min(amounts[which],creatures[which].amount));
  1800. }
  1801. void CRecruitmentWindow::cleanCres()
  1802. {
  1803. for(int i=0;i<creatures.size();i++)
  1804. {
  1805. delete creatures[i].pic;
  1806. }
  1807. creatures.clear();
  1808. amounts.clear();
  1809. }
  1810. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1811. {
  1812. last = Last;
  1813. which = 1;
  1814. c=cid;
  1815. slider = NULL;
  1816. gar = Owner;
  1817. bitmap = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1818. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  1819. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1820. pos.x = screen->w/2 - bitmap->w/2;
  1821. pos.y = screen->h/2 - bitmap->h/2;
  1822. pos.w = bitmap->w;
  1823. pos.h = bitmap->h;
  1824. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1825. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1826. int sliderPositions = max - (last>=0) - (last==2);
  1827. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),1,sliderPositions,val,true);
  1828. a1 = max-val;
  1829. a2 = val;
  1830. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  1831. anim->anim->setType(1);
  1832. std::string title = CGI->generaltexth->allTexts[256];
  1833. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  1834. printAtMiddle(title,150,34,GEOR16,tytulowy,bitmap);
  1835. }
  1836. CSplitWindow::~CSplitWindow() //d-tor
  1837. {
  1838. SDL_FreeSurface(bitmap);
  1839. delete ok;
  1840. delete cancel;
  1841. delete slider;
  1842. delete anim;
  1843. }
  1844. void CSplitWindow::activate()
  1845. {
  1846. activateLClick();
  1847. activateKeys();
  1848. ok->activate();
  1849. cancel->activate();
  1850. slider->activate();
  1851. }
  1852. void CSplitWindow::deactivate()
  1853. {
  1854. deactivateLClick();
  1855. deactivateKeys();
  1856. ok->deactivate();
  1857. cancel->deactivate();
  1858. slider->deactivate();
  1859. }
  1860. void CSplitWindow::split()
  1861. {
  1862. gar->splitStacks(a2);
  1863. close();
  1864. }
  1865. void CSplitWindow::close()
  1866. {
  1867. GH.popIntTotally(this);
  1868. }
  1869. void CSplitWindow::sliderMoved(int to)
  1870. {
  1871. int all = a1+a2;
  1872. a2 = to + (last==1 || last==2);
  1873. if(slider)
  1874. a1 = all - a2;
  1875. }
  1876. void CSplitWindow::show(SDL_Surface * to)
  1877. {
  1878. blitAt(bitmap,pos.x,pos.y,to);
  1879. ok->show(to);
  1880. cancel->show(to);
  1881. slider->show(to);
  1882. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,GEOR16,zwykly,to);
  1883. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,GEOR16,zwykly,to);
  1884. anim->blitPic(to,pos.x+20,pos.y+54,false);
  1885. anim->blitPic(to,pos.x+177,pos.y+54,false);
  1886. }
  1887. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1888. {
  1889. SDLKey k = key.keysym.sym;
  1890. if (isNumKey(k)) //convert numpad number to normal digit
  1891. k = numToDigit(k);
  1892. if(key.state != SDL_PRESSED)
  1893. return;
  1894. int &cur = (which ? a2 : a1),
  1895. &sec = (which ? a1 : a2),
  1896. ncur = cur;
  1897. if (k == SDLK_BACKSPACE)
  1898. {
  1899. ncur /= 10;
  1900. }
  1901. else if(k == SDLK_TAB)
  1902. {
  1903. which = !which;
  1904. }
  1905. else
  1906. {
  1907. int number = k - SDLK_0;
  1908. if (number < 0 || number > 9) //not a number pressed
  1909. {
  1910. return;
  1911. }
  1912. ncur = cur*10 + number;
  1913. }
  1914. int delta = ncur - cur;
  1915. if(delta > sec)
  1916. {
  1917. cur += sec;
  1918. sec = 0;
  1919. }
  1920. slider->moveTo(which ? ncur : a1+a2-ncur);
  1921. }
  1922. void CSplitWindow::clickLeft(tribool down, bool previousState)
  1923. {
  1924. if(down)
  1925. {
  1926. Point click(GH.current->motion.x,GH.current->motion.y);
  1927. click -= pos.topLeft();
  1928. if(Rect(19,216,105,40).isIn(click)) //left picture
  1929. which = 0;
  1930. else if(Rect(175,216,105,40).isIn(click)) //right picture
  1931. which = 1;
  1932. }
  1933. }
  1934. void CCreInfoWindow::show(SDL_Surface * to)
  1935. {
  1936. char pom[15];
  1937. blitAt(bitmap,pos.x,pos.y,to);
  1938. anim->blitPic(to,pos.x+21,pos.y+48,(type) && !(anf%4));
  1939. if(++anf==4)
  1940. anf=0;
  1941. if(count.size())
  1942. printTo(count.c_str(),pos.x+114,pos.y+174,GEOR16,zwykly,to);
  1943. if(upgrade)
  1944. upgrade->show(to);
  1945. if(dismiss)
  1946. dismiss->show(to);
  1947. if(ok)
  1948. ok->show(to);
  1949. }
  1950. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  1951. : type(Type), dsm(Dsm), dismiss(0), upgrade(0), ok(0)
  1952. {
  1953. //active = false;
  1954. anf = 0;
  1955. c = &CGI->creh->creatures[Cid];
  1956. bitmap = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  1957. pos.x = screen->w/2 - bitmap->w/2;
  1958. pos.y = screen->h/2 - bitmap->h/2;
  1959. pos.w = bitmap->w;
  1960. pos.h = bitmap->h;
  1961. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  1962. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1963. anim = new CCreaturePic(c);
  1964. if(!type) anim->anim->setType(2);
  1965. char pom[75];int hlp=0;
  1966. if(creatureCount)
  1967. {
  1968. SDL_itoa(creatureCount,pom,10);
  1969. count = pom;
  1970. }
  1971. printAtMiddle(c->namePl,149,30,GEOR13,tytulowy,bitmap); //creature name
  1972. //atttack
  1973. printAt(CGI->generaltexth->primarySkillNames[0],155,48,GEOR13,zwykly,bitmap);
  1974. SDL_itoa(c->attack,pom,10);
  1975. if(State && State->attackBonus)
  1976. {
  1977. int hlp;
  1978. if(c->attack > 0)
  1979. hlp = log10f(c->attack)+2;
  1980. else
  1981. hlp = 2;
  1982. pom[hlp-1] = ' '; pom[hlp] = '(';
  1983. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  1984. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  1985. pom[hlp] = ')'; pom[hlp+1] = '\0';
  1986. }
  1987. printToWR(pom,276,61,GEOR13,zwykly,bitmap);
  1988. //defense
  1989. printAt(CGI->generaltexth->primarySkillNames[1],155,67,GEOR13,zwykly,bitmap);
  1990. SDL_itoa(c->defence,pom,10);
  1991. if(State && State->defenseBonus)
  1992. {
  1993. int hlp;
  1994. if(c->defence > 0)
  1995. hlp = log10f(c->defence)+2;
  1996. else
  1997. hlp = 2;
  1998. pom[hlp-1] = ' '; pom[hlp] = '(';
  1999. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  2000. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  2001. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2002. }
  2003. printToWR(pom,276,80,GEOR13,zwykly,bitmap);
  2004. //shots
  2005. if(c->shots)
  2006. {
  2007. printAt(CGI->generaltexth->allTexts[198], 155, 86, GEOR13, zwykly, bitmap);
  2008. if(State && State->shotsLeft >= 0)
  2009. sprintf(pom,"%d (%d)", c->shots, State->shotsLeft);
  2010. else
  2011. SDL_itoa(c->shots, pom, 10);
  2012. printToWR(pom, 276, 99, GEOR13, zwykly, bitmap);
  2013. }
  2014. //damage
  2015. int dmgMin = c->damageMin * (State ? State->dmgMultiplier : 1);
  2016. int dmgMax = c->damageMax * (State ? State->dmgMultiplier : 1);
  2017. printAt(CGI->generaltexth->allTexts[199], 155, 105, GEOR13, zwykly, bitmap);
  2018. SDL_itoa(dmgMin, pom, 10);
  2019. if(dmgMin > 0)
  2020. hlp = log10f(dmgMin) + 2;
  2021. else
  2022. hlp = 2;
  2023. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  2024. SDL_itoa(dmgMax, pom+hlp+2, 10);
  2025. printToWR(pom, 276, 118, GEOR13, zwykly, bitmap);
  2026. //health
  2027. printAt(CGI->generaltexth->allTexts[388],155,124,GEOR13,zwykly,bitmap);
  2028. if(State && State->healthBonus)
  2029. sprintf(pom,"%d (%d)",c->hitPoints, c->hitPoints + State->healthBonus);
  2030. else
  2031. SDL_itoa(c->hitPoints,pom,10);
  2032. printToWR(pom,276,137,GEOR13,zwykly,bitmap);
  2033. //remaining health
  2034. if(State && State->currentHealth)
  2035. {
  2036. printAt(CGI->generaltexth->allTexts[200],155,143,GEOR13,zwykly,bitmap);
  2037. SDL_itoa(State->currentHealth,pom,10);
  2038. printToWR(pom,276,156,GEOR13,zwykly,bitmap);
  2039. }
  2040. //speed
  2041. printAt(CGI->generaltexth->zelp[441].first,155,162,GEOR13,zwykly,bitmap);
  2042. SDL_itoa(c->speed,pom,10);
  2043. if(State && State->speedBonus)
  2044. {
  2045. int hlp;
  2046. if(c->speed > 0)
  2047. hlp = log10f(c->speed)+2;
  2048. else
  2049. hlp = 2;
  2050. pom[hlp-1] = ' '; pom[hlp] = '(';
  2051. SDL_itoa(c->speed + State->speedBonus, pom+hlp+1, 10);
  2052. hlp += 2+(int)log10f(c->speed + State->speedBonus);
  2053. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2054. }
  2055. printToWR(pom,276,175,GEOR13,zwykly,bitmap);
  2056. //luck and morale
  2057. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  2058. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  2059. //print abilities text - if r-click popup
  2060. if(type)
  2061. {
  2062. if(Upg && ui)
  2063. {
  2064. bool enough = true;
  2065. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  2066. {
  2067. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  2068. enough = false;
  2069. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  2070. }
  2071. if(enough)
  2072. {
  2073. CFunctionList<void()> fs[2];
  2074. fs[0] += Upg;
  2075. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  2076. CFunctionList<void()> cfl;
  2077. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false);
  2078. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF",SDLK_u);
  2079. }
  2080. else
  2081. {
  2082. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  2083. upgrade->callback.funcs.clear();
  2084. upgrade->bitmapOffset = 2;
  2085. }
  2086. }
  2087. if(Dsm)
  2088. {
  2089. CFunctionList<void()> fs[2];
  2090. //on dismiss confirmed
  2091. fs[0] += Dsm; //dismiss
  2092. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  2093. CFunctionList<void()> cfl;
  2094. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  2095. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF",SDLK_d);
  2096. }
  2097. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF",SDLK_RETURN);
  2098. }
  2099. else
  2100. {
  2101. printAtWB(c->abilityText,17,231,GEOR13,35,zwykly,bitmap);
  2102. }
  2103. //spell effects
  2104. if(State)
  2105. {
  2106. int printed=0; //how many effect pics have been printed
  2107. for(std::set<int>::const_iterator it = State->effects.begin(); it!=State->effects.end(); ++it)
  2108. {
  2109. blitAt(graphics->spellEffectsPics->ourImages[*it + 1].bitmap, 127 + 52 * printed, 186, bitmap);
  2110. ++printed;
  2111. if(printed >= 3)
  2112. {
  2113. break;
  2114. }
  2115. }
  2116. }
  2117. }
  2118. CCreInfoWindow::~CCreInfoWindow()
  2119. {
  2120. SDL_FreeSurface(bitmap);
  2121. delete anim;
  2122. delete upgrade;
  2123. delete ok;
  2124. delete dismiss;
  2125. for(int i=0; i<upgResCost.size();i++)
  2126. delete upgResCost[i];
  2127. }
  2128. void CCreInfoWindow::activate()
  2129. {
  2130. //if(active) return;
  2131. //active = true;
  2132. if(!type)
  2133. activateRClick();
  2134. if(ok)
  2135. ok->activate();
  2136. if(dismiss)
  2137. dismiss->activate();
  2138. if(upgrade)
  2139. upgrade->activate();
  2140. }
  2141. void CCreInfoWindow::close()
  2142. {
  2143. GH.popIntTotally(this);
  2144. }
  2145. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  2146. {
  2147. if(down)
  2148. return;
  2149. close();
  2150. }
  2151. void CCreInfoWindow::dismissF()
  2152. {
  2153. dsm();
  2154. close();
  2155. }
  2156. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  2157. {
  2158. }
  2159. void CCreInfoWindow::deactivate()
  2160. {
  2161. //if(!active) return;
  2162. //active = false;
  2163. if(!type)
  2164. deactivateRClick();
  2165. if(ok)
  2166. ok->deactivate();
  2167. if(dismiss)
  2168. dismiss->deactivate();
  2169. if(upgrade)
  2170. upgrade->deactivate();
  2171. }
  2172. void CLevelWindow::close()
  2173. {
  2174. for(int i=0;i<comps.size();i++)
  2175. {
  2176. if(comps[i]->selected)
  2177. {
  2178. cb(i);
  2179. break;
  2180. }
  2181. }
  2182. GH.popIntTotally(this);
  2183. LOCPLINT->showingDialog->setn(false);
  2184. }
  2185. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  2186. {
  2187. LOCPLINT->showingDialog->setn(true);
  2188. heroType = hero->subID;
  2189. cb = callback;
  2190. for(int i=0;i<skills.size();i++)
  2191. {
  2192. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  2193. comps.back()->assignedKeys.insert(SDLK_1 + i);
  2194. }
  2195. bitmap = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  2196. graphics->blueToPlayersAdv(bitmap,hero->tempOwner);
  2197. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2198. pos.x = screen->w/2 - bitmap->w/2;
  2199. pos.y = screen->h/2 - bitmap->h/2;
  2200. pos.w = bitmap->w;
  2201. pos.h = bitmap->h;
  2202. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  2203. //draw window
  2204. char buf[100], buf2[100];
  2205. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  2206. sprintf(buf,buf2,hero->name.c_str());
  2207. printAtMiddle(buf,192,35,GEOR16,zwykly,bitmap);
  2208. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  2209. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  2210. printAtMiddle(buf,192,162,GEOR16,zwykly,bitmap);
  2211. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  2212. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,252,GEOR16,zwykly,bitmap);
  2213. SDL_Surface * ort = TTF_RenderText_Blended(GEOR16,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  2214. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+ort->w) )/2;
  2215. for(int i=0;i<comps.size();i++)
  2216. {
  2217. comps[i]->pos.x = curx+pos.x;
  2218. comps[i]->pos.y = 326+pos.y;
  2219. if( i < (comps.size()-1) )
  2220. {
  2221. curx += 44 + 18; //skill width + margin to "or"
  2222. blitAt(ort,curx,346,bitmap);
  2223. curx += ort->w + 18;
  2224. }
  2225. }
  2226. if(comps.size() > 1)
  2227. {
  2228. ok->block(true);
  2229. }
  2230. else if(comps.size() == 1)
  2231. {
  2232. comps[0]->select(true);
  2233. }
  2234. SDL_FreeSurface(ort);
  2235. }
  2236. void CLevelWindow::selectionChanged(unsigned to)
  2237. {
  2238. if(ok->blocked)
  2239. ok->block(false);
  2240. for(int i=0;i<comps.size();i++)
  2241. if(i==to)
  2242. comps[i]->select(true);
  2243. else
  2244. comps[i]->select(false);
  2245. }
  2246. CLevelWindow::~CLevelWindow()
  2247. {
  2248. delete ok;
  2249. for(int i=0;i<comps.size();i++)
  2250. delete comps[i];
  2251. SDL_FreeSurface(bitmap);
  2252. }
  2253. void CLevelWindow::activate()
  2254. {
  2255. ok->activate();
  2256. for(int i=0;i<comps.size();i++)
  2257. comps[i]->activate();
  2258. }
  2259. void CLevelWindow::deactivate()
  2260. {
  2261. ok->deactivate();
  2262. for(int i=0;i<comps.size();i++)
  2263. comps[i]->deactivate();
  2264. }
  2265. void CLevelWindow::show(SDL_Surface * to)
  2266. {
  2267. blitAt(bitmap,pos.x,pos.y,to);
  2268. blitAt(graphics->portraitLarge[heroType],170+pos.x,66+pos.y,to);
  2269. ok->show(to);
  2270. for(int i=0;i<comps.size();i++)
  2271. comps[i]->show(to);
  2272. }
  2273. void CMinorResDataBar::show(SDL_Surface * to)
  2274. {
  2275. blitAt(bg,pos.x,pos.y,to);
  2276. char buf[30];
  2277. for (int i=0;i<7;i++)
  2278. {
  2279. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2280. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly,to);
  2281. }
  2282. std::vector<std::string> temp;
  2283. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  2284. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  2285. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  2286. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  2287. CGI->generaltexth->allTexts[62]
  2288. +": %s, "
  2289. + CGI->generaltexth->allTexts[63]
  2290. + ": %s, "
  2291. + CGI->generaltexth->allTexts[64]
  2292. + ": %s",temp)
  2293. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly,to);
  2294. }
  2295. CMinorResDataBar::CMinorResDataBar()
  2296. {
  2297. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  2298. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2299. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2300. pos.x = 7;
  2301. pos.y = 575;
  2302. pos.w = bg->w;
  2303. pos.h = bg->h;
  2304. }
  2305. CMinorResDataBar::~CMinorResDataBar()
  2306. {
  2307. SDL_FreeSurface(bg);
  2308. }
  2309. SDL_Surface * CCustomImgComponent::getImg()
  2310. {
  2311. return bmp;
  2312. }
  2313. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  2314. :bmp(sur), free(freeSur)
  2315. {
  2316. init(Type,Subtype,Val);
  2317. }
  2318. CCustomImgComponent::~CCustomImgComponent()
  2319. {
  2320. if(free)
  2321. SDL_FreeSurface(bmp);
  2322. }
  2323. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  2324. {
  2325. left = Left;
  2326. type = Type;
  2327. id = ID;
  2328. }
  2329. void CMarketplaceWindow::CTradeableItem::show(SDL_Surface * to)
  2330. {
  2331. SDL_Surface *hlp = getSurface();
  2332. blitAt(hlp,pos.x+19,pos.y+9,to);
  2333. }
  2334. void CMarketplaceWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  2335. {
  2336. CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt());
  2337. assert(mw);
  2338. if(down)
  2339. {
  2340. if(left)
  2341. {
  2342. if(mw->hLeft != this)
  2343. mw->hLeft = this;
  2344. else
  2345. return;
  2346. }
  2347. else
  2348. {
  2349. if(mw->hRight != this)
  2350. mw->hRight = this;
  2351. else
  2352. return;
  2353. }
  2354. mw->selectionChanged(left);
  2355. }
  2356. }
  2357. void CMarketplaceWindow::CTradeableItem::activate()
  2358. {
  2359. activateLClick();
  2360. }
  2361. void CMarketplaceWindow::CTradeableItem::deactivate()
  2362. {
  2363. deactivateLClick();
  2364. }
  2365. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  2366. {
  2367. switch(type)
  2368. {
  2369. case 0:
  2370. return graphics->resources32->ourImages[id].bitmap;
  2371. case 1:
  2372. return graphics->artDefs->ourImages[id].bitmap;
  2373. default:
  2374. return NULL;
  2375. }
  2376. }
  2377. static void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  2378. {
  2379. i.resize(amount);
  2380. for(int j=0;j<amount;j++)
  2381. {
  2382. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  2383. i[j]->pos = p[j];
  2384. }
  2385. }
  2386. void CMarketplaceWindow::setMode( int mode )
  2387. {
  2388. std::vector<SDL_Rect> lpos, rpos;
  2389. clear();
  2390. switch(mode)
  2391. {
  2392. case 0:
  2393. {
  2394. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  2395. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  2396. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  2397. bg = SDL_ConvertSurface(bg2,screen->format,0);
  2398. SDL_FreeSurface(bg2);
  2399. lpos += genRect(66,74,39,180), genRect(66,74,122,180), genRect(66,74,204,180),
  2400. genRect(66,74,39,259), genRect(66,74,122,259), genRect(66,74,204,259),
  2401. genRect(66,74,122,338);
  2402. for(int i=0;i<lpos.size();i++)
  2403. {
  2404. lpos[i].x += pos.x;
  2405. lpos[i].y += pos.y;
  2406. rpos.push_back(lpos[i]);
  2407. rpos[rpos.size()-1].x += 288;
  2408. }
  2409. initItems(left,lpos,0,7,true,NULL);
  2410. initItems(right,rpos,0,7,false,NULL);
  2411. printAtMiddle(CGI->generaltexth->allTexts[158],300,27,FONT_BIG,tytulowy,bg); //title
  2412. printAtMiddle(CGI->generaltexth->allTexts[270],155,148,FONT_SMALL,zwykly,bg); //kingdom res.
  2413. printAtMiddle(CGI->generaltexth->allTexts[168],450,147,FONT_SMALL,zwykly,bg); //available for trade
  2414. }
  2415. }
  2416. }
  2417. void CMarketplaceWindow::clear()
  2418. {
  2419. for(int i=0;i<left.size();i++)
  2420. delete left[i];
  2421. for(int i=0;i<right.size();i++)
  2422. delete right[i];
  2423. left.clear();
  2424. right.clear();
  2425. SDL_FreeSurface(bg);
  2426. }
  2427. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  2428. {
  2429. type = BLOCK_ADV_HOTKEYS;
  2430. mode = Mode;
  2431. bg = NULL;
  2432. ok = max = deal = NULL;
  2433. pos.x = screen->w/2 - 300;
  2434. pos.y = screen->h/2 - 296;
  2435. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  2436. setMode(mode);
  2437. hLeft = hRight = NULL;
  2438. ok = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH,this),pos.x+516,pos.y+520,"IOK6432.DEF",SDLK_RETURN);
  2439. ok->assignedKeys.insert(SDLK_ESCAPE);
  2440. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  2441. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  2442. max->block(true);
  2443. deal->block(true);
  2444. }
  2445. CMarketplaceWindow::~CMarketplaceWindow()
  2446. {
  2447. clear();
  2448. delete slider;
  2449. }
  2450. void CMarketplaceWindow::show(SDL_Surface * to)
  2451. {
  2452. blitAt(bg,pos,to);
  2453. if(hRight)
  2454. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2455. if(hLeft)
  2456. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2457. ok->show(to);
  2458. deal->show(to);
  2459. max->show(to);
  2460. slider->show(to);
  2461. for(int i=0;i<left.size();i++)
  2462. left[i]->show(to);
  2463. for(int i=0;i<right.size();i++)
  2464. right[i]->show(to);
  2465. if(mode==0)
  2466. {
  2467. char buf[15];
  2468. for(int i=0;i<left.size();i++)
  2469. {
  2470. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2471. printAtMiddle(buf,left[i]->pos.x+36,left[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2472. }
  2473. if(hLeft) //print prices
  2474. {
  2475. for(int i=0; i<right.size();i++)
  2476. {
  2477. if(right[i]->id != hLeft->id)
  2478. printAtMiddle(rSubs[i],right[i]->pos.x+36,right[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2479. else
  2480. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+36,right[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2481. }
  2482. }
  2483. if(hLeft && hRight && hLeft->id!= hRight->id)
  2484. {
  2485. blitAt(hLeft->getSurface(),pos.x+141,pos.y+457,to);
  2486. blitAt(hRight->getSurface(),pos.x+429,pos.y+457,to);
  2487. SDL_itoa(slider->value * r1,buf,10);
  2488. printAtMiddle(buf,pos.x+156,pos.y+505,FONT_SMALL,zwykly,to);
  2489. SDL_itoa(slider->value * r2,buf,10);
  2490. printAtMiddle(buf,pos.x+443,pos.y+505,FONT_SMALL,zwykly,to);
  2491. }
  2492. }
  2493. }
  2494. void CMarketplaceWindow::activate()
  2495. {
  2496. for(int i=0;i<left.size();i++)
  2497. left[i]->activate();
  2498. for(int i=0;i<right.size();i++)
  2499. right[i]->activate();
  2500. ok->activate();
  2501. max->activate();
  2502. deal->activate();
  2503. slider->activate();
  2504. }
  2505. void CMarketplaceWindow::deactivate()
  2506. {
  2507. for(int i=0;i<left.size();i++)
  2508. left[i]->deactivate();
  2509. for(int i=0;i<right.size();i++)
  2510. right[i]->deactivate();
  2511. ok->deactivate();
  2512. max->deactivate();
  2513. deal->deactivate();
  2514. slider->deactivate();
  2515. }
  2516. void CMarketplaceWindow::setMax()
  2517. {
  2518. slider->moveTo(slider->amount);
  2519. }
  2520. void CMarketplaceWindow::makeDeal()
  2521. {
  2522. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  2523. slider->moveTo(0);
  2524. hLeft = NULL;
  2525. selectionChanged(true);
  2526. }
  2527. void CMarketplaceWindow::sliderMoved( int to )
  2528. {
  2529. }
  2530. void CMarketplaceWindow::selectionChanged(bool side)
  2531. {
  2532. if(hLeft && hRight && hLeft->id!= hRight->id)
  2533. {
  2534. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  2535. slider->setAmount(LOCPLINT->cb->getResourceAmount(hLeft->id) / r1);
  2536. slider->moveTo(0);
  2537. max->block(false);
  2538. deal->block(false);
  2539. }
  2540. else
  2541. {
  2542. max->block(true);
  2543. deal->block(true);
  2544. slider->setAmount(0);
  2545. slider->moveTo(0);
  2546. }
  2547. if(side && hLeft) //left selection changed, recalculate offers
  2548. {
  2549. rSubs.clear();
  2550. rSubs.resize(right.size());
  2551. int h1, h2;
  2552. for(int i=0;i<right.size();i++)
  2553. {
  2554. std::ostringstream oss;
  2555. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  2556. oss << h2;
  2557. if(h1!=1)
  2558. oss << "/" << h1;
  2559. rSubs[i] = oss.str();
  2560. }
  2561. }
  2562. }
  2563. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  2564. {
  2565. this->pos = pos;
  2566. background = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  2567. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2568. //printing texts
  2569. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 240, 32, GEOR16, tytulowy, background); //window title
  2570. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 65, GEOR16, tytulowy, background); //hero speed
  2571. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 131, GEOR16, tytulowy, background); //enemy speed
  2572. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 197, GEOR16, tytulowy, background); //map scroll speed
  2573. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 263, GEOR16, tytulowy, background); //video quality
  2574. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 348, GEOR16, tytulowy, background); //music volume
  2575. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 414, GEOR16, tytulowy, background); //effects volume
  2576. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 283, 57, GEOR16, zwykly, background); //show move path
  2577. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 283, 89, GEOR16, zwykly, background); //show hero reminder
  2578. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 283, 121, GEOR16, zwykly, background); //quick combat
  2579. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 283, 153, GEOR16, zwykly, background); //video subtitles
  2580. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 283, 185, GEOR16, zwykly, background); //town building outlines
  2581. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 283, 217, GEOR16, zwykly, background); //spell book animation
  2582. //setting up buttons
  2583. save = new AdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::bsavef, this), 516, 354, "SOSAVE.DEF", SDLK_s);
  2584. std::swap(save->imgs[0][0], save->imgs[0][1]);
  2585. quitGame = new AdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), 405, 471, "soquit.def", SDLK_q);
  2586. std::swap(quitGame->imgs[0][0], quitGame->imgs[0][1]);
  2587. backToMap = new AdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), 516, 471, "soretrn.def", SDLK_RETURN);
  2588. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  2589. std::swap(backToMap->imgs[0][0], backToMap->imgs[0][1]);
  2590. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  2591. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].first),CGI->generaltexth->zelp[349].second, "sysopb1.def", 187, 134, 1);
  2592. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].first),CGI->generaltexth->zelp[350].second, "sysopb2.def", 235, 134, 2);
  2593. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].first),CGI->generaltexth->zelp[351].second, "sysopb3.def", 283, 134, 4);
  2594. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].first),CGI->generaltexth->zelp[352].second, "sysopb4.def", 331, 134, 8);
  2595. heroMoveSpeed->select(owner->sysOpts.heroMoveSpeed, 1);
  2596. heroMoveSpeed->onChange = boost::bind(&SystemOptions::setHeroMoveSpeed, &owner->sysOpts, _1);
  2597. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  2598. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].first),CGI->generaltexth->zelp[357].second, "sysopb9.def", 187, 267, 1);
  2599. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].first),CGI->generaltexth->zelp[358].second, "sysob10.def", 251, 267, 2);
  2600. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].first),CGI->generaltexth->zelp[359].second, "sysob11.def", 315, 267, 4);
  2601. mapScrollSpeed->select(owner->sysOpts.mapScrollingSpeed, 1);
  2602. mapScrollSpeed->onChange = boost::bind(&SystemOptions::setMapScrollingSpeed, &owner->sysOpts, _1);
  2603. musicVolume = new CHighlightableButtonsGroup(0, true);
  2604. for(int i=0; i<10; ++i)
  2605. {
  2606. 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);
  2607. }
  2608. musicVolume->select(CGI->musich->getVolume(), 1);
  2609. musicVolume->onChange = boost::bind(&SystemOptions::setMusicVolume, &owner->sysOpts, _1);
  2610. effectsVolume = new CHighlightableButtonsGroup(0, true);
  2611. for(int i=0; i<10; ++i)
  2612. {
  2613. 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);
  2614. }
  2615. effectsVolume->select(CGI->soundh->getVolume(), 1);
  2616. effectsVolume->onChange = boost::bind(&SystemOptions::setSoundVolume, &owner->sysOpts, _1);
  2617. }
  2618. CSystemOptionsWindow::~CSystemOptionsWindow()
  2619. {
  2620. SDL_FreeSurface(background);
  2621. delete save;
  2622. delete quitGame;
  2623. delete backToMap;
  2624. delete heroMoveSpeed;
  2625. delete mapScrollSpeed;
  2626. delete musicVolume;
  2627. delete effectsVolume;
  2628. }
  2629. void CSystemOptionsWindow::bquitf()
  2630. {
  2631. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(exit, 0), boost::bind(&CSystemOptionsWindow::activate, this), false);
  2632. }
  2633. void CSystemOptionsWindow::breturnf()
  2634. {
  2635. GH.popIntTotally(this);
  2636. }
  2637. void CSystemOptionsWindow::bsavef()
  2638. {
  2639. GH.popIntTotally(this);
  2640. GH.pushInt(new CSelectionScreen(saveGame));
  2641. /*using namespace boost::posix_time;
  2642. std::ostringstream fnameStream;
  2643. fnameStream << second_clock::local_time();
  2644. std::string fname = fnameStream.str();
  2645. boost::algorithm::replace_all(fname,":","");
  2646. boost::algorithm::replace_all(fname," ","-");
  2647. 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);*/
  2648. }
  2649. void CSystemOptionsWindow::activate()
  2650. {
  2651. save->activate();
  2652. quitGame->activate();
  2653. backToMap->activate();
  2654. heroMoveSpeed->activate();
  2655. mapScrollSpeed->activate();
  2656. musicVolume->activate();
  2657. effectsVolume->activate();
  2658. }
  2659. void CSystemOptionsWindow::deactivate()
  2660. {
  2661. save->deactivate();
  2662. quitGame->deactivate();
  2663. backToMap->deactivate();
  2664. heroMoveSpeed->deactivate();
  2665. mapScrollSpeed->deactivate();
  2666. musicVolume->deactivate();
  2667. effectsVolume->deactivate();
  2668. }
  2669. void CSystemOptionsWindow::show(SDL_Surface *to)
  2670. {
  2671. SDL_BlitSurface(background, NULL, to, &pos);
  2672. save->show(to);
  2673. quitGame->show(to);
  2674. backToMap->show(to);
  2675. heroMoveSpeed->show(to);
  2676. mapScrollSpeed->show(to);
  2677. musicVolume->show(to);
  2678. effectsVolume->show(to);
  2679. }
  2680. CTavernWindow::CTavernWindow(const CGHeroInstance *H1, const CGHeroInstance *H2, const std::string &gossip)
  2681. :h1(selected,0,72,299,H1),h2(selected,1,162,299,H2)
  2682. {
  2683. if(H1)
  2684. selected = 0;
  2685. else
  2686. selected = -1;
  2687. oldSelected = -1;
  2688. bg = BitmapHandler::loadBitmap("TPTAVERN.bmp");
  2689. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2690. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2691. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,GEOR16,tytulowy,bg);
  2692. printAtMiddle("2500",320,328,GEOR13,zwykly,bg);
  2693. printAtMiddle(CGI->generaltexth->jktexts[38],146,283,GEOR16,tytulowy,bg);
  2694. printAtMiddleWB(gossip,200,220,GEOR13,50,zwykly,bg);
  2695. pos.w = bg->w;
  2696. pos.h = bg->h;
  2697. pos.x = (screen->w-bg->w)/2;
  2698. pos.y = (screen->h-bg->h)/2;
  2699. bar = new CStatusBar(pos.x+8, pos.y+478, "APHLFTRT.bmp", 380);
  2700. h1.pos.x += pos.x;
  2701. h2.pos.x += pos.x;
  2702. h1.pos.y += pos.y;
  2703. h2.pos.y += pos.y;
  2704. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"",boost::bind(&CTavernWindow::close,this),pos.x+310,pos.y+428,"ICANCEL.DEF",SDLK_ESCAPE);
  2705. recruit = new AdventureMapButton("","",boost::bind(&CTavernWindow::recruitb,this),pos.x+272,pos.y+355,"TPTAV01.DEF",SDLK_RETURN);
  2706. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"",0,pos.x+22,pos.y+428,"TPTAV02.DEF",SDLK_t);
  2707. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  2708. {
  2709. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  2710. recruit->block(2);
  2711. }
  2712. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  2713. {
  2714. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  2715. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  2716. recruit->block(2);
  2717. }
  2718. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  2719. {
  2720. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  2721. recruit->block(2);
  2722. }
  2723. else
  2724. {
  2725. if(!H1)
  2726. recruit->block(1);
  2727. }
  2728. #ifdef _WIN32
  2729. CGI->videoh->open("TAVERN.BIK");
  2730. #else
  2731. CGI->videoh->open("tavern.mjpg", true, false);
  2732. #endif
  2733. }
  2734. void CTavernWindow::recruitb()
  2735. {
  2736. const CGHeroInstance *toBuy = (selected ? h2 : h1).h;
  2737. close();
  2738. LOCPLINT->cb->recruitHero(LOCPLINT->castleInt->town,toBuy);
  2739. }
  2740. CTavernWindow::~CTavernWindow()
  2741. {
  2742. CGI->videoh->close();
  2743. SDL_FreeSurface(bg);
  2744. delete cancel;
  2745. delete thiefGuild;
  2746. delete recruit;
  2747. delete bar;
  2748. }
  2749. void CTavernWindow::activate()
  2750. {
  2751. thiefGuild->activate();
  2752. cancel->activate();
  2753. if(h1.h)
  2754. h1.activate();
  2755. if(h2.h)
  2756. h2.activate();
  2757. recruit->activate();
  2758. LOCPLINT->statusbar = bar;
  2759. }
  2760. void CTavernWindow::deactivate()
  2761. {
  2762. thiefGuild->deactivate();
  2763. cancel->deactivate();
  2764. if(h1.h)
  2765. h1.deactivate();
  2766. if(h2.h)
  2767. h2.deactivate();
  2768. recruit->deactivate();
  2769. }
  2770. void CTavernWindow::close()
  2771. {
  2772. GH.popIntTotally(this);
  2773. }
  2774. void CTavernWindow::show(SDL_Surface * to)
  2775. {
  2776. blitAt(bg,pos.x,pos.y,to);
  2777. CGI->videoh->update(pos.x+70, pos.y+56, to, true, false);
  2778. if(h1.h)
  2779. h1.show(to);
  2780. if(h2.h)
  2781. h2.show(to);
  2782. thiefGuild->show(to);
  2783. cancel->show(to);
  2784. recruit->show(to);
  2785. bar->show(to);
  2786. if(selected >= 0)
  2787. {
  2788. HeroPortrait *sel = selected ? &h2 : &h1;
  2789. if (selected != oldSelected && !recruit->blocked)
  2790. {
  2791. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  2792. oldSelected = selected;
  2793. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  2794. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  2795. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  2796. }
  2797. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,GEOR13,40,zwykly,to);
  2798. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  2799. }
  2800. }
  2801. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  2802. {
  2803. if(previousState && !down)
  2804. as();
  2805. //ClickableL::clickLeft(down);
  2806. }
  2807. void CTavernWindow::HeroPortrait::activate()
  2808. {
  2809. activateLClick();
  2810. activateRClick();
  2811. activateHover();
  2812. }
  2813. void CTavernWindow::HeroPortrait::deactivate()
  2814. {
  2815. deactivateLClick();
  2816. deactivateRClick();
  2817. deactivateHover();
  2818. }
  2819. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  2820. {
  2821. if(down)
  2822. {
  2823. LOCPLINT->adventureInt->heroWindow->setHero(h);
  2824. GH.pushInt(new CRClickPopupInt(LOCPLINT->adventureInt->heroWindow,false));
  2825. }
  2826. }
  2827. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  2828. :as(sel,id)
  2829. {
  2830. h = H;
  2831. pos.x = x;
  2832. pos.y = y;
  2833. pos.w = 58;
  2834. pos.h = 64;
  2835. if(H)
  2836. {
  2837. hoverName = CGI->generaltexth->tavernInfo[4];
  2838. boost::algorithm::replace_first(hoverName,"%s",H->name);
  2839. int artifs = h->artifWorn.size() + h->artifacts.size();
  2840. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  2841. if(vstd::contains(h->artifWorn,i))
  2842. artifs--;
  2843. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  2844. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  2845. descr[sizeof(descr)-1] = '\0';
  2846. }
  2847. }
  2848. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  2849. {
  2850. blitAt(graphics->portraitLarge[h->subID],pos,to);
  2851. }
  2852. void CTavernWindow::HeroPortrait::hover( bool on )
  2853. {
  2854. //Hoverable::hover(on);
  2855. if(on)
  2856. LOCPLINT->statusbar->print(hoverName);
  2857. else
  2858. LOCPLINT->statusbar->clear();
  2859. }
  2860. void CInGameConsole::activate()
  2861. {
  2862. activateKeys();
  2863. }
  2864. void CInGameConsole::deactivate()
  2865. {
  2866. deactivateKeys();
  2867. }
  2868. void CInGameConsole::show(SDL_Surface * to)
  2869. {
  2870. int number = 0;
  2871. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  2872. texts_mx.lock();
  2873. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  2874. {
  2875. SDL_Color green = {0,0xff,0,0};
  2876. Point leftBottomCorner(0, screen->h);
  2877. if(LOCPLINT->battleInt)
  2878. {
  2879. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  2880. }
  2881. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, TNRB16, green);
  2882. if(SDL_GetTicks() - it->second > defaultTimeout)
  2883. {
  2884. toDel.push_back(it);
  2885. }
  2886. }
  2887. for(int it=0; it<toDel.size(); ++it)
  2888. {
  2889. texts.erase(toDel[it]);
  2890. }
  2891. texts_mx.unlock();
  2892. }
  2893. void CInGameConsole::print(const std::string &txt)
  2894. {
  2895. texts_mx.lock();
  2896. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  2897. if(texts.size() > maxDisplayedTexts)
  2898. {
  2899. texts.pop_front();
  2900. }
  2901. texts_mx.unlock();
  2902. }
  2903. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  2904. {
  2905. if(key.type != SDL_KEYDOWN) return;
  2906. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  2907. switch(key.keysym.sym)
  2908. {
  2909. case SDLK_TAB:
  2910. case SDLK_ESCAPE:
  2911. {
  2912. if(captureAllKeys)
  2913. {
  2914. captureAllKeys = false;
  2915. endEnteringText(false);
  2916. }
  2917. else if(SDLK_TAB)
  2918. {
  2919. captureAllKeys = true;
  2920. startEnteringText();
  2921. }
  2922. break;
  2923. }
  2924. case SDLK_RETURN: //enter key
  2925. {
  2926. if(enteredText.size() > 0 && captureAllKeys)
  2927. {
  2928. captureAllKeys = false;
  2929. endEnteringText(true);
  2930. }
  2931. break;
  2932. }
  2933. case SDLK_BACKSPACE:
  2934. {
  2935. if(enteredText.size() > 1)
  2936. {
  2937. enteredText.resize(enteredText.size()-1);
  2938. enteredText[enteredText.size()-1] = '_';
  2939. refreshEnteredText();
  2940. }
  2941. break;
  2942. }
  2943. case SDLK_UP: //up arrow
  2944. {
  2945. if(previouslyEntered.size() == 0)
  2946. break;
  2947. if(prevEntDisp == -1)
  2948. {
  2949. prevEntDisp = previouslyEntered.size() - 1;
  2950. enteredText = previouslyEntered[prevEntDisp] + "_";
  2951. refreshEnteredText();
  2952. }
  2953. else if( prevEntDisp > 0)
  2954. {
  2955. --prevEntDisp;
  2956. enteredText = previouslyEntered[prevEntDisp] + "_";
  2957. refreshEnteredText();
  2958. }
  2959. break;
  2960. }
  2961. case SDLK_DOWN: //down arrow
  2962. {
  2963. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  2964. {
  2965. ++prevEntDisp;
  2966. enteredText = previouslyEntered[prevEntDisp] + "_";
  2967. refreshEnteredText();
  2968. }
  2969. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  2970. {
  2971. prevEntDisp = -1;
  2972. enteredText = "_";
  2973. refreshEnteredText();
  2974. }
  2975. break;
  2976. }
  2977. default:
  2978. {
  2979. if(enteredText.size() > 0)
  2980. {
  2981. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  2982. {
  2983. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  2984. enteredText += "_";
  2985. refreshEnteredText();
  2986. }
  2987. }
  2988. break;
  2989. }
  2990. }
  2991. }
  2992. void CInGameConsole::startEnteringText()
  2993. {
  2994. enteredText = "_";
  2995. if(GH.topInt() == LOCPLINT->adventureInt)
  2996. {
  2997. LOCPLINT->statusbar->print(enteredText);
  2998. }
  2999. else if(LOCPLINT->battleInt)
  3000. {
  3001. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3002. }
  3003. }
  3004. void CInGameConsole::endEnteringText(bool printEnteredText)
  3005. {
  3006. prevEntDisp = -1;
  3007. if(printEnteredText)
  3008. {
  3009. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3010. LOCPLINT->cb->sendMessage(txt);
  3011. previouslyEntered.push_back(txt);
  3012. print(txt);
  3013. }
  3014. enteredText = "";
  3015. if(GH.topInt() == LOCPLINT->adventureInt)
  3016. {
  3017. LOCPLINT->statusbar->clear();
  3018. }
  3019. else if(LOCPLINT->battleInt)
  3020. {
  3021. LOCPLINT->battleInt->console->ingcAlter = "";
  3022. }
  3023. }
  3024. void CInGameConsole::refreshEnteredText()
  3025. {
  3026. if(GH.topInt() == LOCPLINT->adventureInt)
  3027. {
  3028. LOCPLINT->statusbar->print(enteredText);
  3029. }
  3030. else if(LOCPLINT->battleInt)
  3031. {
  3032. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3033. }
  3034. }
  3035. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3036. {
  3037. }
  3038. void CGarrisonWindow::close()
  3039. {
  3040. GH.popIntTotally(this);
  3041. }
  3042. void CGarrisonWindow::activate()
  3043. {
  3044. quit->activate();
  3045. garr->activate();
  3046. }
  3047. void CGarrisonWindow::deactivate()
  3048. {
  3049. quit->deactivate();
  3050. garr->deactivate();
  3051. }
  3052. void CGarrisonWindow::show(SDL_Surface * to)
  3053. {
  3054. blitAt(bg,pos,to);
  3055. quit->show(to);
  3056. garr->show(to);
  3057. blitAt(graphics->flags->ourImages[garr->odown->getOwner()].bitmap,pos.x+28,pos.y+124,to);
  3058. blitAt(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->odown)->portrait],pos.x+29,pos.y+222,to);
  3059. printAtMiddle(CGI->generaltexth->allTexts[709],pos.x+275,pos.y+30,GEOR16,tytulowy,to);
  3060. }
  3061. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits )
  3062. {
  3063. bg = BitmapHandler::loadBitmap("GARRISON.bmp");
  3064. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  3065. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  3066. pos.x = screen->w/2 - bg->w/2;
  3067. pos.y = screen->h/2 - bg->h/2;
  3068. pos.w = screen->w;
  3069. pos.h = screen->h;
  3070. garr = new CGarrisonInt(pos.x+92, pos.y+127, 4, Point(0,96), bg, Point(93,127), up, down, removableUnits);
  3071. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+88,pos.y+314,"IDV6432.DEF"));
  3072. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),pos.x+399,pos.y+314,"IOK6432.DEF",SDLK_RETURN);
  3073. }
  3074. CGarrisonWindow::~CGarrisonWindow()
  3075. {
  3076. SDL_FreeSurface(bg);
  3077. delete quit;
  3078. delete garr;
  3079. }
  3080. IShowActivable::IShowActivable()
  3081. {
  3082. type = 0;
  3083. }
  3084. CWindowWithGarrison::CWindowWithGarrison()
  3085. {
  3086. type |= WITH_GARRISON;
  3087. }
  3088. void CRClickPopupInt::show(SDL_Surface * to)
  3089. {
  3090. inner->show(to);
  3091. }
  3092. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  3093. {
  3094. inner = our;
  3095. delInner = deleteInt;
  3096. }
  3097. CRClickPopupInt::~CRClickPopupInt()
  3098. {
  3099. //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  3100. //TODO? it might be better to rewrite hero window so it will bee newed/deleted on opening / closing (not effort-worthy now, but on some day...?)
  3101. if(LOCPLINT && inner == LOCPLINT->adventureInt->heroWindow)
  3102. LOCPLINT->adventureInt->heroWindow->dispose();
  3103. if(delInner)
  3104. delete inner;
  3105. }
  3106. CArtPlace::CArtPlace(const CArtifact* Art): active(false), clicked(false), ourArt(Art)/*,
  3107. spellBook(false), warMachine1(false), warMachine2(false), warMachine3(false),
  3108. warMachine4(false),misc1(false), misc2(false), misc3(false), misc4(false),
  3109. misc5(false), feet(false), lRing(false), rRing(false), torso(false),
  3110. lHand(false), rHand(false), neck(false), shoulders(false), head(false) */{}
  3111. void CArtPlace::activate()
  3112. {
  3113. if(!active)
  3114. {
  3115. //activateLClick();
  3116. LRClickableAreaWTextComp::activate();
  3117. active = true;
  3118. }
  3119. }
  3120. void CArtPlace::clickLeft(tribool down, bool previousState)
  3121. {
  3122. //LRClickableAreaWTextComp::clickLeft(down);
  3123. if(ourArt && !down && previousState) //we are spellbook
  3124. {
  3125. if(ourArt->id == 0)
  3126. {
  3127. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), ourOwner->curHero);
  3128. GH.pushInt(spellWindow);
  3129. }
  3130. }
  3131. if(!down && !clicked && previousState) //not clicked before
  3132. {
  3133. if(ourArt && ourArt->id == 0)
  3134. return; //this is handled separately
  3135. if(!ourOwner->commonInfo->activeArtPlace) //nothing has been clicked
  3136. {
  3137. if(ourArt) //to prevent selecting empty slots (bugfix to what GrayFace reported)
  3138. {
  3139. if(ourArt->id == 3) //catapult cannot be highlighted
  3140. {
  3141. std::vector<SComponent *> catapult(1, new SComponent(SComponent::artifact, 3, 0));
  3142. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  3143. return;
  3144. }
  3145. clicked = true;
  3146. ourOwner->commonInfo->activeArtPlace = this;
  3147. }
  3148. }
  3149. else //perform artifact substitution
  3150. {
  3151. if(slotID >= 19) //we are an backpack slot - remove active artifact and put it to the last free pos in backpack
  3152. { //TODO: putting artifacts in the middle of backpack (pushing following arts)
  3153. const CArtifact *cur = ourOwner->commonInfo->activeArtPlace->ourArt;
  3154. assert(cur); //there is highlighted slot, it must contain an art
  3155. switch(cur->id)
  3156. {
  3157. case 3:
  3158. //should not happen, catapult cannot be selected
  3159. break;
  3160. case 4: case 5: case 6:
  3161. {
  3162. std::string text = CGI->generaltexth->allTexts[153];
  3163. boost::algorithm::replace_first(text, "%s", cur->Name());
  3164. LOCPLINT->showInfoDialog(text);
  3165. }
  3166. break;
  3167. default:
  3168. LOCPLINT->cb->swapArtifacts(ourOwner->commonInfo->activeArtPlace->ourOwner->curHero, ourOwner->commonInfo->activeArtPlace->slotID, ourOwner->curHero, ourOwner->curHero->artifacts.size()+19);
  3169. break;
  3170. }
  3171. }
  3172. //check if swap is possible
  3173. else if(this->fitsHere(ourOwner->commonInfo->activeArtPlace->ourArt) && ourOwner->commonInfo->activeArtPlace->fitsHere(this->ourArt))
  3174. {
  3175. int destSlot = slotID,
  3176. srcSlot = ourOwner->commonInfo->activeArtPlace->slotID;
  3177. LOCPLINT->cb->swapArtifacts(ourOwner->curHero,destSlot,ourOwner->commonInfo->activeArtPlace->ourOwner->curHero,srcSlot);
  3178. ourOwner->commonInfo->activeArtPlace->clicked = false;
  3179. ourOwner->commonInfo->activeArtPlace = NULL;
  3180. }
  3181. }
  3182. }
  3183. else if(!down && clicked)
  3184. {
  3185. if(ourArt && ourArt->id == 0)
  3186. return; //this is handled separately
  3187. clicked = false;
  3188. ourOwner->commonInfo->activeArtPlace = NULL;
  3189. }
  3190. //ClickableL::clickLeft(down);
  3191. }
  3192. void CArtPlace::clickRight(tribool down, bool previousState)
  3193. {
  3194. if(text.size()) //if there is no description, do nothing ;]
  3195. LRClickableAreaWTextComp::clickRight(down, previousState);
  3196. }
  3197. void CArtPlace::deactivate()
  3198. {
  3199. if(active)
  3200. {
  3201. active = false;
  3202. //deactivateLClick();
  3203. LRClickableAreaWTextComp::deactivate();
  3204. }
  3205. }
  3206. void CArtPlace::show(SDL_Surface *to)
  3207. {
  3208. if(ourArt)
  3209. {
  3210. blitAt(graphics->artDefs->ourImages[ourArt->id].bitmap, pos.x, pos.y, to);
  3211. }
  3212. if(clicked && active)
  3213. {
  3214. for(int i=0; i<pos.h; ++i)
  3215. {
  3216. for(int j=0; j<pos.w; ++j)
  3217. {
  3218. if(i==0 || j==0 || i==pos.h-1 || j==pos.w-1)
  3219. {
  3220. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x+j, pos.y+i, 240, 220, 120);
  3221. }
  3222. }
  3223. }
  3224. }
  3225. }
  3226. bool CArtPlace::fitsHere(const CArtifact * art)
  3227. {
  3228. if(!art)
  3229. return true; //you can have no artifact somewhere
  3230. if(slotID > 18 && art->id >= 3 && art->id <= 6 //everything can bee placed in backpack exept of War Machines
  3231. || vstd::contains(art->possibleSlots,slotID))
  3232. {
  3233. return true;
  3234. }
  3235. return false;
  3236. }
  3237. CArtPlace::~CArtPlace()
  3238. {
  3239. deactivate();
  3240. }
  3241. void LClickableArea::activate()
  3242. {
  3243. activateLClick();
  3244. }
  3245. void LClickableArea::deactivate()
  3246. {
  3247. deactivateLClick();
  3248. }
  3249. void LClickableArea::clickLeft(tribool down, bool previousState)
  3250. {
  3251. //if(!down)
  3252. //{
  3253. // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>());
  3254. //}
  3255. }
  3256. void RClickableArea::activate()
  3257. {
  3258. activateRClick();
  3259. }
  3260. void RClickableArea::deactivate()
  3261. {
  3262. deactivateRClick();
  3263. }
  3264. void RClickableArea::clickRight(tribool down, bool previousState)
  3265. {
  3266. //if(!down)
  3267. //{
  3268. // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>());
  3269. //}
  3270. }
  3271. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  3272. {
  3273. if(!down && previousState)
  3274. {
  3275. LOCPLINT->showInfoDialog(text, std::vector<SComponent*>(), soundBase::sound_todo);
  3276. }
  3277. //ClickableL::clickLeft(down);
  3278. }
  3279. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  3280. {
  3281. LOCPLINT->adventureInt->handleRightClick(text, down, this);
  3282. }
  3283. void LRClickableAreaWText::activate()
  3284. {
  3285. LClickableArea::activate();
  3286. RClickableArea::activate();
  3287. activateHover();
  3288. }
  3289. void LRClickableAreaWText::deactivate()
  3290. {
  3291. LClickableArea::deactivate();
  3292. RClickableArea::deactivate();
  3293. deactivateHover();
  3294. }
  3295. void LRClickableAreaWText::hover(bool on)
  3296. {
  3297. //Hoverable::hover(on);
  3298. if (on)
  3299. LOCPLINT->statusbar->print(hoverText);
  3300. else if (LOCPLINT->statusbar->getCurrent()==hoverText)
  3301. LOCPLINT->statusbar->clear();
  3302. }
  3303. void LClickableAreaHero::clickLeft(tribool down, bool previousState)
  3304. {
  3305. if(!down)
  3306. {
  3307. owner->deactivate();
  3308. const CGHeroInstance * buf = LOCPLINT->getWHero(id);
  3309. owner->setHero(buf);
  3310. owner->redrawCurBack();
  3311. owner->activate();
  3312. }
  3313. }
  3314. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  3315. {
  3316. if((!down) && previousState)
  3317. {
  3318. std::vector<SComponent*> comp(1, new SComponent(SComponent::Etype(baseType), type, bonus));
  3319. LOCPLINT->showInfoDialog(text, comp, soundBase::sound_todo);
  3320. }
  3321. //ClickableL::clickLeft(down);
  3322. }
  3323. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  3324. {
  3325. LOCPLINT->adventureInt->handleRightClick(text, down, this);
  3326. }
  3327. void LRClickableAreaWTextComp::activate()
  3328. {
  3329. LClickableArea::activate();
  3330. RClickableArea::activate();
  3331. activateHover();
  3332. }
  3333. void LRClickableAreaWTextComp::deactivate()
  3334. {
  3335. LClickableArea::deactivate();
  3336. RClickableArea::deactivate();
  3337. deactivateHover();
  3338. }
  3339. void LRClickableAreaWTextComp::hover(bool on)
  3340. {
  3341. //Hoverable::hover(on);
  3342. if (on)
  3343. LOCPLINT->statusbar->print(hoverText);
  3344. else if (LOCPLINT->statusbar->getCurrent()==hoverText)
  3345. LOCPLINT->statusbar->clear();
  3346. }
  3347. void CArtifactsOfHero::activate()
  3348. {
  3349. for(size_t f=0; f<artWorn.size(); ++f)
  3350. {
  3351. if(artWorn[f])
  3352. artWorn[f]->activate();
  3353. }
  3354. for(size_t f=0; f<backpack.size(); ++f)
  3355. {
  3356. if(backpack[f])
  3357. backpack[f]->activate();
  3358. }
  3359. leftArtRoll->activate();
  3360. rightArtRoll->activate();
  3361. }
  3362. void CArtifactsOfHero::deactivate()
  3363. {
  3364. for(size_t f=0; f<artWorn.size(); ++f)
  3365. {
  3366. if(artWorn[f])
  3367. artWorn[f]->deactivate();
  3368. }
  3369. for(size_t f=0; f<backpack.size(); ++f)
  3370. {
  3371. if(backpack[f])
  3372. backpack[f]->deactivate();
  3373. }
  3374. leftArtRoll->deactivate();
  3375. rightArtRoll->deactivate();
  3376. }
  3377. void CArtifactsOfHero::show(SDL_Surface * to)
  3378. {
  3379. for(size_t d=0; d<artWorn.size(); ++d)
  3380. {
  3381. artWorn[d]->show(to);
  3382. }
  3383. for(size_t d=0; d<backpack.size(); ++d)
  3384. {
  3385. backpack[d]->show(to);
  3386. }
  3387. leftArtRoll->show(to);
  3388. rightArtRoll->show(to);
  3389. }
  3390. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  3391. {
  3392. curHero = hero;
  3393. char bufor[400];
  3394. for(size_t g=0; g<artWorn.size(); ++g)
  3395. {
  3396. delete artWorn[g];
  3397. }
  3398. for(size_t g=0; g<backpack.size(); ++g)
  3399. {
  3400. delete backpack[g];
  3401. }
  3402. backpack.clear();
  3403. std::vector<SDL_Rect> slotPos;
  3404. backpackPos = 0;
  3405. slotPos += genRect(44,44,pos.x+509,pos.y+30), genRect(44,44,pos.x+567,pos.y+240), genRect(44,44,pos.x+509,pos.y+80),
  3406. genRect(44,44,pos.x+383,pos.y+68), genRect(44,44,pos.x+564,pos.y+183), genRect(44,44,pos.x+509,pos.y+130),
  3407. genRect(44,44,pos.x+431,pos.y+68), genRect(44,44,pos.x+610,pos.y+183), genRect(44,44,pos.x+515,pos.y+295),
  3408. genRect(44,44,pos.x+383,pos.y+143), genRect(44,44,pos.x+399,pos.y+194), genRect(44,44,pos.x+415,pos.y+245),
  3409. genRect(44,44,pos.x+431,pos.y+296), genRect(44,44,pos.x+564,pos.y+30), genRect(44,44,pos.x+610,pos.y+30),
  3410. genRect(44,44,pos.x+610,pos.y+76), genRect(44,44,pos.x+610,pos.y+122), genRect(44,44,pos.x+610,pos.y+310),
  3411. genRect(44,44,pos.x+381,pos.y+296);
  3412. for (int g = 0; g < 19 ; g++)
  3413. {
  3414. artWorn[g] = new CArtPlace(hero->getArt(g));
  3415. artWorn[g]->pos = slotPos[g];
  3416. if(hero->getArt(g))
  3417. artWorn[g]->text = hero->getArt(g)->Description();
  3418. artWorn[g]->ourOwner = this;
  3419. }
  3420. for(size_t g=0; g<artWorn.size(); ++g)
  3421. {
  3422. artWorn[g]->slotID = g;
  3423. if(artWorn[g]->ourArt)
  3424. {
  3425. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), artWorn[g]->ourArt->Name().c_str());
  3426. artWorn[g]->hoverText = std::string(bufor);
  3427. }
  3428. else
  3429. {
  3430. artWorn[g]->hoverText = CGI->generaltexth->allTexts[507];
  3431. }
  3432. }
  3433. for(size_t s=0; s<5; ++s)
  3434. {
  3435. CArtPlace * add;
  3436. if( s < curHero->artifacts.size() )
  3437. {
  3438. add = new CArtPlace(&CGI->arth->artifacts[curHero->artifacts[(s+backpackPos) % curHero->artifacts.size() ]]);
  3439. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), add->ourArt->Name().c_str());
  3440. add->hoverText = bufor;
  3441. }
  3442. else
  3443. {
  3444. add = new CArtPlace(NULL);
  3445. add->hoverText = CGI->generaltexth->allTexts[507];
  3446. }
  3447. add->pos.x = pos.x + 403 + 46*s;
  3448. add->pos.y = pos.y + 365;
  3449. add->pos.h = add->pos.w = 44;
  3450. if(s<hero->artifacts.size() && hero->artifacts[s])
  3451. {
  3452. add->text = hero->getArt(19+s)->Description();
  3453. }
  3454. else
  3455. {
  3456. add->text = std::string();
  3457. }
  3458. add->ourOwner = this;
  3459. add->slotID = 19+s;
  3460. backpack.push_back(add);
  3461. }
  3462. commonInfo->activeArtPlace = NULL;
  3463. //blocking scrolling if there is not enough artifacts to scroll
  3464. leftArtRoll->block(hero->artifacts.size()<6);
  3465. rightArtRoll->block(hero->artifacts.size()<6);
  3466. }
  3467. void CArtifactsOfHero::dispose()
  3468. {
  3469. curHero = NULL;
  3470. for(size_t g=0; g<artWorn.size(); ++g)
  3471. {
  3472. delete artWorn[g];
  3473. artWorn[g] = NULL;
  3474. }
  3475. for(size_t g=0; g<backpack.size(); ++g)
  3476. {
  3477. delete backpack[g];
  3478. backpack[g] = NULL;
  3479. }
  3480. backpack.clear();
  3481. if(commonInfo)
  3482. commonInfo->activeArtPlace = NULL;
  3483. }
  3484. void CArtifactsOfHero::scrollBackpack(int dir)
  3485. {
  3486. backpackPos += dir + curHero->artifacts.size();
  3487. backpackPos %= curHero->artifacts.size();
  3488. for(size_t s=0; s<5 && s<curHero->artifacts.size(); ++s) //set new data
  3489. {
  3490. CArtPlace *cur = backpack[s];
  3491. cur->slotID = 19+((s+backpackPos)%curHero->artifacts.size());
  3492. cur->ourArt = curHero->getArt(cur->slotID);
  3493. if(cur->ourArt)
  3494. cur->text = cur->ourArt->Description();
  3495. else
  3496. cur->text = std::string();
  3497. }
  3498. }
  3499. CArtifactsOfHero::CArtifactsOfHero(const SDL_Rect & position) :
  3500. backpackPos(0)
  3501. {
  3502. pos = position;
  3503. artWorn.resize(19);
  3504. leftArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), pos.x+379, pos.y+364, "hsbtns3.def", SDLK_LEFT);
  3505. rightArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), pos.x+632, pos.y+364, "hsbtns5.def", SDLK_RIGHT);
  3506. }
  3507. CArtifactsOfHero::~CArtifactsOfHero()
  3508. {
  3509. dispose();
  3510. artWorn.clear();
  3511. delete leftArtRoll;
  3512. delete rightArtRoll;
  3513. }
  3514. void CExchangeWindow::close()
  3515. {
  3516. GH.popIntTotally(this);
  3517. }
  3518. void CExchangeWindow::activate()
  3519. {
  3520. quit->activate();
  3521. garr->activate();
  3522. artifs[0]->activate();
  3523. artifs[1]->activate();
  3524. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  3525. {
  3526. for(int b=0; b<secSkillAreas[g].size(); ++b)
  3527. {
  3528. secSkillAreas[g][b]->activate();
  3529. }
  3530. }
  3531. for(int b=0; b<primSkillAreas.size(); ++b)
  3532. {
  3533. primSkillAreas[b]->activate();
  3534. }
  3535. LOCPLINT->statusbar = ourBar;
  3536. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  3537. {
  3538. questlogButton[g]->activate();
  3539. }
  3540. for(int g=0; g<ARRAY_COUNT(morale); g++)
  3541. {
  3542. morale[g]->activate();
  3543. }
  3544. for(int g=0; g<ARRAY_COUNT(luck); g++)
  3545. {
  3546. luck[g]->activate();
  3547. }
  3548. }
  3549. void CExchangeWindow::deactivate()
  3550. {
  3551. quit->deactivate();
  3552. garr->deactivate();
  3553. artifs[0]->deactivate();
  3554. artifs[1]->deactivate();
  3555. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  3556. {
  3557. for(int b=0; b<secSkillAreas[g].size(); ++b)
  3558. {
  3559. secSkillAreas[g][b]->deactivate();
  3560. }
  3561. }
  3562. for(int b=0; b<primSkillAreas.size(); ++b)
  3563. {
  3564. primSkillAreas[b]->deactivate();
  3565. }
  3566. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  3567. {
  3568. questlogButton[g]->deactivate();
  3569. }
  3570. for(int g=0; g<ARRAY_COUNT(morale); g++)
  3571. {
  3572. morale[g]->deactivate();
  3573. }
  3574. for(int g=0; g<ARRAY_COUNT(luck); g++)
  3575. {
  3576. luck[g]->deactivate();
  3577. }
  3578. }
  3579. void CExchangeWindow::show(SDL_Surface * to)
  3580. {
  3581. blitAt(bg, pos, to);
  3582. quit->show(to);
  3583. //printing border around window
  3584. if(screen->w != 800 || screen->h !=600)
  3585. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  3586. artifs[0]->show(to);
  3587. artifs[1]->show(to);
  3588. ourBar->show(to);
  3589. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  3590. {
  3591. questlogButton[g]->show(to);
  3592. }
  3593. garr->show(to);
  3594. }
  3595. void CExchangeWindow::questlog(int whichHero)
  3596. {
  3597. }
  3598. void CExchangeWindow::prepareBackground()
  3599. {
  3600. if(bg)
  3601. SDL_FreeSurface(bg);
  3602. SDL_Surface * bgtemp; //loaded as 8bpp surface
  3603. bgtemp = BitmapHandler::loadBitmap("TRADE2.BMP");
  3604. graphics->blueToPlayersAdv(bgtemp, heroInst[0]->tempOwner);
  3605. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  3606. SDL_FreeSurface(bgtemp);
  3607. //printing heroes' names and levels
  3608. std::ostringstream os, os2;
  3609. os<<heroInst[0]->name<<", Level "<<heroInst[0]->level<<" "<<heroInst[0]->type->heroClass->name;
  3610. CSDL_Ext::printAtMiddle(os.str(), 147, 23, GEOR13, zwykly, bg);
  3611. os2<<heroInst[1]->name<<", Level "<<heroInst[1]->level<<" "<<heroInst[1]->type->heroClass->name;
  3612. CSDL_Ext::printAtMiddle(os2.str(), 653, 23, GEOR13, zwykly, bg);
  3613. //printing primary skills
  3614. CDefHandler * skilldef = CDefHandler::giveDef("PSKIL32.DEF");
  3615. for(int g=0; g<4; ++g)
  3616. {
  3617. //graphics
  3618. blitAt(skilldef->ourImages[g].bitmap, genRect(32, 32, 385, 19 + 36 * g), bg);
  3619. }
  3620. CDefHandler * un32 = CDefHandler::giveDef("UN32.DEF");
  3621. //heroes related thing
  3622. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  3623. {
  3624. //printing primary skills' amounts
  3625. for(int m=0; m<4; ++m)
  3626. {
  3627. std::ostringstream primarySkill;
  3628. primarySkill<<heroInst[b]->getPrimSkillLevel(m);
  3629. CSDL_Ext::printAtMiddle(primarySkill.str(), 353 + 93 * b, 35 + 36 * m, TNRB16, zwykly, bg);
  3630. }
  3631. //printing secondary skills
  3632. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  3633. {
  3634. blitAt(graphics->abils32->ourImages[heroInst[b]->secSkills[m].first * 3 + heroInst[b]->secSkills[m].second + 2].bitmap, genRect(32, 32, 32 + 36 * m + 454 * b, 88), bg);
  3635. }
  3636. //hero's specialty
  3637. blitAt(un32->ourImages[heroInst[b]->subID].bitmap, 67 + 490*b, 45, bg);
  3638. //experience
  3639. blitAt(skilldef->ourImages[4].bitmap, 103 + 490*b, 45, bg);
  3640. printAtMiddle( makeNumberShort(heroInst[b]->exp), 119 + 490*b, 71, GEOR13, zwykly, bg );
  3641. //mana points
  3642. blitAt(skilldef->ourImages[5].bitmap, 139 + 490*b, 45, bg);
  3643. printAtMiddle( makeNumberShort(heroInst[b]->mana), 155 + 490*b, 71, GEOR13, zwykly, bg );
  3644. //setting morale
  3645. blitAt(graphics->morale30->ourImages[heroInst[b]->getCurrentMorale()+3].bitmap, 177 + 490*b, 45, bg);
  3646. //setting luck
  3647. blitAt(graphics->luck30->ourImages[heroInst[b]->getCurrentLuck()+3].bitmap, 213 + 490*b, 45, bg);
  3648. }
  3649. //printing portraits
  3650. blitAt(graphics->portraitLarge[heroInst[0]->portrait], 257, 13, bg);
  3651. blitAt(graphics->portraitLarge[heroInst[1]->portrait], 485, 13, bg);
  3652. delete un32;
  3653. delete skilldef;
  3654. }
  3655. CExchangeWindow::CExchangeWindow(si32 hero1, si32 hero2) : bg(NULL)
  3656. {
  3657. char bufor[400];
  3658. heroInst[0] = LOCPLINT->cb->getHeroInfo(hero1, 2);
  3659. heroInst[1] = LOCPLINT->cb->getHeroInfo(hero2, 2);
  3660. prepareBackground();
  3661. pos.x = screen->w/2 - bg->w/2;
  3662. pos.y = screen->h/2 - bg->h/2;
  3663. pos.w = screen->w;
  3664. pos.h = screen->h;
  3665. artifs[0] = new CArtifactsOfHero(genRect(600, 800, pos.x + -334, pos.y + 150));
  3666. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  3667. artifs[0]->setHero(heroInst[0]);
  3668. artifs[0]->commonInfo->activeArtPlace = NULL;
  3669. artifs[1] = new CArtifactsOfHero(genRect(600, 800, pos.x + 96, pos.y + 150));
  3670. artifs[1]->commonInfo = artifs[0]->commonInfo;
  3671. artifs[1]->setHero(heroInst[1]);
  3672. //primary skills
  3673. for(int g=0; g<4; ++g)
  3674. {
  3675. //primary skill's clickable areas
  3676. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  3677. primSkillAreas[g]->pos = genRect(32, 32, pos.x+385, pos.y + 19 + 36 * g);
  3678. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  3679. primSkillAreas[g]->type = g;
  3680. primSkillAreas[g]->bonus = -1;
  3681. primSkillAreas[g]->baseType = 0;
  3682. }
  3683. //heroes related thing
  3684. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  3685. {
  3686. //secondary skill's clickable areas
  3687. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  3688. {
  3689. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  3690. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  3691. secSkillAreas[b][g]->baseType = 1;
  3692. secSkillAreas[b][g]->type = heroInst[b]->secSkills[g].first;
  3693. secSkillAreas[b][g]->bonus = heroInst[b]->secSkills[g].second;
  3694. std::string hlp = CGI->generaltexth->skillInfoTexts[ heroInst[b]->secSkills[g].first ][heroInst[b]->secSkills[g].second-1];
  3695. secSkillAreas[b][g]->text = hlp.substr(1, hlp.size()-2);
  3696. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[heroInst[b]->secSkills[g].second-1].c_str(), CGI->generaltexth->skillName[heroInst[b]->secSkills[g].first].c_str());
  3697. secSkillAreas[b][g]->hoverText = std::string(bufor);
  3698. }
  3699. //setting morale
  3700. morale[b] = new LRClickableAreaWTextComp();
  3701. morale[b]->pos = genRect(32, 32, pos.x + 177 + 490*b, pos.y + 45);
  3702. std::vector<std::pair<int,std::string> > mrl = heroInst[b]->getCurrentMoraleModifiers();
  3703. int mrlv = heroInst[b]->getCurrentMorale();
  3704. int mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad morale[b], 0 - neutral, 1 - good
  3705. morale[b]->hoverText = CGI->generaltexth->heroscrn[4 - mrlt];
  3706. morale[b]->baseType = SComponent::morale;
  3707. morale[b]->bonus = mrlv;
  3708. morale[b]->text = CGI->generaltexth->arraytxt[88];
  3709. boost::algorithm::replace_first(morale[b]->text,"%s",CGI->generaltexth->arraytxt[86-mrlt]);
  3710. for(int it=0; it < mrl.size(); it++)
  3711. morale[b]->text += mrl[it].second;
  3712. //setting luck
  3713. luck[b] = new LRClickableAreaWTextComp();
  3714. luck[b]->pos = genRect(32, 32, pos.x + 213 + 490*b, pos.y + 45);
  3715. mrl = heroInst[b]->getCurrentLuckModifiers();
  3716. mrlv = heroInst[b]->getCurrentLuck();
  3717. mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad luck[b], 0 - neutral, 1 - good
  3718. luck[b]->hoverText = CGI->generaltexth->heroscrn[7 - mrlt];
  3719. luck[b]->baseType = SComponent::luck;
  3720. luck[b]->bonus = mrlv;
  3721. luck[b]->text = CGI->generaltexth->arraytxt[62];
  3722. boost::algorithm::replace_first(luck[b]->text,"%s",CGI->generaltexth->arraytxt[60-mrlt]);
  3723. for(int it=0; it < mrl.size(); it++)
  3724. luck[b]->text += mrl[it].second;
  3725. }
  3726. //buttons
  3727. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), pos.x+732, pos.y+567, "IOKAY.DEF", SDLK_RETURN);
  3728. questlogButton[0] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 0), pos.x+10, pos.y+44, "hsbtns4.def");
  3729. questlogButton[1] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 1), pos.x+740, pos.y+44, "hsbtns4.def");
  3730. //statusbar
  3731. ourBar = new CStatusBar(pos.x + 3, pos.y + 577, "TSTATBAR.bmp", 726);
  3732. //garrison interface
  3733. garr = new CGarrisonInt(pos.x + 69, pos.y + 131, 4, Point(418,0), bg, Point(69,131), heroInst[0],heroInst[1], true, true);
  3734. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+10,pos.y+132,"TSBTNS.DEF"));
  3735. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+740,pos.y+132,"TSBTNS.DEF"));
  3736. }
  3737. CExchangeWindow::~CExchangeWindow() //d-tor
  3738. {
  3739. SDL_FreeSurface(bg);
  3740. delete quit;
  3741. //warning: don't experiment with these =NULL lines, they prevent heap corruption!
  3742. delete artifs[0]->commonInfo;
  3743. artifs[0]->commonInfo = NULL;
  3744. delete artifs[0];
  3745. artifs[1]->commonInfo = NULL;
  3746. delete artifs[1];
  3747. delete garr;
  3748. delete ourBar;
  3749. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  3750. {
  3751. for(int b=0; b<secSkillAreas[g].size(); ++b)
  3752. {
  3753. delete secSkillAreas[g][b];
  3754. }
  3755. }
  3756. for(int b=0; b<primSkillAreas.size(); ++b)
  3757. {
  3758. delete primSkillAreas[b];
  3759. }
  3760. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  3761. {
  3762. delete questlogButton[g];
  3763. }
  3764. for(int g=0; g<ARRAY_COUNT(morale); g++)
  3765. {
  3766. delete morale[g];
  3767. }
  3768. for(int g=0; g<ARRAY_COUNT(luck); g++)
  3769. {
  3770. delete luck[g];
  3771. }
  3772. }
  3773. void CShipyardWindow::activate()
  3774. {
  3775. build->activate();
  3776. quit->activate();
  3777. }
  3778. void CShipyardWindow::deactivate()
  3779. {
  3780. build->deactivate();
  3781. quit->deactivate();
  3782. }
  3783. void CShipyardWindow::show( SDL_Surface * to )
  3784. {
  3785. blitAt(bg,pos,to);
  3786. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->boatAnims[1]->ourImages[21 + frame++/8%8].bitmap, NULL, to, &genRect(64, 96, pos.x+110, pos.y+85));
  3787. build->show(to);
  3788. quit->show(to);
  3789. }
  3790. CShipyardWindow::~CShipyardWindow()
  3791. {
  3792. delete build;
  3793. delete quit;
  3794. }
  3795. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, const boost::function<void()> &onBuy)
  3796. {
  3797. frame = 0;
  3798. SDL_Surface * bgtemp; //loaded as 8bpp surface
  3799. bgtemp = BitmapHandler::loadBitmap("TPSHIP.bmp");
  3800. pos.x = screen->w/2 - bgtemp->w/2;
  3801. pos.y = screen->h/2 - bgtemp->h/2;
  3802. pos.w = bgtemp->w;
  3803. pos.h = bgtemp->h;
  3804. SDL_SetColorKey(bgtemp,SDL_SRCCOLORKEY,SDL_MapRGB(bgtemp->format,0,255,255));
  3805. graphics->blueToPlayersAdv(bgtemp, LOCPLINT->playerID);
  3806. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  3807. SDL_FreeSurface(bgtemp);
  3808. bgtemp = BitmapHandler::loadBitmap("TPSHIPBK.bmp");
  3809. blitAt(bgtemp, 100, 69, bg);
  3810. SDL_FreeSurface(bgtemp);
  3811. // Draw resource icons and costs.
  3812. std::string goldCost = boost::lexical_cast<std::string>(1000);
  3813. std::string woodCost = boost::lexical_cast<std::string>(10);
  3814. blitAt(graphics->resources32->ourImages[6].bitmap, 100, 244, bg);
  3815. printAtMiddle(goldCost.c_str(), 116, 290, GEOR13, zwykly, bg);
  3816. blitAt(graphics->resources32->ourImages[0].bitmap, 196, 244, bg);
  3817. printAtMiddle(woodCost.c_str(), 212, 290, GEOR13, zwykly, bg);
  3818. bool affordable = true;
  3819. for(int i = 0; i < cost.size(); i++)
  3820. {
  3821. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  3822. {
  3823. affordable = false;
  3824. break;
  3825. }
  3826. }
  3827. quit = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+224, pos.y+312, "ICANCEL.DEF", SDLK_RETURN);
  3828. build = new AdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+42, pos.y+312, "IBY6432.DEF", SDLK_RETURN);
  3829. build->callback += onBuy;
  3830. if(!affordable)
  3831. build->block(true);
  3832. printAtMiddle(CGI->generaltexth->jktexts[13], 165, 28, GEORXX, tytulowy, bg); //Build A New Ship
  3833. printAtMiddle(CGI->generaltexth->jktexts[14], 165, 218, GEOR16, zwykly, bg); //Resource cost:
  3834. }
  3835. CPuzzleWindow::CPuzzleWindow()
  3836. :animCount(0)
  3837. {
  3838. SDL_Surface * back = BitmapHandler::loadBitmap("PUZZLE.BMP", false);
  3839. graphics->blueToPlayersAdv(back, LOCPLINT->playerID);
  3840. //make transparency black
  3841. back->format->palette->colors[0].b = back->format->palette->colors[0].r = back->format->palette->colors[0].g = 0;
  3842. //the rest
  3843. background = SDL_ConvertSurface(back, screen->format, back->flags);
  3844. SDL_FreeSurface(back);
  3845. pos = genRect(background->h, background->w, (conf.cc.resx - background->w) / 2, (conf.cc.resy - background->h) / 2);
  3846. quitb = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+670, pos.y+538, "IOK6432.DEF", SDLK_RETURN);
  3847. resdatabar = new CResDataBar("ZRESBAR.bmp", pos.x+3, pos.y+575, 32, 2, 85, 85);
  3848. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  3849. //printing necessary thinks to background
  3850. CGI->mh->terrainRect
  3851. (int3(14, 15, 0), LOCPLINT->adventureInt->anim,
  3852. &LOCPLINT->cb->getVisibilityMap(), true, LOCPLINT->adventureInt->heroAnim,
  3853. background, &genRect(544, 591, 8, 8), 0, 0, true);
  3854. float discoveryRatio = 0.5f;
  3855. int faction = LOCPLINT->cb->getStartInfo()->playerInfos[LOCPLINT->serialID].castle;
  3856. std::vector<SPuzzleInfo> puzzlesToPrint;
  3857. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  3858. {
  3859. if(CGI->heroh->puzzleInfo[faction][g].whenUncovered >= PUZZLES_PER_FACTION * discoveryRatio)
  3860. {
  3861. puzzlesToPrint.push_back(CGI->heroh->puzzleInfo[faction][g]);
  3862. }
  3863. else
  3864. {
  3865. SDL_Surface *buf = BitmapHandler::loadBitmap(CGI->heroh->puzzleInfo[faction][g].filename);
  3866. puzzlesToPullBack.push_back( std::make_pair(buf, &CGI->heroh->puzzleInfo[faction][g]) );
  3867. }
  3868. }
  3869. for(int b = 0; b < puzzlesToPrint.size(); ++b)
  3870. {
  3871. SDL_Surface * puzzle = BitmapHandler::loadBitmap(puzzlesToPrint[b].filename);
  3872. blitAt(puzzle, puzzlesToPrint[b].x, puzzlesToPrint[b].y, background);
  3873. SDL_FreeSurface(puzzle);
  3874. }
  3875. }
  3876. CPuzzleWindow::~CPuzzleWindow()
  3877. {
  3878. delete quitb;
  3879. delete resdatabar;
  3880. SDL_FreeSurface(background);
  3881. for(int g = 0; g < puzzlesToPullBack.size(); ++g)
  3882. SDL_FreeSurface( puzzlesToPullBack[g].first );
  3883. }
  3884. void CPuzzleWindow::activate()
  3885. {
  3886. quitb->activate();
  3887. }
  3888. void CPuzzleWindow::deactivate()
  3889. {
  3890. quitb->deactivate();
  3891. }
  3892. void CPuzzleWindow::show(SDL_Surface * to)
  3893. {
  3894. blitAt(background, pos.x, pos.y, to);
  3895. quitb->show(to);
  3896. resdatabar->draw(to);
  3897. //blitting disappearing puzzles
  3898. for(int g=0; g<2; ++g)
  3899. if(animCount != 255)
  3900. ++animCount;
  3901. if(animCount != 255)
  3902. {
  3903. for(int b = 0; b < puzzlesToPullBack.size(); ++b)
  3904. {
  3905. int xPos = puzzlesToPullBack[b].second->x + pos.x,
  3906. yPos = puzzlesToPullBack[b].second->y + pos.y;
  3907. SDL_Surface *from = puzzlesToPullBack[b].first;
  3908. SDL_SetAlpha(from, SDL_SRCALPHA, 255 - animCount);
  3909. blitAt(from, xPos, yPos, to);
  3910. }
  3911. }
  3912. //disappearing puzzles blitted
  3913. //printing border around window
  3914. if(screen->w != 800 || screen->h !=600)
  3915. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  3916. }