GUIClasses.cpp 189 KB

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