GUIClasses.cpp 190 KB

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