GUIClasses.cpp 189 KB

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