2
0

GUIClasses.cpp 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981
  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 "CCreatureWindow.h"
  9. #include "CCursorHandler.h"
  10. #include "CGameInfo.h"
  11. #include "CHeroWindow.h"
  12. #include "CMessage.h"
  13. #include "CConfigHandler.h"
  14. #include "SDL_framerate.h"
  15. #include "CConfigHandler.h"
  16. #include "CCreatureAnimation.h"
  17. #include "CPlayerInterface.h"
  18. #include "Graphics.h"
  19. #include "CAnimation.h"
  20. #include "../lib/CArtHandler.h"
  21. #include "../lib/CBuildingHandler.h"
  22. #include "../lib/CGeneralTextHandler.h"
  23. #include "../lib/CHeroHandler.h"
  24. #include "../lib/CLodHandler.h"
  25. #include "../lib/CObjectHandler.h"
  26. #include "../lib/CSpellHandler.h"
  27. #include "../lib/CTownHandler.h"
  28. #include "../lib/CondSh.h"
  29. #include "../lib/map.h"
  30. #include "mapHandler.h"
  31. #include "../timeHandler.h"
  32. #include <boost/foreach.hpp>
  33. #include <boost/lexical_cast.hpp>
  34. #include <boost/format.hpp>
  35. #include <boost/algorithm/string.hpp>
  36. #include <boost/algorithm/string/replace.hpp>
  37. #include <boost/assign/std/vector.hpp>
  38. #include <boost/assign/list_of.hpp>
  39. #include <boost/date_time/posix_time/posix_time.hpp>
  40. //#include <boost/thread.hpp>
  41. #include <cmath>
  42. #include <queue>
  43. #include <sstream>
  44. #include "../lib/NetPacks.h"
  45. #include "CSpellWindow.h"
  46. #include "CHeroWindow.h"
  47. #include "CVideoHandler.h"
  48. #include "../StartInfo.h"
  49. #include "CPreGame.h"
  50. #include "../lib/HeroBonus.h"
  51. #include "../lib/CCreatureHandler.h"
  52. #include "CMusicHandler.h"
  53. #include "../lib/BattleState.h"
  54. /*
  55. * GUIClasses.cpp, part of VCMI engine
  56. *
  57. * Authors: listed in file AUTHORS in main folder
  58. *
  59. * License: GNU General Public License v2.0 or later
  60. * Full text of license available in license.txt file, in main folder
  61. *
  62. */
  63. using namespace boost::assign;
  64. using namespace CSDL_Ext;
  65. extern std::queue<SDL_Event*> events;
  66. extern boost::mutex eventsM;
  67. std::list<CFocusable*> CFocusable::focusables;
  68. CFocusable * CFocusable::inputWithFocus;
  69. #undef min
  70. #undef max
  71. void CGarrisonSlot::hover (bool on)
  72. {
  73. ////Hoverable::hover(on);
  74. if(on)
  75. {
  76. std::string temp;
  77. if(creature)
  78. {
  79. if(owner->highlighted)
  80. {
  81. if(owner->highlighted == this)
  82. {
  83. temp = CGI->generaltexth->tcommands[4]; //View %s
  84. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  85. }
  86. else if (owner->highlighted->creature == creature)
  87. {
  88. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  89. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  90. }
  91. else if (owner->highlighted->creature)
  92. {
  93. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  94. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  95. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  96. }
  97. else
  98. {
  99. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  100. tlog2 << "Highlighted set to NULL"<<std::endl;
  101. owner->highlighted = NULL;
  102. }
  103. }
  104. else
  105. {
  106. if(upg)
  107. {
  108. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  109. }
  110. else if(owner->armedObjs[0] && owner->armedObjs[0]->ID == TOWNI_TYPE)
  111. {
  112. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  113. }
  114. else
  115. {
  116. temp = CGI->generaltexth->allTexts[481]; //Select %s
  117. }
  118. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  119. };
  120. }
  121. else
  122. {
  123. if(owner->highlighted)
  124. {
  125. const CArmedInstance *highl = owner->highlighted->getObj();
  126. if( highl->needsLastStack() //we are moving stack from hero's
  127. && highl->stacksCount() == 1 //it's only stack
  128. && owner->highlighted->upg != upg //we're moving it to the other garrison
  129. )
  130. {
  131. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  132. }
  133. else
  134. {
  135. temp = CGI->generaltexth->tcommands[6]; //Move %s
  136. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  137. }
  138. }
  139. else
  140. {
  141. temp = CGI->generaltexth->tcommands[11]; //Empty
  142. }
  143. }
  144. GH.statusbar->print(temp);
  145. }
  146. else
  147. {
  148. GH.statusbar->clear();
  149. }
  150. }
  151. const CArmedInstance * CGarrisonSlot::getObj()
  152. {
  153. return (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]);
  154. }
  155. bool CGarrisonSlot::our()
  156. {
  157. return upg?(owner->owned[1]):(owner->owned[0]);
  158. }
  159. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  160. {
  161. if(down && creature)
  162. {
  163. //GH.pushInt(new CCreInfoWindow(*myStack));
  164. GH.pushInt(new CCreatureWindow(*myStack, 2));
  165. }
  166. }
  167. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  168. {
  169. if(down)
  170. {
  171. bool refr = false;
  172. if(owner->highlighted)
  173. {
  174. if(owner->highlighted == this) //view info
  175. {
  176. UpgradeInfo pom = LOCPLINT->cb->getUpgradeInfo(getObj(), ID);
  177. bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
  178. bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID && (getObj()->stacksCount()>1 || !getObj()->needsLastStack());
  179. boost::function<void()> upgr = NULL;
  180. boost::function<void()> dism = NULL;
  181. if (canUpgrade) upgr = boost::bind(&CCallback::upgradeCreature, LOCPLINT->cb, getObj(), ID, pom.newID[0]);
  182. if (canDismiss) dism = boost::bind(&CCallback::dismissCreature, LOCPLINT->cb, getObj(), ID);
  183. //CCreInfoWindow *creWindow = new CCreInfoWindow( *myStack, 1, upgr, dism, &pom);
  184. CCreatureWindow *creWindow = new CCreatureWindow( *myStack, 3, upgr, dism, &pom);
  185. GH.pushInt(creWindow);
  186. owner->highlighted = NULL;
  187. owner->splitting = false;
  188. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  189. owner->splitButtons[i]->block(true);
  190. redraw();
  191. refr = true;
  192. }
  193. else
  194. {
  195. // Only allow certain moves if troops aren't removable or not ours.
  196. if ( ( owner->highlighted->our()//our creature is selected
  197. || owner->highlighted->creature == creature )//or we are rebalancing army
  198. && ( owner->removableUnits
  199. || (upg == 0 && ( owner->highlighted->upg == 1 && !creature ) )
  200. || (upg == 1 && owner->highlighted->upg == 1 ) ) )
  201. {
  202. //we want to split
  203. if((owner->splitting || LOCPLINT->shiftPressed())
  204. && (!creature
  205. || (creature == owner->highlighted->creature)))
  206. {
  207. owner->p2 = ID; //store the second stack pos
  208. owner->pb = upg;//store the second stack owner (up or down army)
  209. owner->splitting = false;
  210. int totalAmount = owner->highlighted->count;
  211. if(creature)
  212. totalAmount += count;
  213. int last = -1;
  214. if(upg != owner->highlighted->upg) //not splitting within same army
  215. {
  216. if(owner->highlighted->getObj()->stacksCount() == 1 //we're splitting away the last stack
  217. && owner->highlighted->getObj()->needsLastStack() )
  218. {
  219. last = 0;
  220. }
  221. if(getObj()->stacksCount() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
  222. && owner->highlighted->creature == creature
  223. && getObj()->needsLastStack() )
  224. {
  225. last += 2;
  226. }
  227. }
  228. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  229. GH.pushInt(spw);
  230. refr = true;
  231. }
  232. else if(creature != owner->highlighted->creature) //swap
  233. {
  234. LOCPLINT->cb->swapCreatures(
  235. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  236. (!owner->highlighted->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  237. ID,owner->highlighted->ID);
  238. }
  239. else //merge
  240. {
  241. LOCPLINT->cb->mergeStacks(
  242. (!owner->highlighted->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  243. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  244. owner->highlighted->ID,ID);
  245. }
  246. }
  247. else // Highlight
  248. {
  249. if(creature)
  250. owner->highlighted = this;
  251. redraw();
  252. refr = true;
  253. }
  254. }
  255. }
  256. else //highlight
  257. {
  258. if(creature)
  259. {
  260. owner->highlighted = this;
  261. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  262. owner->splitButtons[i]->block(false);
  263. }
  264. redraw();
  265. refr = true;
  266. }
  267. if(refr) {hover(false); hover(true); } //to refresh statusbar
  268. }
  269. }
  270. void CGarrisonSlot::activate()
  271. {
  272. if(!active) active=true;
  273. else return;
  274. activateLClick();
  275. activateRClick();
  276. activateHover();
  277. }
  278. void CGarrisonSlot::deactivate()
  279. {
  280. if(active) active=false;
  281. else return;
  282. deactivateLClick();
  283. deactivateRClick();
  284. deactivateHover();
  285. }
  286. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CStackInstance * Creature)
  287. {
  288. //assert(Creature == CGI->creh->creatures[Creature->idNumber]);
  289. active = false;
  290. upg = Upg;
  291. ID = IID;
  292. myStack = Creature;
  293. creature = Creature ? Creature->type : NULL;
  294. count = Creature ? Creature->count : 0;
  295. pos.x += x;
  296. pos.y += y;
  297. if(Owner->smallIcons)
  298. {
  299. pos.w = 32;
  300. pos.h = 32;
  301. }
  302. else
  303. {
  304. pos.w = 58;
  305. pos.h = 64;
  306. }
  307. owner = Owner;
  308. }
  309. CGarrisonSlot::~CGarrisonSlot()
  310. {
  311. if(active)
  312. deactivate();
  313. }
  314. void CGarrisonSlot::showAll(SDL_Surface * to)
  315. {
  316. std::map<int,SDL_Surface*> &imgs = (owner->smallIcons ? graphics->smallImgs : graphics->bigImgs);
  317. if(creature)
  318. {
  319. char buf[15];
  320. SDL_itoa(count,buf,10);
  321. blitAt(imgs[creature->idNumber],pos,to);
  322. printTo(buf, pos.x+pos.w, pos.y+pos.h+1, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, zwykly, to);
  323. if((owner->highlighted==this)
  324. || (owner->splitting && owner->highlighted->creature == creature))
  325. {
  326. blitAt(imgs[-1],pos,to);
  327. }
  328. }
  329. else//empty slot
  330. {
  331. Rect pos1 = pos, pos2 = pos; //positions on the garr bg sur and scren
  332. pos1.x = owner->surOffset.x+ pos.x-owner->pos.x;
  333. pos1.y = owner->surOffset.y+ pos.y-owner->pos.y;
  334. CSDL_Ext::blitSurface(owner->sur,&pos1,to,&pos2);
  335. if(owner->splitting && owner->highlighted->our())
  336. blitAt(imgs[-1],pos,to);
  337. }
  338. }
  339. CGarrisonInt::~CGarrisonInt()
  340. {/*
  341. for(size_t i = 0; i<splitButtons.size(); i++)
  342. delete splitButtons[i];*/
  343. }
  344. void CGarrisonInt::addSplitBtn(AdventureMapButton * button)
  345. {
  346. addChild(button);
  347. button->recActions = defActions;
  348. splitButtons.push_back(button);
  349. }
  350. void CGarrisonInt::createSet(std::vector<CGarrisonSlot*> &ret, const CCreatureSet * set, int posX, int posY, int distance, int Upg )
  351. {
  352. ret.resize(7);
  353. for(TSlots::const_iterator i=set->Slots().begin(); i!=set->Slots().end(); i++)
  354. {
  355. ret[i->first] = new CGarrisonSlot(this, posX + (i->first*distance), posY, i->first, Upg, i->second);
  356. }
  357. for(int i=0; i<ret.size(); i++)
  358. if(!ret[i])
  359. ret[i] = new CGarrisonSlot(this, posX + (i*distance), posY,i,Upg,NULL);
  360. if (twoRows)
  361. for (int i=4; i<ret.size(); i++)
  362. {
  363. ret[i]->pos.x -= 126;
  364. ret[i]->pos.y += 37;
  365. };
  366. }
  367. void CGarrisonInt::createSlots()
  368. {
  369. OBJ_CONSTRUCTION_CAPTURING_ALL;
  370. int h, w; //height and width of slot
  371. if(smallIcons)
  372. {
  373. h = w = 32;
  374. }
  375. else
  376. {
  377. h = 64;
  378. w = 58;
  379. }
  380. if(armedObjs[0])
  381. createSet(slotsUp, armedObjs[0], 0, 0, w+interx, 0);
  382. if(armedObjs[1])
  383. createSet (slotsDown, armedObjs[1], garOffset.x, garOffset.y, w+interx, 1);
  384. }
  385. void CGarrisonInt::deleteSlots()
  386. {
  387. for (int i=0; i<slotsUp.size(); i++)
  388. delChildNUll(slotsUp[i]);
  389. for (int i=0; i<slotsDown.size(); i++)
  390. delChildNUll(slotsDown[i]);
  391. }
  392. void CGarrisonInt::recreateSlots()
  393. {
  394. splitting = false;
  395. highlighted = NULL;
  396. for(size_t i = 0; i<splitButtons.size(); i++)
  397. splitButtons[i]->block(true);
  398. bool wasActive = active;
  399. if(active)
  400. {
  401. deactivate();
  402. }
  403. deleteSlots();
  404. createSlots();
  405. if(wasActive)
  406. {
  407. activate();
  408. showAll(screen2);
  409. }
  410. }
  411. void CGarrisonInt::splitClick()
  412. {
  413. if(!highlighted)
  414. return;
  415. splitting = !splitting;
  416. redraw();
  417. }
  418. void CGarrisonInt::splitStacks(int am2)
  419. {
  420. LOCPLINT->cb->splitStack(armedObjs[highlighted->upg], armedObjs[pb], highlighted->ID, p2, am2);
  421. }
  422. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset,
  423. SDL_Surface *&pomsur, const Point& SurOffset,
  424. const CArmedInstance *s1, const CArmedInstance *s2,
  425. bool _removableUnits, bool smallImgs, bool _twoRows )
  426. : interx(inx), garOffset(garsOffset), surOffset(SurOffset), highlighted(NULL), sur(pomsur), splitting(false),
  427. smallIcons(smallImgs), removableUnits (_removableUnits), twoRows(_twoRows)
  428. {
  429. setArmy(s1, false);
  430. setArmy(s2, true);
  431. pos.x += x;
  432. pos.y += y;
  433. createSlots();
  434. }
  435. void CGarrisonInt::activate()
  436. {
  437. for(size_t i = 0; i<splitButtons.size(); i++)
  438. if( (splitButtons[i]->isBlocked()) != !highlighted)
  439. splitButtons[i]->block(!highlighted);
  440. CIntObject::activate();
  441. }
  442. void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
  443. {
  444. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == 254) : false; //254 - neutral objects (pandora, banks)
  445. armedObjs[bottomGarrison] = army;
  446. }
  447. CInfoWindow::CInfoWindow(std::string Text, int player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  448. {
  449. OBJ_CONSTRUCTION_CAPTURING_ALL;
  450. type |= BLOCK_ADV_HOTKEYS;
  451. ID = -1;
  452. for(int i=0;i<Buttons.size();i++)
  453. {
  454. buttons.push_back(new AdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first));
  455. buttons[i]->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  456. }
  457. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, zwykly);
  458. text->redrawParentOnScrolling = true;
  459. if(buttons.size())
  460. {
  461. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  462. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  463. }
  464. for(int i=0;i<comps.size();i++)
  465. {
  466. comps[i]->recActions = 0xff;
  467. addChild(comps[i]);
  468. comps[i]->recActions &= ~(SHOWALL | UPDATE);
  469. components.push_back(comps[i]);
  470. }
  471. setDelComps(delComps);
  472. CMessage::drawIWindow(this,Text,player);
  473. }
  474. CInfoWindow::CInfoWindow()
  475. {
  476. ID = -1;
  477. setDelComps(false);
  478. text = NULL;
  479. }
  480. void CInfoWindow::close()
  481. {
  482. GH.popIntTotally(this);
  483. if(LOCPLINT)
  484. LOCPLINT->showingDialog->setn(false);
  485. }
  486. void CInfoWindow::show(SDL_Surface * to)
  487. {
  488. CIntObject::show(to);
  489. }
  490. CInfoWindow::~CInfoWindow()
  491. {
  492. if(!delComps)
  493. {
  494. for (int i=0;i<components.size();i++)
  495. removeChild(components[i]);
  496. }
  497. }
  498. void CInfoWindow::showAll( SDL_Surface * to )
  499. {
  500. CSimpleWindow::show(to);
  501. CIntObject::showAll(to);
  502. }
  503. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<SComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, int player)
  504. {
  505. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  506. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  507. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  508. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  509. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<SComponent*>(), pom, DelComps);
  510. for(int i=0;i<onYes.funcs.size();i++)
  511. temp->buttons[0]->callback += onYes.funcs[i];
  512. for(int i=0;i<onNo.funcs.size();i++)
  513. temp->buttons[1]->callback += onNo.funcs[i];
  514. GH.pushInt(temp);
  515. }
  516. CInfoWindow * CInfoWindow::create(const std::string &text, int playerID /*= 1*/, const std::vector<SComponent*> *components /*= NULL*/, bool DelComps)
  517. {
  518. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  519. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  520. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<SComponent*>(), pom, DelComps);
  521. return ret;
  522. }
  523. void CInfoWindow::setDelComps(bool DelComps)
  524. {
  525. delComps = DelComps;
  526. BOOST_FOREACH(SComponent *comp, components)
  527. {
  528. if(delComps)
  529. comp->recActions |= DISPOSE;
  530. else
  531. comp->recActions &= ~DISPOSE;
  532. }
  533. }
  534. void CRClickPopup::clickRight(tribool down, bool previousState)
  535. {
  536. if(down)
  537. return;
  538. close();
  539. }
  540. void CRClickPopup::activate()
  541. {
  542. activateRClick();
  543. }
  544. void CRClickPopup::deactivate()
  545. {
  546. deactivateRClick();
  547. }
  548. void CRClickPopup::close()
  549. {
  550. GH.popIntTotally(this);
  551. }
  552. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  553. {
  554. int player = LOCPLINT ? LOCPLINT->playerID : 1; //if no player, then use blue
  555. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  556. temp->center(Point(GH.current->motion)); //center on mouse
  557. temp->fitToScreen(10);
  558. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  559. GH.pushInt(rcpi);
  560. }
  561. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  562. {
  563. SDL_Surface *iWin = LOCPLINT->infoWin(obj); //try get custom infowindow for this obj
  564. if(iWin)
  565. GH.pushInt(new CInfoPopup(iWin, p, alignment, true));
  566. else
  567. CRClickPopup::createAndPush(obj->getHoverText());
  568. }
  569. CRClickPopup::CRClickPopup()
  570. {
  571. }
  572. CRClickPopup::~CRClickPopup()
  573. {
  574. }
  575. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  576. :free(Free),bitmap(Bitmap)
  577. {
  578. init(x, y);
  579. }
  580. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  581. : free(Free),bitmap(Bitmap)
  582. {
  583. switch(alignment)
  584. {
  585. case BOTTOMRIGHT:
  586. init(p.x - Bitmap->w, p.y - Bitmap->h);
  587. break;
  588. case CENTER:
  589. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  590. break;
  591. case TOPLEFT:
  592. init(p.x, p.y);
  593. break;
  594. default:
  595. assert(0); //not implemented
  596. }
  597. }
  598. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  599. {
  600. CCS->curh->hide();
  601. free=Free;
  602. bitmap=Bitmap;
  603. if(bitmap)
  604. {
  605. pos.x = screen->w/2 - bitmap->w/2;
  606. pos.y = screen->h/2 - bitmap->h/2;
  607. pos.h = bitmap->h;
  608. pos.w = bitmap->w;
  609. }
  610. }
  611. void CInfoPopup::close()
  612. {
  613. if(free)
  614. SDL_FreeSurface(bitmap);
  615. GH.popIntTotally(this);
  616. }
  617. void CInfoPopup::show(SDL_Surface * to)
  618. {
  619. blitAt(bitmap,pos.x,pos.y,to);
  620. }
  621. CInfoPopup::~CInfoPopup()
  622. {
  623. CCS->curh->show();
  624. }
  625. void CInfoPopup::init(int x, int y)
  626. {
  627. CCS->curh->hide();
  628. pos.x = x;
  629. pos.y = y;
  630. pos.h = bitmap->h;
  631. pos.w = bitmap->w;
  632. // Put the window back on screen if necessary
  633. amax(pos.x, 0);
  634. amax(pos.y, 0);
  635. amin(pos.x, conf.cc.resx - bitmap->w);
  636. amin(pos.y, conf.cc.resy - bitmap->h);
  637. }
  638. void SComponent::init(Etype Type, int Subtype, int Val)
  639. {
  640. std::ostringstream oss;
  641. switch (Type)
  642. {
  643. case artifact:
  644. description = CGI->arth->artifacts[Subtype]->Description();
  645. subtitle = CGI->arth->artifacts[Subtype]->Name();
  646. break;
  647. case primskill:
  648. oss << std::showpos << Val << " ";
  649. if(Subtype < 4)
  650. {
  651. description = CGI->generaltexth->arraytxt[2+Subtype];
  652. oss << CGI->generaltexth->primarySkillNames[Subtype];
  653. }
  654. else if(Subtype == 5) //spell points
  655. {
  656. description = CGI->generaltexth->allTexts[149];
  657. oss << CGI->generaltexth->allTexts[387];
  658. }
  659. else
  660. {
  661. tlog1 << "Wrong subtype=" << Subtype << std::endl;
  662. }
  663. subtitle = oss.str();
  664. break;
  665. case building:
  666. description = CGI->buildh->buildings[Subtype][Val]->Description();
  667. subtitle = CGI->buildh->buildings[Subtype][Val]->Name();
  668. break;
  669. case secskill44: case secskill:
  670. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  671. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  672. break;
  673. case morale:
  674. description = CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  675. break;
  676. case luck:
  677. description = CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  678. break;
  679. case resource:
  680. description = CGI->generaltexth->allTexts[242];
  681. oss << Val;
  682. subtitle = oss.str();
  683. break;
  684. case spell:
  685. description = CGI->spellh->spells[Subtype]->descriptions[Val];
  686. subtitle = CGI->spellh->spells[Subtype]->name;
  687. break;
  688. case creature:
  689. subtitle = (Val? boost::lexical_cast<std::string>(Val) + " " : "") + CGI->creh->creatures[Subtype]->*(Val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  690. break;
  691. case experience:
  692. description = CGI->generaltexth->allTexts[241];
  693. oss << Val ;
  694. if(Subtype && Val==1)
  695. {
  696. subtitle = CGI->generaltexth->allTexts[442];
  697. }
  698. else
  699. {
  700. subtitle = oss.str();
  701. }
  702. break;
  703. case hero:
  704. subtitle = description = CGI->heroh->heroes[Subtype]->name;
  705. break;
  706. case flag:
  707. subtitle = CGI->generaltexth->capColors[Subtype];
  708. break;
  709. }
  710. img = NULL;
  711. free = false;
  712. type = Type;
  713. subtype = Subtype;
  714. val = Val;
  715. SDL_Surface * temp = this->getImg();
  716. if(!temp)
  717. {
  718. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  719. return;
  720. }
  721. pos.w = temp->w;
  722. pos.h = temp->h;
  723. }
  724. SComponent::SComponent(Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur):img(sur),free(freeSur)
  725. {
  726. init(Type,Subtype,Val);
  727. }
  728. SComponent::SComponent(const Component &c)
  729. {
  730. if(c.id==5)
  731. init(experience,c.subtype,c.val);
  732. else if(c.id == Component::SPELL)
  733. init(spell,c.subtype,c.val);
  734. else
  735. init((Etype)c.id,c.subtype,c.val);
  736. if(c.id==2 && c.when==-1)
  737. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  738. }
  739. SComponent::SComponent()
  740. {
  741. img = NULL;
  742. }
  743. SComponent::~SComponent()
  744. {
  745. if (free && img)
  746. SDL_FreeSurface(img);
  747. }
  748. SDL_Surface * SComponent::setSurface(std:: string defname, int imagepos)
  749. {
  750. if (img)
  751. tlog1<<"SComponent::setSurface: Warning - surface is already set!\n";
  752. CDefEssential * def = CDefHandler::giveDefEss(defname);
  753. free = true;
  754. img = def->ourImages[imagepos].bitmap;
  755. img->refcount++;//to preserve surface whed def is deleted
  756. delete def;
  757. return img;
  758. }
  759. void SComponent::show(SDL_Surface * to)
  760. {
  761. blitAt(getImg(),pos.x,pos.y,to);
  762. }
  763. SDL_Surface * SComponent::getImg()
  764. {
  765. if (img)
  766. return img;
  767. switch (type)
  768. {
  769. case artifact:
  770. return graphics->artDefs->ourImages[subtype].bitmap;
  771. case primskill:
  772. return graphics->pskillsb->ourImages[subtype].bitmap;
  773. case secskill44:
  774. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  775. case secskill:
  776. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  777. case resource:
  778. return graphics->resources->ourImages[subtype].bitmap;
  779. case experience:
  780. return graphics->pskillsb->ourImages[4].bitmap;
  781. case morale:
  782. return graphics->morale82->ourImages[val+3].bitmap;
  783. case luck:
  784. return graphics->luck82->ourImages[val+3].bitmap;
  785. case spell:
  786. return graphics->spellscr->ourImages[subtype].bitmap;
  787. case building:
  788. return setSurface(graphics->buildingPics[subtype],val);
  789. case creature:
  790. return graphics->bigImgs[subtype];
  791. case hero:
  792. return graphics->portraitLarge[subtype];
  793. case flag:
  794. return graphics->flags->ourImages[subtype].bitmap;
  795. }
  796. return NULL;
  797. }
  798. void SComponent::clickRight(tribool down, bool previousState)
  799. {
  800. if(description.size())
  801. adventureInt->handleRightClick(description,down);
  802. }
  803. void SComponent::activate()
  804. {
  805. activateRClick();
  806. }
  807. void SComponent::deactivate()
  808. {
  809. deactivateRClick();
  810. }
  811. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  812. {
  813. if (down)
  814. {
  815. if(onSelect)
  816. onSelect();
  817. }
  818. }
  819. void CSelectableComponent::init()
  820. {
  821. selected = false;
  822. }
  823. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect)
  824. :SComponent(c),onSelect(OnSelect)
  825. {
  826. init();
  827. }
  828. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect)
  829. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  830. {
  831. init();
  832. }
  833. CSelectableComponent::~CSelectableComponent()
  834. {
  835. }
  836. void CSelectableComponent::activate()
  837. {
  838. activateKeys();
  839. SComponent::activate();
  840. activateLClick();
  841. }
  842. void CSelectableComponent::deactivate()
  843. {
  844. deactivateKeys();
  845. SComponent::deactivate();
  846. deactivateLClick();
  847. }
  848. void CSelectableComponent::select(bool on)
  849. {
  850. if(on != selected)
  851. {
  852. selected = on;
  853. return;
  854. }
  855. else
  856. {
  857. return;
  858. }
  859. }
  860. void CSelectableComponent::show(SDL_Surface * to)
  861. {
  862. blitAt(getImg(),pos.x,pos.y,to);
  863. if(selected)
  864. {
  865. CSDL_Ext::drawBorder(to, Rect::around(Rect(pos.x, pos.y, getImg()->w, getImg()->h)), int3(239,215,123));
  866. }
  867. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+25,FONT_SMALL,12,zwykly,to);
  868. }
  869. void CSimpleWindow::show(SDL_Surface * to)
  870. {
  871. if(bitmap)
  872. blitAt(bitmap,pos.x,pos.y,to);
  873. }
  874. CSimpleWindow::~CSimpleWindow()
  875. {
  876. if (bitmap)
  877. {
  878. SDL_FreeSurface(bitmap);
  879. bitmap=NULL;
  880. }
  881. }
  882. void CSelWindow::selectionChange(unsigned to)
  883. {
  884. for (unsigned i=0;i<components.size();i++)
  885. {
  886. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  887. if (!pom)
  888. continue;
  889. pom->select(i==to);
  890. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  891. }
  892. }
  893. 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)
  894. {
  895. OBJ_CONSTRUCTION_CAPTURING_ALL;
  896. ID = askID;
  897. for(int i=0;i<Buttons.size();i++)
  898. {
  899. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  900. if(!i && askID >= 0)
  901. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  902. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  903. }
  904. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, zwykly);
  905. text->redrawParentOnScrolling = true;
  906. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  907. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  908. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  909. buttons.back()->callback += boost::bind(&ICallback::selectionMade,LOCPLINT->cb,0,askID);
  910. for(int i=0;i<comps.size();i++)
  911. {
  912. comps[i]->recActions = 255;
  913. addChild(comps[i]);
  914. components.push_back(comps[i]);
  915. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  916. if(i<9)
  917. comps[i]->assignedKeys.insert(SDLK_1+i);
  918. }
  919. CMessage::drawIWindow(this, Text, player);
  920. BOOST_FOREACH(SComponent *c, components)
  921. c->subtitle = "";//workaround - erase subtitles since they were hard-blitted by function drawing window
  922. }
  923. void CSelWindow::madeChoice()
  924. {
  925. if(ID < 0)
  926. return;
  927. int ret = -1;
  928. for (int i=0;i<components.size();i++)
  929. {
  930. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  931. {
  932. ret = i;
  933. }
  934. }
  935. LOCPLINT->cb->selectionMade(ret+1,ID);
  936. }
  937. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  938. {
  939. bg=BitmapHandler::loadBitmap(name);
  940. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  941. pos.x += x;
  942. pos.y += y;
  943. if(maxw >= 0)
  944. pos.w = std::min(bg->w,maxw);
  945. else
  946. pos.w=bg->w;
  947. pos.h=bg->h;
  948. middlex=(pos.w/2)+pos.x;
  949. middley=(bg->h/2)+pos.y;
  950. }
  951. CStatusBar::~CStatusBar()
  952. {
  953. SDL_FreeSurface(bg);
  954. }
  955. void CStatusBar::clear()
  956. {
  957. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  958. {
  959. current="";
  960. redraw();
  961. }
  962. }
  963. void CStatusBar::print(const std::string & text)
  964. {
  965. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  966. {
  967. current=text;
  968. redraw();
  969. }
  970. }
  971. void CStatusBar::show(SDL_Surface * to)
  972. {
  973. SDL_Rect srcRect = genRect(pos.h,pos.w,0,0);
  974. SDL_Rect dstRect = genRect(pos.h,pos.w,pos.x,pos.y);
  975. CSDL_Ext::blitSurface(bg,&srcRect,to,&dstRect);
  976. printAtMiddle(current,middlex,middley,FONT_SMALL,zwykly,to);
  977. }
  978. std::string CStatusBar::getCurrent()
  979. {
  980. return current;
  981. }
  982. void CList::activate()
  983. {
  984. activateLClick();
  985. activateRClick();
  986. activateHover();
  987. activateKeys();
  988. activateMouseMove();
  989. };
  990. void CList::deactivate()
  991. {
  992. deactivateLClick();
  993. deactivateRClick();
  994. deactivateHover();
  995. deactivateKeys();
  996. deactivateMouseMove();
  997. };
  998. void CList::clickLeft(tribool down, bool previousState)
  999. {
  1000. };
  1001. CList::CList(int Size)
  1002. :SIZE(Size)
  1003. {
  1004. }
  1005. void CList::fixPos()
  1006. {
  1007. if(selected < 0) //no selection, do nothing
  1008. return;
  1009. if(selected < from) //scroll up
  1010. from = selected;
  1011. else if(from + SIZE <= selected)
  1012. from = selected - SIZE + 1;
  1013. amin(from, size() - SIZE);
  1014. amax(from, 0);
  1015. draw(screen);
  1016. }
  1017. CHeroList::CHeroList(int Size)
  1018. :CList(Size)
  1019. {
  1020. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  1021. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  1022. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  1023. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  1024. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  1025. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  1026. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  1027. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  1028. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  1029. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  1030. //32px per hero
  1031. posmobx = pos.x+1;
  1032. posmoby = pos.y+arrup->height+1;
  1033. posporx = pos.x+mobile->width+2;
  1034. pospory = pos.y+arrup->height;
  1035. posmanx = pos.x+1+50+mobile->width;
  1036. posmany = pos.y+arrup->height+1;
  1037. from = 0;
  1038. selected = -1;
  1039. pressed = indeterminate;
  1040. }
  1041. void CHeroList::init()
  1042. {
  1043. int w = pos.w+1, h = pos.h+4;
  1044. bg = CSDL_Ext::newSurface(w,h,screen);
  1045. Rect srcRect = genRect(w, h, pos.x, pos.y);
  1046. Rect dstRect = genRect(w, h, 0, 0);
  1047. CSDL_Ext::blitSurface(adventureInt->bg, &srcRect, bg, &dstRect);
  1048. }
  1049. void CHeroList::genList()
  1050. {
  1051. //int howMany = LOCPLINT->cb->howManyHeroes();
  1052. //for (int i=0;i<howMany;i++)
  1053. //{
  1054. // const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  1055. // if(!h->inTownGarrison)
  1056. // items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  1057. //}
  1058. }
  1059. void CHeroList::select(int which)
  1060. {
  1061. if (which<0)
  1062. {
  1063. selected = which;
  1064. adventureInt->selection = NULL;
  1065. adventureInt->terrain.currentPath = NULL;
  1066. draw(screen);
  1067. adventureInt->infoBar.showAll(screen);
  1068. }
  1069. if (which>=LOCPLINT->wanderingHeroes.size())
  1070. return;
  1071. selected = which;
  1072. adventureInt->select(LOCPLINT->wanderingHeroes[which]);
  1073. fixPos();
  1074. draw(screen);
  1075. }
  1076. void CHeroList::clickLeft(tribool down, bool previousState)
  1077. {
  1078. if (down)
  1079. {
  1080. /***************************ARROWS*****************************************/
  1081. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1082. {
  1083. if(from>0)
  1084. {
  1085. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  1086. pressed = true;
  1087. }
  1088. return;
  1089. }
  1090. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1091. {
  1092. if(LOCPLINT->wanderingHeroes.size()-from>SIZE)
  1093. {
  1094. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  1095. pressed = false;
  1096. }
  1097. return;
  1098. }
  1099. /***************************HEROES*****************************************/
  1100. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1101. hx-=pos.x;
  1102. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1103. int ny = hy/32;
  1104. if (ny>=SIZE || ny<0)
  1105. return;
  1106. if ( (ny+from)==selected && (adventureInt->selection->ID == HEROI_TYPE))
  1107. LOCPLINT->openHeroWindow(LOCPLINT->wanderingHeroes[selected]);//print hero screen
  1108. select(ny+from);
  1109. }
  1110. else
  1111. {
  1112. if (indeterminate(pressed))
  1113. return;
  1114. if (pressed) //up
  1115. {
  1116. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  1117. pressed = indeterminate;
  1118. if (!down)
  1119. {
  1120. from--;
  1121. if (from<0)
  1122. from=0;
  1123. draw(screen);
  1124. }
  1125. }
  1126. else if (!pressed) //down
  1127. {
  1128. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  1129. pressed = indeterminate;
  1130. if (!down)
  1131. {
  1132. from++;
  1133. //if (from<items.size()-5)
  1134. // from=items.size()-5;
  1135. draw(screen);
  1136. }
  1137. }
  1138. else
  1139. throw 0;
  1140. }
  1141. }
  1142. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1143. {
  1144. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1145. {
  1146. if (from>0)
  1147. adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  1148. else
  1149. adventureInt->statusbar.clear();
  1150. return;
  1151. }
  1152. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1153. {
  1154. if ((LOCPLINT->wanderingHeroes.size()-from) > SIZE)
  1155. adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1156. else
  1157. adventureInt->statusbar.clear();
  1158. return;
  1159. }
  1160. //if not buttons then heroes
  1161. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1162. hx-=pos.x;
  1163. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1164. int ny = hy/32;
  1165. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1166. {
  1167. adventureInt->statusbar.clear();
  1168. return;
  1169. }
  1170. std::vector<std::string> temp;
  1171. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->name);
  1172. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->type->heroClass->name);
  1173. adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1174. //select(ny+from);
  1175. }
  1176. void CHeroList::clickRight(tribool down, bool previousState)
  1177. {
  1178. if (down)
  1179. {
  1180. /***************************ARROWS*****************************************/
  1181. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1182. {
  1183. adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down);
  1184. }
  1185. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->wanderingHeroes.size()-from>5))
  1186. {
  1187. adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down);
  1188. }
  1189. else
  1190. {
  1191. //if not buttons then heroes
  1192. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1193. hx-=pos.x;
  1194. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1195. int ny = hy/32;
  1196. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1197. {
  1198. return;
  1199. } //show popup
  1200. CRClickPopup::createAndPush(LOCPLINT->wanderingHeroes[from+ny], GH.current->motion);
  1201. }
  1202. }
  1203. }
  1204. void CHeroList::hover (bool on)
  1205. {
  1206. }
  1207. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1208. {
  1209. }
  1210. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1211. {
  1212. if(toRemove) //remove specific hero
  1213. LOCPLINT->wanderingHeroes -= toRemove;
  1214. else
  1215. LOCPLINT->recreateHeroTownList();
  1216. if(selected >= LOCPLINT->wanderingHeroes.size())
  1217. select(LOCPLINT->wanderingHeroes.size()-1);
  1218. if(toRemove)
  1219. {
  1220. if(LOCPLINT->wanderingHeroes.size() == 0)
  1221. adventureInt->townList.select(0);
  1222. else
  1223. select(selected);
  1224. }
  1225. }
  1226. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1227. {
  1228. int ser = -1;
  1229. for(int i=0; i<LOCPLINT->wanderingHeroes.size() && ser<0; i++)
  1230. if(LOCPLINT->wanderingHeroes[i]->subID == which->subID)
  1231. ser = i;
  1232. ser -= from;
  1233. if(ser<0 || ser >= SIZE) return;
  1234. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1235. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1236. }
  1237. void CHeroList::draw(SDL_Surface * to)
  1238. {
  1239. for (int iT=0+from;iT<SIZE+from;iT++)
  1240. {
  1241. int i = iT-from;
  1242. if (iT>=LOCPLINT->wanderingHeroes.size())
  1243. {
  1244. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1245. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1246. blitAt(empty,posporx,pospory+i*32,to);
  1247. continue;
  1248. }
  1249. const CGHeroInstance *cur = LOCPLINT->wanderingHeroes[iT];
  1250. int pom = cur->movement / 100;
  1251. if (pom>25) pom=25;
  1252. if (pom<0) pom=0;
  1253. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1254. pom = cur->mana / 5;
  1255. if (pom>25) pom=25;
  1256. if (pom<0) pom=0;
  1257. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1258. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1259. blitAt(temp,posporx,pospory+i*32,to);
  1260. if (adventureInt->selection && (selected == iT) && (adventureInt->selection->ID == HEROI_TYPE))
  1261. {
  1262. blitAt(selection,posporx,pospory+i*32,to);
  1263. }
  1264. //TODO: support for custom portraits
  1265. }
  1266. if (from>0)
  1267. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1268. else
  1269. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1270. if (LOCPLINT->wanderingHeroes.size()-from > SIZE)
  1271. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1272. else
  1273. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1274. }
  1275. int CHeroList::getPosOfHero(const CGHeroInstance* h)
  1276. {
  1277. return vstd::findPos(LOCPLINT->wanderingHeroes, h, std::equal_to<const CGHeroInstance*>());
  1278. }
  1279. void CHeroList::show( SDL_Surface * to )
  1280. {
  1281. }
  1282. void CHeroList::showAll( SDL_Surface * to )
  1283. {
  1284. draw(to);
  1285. }
  1286. int CHeroList::size()
  1287. {
  1288. return LOCPLINT->wanderingHeroes.size();
  1289. }
  1290. CTownList::~CTownList()
  1291. {
  1292. delete arrup;
  1293. delete arrdo;
  1294. }
  1295. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1296. :CList(Size)
  1297. {
  1298. arrup = CDefHandler::giveDef(arrupg);
  1299. arrdo = CDefHandler::giveDef(arrdog);
  1300. pos.x += x;
  1301. pos.y += y;
  1302. pos.w = std::max(arrdo->width, arrup->width);
  1303. pos.h = arrdo->height + arrup->height + Size*32;
  1304. arrupp.x=pos.x;
  1305. arrupp.y=pos.y;
  1306. arrupp.w=arrup->width;
  1307. arrupp.h=arrup->height;
  1308. arrdop.x=pos.x;
  1309. arrdop.y=pos.y+arrup->height+32*SIZE;
  1310. arrdop.w=arrdo->width;
  1311. arrdop.h=arrdo->height;
  1312. posporx = arrdop.x;
  1313. pospory = arrupp.y + arrupp.h;
  1314. pressed = indeterminate;
  1315. from = 0;
  1316. selected = -1;
  1317. }
  1318. void CTownList::genList()
  1319. {
  1320. // LOCPLINT->towns.clear();
  1321. // int howMany = LOCPLINT->cb->howManyTowns();
  1322. // for (int i=0;i<howMany;i++)
  1323. // {
  1324. // LOCPLINT->towns.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1325. // }
  1326. }
  1327. void CTownList::select(int which)
  1328. {
  1329. if (which>=LOCPLINT->towns.size())
  1330. return;
  1331. selected = which;
  1332. fixPos();
  1333. if(!fun.empty())
  1334. fun();
  1335. }
  1336. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1337. {
  1338. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1339. {
  1340. if (from>0)
  1341. GH.statusbar->print(CGI->generaltexth->zelp[306].first);
  1342. else
  1343. GH.statusbar->clear();
  1344. return;
  1345. }
  1346. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1347. {
  1348. if ((LOCPLINT->towns.size()-from) > SIZE)
  1349. GH.statusbar->print(CGI->generaltexth->zelp[307].first);
  1350. else
  1351. GH.statusbar->clear();
  1352. return;
  1353. }
  1354. //if not buttons then towns
  1355. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1356. hx-=pos.x;
  1357. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1358. int ny = hy/32;
  1359. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1360. {
  1361. GH.statusbar->clear();
  1362. return;
  1363. };
  1364. std::string temp = CGI->generaltexth->tcommands[4];
  1365. boost::algorithm::replace_first(temp,"%s",LOCPLINT->towns[from+ny]->name);
  1366. temp += ", "+LOCPLINT->towns[from+ny]->town->Name();
  1367. GH.statusbar->print(temp);
  1368. }
  1369. void CTownList::clickLeft(tribool down, bool previousState)
  1370. {
  1371. if (down)
  1372. {
  1373. /***************************ARROWS*****************************************/
  1374. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1375. {
  1376. if(from>0)
  1377. {
  1378. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1379. pressed = true;
  1380. }
  1381. return;
  1382. }
  1383. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1384. {
  1385. if(LOCPLINT->towns.size()-from > SIZE)
  1386. {
  1387. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1388. pressed = false;
  1389. }
  1390. return;
  1391. }
  1392. /***************************TOWNS*****************************************/
  1393. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1394. hx-=pos.x;
  1395. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1396. int ny = hy/32;
  1397. if (ny>=SIZE || ny<0)
  1398. return;
  1399. if(GH.topInt() == adventureInt
  1400. && (ny+from)==selected
  1401. && adventureInt->selection->ID == TOWNI_TYPE
  1402. )
  1403. LOCPLINT->openTownWindow(LOCPLINT->towns[selected]);//print town screen
  1404. else
  1405. select(ny+from);
  1406. }
  1407. else
  1408. {
  1409. if (indeterminate(pressed))
  1410. return;
  1411. if (pressed) //up
  1412. {
  1413. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1414. pressed = indeterminate;
  1415. if (!down)
  1416. {
  1417. from--;
  1418. if (from<0)
  1419. from=0;
  1420. draw(screenBuf);
  1421. }
  1422. }
  1423. else if (!pressed) //down
  1424. {
  1425. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1426. pressed = indeterminate;
  1427. if (!down)
  1428. {
  1429. from++;
  1430. //if (from<LOCPLINT->towns.size()-5)
  1431. // from=LOCPLINT->towns.size()-5;
  1432. draw(screenBuf);
  1433. }
  1434. }
  1435. else
  1436. throw 0;
  1437. }
  1438. }
  1439. void CTownList::clickRight(tribool down, bool previousState)
  1440. {
  1441. if (down)
  1442. {
  1443. /***************************ARROWS*****************************************/
  1444. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1445. {
  1446. adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down);
  1447. }
  1448. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->towns.size()-from>5))
  1449. {
  1450. adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down);
  1451. }
  1452. //if not buttons then towns
  1453. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1454. hx-=pos.x;
  1455. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1456. int ny = hy/32;
  1457. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1458. {
  1459. return;
  1460. }
  1461. //show popup
  1462. CRClickPopup::createAndPush(LOCPLINT->towns[from+ny], GH.current->motion);
  1463. }
  1464. }
  1465. void CTownList::hover (bool on)
  1466. {
  1467. }
  1468. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1469. {
  1470. }
  1471. void CTownList::draw(SDL_Surface * to)
  1472. {
  1473. for (int iT=0+from;iT<SIZE+from;iT++)
  1474. {
  1475. int i = iT-from;
  1476. if (iT>=LOCPLINT->towns.size())
  1477. {
  1478. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1479. continue;
  1480. }
  1481. blitAt(graphics->getPic(LOCPLINT->towns[iT]->subID,LOCPLINT->towns[iT]->hasFort(),LOCPLINT->towns[iT]->builded),posporx,pospory+i*32,to);
  1482. if (adventureInt->selection && (selected == iT) && (adventureInt->selection->ID == TOWNI_TYPE))
  1483. {
  1484. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1485. }
  1486. }
  1487. if (from>0)
  1488. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1489. else
  1490. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1491. if (LOCPLINT->towns.size()-from>SIZE)
  1492. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1493. else
  1494. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1495. }
  1496. void CTownList::show( SDL_Surface * to )
  1497. {
  1498. }
  1499. void CTownList::showAll( SDL_Surface * to )
  1500. {
  1501. draw(to);
  1502. }
  1503. int CTownList::size()
  1504. {
  1505. return LOCPLINT->towns.size();
  1506. }
  1507. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  1508. {
  1509. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1510. pos.x+=x;
  1511. pos.y+=y;
  1512. if(Big)
  1513. bg = new CPicture(graphics->backgrounds[cre->faction],0,0,false);
  1514. else
  1515. bg = new CPicture(graphics->backgroundsm[cre->faction],0,0,false);
  1516. bg->needRefresh = true;
  1517. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  1518. anim->clipRect(cre->doubleWide?170:150, 155, bg->pos.w, bg->pos.h);
  1519. anim->startPreview();
  1520. }
  1521. CCreaturePic::~CCreaturePic()
  1522. {
  1523. }
  1524. void CRecruitmentWindow::close()
  1525. {
  1526. GH.popIntTotally(this);
  1527. }
  1528. void CRecruitmentWindow::Max()
  1529. {
  1530. slider->moveToMax();
  1531. }
  1532. void CRecruitmentWindow::Buy()
  1533. {
  1534. int crid = creatures[which].ID,
  1535. dstslot = dst-> getSlotFor(crid);
  1536. if(dstslot < 0 && !vstd::contains(CGI->arth->bigArtifacts,CGI->arth->convertMachineID(crid, true))) //no available slot
  1537. {
  1538. std::string txt;
  1539. if(dst->ID == HEROI_TYPE)
  1540. {
  1541. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1542. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  1543. }
  1544. else
  1545. {
  1546. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1547. }
  1548. LOCPLINT->showInfoDialog(txt);
  1549. return;
  1550. }
  1551. recruit(crid, slider->value);
  1552. if(level >= 0)
  1553. close();
  1554. else
  1555. slider->moveTo(0);
  1556. }
  1557. void CRecruitmentWindow::Cancel()
  1558. {
  1559. close();
  1560. }
  1561. void CRecruitmentWindow::sliderMoved(int to)
  1562. {
  1563. buy->block(!to);
  1564. redraw();
  1565. }
  1566. void CRecruitmentWindow::clickLeft(tribool down, bool previousState)
  1567. {
  1568. for(int i=0;i<creatures.size();i++)
  1569. {
  1570. Rect creaPos = pos + creatures[i].pos;
  1571. if(isItIn(&creaPos, GH.current->motion.x, GH.current->motion.y))
  1572. {
  1573. which = i;
  1574. int newAmount = std::min(amounts[i],creatures[i].amount);
  1575. slider->setAmount(newAmount);
  1576. slider->block(!newAmount);
  1577. max->block(!newAmount);
  1578. if(slider->value > newAmount)
  1579. slider->moveTo(newAmount);
  1580. else
  1581. slider->moveTo(slider->value);
  1582. redraw();
  1583. break;
  1584. }
  1585. }
  1586. }
  1587. void CRecruitmentWindow::clickRight(tribool down, bool previousState)
  1588. {
  1589. if(down)
  1590. {
  1591. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1592. for(int i=0;i<creatures.size();i++)
  1593. {
  1594. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1595. Rect creatureRect = genRect(132, sCREATURE_WIDTH, pos.x+curx, pos.y+64);
  1596. if(isItIn(&creatureRect, GH.current->motion.x, GH.current->motion.y))
  1597. {
  1598. CCreatureWindow *popup = new CCreatureWindow(creatures[i].ID, 0, 0);
  1599. GH.pushInt(popup);
  1600. break;
  1601. }
  1602. curx += TOTAL_CREATURE_WIDTH;
  1603. }
  1604. }
  1605. }
  1606. void CRecruitmentWindow::showAll( SDL_Surface * to )
  1607. {
  1608. CIntObject::showAll(to);
  1609. char pom[15];
  1610. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1611. printAtMiddleLoc(pom,205,253,FONT_SMALL,zwykly,to);
  1612. SDL_itoa(slider->value,pom,10); //recruit
  1613. printAtMiddleLoc(pom,279,253,FONT_SMALL,zwykly,to);
  1614. printAtMiddleLoc(CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creatures[which].ID]->namePl,243,32,FONT_BIG,tytulowy,to); //eg "Recruit Dragon flies"
  1615. int curx = 122-creatures[which].res.size()*24;
  1616. for(int i=creatures[which].res.size()-1; i>=0; i--)// decrement used to make gold displayed as first res
  1617. {
  1618. blitAtLoc(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,243,to);
  1619. blitAtLoc(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,243,to);
  1620. SDL_itoa(creatures[which].res[i].second,pom,10);
  1621. printAtMiddleLoc(pom,curx+15,287,FONT_SMALL,zwykly,to);
  1622. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1623. printAtMiddleLoc(pom,curx+15+258,287,FONT_SMALL,zwykly,to);
  1624. curx+=32+16;//size of bitmap + distance between them
  1625. }
  1626. for(int j=0;j<creatures.size();j++)
  1627. {
  1628. if(which==j)
  1629. drawBorder(*bitmap,creatures[j].pos,int3(255,0,0));
  1630. else
  1631. drawBorder(*bitmap,creatures[j].pos,int3(239,215,123));
  1632. }
  1633. }
  1634. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> &Recruit, int y_offset)
  1635. :recruit(Recruit), dwelling(Dwelling), level(Level), dst(Dst)
  1636. {
  1637. used = LCLICK | RCLICK;
  1638. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1639. which = 0;
  1640. bitmap = new CPicture("TPRCRT.bmp");
  1641. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  1642. bitmap->center();
  1643. pos = (bitmap->pos += Point(0, y_offset));
  1644. bar = new CGStatusBar(8, 370, "APHLFTRT.bmp", 471);
  1645. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),134,313,"IRCBTNS.DEF",SDLK_m);
  1646. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),212,313,"IBY6432.DEF",SDLK_RETURN);
  1647. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),290,313,"ICN6432.DEF",SDLK_ESCAPE);
  1648. slider = new CSlider(176,279,135,0,0,0,0,true);
  1649. slider->moved = boost::bind(&CRecruitmentWindow::sliderMoved,this, _1);
  1650. initCres();
  1651. printAtMiddle(CGI->generaltexth->allTexts[346],113,232,FONT_SMALL,zwykly,*bitmap); //cost per troop t
  1652. printAtMiddle(CGI->generaltexth->allTexts[465],205,233,FONT_SMALL,zwykly,*bitmap); //available t
  1653. printAtMiddle(CGI->generaltexth->allTexts[16],279,233,FONT_SMALL,zwykly,*bitmap); //recruit t
  1654. printAtMiddle(CGI->generaltexth->allTexts[466],371,232,FONT_SMALL,zwykly,*bitmap); //total cost t
  1655. drawBorder(*bitmap,172,222,67,42,int3(239,215,123));
  1656. drawBorder(*bitmap,246,222,67,42,int3(239,215,123));
  1657. drawBorder(*bitmap,64,222,99,76,int3(239,215,123));
  1658. drawBorder(*bitmap,322,222,99,76,int3(239,215,123));
  1659. drawBorder(*bitmap,133,312,66,34,int3(173,142,66));
  1660. drawBorder(*bitmap,211,312,66,34,int3(173,142,66));
  1661. drawBorder(*bitmap,289,312,66,34,int3(173,142,66));
  1662. //border for creatures
  1663. int curx = 192 + 50 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1664. for(int i=0;i<creatures.size();i++)
  1665. {
  1666. creatures[i].pos.x = curx-1;
  1667. creatures[i].pos.y = 65 - 1;
  1668. creatures[i].pos.w = 100 + 2;
  1669. creatures[i].pos.h = 130 + 2;
  1670. // if(which==i)
  1671. // drawBorder(*bitmap,curx-1,64,CREATURE_WIDTH,132,int3(255,0,0));
  1672. // else
  1673. // drawBorder(*bitmap,curx-1,64,CREATURE_WIDTH,132,int3(239,215,123));
  1674. creatures[i].pic = new CCreaturePic(curx, 65, CGI->creh->creatures[creatures[i].ID]);
  1675. curx += TOTAL_CREATURE_WIDTH;
  1676. }
  1677. if(!creatures[0].amount || !amounts[0])
  1678. {
  1679. max->block(true);
  1680. slider->block(true);
  1681. }
  1682. buy->block(true);
  1683. }
  1684. CRecruitmentWindow::~CRecruitmentWindow()
  1685. {
  1686. }
  1687. void CRecruitmentWindow::initCres()
  1688. {
  1689. creatures.clear();
  1690. amounts.clear();
  1691. for(int i=0; i<dwelling->creatures.size(); i++)
  1692. {
  1693. if(level >= 0 && i != level)
  1694. continue;
  1695. for(int j = dwelling->creatures[i].second.size() - 1; j >= 0 ; j--)
  1696. {
  1697. creatures.resize(creatures.size()+1);
  1698. creinfo &cur = creatures.back();
  1699. cur.amount = dwelling->creatures[i].first;
  1700. cur.ID = dwelling->creatures[i].second[j];
  1701. const CCreature * cre= CGI->creh->creatures[cur.ID];
  1702. for(int k=0; k<cre->cost.size(); k++)
  1703. if(cre->cost[k])
  1704. cur.res.push_back(std::make_pair(k,cre->cost[k]));
  1705. amounts.push_back(cre->maxAmount(LOCPLINT->cb->getResourceAmount()));
  1706. }
  1707. }
  1708. slider->setAmount(std::min(amounts[which],creatures[which].amount));
  1709. }
  1710. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1711. {
  1712. last = Last;
  1713. which = 1;
  1714. c=cid;
  1715. slider = NULL;
  1716. gar = Owner;
  1717. SDL_Surface *hhlp = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1718. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1719. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1720. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1721. SDL_FreeSurface(hhlp);
  1722. pos.x = screen->w/2 - bitmap->w/2;
  1723. pos.y = screen->h/2 - bitmap->h/2;
  1724. pos.w = bitmap->w;
  1725. pos.h = bitmap->h;
  1726. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1727. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1728. int sliderPositions = max - (last>=0) - (last==2);
  1729. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),0,sliderPositions,val,true);
  1730. a1 = max-val;
  1731. a2 = val;
  1732. animLeft = new CCreaturePic(pos.x+20, pos.y+54, CGI->creh->creatures[cid], true, false);
  1733. animRight = new CCreaturePic(pos.x+177, pos.y+54, CGI->creh->creatures[cid], true, false);
  1734. std::string title = CGI->generaltexth->allTexts[256];
  1735. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid]->namePl);
  1736. printAtMiddle(title,150,34,FONT_BIG,tytulowy,bitmap);
  1737. }
  1738. CSplitWindow::~CSplitWindow() //d-tor
  1739. {
  1740. SDL_FreeSurface(bitmap);
  1741. delete ok;
  1742. delete cancel;
  1743. delete slider;
  1744. delete animLeft;
  1745. delete animRight;
  1746. }
  1747. void CSplitWindow::activate()
  1748. {
  1749. activateLClick();
  1750. activateKeys();
  1751. ok->activate();
  1752. cancel->activate();
  1753. slider->activate();
  1754. }
  1755. void CSplitWindow::deactivate()
  1756. {
  1757. deactivateLClick();
  1758. deactivateKeys();
  1759. ok->deactivate();
  1760. cancel->deactivate();
  1761. slider->deactivate();
  1762. }
  1763. void CSplitWindow::split()
  1764. {
  1765. gar->splitStacks(a2);
  1766. close();
  1767. }
  1768. void CSplitWindow::close()
  1769. {
  1770. GH.popIntTotally(this);
  1771. }
  1772. void CSplitWindow::sliderMoved(int to)
  1773. {
  1774. int all = a1+a2;
  1775. a2 = to + (last==1 || last==2);
  1776. if(slider)
  1777. a1 = all - a2;
  1778. }
  1779. void CSplitWindow::show(SDL_Surface * to)
  1780. {
  1781. blitAt(bitmap,pos.x,pos.y,to);
  1782. ok->showAll(to);
  1783. cancel->showAll(to);
  1784. slider->showAll(to);
  1785. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,FONT_BIG,zwykly,to);
  1786. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,FONT_BIG,zwykly,to);
  1787. animLeft->show(to);
  1788. animRight->show(to);
  1789. }
  1790. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1791. {
  1792. SDLKey k = key.keysym.sym;
  1793. if (isNumKey(k)) //convert numpad number to normal digit
  1794. k = numToDigit(k);
  1795. if(key.state != SDL_PRESSED)
  1796. return;
  1797. int &cur = (which ? a2 : a1),
  1798. &sec = (which ? a1 : a2),
  1799. ncur = cur;
  1800. if (k == SDLK_BACKSPACE)
  1801. {
  1802. ncur /= 10;
  1803. }
  1804. else if(k == SDLK_TAB)
  1805. {
  1806. which = !which;
  1807. }
  1808. else if (k == SDLK_LEFT)
  1809. {
  1810. ncur--;
  1811. }
  1812. else if (k == SDLK_RIGHT)
  1813. {
  1814. ncur++;
  1815. }
  1816. else
  1817. {
  1818. int number = k - SDLK_0;
  1819. if (number < 0 || number > 9) //not a number pressed
  1820. {
  1821. return;
  1822. }
  1823. ncur = cur*10 + number;
  1824. }
  1825. int delta = ncur - cur;
  1826. if(delta > sec)
  1827. {
  1828. cur += sec;
  1829. sec = 0;
  1830. }
  1831. slider->moveTo(which ? ncur : a1+a2-ncur);
  1832. }
  1833. void CSplitWindow::clickLeft(tribool down, bool previousState)
  1834. {
  1835. if(down)
  1836. {
  1837. Point click(GH.current->motion.x,GH.current->motion.y);
  1838. click = click - pos.topLeft();
  1839. if(Rect(19,216,105,40).isIn(click)) //left picture
  1840. which = 0;
  1841. else if(Rect(175,216,105,40).isIn(click)) //right picture
  1842. which = 1;
  1843. }
  1844. }
  1845. void CCreInfoWindow::show(SDL_Surface * to)
  1846. {
  1847. blitAt(*bitmap,pos.x,pos.y,to);
  1848. anim->show(to);
  1849. if(count.size())
  1850. printTo(count.c_str(),pos.x+114,pos.y+174,FONT_TIMES,zwykly,to);
  1851. if(upgrade)
  1852. upgrade->showAll(to);
  1853. if(dismiss)
  1854. dismiss->showAll(to);
  1855. if(ok)
  1856. ok->showAll(to);
  1857. }
  1858. CCreInfoWindow::CCreInfoWindow(const CStackInstance &st, int Type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  1859. : type(Type), dsm(Dsm), dismiss(0), upgrade(0), ok(0)
  1860. {
  1861. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1862. init(st.type, &st, dynamic_cast<const CGHeroInstance*>(st.armyObj), st.count);
  1863. //print abilities text - if r-click popup
  1864. if(type)
  1865. {
  1866. if(Upg && ui)
  1867. {
  1868. bool enough = true;
  1869. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  1870. {
  1871. BLOCK_CAPTURING;
  1872. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*st.count)
  1873. enough = false;
  1874. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*st.count));
  1875. }
  1876. if(enough)
  1877. {
  1878. CFunctionList<void()> fs;
  1879. fs += Upg;
  1880. fs += boost::bind(&CCreInfoWindow::close,this);
  1881. CFunctionList<void()> cfl;
  1882. cfl = boost::bind(&CPlayerInterface::showYesNoDialog, LOCPLINT, CGI->generaltexth->allTexts[207], boost::ref(upgResCost), fs, 0, false);
  1883. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,76,237,"IVIEWCR.DEF",SDLK_u);
  1884. }
  1885. else
  1886. {
  1887. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),76,237,"IVIEWCR.DEF");
  1888. upgrade->callback.funcs.clear();
  1889. upgrade->setOffset(2);
  1890. }
  1891. }
  1892. if(Dsm)
  1893. {
  1894. CFunctionList<void()> fs[2];
  1895. //on dismiss confirmed
  1896. fs[0] += Dsm; //dismiss
  1897. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  1898. CFunctionList<void()> cfl;
  1899. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  1900. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,21,237,"IVIEWCR2.DEF",SDLK_d);
  1901. }
  1902. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),216,237,"IOKAY.DEF",SDLK_RETURN);
  1903. }
  1904. }
  1905. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount)
  1906. : type(Type), dismiss(0), upgrade(0), ok(0)
  1907. {
  1908. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1909. const CCreature *cre = CGI->creh->creatures[Cid];
  1910. init(cre, NULL, NULL, creatureCount);
  1911. }
  1912. CCreInfoWindow::CCreInfoWindow(const CStack &st, int Type /*= 0*/)
  1913. : type(Type), dismiss(0), upgrade(0), ok(0)
  1914. {
  1915. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1916. init(st.getCreature(), &st, st.getMyHero(), st.count);
  1917. }
  1918. void CCreInfoWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  1919. {
  1920. printAt(text, 155, 48 + nr*19, FONT_SMALL, zwykly, *bitmap);
  1921. std::string hlp;
  1922. if(range && baseVal != val)
  1923. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  1924. else if(baseVal != val && val>=0)
  1925. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  1926. else
  1927. hlp = boost::lexical_cast<std::string>(baseVal);
  1928. printTo(hlp, 276, 61 + nr*19, FONT_SMALL, zwykly, *bitmap);
  1929. }
  1930. //void CCreInfoWindow::init(const CCreature *cre, const CStackInstance *stack, int creatureCount)
  1931. void CCreInfoWindow::init(const CCreature *cre, const CBonusSystemNode *stackNode, const CGHeroInstance *heroOwner, int creatureCount)
  1932. {
  1933. c = cre;
  1934. if(!stackNode) stackNode = c;
  1935. bitmap = new CPicture("CRSTKPU.bmp");
  1936. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  1937. pos = bitmap->center();
  1938. anim = new CCreaturePic(21, 48, c);
  1939. count = boost::lexical_cast<std::string>(creatureCount);
  1940. printAtMiddle(c->namePl,149,30,FONT_SMALL,tytulowy,*bitmap); //creature name
  1941. printLine(0, CGI->generaltexth->primarySkillNames[0], cre->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK));
  1942. printLine(1, CGI->generaltexth->primarySkillNames[1], cre->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE));
  1943. //if(c->shots)
  1944. // printLine(2, CGI->generaltexth->allTexts[198], c->shots);
  1945. if(stackNode->valOfBonuses(Bonus::SHOTS))
  1946. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  1947. //TODO
  1948. int dmgMultiply = 1;
  1949. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  1950. dmgMultiply += heroOwner->Attack();
  1951. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  1952. printLine(4, CGI->generaltexth->allTexts[388], cre->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  1953. printLine(6, CGI->generaltexth->zelp[441].first, cre->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  1954. //setting morale
  1955. morale = new MoraleLuckBox(true, genRect(42, 42, 24, 189));
  1956. morale->set(stackNode);
  1957. //setting luck
  1958. luck = new MoraleLuckBox(false, genRect(42, 42, 77, 189));
  1959. luck->set(stackNode);
  1960. //luck and morale
  1961. int luck = 3, morale = 3;
  1962. if(stackNode)
  1963. {
  1964. //add modifiers
  1965. luck += stackNode->LuckVal();
  1966. morale += stackNode->MoraleVal();
  1967. }
  1968. blitAt(graphics->morale42->ourImages[morale].bitmap, 24, 189, *bitmap);
  1969. blitAt(graphics->luck42->ourImages[luck].bitmap, 77, 189, *bitmap);
  1970. if(!type)
  1971. {
  1972. printAtWB(c->abilityText,17,231,FONT_SMALL,35,zwykly,*bitmap);
  1973. }
  1974. //if we are displying window fo r stack in battle, there are several more things that we need to display
  1975. if(const CStack *battleStack = dynamic_cast<const CStack*>(stackNode))
  1976. {
  1977. //spell effects
  1978. int printed=0; //how many effect pics have been printed
  1979. std::vector<si32> spells = battleStack->activeSpells();
  1980. BOOST_FOREACH(si32 effect, spells)
  1981. {
  1982. blitAt(graphics->spellEffectsPics->ourImages[effect + 1].bitmap, 127 + 52 * printed, 186, *bitmap);
  1983. ++printed;
  1984. if(printed >= 3) //we can fit only 3 effects
  1985. break;
  1986. }
  1987. //print current health
  1988. printLine(5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  1989. }
  1990. }
  1991. CCreInfoWindow::~CCreInfoWindow()
  1992. {
  1993. for(int i=0; i<upgResCost.size();i++)
  1994. delete upgResCost[i];
  1995. }
  1996. void CCreInfoWindow::activate()
  1997. {
  1998. CIntObject::activate();
  1999. if(!type)
  2000. activateRClick();
  2001. }
  2002. void CCreInfoWindow::close()
  2003. {
  2004. GH.popIntTotally(this);
  2005. }
  2006. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  2007. {
  2008. if(down)
  2009. return;
  2010. close();
  2011. }
  2012. void CCreInfoWindow::dismissF()
  2013. {
  2014. dsm();
  2015. close();
  2016. }
  2017. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  2018. {
  2019. }
  2020. void CCreInfoWindow::deactivate()
  2021. {
  2022. if(!type)
  2023. deactivateRClick();
  2024. CIntObject::deactivate();
  2025. }
  2026. void CLevelWindow::close()
  2027. {
  2028. for(int i=0;i<comps.size();i++)
  2029. {
  2030. if(comps[i]->selected)
  2031. {
  2032. cb(i);
  2033. break;
  2034. }
  2035. }
  2036. GH.popIntTotally(this);
  2037. LOCPLINT->showingDialog->setn(false);
  2038. }
  2039. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  2040. {
  2041. LOCPLINT->showingDialog->setn(true);
  2042. heroPortrait = hero->portrait;
  2043. cb = callback;
  2044. for(int i=0;i<skills.size();i++)
  2045. {
  2046. comps.push_back(new CSelectableComponent(SComponent::secskill44, skills[i],
  2047. hero->getSecSkillLevel( static_cast<CGHeroInstance::SecondarySkill>(skills[i]) )+1,
  2048. boost::bind(&CLevelWindow::selectionChanged,this,i)));
  2049. comps.back()->assignedKeys.insert(SDLK_1 + i);
  2050. }
  2051. SDL_Surface *hhlp = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  2052. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2053. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  2054. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2055. SDL_FreeSurface(hhlp);
  2056. pos.x = screen->w/2 - bitmap->w/2;
  2057. pos.y = screen->h/2 - bitmap->h/2;
  2058. pos.w = bitmap->w;
  2059. pos.h = bitmap->h;
  2060. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  2061. //draw window
  2062. char buf[100], buf2[100];
  2063. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  2064. sprintf(buf,buf2,hero->name.c_str());
  2065. printAtMiddle(buf,192,33,FONT_MEDIUM,zwykly,bitmap);
  2066. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  2067. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  2068. printAtMiddle(buf,192,162,FONT_MEDIUM,zwykly,bitmap);
  2069. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  2070. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,253,FONT_MEDIUM,zwykly,bitmap);
  2071. const Font *f = graphics->fonts[FONT_MEDIUM];
  2072. std::string text = CGI->generaltexth->allTexts[4];
  2073. int fontWidth = f->getWidth(text.c_str())/2;
  2074. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+fontWidth) )/2;
  2075. for(int i=0;i<comps.size();i++)
  2076. {
  2077. comps[i]->pos.x = curx+pos.x;
  2078. comps[i]->pos.y = 326+pos.y;
  2079. if( i < (comps.size()-1) )
  2080. {
  2081. curx += 44+21; //skill width + margin to "or"
  2082. printAtMiddle(text ,curx,346,FONT_MEDIUM,zwykly,bitmap);
  2083. curx += fontWidth+15;
  2084. }
  2085. }
  2086. if(comps.size())
  2087. {
  2088. comps[0]->select(true);
  2089. }
  2090. }
  2091. void CLevelWindow::selectionChanged(unsigned to)
  2092. {
  2093. if(ok->isBlocked())
  2094. ok->block(false);
  2095. for(int i=0;i<comps.size();i++)
  2096. if(i==to)
  2097. comps[i]->select(true);
  2098. else
  2099. comps[i]->select(false);
  2100. }
  2101. CLevelWindow::~CLevelWindow()
  2102. {
  2103. delete ok;
  2104. for(int i=0;i<comps.size();i++)
  2105. delete comps[i];
  2106. SDL_FreeSurface(bitmap);
  2107. }
  2108. void CLevelWindow::activate()
  2109. {
  2110. ok->activate();
  2111. for(int i=0;i<comps.size();i++)
  2112. comps[i]->activate();
  2113. }
  2114. void CLevelWindow::deactivate()
  2115. {
  2116. ok->deactivate();
  2117. for(int i=0;i<comps.size();i++)
  2118. comps[i]->deactivate();
  2119. }
  2120. void CLevelWindow::show(SDL_Surface * to)
  2121. {
  2122. blitAt(bitmap,pos.x,pos.y,to);
  2123. blitAt(graphics->portraitLarge[heroPortrait],170+pos.x,66+pos.y,to);
  2124. ok->showAll(to);
  2125. for(int i=0;i<comps.size();i++)
  2126. comps[i]->show(to);
  2127. }
  2128. void CMinorResDataBar::show(SDL_Surface * to)
  2129. {
  2130. blitAt(bg,pos.x,pos.y,to);
  2131. char buf[30];
  2132. for (int i=0;i<7;i++)
  2133. {
  2134. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2135. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2136. }
  2137. std::vector<std::string> temp;
  2138. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  2139. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  2140. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  2141. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  2142. CGI->generaltexth->allTexts[62]
  2143. +": %s, "
  2144. + CGI->generaltexth->allTexts[63]
  2145. + ": %s, "
  2146. + CGI->generaltexth->allTexts[64]
  2147. + ": %s",temp)
  2148. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2149. }
  2150. void CMinorResDataBar::showAll(SDL_Surface * to)
  2151. {
  2152. show(to);
  2153. }
  2154. CMinorResDataBar::CMinorResDataBar()
  2155. {
  2156. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  2157. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2158. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2159. pos.x = 7;
  2160. pos.y = 575;
  2161. pos.w = bg->w;
  2162. pos.h = bg->h;
  2163. }
  2164. CMinorResDataBar::~CMinorResDataBar()
  2165. {
  2166. SDL_FreeSurface(bg);
  2167. }
  2168. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  2169. boost::function<void(int)> Callback, int initState)
  2170. :title(_title), descr(_descr),items(_items),selected(initState)
  2171. {
  2172. init = false;
  2173. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2174. defActions = ACTIVATE | DEACTIVATE | UPDATE | SHOWALL | DISPOSE | SHARE_POS;
  2175. used = LCLICK | KEYBOARD;
  2176. onSelect = Callback;
  2177. length = 9;
  2178. pos.x = (screen->w-306)/2;
  2179. pos.y = (screen->h-468)/2;
  2180. bg = new CPicture("TPGATE.pcx");//x=0, y=0
  2181. bg->colorizeAndConvert(LOCPLINT->playerID);
  2182. slider = new CSlider(277, 120, 256, boost::bind(&CObjectListWindow::moveList,this, _1), length, items.size(), 0, false, 0);
  2183. ok = new AdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  2184. exit = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  2185. pos.w = bg->pos.w;
  2186. pos.h = bg->pos.h;
  2187. titleImage = titlePic;
  2188. titleImage->pos.x =153+pos.x-titleImage->pos.w/2;
  2189. titleImage->pos.y =75 +pos.y-titleImage->pos.h/2;
  2190. for (int i=0; i<length; i++)
  2191. areas.push_back(genRect(22, 260, pos.x+15, pos.y+152+i*25 ));//rects for selecting\printing items
  2192. init = true;
  2193. }
  2194. CObjectListWindow::~CObjectListWindow()
  2195. {
  2196. delete titleImage;
  2197. }
  2198. void CObjectListWindow::elementSelected()
  2199. {
  2200. boost::function<void(int)> toCall = onSelect;//save
  2201. int where = items[selected]; //required variables
  2202. GH.popIntTotally(this);//then destroy window
  2203. toCall(where);//and send selected object
  2204. }
  2205. void CObjectListWindow::moveList(int which)
  2206. {
  2207. if (init)//TODO: is there a way to disable running this when CSlider is created?
  2208. showAll(screen2);
  2209. }
  2210. void CObjectListWindow::clickLeft(tribool down, bool previousState)
  2211. {
  2212. if (previousState && (!down))
  2213. {
  2214. for (int i=0; i<areas.size(); i++)
  2215. if(slider->value+i < items.size() && isItIn(&areas[i],GH.current->motion.x,GH.current->motion.y))
  2216. {//check all areas to find element which was clicked
  2217. selected = i+slider->value;
  2218. showAll(screen2);
  2219. return;
  2220. }
  2221. }
  2222. }
  2223. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  2224. {
  2225. if(key.state != SDL_PRESSED) return;
  2226. int sel = selected;
  2227. switch(key.keysym.sym)
  2228. {
  2229. case SDLK_UP:
  2230. sel -=1;
  2231. break;
  2232. case SDLK_DOWN:
  2233. sel +=1;
  2234. break;
  2235. case SDLK_PAGEUP:
  2236. sel -=length;
  2237. break;
  2238. case SDLK_PAGEDOWN:
  2239. sel +=length;
  2240. break;
  2241. case SDLK_HOME:
  2242. sel = 0;
  2243. break;
  2244. case SDLK_END:
  2245. sel = slider->amount;
  2246. break;
  2247. default:
  2248. return;
  2249. }
  2250. if (sel<-1)//nothing was selected & list was moved up
  2251. return;
  2252. if (sel<0)//start of list reached
  2253. sel = 0;
  2254. if ( sel >= slider->amount )//end of list reached
  2255. sel = slider->amount-1;
  2256. if ( sel >= items.size() )
  2257. sel = items.size()-1;
  2258. if ( sel < slider->value )//need to move list up
  2259. slider->moveTo(sel);
  2260. else
  2261. if ( sel >= slider->value+length )//move to bottom
  2262. slider->moveTo(sel-length+1);
  2263. selected = sel;
  2264. showAll(screen2);
  2265. }
  2266. void CObjectListWindow::show(SDL_Surface * to)
  2267. {
  2268. }
  2269. void CObjectListWindow::showAll(SDL_Surface * to)
  2270. {
  2271. ok->block((selected<0)?2:0);
  2272. CIntObject::showAll(to);
  2273. CSDL_Ext::printAtMiddle(title,pos.x+152,pos.y+27,FONT_BIG,tytulowy,to);//"castle gate"
  2274. CSDL_Ext::printAtMiddle(descr,pos.x+145,pos.y+133,FONT_SMALL,zwykly,to);//"select destination"
  2275. titleImage->showAll(to);
  2276. if ( selected >= slider->value && selected < slider->value+length )//if selected item is visible
  2277. {
  2278. SDL_Rect a = areas[selected-slider->value];
  2279. CSDL_Ext::drawBorder(to, a.x, a.y, a.w, a.h, int3(255, 231, 148));
  2280. CSDL_Ext::drawBorder(to, a.x-1, a.y-1, a.w+2, a.h+2, int3(255, 231, 148));//border shoul be 2 pixels width
  2281. }
  2282. int position = slider->value;
  2283. for ( int i = 0; i<9 && i<items.size()-position; i++)
  2284. CSDL_Ext::printAtMiddle(CGI->mh->map->objects[items[i+position]]->hoverName,pos.x+145,pos.y+163+25*i,
  2285. FONT_SMALL, zwykly, to);//print item names in list
  2286. }
  2287. CTradeWindow::CTradeableItem::CTradeableItem( EType Type, int ID, bool Left, int Serial)
  2288. {
  2289. serial = Serial;
  2290. left = Left;
  2291. type = Type;
  2292. id = ID;
  2293. used = LCLICK | HOVER | RCLICK;
  2294. downSelection = false;
  2295. hlp = NULL;
  2296. }
  2297. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  2298. {
  2299. Point posToBitmap;
  2300. Point posToSubCenter;
  2301. switch(type)
  2302. {
  2303. case RESOURCE:
  2304. posToBitmap = Point(19,9);
  2305. posToSubCenter = Point(36, 59);
  2306. break;
  2307. case CREATURE_PLACEHOLDER:
  2308. case CREATURE:
  2309. posToSubCenter = Point(29, 76);
  2310. if(downSelection)
  2311. posToSubCenter.y += 5;
  2312. break;
  2313. case PLAYER:
  2314. posToSubCenter = Point(31, 76);
  2315. break;
  2316. case ARTIFACT_PLACEHOLDER:
  2317. posToSubCenter = Point(19, 55);
  2318. if(downSelection)
  2319. posToSubCenter.y += 8;
  2320. break;
  2321. case ARTIFACT_TYPE:
  2322. posToSubCenter = Point(19, 58);
  2323. break;
  2324. }
  2325. if(SDL_Surface *hlp = getSurface())
  2326. blitAt(hlp, pos + posToBitmap, to);
  2327. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, zwykly, to);
  2328. }
  2329. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  2330. {
  2331. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  2332. assert(mw);
  2333. if(down)
  2334. {
  2335. if(type == ARTIFACT_PLACEHOLDER)
  2336. {
  2337. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  2338. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  2339. {
  2340. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this, movedArt);
  2341. }
  2342. else if(const CArtifactInstance *art = getArtInstance())
  2343. {
  2344. aw->arts->commonInfo->src.AOH = aw->arts;
  2345. aw->arts->commonInfo->src.art = art;
  2346. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  2347. aw->arts->markPossibleSlots(art);
  2348. //aw->arts->commonInfo->dst.AOH = aw->arts;
  2349. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[art->artType->id].bitmap);
  2350. aw->arts->artifactsOnAltar.erase(art);
  2351. id = -1;
  2352. subtitle = "";
  2353. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  2354. }
  2355. aw->calcTotalExp();
  2356. return;
  2357. }
  2358. if(left)
  2359. {
  2360. if(mw->hLeft != this)
  2361. mw->hLeft = this;
  2362. else
  2363. return;
  2364. }
  2365. else
  2366. {
  2367. if(mw->hRight != this)
  2368. mw->hRight = this;
  2369. else
  2370. return;
  2371. }
  2372. mw->selectionChanged(left);
  2373. }
  2374. }
  2375. SDL_Surface * CTradeWindow::CTradeableItem::getSurface()
  2376. {
  2377. switch(type)
  2378. {
  2379. case RESOURCE:
  2380. return graphics->resources32->ourImages[id].bitmap;
  2381. case PLAYER:
  2382. return graphics->flags->ourImages[id].bitmap;
  2383. case ARTIFACT_TYPE:
  2384. case ARTIFACT_PLACEHOLDER:
  2385. return id >= 0 ? graphics->artDefs->ourImages[id].bitmap : NULL;
  2386. case CREATURE:
  2387. return graphics->bigImgs[id];
  2388. default:
  2389. return NULL;
  2390. }
  2391. }
  2392. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  2393. {
  2394. Rect oldPos = pos;
  2395. std::string oldSub = subtitle;
  2396. downSelection = true;
  2397. pos = dstPos;
  2398. subtitle = customSub;
  2399. showAll(to);
  2400. downSelection = false;
  2401. pos = oldPos;
  2402. subtitle = oldSub;
  2403. }
  2404. void CTradeWindow::CTradeableItem::hover(bool on)
  2405. {
  2406. if(!on)
  2407. {
  2408. GH.statusbar->clear();
  2409. return;
  2410. }
  2411. switch(type)
  2412. {
  2413. case CREATURE:
  2414. case CREATURE_PLACEHOLDER:
  2415. GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  2416. break;
  2417. case ARTIFACT_PLACEHOLDER:
  2418. if(id < 0)
  2419. GH.statusbar->print(CGI->generaltexth->zelp[582].first);
  2420. else
  2421. GH.statusbar->print(CGI->arth->artifacts[id]->Name());
  2422. break;
  2423. }
  2424. }
  2425. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  2426. {
  2427. if(down)
  2428. {
  2429. switch(type)
  2430. {
  2431. case CREATURE:
  2432. case CREATURE_PLACEHOLDER:
  2433. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  2434. break;
  2435. case ARTIFACT_TYPE:
  2436. case ARTIFACT_PLACEHOLDER:
  2437. if(id >= 0)
  2438. adventureInt->handleRightClick(CGI->arth->artifacts[id]->Description(), down);
  2439. break;
  2440. }
  2441. }
  2442. }
  2443. std::string CTradeWindow::CTradeableItem::getName(int number /*= -1*/) const
  2444. {
  2445. switch(type)
  2446. {
  2447. case PLAYER:
  2448. return CGI->generaltexth->capColors[id];
  2449. case RESOURCE:
  2450. return CGI->generaltexth->restypes[id];
  2451. case CREATURE:
  2452. if(number == 1)
  2453. return CGI->creh->creatures[id]->nameSing;
  2454. else
  2455. return CGI->creh->creatures[id]->namePl;
  2456. case ARTIFACT_TYPE:
  2457. return CGI->arth->artifacts[id]->Name();
  2458. }
  2459. assert(0);
  2460. return "";
  2461. }
  2462. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  2463. {
  2464. switch(type)
  2465. {
  2466. case ARTIFACT_PLACEHOLDER:
  2467. case ARTIFACT_INSTANCE:
  2468. return (const CArtifactInstance *)hlp;
  2469. default:
  2470. return NULL;
  2471. }
  2472. }
  2473. // const CArtifact * CTradeWindow::CTradeableItem::getArt() const
  2474. // {
  2475. // return NULL;
  2476. // }
  2477. //
  2478. // void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art) const
  2479. // {
  2480. //
  2481. // }
  2482. //
  2483. // void CTradeWindow::CTradeableItem::setArt(const CArtifact *artT) const
  2484. // {
  2485. //
  2486. // }
  2487. CTradeWindow::CTradeWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode Mode)
  2488. : market(Market), hero(Hero), arts(NULL), hLeft(NULL), hRight(NULL), readyToTrade(false)
  2489. {
  2490. type |= BLOCK_ADV_HOTKEYS;
  2491. mode = Mode;
  2492. initTypes();
  2493. }
  2494. void CTradeWindow::initTypes()
  2495. {
  2496. switch(mode)
  2497. {
  2498. case RESOURCE_RESOURCE:
  2499. itemsType[1] = RESOURCE;
  2500. itemsType[0] = RESOURCE;
  2501. break;
  2502. case RESOURCE_PLAYER:
  2503. itemsType[1] = RESOURCE;
  2504. itemsType[0] = PLAYER;
  2505. break;
  2506. case CREATURE_RESOURCE:
  2507. itemsType[1] = CREATURE;
  2508. itemsType[0] = RESOURCE;
  2509. break;
  2510. case RESOURCE_ARTIFACT:
  2511. itemsType[1] = RESOURCE;
  2512. itemsType[0] = ARTIFACT_TYPE;
  2513. break;
  2514. case CREATURE_EXP:
  2515. itemsType[1] = CREATURE;
  2516. itemsType[0] = CREATURE_PLACEHOLDER;
  2517. break;
  2518. case ARTIFACT_EXP:
  2519. itemsType[1] = ARTIFACT_TYPE;
  2520. itemsType[0] = ARTIFACT_PLACEHOLDER;
  2521. break;
  2522. }
  2523. }
  2524. void CTradeWindow::initItems(bool Left)
  2525. {
  2526. std::vector<int> *ids = getItemsIds(Left);
  2527. std::vector<Rect> pos;
  2528. int amount = -1;
  2529. getPositionsFor(pos, Left, itemsType[Left]);
  2530. if(Left || !ids)
  2531. amount = 7;
  2532. else
  2533. amount = ids->size();
  2534. if(ids)
  2535. amin(amount, ids->size());
  2536. for(int j=0; j<amount; j++)
  2537. {
  2538. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  2539. if(id < 0 && mode != ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  2540. continue;
  2541. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], id, Left, j);
  2542. hlp->pos = pos[j] + hlp->pos;
  2543. items[Left].push_back(hlp);
  2544. }
  2545. initSubs(Left);
  2546. }
  2547. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  2548. {
  2549. std::vector<int> *ids = NULL;
  2550. if(mode == ARTIFACT_EXP)
  2551. return new std::vector<int>(22, -1);
  2552. if(Left)
  2553. {
  2554. switch(itemsType[1])
  2555. {
  2556. case CREATURE:
  2557. ids = new std::vector<int>;
  2558. for(int i = 0; i < 7; i++)
  2559. {
  2560. if(const CCreature *c = hero->getCreature(i))
  2561. ids->push_back(c->idNumber);
  2562. else
  2563. ids->push_back(-1);
  2564. }
  2565. break;
  2566. }
  2567. }
  2568. else
  2569. {
  2570. switch(itemsType[0])
  2571. {
  2572. case PLAYER:
  2573. ids = new std::vector<int>;
  2574. for(int i = 0; i < PLAYER_LIMIT; i++)
  2575. if(i != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  2576. ids->push_back(i);
  2577. break;
  2578. case ARTIFACT_TYPE:
  2579. ids = new std::vector<int>(market->availableItemsIds(mode));
  2580. break;
  2581. }
  2582. }
  2583. return ids;
  2584. }
  2585. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  2586. {
  2587. if(mode == ARTIFACT_EXP && !Left)
  2588. {
  2589. //22 boxes, 5 in row, last row: two boxes centered
  2590. int h, w, x, y, dx, dy;
  2591. h = w = 44;
  2592. x = 317;
  2593. y = 53;
  2594. dx = 54;
  2595. dy = 70;
  2596. for (int i = 0; i < 4 ; i++)
  2597. for (int j = 0; j < 5 ; j++)
  2598. poss += Rect(x + dx*j, y + dy*i, w, h);
  2599. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  2600. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  2601. }
  2602. else
  2603. {
  2604. //seven boxes:
  2605. // X X X
  2606. // X X X
  2607. // X
  2608. int h, w, x, y, dx, dy;
  2609. int leftToRightOffset;
  2610. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  2611. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  2612. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  2613. genRect(h, w, x + dx, y + 2*dy);
  2614. if(!Left)
  2615. {
  2616. BOOST_FOREACH(Rect &r, poss)
  2617. r.x += leftToRightOffset;
  2618. }
  2619. }
  2620. }
  2621. void CTradeWindow::initSubs(bool Left)
  2622. {
  2623. BOOST_FOREACH(CTradeableItem *t, items[Left])
  2624. {
  2625. if(Left)
  2626. {
  2627. switch(itemsType[1])
  2628. {
  2629. case CREATURE:
  2630. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(t->serial));
  2631. break;
  2632. case RESOURCE:
  2633. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(t->serial));
  2634. break;
  2635. }
  2636. }
  2637. else //right side
  2638. {
  2639. if(itemsType[0] == PLAYER)
  2640. {
  2641. t->subtitle = CGI->generaltexth->capColors[t->id];
  2642. }
  2643. else if(hLeft)//artifact, creature
  2644. {
  2645. int h1, h2; //hlp variables for getting offer
  2646. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  2647. if(t->id != hLeft->id || mode != RESOURCE_RESOURCE) //don't allow exchanging same resources
  2648. {
  2649. std::ostringstream oss;
  2650. oss << h2;
  2651. if(h1!=1)
  2652. oss << "/" << h1;
  2653. t->subtitle = oss.str();
  2654. }
  2655. else
  2656. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  2657. }
  2658. else
  2659. t->subtitle = "";
  2660. }
  2661. }
  2662. }
  2663. void CTradeWindow::showAll(SDL_Surface * to)
  2664. {
  2665. CIntObject::showAll(to);
  2666. if(hRight)
  2667. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2668. if(hLeft)
  2669. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2670. if(readyToTrade)
  2671. {
  2672. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  2673. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  2674. }
  2675. }
  2676. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  2677. {
  2678. BOOST_FOREACH(CTradeableItem *t, toRemove)
  2679. removeItem(t);
  2680. }
  2681. void CTradeWindow::removeItem(CTradeableItem * t)
  2682. {
  2683. if(active)
  2684. t->deactivate();
  2685. items[t->left] -= t;
  2686. delChild(t);
  2687. if(hRight == t)
  2688. {
  2689. hRight = NULL;
  2690. selectionChanged(false);
  2691. }
  2692. }
  2693. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  2694. {
  2695. BOOST_FOREACH(CTradeableItem *t, items[1])
  2696. if(!hero->getStackCount(t->serial))
  2697. toRemove.insert(t);
  2698. }
  2699. void CTradeWindow::setMode(EMarketMode Mode)
  2700. {
  2701. const IMarket *m = market;
  2702. const CGHeroInstance *h = hero;
  2703. CTradeWindow *nwindow = NULL;
  2704. GH.popIntTotally(this);
  2705. switch(Mode)
  2706. {
  2707. case CREATURE_EXP:
  2708. case ARTIFACT_EXP:
  2709. nwindow = new CAltarWindow(m, h, Mode);
  2710. break;
  2711. default:
  2712. nwindow = new CMarketplaceWindow(m, h, Mode);
  2713. break;
  2714. }
  2715. GH.pushInt(nwindow);
  2716. }
  2717. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode Mode)
  2718. : CTradeWindow(Market, Hero, Mode)
  2719. {
  2720. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2721. madeTransaction = false;
  2722. std::string bgName;
  2723. bool sliderNeeded = true;
  2724. switch(Mode)
  2725. {
  2726. case RESOURCE_RESOURCE:
  2727. bgName = "TPMRKRES.bmp";
  2728. break;
  2729. case RESOURCE_PLAYER:
  2730. bgName = "TPMRKPTS.bmp";
  2731. break;
  2732. case CREATURE_RESOURCE:
  2733. bgName = "TPMRKCRS.bmp";
  2734. break;
  2735. case RESOURCE_ARTIFACT:
  2736. bgName = "TPMRKABS.bmp";
  2737. sliderNeeded = false;
  2738. break;
  2739. case ARTIFACT_RESOURCE:
  2740. bgName = "TPMRKASS.bmp";
  2741. break;
  2742. }
  2743. bg = new CPicture(bgName);
  2744. bg->colorizeAndConvert(LOCPLINT->playerID);
  2745. pos = bg->center();
  2746. new CGStatusBar(302, 576);
  2747. if(market->o->ID == 7) //black market
  2748. {
  2749. printAtMiddle(CGI->generaltexth->allTexts[349],300,27,FONT_BIG,tytulowy,*bg); //title
  2750. }
  2751. else if(market->o->ID == 99 || market->o->ID == 221) //trading post
  2752. {
  2753. printAtMiddle(CGI->generaltexth->allTexts[159],300,27,FONT_BIG,tytulowy,*bg); //title
  2754. }
  2755. else if(mode == CREATURE_RESOURCE)
  2756. {
  2757. if(market->o->ID == TOWNI_TYPE)
  2758. printAtMiddle(CGI->buildh->buildings[6][21]->Name(), 300, 27, FONT_BIG, tytulowy, *bg); //title
  2759. else
  2760. printAtMiddle(market->o->getHoverText(), 300, 27, FONT_BIG, tytulowy, *bg); //title
  2761. }
  2762. else if(mode == RESOURCE_ARTIFACT || mode == ARTIFACT_RESOURCE)
  2763. {
  2764. const std::string &title = market->o->ID == TOWNI_TYPE
  2765. ? CGI->buildh->buildings[market->o->subID][17]->Name()
  2766. : market->o->getHoverText();
  2767. printAtMiddle(title, 300, 27, FONT_BIG, tytulowy, *bg); //title
  2768. }
  2769. else
  2770. {
  2771. printAtMiddle(CGI->generaltexth->allTexts[158],300,27,FONT_BIG,tytulowy,*bg); //marketplace
  2772. }
  2773. initItems(false);
  2774. initItems(true);
  2775. ok = new AdventureMapButton(CGI->generaltexth->zelp[600],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2776. ok->assignedKeys.insert(SDLK_ESCAPE);
  2777. deal = new AdventureMapButton(CGI->generaltexth->zelp[595],boost::bind(&CMarketplaceWindow::makeDeal,this),307,520,"TPMRKB.DEF");
  2778. deal->block(true);
  2779. //slider and buttons must be created after bg
  2780. if(sliderNeeded)
  2781. {
  2782. slider = new CSlider(231,490,137,0,0,0);
  2783. slider->moved = boost::bind(&CMarketplaceWindow::sliderMoved,this,_1);
  2784. max = new AdventureMapButton(CGI->generaltexth->zelp[596],boost::bind(&CMarketplaceWindow::setMax,this),229,520,"IRCBTNS.DEF");
  2785. max->block(true);
  2786. }
  2787. else
  2788. {
  2789. slider = NULL;
  2790. max = NULL;
  2791. deal->pos.x -= 38;
  2792. }
  2793. Rect traderTextRect;
  2794. //left side
  2795. switch(Mode)
  2796. {
  2797. case RESOURCE_RESOURCE:
  2798. case RESOURCE_PLAYER:
  2799. case RESOURCE_ARTIFACT:
  2800. printAtMiddle(CGI->generaltexth->allTexts[270],154,148,FONT_SMALL,zwykly,*bg); //kingdom res.
  2801. break;
  2802. case CREATURE_RESOURCE:
  2803. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name), 152, 102, FONT_SMALL, zwykly, *bg); //%s's Creatures
  2804. break;
  2805. }
  2806. //right side
  2807. switch(Mode)
  2808. {
  2809. case RESOURCE_RESOURCE:
  2810. case CREATURE_RESOURCE:
  2811. case RESOURCE_ARTIFACT:
  2812. printAtMiddle(CGI->generaltexth->allTexts[168],445,148,FONT_SMALL,zwykly,*bg); //available for trade
  2813. traderTextRect = Rect(316, 48, 260, 75);
  2814. break;
  2815. case RESOURCE_PLAYER:
  2816. printAtMiddle(CGI->generaltexth->allTexts[169],445,55,FONT_SMALL,zwykly,*bg); //players
  2817. traderTextRect = Rect(28, 48, 260, 75);
  2818. break;
  2819. }
  2820. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  2821. if(printButtonFor(RESOURCE_PLAYER))
  2822. new AdventureMapButton(CGI->generaltexth->zelp[612],boost::bind(&CMarketplaceWindow::setMode,this, RESOURCE_PLAYER), 18, 520,"TPMRKBU1.DEF");
  2823. if(printButtonFor(RESOURCE_RESOURCE))
  2824. new AdventureMapButton(CGI->generaltexth->zelp[605],boost::bind(&CMarketplaceWindow::setMode,this, RESOURCE_RESOURCE), 516, 450,"TPMRKBU5.DEF");
  2825. if(printButtonFor(CREATURE_RESOURCE))
  2826. new AdventureMapButton(CGI->generaltexth->zelp[599],boost::bind(&CMarketplaceWindow::setMode,this, CREATURE_RESOURCE), 516, 485,"TPMRKBU4.DEF"); //was y=450, changed to not overlap res-res in some conditions
  2827. if(printButtonFor(RESOURCE_ARTIFACT))
  2828. new AdventureMapButton(CGI->generaltexth->zelp[598],boost::bind(&CMarketplaceWindow::setMode,this, RESOURCE_ARTIFACT), 18, 450,"TPMRKBU2.DEF");
  2829. if(printButtonFor(ARTIFACT_RESOURCE)) //unblock when support for art-res is ready
  2830. (new AdventureMapButton(CGI->generaltexth->zelp[613],boost::bind(&CMarketplaceWindow::setMode,this, ARTIFACT_RESOURCE), 18, 485,"TPMRKBU3.DEF"))->block(true); //was y=450, changed to not overlap res-art in some conditions
  2831. updateTraderText();
  2832. }
  2833. CMarketplaceWindow::~CMarketplaceWindow()
  2834. {
  2835. hLeft = hRight = NULL;
  2836. for(int i=0;i<items[1].size();i++)
  2837. delChild(items[1][i]);
  2838. for(int i=0;i<items[0].size();i++)
  2839. delChild(items[0][i]);
  2840. items[1].clear();
  2841. items[0].clear();
  2842. delChild(bg);
  2843. bg = NULL;
  2844. }
  2845. void CMarketplaceWindow::setMax()
  2846. {
  2847. slider->moveToMax();
  2848. }
  2849. void CMarketplaceWindow::makeDeal()
  2850. {
  2851. int sliderValue = 0;
  2852. if(slider)
  2853. sliderValue = slider->value;
  2854. else
  2855. sliderValue = !deal->isBlocked(); //should always be 1
  2856. if(!sliderValue)
  2857. return;
  2858. int leftIdToSend = -1;
  2859. if(mode == CREATURE_RESOURCE)
  2860. leftIdToSend = hLeft->serial;
  2861. else
  2862. leftIdToSend = hLeft->id;
  2863. if(mode != RESOURCE_ARTIFACT)
  2864. {
  2865. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->value*r1, hero);
  2866. slider->moveTo(0);
  2867. }
  2868. else
  2869. {
  2870. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  2871. }
  2872. madeTransaction = true;
  2873. hLeft = NULL;
  2874. hRight = NULL;
  2875. selectionChanged(true);
  2876. }
  2877. void CMarketplaceWindow::sliderMoved( int to )
  2878. {
  2879. redraw();
  2880. }
  2881. void CMarketplaceWindow::selectionChanged(bool side)
  2882. {
  2883. readyToTrade = (hLeft && hRight && (hLeft->id!= hRight->id || mode != RESOURCE_RESOURCE));
  2884. if(readyToTrade)
  2885. {
  2886. int newAmount = -1;
  2887. market->getOffer(hLeft->id, hRight->id, r1, r2, mode);
  2888. if(itemsType[1] == RESOURCE)
  2889. newAmount = LOCPLINT->cb->getResourceAmount(hLeft->id);
  2890. else if(itemsType[1] == CREATURE)
  2891. newAmount = hero->getStackCount(hLeft->serial) - (hero->Slots().size() == 1 && hero->needsLastStack());
  2892. else
  2893. assert(0);
  2894. if(slider)
  2895. {
  2896. slider->setAmount(newAmount / r1);
  2897. slider->moveTo(0);
  2898. max->block(false);
  2899. deal->block(false);
  2900. }
  2901. else
  2902. {
  2903. deal->block(LOCPLINT->cb->getResourceAmount(hLeft->id) < r1);
  2904. }
  2905. }
  2906. else
  2907. {
  2908. if(slider)
  2909. {
  2910. max->block(true);
  2911. slider->setAmount(0);
  2912. slider->moveTo(0);
  2913. }
  2914. deal->block(true);
  2915. }
  2916. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  2917. initSubs(false);
  2918. updateTraderText();
  2919. redraw();
  2920. }
  2921. bool CMarketplaceWindow::printButtonFor(EMarketMode M) const
  2922. {
  2923. return market->allowsTrade(M) && M != mode && (hero || ( M != CREATURE_RESOURCE && M != RESOURCE_ARTIFACT && M != ARTIFACT_RESOURCE ));
  2924. }
  2925. void CMarketplaceWindow::garrisonChanged()
  2926. {
  2927. if(mode != CREATURE_RESOURCE)
  2928. return;
  2929. std::set<CTradeableItem *> toRemove;
  2930. getEmptySlots(toRemove);
  2931. removeItems(toRemove);
  2932. initSubs(true);
  2933. }
  2934. void CMarketplaceWindow::artifactsChanged(bool Left)
  2935. {
  2936. assert(!Left);
  2937. if(mode != RESOURCE_ARTIFACT)
  2938. return;
  2939. std::vector<int> available = market->availableItemsIds(mode);
  2940. std::set<CTradeableItem *> toRemove;
  2941. BOOST_FOREACH(CTradeableItem *t, items[0])
  2942. if(!vstd::contains(available, t->id))
  2943. toRemove.insert(t);
  2944. removeItems(toRemove);
  2945. redraw();
  2946. }
  2947. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  2948. {
  2949. if(Left)
  2950. {
  2951. assert(itemsType[1] == CREATURE || itemsType[1] == RESOURCE);
  2952. int val = slider
  2953. ? slider->value * r1
  2954. : (((deal->isBlocked())) ? 0 : r1);
  2955. return boost::lexical_cast<std::string>(val);
  2956. }
  2957. else
  2958. {
  2959. switch(itemsType[0])
  2960. {
  2961. case RESOURCE:
  2962. return boost::lexical_cast<std::string>( slider->value * r2 );
  2963. case ARTIFACT_TYPE:
  2964. return ((deal->isBlocked()) ? "0" : "1");
  2965. case PLAYER:
  2966. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  2967. }
  2968. }
  2969. return "???";
  2970. }
  2971. Point CMarketplaceWindow::selectionOffset(bool Left) const
  2972. {
  2973. if(Left)
  2974. {
  2975. switch(itemsType[1])
  2976. {
  2977. case RESOURCE:
  2978. return Point(122, 446);
  2979. case CREATURE:
  2980. return Point(128, 450);
  2981. }
  2982. }
  2983. else
  2984. {
  2985. switch(itemsType[0])
  2986. {
  2987. case RESOURCE:
  2988. return Point(410, 446);
  2989. case ARTIFACT_TYPE:
  2990. return Point(425, 447);
  2991. case PLAYER:
  2992. return Point(417, 451);
  2993. }
  2994. }
  2995. assert(0);
  2996. return Point(0,0);
  2997. }
  2998. void CMarketplaceWindow::resourceChanged(int type, int val)
  2999. {
  3000. initSubs(true);
  3001. }
  3002. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  3003. {
  3004. switch(type)
  3005. {
  3006. case RESOURCE:
  3007. dx = 82;
  3008. dy = 79;
  3009. x = 39;
  3010. y = 180;
  3011. h = 66;
  3012. w = 74;
  3013. break;
  3014. case PLAYER:
  3015. dx = 83;
  3016. dy = 118;
  3017. h = 64;
  3018. w = 58;
  3019. x = 44;
  3020. y = 83;
  3021. assert(Right);
  3022. break;
  3023. case CREATURE://45,123
  3024. x = 45;
  3025. y = 123;
  3026. w = 58;
  3027. h = 64;
  3028. dx = 83;
  3029. dy = 98;
  3030. assert(!Right);
  3031. break;
  3032. case ARTIFACT_TYPE://45,123
  3033. x = 340-289;
  3034. y = 180;
  3035. w = 44;
  3036. h = 44;
  3037. dx = 83;
  3038. dy = 79;
  3039. break;
  3040. }
  3041. leftToRightOffset = 289;
  3042. }
  3043. void CMarketplaceWindow::updateTraderText()
  3044. {
  3045. if(readyToTrade)
  3046. {
  3047. if(mode == RESOURCE_PLAYER)
  3048. {
  3049. //I can give %s to the %s player.
  3050. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  3051. }
  3052. else if(mode == RESOURCE_ARTIFACT)
  3053. {
  3054. //I can offer you the %s for %d %s of %s.
  3055. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  3056. }
  3057. else if(mode == RESOURCE_RESOURCE)
  3058. {
  3059. //I can offer you %d %s of %s for %d %s of %s.
  3060. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[157]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  3061. }
  3062. else if(mode == CREATURE_RESOURCE)
  3063. {
  3064. //I can offer you %d %s of %s for %d %s.
  3065. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  3066. }
  3067. return;
  3068. }
  3069. int gnrtxtnr = -1;
  3070. if(madeTransaction)
  3071. {
  3072. if(mode == RESOURCE_PLAYER)
  3073. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  3074. else
  3075. gnrtxtnr = 162; //You have received quite a bargain. I expect to make no profit on the deal. Can I interest you in any of my other wares?
  3076. }
  3077. else
  3078. {
  3079. if(mode == RESOURCE_PLAYER)
  3080. gnrtxtnr = 167; //If you'd like to give any of your resources to another player, click on the item you wish to give and to whom.
  3081. else
  3082. gnrtxtnr = 163; //Please inspect our fine wares. If you feel like offering a trade, click on the items you wish to trade with and for.
  3083. }
  3084. traderText->setTxt(CGI->generaltexth->allTexts[gnrtxtnr]);
  3085. }
  3086. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= NULL*/, EMarketMode Mode)
  3087. :CTradeWindow(Market, Hero, Mode)
  3088. {
  3089. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3090. bg = new CPicture(Mode == CREATURE_EXP ? "ALTARMON.bmp" : "ALTARART.bmp");
  3091. bg->colorizeAndConvert(LOCPLINT->playerID);
  3092. pos = bg->center();
  3093. if(Mode == CREATURE_EXP)
  3094. {
  3095. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name), 155, 30, FONT_SMALL, tytulowy, *bg); //%s's Creatures
  3096. printAtMiddle(CGI->generaltexth->allTexts[479], 450, 30, FONT_SMALL, tytulowy, *bg); //Altar of Sacrifice
  3097. printAtMiddleWB(CGI->generaltexth->allTexts[480], 450, 70, FONT_SMALL, 45, tytulowy, *bg); //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  3098. slider = new CSlider(231,481,137,0,0,0);
  3099. slider->moved = boost::bind(&CAltarWindow::sliderMoved,this,_1);
  3100. max = new AdventureMapButton(CGI->generaltexth->zelp[578],boost::bind(&CSlider::moveToMax, slider),147,520,"IRCBTNS.DEF");
  3101. sacrificedUnits.resize(ARMY_SIZE, 0);
  3102. sacrificeAll = new AdventureMapButton(CGI->generaltexth->zelp[579],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTARMY.DEF");
  3103. sacrificeBackpack = NULL;
  3104. initItems(true);
  3105. mimicCres();
  3106. }
  3107. else
  3108. {
  3109. printAtMiddle(CGI->generaltexth->allTexts[477], 450, 34, FONT_SMALL, tytulowy, *bg); //Sacrifice artifacts for experience
  3110. printAtMiddle(CGI->generaltexth->allTexts[478], 302, 423, FONT_SMALL, tytulowy, *bg); //%s's Creatures
  3111. sacrificeAll = new AdventureMapButton(CGI->generaltexth->zelp[571],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTFILL.DEF");
  3112. sacrificeAll->block(!hero->artifactsInBackpack.size() && !hero->artifactsWorn.size());
  3113. sacrificeBackpack = new AdventureMapButton(CGI->generaltexth->zelp[570],boost::bind(&CAltarWindow::SacrificeBackpack,this),147,520,"ALTEMBK.DEF");
  3114. sacrificeBackpack->block(!hero->artifactsInBackpack.size());
  3115. slider = NULL;
  3116. max = NULL;
  3117. {
  3118. BLOCK_CAPTURING;
  3119. arts = new CArtifactsOfHero(Point(pos.x-363, pos.y-12));
  3120. arts->commonInfo = new CArtifactsOfHero::SCommonPart;
  3121. arts->commonInfo->participants.insert(arts);
  3122. arts->setHero(Hero);
  3123. arts->recActions = 255;
  3124. arts->allowedAssembling = false;
  3125. addChild(arts);
  3126. artSets.push_back(arts);
  3127. }
  3128. initItems(false);
  3129. }
  3130. printAtMiddleWB(CGI->generaltexth->allTexts[475], 72, 437, FONT_SMALL, 17, tytulowy, *bg); //Experience needed to reach next level
  3131. printAtMiddleWB(CGI->generaltexth->allTexts[476], 72, 505, FONT_SMALL, 17, tytulowy, *bg); //Total experience on the Altar
  3132. new CGStatusBar(302, 576);
  3133. ok = new AdventureMapButton(CGI->generaltexth->zelp[568],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  3134. ok->assignedKeys.insert(SDLK_ESCAPE);
  3135. deal = new AdventureMapButton(CGI->generaltexth->zelp[585],boost::bind(&CAltarWindow::makeDeal,this),269,520,"ALTSACR.DEF");
  3136. if(Hero->getAlignment() != EVIL && Mode == CREATURE_EXP)
  3137. new AdventureMapButton(CGI->generaltexth->zelp[580], boost::bind(&CTradeWindow::setMode,this, ARTIFACT_EXP), 516, 421, "ALTART.DEF");
  3138. if(Hero->getAlignment() != GOOD && Mode == ARTIFACT_EXP)
  3139. new AdventureMapButton(CGI->generaltexth->zelp[572], boost::bind(&CTradeWindow::setMode,this, CREATURE_EXP), 516, 421, "ALTSACC.DEF");
  3140. expPerUnit.resize(ARMY_SIZE, 0);
  3141. getExpValues();
  3142. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  3143. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  3144. setExpToLevel();
  3145. calcTotalExp();
  3146. blockTrade();
  3147. }
  3148. CAltarWindow::~CAltarWindow()
  3149. {
  3150. }
  3151. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  3152. {
  3153. leftToRightOffset = 289;
  3154. x = 45;
  3155. y = 110;
  3156. w = 58;
  3157. h = 64;
  3158. dx = 83;
  3159. dy = 98;
  3160. }
  3161. void CAltarWindow::sliderMoved(int to)
  3162. {
  3163. sacrificedUnits[hLeft->serial] = to;
  3164. updateRight(hRight);
  3165. deal->block(!to);
  3166. calcTotalExp();
  3167. redraw();
  3168. }
  3169. void CAltarWindow::makeDeal()
  3170. {
  3171. if(mode == CREATURE_EXP)
  3172. {
  3173. blockTrade();
  3174. slider->value = 0;
  3175. std::vector<int> toSacrifice = sacrificedUnits;
  3176. for (int i = 0; i < toSacrifice.size(); i++)
  3177. {
  3178. if(toSacrifice[i])
  3179. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  3180. }
  3181. BOOST_FOREACH(int& val, sacrificedUnits)
  3182. val = 0;
  3183. BOOST_FOREACH(CTradeableItem *t, items[0])
  3184. {
  3185. t->type = CREATURE_PLACEHOLDER;
  3186. t->subtitle = "";
  3187. }
  3188. }
  3189. else
  3190. {
  3191. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar) //sacrifice each artifact on the list
  3192. {
  3193. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  3194. }
  3195. arts->artifactsOnAltar.clear();
  3196. BOOST_FOREACH(CTradeableItem *t, items[0])
  3197. {
  3198. t->id = -1;
  3199. t->subtitle = "";
  3200. }
  3201. arts->commonInfo->reset();
  3202. //arts->scrollBackpack(0);
  3203. deal->block(true);
  3204. }
  3205. calcTotalExp();
  3206. }
  3207. void CAltarWindow::SacrificeAll()
  3208. {
  3209. if(mode == CREATURE_EXP)
  3210. {
  3211. bool movedAnything = false;
  3212. BOOST_FOREACH(CTradeableItem *t, items[1])
  3213. sacrificedUnits[t->serial] = hero->getStackCount(t->serial);
  3214. sacrificedUnits[items[1].front()->serial]--;
  3215. BOOST_FOREACH(CTradeableItem *t, items[0])
  3216. {
  3217. updateRight(t);
  3218. if(t->type == CREATURE)
  3219. movedAnything = true;
  3220. }
  3221. deal->block(!movedAnything);
  3222. calcTotalExp();
  3223. }
  3224. else
  3225. {
  3226. for(std::map<ui16, ArtSlotInfo>::const_iterator i = hero->artifactsWorn.begin(); i != hero->artifactsWorn.end(); i++)
  3227. {
  3228. if(i->second.artifact->artType->id != 145) //ignore locks from assembled artifacts
  3229. moveFromSlotToAltar(i->first, NULL, i->second.artifact);
  3230. }
  3231. SacrificeBackpack();
  3232. }
  3233. redraw();
  3234. }
  3235. void CAltarWindow::selectionChanged(bool side)
  3236. {
  3237. if(mode != CREATURE_EXP)
  3238. return;
  3239. CTradeableItem *&selected = side ? hLeft : hRight;
  3240. CTradeableItem *&theOther = side ? hRight : hLeft;
  3241. theOther = *std::find_if(items[!side].begin(), items[!side].end(), boost::bind(&CTradeableItem::serial, _1) == selected->serial);
  3242. int stackCount = 0;
  3243. for (int i = 0; i < ARMY_SIZE; i++)
  3244. if(hero->getStackCount(i) > sacrificedUnits[i])
  3245. stackCount++;
  3246. slider->setAmount(hero->getStackCount(hLeft->serial) - (stackCount == 1));
  3247. slider->block(!slider->amount);
  3248. slider->value = sacrificedUnits[hLeft->serial];
  3249. max->block(!slider->amount);
  3250. readyToTrade = true;
  3251. redraw();
  3252. }
  3253. void CAltarWindow::mimicCres()
  3254. {
  3255. std::vector<Rect> positions;
  3256. getPositionsFor(positions, false, CREATURE);
  3257. BOOST_FOREACH(CTradeableItem *t, items[1])
  3258. {
  3259. CTradeableItem *hlp = new CTradeableItem(CREATURE_PLACEHOLDER, t->id, false, t->serial);
  3260. hlp->pos = positions[t->serial] + hlp->pos;
  3261. items[0].push_back(hlp);
  3262. }
  3263. }
  3264. Point CAltarWindow::selectionOffset(bool Left) const
  3265. {
  3266. if(Left)
  3267. return Point(150, 421);
  3268. else
  3269. return Point(396, 421);
  3270. }
  3271. std::string CAltarWindow::selectionSubtitle(bool Left) const
  3272. {
  3273. if(Left && slider && hLeft)
  3274. return boost::lexical_cast<std::string>(slider->value);
  3275. else if(!Left && hRight)
  3276. return hRight->subtitle;
  3277. else
  3278. return "";
  3279. }
  3280. void CAltarWindow::artifactsChanged(bool left)
  3281. {
  3282. }
  3283. void CAltarWindow::garrisonChanged()
  3284. {
  3285. if(mode != CREATURE_EXP)
  3286. return;
  3287. std::set<CTradeableItem *> empty;
  3288. getEmptySlots(empty);
  3289. BOOST_FOREACH(CTradeableItem *t, empty)
  3290. {
  3291. removeItem(*std::find_if(items[0].begin(), items[0].end(), boost::bind(&CTradeableItem::serial, _1) == t->serial));
  3292. removeItem(t);
  3293. }
  3294. initSubs(true);
  3295. getExpValues();
  3296. }
  3297. void CAltarWindow::getExpValues()
  3298. {
  3299. int dump;
  3300. BOOST_FOREACH(CTradeableItem *t, items[1])
  3301. if(t->id >= 0)
  3302. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], CREATURE_EXP);
  3303. }
  3304. void CAltarWindow::calcTotalExp()
  3305. {
  3306. int val = 0;
  3307. if(mode == CREATURE_EXP)
  3308. {
  3309. for (int i = 0; i < sacrificedUnits.size(); i++)
  3310. {
  3311. val += expPerUnit[i] * sacrificedUnits[i];
  3312. }
  3313. }
  3314. else
  3315. {
  3316. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar)
  3317. {
  3318. int dmp, valOfArt;
  3319. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  3320. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  3321. }
  3322. }
  3323. val *=(100+hero->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0f;
  3324. expOnAltar->setTxt(boost::lexical_cast<std::string>(val));
  3325. }
  3326. void CAltarWindow::setExpToLevel()
  3327. {
  3328. expToLevel->setTxt(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  3329. }
  3330. void CAltarWindow::blockTrade()
  3331. {
  3332. hLeft = hRight = NULL;
  3333. readyToTrade = false;
  3334. if(slider)
  3335. {
  3336. slider->block(true);
  3337. max->block(true);
  3338. }
  3339. deal->block(true);
  3340. }
  3341. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  3342. {
  3343. int val = sacrificedUnits[toUpdate->serial];
  3344. toUpdate->type = val ? CREATURE : CREATURE_PLACEHOLDER;
  3345. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  3346. }
  3347. int CAltarWindow::firstFreeSlot()
  3348. {
  3349. int ret = -1;
  3350. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  3351. return ret < items[0].size() ? ret : -1;
  3352. }
  3353. void CAltarWindow::SacrificeBackpack()
  3354. {
  3355. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  3356. for (int i = 0; i < hero->artifactsInBackpack.size(); i++)
  3357. {
  3358. if(vstd::contains(toOmmit, hero->artifactsInBackpack[i].artifact))
  3359. {
  3360. toOmmit -= hero->artifactsInBackpack[i].artifact;
  3361. continue;
  3362. }
  3363. putOnAltar(NULL, hero->artifactsInBackpack[i].artifact);
  3364. }
  3365. arts->scrollBackpack(0);
  3366. calcTotalExp();
  3367. }
  3368. void CAltarWindow::artifactPicked()
  3369. {
  3370. redraw();
  3371. }
  3372. void CAltarWindow::showAll(SDL_Surface * to)
  3373. {
  3374. CTradeWindow::showAll(to);
  3375. if(mode == ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  3376. {
  3377. blitAtLoc(graphics->artDefs->ourImages[arts->commonInfo->src.art->artType->id].bitmap, 281, 442, to);
  3378. int dmp, val;
  3379. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, ARTIFACT_EXP);
  3380. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, zwykly, to);
  3381. }
  3382. }
  3383. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  3384. {
  3385. int artID = art->artType->id;
  3386. if(artID != 1 && artID < 7) //special art
  3387. {
  3388. tlog2 << "Cannot put special artifact on altar!\n";
  3389. return false;
  3390. }
  3391. if(!altarSlot)
  3392. {
  3393. int slotIndex = firstFreeSlot();
  3394. if(slotIndex < 0)
  3395. {
  3396. tlog2 << "No free slots on altar!\n";
  3397. return false;
  3398. }
  3399. altarSlot = items[0][slotIndex];
  3400. }
  3401. int dmp, val;
  3402. market->getOffer(artID, 0, dmp, val, ARTIFACT_EXP);
  3403. arts->artifactsOnAltar.insert(art);
  3404. altarSlot->id = artID;
  3405. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  3406. altarSlot->hlp = art;
  3407. deal->block(false);
  3408. return true;
  3409. }
  3410. void CAltarWindow::moveFromSlotToAltar(int slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  3411. {
  3412. int freeBackpackSlot = hero->artifactsInBackpack.size() + Arts::BACKPACK_START;
  3413. if(arts->commonInfo->src.art)
  3414. {
  3415. arts->commonInfo->dst.slotID = freeBackpackSlot;
  3416. arts->commonInfo->dst.AOH = arts;
  3417. }
  3418. if(putOnAltar(altarSlot, art))
  3419. {
  3420. if(slotID < Arts::BACKPACK_START)
  3421. LOCPLINT->cb->swapArtifacts(hero, slotID, hero, freeBackpackSlot);
  3422. else
  3423. {
  3424. arts->commonInfo->src.clear();
  3425. arts->commonInfo->dst.clear();
  3426. CCS->curh->dragAndDropCursor(NULL);
  3427. arts->unmarkSlots(false);
  3428. }
  3429. }
  3430. }
  3431. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  3432. {
  3433. this->pos = pos;
  3434. SDL_Surface *hhlp = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  3435. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  3436. background = SDL_ConvertSurface(hhlp,screen->format,0);
  3437. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  3438. SDL_FreeSurface(hhlp);
  3439. //printing texts
  3440. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 242, 32, FONT_BIG, tytulowy, background); //window title
  3441. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 64, FONT_MEDIUM, tytulowy, background); //hero speed
  3442. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 130, FONT_MEDIUM, tytulowy, background); //enemy speed
  3443. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 196, FONT_MEDIUM, tytulowy, background); //map scroll speed
  3444. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 262, FONT_MEDIUM, tytulowy, background); //video quality
  3445. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 347, FONT_MEDIUM, tytulowy, background); //music volume
  3446. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 412, FONT_MEDIUM, tytulowy, background); //effects volume
  3447. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 282, 57, FONT_MEDIUM, zwykly, background); //show move path
  3448. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 282, 89, FONT_MEDIUM, zwykly, background); //show hero reminder
  3449. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 282, 121, FONT_MEDIUM, zwykly, background); //quick combat
  3450. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 282, 153, FONT_MEDIUM, zwykly, background); //video subtitles
  3451. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 282, 185, FONT_MEDIUM, zwykly, background); //town building outlines
  3452. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 282, 217, FONT_MEDIUM, zwykly, background); //spell book animation
  3453. //setting up buttons
  3454. // load = new AdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::loadf, this), pos.x+246, pos.y+298, "SOLOAD.DEF", SDLK_l);
  3455. // std::swap(save->imgs[0][0], load->imgs[0][1]);
  3456. save = new AdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second, boost::bind(&CSystemOptionsWindow::bsavef, this), pos.x+357, pos.y+298, "SOSAVE.DEF", SDLK_s);
  3457. save->swappedImages = true;
  3458. save->update();
  3459. // restart = new AdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second, boost::bind(&CSystemOptionsWindow::bmainmenuf, this), pos.x+346, pos.y+357, "SORSTRT", SDLK_r);
  3460. // std::swap(save->imgs[0][0], restart->imgs[0][1]);
  3461. mainMenu = new AdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second, boost::bind(&CSystemOptionsWindow::bmainmenuf, this), pos.x+357, pos.y+357, "SOMAIN.DEF", SDLK_m);
  3462. mainMenu->swappedImages = true;
  3463. mainMenu->update();
  3464. quitGame = new AdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), pos.x+246, pos.y+415, "soquit.def", SDLK_q);
  3465. quitGame->swappedImages = true;
  3466. quitGame->update();
  3467. backToMap = new AdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), pos.x+357, pos.y+415, "soretrn.def", SDLK_RETURN);
  3468. backToMap->swappedImages = true;
  3469. backToMap->update();
  3470. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  3471. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  3472. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", pos.x+28, pos.y+77, 1);
  3473. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", pos.x+76, pos.y+77, 2);
  3474. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", pos.x+124, pos.y+77, 4);
  3475. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", pos.x+172, pos.y+77, 8);
  3476. heroMoveSpeed->select(owner->sysOpts.heroMoveSpeed, 1);
  3477. heroMoveSpeed->onChange = boost::bind(&SystemOptions::setHeroMoveSpeed, &owner->sysOpts, _1);
  3478. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  3479. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", pos.x+28, pos.y+210, 1);
  3480. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", pos.x+92, pos.y+210, 2);
  3481. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", pos.x+156, pos.y+210, 4);
  3482. mapScrollSpeed->select(owner->sysOpts.mapScrollingSpeed, 1);
  3483. mapScrollSpeed->onChange = boost::bind(&SystemOptions::setMapScrollingSpeed, &owner->sysOpts, _1);
  3484. musicVolume = new CHighlightableButtonsGroup(0, true);
  3485. for(int i=0; i<10; ++i)
  3486. {
  3487. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", pos.x+29 + 19*i, pos.y+359, i*11);
  3488. }
  3489. musicVolume->select(CCS->musich->getVolume(), 1);
  3490. musicVolume->onChange = boost::bind(&SystemOptions::setMusicVolume, &owner->sysOpts, _1);
  3491. effectsVolume = new CHighlightableButtonsGroup(0, true);
  3492. for(int i=0; i<10; ++i)
  3493. {
  3494. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", pos.x+29 + 19*i, pos.y+425, i*11);
  3495. }
  3496. effectsVolume->select(CCS->soundh->getVolume(), 1);
  3497. effectsVolume->onChange = boost::bind(&SystemOptions::setSoundVolume, &owner->sysOpts, _1);
  3498. }
  3499. CSystemOptionsWindow::~CSystemOptionsWindow()
  3500. {
  3501. SDL_FreeSurface(background);
  3502. delete save;
  3503. delete quitGame;
  3504. delete backToMap;
  3505. delete mainMenu;
  3506. delete heroMoveSpeed;
  3507. delete mapScrollSpeed;
  3508. delete musicVolume;
  3509. delete effectsVolume;
  3510. }
  3511. void CSystemOptionsWindow::pushSDLEvent(int type, int usercode)
  3512. {
  3513. GH.popIntTotally(this);
  3514. SDL_Event event;
  3515. event.type = type;
  3516. event.user.code = usercode; // not necessarily used
  3517. SDL_PushEvent(&event);
  3518. }
  3519. void CSystemOptionsWindow::bquitf()
  3520. {
  3521. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_QUIT, 0), 0, false);
  3522. }
  3523. void CSystemOptionsWindow::breturnf()
  3524. {
  3525. GH.popIntTotally(this);
  3526. }
  3527. void CSystemOptionsWindow::bmainmenuf()
  3528. {
  3529. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_USEREVENT, 2), 0, false);
  3530. }
  3531. void CSystemOptionsWindow::bsavef()
  3532. {
  3533. GH.popIntTotally(this);
  3534. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  3535. /*using namespace boost::posix_time;
  3536. std::ostringstream fnameStream;
  3537. fnameStream << second_clock::local_time();
  3538. std::string fname = fnameStream.str();
  3539. boost::algorithm::replace_all(fname,":","");
  3540. boost::algorithm::replace_all(fname," ","-");
  3541. 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);*/
  3542. }
  3543. void CSystemOptionsWindow::activate()
  3544. {
  3545. save->activate();
  3546. quitGame->activate();
  3547. backToMap->activate();
  3548. mainMenu->activate();
  3549. heroMoveSpeed->activate();
  3550. mapScrollSpeed->activate();
  3551. musicVolume->activate();
  3552. effectsVolume->activate();
  3553. }
  3554. void CSystemOptionsWindow::deactivate()
  3555. {
  3556. save->deactivate();
  3557. quitGame->deactivate();
  3558. backToMap->deactivate();
  3559. mainMenu->deactivate();
  3560. heroMoveSpeed->deactivate();
  3561. mapScrollSpeed->deactivate();
  3562. musicVolume->deactivate();
  3563. effectsVolume->deactivate();
  3564. }
  3565. void CSystemOptionsWindow::show(SDL_Surface *to)
  3566. {
  3567. CSDL_Ext::blitSurface(background, NULL, to, &pos);
  3568. save->showAll(to);
  3569. quitGame->showAll(to);
  3570. backToMap->showAll(to);
  3571. mainMenu->showAll(to);
  3572. heroMoveSpeed->showAll(to);
  3573. mapScrollSpeed->showAll(to);
  3574. musicVolume->showAll(to);
  3575. effectsVolume->showAll(to);
  3576. }
  3577. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj)
  3578. : tavernObj(TavernObj)
  3579. {
  3580. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3581. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  3582. assert(h.size() == 2);
  3583. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  3584. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  3585. if(h[0])
  3586. selected = 0;
  3587. else
  3588. selected = -1;
  3589. oldSelected = -1;
  3590. bg = new CPicture("TPTAVERN.bmp");
  3591. bg->colorizeAndConvert(LOCPLINT->playerID);
  3592. pos = center(bg->pos);
  3593. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,FONT_BIG,tytulowy,*bg);
  3594. printAtMiddle("2500",320,328,FONT_SMALL,zwykly,*bg);
  3595. // printAtMiddle(CGI->generaltexth->jktexts[38],146,283,FONT_BIG,tytulowy,bg); //what is this???
  3596. printAtMiddleWB(LOCPLINT->cb->getTavernGossip(tavernObj), 200, 220, FONT_SMALL, 50, zwykly, *bg);
  3597. bar = new CGStatusBar(8, 478, "APHLFTRT.bmp", 380);
  3598. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  3599. recruit = new AdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  3600. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  3601. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  3602. {
  3603. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  3604. recruit->block(2);
  3605. }
  3606. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  3607. {
  3608. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  3609. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  3610. recruit->block(2);
  3611. }
  3612. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  3613. {
  3614. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  3615. recruit->block(2);
  3616. }
  3617. else
  3618. {
  3619. if(!h[0])
  3620. recruit->block(1);
  3621. }
  3622. #ifdef _WIN32
  3623. CCS->videoh->open("TAVERN.BIK");
  3624. #else
  3625. CCS->videoh->open("tavern.mjpg", true, false);
  3626. #endif
  3627. }
  3628. void CTavernWindow::recruitb()
  3629. {
  3630. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  3631. const CGObjectInstance *obj = tavernObj;
  3632. close();
  3633. LOCPLINT->cb->recruitHero(obj, toBuy);
  3634. }
  3635. void CTavernWindow::thievesguildb()
  3636. {
  3637. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  3638. }
  3639. CTavernWindow::~CTavernWindow()
  3640. {
  3641. CCS->videoh->close();
  3642. }
  3643. void CTavernWindow::close()
  3644. {
  3645. GH.popIntTotally(this);
  3646. }
  3647. void CTavernWindow::show(SDL_Surface * to)
  3648. {
  3649. CIntObject::show(to);
  3650. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  3651. if(selected >= 0)
  3652. {
  3653. HeroPortrait *sel = selected ? h2 : h1;
  3654. if (selected != oldSelected && !recruit->isBlocked())
  3655. {
  3656. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  3657. oldSelected = selected;
  3658. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  3659. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  3660. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  3661. }
  3662. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,FONT_SMALL,40,zwykly,to);
  3663. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  3664. }
  3665. }
  3666. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  3667. {
  3668. if(previousState && !down && h)
  3669. as();
  3670. //ClickableL::clickLeft(down);
  3671. }
  3672. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  3673. {
  3674. if(down && h)
  3675. {
  3676. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  3677. }
  3678. }
  3679. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  3680. :as(sel,id), h(H)
  3681. {
  3682. used = LCLICK | RCLICK | HOVER;
  3683. h = H;
  3684. pos.x = x;
  3685. pos.y = y;
  3686. pos.w = 58;
  3687. pos.h = 64;
  3688. if(H)
  3689. {
  3690. hoverName = CGI->generaltexth->tavernInfo[4];
  3691. boost::algorithm::replace_first(hoverName,"%s",H->name);
  3692. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  3693. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  3694. if(vstd::contains(h->artifactsWorn,i))
  3695. artifs--;
  3696. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  3697. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  3698. descr[sizeof(descr)-1] = '\0';
  3699. }
  3700. }
  3701. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  3702. {
  3703. blitAt(graphics->portraitLarge[h->subID],pos,to);
  3704. }
  3705. void CTavernWindow::HeroPortrait::hover( bool on )
  3706. {
  3707. //Hoverable::hover(on);
  3708. if(on)
  3709. GH.statusbar->print(hoverName);
  3710. else
  3711. GH.statusbar->clear();
  3712. }
  3713. void CInGameConsole::activate()
  3714. {
  3715. activateKeys();
  3716. }
  3717. void CInGameConsole::deactivate()
  3718. {
  3719. deactivateKeys();
  3720. }
  3721. void CInGameConsole::show(SDL_Surface * to)
  3722. {
  3723. int number = 0;
  3724. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3725. texts_mx.lock();
  3726. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  3727. {
  3728. SDL_Color green = {0,0xff,0,0};
  3729. Point leftBottomCorner(0, screen->h);
  3730. if(LOCPLINT->battleInt)
  3731. {
  3732. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3733. }
  3734. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, FONT_MEDIUM, green);
  3735. if(SDL_GetTicks() - it->second > defaultTimeout)
  3736. {
  3737. toDel.push_back(it);
  3738. }
  3739. }
  3740. for(int it=0; it<toDel.size(); ++it)
  3741. {
  3742. texts.erase(toDel[it]);
  3743. }
  3744. texts_mx.unlock();
  3745. }
  3746. void CInGameConsole::print(const std::string &txt)
  3747. {
  3748. texts_mx.lock();
  3749. int lineLen = conf.go()->ac.outputLineLength;
  3750. if(txt.size() < lineLen)
  3751. {
  3752. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3753. if(texts.size() > maxDisplayedTexts)
  3754. {
  3755. texts.pop_front();
  3756. }
  3757. }
  3758. else
  3759. {
  3760. assert(lineLen);
  3761. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3762. {
  3763. std::string part = txt.substr(g * lineLen, lineLen);
  3764. if(part.size() == 0)
  3765. break;
  3766. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3767. if(texts.size() > maxDisplayedTexts)
  3768. {
  3769. texts.pop_front();
  3770. }
  3771. }
  3772. }
  3773. texts_mx.unlock();
  3774. }
  3775. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3776. {
  3777. if(key.type != SDL_KEYDOWN) return;
  3778. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3779. switch(key.keysym.sym)
  3780. {
  3781. case SDLK_TAB:
  3782. case SDLK_ESCAPE:
  3783. {
  3784. if(captureAllKeys)
  3785. {
  3786. captureAllKeys = false;
  3787. endEnteringText(false);
  3788. }
  3789. else if(SDLK_TAB)
  3790. {
  3791. captureAllKeys = true;
  3792. startEnteringText();
  3793. }
  3794. break;
  3795. }
  3796. case SDLK_RETURN: //enter key
  3797. {
  3798. if(enteredText.size() > 0 && captureAllKeys)
  3799. {
  3800. captureAllKeys = false;
  3801. endEnteringText(true);
  3802. }
  3803. break;
  3804. }
  3805. case SDLK_BACKSPACE:
  3806. {
  3807. if(enteredText.size() > 1)
  3808. {
  3809. enteredText.resize(enteredText.size()-1);
  3810. enteredText[enteredText.size()-1] = '_';
  3811. refreshEnteredText();
  3812. }
  3813. break;
  3814. }
  3815. case SDLK_UP: //up arrow
  3816. {
  3817. if(previouslyEntered.size() == 0)
  3818. break;
  3819. if(prevEntDisp == -1)
  3820. {
  3821. prevEntDisp = previouslyEntered.size() - 1;
  3822. enteredText = previouslyEntered[prevEntDisp] + "_";
  3823. refreshEnteredText();
  3824. }
  3825. else if( prevEntDisp > 0)
  3826. {
  3827. --prevEntDisp;
  3828. enteredText = previouslyEntered[prevEntDisp] + "_";
  3829. refreshEnteredText();
  3830. }
  3831. break;
  3832. }
  3833. case SDLK_DOWN: //down arrow
  3834. {
  3835. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3836. {
  3837. ++prevEntDisp;
  3838. enteredText = previouslyEntered[prevEntDisp] + "_";
  3839. refreshEnteredText();
  3840. }
  3841. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3842. {
  3843. prevEntDisp = -1;
  3844. enteredText = "_";
  3845. refreshEnteredText();
  3846. }
  3847. break;
  3848. }
  3849. default:
  3850. {
  3851. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3852. {
  3853. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3854. {
  3855. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3856. enteredText += "_";
  3857. refreshEnteredText();
  3858. }
  3859. }
  3860. break;
  3861. }
  3862. }
  3863. }
  3864. void CInGameConsole::startEnteringText()
  3865. {
  3866. enteredText = "_";
  3867. if(GH.topInt() == adventureInt)
  3868. {
  3869. GH.statusbar->print(enteredText);
  3870. }
  3871. else if(LOCPLINT->battleInt)
  3872. {
  3873. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3874. }
  3875. }
  3876. void CInGameConsole::endEnteringText(bool printEnteredText)
  3877. {
  3878. prevEntDisp = -1;
  3879. if(printEnteredText)
  3880. {
  3881. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3882. LOCPLINT->cb->sendMessage(txt);
  3883. previouslyEntered.push_back(txt);
  3884. print(txt);
  3885. }
  3886. enteredText = "";
  3887. if(GH.topInt() == adventureInt)
  3888. {
  3889. GH.statusbar->clear();
  3890. }
  3891. else if(LOCPLINT->battleInt)
  3892. {
  3893. LOCPLINT->battleInt->console->ingcAlter = "";
  3894. }
  3895. }
  3896. void CInGameConsole::refreshEnteredText()
  3897. {
  3898. if(GH.topInt() == adventureInt)
  3899. {
  3900. GH.statusbar->print(enteredText);
  3901. }
  3902. else if(LOCPLINT->battleInt)
  3903. {
  3904. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3905. }
  3906. }
  3907. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3908. {
  3909. }
  3910. void CGarrisonWindow::close()
  3911. {
  3912. GH.popIntTotally(this);
  3913. }
  3914. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits )
  3915. {
  3916. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3917. bg = new CPicture("GARRISON.bmp");
  3918. bg->colorizeAndConvert(LOCPLINT->playerID);
  3919. pos = bg->center();
  3920. garr = new CGarrisonInt(92, 127, 4, Point(0,96), bg->bg, Point(93,127), up, down, removableUnits);
  3921. {
  3922. AdventureMapButton *split = new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),88,314,"IDV6432.DEF");
  3923. removeChild(split);
  3924. garr->addSplitBtn(split);
  3925. }
  3926. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),399,314,"IOK6432.DEF",SDLK_RETURN);
  3927. }
  3928. CGarrisonWindow::~CGarrisonWindow()
  3929. {
  3930. }
  3931. void CGarrisonWindow::showAll(SDL_Surface * to)
  3932. {
  3933. CIntObject::showAll(to);
  3934. std::string title;
  3935. if (garr->armedObjs[1]->tempOwner == garr->armedObjs[0]->tempOwner)
  3936. title = CGI->generaltexth->allTexts[709];
  3937. else
  3938. {
  3939. title = CGI->generaltexth->allTexts[35];
  3940. boost::algorithm::replace_first(title, "%s", garr->armedObjs[0]->Slots().begin()->second->type->namePl);
  3941. }
  3942. blitAtLoc(graphics->flags->ourImages[garr->armedObjs[1]->getOwner()].bitmap,28,124,to);
  3943. blitAtLoc(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->armedObjs[1])->portrait],29,222,to);
  3944. printAtMiddleLoc(title,275,30,FONT_BIG,tytulowy,to);
  3945. }
  3946. IShowActivable::IShowActivable()
  3947. {
  3948. type = 0;
  3949. }
  3950. CGarrisonHolder::CGarrisonHolder()
  3951. {
  3952. type |= WITH_GARRISON;
  3953. }
  3954. void CWindowWithGarrison::updateGarrisons()
  3955. {
  3956. garr->recreateSlots();
  3957. }
  3958. void CRClickPopupInt::show(SDL_Surface * to)
  3959. {
  3960. inner->show(to);
  3961. }
  3962. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  3963. {
  3964. CCS->curh->hide();
  3965. inner = our;
  3966. delInner = deleteInt;
  3967. }
  3968. CRClickPopupInt::~CRClickPopupInt()
  3969. {
  3970. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  3971. // //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...?)
  3972. // if(LOCPLINT && inner == adventureInt->heroWindow)
  3973. // adventureInt->heroWindow->dispose();
  3974. if(delInner)
  3975. delete inner;
  3976. CCS->curh->show();
  3977. }
  3978. void CRClickPopupInt::showAll(SDL_Surface * to)
  3979. {
  3980. inner->showAll(to);
  3981. }
  3982. CArtPlace::CArtPlace(const CArtifactInstance* Art)
  3983. :picked(false), marked(false), locked(false), ourArt(Art)
  3984. {
  3985. }
  3986. void CArtPlace::activate()
  3987. {
  3988. if(!active)
  3989. {
  3990. LRClickableAreaWTextComp::activate();
  3991. }
  3992. }
  3993. void CArtPlace::clickLeft(tribool down, bool previousState)
  3994. {
  3995. //LRClickableAreaWTextComp::clickLeft(down);
  3996. bool inBackpack = slotID >= Arts::BACKPACK_START,
  3997. srcInBackpack = ourOwner->commonInfo->src.slotID >= Arts::BACKPACK_START,
  3998. srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  3999. // If clicked on spellbook, open it only if no artifact is held at the moment.
  4000. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  4001. {
  4002. if(ourArt->artType->id == 0)
  4003. {
  4004. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt);
  4005. GH.pushInt(spellWindow);
  4006. }
  4007. }
  4008. if (!down && previousState)
  4009. {
  4010. if(ourArt && ourArt->artType->id == 0) //spellbook
  4011. return; //this is handled separately
  4012. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  4013. {
  4014. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  4015. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  4016. {
  4017. if(ourArt->artType->id == 3) //catapult cannot be highlighted
  4018. {
  4019. std::vector<SComponent *> catapult(1, new SComponent(SComponent::artifact, 3, 0));
  4020. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  4021. return;
  4022. }
  4023. select();
  4024. }
  4025. }
  4026. else if(ourArt == ourOwner->commonInfo->src.art) //restore previously picked artifact
  4027. {
  4028. deselect();
  4029. }
  4030. else //perform artifact transition
  4031. {
  4032. if(inBackpack) // Backpack destination.
  4033. {
  4034. if(srcInBackpack && slotID == ourOwner->commonInfo->src.slotID + 1) //next slot (our is not visible, so visually same as "old" place) to the art -> make nothing, return artifact to slot
  4035. {
  4036. deselect();
  4037. }
  4038. else
  4039. {
  4040. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  4041. switch(cur->id)
  4042. {
  4043. case 3:
  4044. //should not happen, catapult cannot be selected
  4045. assert(cur->id != 3);
  4046. break;
  4047. case 4: case 5: case 6: //war machines cannot go to backpack
  4048. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->Name()));
  4049. break;
  4050. default:
  4051. setMeAsDest();
  4052. amin(ourOwner->commonInfo->dst.slotID, ourOwner->curHero->artifactsInBackpack.size() + Arts::BACKPACK_START);
  4053. if(srcInBackpack && srcInSameHero)
  4054. {
  4055. if(!ourArt //cannot move from backpack to AFTER backpack -> combined with amin above it will guarantee that dest is at most the last artifact
  4056. || ourOwner->commonInfo->src.slotID < ourOwner->commonInfo->dst.slotID) //rearranging arts in backpack after taking src artifact, the dest id will be shifted
  4057. ourOwner->commonInfo->dst.slotID--;
  4058. }
  4059. if(srcInSameHero && ourOwner->commonInfo->dst.slotID == ourOwner->commonInfo->src.slotID) //we came to src == dst
  4060. deselect();
  4061. else
  4062. ourOwner->realizeCurrentTransaction();
  4063. break;
  4064. }
  4065. }
  4066. }
  4067. //check if swap is possible
  4068. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  4069. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  4070. {
  4071. setMeAsDest();
  4072. //
  4073. // // Special case when the dest artifact can't be fit into the src slot.
  4074. // //CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
  4075. // const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->src.AOH;
  4076. // ui16 srcSlotID = ourOwner->commonInfo->src.slotID;
  4077. // if (ourArt && srcSlotID < 19 && !ourArt->canBePutAt(ArtifactLocation(srcAOH->curHero, srcSlotID)))
  4078. // {
  4079. // // Put dest artifact into owner's backpack.
  4080. // ourOwner->commonInfo->src.AOH = ourOwner;
  4081. // ourOwner->commonInfo->src.slotID = ourOwner->curHero->artifacts.size() + 19;
  4082. // }
  4083. ourOwner->realizeCurrentTransaction();
  4084. }
  4085. }
  4086. }
  4087. }
  4088. void CArtPlace::clickRight(tribool down, bool previousState)
  4089. {
  4090. if(down && ourArt && !locked && text.size()) //if there is no description or it's a lock, do nothing ;]
  4091. {
  4092. if (slotID < 19)
  4093. {
  4094. if(ourOwner->allowedAssembling)
  4095. {
  4096. std::vector<const CArtifact *> assemblyPossibilities = ourArt->assemblyPossibilities(ourOwner->curHero);
  4097. // If the artifact can be assembled, display dialog.
  4098. BOOST_FOREACH(const CArtifact *combination, assemblyPossibilities)
  4099. {
  4100. LOCPLINT->showArtifactAssemblyDialog(
  4101. ourArt->artType->id,
  4102. combination->id,
  4103. true,
  4104. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, true, combination->id),
  4105. 0);
  4106. if(assemblyPossibilities.size() > 2)
  4107. {
  4108. tlog3 << "More than one possibility of assembling... taking only first\n";
  4109. break;
  4110. }
  4111. }
  4112. // Otherwise if the artifact can be diasassembled, display dialog.
  4113. if(ourArt->canBeDisassembled())
  4114. {
  4115. LOCPLINT->showArtifactAssemblyDialog(
  4116. ourArt->artType->id,
  4117. 0,
  4118. false,
  4119. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, false, 0),
  4120. 0);
  4121. }
  4122. }
  4123. }
  4124. // Lastly just show the artifact description.
  4125. LRClickableAreaWTextComp::clickRight(down, previousState);
  4126. }
  4127. }
  4128. /**
  4129. * Selects artifact slot so that the containing artifact looks like it's picked up.
  4130. */
  4131. void CArtPlace::select ()
  4132. {
  4133. if (locked)
  4134. return;
  4135. picked = true;
  4136. //int backpackCorrection = -(slotID - Arts::BACKPACK_START < ourOwner->backpackPos);
  4137. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[ourArt->artType->id].bitmap);
  4138. ourOwner->commonInfo->src.setTo(this, false);
  4139. ourOwner->markPossibleSlots(ourArt);
  4140. if(slotID >= Arts::BACKPACK_START)
  4141. ourOwner->scrollBackpack(0); //will update slots
  4142. ourOwner->updateParentWindow();
  4143. ourOwner->safeRedraw();
  4144. }
  4145. /**
  4146. * Deselects the artifact slot. FIXME: Not used. Maybe it should?
  4147. */
  4148. void CArtPlace::deselect ()
  4149. {
  4150. picked = false;
  4151. CCS->curh->dragAndDropCursor(NULL);
  4152. ourOwner->unmarkSlots();
  4153. ourOwner->commonInfo->src.clear();
  4154. if(slotID >= Arts::BACKPACK_START)
  4155. ourOwner->scrollBackpack(0); //will update slots
  4156. ourOwner->updateParentWindow();
  4157. ourOwner->safeRedraw();
  4158. }
  4159. void CArtPlace::deactivate()
  4160. {
  4161. if(active)
  4162. {
  4163. LRClickableAreaWTextComp::deactivate();
  4164. }
  4165. }
  4166. void CArtPlace::showAll(SDL_Surface *to)
  4167. {
  4168. if (ourArt && !picked && ourArt == ourOwner->curHero->getArt(slotID, false)) //last condition is needed for disassembling -> artifact may be gone, but we don't know yet TODO: real, nice solution
  4169. {
  4170. int graphic = locked ? 145 : ourArt->artType->id;
  4171. blitAt(graphics->artDefs->ourImages[graphic].bitmap, pos.x, pos.y, to);
  4172. }
  4173. if(marked && active)
  4174. {
  4175. // Draw vertical bars.
  4176. for (int i = 0; i < pos.h; ++i)
  4177. {
  4178. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  4179. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  4180. }
  4181. // Draw horizontal bars.
  4182. for (int i = 0; i < pos.w; ++i)
  4183. {
  4184. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  4185. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  4186. }
  4187. }
  4188. }
  4189. bool CArtPlace::fitsHere(const CArtifactInstance * art) const
  4190. {
  4191. // You can place 'no artifact' anywhere.
  4192. if(!art)
  4193. return true;
  4194. // Anything can but War Machines can be placed in backpack.
  4195. if (slotID >= 19)
  4196. return !CGI->arth->isBigArtifact(art->id);
  4197. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  4198. }
  4199. CArtPlace::~CArtPlace()
  4200. {
  4201. deactivate();
  4202. }
  4203. void CArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  4204. {
  4205. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  4206. }
  4207. void CArtPlace::setArtifact(const CArtifactInstance *art)
  4208. {
  4209. baseType = -1; //by default we don't store any component
  4210. ourArt = art;
  4211. if(!art)
  4212. {
  4213. text = std::string();
  4214. hoverText = CGI->generaltexth->allTexts[507];
  4215. }
  4216. else
  4217. {
  4218. text = ourArt->artType->Description();
  4219. if(art->artType->id == 1) //spell scroll
  4220. {
  4221. // we expect scroll description to be like this: This scroll contains the [spell name] spell which is added into your spell book for as long as you carry the scroll.
  4222. // so we want to replace text in [...] with a spell name
  4223. // however other language versions don't have name placeholder at all, so we have to be careful
  4224. int spellID = art->getGivenSpellID();
  4225. int nameStart = text.find_first_of('[');
  4226. int nameEnd = text.find_first_of(']', nameStart);
  4227. if(spellID >= 0)
  4228. {
  4229. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  4230. text = text.replace(nameStart, nameEnd - nameStart + 1, CGI->spellh->spells[spellID]->name);
  4231. //add spell component info (used to provide a pic in r-click popup)
  4232. baseType = SComponent::spell;
  4233. type = spellID;
  4234. bonusValue = 0;
  4235. }
  4236. }
  4237. if (locked) // Locks should appear as empty.
  4238. hoverText = CGI->generaltexth->allTexts[507];
  4239. else
  4240. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->Name());
  4241. }
  4242. }
  4243. void HoverableArea::hover (bool on)
  4244. {
  4245. if (on)
  4246. GH.statusbar->print(hoverText);
  4247. else if (GH.statusbar->getCurrent()==hoverText)
  4248. GH.statusbar->clear();
  4249. }
  4250. HoverableArea::HoverableArea()
  4251. {
  4252. used |= HOVER;
  4253. }
  4254. HoverableArea::~HoverableArea()
  4255. {
  4256. }
  4257. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  4258. {
  4259. if(!down && previousState)
  4260. {
  4261. LOCPLINT->showInfoDialog(text);
  4262. }
  4263. }
  4264. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  4265. {
  4266. adventureInt->handleRightClick(text, down);
  4267. }
  4268. LRClickableAreaWText::LRClickableAreaWText()
  4269. {
  4270. init();
  4271. }
  4272. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  4273. {
  4274. init();
  4275. pos = Pos + pos;
  4276. hoverText = HoverText;
  4277. text = ClickText;
  4278. }
  4279. LRClickableAreaWText::~LRClickableAreaWText()
  4280. {
  4281. }
  4282. void LRClickableAreaWText::init()
  4283. {
  4284. used = LCLICK | RCLICK | HOVER;
  4285. }
  4286. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  4287. {
  4288. if((!down) && previousState)
  4289. {
  4290. std::vector<SComponent*> comp(1, createComponent());
  4291. LOCPLINT->showInfoDialog(text, comp);
  4292. }
  4293. }
  4294. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  4295. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  4296. {
  4297. }
  4298. SComponent * LRClickableAreaWTextComp::createComponent() const
  4299. {
  4300. if(baseType >= 0)
  4301. return new SComponent(SComponent::Etype(baseType), type, bonusValue);
  4302. else
  4303. return NULL;
  4304. }
  4305. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  4306. {
  4307. if(down)
  4308. {
  4309. if(SComponent *comp = createComponent())
  4310. {
  4311. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  4312. return;
  4313. }
  4314. }
  4315. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occured
  4316. }
  4317. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  4318. {
  4319. used = LCLICK | RCLICK | HOVER;
  4320. pos.x += x; pos.w = 58;
  4321. pos.y += y; pos.h = 64;
  4322. }
  4323. void CHeroArea::clickLeft(tribool down, bool previousState)
  4324. {
  4325. if((!down) && previousState && hero)
  4326. LOCPLINT->openHeroWindow(hero);
  4327. }
  4328. void CHeroArea::clickRight(tribool down, bool previousState)
  4329. {
  4330. if((!down) && previousState && hero)
  4331. LOCPLINT->openHeroWindow(hero);
  4332. }
  4333. void CHeroArea::hover(bool on)
  4334. {
  4335. if (on && hero)
  4336. GH.statusbar->print(hero->hoverName);
  4337. else
  4338. GH.statusbar->clear();
  4339. }
  4340. void CHeroArea::showAll(SDL_Surface * to)
  4341. {
  4342. if (hero)
  4343. blitAtLoc(graphics->portraitLarge[hero->portrait],0,0,to);
  4344. }
  4345. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  4346. {
  4347. if((!down) && previousState && town)
  4348. {
  4349. LOCPLINT->openTownWindow(town);
  4350. if ( type == 2 )
  4351. LOCPLINT->castleInt->builds->buildingClicked(10);
  4352. else if ( type == 3 && town->fortLevel() )
  4353. LOCPLINT->castleInt->builds->buildingClicked(7);
  4354. }
  4355. }
  4356. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  4357. {
  4358. if((!down) && previousState && town)
  4359. LOCPLINT->openTownWindow(town);//TODO: popup?
  4360. }
  4361. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  4362. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  4363. {
  4364. }
  4365. void CArtifactsOfHero::SCommonPart::reset()
  4366. {
  4367. src.clear();
  4368. dst.clear();
  4369. CCS->curh->dragAndDropCursor(NULL);
  4370. }
  4371. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  4372. {
  4373. // // An update is made, rather than initialization.
  4374. // if (curHero && curHero->id == hero->id)
  4375. // {
  4376. // if(curHero != hero)
  4377. // {
  4378. // //delete curHero;
  4379. // curHero = hero; //was: creating a copy
  4380. // }
  4381. //
  4382. // // Compensate backpack pos if an artifact was insertad before it.
  4383. // if (commonInfo->dst.slotID >= 19 && commonInfo->destAOH == this
  4384. // && commonInfo->dst.slotID - 19 < backpackPos)
  4385. // {
  4386. // backpackPos++;
  4387. // }
  4388. //
  4389. // if (updateState && commonInfo->srcAOH == this)
  4390. // {
  4391. // // A swap was made, make the replaced artifact the current selected.
  4392. // if (commonInfo->dst.slotID < 19 && commonInfo->destArtifact)
  4393. // {
  4394. // // // Temporarily remove artifact from hero.
  4395. // // if (commonInfo->srcSlotID < 19)
  4396. // // CGI->arth->unequipArtifact(curHero->artifWorn, commonInfo->srcSlotID);
  4397. // // else
  4398. // // curHero->artifacts.erase(curHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  4399. //
  4400. // updateParentWindow(); //TODO: evil! but does the thing
  4401. //
  4402. // // Source <- Dest
  4403. // commonInfo->srcArtifact = commonInfo->destArtifact;
  4404. //
  4405. // // Reset destination parameters.
  4406. // commonInfo->dst.clear();
  4407. //
  4408. // CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  4409. // markPossibleSlots(commonInfo->srcArtifact);
  4410. // }
  4411. // else if (commonInfo->destAOH != NULL)
  4412. // {
  4413. // // Reset all parameters.
  4414. // commonInfo->reset();
  4415. // unmarkSlots();
  4416. // }
  4417. // }
  4418. // }
  4419. // else
  4420. // {
  4421. // commonInfo->reset();
  4422. // }
  4423. //
  4424. // if(hero != curHero)
  4425. // {
  4426. // // delete curHero;
  4427. // // curHero = new CGHeroInstance(*hero);
  4428. // curHero = hero; //was: creating a copy
  4429. // }
  4430. curHero = hero;
  4431. if (curHero->artifactsInBackpack.size() > 0)
  4432. backpackPos %= curHero->artifactsInBackpack.size();
  4433. else
  4434. backpackPos = 0;
  4435. // Fill the slots for worn artifacts and backpack.
  4436. for (int g = 0; g < 19 ; g++)
  4437. setSlotData(artWorn[g], g);
  4438. scrollBackpack(0);
  4439. }
  4440. void CArtifactsOfHero::dispose()
  4441. {
  4442. //delNull(curHero);
  4443. unmarkSlots(false);
  4444. CCS->curh->dragAndDropCursor(NULL);
  4445. }
  4446. void CArtifactsOfHero::scrollBackpack(int dir)
  4447. {
  4448. int artsInBackpack = curHero->artifactsInBackpack.size();
  4449. backpackPos += dir;
  4450. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  4451. backpackPos += artsInBackpack;
  4452. if(artsInBackpack)
  4453. backpackPos %= artsInBackpack;
  4454. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  4455. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  4456. toOmit.insert(commonInfo->src.art);
  4457. int omitedSoFar = 0;
  4458. //set new data
  4459. size_t s = 0;
  4460. for( ; s < artsInBackpack; ++s)
  4461. {
  4462. if (s < artsInBackpack)
  4463. {
  4464. int slotID = 19 + (s + backpackPos)%artsInBackpack;
  4465. const CArtifactInstance *art = curHero->getArt(slotID);
  4466. assert(art);
  4467. if(!vstd::contains(toOmit, art))
  4468. {
  4469. if(s - omitedSoFar < 5)
  4470. setSlotData(backpack[s-omitedSoFar], slotID);
  4471. }
  4472. else
  4473. {
  4474. toOmit -= art;
  4475. omitedSoFar++;
  4476. continue;
  4477. }
  4478. }
  4479. }
  4480. for( ; s - omitedSoFar < 5; s++)
  4481. eraseSlotData(backpack[s-omitedSoFar], 19 + s);
  4482. //blocking scrolling if there is not enough artifacts to scroll
  4483. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  4484. leftArtRoll->block(!scrollingPossible);
  4485. rightArtRoll->block(!scrollingPossible);
  4486. safeRedraw();
  4487. }
  4488. /**
  4489. * Marks possible slots where a given artifact can be placed, except backpack.
  4490. *
  4491. * @param art Artifact checked against.
  4492. */
  4493. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art)
  4494. {
  4495. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4496. BOOST_FOREACH(CArtPlace *place, aoh->artWorn)
  4497. place->marked = art->canBePutAt(ArtifactLocation(aoh->curHero, place->slotID), true);
  4498. safeRedraw();
  4499. }
  4500. /**
  4501. * Unamarks all slots.
  4502. */
  4503. void CArtifactsOfHero::unmarkSlots(bool withRedraw /*= true*/)
  4504. {
  4505. if(commonInfo)
  4506. {
  4507. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4508. BOOST_FOREACH(CArtPlace *place, aoh->artWorn)
  4509. place->marked = false;
  4510. }
  4511. else
  4512. {
  4513. BOOST_FOREACH(CArtPlace *place, artWorn)
  4514. place->marked = false;
  4515. }
  4516. if(withRedraw)
  4517. safeRedraw();
  4518. }
  4519. /**
  4520. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  4521. */
  4522. void CArtifactsOfHero::setSlotData(CArtPlace* artPlace, int slotID)
  4523. {
  4524. if(!artPlace && slotID >= Arts::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  4525. {
  4526. return;
  4527. }
  4528. artPlace->picked = false;
  4529. artPlace->slotID = slotID;
  4530. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  4531. {
  4532. artPlace->setArtifact(asi->artifact);
  4533. artPlace->locked = asi->locked;
  4534. }
  4535. else
  4536. artPlace->setArtifact(NULL);
  4537. }
  4538. /**
  4539. * Makes given artifact slot appear as empty with a certain slot ID.
  4540. */
  4541. void CArtifactsOfHero::eraseSlotData (CArtPlace* artPlace, int slotID)
  4542. {
  4543. artPlace->picked = false;
  4544. artPlace->slotID = slotID;
  4545. artPlace->setArtifact(NULL);
  4546. }
  4547. CArtifactsOfHero::CArtifactsOfHero(const Point& position, bool createCommonPart /*= false*/)
  4548. : curHero(NULL), backpackPos(0), commonInfo(NULL), updateState(false), allowedAssembling(true)
  4549. {
  4550. if(createCommonPart)
  4551. {
  4552. commonInfo = new CArtifactsOfHero::SCommonPart;
  4553. commonInfo->participants.insert(this);
  4554. }
  4555. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4556. pos += position;
  4557. artWorn.resize(19);
  4558. std::vector<Rect> slotPos;
  4559. slotPos += genRect(44,44,509,30), genRect(44,44,567,240), genRect(44,44,509,80),
  4560. genRect(44,44,383,68), genRect(44,44,564,183), genRect(44,44,509,130),
  4561. genRect(44,44,431,68), genRect(44,44,610,183), genRect(44,44,515,295),
  4562. genRect(44,44,383,143), genRect(44,44,399,194), genRect(44,44,415,245),
  4563. genRect(44,44,431,296), genRect(44,44,564,30), genRect(44,44,610,30),
  4564. genRect(44,44,610,76), genRect(44,44,610,122), genRect(44,44,610,310),
  4565. genRect(44,44,381,296);
  4566. // Create slots for worn artifacts.
  4567. for (int g = 0; g < 19 ; g++)
  4568. {
  4569. artWorn[g] = new CArtPlace(NULL);
  4570. artWorn[g]->pos = slotPos[g] + pos;
  4571. artWorn[g]->ourOwner = this;
  4572. eraseSlotData(artWorn[g], g);
  4573. }
  4574. // Create slots for the backpack.
  4575. for(size_t s=0; s<5; ++s)
  4576. {
  4577. CArtPlace * add = new CArtPlace(NULL);
  4578. add->ourOwner = this;
  4579. add->pos.x = pos.x + 403 + 46*s;
  4580. add->pos.y = pos.y + 365;
  4581. add->pos.h = add->pos.w = 44;
  4582. eraseSlotData(add, 19 + s);
  4583. backpack.push_back(add);
  4584. }
  4585. leftArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), 379, 364, "hsbtns3.def", SDLK_LEFT);
  4586. rightArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), 632, 364, "hsbtns5.def", SDLK_RIGHT);
  4587. }
  4588. CArtifactsOfHero::~CArtifactsOfHero()
  4589. {
  4590. dispose();
  4591. }
  4592. void CArtifactsOfHero::updateParentWindow()
  4593. {
  4594. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt()))
  4595. {
  4596. if(updateState)
  4597. chw->curHero = curHero;
  4598. else
  4599. chw->update(curHero, true);
  4600. }
  4601. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt()))
  4602. {
  4603. //use our copy of hero to draw window
  4604. if(cew->heroInst[0]->id == curHero->id)
  4605. cew->heroInst[0] = curHero;
  4606. else
  4607. cew->heroInst[1] = curHero;
  4608. if(!updateState)
  4609. {
  4610. cew->deactivate();
  4611. // for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  4612. // {
  4613. // if(cew->heroInst[g] == curHero)
  4614. // {
  4615. // cew->artifs[g]->setHero(curHero);
  4616. // }
  4617. // }
  4618. cew->prepareBackground();
  4619. cew->redraw();
  4620. cew->activate();
  4621. }
  4622. }
  4623. }
  4624. void CArtifactsOfHero::safeRedraw()
  4625. {
  4626. if(parent)
  4627. parent->redraw();
  4628. else
  4629. redraw();
  4630. }
  4631. void CArtifactsOfHero::realizeCurrentTransaction()
  4632. {
  4633. assert(commonInfo->src.AOH);
  4634. assert(commonInfo->dst.AOH);
  4635. LOCPLINT->cb->swapArtifacts(commonInfo->src.AOH->curHero, commonInfo->src.slotID,
  4636. commonInfo->dst.AOH->curHero, commonInfo->dst.slotID);
  4637. }
  4638. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  4639. {
  4640. if(src.hero == curHero && src.slot >= Arts::BACKPACK_START)
  4641. setSlotData(getArtPlace(src.slot), src.slot);
  4642. if(dst.hero == curHero && dst.slot >= Arts::BACKPACK_START)
  4643. setSlotData(getArtPlace(dst.slot), dst.slot);
  4644. if(src.hero == curHero || dst.hero == curHero) //we need to update all slots, artifact might be combined and affect more slots
  4645. updateWornSlots(false);
  4646. if(commonInfo->src == src) //artifact was taken from us
  4647. {
  4648. assert(commonInfo->dst == dst || dst.slot == dst.hero->artifactsInBackpack.size() + Arts::BACKPACK_START);
  4649. commonInfo->reset();
  4650. unmarkSlots();
  4651. updateParentWindow();
  4652. }
  4653. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  4654. {
  4655. assert(dst.slot >= Arts::BACKPACK_START);
  4656. commonInfo->reset();
  4657. CArtPlace *ap = NULL;
  4658. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4659. {
  4660. if(aoh->curHero == dst.hero)
  4661. {
  4662. commonInfo->src.AOH = aoh;
  4663. if((ap = aoh->getArtPlace(dst.slot)))
  4664. break;
  4665. }
  4666. }
  4667. if(ap)
  4668. {
  4669. ap->select();
  4670. }
  4671. else
  4672. {
  4673. commonInfo->src.art = dst.getArt();
  4674. commonInfo->src.slotID = dst.slot;
  4675. assert(commonInfo->src.AOH);
  4676. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[dst.getArt()->artType->id].bitmap);
  4677. markPossibleSlots(dst.getArt());
  4678. updateParentWindow();
  4679. }
  4680. }
  4681. else if(src.slot >= Arts::BACKPACK_START && src.slot < commonInfo->src.slotID && src.hero == commonInfo->src.AOH->curHero) //artifact taken from before currently picked one
  4682. {
  4683. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  4684. commonInfo->src.slotID--;
  4685. assert(commonInfo->src.valid());
  4686. updateParentWindow();
  4687. }
  4688. else
  4689. {
  4690. tlog1 << "Unexpected artifact movement...\n";
  4691. }
  4692. int shift = 0;
  4693. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4694. // shift++;
  4695. //
  4696. if(src.slot < Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4697. shift++;
  4698. if(dst.slot < Arts::BACKPACK_START && src.slot - Arts::BACKPACK_START < backpackPos)
  4699. shift--;
  4700. if( (src.hero == curHero && src.slot >= Arts::BACKPACK_START)
  4701. || (dst.hero == curHero && dst.slot >= Arts::BACKPACK_START) )
  4702. scrollBackpack(shift); //update backpack slots
  4703. }
  4704. CArtPlace * CArtifactsOfHero::getArtPlace(int slot)
  4705. {
  4706. if(slot < Arts::BACKPACK_START)
  4707. {
  4708. return artWorn[slot];
  4709. }
  4710. else
  4711. {
  4712. BOOST_FOREACH(CArtPlace *ap, backpack)
  4713. if(ap->slotID == slot)
  4714. return ap;
  4715. }
  4716. return NULL;
  4717. }
  4718. void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al)
  4719. {
  4720. if(al.hero == curHero)
  4721. updateWornSlots();
  4722. }
  4723. void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al)
  4724. {
  4725. if(al.hero == curHero)
  4726. updateWornSlots();
  4727. }
  4728. void CArtifactsOfHero::updateWornSlots(bool redrawParent /*= true*/)
  4729. {
  4730. for(int i = 0; i < Arts::BACKPACK_START; i++)
  4731. setSlotData(getArtPlace(i), i);
  4732. if(redrawParent)
  4733. updateParentWindow();
  4734. }
  4735. const CGHeroInstance * CArtifactsOfHero::getHero() const
  4736. {
  4737. return curHero;
  4738. }
  4739. void CExchangeWindow::close()
  4740. {
  4741. GH.popIntTotally(this);
  4742. }
  4743. void CExchangeWindow::activate()
  4744. {
  4745. quit->activate();
  4746. garr->activate();
  4747. //artifs[0]->setHero(heroInst[0]);
  4748. artifs[0]->activate();
  4749. //artifs[1]->setHero(heroInst[1]);
  4750. artifs[1]->activate();
  4751. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4752. {
  4753. for(int b=0; b<secSkillAreas[g].size(); ++b)
  4754. {
  4755. secSkillAreas[g][b]->activate();
  4756. }
  4757. }
  4758. for(int b=0; b<primSkillAreas.size(); ++b)
  4759. primSkillAreas[b]->activate();
  4760. GH.statusbar = ourBar;
  4761. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  4762. questlogButton[g]->activate();
  4763. for(int g=0; g<ARRAY_COUNT(morale); g++)
  4764. morale[g]->activate();
  4765. for(int g=0; g<ARRAY_COUNT(luck); g++)
  4766. luck[g]->activate();
  4767. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  4768. portrait[g]->activate();
  4769. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  4770. spellPoints[g]->activate();
  4771. for(int g=0; g<ARRAY_COUNT(experience); g++)
  4772. experience[g]->activate();
  4773. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  4774. speciality[g]->activate();
  4775. }
  4776. void CExchangeWindow::deactivate()
  4777. {
  4778. quit->deactivate();
  4779. garr->deactivate();
  4780. artifs[0]->deactivate();
  4781. artifs[1]->deactivate();
  4782. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4783. {
  4784. for(int b=0; b<secSkillAreas[g].size(); ++b)
  4785. {
  4786. secSkillAreas[g][b]->deactivate();
  4787. }
  4788. }
  4789. for(int b=0; b<primSkillAreas.size(); ++b)
  4790. primSkillAreas[b]->deactivate();
  4791. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  4792. questlogButton[g]->deactivate();
  4793. for(int g=0; g<ARRAY_COUNT(morale); g++)
  4794. morale[g]->deactivate();
  4795. for(int g=0; g<ARRAY_COUNT(luck); g++)
  4796. luck[g]->deactivate();
  4797. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  4798. portrait[g]->deactivate();
  4799. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  4800. spellPoints[g]->deactivate();
  4801. for(int g=0; g<ARRAY_COUNT(experience); g++)
  4802. experience[g]->deactivate();
  4803. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  4804. speciality[g]->deactivate();
  4805. }
  4806. void CExchangeWindow::show(SDL_Surface * to)
  4807. {
  4808. blitAt(bg, pos, to);
  4809. quit->showAll(to);
  4810. //printing border around window
  4811. if(screen->w != 800 || screen->h !=600)
  4812. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  4813. artifs[0]->showAll(to);
  4814. artifs[1]->showAll(to);
  4815. ourBar->showAll(to);
  4816. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4817. {
  4818. questlogButton[g]->showAll(to);//FIXME: for array count(secondary skill) show quest log button? WTF?
  4819. }
  4820. garr->showAll(to);
  4821. }
  4822. void CExchangeWindow::questlog(int whichHero)
  4823. {
  4824. CCS->curh->dragAndDropCursor(NULL);
  4825. }
  4826. void CExchangeWindow::prepareBackground()
  4827. {
  4828. if(bg)
  4829. SDL_FreeSurface(bg);
  4830. SDL_Surface * bgtemp; //loaded as 8bpp surface
  4831. bgtemp = BitmapHandler::loadBitmap("TRADE2.BMP");
  4832. graphics->blueToPlayersAdv(bgtemp, heroInst[0]->tempOwner);
  4833. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  4834. SDL_FreeSurface(bgtemp);
  4835. //printing heroes' names and levels
  4836. std::ostringstream os, os2;
  4837. os<<heroInst[0]->name<<", Level "<<heroInst[0]->level<<" "<<heroInst[0]->type->heroClass->name;
  4838. CSDL_Ext::printAtMiddle(os.str(), 147, 25, FONT_SMALL, zwykly, bg);
  4839. os2<<heroInst[1]->name<<", Level "<<heroInst[1]->level<<" "<<heroInst[1]->type->heroClass->name;
  4840. CSDL_Ext::printAtMiddle(os2.str(), 653, 25, FONT_SMALL, zwykly, bg);
  4841. //printing primary skills
  4842. CDefHandler * skilldef = CDefHandler::giveDef("PSKIL32.DEF");
  4843. for(int g=0; g<4; ++g)
  4844. {
  4845. //graphics
  4846. blitAt(skilldef->ourImages[g].bitmap, genRect(32, 32, 385, 19 + 36 * g), bg);
  4847. }
  4848. CDefHandler * un32 = CDefHandler::giveDef("UN32.DEF");
  4849. //heroes related thing
  4850. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4851. {
  4852. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  4853. //printing primary skills' amounts
  4854. for(int m=0; m<4; ++m)
  4855. {
  4856. std::ostringstream primarySkill;
  4857. primarySkill << heroWArt.getPrimSkillLevel(m);
  4858. CSDL_Ext::printAtMiddle(primarySkill.str(), 352 + 93 * b, 35 + 36 * m, FONT_SMALL, zwykly, bg);
  4859. }
  4860. //printing secondary skills
  4861. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  4862. {
  4863. 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);
  4864. }
  4865. //hero's specialty
  4866. blitAt(un32->ourImages[heroInst[b]->subID].bitmap, 67 + 490*b, 45, bg);
  4867. //experience
  4868. blitAt(skilldef->ourImages[4].bitmap, 103 + 490*b, 45, bg);
  4869. printAtMiddle( makeNumberShort(heroInst[b]->exp), 119 + 490*b, 71, FONT_SMALL, zwykly, bg );
  4870. //mana points
  4871. blitAt(skilldef->ourImages[5].bitmap, 139 + 490*b, 45, bg);
  4872. printAtMiddle( makeNumberShort(heroInst[b]->mana), 155 + 490*b, 71, FONT_SMALL, zwykly, bg );
  4873. //setting morale
  4874. blitAt(graphics->morale30->ourImages[heroWArt.MoraleVal()+3].bitmap, 177 + 490*b, 45, bg);
  4875. //setting luck
  4876. blitAt(graphics->luck30->ourImages[heroWArt.LuckVal()+3].bitmap, 213 + 490*b, 45, bg);
  4877. }
  4878. //printing portraits
  4879. blitAt(graphics->portraitLarge[heroInst[0]->portrait], 257, 13, bg);
  4880. blitAt(graphics->portraitLarge[heroInst[1]->portrait], 485, 13, bg);
  4881. delete un32;
  4882. delete skilldef;
  4883. }
  4884. CExchangeWindow::CExchangeWindow(si32 hero1, si32 hero2) : bg(NULL)
  4885. {
  4886. char bufor[400];
  4887. heroInst[0] = LOCPLINT->cb->getHeroInfo(hero1, 2);
  4888. heroInst[1] = LOCPLINT->cb->getHeroInfo(hero2, 2);
  4889. prepareBackground();
  4890. pos.x = screen->w/2 - bg->w/2;
  4891. pos.y = screen->h/2 - bg->h/2;
  4892. pos.w = screen->w;
  4893. pos.h = screen->h;
  4894. artifs[0] = new CArtifactsOfHero(Point(pos.x + -334, pos.y + 150));
  4895. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  4896. artifs[0]->commonInfo->participants.insert(artifs[0]);
  4897. artifs[0]->setHero(heroInst[0]);
  4898. artifs[1] = new CArtifactsOfHero(Point(pos.x + 96, pos.y + 150));
  4899. artifs[1]->commonInfo = artifs[0]->commonInfo;
  4900. artifs[1]->commonInfo->participants.insert(artifs[1]);
  4901. artifs[1]->setHero(heroInst[1]);
  4902. artSets.push_back(artifs[0]);
  4903. artSets.push_back(artifs[1]);
  4904. //primary skills
  4905. for(int g=0; g<4; ++g)
  4906. {
  4907. //primary skill's clickable areas
  4908. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  4909. primSkillAreas[g]->pos = genRect(32, 140, pos.x+329, pos.y + 19 + 36 * g);
  4910. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  4911. primSkillAreas[g]->type = g;
  4912. primSkillAreas[g]->bonusValue = -1;
  4913. primSkillAreas[g]->baseType = 0;
  4914. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[g].c_str());
  4915. primSkillAreas[g]->hoverText = std::string(bufor);
  4916. }
  4917. //heroes related thing
  4918. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4919. {
  4920. //secondary skill's clickable areas
  4921. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  4922. {
  4923. int skill = heroInst[b]->secSkills[g].first,
  4924. level = heroInst[b]->secSkills[g].second; // <1, 3>
  4925. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  4926. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  4927. secSkillAreas[b][g]->baseType = 1;
  4928. secSkillAreas[b][g]->type = skill;
  4929. secSkillAreas[b][g]->bonusValue = level;
  4930. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  4931. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level - 1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  4932. secSkillAreas[b][g]->hoverText = std::string(bufor);
  4933. }
  4934. portrait[b] = new CHeroArea(pos.x + 257 + 228*b, pos.y + 13, heroInst[b]);
  4935. speciality[b] = new LRClickableAreaWText();
  4936. speciality[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  4937. speciality[b]->hoverText = CGI->generaltexth->heroscrn[27];
  4938. speciality[b]->text = CGI->generaltexth->hTxts[heroInst[b]->subID].longBonus;
  4939. experience[b] = new LRClickableAreaWText();
  4940. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  4941. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  4942. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  4943. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  4944. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  4945. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  4946. spellPoints[b] = new LRClickableAreaWText();
  4947. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  4948. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  4949. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), heroInst[b]->name.c_str(), heroInst[b]->mana, heroInst[b]->manaLimit());
  4950. spellPoints[b]->text = std::string(bufor);
  4951. //setting morale
  4952. morale[b] = new MoraleLuckBox(true, genRect(32, 32, pos.x + 177 + 490*b, pos.y + 45));
  4953. morale[b]->set(heroInst[b]);
  4954. //setting luck
  4955. luck[b] = new MoraleLuckBox(false, genRect(32, 32, pos.x + 213 + 490*b, pos.y + 45));
  4956. luck[b]->set(heroInst[b]);
  4957. }
  4958. //buttons
  4959. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), pos.x+732, pos.y+567, "IOKAY.DEF", SDLK_RETURN);
  4960. questlogButton[0] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 0), pos.x+10, pos.y+44, "hsbtns4.def");
  4961. questlogButton[1] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 1), pos.x+740, pos.y+44, "hsbtns4.def");
  4962. //statusbar
  4963. ourBar = new CStatusBar(pos.x + 3, pos.y + 577, "TSTATBAR.bmp", 726);
  4964. //garrison interface
  4965. garr = new CGarrisonInt(pos.x + 69, pos.y + 131, 4, Point(418,0), bg, Point(69,131), heroInst[0],heroInst[1], true, true);
  4966. garr->addSplitBtn(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+10,pos.y+132,"TSBTNS.DEF"));
  4967. garr->addSplitBtn(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+740,pos.y+132,"TSBTNS.DEF"));
  4968. }
  4969. CExchangeWindow::~CExchangeWindow() //d-tor
  4970. {
  4971. SDL_FreeSurface(bg);
  4972. delete quit;
  4973. //warning: don't experiment with these =NULL lines, they prevent heap corruption!
  4974. delete artifs[0]->commonInfo;
  4975. artifs[0]->commonInfo = NULL;
  4976. delete artifs[0];
  4977. artifs[1]->commonInfo = NULL;
  4978. delete artifs[1];
  4979. delete garr;
  4980. delete ourBar;
  4981. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4982. {
  4983. for(int b=0; b<secSkillAreas[g].size(); ++b)
  4984. {
  4985. delete secSkillAreas[g][b];
  4986. }
  4987. }
  4988. for(int b=0; b<primSkillAreas.size(); ++b)
  4989. {
  4990. delete primSkillAreas[b];
  4991. }
  4992. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  4993. {
  4994. delete questlogButton[g];
  4995. }
  4996. for(int g=0; g<ARRAY_COUNT(morale); g++)
  4997. delete morale[g];
  4998. for(int g=0; g<ARRAY_COUNT(luck); g++)
  4999. delete luck[g];
  5000. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  5001. delete portrait[g];
  5002. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  5003. delete spellPoints[g];
  5004. for(int g=0; g<ARRAY_COUNT(experience); g++)
  5005. delete experience[g];
  5006. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  5007. delete speciality[g];
  5008. }
  5009. void CShipyardWindow::activate()
  5010. {
  5011. build->activate();
  5012. quit->activate();
  5013. }
  5014. void CShipyardWindow::deactivate()
  5015. {
  5016. build->deactivate();
  5017. quit->deactivate();
  5018. }
  5019. void CShipyardWindow::show( SDL_Surface * to )
  5020. {
  5021. blitAt(bg,pos,to);
  5022. Rect clipRect = genRect(64, 96, pos.x+110, pos.y+85);
  5023. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->boatAnims[boat]->ourImages[21 + frame++/8%8].bitmap, NULL, to, &clipRect);
  5024. build->showAll(to);
  5025. quit->showAll(to);
  5026. }
  5027. CShipyardWindow::~CShipyardWindow()
  5028. {
  5029. delete build;
  5030. delete quit;
  5031. }
  5032. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy)
  5033. {
  5034. boat = boatType;
  5035. frame = 0;
  5036. SDL_Surface * bgtemp; //loaded as 8bpp surface
  5037. bgtemp = BitmapHandler::loadBitmap("TPSHIP.bmp");
  5038. pos.x = screen->w/2 - bgtemp->w/2;
  5039. pos.y = screen->h/2 - bgtemp->h/2;
  5040. pos.w = bgtemp->w;
  5041. pos.h = bgtemp->h;
  5042. SDL_SetColorKey(bgtemp,SDL_SRCCOLORKEY,SDL_MapRGB(bgtemp->format,0,255,255));
  5043. graphics->blueToPlayersAdv(bgtemp, LOCPLINT->playerID);
  5044. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  5045. SDL_FreeSurface(bgtemp);
  5046. bgtemp = BitmapHandler::loadBitmap("TPSHIPBK.bmp");
  5047. blitAt(bgtemp, 100, 69, bg);
  5048. SDL_FreeSurface(bgtemp);
  5049. // Draw resource icons and costs.
  5050. std::string goldCost = boost::lexical_cast<std::string>(1000);
  5051. std::string woodCost = boost::lexical_cast<std::string>(10);
  5052. blitAt(graphics->resources32->ourImages[6].bitmap, 100, 244, bg);
  5053. printAtMiddle(goldCost.c_str(), 118, 294, FONT_SMALL, zwykly, bg);
  5054. blitAt(graphics->resources32->ourImages[0].bitmap, 196, 244, bg);
  5055. printAtMiddle(woodCost.c_str(), 212, 294, FONT_SMALL, zwykly, bg);
  5056. bool affordable = true;
  5057. for(int i = 0; i < cost.size(); i++)
  5058. {
  5059. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  5060. {
  5061. affordable = false;
  5062. break;
  5063. }
  5064. }
  5065. quit = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+224, pos.y+312, "ICANCEL.DEF", SDLK_RETURN);
  5066. build = new AdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+42, pos.y+312, "IBY6432.DEF", SDLK_RETURN);
  5067. build->callback += onBuy;
  5068. if(!affordable)
  5069. build->block(true);
  5070. printAtMiddle(CGI->generaltexth->jktexts[13], 164, 27, FONT_BIG, tytulowy, bg); //Build A New Ship
  5071. printAtMiddle(CGI->generaltexth->jktexts[14], 164, 220, FONT_MEDIUM, zwykly, bg); //Resource cost:
  5072. }
  5073. CPuzzleWindow::CPuzzleWindow(const int3 &grailPos, float discoveredRatio)
  5074. :animCount(0)
  5075. {
  5076. SDL_Surface * back = BitmapHandler::loadBitmap("PUZZLE.BMP", false);
  5077. graphics->blueToPlayersAdv(back, LOCPLINT->playerID);
  5078. //make transparency black
  5079. back->format->palette->colors[0].b = back->format->palette->colors[0].r = back->format->palette->colors[0].g = 0;
  5080. //the rest
  5081. background = SDL_ConvertSurface(back, screen->format, back->flags);
  5082. SDL_FreeSurface(back);
  5083. pos = genRect(background->h, background->w, (conf.cc.resx - background->w) / 2, (conf.cc.resy - background->h) / 2);
  5084. quitb = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+670, pos.y+538, "IOK6432.DEF", SDLK_RETURN);
  5085. quitb->assignedKeys.insert(SDLK_ESCAPE);
  5086. resdatabar = new CResDataBar("ZRESBAR.bmp", pos.x+3, pos.y+575, 32, 2, 85, 85);
  5087. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  5088. //printing necessary things to background
  5089. int3 moveInt = int3(8, 9, 0);
  5090. Rect mapRect = genRect(544, 591, 8, 8);
  5091. CGI->mh->terrainRect
  5092. (grailPos - moveInt, adventureInt->anim,
  5093. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  5094. background, &mapRect, 0, 0, true, moveInt);
  5095. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  5096. std::vector<SPuzzleInfo> puzzlesToPrint;
  5097. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  5098. {
  5099. if(CGI->heroh->puzzleInfo[faction][g].whenUncovered > PUZZLES_PER_FACTION * discoveredRatio)
  5100. {
  5101. puzzlesToPrint.push_back(CGI->heroh->puzzleInfo[faction][g]);
  5102. }
  5103. else
  5104. {
  5105. SDL_Surface *buf = BitmapHandler::loadBitmap(CGI->heroh->puzzleInfo[faction][g].filename);
  5106. puzzlesToPullBack.push_back( std::make_pair(buf, &CGI->heroh->puzzleInfo[faction][g]) );
  5107. }
  5108. }
  5109. for(int b = 0; b < puzzlesToPrint.size(); ++b)
  5110. {
  5111. SDL_Surface * puzzle = BitmapHandler::loadBitmap(puzzlesToPrint[b].filename);
  5112. blitAt(puzzle, puzzlesToPrint[b].x, puzzlesToPrint[b].y, background);
  5113. SDL_FreeSurface(puzzle);
  5114. }
  5115. }
  5116. CPuzzleWindow::~CPuzzleWindow()
  5117. {
  5118. delete quitb;
  5119. delete resdatabar;
  5120. SDL_FreeSurface(background);
  5121. for(int g = 0; g < puzzlesToPullBack.size(); ++g)
  5122. SDL_FreeSurface( puzzlesToPullBack[g].first );
  5123. }
  5124. void CPuzzleWindow::activate()
  5125. {
  5126. quitb->activate();
  5127. }
  5128. void CPuzzleWindow::deactivate()
  5129. {
  5130. quitb->deactivate();
  5131. }
  5132. void CPuzzleWindow::show(SDL_Surface * to)
  5133. {
  5134. blitAt(background, pos.x, pos.y, to);
  5135. quitb->showAll(to);
  5136. resdatabar->draw(to);
  5137. //blitting disappearing puzzles
  5138. for(int g=0; g<2; ++g)
  5139. if(animCount != 255)
  5140. ++animCount;
  5141. if(animCount != 255)
  5142. {
  5143. for(int b = 0; b < puzzlesToPullBack.size(); ++b)
  5144. {
  5145. int xPos = puzzlesToPullBack[b].second->x + pos.x,
  5146. yPos = puzzlesToPullBack[b].second->y + pos.y;
  5147. SDL_Surface *from = puzzlesToPullBack[b].first;
  5148. SDL_SetAlpha(from, SDL_SRCALPHA, 255 - animCount);
  5149. blitAt(from, xPos, yPos, to);
  5150. }
  5151. }
  5152. //disappearing puzzles blitted
  5153. //printing border around window
  5154. if(screen->w != 800 || screen->h !=600)
  5155. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  5156. }
  5157. void CTransformerWindow::CItem::showAll(SDL_Surface * to)
  5158. {
  5159. SDL_Surface * backgr = graphics->bigImgs[parent->army->getCreature(id)->idNumber];
  5160. blitAt(backgr, pos.x, pos.y, to);
  5161. printAtMiddle(boost::lexical_cast<std::string>(size),pos.x+28, pos.y+76,FONT_SMALL,zwykly,to);//stack size
  5162. }
  5163. void CTransformerWindow::CItem::move()
  5164. {
  5165. if (left)
  5166. pos.x += 289;
  5167. else
  5168. pos.x -= 289;
  5169. left = !left;
  5170. }
  5171. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  5172. {
  5173. if(previousState && (!down))
  5174. {
  5175. move();
  5176. parent->showAll(screen2);
  5177. }
  5178. }
  5179. CTransformerWindow::CItem::CItem(CTransformerWindow * _parent, int _size, int _id):
  5180. id(_id), size(_size), parent(_parent)
  5181. {
  5182. used = LCLICK;
  5183. left = true;
  5184. pos.w = 58;
  5185. pos.h = 64;
  5186. pos.x += 45 + (id%3)*83 + id/6*83;
  5187. pos.y += 109 + (id/3)*98;
  5188. }
  5189. CTransformerWindow::CItem::~CItem()
  5190. {
  5191. }
  5192. void CTransformerWindow::showAll(SDL_Surface * to)
  5193. {
  5194. CIntObject::showAll(to);
  5195. printAtMiddleLoc( CGI->generaltexth->allTexts[485], 153, 29,FONT_SMALL, tytulowy,to);//holding area
  5196. printAtMiddleLoc( CGI->generaltexth->allTexts[486], 153+295, 29,FONT_SMALL, tytulowy,to);//transformer
  5197. printAtMiddleWBLoc(CGI->generaltexth->allTexts[487], 153, 75,FONT_MEDIUM, 32,tytulowy,to);//move creatures to create skeletons
  5198. printAtMiddleWBLoc(CGI->generaltexth->allTexts[488], 153+295, 75,FONT_MEDIUM, 32,tytulowy,to);//creatures here will become skeletons
  5199. }
  5200. void CTransformerWindow::makeDeal()
  5201. {
  5202. for (int i=0; i<items.size(); i++)
  5203. if (!items[i]->left)
  5204. LOCPLINT->cb->trade(town, CREATURE_UNDEAD, items[i]->id, 0, 0, hero);
  5205. }
  5206. void CTransformerWindow::addAll()
  5207. {
  5208. for (int i=0; i<items.size(); i++)
  5209. if (items[i]->left)
  5210. items[i]->move();
  5211. showAll(screen2);
  5212. }
  5213. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):hero(_hero),town(_town)
  5214. {
  5215. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5216. bg = new CPicture ("SKTRNBK.PCX");
  5217. bg->colorizeAndConvert(LOCPLINT->playerID);
  5218. pos = center(bg->pos);
  5219. if (hero)
  5220. army = hero;
  5221. else
  5222. army = town;
  5223. for (int i=0; i<7; i++ )
  5224. if ( army->getCreature(i) )
  5225. items.push_back(new CItem(this, army->getStackCount(i), i));
  5226. all = new AdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  5227. convert= new AdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  5228. cancel = new AdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CGuiHandler::popIntTotally,&GH, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  5229. bar = new CGStatusBar(304, 469);
  5230. }
  5231. CTransformerWindow::~CTransformerWindow()
  5232. {
  5233. }
  5234. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  5235. {
  5236. if(previousState && (!down))
  5237. {
  5238. if ( state() != 2 )
  5239. return;
  5240. CUnivConfirmWindow *win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(6) >= 2000);
  5241. GH.pushInt(win);
  5242. }
  5243. }
  5244. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  5245. {
  5246. if(down)
  5247. {
  5248. CInfoPopup *message = new CInfoPopup();
  5249. message->free = true;
  5250. message->bitmap = CMessage::drawBoxTextBitmapSub
  5251. (LOCPLINT->playerID,
  5252. CGI->generaltexth->skillInfoTexts[ID][0],
  5253. graphics->abils82->ourImages[ID*3+3].bitmap,
  5254. CGI->generaltexth->skillName[ID]);
  5255. message->pos.x = screen->w/2 - message->bitmap->w/2;
  5256. message->pos.y = screen->h/2 - message->bitmap->h/2;
  5257. GH.pushInt(message);
  5258. }
  5259. }
  5260. void CUniversityWindow::CItem::hover(bool on)
  5261. {
  5262. if (on)
  5263. GH.statusbar->print(CGI->generaltexth->skillName[ID]);
  5264. else
  5265. GH.statusbar->clear();
  5266. }
  5267. int CUniversityWindow::CItem::state()
  5268. {
  5269. if (parent->hero->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ID)))//hero know this skill
  5270. return 1;
  5271. if (parent->hero->secSkills.size() >= SKILL_PER_HERO)//can't learn more skills
  5272. return 0;
  5273. if (parent->hero->type->heroClass->proSec[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  5274. return 0;
  5275. /* if (LOCPLINT->cb->getResourceAmount(6) < 2000 )//no gold - allowed in H3, confirm button is blocked instead
  5276. return 0;*/
  5277. return 2;
  5278. }
  5279. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  5280. {
  5281. CPicture * bar;
  5282. switch (state())
  5283. {
  5284. case 0: bar = parent->red;
  5285. break;
  5286. case 1: bar = parent->yellow;
  5287. break;
  5288. case 2: bar = parent->green;
  5289. break;
  5290. default:bar = NULL;
  5291. break;
  5292. }
  5293. assert(bar);
  5294. blitAtLoc(bar->bg, -28, -22, to);
  5295. blitAtLoc(bar->bg, -28, 48, to);
  5296. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, zwykly,to);//Name
  5297. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, zwykly,to);//Level(always basic)
  5298. CPicture::showAll(to);
  5299. }
  5300. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  5301. CPicture (graphics->abils44->ourImages[_ID*3+3].bitmap,X,Y,false),ID(_ID), parent(_parent)
  5302. {
  5303. used = LCLICK | RCLICK | HOVER;
  5304. }
  5305. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):hero(_hero), market(_market)
  5306. {
  5307. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5308. bg = new CPicture("UNIVERS1.PCX");
  5309. bg->colorizeAndConvert(LOCPLINT->playerID);
  5310. green = new CPicture("UNIVGREN.PCX");
  5311. yellow = new CPicture("UNIVGOLD.PCX");//bars
  5312. red = new CPicture("UNIVRED.PCX");
  5313. green->recActions =
  5314. yellow->recActions =
  5315. red->recActions = DISPOSE;
  5316. if ( market->o->ID == 104 ) // this is adventure map university
  5317. {
  5318. SDL_Surface * titleImage = BitmapHandler::loadBitmap("UNIVBLDG.PCX");
  5319. blitAtLoc(titleImage, 232-titleImage->w/2, 76-titleImage->h/2, bg->bg);
  5320. SDL_FreeSurface(titleImage);
  5321. }
  5322. else if (LOCPLINT->castleInt && LOCPLINT->castleInt->town->subID == 8)// this is town university
  5323. {
  5324. SDL_Surface * titleImage = LOCPLINT->castleInt->bicons->ourImages[21].bitmap;
  5325. blitAtLoc(titleImage, 232-titleImage->w/2, 76-titleImage->h/2, bg->bg);
  5326. }
  5327. else
  5328. tlog0<<"Error: Image for university was not found!\n";//This should not happen
  5329. printAtMiddleWBLoc(CGI->generaltexth->allTexts[603], 232, 153, FONT_SMALL, 70,zwykly,bg->bg);//Clerk speech
  5330. printAtMiddleLoc (CGI->generaltexth->allTexts[602], 231, 26 , FONT_MEDIUM ,tytulowy,bg->bg);//University
  5331. std::vector<int> list = market->availableItemsIds(RESOURCE_SKILL);
  5332. if (list.size()!=4)
  5333. tlog0<<"\t\tIncorrect size of available items vector!\n";
  5334. for (int i=0; i<list.size(); i++)//prepare clickable items
  5335. items.push_back(new CItem(this, list[i], pos.x+54+i*104, pos.y+234));
  5336. pos = center(bg->pos);
  5337. cancel = new AdventureMapButton(CGI->generaltexth->zelp[632],
  5338. boost::bind(&CGuiHandler::popIntTotally,&GH, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  5339. bar = new CGStatusBar(232, 371);
  5340. }
  5341. CUniversityWindow::~CUniversityWindow()
  5342. {
  5343. }
  5344. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):parent(PARENT)
  5345. {
  5346. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5347. bg = new CPicture("UNIVERS2.PCX");
  5348. bg->colorizeAndConvert(LOCPLINT->playerID);
  5349. std::string text = CGI->generaltexth->allTexts[608];
  5350. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  5351. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  5352. boost::replace_first(text, "%d", "2000");
  5353. printAtMiddleWBLoc(text, 230, 163, FONT_SMALL, 65,zwykly,bg->bg);//Clerk speech
  5354. printAtMiddleLoc (CGI->generaltexth-> skillName[SKILL], 230, 37, FONT_SMALL, zwykly,bg->bg);//Skill name
  5355. printAtMiddleLoc (CGI->generaltexth->levels[1], 230, 107, FONT_SMALL, zwykly,bg->bg);//Skill level
  5356. printAtMiddleLoc ("2000", 230, 267, FONT_SMALL, zwykly,bg->bg);//Cost
  5357. blitAtLoc(graphics->abils44->ourImages[SKILL*3+3].bitmap, 211, 51, bg->bg);//skill
  5358. blitAtLoc(graphics->resources32->ourImages[6].bitmap, 210, 210, bg->bg);//gold
  5359. pos = center(bg->pos);
  5360. std::string hoverText = CGI->generaltexth->allTexts[609];
  5361. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  5362. text = CGI->generaltexth->zelp[633].second;
  5363. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  5364. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  5365. boost::replace_first(text, "%d", "2000");
  5366. confirm= new AdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  5367. 148,299,"IBY6432.DEF",SDLK_RETURN);
  5368. confirm->block(!available);
  5369. cancel = new AdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CGuiHandler::popIntTotally, &GH, this),
  5370. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  5371. bar = new CGStatusBar(232, 371);
  5372. }
  5373. void CUnivConfirmWindow::makeDeal(int skill)
  5374. {
  5375. LOCPLINT->cb->trade(parent->market->o, RESOURCE_SKILL, 6, skill, 1, parent->hero);
  5376. GH.popIntTotally(this);
  5377. }
  5378. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  5379. fort(object),hero(visitor)
  5380. {
  5381. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5382. slotsCount=7;
  5383. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  5384. bg = new CPicture("APHLFTBK.PCX");
  5385. bg->colorizeAndConvert(LOCPLINT->playerID);
  5386. printAtMiddleLoc (fort->hoverName, 325, 32, FONT_BIG, tytulowy, bg->bg);//Hill Fort
  5387. pos = center(bg->pos);
  5388. heroPic = new CHeroArea(30, 60, hero);
  5389. currState.resize(slotsCount+1);
  5390. costs.resize(slotsCount);
  5391. totalSumm.resize(RESOURCE_QUANTITY);
  5392. std::vector<std::string> files;
  5393. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  5394. for (int i=0; i<slotsCount; i++)
  5395. {
  5396. currState[i] = getState(i);
  5397. upgrade[i] = new AdventureMapButton(getTextForSlot(i),"",boost::bind(&CHillFortWindow::makeDeal, this, i),
  5398. 107+i*76, 171, "", SDLK_1+i, &files);
  5399. upgrade[i]->block(currState[i] == -1);
  5400. }
  5401. files.clear();
  5402. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  5403. currState[slotsCount] = getState(slotsCount);
  5404. upgradeAll = new AdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, slotsCount),
  5405. 30, 231, "", SDLK_0, &files);
  5406. quit = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  5407. bar = new CGStatusBar(327, 332);
  5408. garr = new CGarrisonInt(108, 60, 18, Point(),bg->bg,Point(108,60),hero,NULL);
  5409. updateGarrisons();
  5410. }
  5411. CHillFortWindow::~CHillFortWindow()
  5412. {
  5413. }
  5414. void CHillFortWindow::activate()
  5415. {
  5416. CIntObject::activate();
  5417. GH.statusbar = bar;
  5418. }
  5419. void CHillFortWindow::updateGarrisons()
  5420. {
  5421. for (int i=0; i<RESOURCE_QUANTITY; i++)
  5422. totalSumm[i]=0;
  5423. for (int i=0; i<slotsCount; i++)
  5424. {
  5425. costs[i].clear();
  5426. int newState = getState(i);
  5427. if (newState != -1)
  5428. {
  5429. UpgradeInfo info = LOCPLINT->cb->getUpgradeInfo(hero, i);
  5430. if (info.newID.size())//we have upgrades here - update costs
  5431. for(std::set<std::pair<int,int> >::iterator it=info.cost[0].begin(); it!=info.cost[0].end(); it++)
  5432. {
  5433. std::pair<int, int> pair = std::make_pair(it->first, it->second * hero->getStackCount(i) );
  5434. costs[i].insert(pair);
  5435. totalSumm[pair.first] += pair.second;
  5436. }
  5437. }
  5438. currState[i] = newState;
  5439. upgrade[i]->setIndex(newState);
  5440. upgrade[i]->block(currState[i] == -1);
  5441. upgrade[i]->hoverTexts[0] = getTextForSlot(i);
  5442. }
  5443. int newState = getState(slotsCount);
  5444. currState[slotsCount] = newState;
  5445. upgradeAll->setIndex(newState);
  5446. garr->recreateSlots();
  5447. }
  5448. void CHillFortWindow::makeDeal(int slot)
  5449. {
  5450. int offset = (slot == slotsCount)?2:0;
  5451. switch (currState[slot])
  5452. {
  5453. case 0:
  5454. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  5455. std::vector<SComponent*>(), soundBase::sound_todo);
  5456. break;
  5457. case 1:
  5458. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  5459. std::vector<SComponent*>(), soundBase::sound_todo);
  5460. break;
  5461. case 2:
  5462. for (int i=0; i<slotsCount; i++)
  5463. if ( slot ==i || ( slot == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  5464. {
  5465. UpgradeInfo info = LOCPLINT->cb->getUpgradeInfo(hero, i);
  5466. LOCPLINT->cb->upgradeCreature(hero, i, info.newID[0]);
  5467. }
  5468. break;
  5469. }
  5470. }
  5471. void CHillFortWindow::showAll (SDL_Surface *to)
  5472. {
  5473. CIntObject::showAll(to);
  5474. for ( int i=0; i<slotsCount; i++)
  5475. {
  5476. if ( currState[i] == 0 || currState[i] == 2 )
  5477. {
  5478. if ( costs[i].size() )//we have several elements
  5479. {
  5480. int curY = 128;//reverse iterator is used to display gold as first element
  5481. for( std::map<int,int>::reverse_iterator rit=costs[i].rbegin(); rit!=costs[i].rend(); rit++)
  5482. {
  5483. blitAtLoc(resources->ourImages[rit->first].bitmap, 104+76*i, curY, to);
  5484. printToLoc(boost::lexical_cast<std::string>(rit->second), 168+76*i, curY+16, FONT_SMALL, zwykly, to);
  5485. curY += 20;
  5486. }
  5487. }
  5488. else//free upgrade - print gold image and "Free" text
  5489. {
  5490. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  5491. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, zwykly, to);
  5492. }
  5493. }
  5494. }
  5495. for (int i=0; i<RESOURCE_QUANTITY; i++)
  5496. {
  5497. if (totalSumm[i])//this resource is used - display it
  5498. {
  5499. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  5500. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, zwykly, to);
  5501. }
  5502. }
  5503. }
  5504. std::string CHillFortWindow::getTextForSlot(int slot)
  5505. {
  5506. if ( !hero->getCreature(slot) )//we dont have creature here
  5507. return "";
  5508. std::string str = CGI->generaltexth->allTexts[318];
  5509. int amount = hero->getStackCount(slot);
  5510. if ( amount == 1 )
  5511. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  5512. else
  5513. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  5514. return str;
  5515. }
  5516. int CHillFortWindow::getState(int slot)
  5517. {
  5518. if ( slot == slotsCount )//"Upgrade all" slot
  5519. {
  5520. bool allUpgraded = true;//All creatures are upgraded?
  5521. for (int i=0; i<slotsCount; i++)
  5522. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  5523. if (allUpgraded)
  5524. return 1;
  5525. for ( int i=0; i<RESOURCE_QUANTITY; i++)//if we need more resources
  5526. if(LOCPLINT->cb->getResourceAmount(i) < totalSumm[i])
  5527. return 0;
  5528. return 2;
  5529. }
  5530. if (hero->slotEmpty(slot))//no creature here
  5531. return -1;
  5532. UpgradeInfo info = LOCPLINT->cb->getUpgradeInfo(hero, slot);
  5533. if (!info.newID.size())//already upgraded
  5534. return 1;
  5535. for(std::set<std::pair<int,int> >::iterator it=info.cost[0].begin(); it!=info.cost[0].end(); it++)
  5536. if(LOCPLINT->cb->getResourceAmount(it->first) < it->second * hero->getStackCount(slot))
  5537. return 0;
  5538. return 2;//can upgrade
  5539. }
  5540. void CThievesGuildWindow::activate()
  5541. {
  5542. CIntObject::activate();
  5543. GH.statusbar = statusBar;
  5544. }
  5545. void CThievesGuildWindow::show(SDL_Surface * to)
  5546. {
  5547. blitAt(background, pos.x, pos.y, to);
  5548. statusBar->show(to);
  5549. exitb->showAll(to);
  5550. resdatabar->show(to);
  5551. //showing border around window
  5552. if(screen->w != 800 || screen->h !=600)
  5553. {
  5554. CMessage::drawBorder(LOCPLINT->playerID, to, pos.w + 28, pos.h + 28, pos.x-14, pos.y-15);
  5555. }
  5556. }
  5557. void CThievesGuildWindow::bexitf()
  5558. {
  5559. GH.popIntTotally(this);
  5560. }
  5561. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner)
  5562. :owner(_owner)
  5563. {
  5564. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5565. SThievesGuildInfo tgi; //info to be displayed
  5566. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  5567. pos = center(Rect(0,0,800,600));// Rect( (conf.cc.resx - 800) / 2, (conf.cc.resy - 600) / 2, 800, 600 );
  5568. //loading backround and converting to more bpp form
  5569. SDL_Surface * bg = background = BitmapHandler::loadBitmap("TpRank.bmp", false);
  5570. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  5571. background = newSurface(bg->w, bg->h);
  5572. blitAt(bg, 0, 0, background);
  5573. SDL_FreeSurface(bg);
  5574. exitb = new AdventureMapButton (std::string(), std::string(), boost::bind(&CThievesGuildWindow::bexitf,this), 748, 556, "TPMAGE1.def", SDLK_RETURN);
  5575. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  5576. resdatabar = new CMinorResDataBar();
  5577. resdatabar->pos.x += pos.x;
  5578. resdatabar->pos.y += pos.y;
  5579. static std::vector< std::list< ui8 > > SThievesGuildInfo::* fields[] = { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  5580. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks, &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army,
  5581. &SThievesGuildInfo::income};
  5582. //printing texts & descriptions to background
  5583. for(int g=0; g<12; ++g)
  5584. {
  5585. int y;
  5586. if(g == 9) //best hero
  5587. {
  5588. y = 400;
  5589. }
  5590. else if(g == 10) //personality
  5591. {
  5592. y = 460;
  5593. }
  5594. else if(g == 11) //best monster
  5595. {
  5596. y = 510;
  5597. }
  5598. else
  5599. {
  5600. y = 52 + 32*g;
  5601. }
  5602. std::string text = CGI->generaltexth->jktexts[24+g];
  5603. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  5604. printAtMiddle(text, 135, y, FONT_MEDIUM, tytulowy, background);
  5605. }
  5606. CDefHandler * strips = CDefHandler::giveDef("PRSTRIPS.DEF");
  5607. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  5608. for(int g=0; g<tgi.playerColors.size(); ++g)
  5609. {
  5610. if(g > 0)
  5611. {
  5612. blitAt(strips->ourImages[g-1].bitmap, 250 + 66*g, 7, background);
  5613. }
  5614. printAtMiddle(CGI->generaltexth->jktexts[16+g], 283 + 66*g, 24, FONT_BIG, tytulowy, background);
  5615. SDL_Surface * box = BitmapHandler::loadBitmap(colorToBox[tgi.playerColors[g]]);
  5616. blitAt(box, 253 + 66*g, 334, background);
  5617. SDL_FreeSurface(box);
  5618. }
  5619. delete strips;
  5620. CDefHandler * flagPictures = CDefHandler::giveDef("itgflags.def");
  5621. //printing flags
  5622. for(int g=0; g<ARRAY_COUNT(fields); ++g) //by lines
  5623. {
  5624. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  5625. {
  5626. std::list<ui8> players = (tgi .* fields[g])[b]; //get players with this place in this line
  5627. //std::sort(players.begin(), players.end());
  5628. int counter = 0;
  5629. for(std::list<ui8>::const_iterator it = players.begin(); it != players.end(); ++it)
  5630. {
  5631. int xpos = 259 + 66 * b + 12 * (counter % 4) + 6 * (counter / 4);
  5632. int ypos = 41 + 32 * g + 4 * (counter / 4);
  5633. blitAt(flagPictures->ourImages[*it].bitmap, xpos, ypos, background);
  5634. counter++;
  5635. }
  5636. }
  5637. }
  5638. delete flagPictures;
  5639. flagPictures = NULL;
  5640. //printing best hero
  5641. int counter = 0;
  5642. for(std::map<ui8, InfoAboutHero>::const_iterator it = tgi.colorToBestHero.begin(); it != tgi.colorToBestHero.end(); ++it)
  5643. {
  5644. if(it->second.portrait >= 0)
  5645. blitAt(graphics->portraitSmall[it->second.portrait], 260 + 66 * counter, 360, background);
  5646. counter++;
  5647. //printing stats
  5648. if(it->second.details)
  5649. {
  5650. printAtWB(CGI->generaltexth->allTexts[184], 191 + 66*counter, 396, FONT_TINY, 10, zwykly, background);
  5651. for (int i=0; i<it->second.details->primskills.size(); ++i)
  5652. {
  5653. std::ostringstream skill;
  5654. skill << it->second.details->primskills[i];
  5655. printTo(skill.str(), 244 + 66 * counter, 407 + 11*i, FONT_TINY, zwykly, background);
  5656. }
  5657. }
  5658. }
  5659. //printing best creature
  5660. counter = 0;
  5661. for(std::map<ui8, si32>::const_iterator it = tgi.bestCreature.begin(); it != tgi.bestCreature.end(); ++it)
  5662. {
  5663. if(it->second >= 0)
  5664. blitAt(graphics->bigImgs[it->second], 255 + 66 * counter, 479, background);
  5665. counter++;
  5666. }
  5667. //printing personality
  5668. counter = 0;
  5669. for(std::map<ui8, si8>::const_iterator it = tgi.personality.begin(); it != tgi.personality.end(); ++it)
  5670. {
  5671. int toPrint = 0;
  5672. if(it->second == -1)
  5673. {
  5674. toPrint = 172;
  5675. }
  5676. else
  5677. {
  5678. toPrint = 168 + it->second;
  5679. }
  5680. printAtMiddle(CGI->generaltexth->arraytxt[toPrint], 283 + 66*counter, 459, FONT_SMALL, zwykly, background);
  5681. counter++;
  5682. }
  5683. }
  5684. CThievesGuildWindow::~CThievesGuildWindow()
  5685. {
  5686. SDL_FreeSurface(background);
  5687. // delete exitb;
  5688. // delete statusBar;
  5689. // delete resdatabar;
  5690. }
  5691. void MoraleLuckBox::set(const IBonusBearer *node)
  5692. {
  5693. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  5694. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  5695. const int neutralDescr[] = {60, 86}; //eg {Neutral Morale} \n\n Neutral morale means your armies will neither be blessed with extra attacks or freeze in combat.
  5696. const int componentType[] = {SComponent::luck, SComponent::morale};
  5697. const int hoverTextBase[] = {7, 4};
  5698. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  5699. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  5700. int mrlt = -9;
  5701. TModDescr mrl;
  5702. if (node)
  5703. {
  5704. node->getModifiersWDescr(mrl, bonusType[morale]);
  5705. bonusValue = (node->*getValue[morale])();
  5706. }
  5707. else
  5708. bonusValue = 0;
  5709. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  5710. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  5711. baseType = componentType[morale];
  5712. text = CGI->generaltexth->arraytxt[textId[morale]];
  5713. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  5714. if (!mrl.size())
  5715. text += CGI->generaltexth->arraytxt[noneTxtId];
  5716. else
  5717. {
  5718. if (node->hasBonusOfType (Bonus::UNDEAD) || node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING)) //it's a creature window
  5719. {
  5720. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  5721. }
  5722. else
  5723. {
  5724. for(int it=0; it < mrl.size(); it++)
  5725. text += "\n" + mrl[it].second;
  5726. }
  5727. }
  5728. }
  5729. void MoraleLuckBox::showAll(SDL_Surface * to)
  5730. {
  5731. CDefEssential *def = morale ? graphics->morale42 : graphics->luck42;
  5732. SDL_Surface *img = def->ourImages[bonusValue + 3].bitmap;
  5733. blitAt(img, Rect(img).centerIn(pos), to); //put img in the center of our pos
  5734. }
  5735. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r)
  5736. :morale(Morale)
  5737. {
  5738. bonusValue = 0;
  5739. pos = r + pos;
  5740. }
  5741. MoraleLuckBox::~MoraleLuckBox()
  5742. {
  5743. }
  5744. void CLabel::showAll(SDL_Surface * to)
  5745. {
  5746. CIntObject::showAll(to);
  5747. std::string *hlpText = NULL; //if NULL, text field will be used
  5748. if(ignoreLeadingWhitespace)
  5749. {
  5750. hlpText = new std::string(text);
  5751. boost::trim_left(*hlpText);
  5752. }
  5753. std::string &toPrint = hlpText ? *hlpText : text;
  5754. if(!toPrint.length())
  5755. return;
  5756. static void (*printer[3])(const std::string &, int, int, EFonts, SDL_Color, SDL_Surface *) = {&CSDL_Ext::printAt, &CSDL_Ext::printAtMiddle, &CSDL_Ext::printTo}; //array of printing functions
  5757. printer[alignment](toPrint, pos.x + textOffset.x, pos.y + textOffset.y, font, color, to);
  5758. }
  5759. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= zwykly*/, const std::string &Text /*= ""*/)
  5760. :alignment(Align), font(Font), color(Color), text(Text)
  5761. {
  5762. autoRedraw = true;
  5763. pos.x += x;
  5764. pos.y += y;
  5765. pos.w = pos.h = 0;
  5766. bg = NULL;
  5767. }
  5768. void CLabel::setTxt(const std::string &Txt)
  5769. {
  5770. text = Txt;
  5771. if(autoRedraw)
  5772. {
  5773. if(bg || !parent)
  5774. redraw();
  5775. else
  5776. parent->redraw();
  5777. }
  5778. }
  5779. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= zwykly*/)
  5780. :CLabel(rect.x, rect.y, Font, Align, Color, Text), sliderStyle(SliderStyle), slider(NULL)
  5781. {
  5782. redrawParentOnScrolling = false;
  5783. autoRedraw = false;
  5784. pos.h = rect.h;
  5785. pos.w = rect.w;
  5786. assert(Align == TOPLEFT || Align == CENTER); //TODO: support for other alignments
  5787. assert(pos.w >= 80); //we need some space
  5788. setTxt(Text);
  5789. }
  5790. void CTextBox::showAll(SDL_Surface * to)
  5791. {
  5792. CIntObject::showAll(to);
  5793. const Font &f = *graphics->fonts[font];
  5794. int dy = f.height; //line height
  5795. int base_y = pos.y;
  5796. if(alignment == CENTER)
  5797. base_y += std::max((pos.h - maxH)/2,0);
  5798. int howManyLinesToPrint = slider ? slider->capacity : lines.size();
  5799. int firstLineToPrint = slider ? slider->value : 0;
  5800. for (int i = 0; i < howManyLinesToPrint; i++)
  5801. {
  5802. const std::string &line = lines[i + firstLineToPrint];
  5803. if(!line.size()) continue;
  5804. int x = pos.x;
  5805. if(alignment == CENTER)
  5806. {
  5807. x += (pos.w - f.getWidth(line.c_str())) / 2;
  5808. if(slider)
  5809. x -= slider->pos.w / 2 + 5;
  5810. }
  5811. if(line[0] == '{' && line[line.size()-1] == '}')
  5812. printAt(line, x, base_y + i*dy, font, tytulowy, to);
  5813. else
  5814. printAt(line, x, base_y + i*dy, font, color, to);
  5815. }
  5816. }
  5817. void CTextBox::setTxt(const std::string &Txt)
  5818. {
  5819. recalculateLines(Txt);
  5820. CLabel::setTxt(Txt);
  5821. }
  5822. void CTextBox::sliderMoved(int to)
  5823. {
  5824. if(!slider)
  5825. return;
  5826. if(redrawParentOnScrolling)
  5827. parent->redraw();
  5828. redraw();
  5829. }
  5830. void CTextBox::setBounds(int limitW, int limitH)
  5831. {
  5832. pos.h = limitH;
  5833. pos.w = limitW;
  5834. recalculateLines(text);
  5835. }
  5836. void CTextBox::recalculateLines(const std::string &Txt)
  5837. {
  5838. delChildNUll(slider, true);
  5839. lines.clear();
  5840. const Font &f = *graphics->fonts[font];
  5841. int lineHeight = f.height;
  5842. int lineCapacity = pos.h / lineHeight;
  5843. lines = CMessage::breakText(Txt, pos.w, font);
  5844. if(lines.size() > lineCapacity) //we need to add a slider
  5845. {
  5846. lines = CMessage::breakText(Txt, pos.w - 32 - 10, font);
  5847. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5848. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1), lineCapacity, lines.size(), 0, false, sliderStyle);
  5849. if(active)
  5850. slider->activate();
  5851. }
  5852. maxH = lineHeight * lines.size();
  5853. maxW = 0;
  5854. BOOST_FOREACH(const std::string &line, lines)
  5855. amax(maxW, f.getWidth(line.c_str()));
  5856. }
  5857. void CGStatusBar::print(const std::string & Text)
  5858. {
  5859. setTxt(Text);
  5860. }
  5861. void CGStatusBar::clear()
  5862. {
  5863. setTxt("");
  5864. }
  5865. std::string CGStatusBar::getCurrent()
  5866. {
  5867. return text;
  5868. }
  5869. CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= zwykly*/, const std::string &Text /*= ""*/)
  5870. : CLabel(x, y, Font, Align, Color, Text)
  5871. {
  5872. init();
  5873. }
  5874. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= zwykly*/)
  5875. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  5876. {
  5877. init();
  5878. bg = BG;
  5879. moveChild(bg, bg->parent, this);
  5880. pos = bg->pos;
  5881. calcOffset();
  5882. }
  5883. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  5884. : CLabel(x, y, FONT_SMALL, CENTER)
  5885. {
  5886. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5887. init();
  5888. bg = new CPicture(name);
  5889. pos = bg->pos;
  5890. if(maxw < pos.w)
  5891. {
  5892. amin(pos.w, maxw);
  5893. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  5894. }
  5895. calcOffset();
  5896. }
  5897. CGStatusBar::~CGStatusBar()
  5898. {
  5899. GH.statusbar = oldStatusBar;
  5900. }
  5901. void CGStatusBar::show(SDL_Surface * to)
  5902. {
  5903. }
  5904. void CGStatusBar::init()
  5905. {
  5906. oldStatusBar = GH.statusbar;
  5907. GH.statusbar = this;
  5908. }
  5909. void CGStatusBar::calcOffset()
  5910. {
  5911. switch(alignment)
  5912. {
  5913. case CENTER:
  5914. textOffset = Point(pos.w/2, pos.h/2);
  5915. break;
  5916. case BOTTOMRIGHT:
  5917. textOffset = Point(pos.w, pos.h);
  5918. break;
  5919. }
  5920. }
  5921. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  5922. :cb(CB)
  5923. {
  5924. focus = false;
  5925. pos += Pos;
  5926. captureAllKeys = true;
  5927. OBJ_CONSTRUCTION;
  5928. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  5929. used = LCLICK | KEYBOARD;
  5930. giveFocus();
  5931. }
  5932. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  5933. {
  5934. focus = false;
  5935. pos += Pos;
  5936. captureAllKeys = true;
  5937. OBJ_CONSTRUCTION;
  5938. bg = new CPicture(Pos, 0, true);
  5939. Rect hlp = Pos;
  5940. if(srf)
  5941. CSDL_Ext::blitSurface(srf, &hlp, *bg, NULL);
  5942. else
  5943. SDL_FillRect(*bg, NULL, 0);
  5944. pos.w = bg->pos.w;
  5945. pos.h = bg->pos.h;
  5946. bg->pos = pos;
  5947. used = LCLICK | KEYBOARD;
  5948. giveFocus();
  5949. }
  5950. void CTextInput::showAll( SDL_Surface * to )
  5951. {
  5952. CIntObject::showAll(to);
  5953. const std::string toPrint = focus ? text + "_" : text;
  5954. CSDL_Ext::printAt(toPrint, pos.x, pos.y, FONT_SMALL, zwykly, to);
  5955. }
  5956. void CTextInput::clickLeft( tribool down, bool previousState )
  5957. {
  5958. if(down && !focus)
  5959. giveFocus();
  5960. }
  5961. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  5962. {
  5963. if(!focus || key.state != SDL_PRESSED)
  5964. return;
  5965. if(key.keysym.sym == SDLK_TAB)
  5966. {
  5967. moveFocus();
  5968. GH.breakEventHandling();
  5969. return;
  5970. }
  5971. switch(key.keysym.sym)
  5972. {
  5973. case SDLK_BACKSPACE:
  5974. if(text.size())
  5975. text.resize(text.size()-1);
  5976. break;
  5977. default:
  5978. char c = key.keysym.unicode; //TODO 16-/>8
  5979. static const std::string forbiddenChars = "<>:\"/\\|?*"; //if we are entering a filename, some special characters won't be allowed
  5980. if(!vstd::contains(forbiddenChars,c) && std::isprint(c))
  5981. text += c;
  5982. break;
  5983. }
  5984. redraw();
  5985. cb(text);
  5986. }
  5987. void CTextInput::setText( const std::string &nText, bool callCb )
  5988. {
  5989. text = nText;
  5990. redraw();
  5991. if(callCb)
  5992. cb(text);
  5993. }
  5994. CTextInput::~CTextInput()
  5995. {
  5996. }
  5997. CFocusable::CFocusable()
  5998. {
  5999. focusables.push_back(this);
  6000. }
  6001. CFocusable::~CFocusable()
  6002. {
  6003. if(inputWithFocus == this)
  6004. inputWithFocus = NULL;
  6005. focusables -= this;
  6006. }
  6007. void CFocusable::giveFocus()
  6008. {
  6009. if(inputWithFocus)
  6010. {
  6011. inputWithFocus->focus = false;
  6012. inputWithFocus->redraw();
  6013. }
  6014. focus = true;
  6015. inputWithFocus = this;
  6016. redraw();
  6017. }
  6018. void CFocusable::moveFocus()
  6019. {
  6020. std::list<CFocusable*>::iterator i = vstd::find(focusables, this),
  6021. ourIt = i;
  6022. for(i++; i != ourIt; i++)
  6023. {
  6024. if(i == focusables.end())
  6025. i = focusables.begin();
  6026. if((*i)->active)
  6027. {
  6028. (*i)->giveFocus();
  6029. break;;
  6030. }
  6031. }
  6032. }
  6033. CWindowWithArtifacts::CWindowWithArtifacts()
  6034. {
  6035. type |= WITH_ARTIFACTS;
  6036. }
  6037. CWindowWithArtifacts::~CWindowWithArtifacts()
  6038. {
  6039. }
  6040. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  6041. {
  6042. slotID = -1;
  6043. AOH = NULL;
  6044. art = NULL;
  6045. }
  6046. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  6047. {
  6048. clear();
  6049. }
  6050. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CArtPlace *place, bool dontTakeBackpack)
  6051. {
  6052. slotID = place->slotID;
  6053. AOH = place->ourOwner;
  6054. if(slotID >= 19 && dontTakeBackpack)
  6055. art = NULL;
  6056. else
  6057. art = place->ourArt;
  6058. }
  6059. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  6060. {
  6061. if(!AOH)
  6062. return false;
  6063. bool ret = al.hero == AOH->curHero && al.slot == slotID;
  6064. //assert(al.getArt() == art);
  6065. return ret;
  6066. }
  6067. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  6068. {
  6069. assert(AOH && art);
  6070. return art == AOH->curHero->getArt(slotID);
  6071. }