GUIClasses.cpp 185 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857
  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. if (Val) //no need to display 0 value
  812. subtitle = boost::lexical_cast<std::string>(Val) + " " + CGI->creh->creatures[Subtype]->*(Val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  813. break;
  814. case experience:
  815. description = CGI->generaltexth->allTexts[241];
  816. oss << Val ;
  817. if(Subtype && Val==1)
  818. {
  819. subtitle = CGI->generaltexth->allTexts[442];
  820. }
  821. else
  822. {
  823. subtitle = oss.str();
  824. }
  825. break;
  826. case hero:
  827. subtitle = description = CGI->heroh->heroes[Subtype]->name;
  828. break;
  829. case flag:
  830. subtitle = CGI->generaltexth->capColors[Subtype];
  831. break;
  832. }
  833. img = NULL;
  834. free = false;
  835. type = Type;
  836. subtype = Subtype;
  837. val = Val;
  838. SDL_Surface * temp = this->getImg();
  839. if(!temp)
  840. {
  841. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  842. return;
  843. }
  844. pos.w = temp->w;
  845. pos.h = temp->h;
  846. }
  847. SComponent::SComponent(Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur):img(sur),free(freeSur)
  848. {
  849. init(Type,Subtype,Val);
  850. }
  851. SComponent::SComponent(const Component &c)
  852. {
  853. if(c.id==5)
  854. init(experience,c.subtype,c.val);
  855. else if(c.id == Component::SPELL)
  856. init(spell,c.subtype,c.val);
  857. else
  858. init((Etype)c.id,c.subtype,c.val);
  859. if(c.id==2 && c.when==-1)
  860. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  861. }
  862. SComponent::~SComponent()
  863. {
  864. if (free && img)
  865. SDL_FreeSurface(img);
  866. }
  867. SDL_Surface * SComponent::setSurface(std:: string defname, int imagepos)
  868. {
  869. if (img)
  870. tlog1<<"SComponent::setSurface: Warning - surface is already set!\n";
  871. CDefEssential * def = CDefHandler::giveDefEss(defname);
  872. free = true;
  873. img = def->ourImages[imagepos].bitmap;
  874. img->refcount++;//to preserve surface whed def is deleted
  875. delete def;
  876. }
  877. void SComponent::show(SDL_Surface * to)
  878. {
  879. blitAt(getImg(),pos.x,pos.y,to);
  880. }
  881. SDL_Surface * SComponent::getImg()
  882. {
  883. if (img)
  884. return img;
  885. switch (type)
  886. {
  887. case artifact:
  888. return graphics->artDefs->ourImages[subtype].bitmap;
  889. case primskill:
  890. return graphics->pskillsb->ourImages[subtype].bitmap;
  891. case secskill44:
  892. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  893. case secskill:
  894. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  895. case resource:
  896. return graphics->resources->ourImages[subtype].bitmap;
  897. case experience:
  898. return graphics->pskillsb->ourImages[4].bitmap;
  899. case morale:
  900. return graphics->morale82->ourImages[val+3].bitmap;
  901. case luck:
  902. return graphics->luck82->ourImages[val+3].bitmap;
  903. case spell:
  904. return graphics->spellscr->ourImages[subtype].bitmap;
  905. case building:
  906. return setSurface(graphics->buildingPics[subtype],val);
  907. case creature:
  908. return graphics->bigImgs[subtype];
  909. case hero:
  910. return graphics->portraitLarge[subtype];
  911. case flag:
  912. return graphics->flags->ourImages[subtype].bitmap;
  913. }
  914. return NULL;
  915. }
  916. void SComponent::clickRight(tribool down, bool previousState)
  917. {
  918. if(description.size())
  919. adventureInt->handleRightClick(description,down);
  920. }
  921. void SComponent::activate()
  922. {
  923. activateRClick();
  924. }
  925. void SComponent::deactivate()
  926. {
  927. deactivateRClick();
  928. }
  929. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  930. {
  931. if (down)
  932. {
  933. if(onSelect)
  934. onSelect();
  935. }
  936. }
  937. void CSelectableComponent::init()
  938. {
  939. selected = false;
  940. }
  941. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect)
  942. :SComponent(c),onSelect(OnSelect)
  943. {
  944. init();
  945. }
  946. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect)
  947. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  948. {
  949. init();
  950. }
  951. CSelectableComponent::~CSelectableComponent()
  952. {
  953. }
  954. void CSelectableComponent::activate()
  955. {
  956. activateKeys();
  957. SComponent::activate();
  958. activateLClick();
  959. }
  960. void CSelectableComponent::deactivate()
  961. {
  962. deactivateKeys();
  963. SComponent::deactivate();
  964. deactivateLClick();
  965. }
  966. void CSelectableComponent::select(bool on)
  967. {
  968. if(on != selected)
  969. {
  970. selected = on;
  971. return;
  972. }
  973. else
  974. {
  975. return;
  976. }
  977. }
  978. void CSelectableComponent::show(SDL_Surface * to)
  979. {
  980. blitAt(getImg(),pos.x,pos.y,to);
  981. if(selected)
  982. {
  983. CSDL_Ext::drawBorder(to, Rect::around(Rect(pos.x, pos.y, getImg()->w, getImg()->h)), int3(239,215,123));
  984. }
  985. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+25,FONT_SMALL,12,zwykly,to);
  986. }
  987. void CSimpleWindow::show(SDL_Surface * to)
  988. {
  989. if(bitmap)
  990. blitAt(bitmap,pos.x,pos.y,to);
  991. }
  992. CSimpleWindow::~CSimpleWindow()
  993. {
  994. if (bitmap)
  995. {
  996. SDL_FreeSurface(bitmap);
  997. bitmap=NULL;
  998. }
  999. }
  1000. void CSelWindow::selectionChange(unsigned to)
  1001. {
  1002. for (unsigned i=0;i<components.size();i++)
  1003. {
  1004. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  1005. if (!pom)
  1006. continue;
  1007. pom->select(i==to);
  1008. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  1009. }
  1010. }
  1011. 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)
  1012. {
  1013. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1014. ID = askID;
  1015. for(int i=0;i<Buttons.size();i++)
  1016. {
  1017. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  1018. if(!i && askID >= 0)
  1019. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  1020. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  1021. }
  1022. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, zwykly);
  1023. text->redrawParentOnScrolling = true;
  1024. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  1025. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  1026. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  1027. buttons.back()->callback += boost::bind(&ICallback::selectionMade,LOCPLINT->cb,0,askID);
  1028. for(int i=0;i<comps.size();i++)
  1029. {
  1030. comps[i]->recActions = 255;
  1031. addChild(comps[i]);
  1032. components.push_back(comps[i]);
  1033. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  1034. if(i<9)
  1035. comps[i]->assignedKeys.insert(SDLK_1+i);
  1036. }
  1037. CMessage::drawIWindow(this, Text, player);
  1038. BOOST_FOREACH(SComponent *c, components)
  1039. c->subtitle = "";//workaround - erase subtitles since they were hard-blitted by function drawing window
  1040. }
  1041. void CSelWindow::madeChoice()
  1042. {
  1043. if(ID < 0)
  1044. return;
  1045. int ret = -1;
  1046. for (int i=0;i<components.size();i++)
  1047. {
  1048. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  1049. {
  1050. ret = i;
  1051. }
  1052. }
  1053. LOCPLINT->cb->selectionMade(ret+1,ID);
  1054. }
  1055. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  1056. {
  1057. bg=BitmapHandler::loadBitmap(name);
  1058. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1059. pos.x += x;
  1060. pos.y += y;
  1061. if(maxw >= 0)
  1062. pos.w = std::min(bg->w,maxw);
  1063. else
  1064. pos.w=bg->w;
  1065. pos.h=bg->h;
  1066. middlex=(pos.w/2)+pos.x;
  1067. middley=(bg->h/2)+pos.y;
  1068. }
  1069. CStatusBar::~CStatusBar()
  1070. {
  1071. SDL_FreeSurface(bg);
  1072. }
  1073. void CStatusBar::clear()
  1074. {
  1075. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  1076. {
  1077. current="";
  1078. redraw();
  1079. }
  1080. }
  1081. void CStatusBar::print(const std::string & text)
  1082. {
  1083. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  1084. {
  1085. current=text;
  1086. redraw();
  1087. }
  1088. }
  1089. void CStatusBar::show(SDL_Surface * to)
  1090. {
  1091. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  1092. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),to,&pom);
  1093. printAtMiddle(current,middlex,middley,FONT_SMALL,zwykly,to);
  1094. }
  1095. std::string CStatusBar::getCurrent()
  1096. {
  1097. return current;
  1098. }
  1099. void CList::activate()
  1100. {
  1101. activateLClick();
  1102. activateRClick();
  1103. activateHover();
  1104. activateKeys();
  1105. activateMouseMove();
  1106. };
  1107. void CList::deactivate()
  1108. {
  1109. deactivateLClick();
  1110. deactivateRClick();
  1111. deactivateHover();
  1112. deactivateKeys();
  1113. deactivateMouseMove();
  1114. };
  1115. void CList::clickLeft(tribool down, bool previousState)
  1116. {
  1117. };
  1118. CList::CList(int Size)
  1119. :SIZE(Size)
  1120. {
  1121. }
  1122. void CList::fixPos()
  1123. {
  1124. if(selected < 0) //no selection, do nothing
  1125. return;
  1126. if(selected < from) //scroll up
  1127. from = selected;
  1128. else if(from + SIZE <= selected)
  1129. from = selected - SIZE + 1;
  1130. amin(from, size() - SIZE);
  1131. amax(from, 0);
  1132. draw(screen);
  1133. }
  1134. CHeroList::CHeroList(int Size)
  1135. :CList(Size)
  1136. {
  1137. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  1138. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  1139. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  1140. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  1141. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  1142. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  1143. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  1144. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  1145. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  1146. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  1147. //32px per hero
  1148. posmobx = pos.x+1;
  1149. posmoby = pos.y+arrup->height+1;
  1150. posporx = pos.x+mobile->width+2;
  1151. pospory = pos.y+arrup->height;
  1152. posmanx = pos.x+1+50+mobile->width;
  1153. posmany = pos.y+arrup->height+1;
  1154. from = 0;
  1155. selected = -1;
  1156. pressed = indeterminate;
  1157. }
  1158. void CHeroList::init()
  1159. {
  1160. int w = pos.w+1, h = pos.h+4;
  1161. bg = CSDL_Ext::newSurface(w,h,screen);
  1162. SDL_BlitSurface(adventureInt->bg,&genRect(w,h,pos.x,pos.y),bg,&genRect(w,h,0,0));
  1163. }
  1164. void CHeroList::genList()
  1165. {
  1166. //int howMany = LOCPLINT->cb->howManyHeroes();
  1167. //for (int i=0;i<howMany;i++)
  1168. //{
  1169. // const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  1170. // if(!h->inTownGarrison)
  1171. // items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  1172. //}
  1173. }
  1174. void CHeroList::select(int which)
  1175. {
  1176. if (which<0)
  1177. {
  1178. selected = which;
  1179. adventureInt->selection = NULL;
  1180. adventureInt->terrain.currentPath = NULL;
  1181. draw(screen);
  1182. adventureInt->infoBar.showAll(screen);
  1183. }
  1184. if (which>=LOCPLINT->wanderingHeroes.size())
  1185. return;
  1186. selected = which;
  1187. adventureInt->select(LOCPLINT->wanderingHeroes[which]);
  1188. fixPos();
  1189. draw(screen);
  1190. }
  1191. void CHeroList::clickLeft(tribool down, bool previousState)
  1192. {
  1193. if (down)
  1194. {
  1195. /***************************ARROWS*****************************************/
  1196. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1197. {
  1198. if(from>0)
  1199. {
  1200. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  1201. pressed = true;
  1202. }
  1203. return;
  1204. }
  1205. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1206. {
  1207. if(LOCPLINT->wanderingHeroes.size()-from>SIZE)
  1208. {
  1209. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  1210. pressed = false;
  1211. }
  1212. return;
  1213. }
  1214. /***************************HEROES*****************************************/
  1215. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1216. hx-=pos.x;
  1217. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1218. int ny = hy/32;
  1219. if (ny>=SIZE || ny<0)
  1220. return;
  1221. if ( (ny+from)==selected && (adventureInt->selection->ID == HEROI_TYPE))
  1222. LOCPLINT->openHeroWindow(LOCPLINT->wanderingHeroes[selected]);//print hero screen
  1223. select(ny+from);
  1224. }
  1225. else
  1226. {
  1227. if (indeterminate(pressed))
  1228. return;
  1229. if (pressed) //up
  1230. {
  1231. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  1232. pressed = indeterminate;
  1233. if (!down)
  1234. {
  1235. from--;
  1236. if (from<0)
  1237. from=0;
  1238. draw(screen);
  1239. }
  1240. }
  1241. else if (!pressed) //down
  1242. {
  1243. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  1244. pressed = indeterminate;
  1245. if (!down)
  1246. {
  1247. from++;
  1248. //if (from<items.size()-5)
  1249. // from=items.size()-5;
  1250. draw(screen);
  1251. }
  1252. }
  1253. else
  1254. throw 0;
  1255. }
  1256. }
  1257. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1258. {
  1259. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1260. {
  1261. if (from>0)
  1262. adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  1263. else
  1264. adventureInt->statusbar.clear();
  1265. return;
  1266. }
  1267. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1268. {
  1269. if ((LOCPLINT->wanderingHeroes.size()-from) > SIZE)
  1270. adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1271. else
  1272. adventureInt->statusbar.clear();
  1273. return;
  1274. }
  1275. //if not buttons then heroes
  1276. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1277. hx-=pos.x;
  1278. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1279. int ny = hy/32;
  1280. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1281. {
  1282. adventureInt->statusbar.clear();
  1283. return;
  1284. }
  1285. std::vector<std::string> temp;
  1286. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->name);
  1287. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->type->heroClass->name);
  1288. adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1289. //select(ny+from);
  1290. }
  1291. void CHeroList::clickRight(tribool down, bool previousState)
  1292. {
  1293. if (down)
  1294. {
  1295. /***************************ARROWS*****************************************/
  1296. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1297. {
  1298. adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down);
  1299. }
  1300. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->wanderingHeroes.size()-from>5))
  1301. {
  1302. adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down);
  1303. }
  1304. else
  1305. {
  1306. //if not buttons then heroes
  1307. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1308. hx-=pos.x;
  1309. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1310. int ny = hy/32;
  1311. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1312. {
  1313. return;
  1314. } //show popup
  1315. CRClickPopup::createAndPush(LOCPLINT->wanderingHeroes[from+ny], GH.current->motion);
  1316. }
  1317. }
  1318. }
  1319. void CHeroList::hover (bool on)
  1320. {
  1321. }
  1322. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1323. {
  1324. }
  1325. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1326. {
  1327. if(toRemove) //remove specific hero
  1328. LOCPLINT->wanderingHeroes -= toRemove;
  1329. else
  1330. LOCPLINT->recreateHeroTownList();
  1331. if(selected >= LOCPLINT->wanderingHeroes.size())
  1332. select(LOCPLINT->wanderingHeroes.size()-1);
  1333. if(toRemove)
  1334. {
  1335. if(LOCPLINT->wanderingHeroes.size() == 0)
  1336. adventureInt->townList.select(0);
  1337. else
  1338. select(selected);
  1339. }
  1340. }
  1341. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1342. {
  1343. int ser = -1;
  1344. for(int i=0; i<LOCPLINT->wanderingHeroes.size() && ser<0; i++)
  1345. if(LOCPLINT->wanderingHeroes[i]->subID == which->subID)
  1346. ser = i;
  1347. ser -= from;
  1348. if(ser<0 || ser >= SIZE) return;
  1349. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1350. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1351. }
  1352. void CHeroList::draw(SDL_Surface * to)
  1353. {
  1354. for (int iT=0+from;iT<SIZE+from;iT++)
  1355. {
  1356. int i = iT-from;
  1357. if (iT>=LOCPLINT->wanderingHeroes.size())
  1358. {
  1359. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1360. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1361. blitAt(empty,posporx,pospory+i*32,to);
  1362. continue;
  1363. }
  1364. const CGHeroInstance *cur = LOCPLINT->wanderingHeroes[iT];
  1365. int pom = cur->movement / 100;
  1366. if (pom>25) pom=25;
  1367. if (pom<0) pom=0;
  1368. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1369. pom = cur->mana / 5;
  1370. if (pom>25) pom=25;
  1371. if (pom<0) pom=0;
  1372. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1373. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1374. blitAt(temp,posporx,pospory+i*32,to);
  1375. if (adventureInt->selection && (selected == iT) && (adventureInt->selection->ID == HEROI_TYPE))
  1376. {
  1377. blitAt(selection,posporx,pospory+i*32,to);
  1378. }
  1379. //TODO: support for custom portraits
  1380. }
  1381. if (from>0)
  1382. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1383. else
  1384. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1385. if (LOCPLINT->wanderingHeroes.size()-from > SIZE)
  1386. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1387. else
  1388. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1389. }
  1390. int CHeroList::getPosOfHero(const CGHeroInstance* h)
  1391. {
  1392. return vstd::findPos(LOCPLINT->wanderingHeroes, h, std::equal_to<const CGHeroInstance*>());
  1393. }
  1394. void CHeroList::show( SDL_Surface * to )
  1395. {
  1396. }
  1397. int CHeroList::size()
  1398. {
  1399. return LOCPLINT->wanderingHeroes.size();
  1400. }
  1401. CTownList::~CTownList()
  1402. {
  1403. delete arrup;
  1404. delete arrdo;
  1405. }
  1406. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1407. :CList(Size)
  1408. {
  1409. arrup = CDefHandler::giveDef(arrupg);
  1410. arrdo = CDefHandler::giveDef(arrdog);
  1411. pos.x = x;
  1412. pos.y = y;
  1413. pos.w = std::max(arrdo->width, arrup->width);
  1414. pos.h = arrdo->height + arrup->height + Size*32;
  1415. arrupp.x=x;
  1416. arrupp.y=y;
  1417. arrupp.w=arrup->width;
  1418. arrupp.h=arrup->height;
  1419. arrdop.x=x;
  1420. arrdop.y=y+arrup->height+32*SIZE;
  1421. arrdop.w=arrdo->width;
  1422. arrdop.h=arrdo->height;
  1423. posporx = arrdop.x;
  1424. pospory = arrupp.y + arrupp.h;
  1425. pressed = indeterminate;
  1426. from = 0;
  1427. selected = -1;
  1428. }
  1429. void CTownList::genList()
  1430. {
  1431. // LOCPLINT->towns.clear();
  1432. // int howMany = LOCPLINT->cb->howManyTowns();
  1433. // for (int i=0;i<howMany;i++)
  1434. // {
  1435. // LOCPLINT->towns.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1436. // }
  1437. }
  1438. void CTownList::select(int which)
  1439. {
  1440. if (which>=LOCPLINT->towns.size())
  1441. return;
  1442. selected = which;
  1443. if(!fun.empty())
  1444. fun();
  1445. fixPos();
  1446. }
  1447. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1448. {
  1449. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1450. {
  1451. if (from>0)
  1452. GH.statusbar->print(CGI->generaltexth->zelp[306].first);
  1453. else
  1454. GH.statusbar->clear();
  1455. return;
  1456. }
  1457. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1458. {
  1459. if ((LOCPLINT->towns.size()-from) > SIZE)
  1460. GH.statusbar->print(CGI->generaltexth->zelp[307].first);
  1461. else
  1462. GH.statusbar->clear();
  1463. return;
  1464. }
  1465. //if not buttons then towns
  1466. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1467. hx-=pos.x;
  1468. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1469. int ny = hy/32;
  1470. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1471. {
  1472. GH.statusbar->clear();
  1473. return;
  1474. };
  1475. std::string temp = CGI->generaltexth->tcommands[4];
  1476. boost::algorithm::replace_first(temp,"%s",LOCPLINT->towns[from+ny]->name);
  1477. temp += ", "+LOCPLINT->towns[from+ny]->town->Name();
  1478. GH.statusbar->print(temp);
  1479. }
  1480. void CTownList::clickLeft(tribool down, bool previousState)
  1481. {
  1482. if (down)
  1483. {
  1484. /***************************ARROWS*****************************************/
  1485. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1486. {
  1487. if(from>0)
  1488. {
  1489. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1490. pressed = true;
  1491. }
  1492. return;
  1493. }
  1494. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1495. {
  1496. if(LOCPLINT->towns.size()-from > SIZE)
  1497. {
  1498. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1499. pressed = false;
  1500. }
  1501. return;
  1502. }
  1503. /***************************TOWNS*****************************************/
  1504. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1505. hx-=pos.x;
  1506. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1507. int ny = hy/32;
  1508. if (ny>=SIZE || ny<0)
  1509. return;
  1510. if(GH.topInt() == adventureInt
  1511. && (ny+from)==selected
  1512. && adventureInt->selection->ID == TOWNI_TYPE
  1513. )
  1514. LOCPLINT->openTownWindow(LOCPLINT->towns[selected]);//print town screen
  1515. else
  1516. select(ny+from);
  1517. }
  1518. else
  1519. {
  1520. if (indeterminate(pressed))
  1521. return;
  1522. if (pressed) //up
  1523. {
  1524. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1525. pressed = indeterminate;
  1526. if (!down)
  1527. {
  1528. from--;
  1529. if (from<0)
  1530. from=0;
  1531. draw(screenBuf);
  1532. }
  1533. }
  1534. else if (!pressed) //down
  1535. {
  1536. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1537. pressed = indeterminate;
  1538. if (!down)
  1539. {
  1540. from++;
  1541. //if (from<LOCPLINT->towns.size()-5)
  1542. // from=LOCPLINT->towns.size()-5;
  1543. draw(screenBuf);
  1544. }
  1545. }
  1546. else
  1547. throw 0;
  1548. }
  1549. }
  1550. void CTownList::clickRight(tribool down, bool previousState)
  1551. {
  1552. if (down)
  1553. {
  1554. /***************************ARROWS*****************************************/
  1555. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1556. {
  1557. adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down);
  1558. }
  1559. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->towns.size()-from>5))
  1560. {
  1561. adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down);
  1562. }
  1563. //if not buttons then towns
  1564. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1565. hx-=pos.x;
  1566. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1567. int ny = hy/32;
  1568. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1569. {
  1570. return;
  1571. }
  1572. //show popup
  1573. CRClickPopup::createAndPush(LOCPLINT->towns[from+ny], GH.current->motion);
  1574. }
  1575. }
  1576. void CTownList::hover (bool on)
  1577. {
  1578. }
  1579. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1580. {
  1581. }
  1582. void CTownList::draw(SDL_Surface * to)
  1583. {
  1584. for (int iT=0+from;iT<SIZE+from;iT++)
  1585. {
  1586. int i = iT-from;
  1587. if (iT>=LOCPLINT->towns.size())
  1588. {
  1589. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1590. continue;
  1591. }
  1592. blitAt(graphics->getPic(LOCPLINT->towns[iT]->subID,LOCPLINT->towns[iT]->hasFort(),LOCPLINT->towns[iT]->builded),posporx,pospory+i*32,to);
  1593. if (adventureInt->selection && (selected == iT) && (adventureInt->selection->ID == TOWNI_TYPE))
  1594. {
  1595. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1596. }
  1597. }
  1598. if (from>0)
  1599. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1600. else
  1601. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1602. if (LOCPLINT->towns.size()-from>SIZE)
  1603. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1604. else
  1605. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1606. }
  1607. void CTownList::show( SDL_Surface * to )
  1608. {
  1609. }
  1610. int CTownList::size()
  1611. {
  1612. return LOCPLINT->towns.size();
  1613. }
  1614. CCreaturePic::CCreaturePic(const CCreature *cre, bool Big)
  1615. :c(cre),big(Big)
  1616. {
  1617. anim = new CCreatureAnimation(cre->animDefName);
  1618. }
  1619. CCreaturePic::~CCreaturePic()
  1620. {
  1621. delete anim;
  1622. }
  1623. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  1624. {
  1625. SDL_Rect dst;
  1626. if(big)
  1627. {
  1628. blitAt(graphics->backgrounds[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1629. dst = genRect(130,100,x,y);
  1630. }
  1631. else
  1632. {
  1633. blitAt(graphics->backgroundsm[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1634. dst = genRect(120,100,x,y);
  1635. }
  1636. if(c->isDoubleWide())
  1637. x-=15;
  1638. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  1639. }
  1640. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  1641. {
  1642. //TODO: write
  1643. return NULL;
  1644. }
  1645. void CRecruitmentWindow::close()
  1646. {
  1647. GH.popIntTotally(this);
  1648. }
  1649. void CRecruitmentWindow::Max()
  1650. {
  1651. slider->moveTo(slider->amount);
  1652. }
  1653. void CRecruitmentWindow::Buy()
  1654. {
  1655. int crid = creatures[which].ID,
  1656. dstslot = dst-> getSlotFor(crid);
  1657. if(dstslot < 0) //no available slot
  1658. {
  1659. std::string txt;
  1660. if(dst->ID == HEROI_TYPE)
  1661. {
  1662. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1663. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  1664. }
  1665. else
  1666. {
  1667. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1668. }
  1669. LOCPLINT->showInfoDialog(txt);
  1670. return;
  1671. }
  1672. recruit(crid, slider->value);
  1673. if(level >= 0)
  1674. close();
  1675. else
  1676. slider->moveTo(0);
  1677. }
  1678. void CRecruitmentWindow::Cancel()
  1679. {
  1680. close();
  1681. }
  1682. void CRecruitmentWindow::sliderMoved(int to)
  1683. {
  1684. buy->block(!to);
  1685. }
  1686. void CRecruitmentWindow::clickLeft(tribool down, bool previousState)
  1687. {
  1688. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1689. for(int i=0;i<creatures.size();i++)
  1690. {
  1691. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1692. if(isItIn(&genRect(132,sCREATURE_WIDTH,pos.x+curx,pos.y+64),GH.current->motion.x,GH.current->motion.y))
  1693. {
  1694. which = i;
  1695. int newAmount = std::min(amounts[i],creatures[i].amount);
  1696. slider->setAmount(newAmount);
  1697. slider->block(!newAmount);
  1698. max->block(!newAmount);
  1699. if(slider->value > newAmount)
  1700. slider->moveTo(newAmount);
  1701. else
  1702. slider->moveTo(slider->value);
  1703. curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1704. for(int j=0;j<creatures.size();j++)
  1705. {
  1706. if(which==j)
  1707. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(255,0,0));
  1708. else
  1709. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(239,215,123));
  1710. curx += TOTAL_CREATURE_WIDTH;
  1711. }
  1712. break;
  1713. }
  1714. curx += TOTAL_CREATURE_WIDTH;
  1715. }
  1716. }
  1717. void CRecruitmentWindow::clickRight(tribool down, bool previousState)
  1718. {
  1719. if(down)
  1720. {
  1721. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1722. for(int i=0;i<creatures.size();i++)
  1723. {
  1724. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1725. if(isItIn(&genRect(132,sCREATURE_WIDTH,pos.x+curx,pos.y+64),GH.current->motion.x,GH.current->motion.y))
  1726. {
  1727. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID, 0, 0);
  1728. GH.pushInt(popup);
  1729. break;
  1730. }
  1731. curx += TOTAL_CREATURE_WIDTH;
  1732. }
  1733. }
  1734. }
  1735. void CRecruitmentWindow::activate()
  1736. {
  1737. activateLClick();
  1738. activateRClick();
  1739. buy->activate();
  1740. max->activate();
  1741. cancel->activate();
  1742. slider->activate();
  1743. GH.statusbar = bar;
  1744. }
  1745. void CRecruitmentWindow::deactivate()
  1746. {
  1747. deactivateLClick();
  1748. deactivateRClick();
  1749. buy->deactivate();
  1750. max->deactivate();
  1751. cancel->deactivate();
  1752. slider->deactivate();
  1753. }
  1754. void CRecruitmentWindow::show(SDL_Surface * to)
  1755. {
  1756. static char animCounter=0; //animation counter - for determining appropriate animation frame to be shown
  1757. blitAt(bitmap,pos.x,pos.y,to);
  1758. buy->show(to);
  1759. max->show(to);
  1760. cancel->show(to);
  1761. slider->show(to);
  1762. char pom[15];
  1763. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1764. printAtMiddle(pom,pos.x+205,pos.y+253,FONT_SMALL,zwykly,to);
  1765. SDL_itoa(slider->value,pom,10); //recruit
  1766. printAtMiddle(pom,pos.x+279,pos.y+253,FONT_SMALL,zwykly,to);
  1767. 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"
  1768. int curx = pos.x+122-creatures[which].res.size()*24;
  1769. for(int i=creatures[which].res.size()-1; i>=0; i--)// decrement used to make gold displayed as first res
  1770. {
  1771. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,to);
  1772. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,to);
  1773. SDL_itoa(creatures[which].res[i].second,pom,10);
  1774. printAtMiddle(pom,curx+15,pos.y+287,FONT_SMALL,zwykly,to);
  1775. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1776. printAtMiddle(pom,curx+15+258,pos.y+287,FONT_SMALL,zwykly,to);
  1777. curx+=32+16;//size of bitmap + distance between them
  1778. }
  1779. curx = pos.x + 192 + CREATURE_WIDTH - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1780. for(int i=0; i<creatures.size(); ++i)
  1781. {
  1782. creatures[i].pic->blitPic(to, curx-50, pos.y+130-65, !(animCounter%4));
  1783. curx += TOTAL_CREATURE_WIDTH;
  1784. }
  1785. ++animCounter;
  1786. bar->show(to);
  1787. }
  1788. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> &Recruit, int y_offset)
  1789. :recruit(Recruit), dwelling(Dwelling), dst(Dst), level(Level)
  1790. {
  1791. which = 0;
  1792. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  1793. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1794. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1795. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1796. SDL_FreeSurface(hhlp);
  1797. pos.x = screen->w/2 - bitmap->w/2;
  1798. pos.y = screen->h/2 - bitmap->h/2+y_offset;
  1799. pos.w = bitmap->w;
  1800. pos.h = bitmap->h;
  1801. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  1802. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  1803. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  1804. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  1805. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecruitmentWindow::sliderMoved,this, _1),0,0,0,true);
  1806. initCres();
  1807. printAtMiddle(CGI->generaltexth->allTexts[346],113,232,FONT_SMALL,zwykly,bitmap); //cost per troop t
  1808. printAtMiddle(CGI->generaltexth->allTexts[465],205,233,FONT_SMALL,zwykly,bitmap); //available t
  1809. printAtMiddle(CGI->generaltexth->allTexts[16],279,233,FONT_SMALL,zwykly,bitmap); //recruit t
  1810. printAtMiddle(CGI->generaltexth->allTexts[466],371,232,FONT_SMALL,zwykly,bitmap); //total cost t
  1811. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  1812. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  1813. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  1814. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  1815. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  1816. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  1817. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  1818. //border for creatures
  1819. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1820. for(int i=0;i<creatures.size();i++)
  1821. {
  1822. creatures[i].pos.x = curx+1;
  1823. creatures[i].pos.y = 65;
  1824. creatures[i].pos.w = 100;
  1825. creatures[i].pos.h = 130;
  1826. if(which==i)
  1827. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(255,0,0));
  1828. else
  1829. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(239,215,123));
  1830. curx += TOTAL_CREATURE_WIDTH;
  1831. }
  1832. if(!creatures[0].amount || !amounts[0])
  1833. {
  1834. max->block(true);
  1835. slider->block(true);
  1836. }
  1837. //buy->block(true); //not needed, will be blocked by initing slider on 0
  1838. }
  1839. CRecruitmentWindow::~CRecruitmentWindow()
  1840. {
  1841. cleanCres();
  1842. delete max;
  1843. delete buy;
  1844. delete cancel;
  1845. SDL_FreeSurface(bitmap);
  1846. delete slider;
  1847. delete bar;
  1848. }
  1849. void CRecruitmentWindow::initCres()
  1850. {
  1851. cleanCres();
  1852. for(int i=0; i<dwelling->creatures.size(); i++)
  1853. {
  1854. if(level >= 0 && i != level)
  1855. continue;
  1856. for(int j = dwelling->creatures[i].second.size() - 1; j >= 0 ; j--)
  1857. {
  1858. creatures.resize(creatures.size()+1);
  1859. creinfo &cur = creatures.back();
  1860. cur.amount = dwelling->creatures[i].first;
  1861. cur.ID = dwelling->creatures[i].second[j];
  1862. const CCreature *cre = CGI->creh->creatures[cur.ID];
  1863. cur.pic = new CCreaturePic(cre);
  1864. for(int k=0; k<cre->cost.size(); k++)
  1865. if(cre->cost[k])
  1866. cur.res.push_back(std::make_pair(k,cre->cost[k]));
  1867. amounts.push_back(cre->maxAmount(LOCPLINT->cb->getResourceAmount()));
  1868. }
  1869. }
  1870. slider->setAmount(std::min(amounts[which],creatures[which].amount));
  1871. }
  1872. void CRecruitmentWindow::cleanCres()
  1873. {
  1874. for(int i=0;i<creatures.size();i++)
  1875. {
  1876. delete creatures[i].pic;
  1877. }
  1878. creatures.clear();
  1879. amounts.clear();
  1880. }
  1881. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1882. {
  1883. last = Last;
  1884. which = 1;
  1885. c=cid;
  1886. slider = NULL;
  1887. gar = Owner;
  1888. SDL_Surface *hhlp = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1889. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1890. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1891. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1892. SDL_FreeSurface(hhlp);
  1893. pos.x = screen->w/2 - bitmap->w/2;
  1894. pos.y = screen->h/2 - bitmap->h/2;
  1895. pos.w = bitmap->w;
  1896. pos.h = bitmap->h;
  1897. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1898. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1899. int sliderPositions = max - (last>=0) - (last==2);
  1900. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),0,sliderPositions,val,true);
  1901. a1 = max-val;
  1902. a2 = val;
  1903. anim = new CCreaturePic(CGI->creh->creatures[cid]);
  1904. anim->anim->setType(1);
  1905. std::string title = CGI->generaltexth->allTexts[256];
  1906. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid]->namePl);
  1907. printAtMiddle(title,150,34,FONT_BIG,tytulowy,bitmap);
  1908. }
  1909. CSplitWindow::~CSplitWindow() //d-tor
  1910. {
  1911. SDL_FreeSurface(bitmap);
  1912. delete ok;
  1913. delete cancel;
  1914. delete slider;
  1915. delete anim;
  1916. }
  1917. void CSplitWindow::activate()
  1918. {
  1919. activateLClick();
  1920. activateKeys();
  1921. ok->activate();
  1922. cancel->activate();
  1923. slider->activate();
  1924. }
  1925. void CSplitWindow::deactivate()
  1926. {
  1927. deactivateLClick();
  1928. deactivateKeys();
  1929. ok->deactivate();
  1930. cancel->deactivate();
  1931. slider->deactivate();
  1932. }
  1933. void CSplitWindow::split()
  1934. {
  1935. gar->splitStacks(a2);
  1936. close();
  1937. }
  1938. void CSplitWindow::close()
  1939. {
  1940. GH.popIntTotally(this);
  1941. }
  1942. void CSplitWindow::sliderMoved(int to)
  1943. {
  1944. int all = a1+a2;
  1945. a2 = to + (last==1 || last==2);
  1946. if(slider)
  1947. a1 = all - a2;
  1948. }
  1949. void CSplitWindow::show(SDL_Surface * to)
  1950. {
  1951. blitAt(bitmap,pos.x,pos.y,to);
  1952. ok->show(to);
  1953. cancel->show(to);
  1954. slider->show(to);
  1955. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,FONT_BIG,zwykly,to);
  1956. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,FONT_BIG,zwykly,to);
  1957. anim->blitPic(to,pos.x+20,pos.y+54,false);
  1958. anim->blitPic(to,pos.x+177,pos.y+54,false);
  1959. }
  1960. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1961. {
  1962. SDLKey k = key.keysym.sym;
  1963. if (isNumKey(k)) //convert numpad number to normal digit
  1964. k = numToDigit(k);
  1965. if(key.state != SDL_PRESSED)
  1966. return;
  1967. int &cur = (which ? a2 : a1),
  1968. &sec = (which ? a1 : a2),
  1969. ncur = cur;
  1970. if (k == SDLK_BACKSPACE)
  1971. {
  1972. ncur /= 10;
  1973. }
  1974. else if(k == SDLK_TAB)
  1975. {
  1976. which = !which;
  1977. }
  1978. else
  1979. {
  1980. int number = k - SDLK_0;
  1981. if (number < 0 || number > 9) //not a number pressed
  1982. {
  1983. return;
  1984. }
  1985. ncur = cur*10 + number;
  1986. }
  1987. int delta = ncur - cur;
  1988. if(delta > sec)
  1989. {
  1990. cur += sec;
  1991. sec = 0;
  1992. }
  1993. slider->moveTo(which ? ncur : a1+a2-ncur);
  1994. }
  1995. void CSplitWindow::clickLeft(tribool down, bool previousState)
  1996. {
  1997. if(down)
  1998. {
  1999. Point click(GH.current->motion.x,GH.current->motion.y);
  2000. click = click - pos.topLeft();
  2001. if(Rect(19,216,105,40).isIn(click)) //left picture
  2002. which = 0;
  2003. else if(Rect(175,216,105,40).isIn(click)) //right picture
  2004. which = 1;
  2005. }
  2006. }
  2007. void CCreInfoWindow::show(SDL_Surface * to)
  2008. {
  2009. char pom[15];
  2010. blitAt(*bitmap,pos.x,pos.y,to);
  2011. anim->blitPic(to,pos.x+21,pos.y+48,(type) && !(anf%4));
  2012. if(++anf==4)
  2013. anf=0;
  2014. if(count.size())
  2015. printTo(count.c_str(),pos.x+114,pos.y+174,FONT_TIMES,zwykly,to);
  2016. if(upgrade)
  2017. upgrade->show(to);
  2018. if(dismiss)
  2019. dismiss->show(to);
  2020. if(ok)
  2021. ok->show(to);
  2022. }
  2023. CCreInfoWindow::CCreInfoWindow(const CStackInstance &st, int Type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  2024. : type(Type), dsm(Dsm), dismiss(0), upgrade(0), ok(0)
  2025. {
  2026. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2027. init(st.type, &st, st.count);
  2028. //print abilities text - if r-click popup
  2029. if(type)
  2030. {
  2031. if(Upg && ui)
  2032. {
  2033. bool enough = true;
  2034. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  2035. {
  2036. BLOCK_CAPTURING;
  2037. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*st.count)
  2038. enough = false;
  2039. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*st.count));
  2040. }
  2041. if(enough)
  2042. {
  2043. CFunctionList<void()> fs;
  2044. fs += Upg;
  2045. fs += boost::bind(&CCreInfoWindow::close,this);
  2046. CFunctionList<void()> cfl;
  2047. cfl = boost::bind(&CPlayerInterface::showYesNoDialog, LOCPLINT, CGI->generaltexth->allTexts[207], boost::ref(upgResCost), fs, 0, false);
  2048. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,76,237,"IVIEWCR.DEF",SDLK_u);
  2049. }
  2050. else
  2051. {
  2052. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),76,237,"IVIEWCR.DEF");
  2053. upgrade->callback.funcs.clear();
  2054. upgrade->bitmapOffset = 2;
  2055. }
  2056. }
  2057. if(Dsm)
  2058. {
  2059. CFunctionList<void()> fs[2];
  2060. //on dismiss confirmed
  2061. fs[0] += Dsm; //dismiss
  2062. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  2063. CFunctionList<void()> cfl;
  2064. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  2065. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,21,237,"IVIEWCR2.DEF",SDLK_d);
  2066. }
  2067. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),216,237,"IOKAY.DEF",SDLK_RETURN);
  2068. }
  2069. else
  2070. {
  2071. printAtWB(c->abilityText,17,231,FONT_SMALL,35,zwykly,*bitmap);
  2072. }
  2073. //if we are displying window fo r stack in battle, there are several more things that we need to display
  2074. if(const CStack *battleStack = dynamic_cast<const CStack*>(&st))
  2075. {
  2076. //spell effects
  2077. int printed=0; //how many effect pics have been printed
  2078. BOOST_FOREACH(const CStack::StackEffect &effect, battleStack->effects)
  2079. {
  2080. blitAt(graphics->spellEffectsPics->ourImages[effect.id + 1].bitmap, 127 + 52 * printed, 186, *bitmap);
  2081. ++printed;
  2082. if(printed >= 3) //we can fit only 3 effects
  2083. break;
  2084. }
  2085. //print current health
  2086. printLine(5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  2087. }
  2088. }
  2089. void CCreInfoWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  2090. {
  2091. printAt(text, 155, 48 + nr*19, FONT_SMALL, zwykly, *bitmap);
  2092. std::string hlp;
  2093. if(range && baseVal != val)
  2094. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  2095. else if(baseVal != val && val>=0)
  2096. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  2097. else
  2098. hlp = boost::lexical_cast<std::string>(baseVal);
  2099. printTo(hlp, 276, 61 + nr*19, FONT_SMALL, zwykly, *bitmap);
  2100. }
  2101. void CCreInfoWindow::init(const CCreature *cre, const CStackInstance *stack, int creatureCount)
  2102. {
  2103. const CBonusSystemNode *finalNode = NULL;
  2104. if(stack)
  2105. finalNode = stack;
  2106. else
  2107. finalNode = cre;
  2108. anf = 0;
  2109. c = cre;
  2110. bitmap = new CPicture("CRSTKPU.bmp");
  2111. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  2112. pos = bitmap->center();
  2113. {
  2114. BLOCK_CAPTURING;
  2115. anim = new CCreaturePic(c);
  2116. }
  2117. if(!type) anim->anim->setType(2);
  2118. count = boost::lexical_cast<std::string>(creatureCount);
  2119. printAtMiddle(c->namePl,149,30,FONT_SMALL,tytulowy,*bitmap); //creature name
  2120. printLine(0, CGI->generaltexth->primarySkillNames[0], cre->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), finalNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK));
  2121. printLine(1, CGI->generaltexth->primarySkillNames[1], cre->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), finalNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE));
  2122. if(c->shots)
  2123. printLine(2, CGI->generaltexth->allTexts[198], c->shots);
  2124. //TODO
  2125. int dmgMultiply = 1;
  2126. if(stack && stack->hasBonusOfType(Bonus::SIEGE_WEAPON))
  2127. dmgMultiply += stack->armyObj->Attack();
  2128. printLine(3, CGI->generaltexth->allTexts[199], finalNode->getMinDamage() * dmgMultiply, finalNode->getMaxDamage() * dmgMultiply, true);
  2129. printLine(4, CGI->generaltexth->allTexts[388], cre->valOfBonuses(Bonus::STACK_HEALTH), finalNode->valOfBonuses(Bonus::STACK_HEALTH));
  2130. printLine(6, CGI->generaltexth->zelp[441].first, cre->valOfBonuses(Bonus::STACKS_SPEED), finalNode->valOfBonuses(Bonus::STACKS_SPEED));
  2131. //setting morale
  2132. morale = new MoraleLuckBox(true);
  2133. morale->pos = genRect(42, 42, pos.x + 24, pos.y + 189);
  2134. morale->set(stack);
  2135. //setting luck
  2136. luck = new MoraleLuckBox(false);
  2137. luck->pos = genRect(42, 42, pos.x + 77, pos.y + 189);
  2138. luck->set(stack);
  2139. //luck and morale
  2140. int luck = 3, morale = 3;
  2141. if(stack)
  2142. {
  2143. //add modifiers
  2144. luck += stack->LuckVal();
  2145. morale += stack->MoraleVal();
  2146. }
  2147. blitAt(graphics->morale42->ourImages[morale].bitmap, 24, 189, *bitmap);
  2148. blitAt(graphics->luck42->ourImages[luck].bitmap, 77, 189, *bitmap);
  2149. }
  2150. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount)
  2151. : type(Type), dismiss(0), upgrade(0), ok(0)
  2152. {
  2153. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2154. const CCreature *cre = CGI->creh->creatures[Cid];
  2155. init(cre, NULL, creatureCount);
  2156. }
  2157. CCreInfoWindow::~CCreInfoWindow()
  2158. {
  2159. delete anim;
  2160. for(int i=0; i<upgResCost.size();i++)
  2161. delete upgResCost[i];
  2162. }
  2163. void CCreInfoWindow::activate()
  2164. {
  2165. CIntObject::activate();
  2166. if(!type)
  2167. activateRClick();
  2168. }
  2169. void CCreInfoWindow::close()
  2170. {
  2171. GH.popIntTotally(this);
  2172. }
  2173. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  2174. {
  2175. if(down)
  2176. return;
  2177. close();
  2178. }
  2179. void CCreInfoWindow::dismissF()
  2180. {
  2181. dsm();
  2182. close();
  2183. }
  2184. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  2185. {
  2186. }
  2187. void CCreInfoWindow::deactivate()
  2188. {
  2189. if(!type)
  2190. deactivateRClick();
  2191. CIntObject::deactivate();
  2192. }
  2193. void CLevelWindow::close()
  2194. {
  2195. for(int i=0;i<comps.size();i++)
  2196. {
  2197. if(comps[i]->selected)
  2198. {
  2199. cb(i);
  2200. break;
  2201. }
  2202. }
  2203. GH.popIntTotally(this);
  2204. LOCPLINT->showingDialog->setn(false);
  2205. }
  2206. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  2207. {
  2208. LOCPLINT->showingDialog->setn(true);
  2209. heroPortrait = hero->portrait;
  2210. cb = callback;
  2211. for(int i=0;i<skills.size();i++)
  2212. {
  2213. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  2214. comps.back()->assignedKeys.insert(SDLK_1 + i);
  2215. }
  2216. SDL_Surface *hhlp = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  2217. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2218. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  2219. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2220. SDL_FreeSurface(hhlp);
  2221. pos.x = screen->w/2 - bitmap->w/2;
  2222. pos.y = screen->h/2 - bitmap->h/2;
  2223. pos.w = bitmap->w;
  2224. pos.h = bitmap->h;
  2225. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  2226. //draw window
  2227. char buf[100], buf2[100];
  2228. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  2229. sprintf(buf,buf2,hero->name.c_str());
  2230. printAtMiddle(buf,192,33,FONT_MEDIUM,zwykly,bitmap);
  2231. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  2232. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  2233. printAtMiddle(buf,192,162,FONT_MEDIUM,zwykly,bitmap);
  2234. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  2235. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,253,FONT_MEDIUM,zwykly,bitmap);
  2236. const Font *f = graphics->fonts[FONT_MEDIUM];
  2237. std::string text = CGI->generaltexth->allTexts[4];
  2238. int fontWidth = f->getWidth(text.c_str())/2;
  2239. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+fontWidth) )/2;
  2240. for(int i=0;i<comps.size();i++)
  2241. {
  2242. comps[i]->pos.x = curx+pos.x;
  2243. comps[i]->pos.y = 326+pos.y;
  2244. if( i < (comps.size()-1) )
  2245. {
  2246. curx += 44+21; //skill width + margin to "or"
  2247. printAtMiddle(text ,curx,346,FONT_MEDIUM,zwykly,bitmap);
  2248. curx += fontWidth+15;
  2249. }
  2250. }
  2251. if(comps.size())
  2252. {
  2253. comps[0]->select(true);
  2254. }
  2255. }
  2256. void CLevelWindow::selectionChanged(unsigned to)
  2257. {
  2258. if(ok->blocked)
  2259. ok->block(false);
  2260. for(int i=0;i<comps.size();i++)
  2261. if(i==to)
  2262. comps[i]->select(true);
  2263. else
  2264. comps[i]->select(false);
  2265. }
  2266. CLevelWindow::~CLevelWindow()
  2267. {
  2268. delete ok;
  2269. for(int i=0;i<comps.size();i++)
  2270. delete comps[i];
  2271. SDL_FreeSurface(bitmap);
  2272. }
  2273. void CLevelWindow::activate()
  2274. {
  2275. ok->activate();
  2276. for(int i=0;i<comps.size();i++)
  2277. comps[i]->activate();
  2278. }
  2279. void CLevelWindow::deactivate()
  2280. {
  2281. ok->deactivate();
  2282. for(int i=0;i<comps.size();i++)
  2283. comps[i]->deactivate();
  2284. }
  2285. void CLevelWindow::show(SDL_Surface * to)
  2286. {
  2287. blitAt(bitmap,pos.x,pos.y,to);
  2288. blitAt(graphics->portraitLarge[heroPortrait],170+pos.x,66+pos.y,to);
  2289. ok->show(to);
  2290. for(int i=0;i<comps.size();i++)
  2291. comps[i]->show(to);
  2292. }
  2293. void CMinorResDataBar::show(SDL_Surface * to)
  2294. {
  2295. blitAt(bg,pos.x,pos.y,to);
  2296. char buf[30];
  2297. for (int i=0;i<7;i++)
  2298. {
  2299. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2300. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2301. }
  2302. std::vector<std::string> temp;
  2303. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  2304. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  2305. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  2306. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  2307. CGI->generaltexth->allTexts[62]
  2308. +": %s, "
  2309. + CGI->generaltexth->allTexts[63]
  2310. + ": %s, "
  2311. + CGI->generaltexth->allTexts[64]
  2312. + ": %s",temp)
  2313. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2314. }
  2315. CMinorResDataBar::CMinorResDataBar()
  2316. {
  2317. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  2318. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2319. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2320. pos.x = 7;
  2321. pos.y = 575;
  2322. pos.w = bg->w;
  2323. pos.h = bg->h;
  2324. }
  2325. CMinorResDataBar::~CMinorResDataBar()
  2326. {
  2327. SDL_FreeSurface(bg);
  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. }