GUIClasses.cpp 184 KB

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