GUIClasses.cpp 197 KB

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