GUIClasses.cpp 184 KB

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