GUIClasses.cpp 179 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158
  1. #include "StdInc.h"
  2. #include "GUIClasses.h"
  3. #include "gui/SDL_Extensions.h"
  4. #include "CAdvmapInterface.h"
  5. #include "battle/CBattleInterface.h"
  6. #include "battle/CBattleInterfaceClasses.h"
  7. #include "../CCallback.h"
  8. #include "CCastleInterface.h"
  9. #include "CCreatureWindow.h"
  10. #include "gui/CCursorHandler.h"
  11. #include "CGameInfo.h"
  12. #include "CHeroWindow.h"
  13. #include "CMessage.h"
  14. #include "../lib/CConfigHandler.h"
  15. #include "battle/CCreatureAnimation.h"
  16. #include "CPlayerInterface.h"
  17. #include "Graphics.h"
  18. #include "CAnimation.h"
  19. #include "../lib/CArtHandler.h"
  20. #include "../lib/CBuildingHandler.h"
  21. #include "../lib/CGeneralTextHandler.h"
  22. #include "../lib/CHeroHandler.h"
  23. #include "../lib/CModHandler.h"
  24. #include "../lib/CObjectHandler.h"
  25. #include "../lib/CSpellHandler.h"
  26. #include "../lib/CTownHandler.h"
  27. #include "../lib/CondSh.h"
  28. #include "../lib/mapping/CMap.h"
  29. #include "mapHandler.h"
  30. #include "../lib/CStopWatch.h"
  31. #include "../lib/NetPacks.h"
  32. #include "CSpellWindow.h"
  33. #include "CHeroWindow.h"
  34. #include "CVideoHandler.h"
  35. #include "../lib/StartInfo.h"
  36. #include "CPreGame.h"
  37. #include "../lib/HeroBonus.h"
  38. #include "../lib/CCreatureHandler.h"
  39. #include "CMusicHandler.h"
  40. #include "../lib/BattleState.h"
  41. #include "../lib/CGameState.h"
  42. #include "../lib/GameConstants.h"
  43. #include "gui/CGuiHandler.h"
  44. /*
  45. * GUIClasses.cpp, part of VCMI engine
  46. *
  47. * Authors: listed in file AUTHORS in main folder
  48. *
  49. * License: GNU General Public License v2.0 or later
  50. * Full text of license available in license.txt file, in main folder
  51. *
  52. */
  53. using namespace boost::assign;
  54. using namespace CSDL_Ext;
  55. extern std::queue<SDL_Event> events;
  56. extern boost::mutex eventsM;
  57. std::list<CFocusable*> CFocusable::focusables;
  58. CFocusable * CFocusable::inputWithFocus;
  59. #undef min
  60. #undef max
  61. void CArmyTooltip::init(const InfoAboutArmy &army)
  62. {
  63. OBJ_CONSTRUCTION_CAPTURING_ALL;
  64. new CLabel(66, 2, FONT_SMALL, TOPLEFT, Colors::WHITE, army.name);
  65. std::vector<Point> slotsPos;
  66. slotsPos.push_back(Point(36,73));
  67. slotsPos.push_back(Point(72,73));
  68. slotsPos.push_back(Point(108,73));
  69. slotsPos.push_back(Point(18,122));
  70. slotsPos.push_back(Point(54,122));
  71. slotsPos.push_back(Point(90,122));
  72. slotsPos.push_back(Point(126,122));
  73. for(auto & slot : army.army)
  74. {
  75. if(slot.first.getNum() >= GameConstants::ARMY_SIZE)
  76. {
  77. logGlobal->warnStream() << "Warning: " << army.name << " has stack in slot " << slot.first;
  78. continue;
  79. }
  80. new CAnimImage("CPRSMALL", slot.second.type->iconIndex, 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y);
  81. std::string subtitle;
  82. if(army.army.isDetailed)
  83. subtitle = boost::lexical_cast<std::string>(slot.second.count);
  84. else
  85. {
  86. //if =0 - we have no information about stack size at all
  87. if (slot.second.count)
  88. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  89. }
  90. new CLabel(slotsPos[slot.first.getNum()].x + 17, slotsPos[slot.first.getNum()].y + 41, FONT_TINY, CENTER, Colors::WHITE, subtitle);
  91. }
  92. }
  93. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy &army):
  94. CIntObject(0, pos)
  95. {
  96. init(army);
  97. }
  98. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  99. CIntObject(0, pos)
  100. {
  101. init(InfoAboutArmy(army, true));
  102. }
  103. void CHeroTooltip::init(const InfoAboutHero &hero)
  104. {
  105. OBJ_CONSTRUCTION_CAPTURING_ALL;
  106. new CAnimImage("PortraitsLarge", hero.portrait, 0, 3, 2);
  107. if(hero.details)
  108. {
  109. for (size_t i = 0; i < hero.details->primskills.size(); i++)
  110. new CLabel(75 + 28 * i, 58, FONT_SMALL, CENTER, Colors::WHITE,
  111. boost::lexical_cast<std::string>(hero.details->primskills[i]));
  112. new CLabel(158, 98, FONT_TINY, CENTER, Colors::WHITE,
  113. boost::lexical_cast<std::string>(hero.details->mana));
  114. new CAnimImage("IMRL22", hero.details->morale + 3, 0, 5, 74);
  115. new CAnimImage("ILCK22", hero.details->luck + 3, 0, 5, 91);
  116. }
  117. }
  118. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  119. CArmyTooltip(pos, hero)
  120. {
  121. init(hero);
  122. }
  123. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  124. CArmyTooltip(pos, InfoAboutHero(hero, true))
  125. {
  126. init(InfoAboutHero(hero, true));
  127. }
  128. void CTownTooltip::init(const InfoAboutTown &town)
  129. {
  130. OBJ_CONSTRUCTION_CAPTURING_ALL;
  131. //order of icons in def: fort, citadel, castle, no fort
  132. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  133. new CAnimImage("ITMCLS", fortIndex, 0, 105, 31);
  134. assert(town.tType);
  135. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  136. new CAnimImage("itpt", iconIndex, 0, 3, 2);
  137. if(town.details)
  138. {
  139. new CAnimImage("ITMTLS", town.details->hallLevel, 0, 67, 31);
  140. if (town.details->goldIncome)
  141. new CLabel(157, 58, FONT_TINY, CENTER, Colors::WHITE,
  142. boost::lexical_cast<std::string>(town.details->goldIncome));
  143. if(town.details->garrisonedHero) //garrisoned hero icon
  144. new CPicture("TOWNQKGH", 149, 76);
  145. if(town.details->customRes)//silo is built
  146. {
  147. if (town.tType->primaryRes == Res::WOOD_AND_ORE )// wood & ore
  148. {
  149. new CAnimImage("SMALRES", Res::WOOD, 0, 7, 75);
  150. new CAnimImage("SMALRES", Res::ORE , 0, 7, 88);
  151. }
  152. else
  153. new CAnimImage("SMALRES", town.tType->primaryRes, 0, 7, 81);
  154. }
  155. }
  156. }
  157. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown &town):
  158. CArmyTooltip(pos, town)
  159. {
  160. init(town);
  161. }
  162. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town):
  163. CArmyTooltip(pos, InfoAboutTown(town, true))
  164. {
  165. init(InfoAboutTown(town, true));
  166. }
  167. void CGarrisonSlot::setHighlight(bool on)
  168. {
  169. if (on)
  170. selectionImage->enable(); //show
  171. else
  172. selectionImage->disable(); //hide
  173. }
  174. void CGarrisonSlot::hover (bool on)
  175. {
  176. ////Hoverable::hover(on);
  177. if(on)
  178. {
  179. std::string temp;
  180. if(creature)
  181. {
  182. if(owner->getSelection())
  183. {
  184. if(owner->getSelection() == this)
  185. {
  186. temp = CGI->generaltexth->tcommands[4]; //View %s
  187. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  188. }
  189. else if (owner->getSelection()->creature == creature)
  190. {
  191. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  192. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  193. }
  194. else if (owner->getSelection()->creature)
  195. {
  196. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  197. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->nameSing);
  198. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  199. }
  200. else
  201. {
  202. logGlobal->warnStream() << "Warning - shouldn't be - highlighted void slot "<<owner->getSelection();
  203. logGlobal->warnStream() << "Highlighted set to nullptr";
  204. owner->selectSlot(nullptr);
  205. }
  206. }
  207. else
  208. {
  209. if(upg)
  210. {
  211. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  212. }
  213. else if(owner->armedObjs[0] && owner->armedObjs[0]->ID == Obj::TOWN)
  214. {
  215. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  216. }
  217. else
  218. {
  219. temp = CGI->generaltexth->allTexts[481]; //Select %s
  220. }
  221. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  222. };
  223. }
  224. else
  225. {
  226. if(owner->getSelection())
  227. {
  228. const CArmedInstance *highl = owner->getSelection()->getObj();
  229. if( highl->needsLastStack() //we are moving stack from hero's
  230. && highl->stacksCount() == 1 //it's only stack
  231. && owner->getSelection()->upg != upg //we're moving it to the other garrison
  232. )
  233. {
  234. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  235. }
  236. else
  237. {
  238. temp = CGI->generaltexth->tcommands[6]; //Move %s
  239. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->nameSing);
  240. }
  241. }
  242. else
  243. {
  244. temp = CGI->generaltexth->tcommands[11]; //Empty
  245. }
  246. }
  247. GH.statusbar->print(temp);
  248. }
  249. else
  250. {
  251. GH.statusbar->clear();
  252. }
  253. }
  254. const CArmedInstance * CGarrisonSlot::getObj() const
  255. {
  256. return (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]);
  257. }
  258. bool CGarrisonSlot::our() const
  259. {
  260. return upg?(owner->owned[1]):(owner->owned[0]);
  261. }
  262. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  263. {
  264. if(down && creature)
  265. {
  266. GH.pushInt(createCreWindow(myStack, CCreatureWindow::ARMY));
  267. }
  268. }
  269. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  270. {
  271. if(down)
  272. {
  273. bool refr = false;
  274. if(owner->getSelection())
  275. {
  276. if(owner->getSelection() == this) //view info
  277. {
  278. UpgradeInfo pom;
  279. LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
  280. bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
  281. bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID && (getObj()->stacksCount()>1 || !getObj()->needsLastStack());
  282. std::function<void()> upgr = nullptr;
  283. std::function<void()> dism = nullptr;
  284. if(canUpgrade) upgr = [=] { LOCPLINT->cb->upgradeCreature(getObj(), ID, pom.newID[0]); };
  285. if(canDismiss) dism = [=] { LOCPLINT->cb->dismissCreature(getObj(), ID); };
  286. owner->selectSlot(nullptr);
  287. owner->setSplittingMode(false);
  288. for(auto & elem : owner->splitButtons)
  289. elem->block(true);
  290. redraw();
  291. refr = true;
  292. CIntObject *creWindow = createCreWindow(myStack, CCreatureWindow::HERO, upgr, dism, &pom);
  293. GH.pushInt(creWindow);
  294. }
  295. else
  296. {
  297. // Only allow certain moves if troops aren't removable or not ours.
  298. if ( ( owner->getSelection()->our()//our creature is selected
  299. || owner->getSelection()->creature == creature )//or we are rebalancing army
  300. && ( owner->removableUnits
  301. || (upg == 0 && ( owner->getSelection()->upg == 1 && !creature ) )
  302. || (upg == 1 && owner->getSelection()->upg == 1 ) ) )
  303. {
  304. //we want to split
  305. if((owner->getSplittingMode() || LOCPLINT->shiftPressed())
  306. && (!creature
  307. || (creature == owner->getSelection()->creature)))
  308. {
  309. owner->p2 = ID; //store the second stack pos
  310. owner->pb = upg;//store the second stack owner (up or down army)
  311. owner->setSplittingMode(false);
  312. int minLeft=0, minRight=0;
  313. if(upg != owner->getSelection()->upg) //not splitting within same army
  314. {
  315. if(owner->getSelection()->getObj()->stacksCount() == 1 //we're splitting away the last stack
  316. && owner->getSelection()->getObj()->needsLastStack() )
  317. {
  318. minLeft = 1;
  319. }
  320. if(getObj()->stacksCount() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
  321. && owner->getSelection()->creature == creature
  322. && getObj()->needsLastStack() )
  323. {
  324. minRight = 1;
  325. }
  326. }
  327. int countLeft = owner->getSelection()->myStack ? owner->getSelection()->myStack->count : 0;
  328. int countRight = myStack ? myStack->count : 0;
  329. GH.pushInt(new CSplitWindow(owner->getSelection()->creature, boost::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
  330. minLeft, minRight, countLeft, countRight));
  331. refr = true;
  332. }
  333. else if(creature != owner->getSelection()->creature) //swap
  334. {
  335. LOCPLINT->cb->swapCreatures(
  336. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  337. (!owner->getSelection()->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  338. ID,owner->getSelection()->ID);
  339. }
  340. else //merge
  341. {
  342. LOCPLINT->cb->mergeStacks(
  343. (!owner->getSelection()->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  344. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  345. owner->getSelection()->ID,ID);
  346. }
  347. }
  348. else // Highlight
  349. {
  350. if(creature)
  351. owner->selectSlot(this);
  352. redraw();
  353. refr = true;
  354. }
  355. }
  356. }
  357. else //highlight or drop artifact
  358. {
  359. bool artSelected = false;
  360. if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt())) //dirty solution
  361. {
  362. const CArtifactsOfHero::SCommonPart *commonInfo = chw->artSets.front()->commonInfo;
  363. if (const CArtifactInstance *art = commonInfo->src.art)
  364. {
  365. const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
  366. artSelected = true;
  367. ArtifactLocation src(srcHero, commonInfo->src.slotID);
  368. ArtifactLocation dst(myStack, ArtifactPosition::CREATURE_SLOT);
  369. if (art->canBePutAt(dst, true))
  370. { //equip clicked stack
  371. if(dst.getArt())
  372. {
  373. //creature can wear only one active artifact
  374. //if we are placing a new one, the old one will be returned to the hero's backpack
  375. LOCPLINT->cb->swapArtifacts(dst, ArtifactLocation(srcHero, dst.getArt()->firstBackpackSlot(srcHero)));
  376. }
  377. LOCPLINT->cb->swapArtifacts(src, dst);
  378. }
  379. }
  380. }
  381. if (!artSelected && creature)
  382. {
  383. owner->selectSlot(this);
  384. if(creature)
  385. {
  386. for(auto & elem : owner->splitButtons)
  387. elem->block(false);
  388. }
  389. }
  390. redraw();
  391. refr = true;
  392. }
  393. if(refr) {hover(false); hover(true); } //to refresh statusbar
  394. }
  395. }
  396. void CGarrisonSlot::update()
  397. {
  398. if (getObj() != nullptr)
  399. {
  400. addUsedEvents(LCLICK | RCLICK | HOVER);
  401. myStack = getObj()->getStackPtr(ID);
  402. creature = myStack ? myStack->type : nullptr;
  403. }
  404. else
  405. {
  406. removeUsedEvents(LCLICK | RCLICK | HOVER);
  407. myStack = nullptr;
  408. creature = nullptr;
  409. }
  410. if (creature)
  411. {
  412. creatureImage->enable();
  413. creatureImage->setFrame(creature->iconIndex);
  414. stackCount->enable();
  415. stackCount->setTxt(boost::lexical_cast<std::string>(myStack->count));
  416. }
  417. else
  418. {
  419. creatureImage->disable();
  420. stackCount->disable();
  421. }
  422. }
  423. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, int Upg, const CStackInstance * Creature):
  424. ID(IID),
  425. owner(Owner),
  426. myStack(Creature),
  427. creature(Creature ? Creature->type : nullptr),
  428. upg(Upg)
  429. {
  430. OBJ_CONSTRUCTION_CAPTURING_ALL;
  431. if (getObj())
  432. addUsedEvents(LCLICK | RCLICK | HOVER);
  433. pos.x += x;
  434. pos.y += y;
  435. std::string imgName = owner->smallIcons ? "cprsmall" : "TWCRPORT";
  436. creatureImage = new CAnimImage(imgName, creature ? creature->iconIndex : 0);
  437. if (!creature)
  438. creatureImage->disable();
  439. selectionImage = new CAnimImage(imgName, 1);
  440. selectionImage->disable();
  441. if(Owner->smallIcons)
  442. {
  443. pos.w = 32;
  444. pos.h = 32;
  445. }
  446. else
  447. {
  448. pos.w = 58;
  449. pos.h = 64;
  450. }
  451. stackCount = new CLabel(pos.w, pos.h, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, BOTTOMRIGHT, Colors::WHITE);
  452. if (!creature)
  453. stackCount->disable();
  454. else
  455. stackCount->setTxt(boost::lexical_cast<std::string>(myStack->count));
  456. }
  457. void CGarrisonInt::addSplitBtn(CAdventureMapButton * button)
  458. {
  459. addChild(button);
  460. button->recActions = defActions;
  461. splitButtons.push_back(button);
  462. button->block(getSelection() == nullptr);
  463. }
  464. void CGarrisonInt::createSet(std::vector<CGarrisonSlot*> &ret, const CCreatureSet * set, int posX, int posY, int distance, int Upg )
  465. {
  466. ret.resize(7);
  467. if (set)
  468. {
  469. for(auto & elem : set->Slots())
  470. {
  471. ret[elem.first.getNum()] = new CGarrisonSlot(this, posX + (elem.first.getNum()*distance), posY, elem.first, Upg, elem.second);
  472. }
  473. }
  474. for(int i=0; i<ret.size(); i++)
  475. if(!ret[i])
  476. ret[i] = new CGarrisonSlot(this, posX + (i*distance), posY, SlotID(i), Upg, nullptr);
  477. if (twoRows)
  478. for (int i=4; i<ret.size(); i++)
  479. {
  480. ret[i]->pos.x -= 126;
  481. ret[i]->pos.y += 37;
  482. };
  483. }
  484. void CGarrisonInt::createSlots()
  485. {
  486. OBJ_CONSTRUCTION_CAPTURING_ALL;
  487. int width = smallIcons? 32 : 58;
  488. createSet(slotsUp, armedObjs[0], 0, 0, width+interx, 0);
  489. createSet(slotsDown, armedObjs[1], garOffset.x, garOffset.y, width+interx, 1);
  490. }
  491. void CGarrisonInt::recreateSlots()
  492. {
  493. selectSlot(nullptr);
  494. setSplittingMode(false);
  495. for(auto & elem : splitButtons)
  496. elem->block(true);
  497. for(CGarrisonSlot * slot : slotsUp)
  498. slot->update();
  499. for(CGarrisonSlot * slot : slotsDown)
  500. slot->update();
  501. }
  502. void CGarrisonInt::splitClick()
  503. {
  504. if(!getSelection())
  505. return;
  506. setSplittingMode(!getSplittingMode());
  507. redraw();
  508. }
  509. void CGarrisonInt::splitStacks(int, int amountRight)
  510. {
  511. LOCPLINT->cb->splitStack(armedObjs[getSelection()->upg], armedObjs[pb], getSelection()->ID, p2, amountRight);
  512. }
  513. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset,
  514. SDL_Surface *pomsur, const Point& SurOffset,
  515. const CArmedInstance *s1, const CArmedInstance *s2,
  516. bool _removableUnits, bool smallImgs, bool _twoRows ) :
  517. highlighted(nullptr),
  518. inSplittingMode(false),
  519. interx(inx),
  520. garOffset(garsOffset),
  521. smallIcons(smallImgs),
  522. removableUnits(_removableUnits),
  523. twoRows(_twoRows)
  524. {
  525. setArmy(s1, false);
  526. setArmy(s2, true);
  527. pos.x += x;
  528. pos.y += y;
  529. createSlots();
  530. }
  531. const CGarrisonSlot * CGarrisonInt::getSelection()
  532. {
  533. return highlighted;
  534. }
  535. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  536. {
  537. if (slot != highlighted)
  538. {
  539. if (highlighted)
  540. highlighted->setHighlight(false);
  541. highlighted = slot;
  542. for (auto button : splitButtons)
  543. button->block(highlighted == nullptr);
  544. if (highlighted)
  545. highlighted->setHighlight(true);
  546. }
  547. }
  548. void CGarrisonInt::setSplittingMode(bool on)
  549. {
  550. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  551. if (inSplittingMode || on)
  552. {
  553. for(CGarrisonSlot * slot : slotsUp)
  554. slot->setHighlight( ( on && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  555. for(CGarrisonSlot * slot : slotsDown)
  556. slot->setHighlight( ( on && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  557. inSplittingMode = on;
  558. }
  559. }
  560. bool CGarrisonInt::getSplittingMode()
  561. {
  562. return inSplittingMode;
  563. }
  564. void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
  565. {
  566. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;
  567. armedObjs[bottomGarrison] = army;
  568. }
  569. CInfoWindow::CInfoWindow(std::string Text, PlayerColor player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  570. {
  571. OBJ_CONSTRUCTION_CAPTURING_ALL;
  572. type |= BLOCK_ADV_HOTKEYS;
  573. ID = QueryID(-1);
  574. for(auto & Button : Buttons)
  575. {
  576. CAdventureMapButton *button = new CAdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Button.first);
  577. button->borderColor = Colors::METALLIC_GOLD;
  578. button->borderEnabled = true;
  579. button->callback.add(Button.second); //each button will close the window apart from call-defined actions
  580. buttons.push_back(button);
  581. }
  582. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  583. if(!text->slider)
  584. {
  585. text->pos.w = text->maxW;
  586. text->pos.h = text->maxH;
  587. }
  588. if(buttons.size())
  589. {
  590. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  591. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  592. }
  593. for(auto & comp : comps)
  594. {
  595. comp->recActions = 0xff;
  596. addChild(comp);
  597. comp->recActions &= ~(SHOWALL | UPDATE);
  598. components.push_back(comp);
  599. }
  600. setDelComps(delComps);
  601. CMessage::drawIWindow(this,Text,player);
  602. }
  603. CInfoWindow::CInfoWindow()
  604. {
  605. ID = QueryID(-1);
  606. setDelComps(false);
  607. text = nullptr;
  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 (auto & elem : components)
  624. removeChild(elem);
  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<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, PlayerColor 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<CComponent*>(), pom, DelComps);
  639. for(auto & elem : onYes.funcs)
  640. temp->buttons[0]->callback += elem;
  641. for(auto & elem : onNo.funcs)
  642. temp->buttons[1]->callback += elem;
  643. GH.pushInt(temp);
  644. }
  645. CInfoWindow * CInfoWindow::create(const std::string &text, PlayerColor playerID /*= 1*/, const std::vector<CComponent*> *components /*= nullptr*/, 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<CComponent*>(), pom, DelComps);
  650. return ret;
  651. }
  652. std::string CInfoWindow::genText(std::string title, std::string description)
  653. {
  654. return std::string("{") + title + "}" + "\n\n" + description;
  655. }
  656. void CInfoWindow::setDelComps(bool DelComps)
  657. {
  658. delComps = DelComps;
  659. for(CComponent *comp : components)
  660. {
  661. if(delComps)
  662. comp->recActions |= DISPOSE;
  663. else
  664. comp->recActions &= ~DISPOSE;
  665. }
  666. }
  667. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  668. :free(Free),bitmap(Bitmap)
  669. {
  670. init(x, y);
  671. }
  672. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  673. : free(Free),bitmap(Bitmap)
  674. {
  675. switch(alignment)
  676. {
  677. case BOTTOMRIGHT:
  678. init(p.x - Bitmap->w, p.y - Bitmap->h);
  679. break;
  680. case CENTER:
  681. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  682. break;
  683. case TOPLEFT:
  684. init(p.x, p.y);
  685. break;
  686. default:
  687. assert(0); //not implemented
  688. }
  689. }
  690. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  691. {
  692. CCS->curh->hide();
  693. free=Free;
  694. bitmap=Bitmap;
  695. if(bitmap)
  696. {
  697. pos.x = screen->w/2 - bitmap->w/2;
  698. pos.y = screen->h/2 - bitmap->h/2;
  699. pos.h = bitmap->h;
  700. pos.w = bitmap->w;
  701. }
  702. }
  703. void CInfoPopup::close()
  704. {
  705. if(free)
  706. SDL_FreeSurface(bitmap);
  707. GH.popIntTotally(this);
  708. }
  709. void CInfoPopup::show(SDL_Surface * to)
  710. {
  711. blitAt(bitmap,pos.x,pos.y,to);
  712. }
  713. CInfoPopup::~CInfoPopup()
  714. {
  715. CCS->curh->show();
  716. }
  717. void CInfoPopup::init(int x, int y)
  718. {
  719. CCS->curh->hide();
  720. pos.x = x;
  721. pos.y = y;
  722. pos.h = bitmap->h;
  723. pos.w = bitmap->w;
  724. // Put the window back on screen if necessary
  725. vstd::amax(pos.x, 0);
  726. vstd::amax(pos.y, 0);
  727. vstd::amin(pos.x, screen->w - bitmap->w);
  728. vstd::amin(pos.y, screen->h - bitmap->h);
  729. }
  730. CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize):
  731. image(nullptr),
  732. perDay(false)
  733. {
  734. addUsedEvents(RCLICK);
  735. init(Type, Subtype, Val, imageSize);
  736. }
  737. CComponent::CComponent(const Component &c):
  738. image(nullptr),
  739. perDay(false)
  740. {
  741. addUsedEvents(RCLICK);
  742. if(c.id == Component::RESOURCE && c.when==-1)
  743. perDay = true;
  744. init((Etype)c.id,c.subtype,c.val, large);
  745. }
  746. void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
  747. {
  748. OBJ_CONSTRUCTION_CAPTURING_ALL;
  749. compType = Type;
  750. subtype = Subtype;
  751. val = Val;
  752. size = imageSize;
  753. assert(compType < typeInvalid);
  754. assert(size < sizeInvalid);
  755. setSurface(getFileName()[size], getIndex());
  756. pos.w = image->pos.w;
  757. pos.h = image->pos.h;
  758. EFonts font = FONT_SMALL;
  759. if (imageSize < small)
  760. font = FONT_TINY; //other sizes?
  761. pos.h += 4; //distance between text and image
  762. std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(80, pos.w), font);
  763. for(auto & line : textLines)
  764. {
  765. int height = graphics->fonts[font]->getLineHeight();
  766. auto label = new CLabel(pos.w/2, pos.h + height/2, font, CENTER, Colors::WHITE, line);
  767. pos.h += height;
  768. if (label->pos.w > pos.w)
  769. {
  770. pos.x -= (label->pos.w - pos.w)/2;
  771. pos.w = label->pos.w;
  772. }
  773. }
  774. }
  775. const std::vector<std::string> CComponent::getFileName()
  776. {
  777. static const std::string primSkillsArr [] = {"PSKIL32", "PSKIL32", "PSKIL42", "PSKILL"};
  778. static const std::string secSkillsArr [] = {"SECSK32", "SECSK32", "SECSKILL", "SECSK82"};
  779. static const std::string resourceArr [] = {"SMALRES", "RESOURCE", "RESOUR82", "RESOUR82"};
  780. static const std::string creatureArr [] = {"CPRSMALL", "CPRSMALL", "TWCRPORT", "TWCRPORT"};
  781. static const std::string artifactArr[] = {"Artifact", "Artifact", "Artifact", "Artifact"};
  782. static const std::string spellsArr [] = {"SpellInt", "SpellInt", "SPELLSCR", "SPELLSCR"};
  783. static const std::string moraleArr [] = {"IMRL22", "IMRL30", "IMRL42", "imrl82"};
  784. static const std::string luckArr [] = {"ILCK22", "ILCK30", "ILCK42", "ilck82"};
  785. static const std::string heroArr [] = {"PortraitsSmall", "PortraitsSmall", "PortraitsLarge", "PortraitsLarge"};
  786. static const std::string flagArr [] = {"CREST58", "CREST58", "CREST58", "CREST58"};
  787. auto gen = [](const std::string * arr)
  788. {
  789. return std::vector<std::string>(arr, arr + 4);
  790. };
  791. switch(compType)
  792. {
  793. case primskill: return gen(primSkillsArr);
  794. case secskill: return gen(secSkillsArr);
  795. case resource: return gen(resourceArr);
  796. case creature: return gen(creatureArr);
  797. case artifact: return gen(artifactArr);
  798. case experience: return gen(primSkillsArr);
  799. case spell: return gen(spellsArr);
  800. case morale: return gen(moraleArr);
  801. case luck: return gen(luckArr);
  802. case building: return std::vector<std::string>(4, CGI->townh->factions[subtype]->town->clientInfo.buildingsIcons);
  803. case hero: return gen(heroArr);
  804. case flag: return gen(flagArr);
  805. }
  806. assert(0);
  807. return std::vector<std::string>();
  808. }
  809. size_t CComponent::getIndex()
  810. {
  811. switch(compType)
  812. {
  813. case primskill: return subtype;
  814. case secskill: return subtype*3 + 3 + val - 1;
  815. case resource: return subtype;
  816. case creature: return CGI->creh->creatures[subtype]->iconIndex;
  817. case artifact: return CGI->arth->artifacts[subtype]->iconIndex;
  818. case experience: return 4;
  819. case spell: return subtype;
  820. case morale: return val+3;
  821. case luck: return val+3;
  822. case building: return val;
  823. case hero: return CGI->heroh->heroes[subtype]->imageIndex;
  824. case flag: return subtype;
  825. }
  826. assert(0);
  827. return 0;
  828. }
  829. std::string CComponent::getDescription()
  830. {
  831. switch (compType)
  832. {
  833. case primskill: return (subtype < 4)? CGI->generaltexth->arraytxt[2+subtype] //Primary skill
  834. : CGI->generaltexth->allTexts[149]; //mana
  835. case secskill: return CGI->generaltexth->skillInfoTexts[subtype][val-1];
  836. case resource: return CGI->generaltexth->allTexts[242];
  837. case creature: return "";
  838. case artifact: return CGI->arth->artifacts[subtype]->Description();
  839. case experience: return CGI->generaltexth->allTexts[241];
  840. case spell: return CGI->spellh->spells[subtype]->descriptions[val];
  841. case morale: return CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  842. case luck: return CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  843. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Description();
  844. case hero: return CGI->heroh->heroes[subtype]->name;
  845. case flag: return "";
  846. }
  847. assert(0);
  848. return "";
  849. }
  850. std::string CComponent::getSubtitle()
  851. {
  852. if (!perDay)
  853. return getSubtitleInternal();
  854. std::string ret = CGI->generaltexth->allTexts[3];
  855. boost::replace_first(ret, "%d", getSubtitleInternal());
  856. return ret;
  857. }
  858. std::string CComponent::getSubtitleInternal()
  859. {
  860. //FIXME: some of these are horrible (e.g creature)
  861. switch(compType)
  862. {
  863. case primskill: return boost::str(boost::format("%+d %s") % val % (subtype < 4 ? CGI->generaltexth->primarySkillNames[subtype] : CGI->generaltexth->allTexts[387]));
  864. case secskill: return CGI->generaltexth->levels[val-1] + "\n" + CGI->generaltexth->skillName[subtype];
  865. case resource: return boost::lexical_cast<std::string>(val);
  866. case creature: return (val? boost::lexical_cast<std::string>(val) + " " : "") + CGI->creh->creatures[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  867. case artifact: return CGI->arth->artifacts[subtype]->Name();
  868. case experience: return (subtype && val==1) ? CGI->generaltexth->allTexts[442] : boost::lexical_cast<std::string>(val);
  869. case spell: return CGI->spellh->spells[subtype]->name;
  870. case morale: return "";
  871. case luck: return "";
  872. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Name();
  873. case hero: return CGI->heroh->heroes[subtype]->name;
  874. case flag: return CGI->generaltexth->capColors[subtype];
  875. }
  876. assert(0);
  877. return "";
  878. }
  879. void CComponent::setSurface(std::string defName, int imgPos)
  880. {
  881. OBJ_CONSTRUCTION_CAPTURING_ALL;
  882. vstd::clear_pointer(image);
  883. image = new CAnimImage(defName, imgPos);
  884. }
  885. void CComponent::clickRight(tribool down, bool previousState)
  886. {
  887. if(!getDescription().empty())
  888. adventureInt->handleRightClick(getDescription(), down);
  889. }
  890. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  891. {
  892. if (down)
  893. {
  894. if(onSelect)
  895. onSelect();
  896. }
  897. }
  898. void CSelectableComponent::init()
  899. {
  900. selected = false;
  901. }
  902. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  903. CComponent(c),onSelect(OnSelect)
  904. {
  905. type |= REDRAW_PARENT;
  906. addUsedEvents(LCLICK | KEYBOARD);
  907. init();
  908. }
  909. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  910. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  911. {
  912. type |= REDRAW_PARENT;
  913. addUsedEvents(LCLICK | KEYBOARD);
  914. init();
  915. }
  916. void CSelectableComponent::select(bool on)
  917. {
  918. if(on != selected)
  919. {
  920. selected = on;
  921. redraw();
  922. }
  923. }
  924. void CSelectableComponent::showAll(SDL_Surface * to)
  925. {
  926. CComponent::showAll(to);
  927. if(selected)
  928. {
  929. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  930. }
  931. }
  932. void CComponentBox::selectionChanged(CSelectableComponent * newSelection)
  933. {
  934. if (newSelection == selected)
  935. return;
  936. if (selected)
  937. selected->select(false);
  938. selected = newSelection;
  939. if (onSelect)
  940. onSelect(selectedIndex());
  941. if (selected)
  942. selected->select(true);
  943. }
  944. int CComponentBox::selectedIndex()
  945. {
  946. if (selected)
  947. return std::find(components.begin(), components.end(), selected) - components.begin();
  948. return -1;
  949. }
  950. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  951. {
  952. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  953. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  954. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  955. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  956. }
  957. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  958. {
  959. static const int betweenImagesMin = 20;
  960. static const int betweenSubtitlesMin = 10;
  961. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  962. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  963. int subtitlesOffset = leftSubtitle + rightSubtitle;
  964. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  965. }
  966. void CComponentBox::placeComponents(bool selectable)
  967. {
  968. static const int betweenRows = 22;
  969. OBJ_CONSTRUCTION_CAPTURING_ALL;
  970. if (components.empty())
  971. return;
  972. //prepare components
  973. for(auto & comp : components)
  974. {
  975. addChild(comp);
  976. comp->moveTo(Point(pos.x, pos.y));
  977. }
  978. struct RowData
  979. {
  980. size_t comps;
  981. int width;
  982. int height;
  983. RowData (size_t Comps, int Width, int Height):
  984. comps(Comps), width (Width), height (Height){};
  985. };
  986. std::vector<RowData> rows;
  987. rows.push_back (RowData (0,0,0));
  988. //split components in rows
  989. CComponent * prevComp = nullptr;
  990. for(CComponent * comp : components)
  991. {
  992. //make sure that components are smaller than our width
  993. //assert(pos.w == 0 || pos.w < comp->pos.w);
  994. const int distance = prevComp ? getDistance(prevComp, comp) : 0;
  995. //start next row
  996. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  997. || rows.back().comps >= 4) // no more than 4 comps per row
  998. {
  999. prevComp = nullptr;
  1000. rows.push_back (RowData (0,0,0));
  1001. }
  1002. if (prevComp)
  1003. rows.back().width += distance;
  1004. rows.back().comps++;
  1005. rows.back().width += comp->pos.w;
  1006. vstd::amax(rows.back().height, comp->pos.h);
  1007. prevComp = comp;
  1008. }
  1009. if (pos.w == 0)
  1010. {
  1011. for(auto & row : rows)
  1012. vstd::amax(pos.w, row.width);
  1013. }
  1014. int height = (rows.size() - 1) * betweenRows;
  1015. for(auto & row : rows)
  1016. height += row.height;
  1017. //assert(pos.h == 0 || pos.h < height);
  1018. if (pos.h == 0)
  1019. pos.h = height;
  1020. auto iter = components.begin();
  1021. int currentY = (pos.h - height) / 2;
  1022. //move components to their positions
  1023. for (auto & rows_row : rows)
  1024. {
  1025. // amount of free space we may add on each side of every component
  1026. int freeSpace = (pos.w - rows_row.width) / (rows_row.comps * 2);
  1027. prevComp = nullptr;
  1028. int currentX = 0;
  1029. for (size_t col = 0; col < rows_row.comps; col++)
  1030. {
  1031. currentX += freeSpace;
  1032. if (prevComp)
  1033. {
  1034. if (selectable)
  1035. {
  1036. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp, *iter);
  1037. new CLabel(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]);
  1038. }
  1039. currentX += getDistance(prevComp, *iter);
  1040. }
  1041. (*iter)->moveBy(Point(currentX, currentY));
  1042. currentX += (*iter)->pos.w;
  1043. currentX += freeSpace;
  1044. prevComp = *(iter++);
  1045. }
  1046. currentY += rows_row.height + betweenRows;
  1047. }
  1048. }
  1049. CComponentBox::CComponentBox(CComponent * _components, Rect position):
  1050. components(1, _components),
  1051. selected(nullptr)
  1052. {
  1053. type |= REDRAW_PARENT;
  1054. pos = position + pos;
  1055. placeComponents(false);
  1056. }
  1057. CComponentBox::CComponentBox(std::vector<CComponent *> _components, Rect position):
  1058. components(_components),
  1059. selected(nullptr)
  1060. {
  1061. type |= REDRAW_PARENT;
  1062. pos = position + pos;
  1063. placeComponents(false);
  1064. }
  1065. CComponentBox::CComponentBox(std::vector<CSelectableComponent *> _components, Rect position, std::function<void(int newID)> _onSelect):
  1066. components(_components.begin(), _components.end()),
  1067. selected(nullptr),
  1068. onSelect(_onSelect)
  1069. {
  1070. type |= REDRAW_PARENT;
  1071. pos = position + pos;
  1072. placeComponents(true);
  1073. assert(!components.empty());
  1074. int key = SDLK_1;
  1075. for(auto & comp : _components)
  1076. {
  1077. comp->onSelect = boost::bind(&CComponentBox::selectionChanged, this, comp);
  1078. comp->assignedKeys.insert(key++);
  1079. }
  1080. selectionChanged(_components.front());
  1081. }
  1082. void CSelWindow::selectionChange(unsigned to)
  1083. {
  1084. for (unsigned i=0;i<components.size();i++)
  1085. {
  1086. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  1087. if (!pom)
  1088. continue;
  1089. pom->select(i==to);
  1090. }
  1091. redraw();
  1092. }
  1093. CSelWindow::CSelWindow(const std::string &Text, PlayerColor player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, QueryID askID)
  1094. {
  1095. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1096. ID = askID;
  1097. for(int i=0;i<Buttons.size();i++)
  1098. {
  1099. buttons.push_back(new CAdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  1100. if(!i && askID.getNum() >= 0)
  1101. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  1102. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  1103. }
  1104. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  1105. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  1106. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  1107. if(buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
  1108. buttons.back()->callback += boost::bind(&CCallback::selectionMade,LOCPLINT->cb.get(),0,askID);
  1109. for(int i=0;i<comps.size();i++)
  1110. {
  1111. comps[i]->recActions = 255;
  1112. addChild(comps[i]);
  1113. components.push_back(comps[i]);
  1114. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  1115. if(i<9)
  1116. comps[i]->assignedKeys.insert(SDLK_1+i);
  1117. }
  1118. CMessage::drawIWindow(this, Text, player);
  1119. }
  1120. void CSelWindow::madeChoice()
  1121. {
  1122. if(ID.getNum() < 0)
  1123. return;
  1124. int ret = -1;
  1125. for (int i=0;i<components.size();i++)
  1126. {
  1127. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  1128. {
  1129. ret = i;
  1130. }
  1131. }
  1132. LOCPLINT->cb->selectionMade(ret+1,ID);
  1133. }
  1134. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  1135. {
  1136. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1137. pos.x+=x;
  1138. pos.y+=y;
  1139. TFaction faction = cre->faction;
  1140. assert(CGI->townh->factions.size() > faction);
  1141. if(Big)
  1142. bg = new CPicture(CGI->townh->factions[faction]->creatureBg130);
  1143. else
  1144. bg = new CPicture(CGI->townh->factions[faction]->creatureBg120);
  1145. bg->needRefresh = true;
  1146. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  1147. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  1148. anim->startPreview(cre->hasBonusOfType(Bonus::SIEGE_WEAPON));
  1149. pos.w = bg->pos.w;
  1150. pos.h = bg->pos.h;
  1151. }
  1152. CRecruitmentWindow::CCreatureCard::CCreatureCard(CRecruitmentWindow *window, const CCreature *crea, int totalAmount):
  1153. CIntObject(LCLICK | RCLICK),
  1154. parent(window),
  1155. selected(false),
  1156. creature(crea),
  1157. amount(totalAmount)
  1158. {
  1159. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1160. pic = new CCreaturePic(1,1, creature, true, true);
  1161. // 1 + 1 px for borders
  1162. pos.w = pic->pos.w + 2;
  1163. pos.h = pic->pos.h + 2;
  1164. }
  1165. void CRecruitmentWindow::CCreatureCard::select(bool on)
  1166. {
  1167. selected = on;
  1168. redraw();
  1169. }
  1170. void CRecruitmentWindow::CCreatureCard::clickLeft(tribool down, bool previousState)
  1171. {
  1172. if (down)
  1173. parent->select(this);
  1174. }
  1175. void CRecruitmentWindow::CCreatureCard::clickRight(tribool down, bool previousState)
  1176. {
  1177. if (down)
  1178. GH.pushInt(createCreWindow(creature->idNumber, CCreatureWindow::OTHER, 0));
  1179. }
  1180. void CRecruitmentWindow::CCreatureCard::showAll(SDL_Surface * to)
  1181. {
  1182. CIntObject::showAll(to);
  1183. if (selected)
  1184. drawBorder(to, pos, int3(248, 0, 0));
  1185. else
  1186. drawBorder(to, pos, int3(232, 212, 120));
  1187. }
  1188. CRecruitmentWindow::CCostBox::CCostBox(Rect position, std::string title)
  1189. {
  1190. type |= REDRAW_PARENT;
  1191. pos = position + pos;
  1192. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1193. new CLabel(pos.w/2, 10, FONT_SMALL, CENTER, Colors::WHITE, title);
  1194. }
  1195. void CRecruitmentWindow::CCostBox::set(TResources res)
  1196. {
  1197. //just update values
  1198. for(auto & item : resources)
  1199. {
  1200. item.second.first->setTxt(boost::lexical_cast<std::string>(res[item.first]));
  1201. }
  1202. }
  1203. void CRecruitmentWindow::CCostBox::createItems(TResources res)
  1204. {
  1205. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1206. for(auto & curr : resources)
  1207. {
  1208. delete curr.second.first;
  1209. delete curr.second.second;
  1210. }
  1211. resources.clear();
  1212. TResources::nziterator iter(res);
  1213. while (iter.valid())
  1214. {
  1215. CAnimImage * image = new CAnimImage("RESOURCE", iter->resType);
  1216. CLabel * text = new CLabel(15, 43, FONT_SMALL, CENTER, Colors::WHITE, "0");
  1217. resources.insert(std::make_pair(iter->resType, std::make_pair(text, image)));
  1218. iter++;
  1219. }
  1220. if (!resources.empty())
  1221. {
  1222. int curx = pos.w / 2 - (16 * resources.size()) - (8 * (resources.size() - 1));
  1223. //reverse to display gold as first resource
  1224. for (auto & res : boost::adaptors::reverse(resources))
  1225. {
  1226. res.second.first->moveBy(Point(curx, 22));
  1227. res.second.second->moveBy(Point(curx, 22));
  1228. curx += 48;
  1229. }
  1230. }
  1231. redraw();
  1232. }
  1233. void CRecruitmentWindow::select(CCreatureCard *card)
  1234. {
  1235. if (card == selected)
  1236. return;
  1237. if (selected)
  1238. selected->select(false);
  1239. selected = card;
  1240. if (selected)
  1241. selected->select(true);
  1242. if (card)
  1243. {
  1244. si32 maxAmount = card->creature->maxAmount(LOCPLINT->cb->getResourceAmount());
  1245. vstd::amin(maxAmount, card->amount);
  1246. slider->setAmount(maxAmount);
  1247. if(slider->value != maxAmount)
  1248. slider->moveTo(maxAmount);
  1249. else // if slider already at 0 - emulate call to sliderMoved()
  1250. sliderMoved(maxAmount);
  1251. costPerTroopValue->createItems(card->creature->cost);
  1252. totalCostValue->createItems(card->creature->cost);
  1253. costPerTroopValue->set(card->creature->cost);
  1254. totalCostValue->set(card->creature->cost * maxAmount);
  1255. //Recruit %s
  1256. title->setTxt(boost::str(boost::format(CGI->generaltexth->tcommands[21]) % card->creature->namePl));
  1257. maxButton->block(maxAmount == 0);
  1258. slider->block(maxAmount == 0);
  1259. }
  1260. }
  1261. void CRecruitmentWindow::buy()
  1262. {
  1263. CreatureID crid = selected->creature->idNumber;
  1264. SlotID dstslot = dst-> getSlotFor(crid);
  1265. if(!dstslot.validSlot() && !vstd::contains(CGI->arth->bigArtifacts,CGI->arth->creatureToMachineID(crid))) //no available slot
  1266. {
  1267. std::string txt;
  1268. if(dst->ID == Obj::HERO)
  1269. {
  1270. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1271. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  1272. }
  1273. else
  1274. {
  1275. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1276. }
  1277. LOCPLINT->showInfoDialog(txt);
  1278. return;
  1279. }
  1280. onRecruit(crid, slider->value);
  1281. if(level >= 0)
  1282. close();
  1283. }
  1284. void CRecruitmentWindow::showAll(SDL_Surface * to)
  1285. {
  1286. CWindowObject::showAll(to);
  1287. // recruit\total values
  1288. drawBorder(to, pos.x + 172, pos.y + 222, 67, 42, int3(239,215,123));
  1289. drawBorder(to, pos.x + 246, pos.y + 222, 67, 42, int3(239,215,123));
  1290. //cost boxes
  1291. drawBorder(to, pos.x + 64, pos.y + 222, 99, 76, int3(239,215,123));
  1292. drawBorder(to, pos.x + 322, pos.y + 222, 99, 76, int3(239,215,123));
  1293. //buttons borders
  1294. drawBorder(to, pos.x + 133, pos.y + 312, 66, 34, int3(173,142,66));
  1295. drawBorder(to, pos.x + 211, pos.y + 312, 66, 34, int3(173,142,66));
  1296. drawBorder(to, pos.x + 289, pos.y + 312, 66, 34, int3(173,142,66));
  1297. }
  1298. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const std::function<void(CreatureID,int)> &Recruit, int y_offset):
  1299. CWindowObject(PLAYER_COLORED, "TPRCRT"),
  1300. onRecruit(Recruit),
  1301. level(Level),
  1302. dst(Dst),
  1303. selected(nullptr),
  1304. dwelling(Dwelling)
  1305. {
  1306. moveBy(Point(0, y_offset));
  1307. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1308. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1309. slider = new CSlider(176,279,135,nullptr,0,0,0,true);
  1310. slider->moved = boost::bind(&CRecruitmentWindow::sliderMoved,this, _1);
  1311. maxButton = new CAdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CSlider::moveToMax,slider),134,313,"IRCBTNS.DEF",SDLK_m);
  1312. buyButton = new CAdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::buy,this),212,313,"IBY6432.DEF",SDLK_RETURN);
  1313. cancelButton = new CAdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::close,this),290,313,"ICN6432.DEF",SDLK_ESCAPE);
  1314. title = new CLabel(243, 32, FONT_BIG, CENTER, Colors::YELLOW);
  1315. availableValue = new CLabel(205, 253, FONT_SMALL, CENTER, Colors::WHITE);
  1316. toRecruitValue = new CLabel(279, 253, FONT_SMALL, CENTER, Colors::WHITE);
  1317. costPerTroopValue = new CCostBox(Rect(65, 222, 97, 74), CGI->generaltexth->allTexts[346]);
  1318. totalCostValue = new CCostBox(Rect(323, 222, 97, 74), CGI->generaltexth->allTexts[466]);
  1319. new CLabel(205, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[465]); //available t
  1320. new CLabel(279, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[16]); //recruit t
  1321. availableCreaturesChanged();
  1322. }
  1323. void CRecruitmentWindow::availableCreaturesChanged()
  1324. {
  1325. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1326. size_t selectedIndex = 0;
  1327. if (!cards.empty() && selected) // find position of selected item
  1328. selectedIndex = std::find(cards.begin(), cards.end(), selected) - cards.begin();
  1329. //deselect card
  1330. select(nullptr);
  1331. //delete old cards
  1332. for(auto & card : cards)
  1333. delete card;
  1334. cards.clear();
  1335. for(int i=0; i<dwelling->creatures.size(); i++)
  1336. {
  1337. //find appropriate level
  1338. if(level >= 0 && i != level)
  1339. continue;
  1340. int amount = dwelling->creatures[i].first;
  1341. //create new cards
  1342. for(auto & creature : boost::adaptors::reverse(dwelling->creatures[i].second))
  1343. cards.push_back(new CCreatureCard(this, CGI->creh->creatures[creature], amount));
  1344. }
  1345. assert(!cards.empty());
  1346. const int creatureWidth = 102;
  1347. //normal distance between cards - 18px
  1348. int requiredSpace = 18;
  1349. //maximum distance we can use without reaching window borders
  1350. int availableSpace = pos.w - 50 - creatureWidth * cards.size();
  1351. if (cards.size() > 1) // avoid division by zero
  1352. availableSpace /= cards.size() - 1;
  1353. else
  1354. availableSpace = 0;
  1355. assert(availableSpace >= 0);
  1356. const int spaceBetween = std::min(requiredSpace, availableSpace);
  1357. const int totalCreatureWidth = spaceBetween + creatureWidth;
  1358. //now we know total amount of cards and can move them to correct position
  1359. int curx = pos.w / 2 - (creatureWidth*cards.size()/2) - (spaceBetween*(cards.size()-1)/2);
  1360. for(auto & card : cards)
  1361. {
  1362. card->moveBy(Point(curx, 64));
  1363. curx += totalCreatureWidth;
  1364. }
  1365. //restore selection
  1366. select(cards[selectedIndex]);
  1367. if(slider->value == slider->amount)
  1368. slider->moveTo(slider->amount);
  1369. else // if slider already at 0 - emulate call to sliderMoved()
  1370. sliderMoved(slider->amount);
  1371. }
  1372. void CRecruitmentWindow::sliderMoved(int to)
  1373. {
  1374. if (!selected)
  1375. return;
  1376. buyButton->block(!to);
  1377. availableValue->setTxt(boost::lexical_cast<std::string>(selected->amount - to));
  1378. toRecruitValue->setTxt(boost::lexical_cast<std::string>(to));
  1379. totalCostValue->set(selected->creature->cost * to);
  1380. }
  1381. CSplitWindow::CSplitWindow(const CCreature * creature, std::function<void(int, int)> callback_,
  1382. int leftMin_, int rightMin_, int leftAmount_, int rightAmount_):
  1383. CWindowObject(PLAYER_COLORED, "GPUCRDIV"),
  1384. callback(callback_),
  1385. leftAmount(leftAmount_),
  1386. rightAmount(rightAmount_),
  1387. leftMin(leftMin_),
  1388. rightMin(rightMin_),
  1389. slider(nullptr)
  1390. {
  1391. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1392. int total = leftAmount + rightAmount;
  1393. int leftMax = total - rightMin;
  1394. int rightMax = total - leftMin;
  1395. ok = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::apply, this), 20, 263, "IOK6432", SDLK_RETURN);
  1396. cancel = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::close, this), 214, 263, "ICN6432", SDLK_ESCAPE);
  1397. int sliderPositions = total - leftMin - rightMin;
  1398. leftInput = new CTextInput(Rect(20, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, true));
  1399. rightInput = new CTextInput(Rect(176, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, false));
  1400. //add filters to allow only number input
  1401. leftInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax));
  1402. rightInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax));
  1403. leftInput->setTxt(boost::lexical_cast<std::string>(leftAmount), false);
  1404. rightInput->setTxt(boost::lexical_cast<std::string>(rightAmount), false);
  1405. animLeft = new CCreaturePic(20, 54, creature, true, false);
  1406. animRight = new CCreaturePic(177, 54,creature, true, false);
  1407. slider = new CSlider(21, 194, 257, boost::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPositions, rightAmount - rightMin, true);
  1408. std::string title = CGI->generaltexth->allTexts[256];
  1409. boost::algorithm::replace_first(title,"%s", creature->namePl);
  1410. new CLabel(150, 34, FONT_BIG, CENTER, Colors::YELLOW, title);
  1411. }
  1412. void CSplitWindow::setAmountText(std::string text, bool left)
  1413. {
  1414. try
  1415. {
  1416. setAmount(boost::lexical_cast<int>(text), left);
  1417. slider->moveTo(rightAmount - rightMin);
  1418. }
  1419. catch(boost::bad_lexical_cast &)
  1420. {
  1421. }
  1422. }
  1423. void CSplitWindow::setAmount(int value, bool left)
  1424. {
  1425. int total = leftAmount + rightAmount;
  1426. leftAmount = left ? value : total - value;
  1427. rightAmount = left ? total - value : value;
  1428. leftInput->setTxt(boost::lexical_cast<std::string>(leftAmount));
  1429. rightInput->setTxt(boost::lexical_cast<std::string>(rightAmount));
  1430. }
  1431. void CSplitWindow::apply()
  1432. {
  1433. callback(leftAmount, rightAmount);
  1434. close();
  1435. }
  1436. void CSplitWindow::sliderMoved(int to)
  1437. {
  1438. setAmount(rightMin + to, false);
  1439. }
  1440. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, std::function<void(ui32)> callback):
  1441. CWindowObject(PLAYER_COLORED, "LVLUPBKG"),
  1442. cb(callback)
  1443. {
  1444. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1445. LOCPLINT->showingDialog->setn(true);
  1446. new CAnimImage("PortraitsLarge", hero->portrait, 0, 170, 66);
  1447. new CAdventureMapButton("", "", boost::bind(&CLevelWindow::close, this), 297, 413, "IOKAY", SDLK_RETURN);
  1448. //%s has gained a level.
  1449. new CLabel(192, 33, FONT_MEDIUM, CENTER, Colors::WHITE,
  1450. boost::str(boost::format(CGI->generaltexth->allTexts[444]) % hero->name));
  1451. //%s is now a level %d %s.
  1452. new CLabel(192, 162, FONT_MEDIUM, CENTER, Colors::WHITE,
  1453. boost::str(boost::format(CGI->generaltexth->allTexts[445]) % hero->name % hero->level % hero->type->heroClass->name));
  1454. new CAnimImage("PSKIL42", pskill, 0, 174, 190);
  1455. new CLabel(192, 253, FONT_MEDIUM, CENTER, Colors::WHITE,
  1456. CGI->generaltexth->primarySkillNames[pskill] + " +1");
  1457. if (!skills.empty())
  1458. {
  1459. std::vector<CSelectableComponent *> comps;
  1460. for(auto & skill : skills)
  1461. {
  1462. comps.push_back(new CSelectableComponent(
  1463. CComponent::secskill,
  1464. skill,
  1465. hero->getSecSkillLevel( SecondarySkill(skill) )+1,
  1466. CComponent::medium));
  1467. }
  1468. box = new CComponentBox(comps, Rect(75, 300, pos.w - 150, 100));
  1469. }
  1470. else
  1471. box = nullptr;
  1472. }
  1473. CLevelWindow::~CLevelWindow()
  1474. {
  1475. //FIXME: call callback if there was nothing to select?
  1476. if (box && box->selectedIndex() != -1)
  1477. cb(box->selectedIndex());
  1478. LOCPLINT->showingDialog->setn(false);
  1479. }
  1480. void CMinorResDataBar::show(SDL_Surface * to)
  1481. {
  1482. }
  1483. void CMinorResDataBar::showAll(SDL_Surface * to)
  1484. {
  1485. blitAt(bg,pos.x,pos.y,to);
  1486. for (Res::ERes i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  1487. {
  1488. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  1489. graphics->fonts[FONT_SMALL]->renderTextCenter(to, text, Colors::WHITE, Point(pos.x + 50 + 76 * i, pos.y + pos.h/2));
  1490. }
  1491. std::vector<std::string> temp;
  1492. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  1493. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  1494. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  1495. std::string datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  1496. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  1497. graphics->fonts[FONT_SMALL]->renderTextCenter(to, processStr(datetext,temp), Colors::WHITE, Point(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2));
  1498. }
  1499. CMinorResDataBar::CMinorResDataBar()
  1500. {
  1501. bg = BitmapHandler::loadBitmap("KRESBAR.bmp");
  1502. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1503. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1504. pos.x = 7;
  1505. pos.y = 575;
  1506. pos.w = bg->w;
  1507. pos.h = bg->h;
  1508. }
  1509. CMinorResDataBar::~CMinorResDataBar()
  1510. {
  1511. SDL_FreeSurface(bg);
  1512. }
  1513. CObjectListWindow::CItem::CItem(CObjectListWindow *_parent, size_t _id, std::string _text):
  1514. parent(_parent),
  1515. index(_id)
  1516. {
  1517. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1518. border = new CPicture("TPGATES");
  1519. pos = border->pos;
  1520. addUsedEvents(LCLICK);
  1521. type |= REDRAW_PARENT;
  1522. text = new CLabel(pos.w/2, pos.h/2, FONT_SMALL, CENTER, Colors::WHITE, _text);
  1523. select(index == parent->selected);
  1524. }
  1525. void CObjectListWindow::CItem::select(bool on)
  1526. {
  1527. if (on)
  1528. border->recActions = 255;
  1529. else
  1530. border->recActions = ~(UPDATE | SHOWALL);
  1531. redraw();
  1532. }
  1533. void CObjectListWindow::CItem::clickLeft(tribool down, bool previousState)
  1534. {
  1535. if( previousState && !down)
  1536. parent->changeSelection(index);
  1537. }
  1538. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1539. std::function<void(int)> Callback):
  1540. CWindowObject(PLAYER_COLORED, "TPGATE"),
  1541. onSelect(Callback)
  1542. {
  1543. items.reserve(_items.size());
  1544. for(int id : _items)
  1545. {
  1546. items.push_back(std::make_pair(id, CGI->mh->map->objects[id]->hoverName));
  1547. }
  1548. init(titlePic, _title, _descr);
  1549. }
  1550. CObjectListWindow::CObjectListWindow(const std::vector<std::string> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1551. std::function<void(int)> Callback):
  1552. CWindowObject(PLAYER_COLORED, "TPGATE"),
  1553. onSelect(Callback)
  1554. {
  1555. items.reserve(_items.size());
  1556. for (size_t i=0; i<_items.size(); i++)
  1557. items.push_back(std::make_pair(int(i), _items[i]));
  1558. init(titlePic, _title, _descr);
  1559. }
  1560. void CObjectListWindow::init(CPicture * titlePic, std::string _title, std::string _descr)
  1561. {
  1562. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1563. title = new CLabel(152, 27, FONT_BIG, CENTER, Colors::YELLOW, _title);
  1564. descr = new CLabel(145, 133, FONT_SMALL, CENTER, Colors::WHITE, _descr);
  1565. ok = new CAdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  1566. ok->block(true);
  1567. exit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  1568. if (titlePic)
  1569. {
  1570. titleImage = titlePic;
  1571. addChild(titleImage);
  1572. titleImage->recActions = defActions;
  1573. titleImage->pos.x = pos.w/2 + pos.x - titleImage->pos.w/2;
  1574. titleImage->pos.y =75 + pos.y - titleImage->pos.h/2;
  1575. }
  1576. list = new CListBox(boost::bind(&CObjectListWindow::genItem, this, _1), CListBox::DestroyFunc(),
  1577. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1578. list->type |= REDRAW_PARENT;
  1579. }
  1580. CIntObject * CObjectListWindow::genItem(size_t index)
  1581. {
  1582. if (index < items.size())
  1583. return new CItem(this, index, items[index].second);
  1584. return nullptr;
  1585. }
  1586. void CObjectListWindow::elementSelected()
  1587. {
  1588. std::function<void(int)> toCall = onSelect;//save
  1589. int where = items[selected].first; //required variables
  1590. GH.popIntTotally(this);//then destroy window
  1591. toCall(where);//and send selected object
  1592. }
  1593. void CObjectListWindow::changeSelection(size_t which)
  1594. {
  1595. ok->block(false);
  1596. if (selected == which)
  1597. return;
  1598. std::list< CIntObject * > elements = list->getItems();
  1599. for(CIntObject * element : elements)
  1600. {
  1601. CItem *item;
  1602. if ( (item = dynamic_cast<CItem*>(element)) )
  1603. {
  1604. if (item->index == selected)
  1605. item->select(false);
  1606. if (item->index == which)
  1607. item->select(true);
  1608. }
  1609. }
  1610. selected = which;
  1611. }
  1612. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  1613. {
  1614. if(key.state != SDL_PRESSED)
  1615. return;
  1616. int sel = selected;
  1617. switch(key.keysym.sym)
  1618. {
  1619. break; case SDLK_UP:
  1620. sel -=1;
  1621. break; case SDLK_DOWN:
  1622. sel +=1;
  1623. break; case SDLK_PAGEUP:
  1624. sel -=9;
  1625. break; case SDLK_PAGEDOWN:
  1626. sel +=9;
  1627. break; case SDLK_HOME:
  1628. sel = 0;
  1629. break; case SDLK_END:
  1630. sel = items.size();
  1631. break; default:
  1632. return;
  1633. }
  1634. vstd::abetween(sel, 0, items.size()-1);
  1635. list->scrollTo(sel);
  1636. changeSelection(sel);
  1637. }
  1638. CTradeWindow::CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial):
  1639. CIntObject(LCLICK | HOVER | RCLICK, pos),
  1640. type(EType(-1)),// set to invalid, will be corrected in setType
  1641. id(ID),
  1642. serial(Serial),
  1643. left(Left)
  1644. {
  1645. downSelection = false;
  1646. hlp = nullptr;
  1647. image = nullptr;
  1648. setType(Type);
  1649. }
  1650. void CTradeWindow::CTradeableItem::setType(EType newType)
  1651. {
  1652. if (type != newType)
  1653. {
  1654. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1655. type = newType;
  1656. delete image;
  1657. if (getIndex() < 0)
  1658. {
  1659. image = new CAnimImage(getFilename(), 0);
  1660. image->disable();
  1661. }
  1662. else
  1663. image = new CAnimImage(getFilename(), getIndex());
  1664. }
  1665. }
  1666. void CTradeWindow::CTradeableItem::setID(int newID)
  1667. {
  1668. if (id != newID)
  1669. {
  1670. id = newID;
  1671. if (image)
  1672. {
  1673. int index = getIndex();
  1674. if (index < 0)
  1675. image->disable();
  1676. else
  1677. {
  1678. image->enable();
  1679. image->setFrame(index);
  1680. }
  1681. }
  1682. }
  1683. }
  1684. std::string CTradeWindow::CTradeableItem::getFilename()
  1685. {
  1686. switch(type)
  1687. {
  1688. case RESOURCE:
  1689. return "RESOURCE";
  1690. case PLAYER:
  1691. return "CREST58";
  1692. case ARTIFACT_TYPE:
  1693. case ARTIFACT_PLACEHOLDER:
  1694. case ARTIFACT_INSTANCE:
  1695. return "artifact";
  1696. case CREATURE:
  1697. return "TWCRPORT";
  1698. default:
  1699. return "";
  1700. }
  1701. }
  1702. int CTradeWindow::CTradeableItem::getIndex()
  1703. {
  1704. if (id < 0)
  1705. return -1;
  1706. switch(type)
  1707. {
  1708. case RESOURCE:
  1709. case PLAYER:
  1710. return id;
  1711. case ARTIFACT_TYPE:
  1712. case ARTIFACT_INSTANCE:
  1713. case ARTIFACT_PLACEHOLDER:
  1714. return VLC->arth->artifacts[id]->iconIndex;
  1715. case CREATURE:
  1716. return VLC->creh->creatures[id]->iconIndex;
  1717. default:
  1718. return -1;
  1719. }
  1720. }
  1721. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  1722. {
  1723. Point posToBitmap;
  1724. Point posToSubCenter;
  1725. switch(type)
  1726. {
  1727. case RESOURCE:
  1728. posToBitmap = Point(19,9);
  1729. posToSubCenter = Point(36, 59);
  1730. break;
  1731. case CREATURE_PLACEHOLDER:
  1732. case CREATURE:
  1733. posToSubCenter = Point(29, 76);
  1734. if(downSelection)
  1735. posToSubCenter.y += 5;
  1736. break;
  1737. case PLAYER:
  1738. posToSubCenter = Point(31, 76);
  1739. break;
  1740. case ARTIFACT_PLACEHOLDER:
  1741. case ARTIFACT_INSTANCE:
  1742. posToSubCenter = Point(19, 55);
  1743. if(downSelection)
  1744. posToSubCenter.y += 8;
  1745. break;
  1746. case ARTIFACT_TYPE:
  1747. posToSubCenter = Point(19, 58);
  1748. break;
  1749. }
  1750. if (image)
  1751. {
  1752. image->moveTo(pos.topLeft() + posToBitmap);
  1753. CIntObject::showAll(to);
  1754. }
  1755. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, Colors::WHITE, to);
  1756. }
  1757. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  1758. {
  1759. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  1760. assert(mw);
  1761. if(down)
  1762. {
  1763. if(type == ARTIFACT_PLACEHOLDER)
  1764. {
  1765. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  1766. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  1767. {
  1768. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this, movedArt);
  1769. }
  1770. else if(const CArtifactInstance *art = getArtInstance())
  1771. {
  1772. aw->arts->commonInfo->src.AOH = aw->arts;
  1773. aw->arts->commonInfo->src.art = art;
  1774. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  1775. aw->arts->markPossibleSlots(art);
  1776. //aw->arts->commonInfo->dst.AOH = aw->arts;
  1777. CCS->curh->dragAndDropCursor(new CAnimImage("artifact", art->artType->iconIndex));
  1778. aw->arts->artifactsOnAltar.erase(art);
  1779. setID(-1);
  1780. subtitle = "";
  1781. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  1782. }
  1783. aw->calcTotalExp();
  1784. return;
  1785. }
  1786. if(left)
  1787. {
  1788. if(mw->hLeft != this)
  1789. mw->hLeft = this;
  1790. else
  1791. return;
  1792. }
  1793. else
  1794. {
  1795. if(mw->hRight != this)
  1796. mw->hRight = this;
  1797. else
  1798. return;
  1799. }
  1800. mw->selectionChanged(left);
  1801. }
  1802. }
  1803. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  1804. {
  1805. Rect oldPos = pos;
  1806. std::string oldSub = subtitle;
  1807. downSelection = true;
  1808. moveTo(dstPos);
  1809. subtitle = customSub;
  1810. showAll(to);
  1811. downSelection = false;
  1812. moveTo(oldPos.topLeft());
  1813. subtitle = oldSub;
  1814. }
  1815. void CTradeWindow::CTradeableItem::hover(bool on)
  1816. {
  1817. if(!on)
  1818. {
  1819. GH.statusbar->clear();
  1820. return;
  1821. }
  1822. switch(type)
  1823. {
  1824. case CREATURE:
  1825. case CREATURE_PLACEHOLDER:
  1826. GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  1827. break;
  1828. case ARTIFACT_PLACEHOLDER:
  1829. if(id < 0)
  1830. GH.statusbar->print(CGI->generaltexth->zelp[582].first);
  1831. else
  1832. GH.statusbar->print(CGI->arth->artifacts[id]->Name());
  1833. break;
  1834. }
  1835. }
  1836. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  1837. {
  1838. if(down)
  1839. {
  1840. switch(type)
  1841. {
  1842. case CREATURE:
  1843. case CREATURE_PLACEHOLDER:
  1844. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  1845. break;
  1846. case ARTIFACT_TYPE:
  1847. case ARTIFACT_PLACEHOLDER:
  1848. if(id >= 0)
  1849. adventureInt->handleRightClick(CGI->arth->artifacts[id]->Description(), down);
  1850. break;
  1851. }
  1852. }
  1853. }
  1854. std::string CTradeWindow::CTradeableItem::getName(int number /*= -1*/) const
  1855. {
  1856. switch(type)
  1857. {
  1858. case PLAYER:
  1859. return CGI->generaltexth->capColors[id];
  1860. case RESOURCE:
  1861. return CGI->generaltexth->restypes[id];
  1862. case CREATURE:
  1863. if(number == 1)
  1864. return CGI->creh->creatures[id]->nameSing;
  1865. else
  1866. return CGI->creh->creatures[id]->namePl;
  1867. case ARTIFACT_TYPE:
  1868. case ARTIFACT_INSTANCE:
  1869. return CGI->arth->artifacts[id]->Name();
  1870. }
  1871. assert(0);
  1872. return "";
  1873. }
  1874. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  1875. {
  1876. switch(type)
  1877. {
  1878. case ARTIFACT_PLACEHOLDER:
  1879. case ARTIFACT_INSTANCE:
  1880. return (const CArtifactInstance *)hlp;
  1881. default:
  1882. return nullptr;
  1883. }
  1884. }
  1885. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  1886. {
  1887. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  1888. hlp = art;
  1889. if(art)
  1890. setID(art->artType->id);
  1891. else
  1892. setID(-1);
  1893. }
  1894. CTradeWindow::CTradeWindow(std::string bgName, const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode):
  1895. CWindowObject(PLAYER_COLORED, bgName),
  1896. market(Market),
  1897. hero(Hero),
  1898. arts(nullptr),
  1899. hLeft(nullptr),
  1900. hRight(nullptr),
  1901. readyToTrade(false)
  1902. {
  1903. type |= BLOCK_ADV_HOTKEYS;
  1904. mode = Mode;
  1905. initTypes();
  1906. }
  1907. void CTradeWindow::initTypes()
  1908. {
  1909. switch(mode)
  1910. {
  1911. case EMarketMode::RESOURCE_RESOURCE:
  1912. itemsType[1] = RESOURCE;
  1913. itemsType[0] = RESOURCE;
  1914. break;
  1915. case EMarketMode::RESOURCE_PLAYER:
  1916. itemsType[1] = RESOURCE;
  1917. itemsType[0] = PLAYER;
  1918. break;
  1919. case EMarketMode::CREATURE_RESOURCE:
  1920. itemsType[1] = CREATURE;
  1921. itemsType[0] = RESOURCE;
  1922. break;
  1923. case EMarketMode::RESOURCE_ARTIFACT:
  1924. itemsType[1] = RESOURCE;
  1925. itemsType[0] = ARTIFACT_TYPE;
  1926. break;
  1927. case EMarketMode::ARTIFACT_RESOURCE:
  1928. itemsType[1] = ARTIFACT_INSTANCE;
  1929. itemsType[0] = RESOURCE;
  1930. break;
  1931. case EMarketMode::CREATURE_EXP:
  1932. itemsType[1] = CREATURE;
  1933. itemsType[0] = CREATURE_PLACEHOLDER;
  1934. break;
  1935. case EMarketMode::ARTIFACT_EXP:
  1936. itemsType[1] = ARTIFACT_TYPE;
  1937. itemsType[0] = ARTIFACT_PLACEHOLDER;
  1938. break;
  1939. }
  1940. }
  1941. void CTradeWindow::initItems(bool Left)
  1942. {
  1943. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  1944. {
  1945. int xOffset = 0, yOffset = 0;
  1946. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1947. {
  1948. xOffset = -361;
  1949. yOffset = +46;
  1950. auto hlp = new CTradeableItem(Point(137, 469), itemsType[Left], -1, 1, 0);
  1951. hlp->recActions &= ~(UPDATE | SHOWALL);
  1952. items[Left].push_back(hlp);
  1953. }
  1954. else //ARTIFACT_EXP
  1955. {
  1956. xOffset = -363;
  1957. yOffset = -12;
  1958. }
  1959. BLOCK_CAPTURING;
  1960. arts = new CArtifactsOfHero(Point(pos.x+xOffset, pos.y+yOffset));
  1961. arts->commonInfo = new CArtifactsOfHero::SCommonPart;
  1962. arts->commonInfo->participants.insert(arts);
  1963. arts->recActions = 255;
  1964. arts->setHero(hero);
  1965. arts->allowedAssembling = false;
  1966. addChild(arts);
  1967. artSets.push_back(arts);
  1968. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1969. arts->highlightModeCallback = boost::bind(&CTradeWindow::artifactSelected, this, _1);
  1970. return;
  1971. }
  1972. std::vector<int> *ids = getItemsIds(Left);
  1973. std::vector<Rect> pos;
  1974. int amount = -1;
  1975. getPositionsFor(pos, Left, itemsType[Left]);
  1976. if(Left || !ids)
  1977. amount = 7;
  1978. else
  1979. amount = ids->size();
  1980. if(ids)
  1981. vstd::amin(amount, ids->size());
  1982. for(int j=0; j<amount; j++)
  1983. {
  1984. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  1985. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  1986. continue;
  1987. auto hlp = new CTradeableItem(pos[j].topLeft(), itemsType[Left], id, Left, j);
  1988. hlp->pos = pos[j] + this->pos.topLeft();
  1989. items[Left].push_back(hlp);
  1990. }
  1991. initSubs(Left);
  1992. }
  1993. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  1994. {
  1995. std::vector<int> *ids = nullptr;
  1996. if(mode == EMarketMode::ARTIFACT_EXP)
  1997. return new std::vector<int>(22, -1);
  1998. if(Left)
  1999. {
  2000. switch(itemsType[1])
  2001. {
  2002. case CREATURE:
  2003. ids = new std::vector<int>;
  2004. for(int i = 0; i < 7; i++)
  2005. {
  2006. if(const CCreature *c = hero->getCreature(SlotID(i)))
  2007. ids->push_back(c->idNumber);
  2008. else
  2009. ids->push_back(-1);
  2010. }
  2011. break;
  2012. }
  2013. }
  2014. else
  2015. {
  2016. switch(itemsType[0])
  2017. {
  2018. case PLAYER:
  2019. ids = new std::vector<int>;
  2020. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  2021. if(PlayerColor(i) != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(PlayerColor(i)) == EPlayerStatus::INGAME)
  2022. ids->push_back(i);
  2023. break;
  2024. case ARTIFACT_TYPE:
  2025. ids = new std::vector<int>(market->availableItemsIds(mode));
  2026. break;
  2027. }
  2028. }
  2029. return ids;
  2030. }
  2031. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  2032. {
  2033. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  2034. {
  2035. //22 boxes, 5 in row, last row: two boxes centered
  2036. int h, w, x, y, dx, dy;
  2037. h = w = 44;
  2038. x = 317;
  2039. y = 53;
  2040. dx = 54;
  2041. dy = 70;
  2042. for (int i = 0; i < 4 ; i++)
  2043. for (int j = 0; j < 5 ; j++)
  2044. poss += Rect(x + dx*j, y + dy*i, w, h);
  2045. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  2046. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  2047. }
  2048. else
  2049. {
  2050. //seven boxes:
  2051. // X X X
  2052. // X X X
  2053. // X
  2054. int h, w, x, y, dx, dy;
  2055. int leftToRightOffset;
  2056. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  2057. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  2058. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  2059. genRect(h, w, x + dx, y + 2*dy);
  2060. if(!Left)
  2061. {
  2062. for(Rect &r : poss)
  2063. r.x += leftToRightOffset;
  2064. }
  2065. }
  2066. }
  2067. void CTradeWindow::initSubs(bool Left)
  2068. {
  2069. for(CTradeableItem *t : items[Left])
  2070. {
  2071. if(Left)
  2072. {
  2073. switch(itemsType[1])
  2074. {
  2075. case CREATURE:
  2076. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(SlotID(t->serial)));
  2077. break;
  2078. case RESOURCE:
  2079. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(t->serial)));
  2080. break;
  2081. }
  2082. }
  2083. else //right side
  2084. {
  2085. if(itemsType[0] == PLAYER)
  2086. {
  2087. t->subtitle = CGI->generaltexth->capColors[t->id];
  2088. }
  2089. else if(hLeft)//artifact, creature
  2090. {
  2091. int h1, h2; //hlp variables for getting offer
  2092. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  2093. if(t->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  2094. {
  2095. std::ostringstream oss;
  2096. oss << h2;
  2097. if(h1!=1)
  2098. oss << "/" << h1;
  2099. t->subtitle = oss.str();
  2100. }
  2101. else
  2102. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  2103. }
  2104. else
  2105. t->subtitle = "";
  2106. }
  2107. }
  2108. }
  2109. void CTradeWindow::showAll(SDL_Surface * to)
  2110. {
  2111. CWindowObject::showAll(to);
  2112. if(hRight)
  2113. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2114. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  2115. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2116. if(readyToTrade)
  2117. {
  2118. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  2119. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  2120. }
  2121. }
  2122. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  2123. {
  2124. for(CTradeableItem *t : toRemove)
  2125. removeItem(t);
  2126. }
  2127. void CTradeWindow::removeItem(CTradeableItem * t)
  2128. {
  2129. items[t->left] -= t;
  2130. delete t;
  2131. if(hRight == t)
  2132. {
  2133. hRight = nullptr;
  2134. selectionChanged(false);
  2135. }
  2136. }
  2137. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  2138. {
  2139. for(CTradeableItem *t : items[1])
  2140. if(!hero->getStackCount(SlotID(t->serial)))
  2141. toRemove.insert(t);
  2142. }
  2143. void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
  2144. {
  2145. const IMarket *m = market;
  2146. const CGHeroInstance *h = hero;
  2147. CTradeWindow *nwindow = nullptr;
  2148. GH.popIntTotally(this);
  2149. switch(Mode)
  2150. {
  2151. case EMarketMode::CREATURE_EXP:
  2152. case EMarketMode::ARTIFACT_EXP:
  2153. nwindow = new CAltarWindow(m, h, Mode);
  2154. break;
  2155. default:
  2156. nwindow = new CMarketplaceWindow(m, h, Mode);
  2157. break;
  2158. }
  2159. GH.pushInt(nwindow);
  2160. }
  2161. void CTradeWindow::artifactSelected(CArtPlace *slot)
  2162. {
  2163. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  2164. items[1][0]->setArtInstance(slot->ourArt);
  2165. if(slot->ourArt)
  2166. hLeft = items[1][0];
  2167. else
  2168. hLeft = nullptr;
  2169. selectionChanged(true);
  2170. }
  2171. std::string CMarketplaceWindow::getBackgroundForMode(EMarketMode::EMarketMode mode)
  2172. {
  2173. switch(mode)
  2174. {
  2175. case EMarketMode::RESOURCE_RESOURCE:
  2176. return "TPMRKRES.bmp";
  2177. case EMarketMode::RESOURCE_PLAYER:
  2178. return "TPMRKPTS.bmp";
  2179. case EMarketMode::CREATURE_RESOURCE:
  2180. return "TPMRKCRS.bmp";
  2181. case EMarketMode::RESOURCE_ARTIFACT:
  2182. return "TPMRKABS.bmp";
  2183. case EMarketMode::ARTIFACT_RESOURCE:
  2184. return "TPMRKASS.bmp";
  2185. }
  2186. assert(0);
  2187. return "";
  2188. }
  2189. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  2190. : CTradeWindow(getBackgroundForMode(Mode), Market, Hero, Mode)
  2191. {
  2192. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2193. madeTransaction = false;
  2194. bool sliderNeeded = true;
  2195. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  2196. std::string title;
  2197. if (market->o->ID == Obj::TOWN)
  2198. {
  2199. switch (mode)
  2200. {
  2201. break; case EMarketMode::CREATURE_RESOURCE:
  2202. title = CGI->townh->factions[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->Name();
  2203. break; case EMarketMode::RESOURCE_ARTIFACT:
  2204. title = CGI->townh->factions[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->Name();
  2205. sliderNeeded = false;
  2206. break; case EMarketMode::ARTIFACT_RESOURCE:
  2207. title = CGI->townh->factions[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->Name();
  2208. sliderNeeded = false;
  2209. break; default:
  2210. title = CGI->generaltexth->allTexts[158];
  2211. }
  2212. }
  2213. else
  2214. {
  2215. switch (market->o->ID)
  2216. {
  2217. break; case Obj::BLACK_MARKET: title = CGI->generaltexth->allTexts[349];
  2218. break; case Obj::TRADING_POST: title = CGI->generaltexth->allTexts[159];
  2219. break; case Obj::TRADING_POST_SNOW: title = CGI->generaltexth->allTexts[159];
  2220. break; default: title = market->o->getHoverText();
  2221. }
  2222. }
  2223. new CLabel(300, 27, FONT_BIG, CENTER, Colors::YELLOW, title);
  2224. initItems(false);
  2225. initItems(true);
  2226. ok = new CAdventureMapButton(CGI->generaltexth->zelp[600],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2227. ok->assignedKeys.insert(SDLK_ESCAPE);
  2228. deal = new CAdventureMapButton(CGI->generaltexth->zelp[595],boost::bind(&CMarketplaceWindow::makeDeal,this),307,520,"TPMRKB.DEF");
  2229. deal->block(true);
  2230. if(sliderNeeded)
  2231. {
  2232. slider = new CSlider(231,490,137,nullptr,0,0);
  2233. slider->moved = boost::bind(&CMarketplaceWindow::sliderMoved,this,_1);
  2234. max = new CAdventureMapButton(CGI->generaltexth->zelp[596],boost::bind(&CMarketplaceWindow::setMax,this),229,520,"IRCBTNS.DEF");
  2235. max->block(true);
  2236. }
  2237. else
  2238. {
  2239. slider = nullptr;
  2240. max = nullptr;
  2241. deal->moveBy(Point(-30, 0));
  2242. }
  2243. Rect traderTextRect;
  2244. //left side
  2245. switch(Mode)
  2246. {
  2247. case EMarketMode::RESOURCE_RESOURCE:
  2248. case EMarketMode::RESOURCE_PLAYER:
  2249. case EMarketMode::RESOURCE_ARTIFACT:
  2250. new CLabel(154, 148, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]);
  2251. break;
  2252. case EMarketMode::CREATURE_RESOURCE:
  2253. //%s's Creatures
  2254. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::WHITE,
  2255. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2256. break;
  2257. case EMarketMode::ARTIFACT_RESOURCE:
  2258. //%s's Artifacts
  2259. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::WHITE,
  2260. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2261. break;
  2262. }
  2263. //right side
  2264. switch(Mode)
  2265. {
  2266. case EMarketMode::RESOURCE_RESOURCE:
  2267. case EMarketMode::CREATURE_RESOURCE:
  2268. case EMarketMode::RESOURCE_ARTIFACT:
  2269. case EMarketMode::ARTIFACT_RESOURCE:
  2270. new CLabel(445, 148, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]);
  2271. traderTextRect = Rect(316, 48, 260, 75);
  2272. break;
  2273. case EMarketMode::RESOURCE_PLAYER:
  2274. new CLabel(445, 55, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]);
  2275. traderTextRect = Rect(28, 48, 260, 75);
  2276. break;
  2277. }
  2278. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  2279. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  2280. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  2281. new CAdventureMapButton(CGI->generaltexth->zelp[612],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_PLAYER), 18, 520,"TPMRKBU1.DEF");
  2282. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  2283. new CAdventureMapButton(CGI->generaltexth->zelp[605],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_RESOURCE), 516, 450 + specialOffset,"TPMRKBU5.DEF");
  2284. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  2285. new CAdventureMapButton(CGI->generaltexth->zelp[599],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::CREATURE_RESOURCE), 516, 485,"TPMRKBU4.DEF"); //was y=450, changed to not overlap res-res in some conditions
  2286. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  2287. new CAdventureMapButton(CGI->generaltexth->zelp[598],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_ARTIFACT), 18, 450 + specialOffset,"TPMRKBU2.DEF");
  2288. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  2289. new CAdventureMapButton(CGI->generaltexth->zelp[613],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::ARTIFACT_RESOURCE), 18, 485,"TPMRKBU3.DEF"); //was y=450, changed to not overlap res-art in some conditions
  2290. updateTraderText();
  2291. }
  2292. CMarketplaceWindow::~CMarketplaceWindow()
  2293. {
  2294. hLeft = hRight = nullptr;
  2295. for(auto & elem : items[1])
  2296. delete elem;
  2297. for(auto & elem : items[0])
  2298. delete elem;
  2299. items[1].clear();
  2300. items[0].clear();
  2301. }
  2302. void CMarketplaceWindow::setMax()
  2303. {
  2304. slider->moveToMax();
  2305. }
  2306. void CMarketplaceWindow::makeDeal()
  2307. {
  2308. int sliderValue = 0;
  2309. if(slider)
  2310. sliderValue = slider->value;
  2311. else
  2312. sliderValue = !deal->isBlocked(); //should always be 1
  2313. if(!sliderValue)
  2314. return;
  2315. int leftIdToSend = -1;
  2316. switch (mode)
  2317. {
  2318. case EMarketMode::CREATURE_RESOURCE:
  2319. leftIdToSend = hLeft->serial;
  2320. break;
  2321. case EMarketMode::ARTIFACT_RESOURCE:
  2322. leftIdToSend = hLeft->getArtInstance()->id.getNum();
  2323. break;
  2324. default:
  2325. leftIdToSend = hLeft->id;
  2326. break;
  2327. }
  2328. if(slider)
  2329. {
  2330. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->value*r1, hero);
  2331. slider->moveTo(0);
  2332. }
  2333. else
  2334. {
  2335. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  2336. }
  2337. madeTransaction = true;
  2338. hLeft = nullptr;
  2339. hRight = nullptr;
  2340. selectionChanged(true);
  2341. }
  2342. void CMarketplaceWindow::sliderMoved( int to )
  2343. {
  2344. redraw();
  2345. }
  2346. void CMarketplaceWindow::selectionChanged(bool side)
  2347. {
  2348. readyToTrade = hLeft && hRight;
  2349. if(mode == EMarketMode::RESOURCE_RESOURCE)
  2350. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  2351. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  2352. arts->unmarkSlots(false);
  2353. if(readyToTrade)
  2354. {
  2355. int soldItemId = hLeft->id;
  2356. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  2357. if(slider)
  2358. {
  2359. int newAmount = -1;
  2360. if(itemsType[1] == RESOURCE)
  2361. newAmount = LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId));
  2362. else if(itemsType[1] == CREATURE)
  2363. newAmount = hero->getStackCount(SlotID(hLeft->serial)) - (hero->Slots().size() == 1 && hero->needsLastStack());
  2364. else
  2365. assert(0);
  2366. slider->setAmount(newAmount / r1);
  2367. slider->moveTo(0);
  2368. max->block(false);
  2369. deal->block(false);
  2370. }
  2371. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  2372. {
  2373. deal->block(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId)) < r1);
  2374. }
  2375. else
  2376. deal->block(false);
  2377. }
  2378. else
  2379. {
  2380. if(slider)
  2381. {
  2382. max->block(true);
  2383. slider->setAmount(0);
  2384. slider->moveTo(0);
  2385. }
  2386. deal->block(true);
  2387. }
  2388. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  2389. initSubs(false);
  2390. updateTraderText();
  2391. redraw();
  2392. }
  2393. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  2394. {
  2395. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  2396. }
  2397. void CMarketplaceWindow::garrisonChanged()
  2398. {
  2399. if(mode != EMarketMode::CREATURE_RESOURCE)
  2400. return;
  2401. std::set<CTradeableItem *> toRemove;
  2402. getEmptySlots(toRemove);
  2403. removeItems(toRemove);
  2404. initSubs(true);
  2405. }
  2406. void CMarketplaceWindow::artifactsChanged(bool Left)
  2407. {
  2408. assert(!Left);
  2409. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  2410. return;
  2411. std::vector<int> available = market->availableItemsIds(mode);
  2412. std::set<CTradeableItem *> toRemove;
  2413. for(CTradeableItem *t : items[0])
  2414. if(!vstd::contains(available, t->id))
  2415. toRemove.insert(t);
  2416. removeItems(toRemove);
  2417. redraw();
  2418. }
  2419. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  2420. {
  2421. if(Left)
  2422. {
  2423. switch(itemsType[1])
  2424. {
  2425. case RESOURCE:
  2426. case CREATURE:
  2427. {
  2428. int val = slider
  2429. ? slider->value * r1
  2430. : (((deal->isBlocked())) ? 0 : r1);
  2431. return boost::lexical_cast<std::string>(val);
  2432. }
  2433. case ARTIFACT_INSTANCE:
  2434. return ((deal->isBlocked()) ? "0" : "1");
  2435. }
  2436. }
  2437. else
  2438. {
  2439. switch(itemsType[0])
  2440. {
  2441. case RESOURCE:
  2442. if(slider)
  2443. return boost::lexical_cast<std::string>( slider->value * r2 );
  2444. else
  2445. return boost::lexical_cast<std::string>(r2);
  2446. case ARTIFACT_TYPE:
  2447. return ((deal->isBlocked()) ? "0" : "1");
  2448. case PLAYER:
  2449. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  2450. }
  2451. }
  2452. return "???";
  2453. }
  2454. Point CMarketplaceWindow::selectionOffset(bool Left) const
  2455. {
  2456. if(Left)
  2457. {
  2458. switch(itemsType[1])
  2459. {
  2460. case RESOURCE:
  2461. return Point(122, 446);
  2462. case CREATURE:
  2463. return Point(128, 450);
  2464. case ARTIFACT_INSTANCE:
  2465. return Point(134, 466);
  2466. }
  2467. }
  2468. else
  2469. {
  2470. switch(itemsType[0])
  2471. {
  2472. case RESOURCE:
  2473. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2474. return Point(410, 469);
  2475. else
  2476. return Point(410, 446);
  2477. case ARTIFACT_TYPE:
  2478. return Point(425, 447);
  2479. case PLAYER:
  2480. return Point(417, 451);
  2481. }
  2482. }
  2483. assert(0);
  2484. return Point(0,0);
  2485. }
  2486. void CMarketplaceWindow::resourceChanged(int type, int val)
  2487. {
  2488. initSubs(true);
  2489. }
  2490. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2491. {
  2492. switch(type)
  2493. {
  2494. case RESOURCE:
  2495. dx = 82;
  2496. dy = 79;
  2497. x = 39;
  2498. y = 180;
  2499. h = 66;
  2500. w = 74;
  2501. break;
  2502. case PLAYER:
  2503. dx = 83;
  2504. dy = 118;
  2505. h = 64;
  2506. w = 58;
  2507. x = 44;
  2508. y = 83;
  2509. assert(Right);
  2510. break;
  2511. case CREATURE://45,123
  2512. x = 45;
  2513. y = 123;
  2514. w = 58;
  2515. h = 64;
  2516. dx = 83;
  2517. dy = 98;
  2518. assert(!Right);
  2519. break;
  2520. case ARTIFACT_TYPE://45,123
  2521. x = 340-289;
  2522. y = 180;
  2523. w = 44;
  2524. h = 44;
  2525. dx = 83;
  2526. dy = 79;
  2527. break;
  2528. }
  2529. leftToRightOffset = 289;
  2530. }
  2531. void CMarketplaceWindow::updateTraderText()
  2532. {
  2533. if(readyToTrade)
  2534. {
  2535. if(mode == EMarketMode::RESOURCE_PLAYER)
  2536. {
  2537. //I can give %s to the %s player.
  2538. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  2539. }
  2540. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  2541. {
  2542. //I can offer you the %s for %d %s of %s.
  2543. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  2544. }
  2545. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  2546. {
  2547. //I can offer you %d %s of %s for %d %s of %s.
  2548. 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()));
  2549. }
  2550. else if(mode == EMarketMode::CREATURE_RESOURCE)
  2551. {
  2552. //I can offer you %d %s of %s for %d %s.
  2553. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  2554. }
  2555. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2556. {
  2557. //I can offer you %d %s of %s for your %s.
  2558. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  2559. }
  2560. return;
  2561. }
  2562. int gnrtxtnr = -1;
  2563. if(madeTransaction)
  2564. {
  2565. if(mode == EMarketMode::RESOURCE_PLAYER)
  2566. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  2567. else
  2568. 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?
  2569. }
  2570. else
  2571. {
  2572. if(mode == EMarketMode::RESOURCE_PLAYER)
  2573. 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.
  2574. else
  2575. 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.
  2576. }
  2577. traderText->setTxt(CGI->generaltexth->allTexts[gnrtxtnr]);
  2578. }
  2579. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= nullptr*/, EMarketMode::EMarketMode Mode)
  2580. :CTradeWindow((Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, Mode)
  2581. {
  2582. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2583. if(Mode == EMarketMode::CREATURE_EXP)
  2584. {
  2585. //%s's Creatures
  2586. new CLabel(155, 30, FONT_SMALL, CENTER, Colors::YELLOW,
  2587. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2588. //Altar of Sacrifice
  2589. new CLabel(450, 30, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]);
  2590. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  2591. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  2592. slider = new CSlider(231,481,137,nullptr,0,0);
  2593. slider->moved = boost::bind(&CAltarWindow::sliderMoved,this,_1);
  2594. max = new CAdventureMapButton(CGI->generaltexth->zelp[578],boost::bind(&CSlider::moveToMax, slider),147,520,"IRCBTNS.DEF");
  2595. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  2596. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[579],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTARMY.DEF");
  2597. sacrificeBackpack = nullptr;
  2598. initItems(true);
  2599. mimicCres();
  2600. artIcon = nullptr;
  2601. }
  2602. else
  2603. {
  2604. //Sacrifice artifacts for experience
  2605. new CLabel(450, 34, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]);
  2606. //%s's Creatures
  2607. new CLabel(302, 423, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]);
  2608. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[571], boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTFILL.DEF");
  2609. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  2610. sacrificeBackpack = new CAdventureMapButton(CGI->generaltexth->zelp[570],boost::bind(&CAltarWindow::SacrificeBackpack,this),147,520,"ALTEMBK.DEF");
  2611. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  2612. slider = nullptr;
  2613. max = nullptr;
  2614. initItems(true);
  2615. initItems(false);
  2616. artIcon = new CAnimImage("ARTIFACT", 0, 0, 281, 442);
  2617. artIcon->disable();
  2618. }
  2619. //Experience needed to reach next level
  2620. new CTextBox(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  2621. //Total experience on the Altar
  2622. new CTextBox(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  2623. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  2624. ok = new CAdventureMapButton(CGI->generaltexth->zelp[568],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2625. ok->assignedKeys.insert(SDLK_ESCAPE);
  2626. deal = new CAdventureMapButton(CGI->generaltexth->zelp[585],boost::bind(&CAltarWindow::makeDeal,this),269,520,"ALTSACR.DEF");
  2627. if(Hero->getAlignment() != ::EAlignment::EVIL && Mode == EMarketMode::CREATURE_EXP)
  2628. new CAdventureMapButton(CGI->generaltexth->zelp[580], boost::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP), 516, 421, "ALTART.DEF");
  2629. if(Hero->getAlignment() != ::EAlignment::GOOD && Mode == EMarketMode::ARTIFACT_EXP)
  2630. new CAdventureMapButton(CGI->generaltexth->zelp[572], boost::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP), 516, 421, "ALTSACC.DEF");
  2631. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  2632. getExpValues();
  2633. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  2634. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  2635. setExpToLevel();
  2636. calcTotalExp();
  2637. blockTrade();
  2638. }
  2639. CAltarWindow::~CAltarWindow()
  2640. {
  2641. }
  2642. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2643. {
  2644. leftToRightOffset = 289;
  2645. x = 45;
  2646. y = 110;
  2647. w = 58;
  2648. h = 64;
  2649. dx = 83;
  2650. dy = 98;
  2651. }
  2652. void CAltarWindow::sliderMoved(int to)
  2653. {
  2654. sacrificedUnits[hLeft->serial] = to;
  2655. updateRight(hRight);
  2656. deal->block(!to);
  2657. calcTotalExp();
  2658. redraw();
  2659. }
  2660. void CAltarWindow::makeDeal()
  2661. {
  2662. if(mode == EMarketMode::CREATURE_EXP)
  2663. {
  2664. blockTrade();
  2665. slider->value = 0;
  2666. std::vector<int> toSacrifice = sacrificedUnits;
  2667. for (int i = 0; i < toSacrifice.size(); i++)
  2668. {
  2669. if(toSacrifice[i])
  2670. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  2671. }
  2672. for(int& val : sacrificedUnits)
  2673. val = 0;
  2674. for(CTradeableItem *t : items[0])
  2675. {
  2676. t->setType(CREATURE_PLACEHOLDER);
  2677. t->subtitle = "";
  2678. }
  2679. }
  2680. else
  2681. {
  2682. for(const CArtifactInstance *art : arts->artifactsOnAltar) //sacrifice each artifact on the list
  2683. {
  2684. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  2685. }
  2686. arts->artifactsOnAltar.clear();
  2687. for(CTradeableItem *t : items[0])
  2688. {
  2689. t->setID(-1);
  2690. t->subtitle = "";
  2691. }
  2692. arts->commonInfo->reset();
  2693. //arts->scrollBackpack(0);
  2694. deal->block(true);
  2695. }
  2696. calcTotalExp();
  2697. }
  2698. void CAltarWindow::SacrificeAll()
  2699. {
  2700. if(mode == EMarketMode::CREATURE_EXP)
  2701. {
  2702. bool movedAnything = false;
  2703. for(CTradeableItem *t : items[1])
  2704. sacrificedUnits[t->serial] = hero->getStackCount(SlotID(t->serial));
  2705. sacrificedUnits[items[1].front()->serial]--;
  2706. for(CTradeableItem *t : items[0])
  2707. {
  2708. updateRight(t);
  2709. if(t->type == CREATURE)
  2710. movedAnything = true;
  2711. }
  2712. deal->block(!movedAnything);
  2713. calcTotalExp();
  2714. }
  2715. else
  2716. {
  2717. for(auto i = hero->artifactsWorn.cbegin(); i != hero->artifactsWorn.cend(); i++)
  2718. {
  2719. if(i->second.artifact->artType->id != ArtifactID::ART_LOCK) //ignore locks from assembled artifacts
  2720. moveFromSlotToAltar(i->first, nullptr, i->second.artifact);
  2721. }
  2722. SacrificeBackpack();
  2723. }
  2724. redraw();
  2725. }
  2726. void CAltarWindow::selectionChanged(bool side)
  2727. {
  2728. if(mode != EMarketMode::CREATURE_EXP)
  2729. return;
  2730. CTradeableItem *&selected = side ? hLeft : hRight;
  2731. CTradeableItem *&theOther = side ? hRight : hLeft;
  2732. theOther = *std::find_if(items[!side].begin(), items[!side].end(), [&](const CTradeableItem * item)
  2733. {
  2734. return item->serial == selected->serial;
  2735. });
  2736. int stackCount = 0;
  2737. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  2738. if(hero->getStackCount(SlotID(i)) > sacrificedUnits[i])
  2739. stackCount++;
  2740. slider->setAmount(hero->getStackCount(SlotID(hLeft->serial)) - (stackCount == 1));
  2741. slider->block(!slider->amount);
  2742. slider->value = sacrificedUnits[hLeft->serial];
  2743. max->block(!slider->amount);
  2744. readyToTrade = true;
  2745. redraw();
  2746. }
  2747. void CAltarWindow::mimicCres()
  2748. {
  2749. std::vector<Rect> positions;
  2750. getPositionsFor(positions, false, CREATURE);
  2751. for(CTradeableItem *t : items[1])
  2752. {
  2753. auto hlp = new CTradeableItem(positions[t->serial].topLeft(), CREATURE_PLACEHOLDER, t->id, false, t->serial);
  2754. hlp->pos = positions[t->serial] + this->pos.topLeft();
  2755. items[0].push_back(hlp);
  2756. }
  2757. }
  2758. Point CAltarWindow::selectionOffset(bool Left) const
  2759. {
  2760. if(Left)
  2761. return Point(150, 421);
  2762. else
  2763. return Point(396, 421);
  2764. }
  2765. std::string CAltarWindow::selectionSubtitle(bool Left) const
  2766. {
  2767. if(Left && slider && hLeft)
  2768. return boost::lexical_cast<std::string>(slider->value);
  2769. else if(!Left && hRight)
  2770. return hRight->subtitle;
  2771. else
  2772. return "";
  2773. }
  2774. void CAltarWindow::artifactsChanged(bool left)
  2775. {
  2776. }
  2777. void CAltarWindow::garrisonChanged()
  2778. {
  2779. if(mode != EMarketMode::CREATURE_EXP)
  2780. return;
  2781. std::set<CTradeableItem *> empty;
  2782. getEmptySlots(empty);
  2783. for(CTradeableItem *t : empty)
  2784. {
  2785. removeItem(*std::find_if(items[0].begin(), items[0].end(), [&](const CTradeableItem * item)
  2786. {
  2787. return item->serial == t->serial;
  2788. }));
  2789. }
  2790. initSubs(true);
  2791. getExpValues();
  2792. }
  2793. void CAltarWindow::getExpValues()
  2794. {
  2795. int dump;
  2796. for(CTradeableItem *t : items[1])
  2797. if(t->id >= 0)
  2798. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], EMarketMode::CREATURE_EXP);
  2799. }
  2800. void CAltarWindow::calcTotalExp()
  2801. {
  2802. int val = 0;
  2803. if(mode == EMarketMode::CREATURE_EXP)
  2804. {
  2805. for (int i = 0; i < sacrificedUnits.size(); i++)
  2806. {
  2807. val += expPerUnit[i] * sacrificedUnits[i];
  2808. }
  2809. }
  2810. else
  2811. {
  2812. for(const CArtifactInstance *art : arts->artifactsOnAltar)
  2813. {
  2814. int dmp, valOfArt;
  2815. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  2816. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  2817. }
  2818. }
  2819. val = hero->calculateXp(val);
  2820. expOnAltar->setTxt(boost::lexical_cast<std::string>(val));
  2821. }
  2822. void CAltarWindow::setExpToLevel()
  2823. {
  2824. expToLevel->setTxt(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  2825. }
  2826. void CAltarWindow::blockTrade()
  2827. {
  2828. hLeft = hRight = nullptr;
  2829. readyToTrade = false;
  2830. if(slider)
  2831. {
  2832. slider->block(true);
  2833. max->block(true);
  2834. }
  2835. deal->block(true);
  2836. }
  2837. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  2838. {
  2839. int val = sacrificedUnits[toUpdate->serial];
  2840. toUpdate->setType(val ? CREATURE : CREATURE_PLACEHOLDER);
  2841. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  2842. }
  2843. int CAltarWindow::firstFreeSlot()
  2844. {
  2845. int ret = -1;
  2846. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  2847. return ret < items[0].size() ? ret : -1;
  2848. }
  2849. void CAltarWindow::SacrificeBackpack()
  2850. {
  2851. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  2852. for (auto & elem : hero->artifactsInBackpack)
  2853. {
  2854. if(vstd::contains(toOmmit, elem.artifact))
  2855. {
  2856. toOmmit -= elem.artifact;
  2857. continue;
  2858. }
  2859. putOnAltar(nullptr, elem.artifact);
  2860. }
  2861. arts->scrollBackpack(0);
  2862. calcTotalExp();
  2863. }
  2864. void CAltarWindow::artifactPicked()
  2865. {
  2866. redraw();
  2867. }
  2868. void CAltarWindow::showAll(SDL_Surface * to)
  2869. {
  2870. CTradeWindow::showAll(to);
  2871. if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  2872. {
  2873. artIcon->setFrame(arts->commonInfo->src.art->artType->iconIndex);
  2874. artIcon->showAll(to);
  2875. int dmp, val;
  2876. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  2877. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, Colors::WHITE, to);
  2878. }
  2879. }
  2880. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  2881. {
  2882. int artID = art->artType->id;
  2883. if(artID != 1 && artID < 7) //special art
  2884. {
  2885. logGlobal->warnStream() << "Cannot put special artifact on altar!";
  2886. return false;
  2887. }
  2888. if(!altarSlot)
  2889. {
  2890. int slotIndex = firstFreeSlot();
  2891. if(slotIndex < 0)
  2892. {
  2893. logGlobal->warnStream() << "No free slots on altar!";
  2894. return false;
  2895. }
  2896. altarSlot = items[0][slotIndex];
  2897. }
  2898. int dmp, val;
  2899. market->getOffer(artID, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  2900. arts->artifactsOnAltar.insert(art);
  2901. altarSlot->setArtInstance(art);
  2902. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  2903. deal->block(false);
  2904. return true;
  2905. }
  2906. void CAltarWindow::moveFromSlotToAltar(ArtifactPosition slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  2907. {
  2908. auto freeBackpackSlot = ArtifactPosition(hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  2909. if(arts->commonInfo->src.art)
  2910. {
  2911. arts->commonInfo->dst.slotID = freeBackpackSlot;
  2912. arts->commonInfo->dst.AOH = arts;
  2913. }
  2914. if(putOnAltar(altarSlot, art))
  2915. {
  2916. if(slotID < GameConstants::BACKPACK_START)
  2917. LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slotID), ArtifactLocation(hero, freeBackpackSlot));
  2918. else
  2919. {
  2920. arts->commonInfo->src.clear();
  2921. arts->commonInfo->dst.clear();
  2922. CCS->curh->dragAndDropCursor(nullptr);
  2923. arts->unmarkSlots(false);
  2924. }
  2925. }
  2926. }
  2927. void CSystemOptionsWindow::setMusicVolume( int newVolume )
  2928. {
  2929. Settings volume = settings.write["general"]["music"];
  2930. volume->Float() = newVolume;
  2931. }
  2932. void CSystemOptionsWindow::setSoundVolume( int newVolume )
  2933. {
  2934. Settings volume = settings.write["general"]["sound"];
  2935. volume->Float() = newVolume;
  2936. }
  2937. void CSystemOptionsWindow::setHeroMoveSpeed( int newSpeed )
  2938. {
  2939. Settings speed = settings.write["adventure"]["heroSpeed"];
  2940. speed->Float() = newSpeed;
  2941. }
  2942. void CSystemOptionsWindow::setMapScrollingSpeed( int newSpeed )
  2943. {
  2944. Settings speed = settings.write["adventure"]["scrollSpeed"];
  2945. speed->Float() = newSpeed;
  2946. }
  2947. CSystemOptionsWindow::CSystemOptionsWindow():
  2948. CWindowObject(PLAYER_COLORED, "SysOpBck"),
  2949. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  2950. {
  2951. //TODO: translation and\or config file
  2952. static const std::string fsLabel = "Fullscreen";
  2953. static const std::string fsHelp = "{Fullscreen}\n\n If selected, VCMI will run in fullscreen mode, othervice VCMI will run in window";
  2954. static const std::string cwLabel = "Classic creature window";
  2955. static const std::string cwHelp = "{Classic creature window}\n\n Enable original Heroes 3 creature window instead of new window from VCMI";
  2956. static const std::string rsLabel = "Resolution";
  2957. static const std::string rsHelp = "{Select resolution}\n\n Change in-game screen resolution. Will only affect adventure map. Game restart required to apply new resolution.";
  2958. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2959. title = new CLabel(242, 32, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[568]);
  2960. //left window section
  2961. leftGroup = new CLabelGroup(FONT_MEDIUM, CENTER, Colors::YELLOW);
  2962. leftGroup->add(122, 64, CGI->generaltexth->allTexts[569]);
  2963. leftGroup->add(122, 130, CGI->generaltexth->allTexts[570]);
  2964. leftGroup->add(122, 196, CGI->generaltexth->allTexts[571]);
  2965. leftGroup->add(122, 262, rsLabel); //CGI->generaltexth->allTexts[20]
  2966. leftGroup->add(122, 347, CGI->generaltexth->allTexts[394]);
  2967. leftGroup->add(122, 412, CGI->generaltexth->allTexts[395]);
  2968. //right section
  2969. rightGroup = new CLabelGroup(FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  2970. rightGroup->add(282, 57, CGI->generaltexth->allTexts[572]);
  2971. rightGroup->add(282, 89, CGI->generaltexth->allTexts[573]);
  2972. rightGroup->add(282, 121, CGI->generaltexth->allTexts[574]);
  2973. rightGroup->add(282, 153, CGI->generaltexth->allTexts[575]);
  2974. rightGroup->add(282, 185, cwLabel); //CGI->generaltexth->allTexts[576]
  2975. rightGroup->add(282, 217, fsLabel); //CGI->generaltexth->allTexts[577]
  2976. //setting up buttons
  2977. load = new CAdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second,
  2978. boost::bind(&CSystemOptionsWindow::bloadf, this), 246, 298, "SOLOAD.DEF", SDLK_l);
  2979. load->swappedImages = true;
  2980. load->update();
  2981. save = new CAdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second,
  2982. boost::bind(&CSystemOptionsWindow::bsavef, this), 357, 298, "SOSAVE.DEF", SDLK_s);
  2983. save->swappedImages = true;
  2984. save->update();
  2985. restart = new CAdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second,
  2986. boost::bind(&CSystemOptionsWindow::brestartf, this), 246, 357, "SORSTRT", SDLK_r);
  2987. restart->swappedImages = true;
  2988. restart->update();
  2989. mainMenu = new CAdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second,
  2990. boost::bind(&CSystemOptionsWindow::bmainmenuf, this), 357, 357, "SOMAIN.DEF", SDLK_m);
  2991. mainMenu->swappedImages = true;
  2992. mainMenu->update();
  2993. quitGame = new CAdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second,
  2994. boost::bind(&CSystemOptionsWindow::bquitf, this), 246, 415, "soquit.def", SDLK_q);
  2995. quitGame->swappedImages = true;
  2996. quitGame->update();
  2997. backToMap = new CAdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second,
  2998. boost::bind(&CSystemOptionsWindow::breturnf, this), 357, 415, "soretrn.def", SDLK_RETURN);
  2999. backToMap->swappedImages = true;
  3000. backToMap->update();
  3001. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  3002. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  3003. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", 28, 77, 1);
  3004. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", 76, 77, 2);
  3005. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", 124, 77, 4);
  3006. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", 172, 77, 8);
  3007. heroMoveSpeed->select(settings["adventure"]["heroSpeed"].Float(), 1);
  3008. heroMoveSpeed->onChange = boost::bind(&CSystemOptionsWindow::setHeroMoveSpeed, this, _1);
  3009. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  3010. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", 28, 210, 1);
  3011. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", 92, 210, 2);
  3012. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", 156, 210, 4);
  3013. mapScrollSpeed->select(settings["adventure"]["scrollSpeed"].Float(), 1);
  3014. mapScrollSpeed->onChange = boost::bind(&CSystemOptionsWindow::setMapScrollingSpeed, this, _1);
  3015. musicVolume = new CHighlightableButtonsGroup(0, true);
  3016. for(int i=0; i<10; ++i)
  3017. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", 29 + 19*i, 359, i*11);
  3018. musicVolume->select(CCS->musich->getVolume(), 1);
  3019. musicVolume->onChange = boost::bind(&CSystemOptionsWindow::setMusicVolume, this, _1);
  3020. effectsVolume = new CHighlightableButtonsGroup(0, true);
  3021. for(int i=0; i<10; ++i)
  3022. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", 29 + 19*i, 425, i*11);
  3023. effectsVolume->select(CCS->soundh->getVolume(), 1);
  3024. effectsVolume->onChange = boost::bind(&CSystemOptionsWindow::setSoundVolume, this, _1);
  3025. showReminder = new CHighlightableButton(
  3026. boost::bind(&CSystemOptionsWindow::toggleReminder, this, true), boost::bind(&CSystemOptionsWindow::toggleReminder, this, false),
  3027. std::map<int,std::string>(), CGI->generaltexth->zelp[361].second, false, "sysopchk.def", nullptr, 246, 87, false);
  3028. quickCombat = new CHighlightableButton(
  3029. boost::bind(&CSystemOptionsWindow::toggleQuickCombat, this, true), boost::bind(&CSystemOptionsWindow::toggleQuickCombat, this, false),
  3030. std::map<int,std::string>(), CGI->generaltexth->zelp[362].second, false, "sysopchk.def", nullptr, 246, 87+32, false);
  3031. newCreatureWin = new CHighlightableButton(
  3032. boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, true), boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, false),
  3033. std::map<int,std::string>(), cwHelp, false, "sysopchk.def", nullptr, 246, 183, false);
  3034. fullscreen = new CHighlightableButton(
  3035. boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, true), boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, false),
  3036. std::map<int,std::string>(), fsHelp, false, "sysopchk.def", nullptr, 246, 215, false);
  3037. showReminder->select(settings["adventure"]["heroReminder"].Bool());
  3038. quickCombat->select(settings["adventure"]["quickCombat"].Bool());
  3039. newCreatureWin->select(settings["general"]["classicCreatureWindow"].Bool());
  3040. fullscreen->select(settings["video"]["fullscreen"].Bool());
  3041. onFullscreenChanged([&](const JsonNode &newState){ fullscreen->select(newState.Bool());});
  3042. gameResButton = new CAdventureMapButton("", rsHelp, boost::bind(&CSystemOptionsWindow::selectGameRes, this), 28, 275,"SYSOB12", SDLK_g);
  3043. std::string resText;
  3044. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["width"].Float());
  3045. resText += "x";
  3046. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["height"].Float());
  3047. gameResLabel = new CLabel(170, 292, FONT_MEDIUM, CENTER, Colors::YELLOW, resText);
  3048. }
  3049. void CSystemOptionsWindow::selectGameRes()
  3050. {
  3051. //TODO: translation and\or config file
  3052. static const std::string rsLabel = "Select resolution";
  3053. static const std::string rsHelp = "Change in-game screen resolution.";
  3054. std::vector<std::string> items;
  3055. for( config::CConfigHandler::GuiOptionsMap::value_type& value : conf.guiOptions)
  3056. {
  3057. std::string resX = boost::lexical_cast<std::string>(value.first.first);
  3058. std::string resY = boost::lexical_cast<std::string>(value.first.second);
  3059. items.push_back(resX + 'x' + resY);
  3060. }
  3061. GH.pushInt(new CObjectListWindow(items, nullptr, rsLabel, rsHelp,
  3062. boost::bind(&CSystemOptionsWindow::setGameRes, this, _1)));
  3063. }
  3064. void CSystemOptionsWindow::setGameRes(int index)
  3065. {
  3066. auto iter = conf.guiOptions.begin();
  3067. std::advance(iter, index);
  3068. //do not set resolution to illegal one (0x0)
  3069. assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
  3070. Settings gameRes = settings.write["video"]["screenRes"];
  3071. gameRes["width"].Float() = iter->first.first;
  3072. gameRes["height"].Float() = iter->first.second;
  3073. std::string resText;
  3074. resText += boost::lexical_cast<std::string>(iter->first.first);
  3075. resText += "x";
  3076. resText += boost::lexical_cast<std::string>(iter->first.second);
  3077. gameResLabel->setTxt(resText);
  3078. }
  3079. void CSystemOptionsWindow::toggleReminder(bool on)
  3080. {
  3081. Settings heroReminder = settings.write["adventure"]["heroReminder"];
  3082. heroReminder->Bool() = on;
  3083. }
  3084. void CSystemOptionsWindow::toggleQuickCombat(bool on)
  3085. {
  3086. Settings quickCombat = settings.write["adventure"]["quickCombat"];
  3087. quickCombat->Bool() = on;
  3088. }
  3089. void CSystemOptionsWindow::toggleCreatureWin(bool on)
  3090. {
  3091. Settings classicCreatureWindow = settings.write["general"]["classicCreatureWindow"];
  3092. classicCreatureWindow->Bool() = on;
  3093. }
  3094. void CSystemOptionsWindow::toggleFullscreen(bool on)
  3095. {
  3096. Settings fullscreen = settings.write["video"]["fullscreen"];
  3097. fullscreen->Bool() = on;
  3098. }
  3099. void CSystemOptionsWindow::bquitf()
  3100. {
  3101. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this]{ closeAndPushEvent(SDL_QUIT); }, 0);
  3102. }
  3103. void CSystemOptionsWindow::breturnf()
  3104. {
  3105. GH.popIntTotally(this);
  3106. }
  3107. void CSystemOptionsWindow::bmainmenuf()
  3108. {
  3109. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this]{ closeAndPushEvent(SDL_USEREVENT, RETURN_TO_MAIN_MENU); }, 0);
  3110. }
  3111. void CSystemOptionsWindow::bloadf()
  3112. {
  3113. GH.popIntTotally(this);
  3114. LOCPLINT->proposeLoadingGame();
  3115. }
  3116. void CSystemOptionsWindow::bsavef()
  3117. {
  3118. GH.popIntTotally(this);
  3119. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  3120. }
  3121. void CSystemOptionsWindow::brestartf()
  3122. {
  3123. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this]{ closeAndPushEvent(SDL_USEREVENT, RESTART_GAME); }, 0);
  3124. }
  3125. void CSystemOptionsWindow::closeAndPushEvent(int eventType, int code /*= 0*/)
  3126. {
  3127. GH.popIntTotally(this);
  3128. GH.pushSDLEvent(eventType, code);
  3129. }
  3130. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj):
  3131. CWindowObject(PLAYER_COLORED, "TPTAVERN"),
  3132. tavernObj(TavernObj)
  3133. {
  3134. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3135. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  3136. assert(h.size() == 2);
  3137. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  3138. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  3139. selected = 0;
  3140. if (!h[0])
  3141. selected = 1;
  3142. if (!h[0] && !h[1])
  3143. selected = -1;
  3144. oldSelected = -1;
  3145. new CLabel(200, 35, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[37]);
  3146. new CLabel(320, 328, FONT_SMALL, CENTER, Colors::WHITE, "2500");
  3147. new CTextBox(LOCPLINT->cb->getTavernGossip(tavernObj), Rect(32, 190, 330, 68), 0, FONT_SMALL, CENTER, Colors::WHITE);
  3148. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  3149. cancel = new CAdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  3150. recruit = new CAdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  3151. thiefGuild = new CAdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  3152. if(LOCPLINT->cb->getResourceAmount(Res::GOLD) < 2500) //not enough gold
  3153. {
  3154. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  3155. recruit->block(true);
  3156. }
  3157. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  3158. {
  3159. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  3160. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  3161. recruit->block(true);
  3162. }
  3163. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  3164. {
  3165. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  3166. recruit->block(true);
  3167. }
  3168. else
  3169. {
  3170. if(selected == -1)
  3171. recruit->block(true);
  3172. }
  3173. CCS->videoh->open("TAVERN.BIK");
  3174. }
  3175. void CTavernWindow::recruitb()
  3176. {
  3177. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  3178. const CGObjectInstance *obj = tavernObj;
  3179. close();
  3180. LOCPLINT->cb->recruitHero(obj, toBuy);
  3181. }
  3182. void CTavernWindow::thievesguildb()
  3183. {
  3184. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  3185. }
  3186. CTavernWindow::~CTavernWindow()
  3187. {
  3188. CCS->videoh->close();
  3189. }
  3190. void CTavernWindow::show(SDL_Surface * to)
  3191. {
  3192. CWindowObject::show(to);
  3193. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  3194. if(selected >= 0)
  3195. {
  3196. HeroPortrait *sel = selected ? h2 : h1;
  3197. if (selected != oldSelected && !recruit->isBlocked())
  3198. {
  3199. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  3200. oldSelected = selected;
  3201. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  3202. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  3203. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  3204. }
  3205. printAtMiddleWBLoc(sel->descr, 146, 395, FONT_SMALL, 200, Colors::WHITE, to);
  3206. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  3207. }
  3208. }
  3209. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  3210. {
  3211. if(previousState && !down && h)
  3212. *_sel = _id;
  3213. }
  3214. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  3215. {
  3216. if(down && h)
  3217. {
  3218. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  3219. }
  3220. }
  3221. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  3222. : h(H), _sel(&sel), _id(id)
  3223. {
  3224. addUsedEvents(LCLICK | RCLICK | HOVER);
  3225. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3226. h = H;
  3227. pos.x += x;
  3228. pos.y += y;
  3229. pos.w = 58;
  3230. pos.h = 64;
  3231. if(H)
  3232. {
  3233. hoverName = CGI->generaltexth->tavernInfo[4];
  3234. boost::algorithm::replace_first(hoverName,"%s",H->name);
  3235. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  3236. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  3237. if(vstd::contains(h->artifactsWorn, ArtifactPosition(i)))
  3238. artifs--;
  3239. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  3240. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  3241. descr[sizeof(descr)-1] = '\0';
  3242. new CAnimImage("portraitsLarge", h->portrait);
  3243. }
  3244. }
  3245. void CTavernWindow::HeroPortrait::hover( bool on )
  3246. {
  3247. //Hoverable::hover(on);
  3248. if(on)
  3249. GH.statusbar->print(hoverName);
  3250. else
  3251. GH.statusbar->clear();
  3252. }
  3253. void CInGameConsole::show(SDL_Surface * to)
  3254. {
  3255. int number = 0;
  3256. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3257. boost::unique_lock<boost::mutex> lock(texts_mx);
  3258. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  3259. {
  3260. Point leftBottomCorner(0, screen->h);
  3261. if(LOCPLINT->battleInt)
  3262. {
  3263. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3264. }
  3265. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  3266. Point(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20));
  3267. if(SDL_GetTicks() - it->second > defaultTimeout)
  3268. {
  3269. toDel.push_back(it);
  3270. }
  3271. }
  3272. for(auto & elem : toDel)
  3273. {
  3274. texts.erase(elem);
  3275. }
  3276. }
  3277. void CInGameConsole::print(const std::string &txt)
  3278. {
  3279. boost::unique_lock<boost::mutex> lock(texts_mx);
  3280. int lineLen = conf.go()->ac.outputLineLength;
  3281. if(txt.size() < lineLen)
  3282. {
  3283. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3284. if(texts.size() > maxDisplayedTexts)
  3285. {
  3286. texts.pop_front();
  3287. }
  3288. }
  3289. else
  3290. {
  3291. assert(lineLen);
  3292. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3293. {
  3294. std::string part = txt.substr(g * lineLen, lineLen);
  3295. if(part.size() == 0)
  3296. break;
  3297. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3298. if(texts.size() > maxDisplayedTexts)
  3299. {
  3300. texts.pop_front();
  3301. }
  3302. }
  3303. }
  3304. }
  3305. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3306. {
  3307. if(key.type != SDL_KEYDOWN) return;
  3308. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3309. switch(key.keysym.sym)
  3310. {
  3311. case SDLK_TAB:
  3312. case SDLK_ESCAPE:
  3313. {
  3314. if(captureAllKeys)
  3315. {
  3316. captureAllKeys = false;
  3317. endEnteringText(false);
  3318. }
  3319. else if(SDLK_TAB)
  3320. {
  3321. captureAllKeys = true;
  3322. startEnteringText();
  3323. }
  3324. break;
  3325. }
  3326. case SDLK_RETURN: //enter key
  3327. {
  3328. if(enteredText.size() > 0 && captureAllKeys)
  3329. {
  3330. captureAllKeys = false;
  3331. endEnteringText(true);
  3332. }
  3333. break;
  3334. }
  3335. case SDLK_BACKSPACE:
  3336. {
  3337. if(enteredText.size() > 1)
  3338. {
  3339. enteredText.resize(enteredText.size()-1);
  3340. enteredText[enteredText.size()-1] = '_';
  3341. refreshEnteredText();
  3342. }
  3343. break;
  3344. }
  3345. case SDLK_UP: //up arrow
  3346. {
  3347. if(previouslyEntered.size() == 0)
  3348. break;
  3349. if(prevEntDisp == -1)
  3350. {
  3351. prevEntDisp = previouslyEntered.size() - 1;
  3352. enteredText = previouslyEntered[prevEntDisp] + "_";
  3353. refreshEnteredText();
  3354. }
  3355. else if( prevEntDisp > 0)
  3356. {
  3357. --prevEntDisp;
  3358. enteredText = previouslyEntered[prevEntDisp] + "_";
  3359. refreshEnteredText();
  3360. }
  3361. break;
  3362. }
  3363. case SDLK_DOWN: //down arrow
  3364. {
  3365. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3366. {
  3367. ++prevEntDisp;
  3368. enteredText = previouslyEntered[prevEntDisp] + "_";
  3369. refreshEnteredText();
  3370. }
  3371. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3372. {
  3373. prevEntDisp = -1;
  3374. enteredText = "_";
  3375. refreshEnteredText();
  3376. }
  3377. break;
  3378. }
  3379. default:
  3380. {
  3381. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3382. {
  3383. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3384. {
  3385. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3386. enteredText += "_";
  3387. refreshEnteredText();
  3388. }
  3389. }
  3390. break;
  3391. }
  3392. }
  3393. }
  3394. void CInGameConsole::startEnteringText()
  3395. {
  3396. enteredText = "_";
  3397. if(GH.topInt() == adventureInt)
  3398. {
  3399. GH.statusbar->alignment = TOPLEFT;
  3400. GH.statusbar->calcOffset();
  3401. GH.statusbar->print(enteredText);
  3402. //Prevent changes to the text from mouse interaction with the adventure map
  3403. GH.statusbar->lock(true);
  3404. }
  3405. else if(LOCPLINT->battleInt)
  3406. {
  3407. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3408. }
  3409. }
  3410. void CInGameConsole::endEnteringText(bool printEnteredText)
  3411. {
  3412. prevEntDisp = -1;
  3413. if(printEnteredText)
  3414. {
  3415. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3416. LOCPLINT->cb->sendMessage(txt);
  3417. previouslyEntered.push_back(txt);
  3418. //print(txt);
  3419. }
  3420. enteredText = "";
  3421. if(GH.topInt() == adventureInt)
  3422. {
  3423. GH.statusbar->alignment = CENTER;
  3424. GH.statusbar->calcOffset();
  3425. GH.statusbar->lock(false);
  3426. GH.statusbar->clear();
  3427. }
  3428. else if(LOCPLINT->battleInt)
  3429. {
  3430. LOCPLINT->battleInt->console->ingcAlter = "";
  3431. }
  3432. }
  3433. void CInGameConsole::refreshEnteredText()
  3434. {
  3435. if(GH.topInt() == adventureInt)
  3436. {
  3437. GH.statusbar->lock(false);
  3438. GH.statusbar->clear();
  3439. GH.statusbar->print(enteredText);
  3440. GH.statusbar->lock(true);
  3441. }
  3442. else if(LOCPLINT->battleInt)
  3443. {
  3444. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3445. }
  3446. }
  3447. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3448. {
  3449. addUsedEvents(KEYBOARD);
  3450. }
  3451. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits ):
  3452. CWindowObject(PLAYER_COLORED, "GARRISON")
  3453. {
  3454. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3455. garr = new CGarrisonInt(92, 127, 4, Point(0,96), background->bg, Point(93,127), up, down, removableUnits);
  3456. {
  3457. CAdventureMapButton *split = new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),88,314,"IDV6432.DEF");
  3458. removeChild(split);
  3459. garr->addSplitBtn(split);
  3460. }
  3461. quit = new CAdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),399,314,"IOK6432.DEF",SDLK_RETURN);
  3462. std::string titleText;
  3463. if (garr->armedObjs[1]->tempOwner == garr->armedObjs[0]->tempOwner)
  3464. titleText = CGI->generaltexth->allTexts[709];
  3465. else
  3466. {
  3467. titleText = CGI->generaltexth->allTexts[35];
  3468. boost::algorithm::replace_first(titleText, "%s", garr->armedObjs[0]->Slots().begin()->second->type->namePl);
  3469. }
  3470. new CLabel(275, 30, FONT_BIG, CENTER, Colors::YELLOW, titleText);
  3471. new CAnimImage("CREST58", garr->armedObjs[0]->getOwner().getNum(), 0, 28, 124);
  3472. new CAnimImage("PortraitsLarge", dynamic_cast<const CGHeroInstance*>(garr->armedObjs[1])->portrait, 0, 29, 222);
  3473. }
  3474. IShowActivatable::IShowActivatable()
  3475. {
  3476. type = 0;
  3477. }
  3478. CGarrisonHolder::CGarrisonHolder()
  3479. {
  3480. }
  3481. void CWindowWithGarrison::updateGarrisons()
  3482. {
  3483. garr->recreateSlots();
  3484. }
  3485. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art):
  3486. locked(false), picked(false), marked(false), ourArt(Art)
  3487. {
  3488. pos += position;
  3489. pos.w = pos.h = 44;
  3490. createImage();
  3491. }
  3492. void CArtPlace::createImage()
  3493. {
  3494. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3495. int graphic = 0;
  3496. if (ourArt)
  3497. graphic = ourArt->artType->iconIndex;
  3498. if (locked)
  3499. graphic = ArtifactID::ART_LOCK;
  3500. image = new CAnimImage("artifact", graphic);
  3501. if (!ourArt)
  3502. image->disable();
  3503. selection = new CAnimImage("artifact", ArtifactID::ART_SELECTION);
  3504. selection->disable();
  3505. }
  3506. void CArtPlace::lockSlot(bool on)
  3507. {
  3508. if (locked == on)
  3509. return;
  3510. locked = on;
  3511. if (on)
  3512. image->setFrame(ArtifactID::ART_LOCK);
  3513. else
  3514. image->setFrame(ourArt->artType->iconIndex);
  3515. }
  3516. void CArtPlace::pickSlot(bool on)
  3517. {
  3518. if (picked == on)
  3519. return;
  3520. picked = on;
  3521. if (on)
  3522. image->disable();
  3523. else
  3524. image->enable();
  3525. }
  3526. void CArtPlace::selectSlot(bool on)
  3527. {
  3528. if (marked == on)
  3529. return;
  3530. marked = on;
  3531. if (on)
  3532. selection->enable();
  3533. else
  3534. selection->disable();
  3535. }
  3536. void CArtPlace::clickLeft(tribool down, bool previousState)
  3537. {
  3538. //LRClickableAreaWTextComp::clickLeft(down);
  3539. bool inBackpack = slotID >= GameConstants::BACKPACK_START,
  3540. srcInBackpack = ourOwner->commonInfo->src.slotID >= GameConstants::BACKPACK_START,
  3541. srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  3542. if(ourOwner->highlightModeCallback && ourArt)
  3543. {
  3544. if(down)
  3545. {
  3546. if(ourArt->artType->id < 7) //War Machine or Spellbook
  3547. {
  3548. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
  3549. }
  3550. else
  3551. {
  3552. ourOwner->unmarkSlots(false);
  3553. selectSlot(true);
  3554. ourOwner->highlightModeCallback(this);
  3555. }
  3556. }
  3557. return;
  3558. }
  3559. // If clicked on spellbook, open it only if no artifact is held at the moment.
  3560. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  3561. {
  3562. if(ourArt->artType->id == 0)
  3563. {
  3564. auto spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt);
  3565. GH.pushInt(spellWindow);
  3566. }
  3567. }
  3568. if (!down && previousState)
  3569. {
  3570. if(ourArt && ourArt->artType->id == 0) //spellbook
  3571. return; //this is handled separately
  3572. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  3573. {
  3574. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  3575. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  3576. {
  3577. if(ourArt->artType->id == 3) //catapult cannot be highlighted
  3578. {
  3579. std::vector<CComponent *> catapult(1, new CComponent(CComponent::artifact, 3, 0));
  3580. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  3581. return;
  3582. }
  3583. select();
  3584. }
  3585. }
  3586. else if(ourArt == ourOwner->commonInfo->src.art) //restore previously picked artifact
  3587. {
  3588. deselect();
  3589. }
  3590. else //perform artifact transition
  3591. {
  3592. if(inBackpack) // Backpack destination.
  3593. {
  3594. if(srcInBackpack && slotID == ourOwner->commonInfo->src.slotID + 1) //next slot (our is not visible, so visually same as "old" place) to the art -> make nothing, return artifact to slot
  3595. {
  3596. deselect();
  3597. }
  3598. else
  3599. {
  3600. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  3601. switch(cur->id)
  3602. {
  3603. case ArtifactID::CATAPULT:
  3604. //should not happen, catapult cannot be selected
  3605. assert(cur->id != ArtifactID::CATAPULT);
  3606. break;
  3607. case ArtifactID::BALLISTA: case ArtifactID::AMMO_CART: case ArtifactID::FIRST_AID_TENT: //war machines cannot go to backpack
  3608. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->Name()));
  3609. break;
  3610. default:
  3611. setMeAsDest();
  3612. vstd::amin(ourOwner->commonInfo->dst.slotID, ArtifactPosition(
  3613. ourOwner->curHero->artifactsInBackpack.size() + GameConstants::BACKPACK_START));
  3614. if(srcInBackpack && srcInSameHero)
  3615. {
  3616. if(!ourArt //cannot move from backpack to AFTER backpack -> combined with vstd::amin above it will guarantee that dest is at most the last artifact
  3617. || ourOwner->commonInfo->src.slotID < ourOwner->commonInfo->dst.slotID) //rearranging arts in backpack after taking src artifact, the dest id will be shifted
  3618. vstd::advance(ourOwner->commonInfo->dst.slotID, -1);
  3619. }
  3620. if(srcInSameHero && ourOwner->commonInfo->dst.slotID == ourOwner->commonInfo->src.slotID) //we came to src == dst
  3621. deselect();
  3622. else
  3623. ourOwner->realizeCurrentTransaction();
  3624. break;
  3625. }
  3626. }
  3627. }
  3628. //check if swap is possible
  3629. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  3630. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  3631. {
  3632. setMeAsDest();
  3633. //
  3634. // // Special case when the dest artifact can't be fit into the src slot.
  3635. // //CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
  3636. // const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->src.AOH;
  3637. // ui16 srcSlotID = ourOwner->commonInfo->src.slotID;
  3638. // if (ourArt && srcSlotID < 19 && !ourArt->canBePutAt(ArtifactLocation(srcAOH->curHero, srcSlotID)))
  3639. // {
  3640. // // Put dest artifact into owner's backpack.
  3641. // ourOwner->commonInfo->src.AOH = ourOwner;
  3642. // ourOwner->commonInfo->src.slotID = ourOwner->curHero->artifacts.size() + 19;
  3643. // }
  3644. ourOwner->realizeCurrentTransaction();
  3645. }
  3646. }
  3647. }
  3648. }
  3649. void CArtPlace::clickRight(tribool down, bool previousState)
  3650. {
  3651. if(down && ourArt && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;]
  3652. {
  3653. if (slotID < GameConstants::BACKPACK_START)
  3654. {
  3655. if(ourOwner->allowedAssembling)
  3656. {
  3657. std::vector<const CArtifact *> assemblyPossibilities = ourArt->assemblyPossibilities(ourOwner->curHero);
  3658. // If the artifact can be assembled, display dialog.
  3659. for(const CArtifact *combination : assemblyPossibilities)
  3660. {
  3661. LOCPLINT->showArtifactAssemblyDialog(
  3662. ourArt->artType->id,
  3663. combination->id,
  3664. true,
  3665. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), ourOwner->curHero, slotID, true, combination->id),
  3666. 0);
  3667. if(assemblyPossibilities.size() > 2)
  3668. {
  3669. logGlobal->warnStream() << "More than one possibility of assembling... taking only first";
  3670. break;
  3671. }
  3672. return;
  3673. }
  3674. // Otherwise if the artifact can be diasassembled, display dialog.
  3675. if(ourArt->canBeDisassembled())
  3676. {
  3677. LOCPLINT->showArtifactAssemblyDialog(
  3678. ourArt->artType->id,
  3679. 0,
  3680. false,
  3681. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb.get(), ourOwner->curHero, slotID, false, ArtifactID()),
  3682. 0);
  3683. return;
  3684. }
  3685. }
  3686. }
  3687. // Lastly just show the artifact description.
  3688. LRClickableAreaWTextComp::clickRight(down, previousState);
  3689. }
  3690. }
  3691. /**
  3692. * Selects artifact slot so that the containing artifact looks like it's picked up.
  3693. */
  3694. void CArtPlace::select ()
  3695. {
  3696. if (locked)
  3697. return;
  3698. selectSlot(true);
  3699. pickSlot(true);
  3700. if(ourArt->canBeDisassembled() && slotID < GameConstants::BACKPACK_START) //worn combined artifact -> locks have to disappear
  3701. {
  3702. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3703. {
  3704. CArtPlace *ap = ourOwner->getArtPlace(i);
  3705. ap->pickSlot(ourArt->isPart(ap->ourArt));
  3706. }
  3707. }
  3708. //int backpackCorrection = -(slotID - Arts::BACKPACK_START < ourOwner->backpackPos);
  3709. CCS->curh->dragAndDropCursor(new CAnimImage("artifact", ourArt->artType->iconIndex));
  3710. ourOwner->commonInfo->src.setTo(this, false);
  3711. ourOwner->markPossibleSlots(ourArt);
  3712. if(slotID >= GameConstants::BACKPACK_START)
  3713. ourOwner->scrollBackpack(0); //will update slots
  3714. ourOwner->updateParentWindow();
  3715. ourOwner->safeRedraw();
  3716. }
  3717. /**
  3718. * Deselects the artifact slot. FIXME: Not used. Maybe it should?
  3719. */
  3720. void CArtPlace::deselect ()
  3721. {
  3722. pickSlot(false);
  3723. if(ourArt && ourArt->canBeDisassembled()) //combined art returned to its slot -> restore locks
  3724. {
  3725. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3726. ourOwner->getArtPlace(i)->pickSlot(false);
  3727. }
  3728. CCS->curh->dragAndDropCursor(nullptr);
  3729. ourOwner->unmarkSlots();
  3730. ourOwner->commonInfo->src.clear();
  3731. if(slotID >= GameConstants::BACKPACK_START)
  3732. ourOwner->scrollBackpack(0); //will update slots
  3733. ourOwner->updateParentWindow();
  3734. ourOwner->safeRedraw();
  3735. }
  3736. void CArtPlace::showAll(SDL_Surface * to)
  3737. {
  3738. if (ourArt && !picked && ourArt == ourOwner->curHero->getArt(slotID, false)) //last condition is needed for disassembling -> artifact may be gone, but we don't know yet TODO: real, nice solution
  3739. {
  3740. CIntObject::showAll(to);
  3741. }
  3742. if(marked && active)
  3743. {
  3744. // Draw vertical bars.
  3745. for (int i = 0; i < pos.h; ++i)
  3746. {
  3747. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  3748. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  3749. }
  3750. // Draw horizontal bars.
  3751. for (int i = 0; i < pos.w; ++i)
  3752. {
  3753. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  3754. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  3755. }
  3756. }
  3757. }
  3758. bool CArtPlace::fitsHere(const CArtifactInstance * art) const
  3759. {
  3760. // You can place 'no artifact' anywhere.
  3761. if(!art)
  3762. return true;
  3763. // Anything can but War Machines can be placed in backpack.
  3764. if (slotID >= GameConstants::BACKPACK_START)
  3765. return !CGI->arth->isBigArtifact(art->artType->id);
  3766. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  3767. }
  3768. void CArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  3769. {
  3770. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  3771. }
  3772. void CArtPlace::setArtifact(const CArtifactInstance *art)
  3773. {
  3774. baseType = -1; //by default we don't store any component
  3775. ourArt = art;
  3776. if(!art)
  3777. {
  3778. image->disable();
  3779. text = std::string();
  3780. hoverText = CGI->generaltexth->allTexts[507];
  3781. }
  3782. else
  3783. {
  3784. image->enable();
  3785. image->setFrame(locked ? ArtifactID::ART_LOCK : art->artType->iconIndex);
  3786. std::string artDesc = ourArt->artType->Description();
  3787. if (vstd::contains (artDesc, '{'))
  3788. text = artDesc;
  3789. else
  3790. text = '{' + ourArt->artType->Name() + "}\n\n" + artDesc; //workaround for new artifacts with single name, turns it to H3-style
  3791. if(art->artType->id == 1) //spell scroll
  3792. {
  3793. // we expect scroll description to be like this: This scroll contains the [spell name] spell which is added into your spell book for as long as you carry the scroll.
  3794. // so we want to replace text in [...] with a spell name
  3795. // however other language versions don't have name placeholder at all, so we have to be careful
  3796. int spellID = art->getGivenSpellID();
  3797. size_t nameStart = text.find_first_of('[');
  3798. size_t nameEnd = text.find_first_of(']', nameStart);
  3799. if(spellID >= 0)
  3800. {
  3801. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  3802. text = text.replace(nameStart, nameEnd - nameStart + 1, CGI->spellh->spells[spellID]->name);
  3803. //add spell component info (used to provide a pic in r-click popup)
  3804. baseType = CComponent::spell;
  3805. type = spellID;
  3806. bonusValue = 0;
  3807. }
  3808. }
  3809. else
  3810. {
  3811. baseType = CComponent::artifact;
  3812. type = art->artType->iconIndex;
  3813. bonusValue = 0;
  3814. }
  3815. if (locked) // Locks should appear as empty.
  3816. hoverText = CGI->generaltexth->allTexts[507];
  3817. else
  3818. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->Name());
  3819. }
  3820. }
  3821. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  3822. {
  3823. if((!down) && previousState)
  3824. {
  3825. std::vector<CComponent*> comp(1, createComponent());
  3826. LOCPLINT->showInfoDialog(text, comp);
  3827. }
  3828. }
  3829. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  3830. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  3831. {
  3832. }
  3833. CComponent * LRClickableAreaWTextComp::createComponent() const
  3834. {
  3835. if(baseType >= 0)
  3836. return new CComponent(CComponent::Etype(baseType), type, bonusValue);
  3837. else
  3838. return nullptr;
  3839. }
  3840. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  3841. {
  3842. if(down)
  3843. {
  3844. if(CComponent *comp = createComponent())
  3845. {
  3846. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  3847. return;
  3848. }
  3849. }
  3850. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occurred
  3851. }
  3852. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  3853. {
  3854. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3855. addUsedEvents(LCLICK | RCLICK | HOVER);
  3856. pos.x += x; pos.w = 58;
  3857. pos.y += y; pos.h = 64;
  3858. if (hero)
  3859. new CAnimImage("PortraitsLarge", hero->portrait);
  3860. }
  3861. void CHeroArea::clickLeft(tribool down, bool previousState)
  3862. {
  3863. if((!down) && previousState && hero)
  3864. LOCPLINT->openHeroWindow(hero);
  3865. }
  3866. void CHeroArea::clickRight(tribool down, bool previousState)
  3867. {
  3868. if((!down) && previousState && hero)
  3869. LOCPLINT->openHeroWindow(hero);
  3870. }
  3871. void CHeroArea::hover(bool on)
  3872. {
  3873. if (on && hero)
  3874. GH.statusbar->print(hero->hoverName);
  3875. else
  3876. GH.statusbar->clear();
  3877. }
  3878. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  3879. {
  3880. if((!down) && previousState && town)
  3881. {
  3882. LOCPLINT->openTownWindow(town);
  3883. if ( type == 2 )
  3884. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::VILLAGE_HALL);
  3885. else if ( type == 3 && town->fortLevel() )
  3886. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::FORT);
  3887. }
  3888. }
  3889. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  3890. {
  3891. if((!down) && previousState && town)
  3892. LOCPLINT->openTownWindow(town);//TODO: popup?
  3893. }
  3894. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  3895. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  3896. {
  3897. }
  3898. void CArtifactsOfHero::SCommonPart::reset()
  3899. {
  3900. src.clear();
  3901. dst.clear();
  3902. CCS->curh->dragAndDropCursor(nullptr);
  3903. }
  3904. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  3905. {
  3906. // // An update is made, rather than initialization.
  3907. // if (curHero && curHero->id == hero->id)
  3908. // {
  3909. // if(curHero != hero)
  3910. // {
  3911. // //delete curHero;
  3912. // curHero = hero; //was: creating a copy
  3913. // }
  3914. //
  3915. // // Compensate backpack pos if an artifact was insertad before it.
  3916. // if (commonInfo->dst.slotID >= 19 && commonInfo->destAOH == this
  3917. // && commonInfo->dst.slotID - 19 < backpackPos)
  3918. // {
  3919. // backpackPos++;
  3920. // }
  3921. //
  3922. // if (updateState && commonInfo->srcAOH == this)
  3923. // {
  3924. // // A swap was made, make the replaced artifact the current selected.
  3925. // if (commonInfo->dst.slotID < 19 && commonInfo->destArtifact)
  3926. // {
  3927. // // // Temporarily remove artifact from hero.
  3928. // // if (commonInfo->srcSlotID < 19)
  3929. // // CGI->arth->unequipArtifact(curHero->artifWorn, commonInfo->srcSlotID);
  3930. // // else
  3931. // // curHero->artifacts.erase(curHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  3932. //
  3933. // updateParentWindow(); //TODO: evil! but does the thing
  3934. //
  3935. // // Source <- Dest
  3936. // commonInfo->srcArtifact = commonInfo->destArtifact;
  3937. //
  3938. // // Reset destination parameters.
  3939. // commonInfo->dst.clear();
  3940. //
  3941. // CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  3942. // markPossibleSlots(commonInfo->srcArtifact);
  3943. // }
  3944. // else if (commonInfo->destAOH != nullptr)
  3945. // {
  3946. // // Reset all parameters.
  3947. // commonInfo->reset();
  3948. // unmarkSlots();
  3949. // }
  3950. // }
  3951. // }
  3952. // else
  3953. // {
  3954. // commonInfo->reset();
  3955. // }
  3956. //
  3957. // if(hero != curHero)
  3958. // {
  3959. // // delete curHero;
  3960. // // curHero = new CGHeroInstance(*hero);
  3961. // curHero = hero; //was: creating a copy
  3962. // }
  3963. curHero = hero;
  3964. if (curHero->artifactsInBackpack.size() > 0)
  3965. backpackPos %= curHero->artifactsInBackpack.size();
  3966. else
  3967. backpackPos = 0;
  3968. // Fill the slots for worn artifacts and backpack.
  3969. for (int g = 0; g < artWorn.size() ; g++)
  3970. setSlotData(artWorn[g], ArtifactPosition(g));
  3971. scrollBackpack(0);
  3972. }
  3973. void CArtifactsOfHero::dispose()
  3974. {
  3975. //vstd::clear_pointer(curHero);
  3976. //unmarkSlots(false);
  3977. CCS->curh->dragAndDropCursor(nullptr);
  3978. }
  3979. void CArtifactsOfHero::scrollBackpack(int dir)
  3980. {
  3981. int artsInBackpack = curHero->artifactsInBackpack.size();
  3982. backpackPos += dir;
  3983. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  3984. backpackPos += artsInBackpack;
  3985. if(artsInBackpack)
  3986. backpackPos %= artsInBackpack;
  3987. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  3988. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  3989. toOmit.insert(commonInfo->src.art);
  3990. int omitedSoFar = 0;
  3991. //set new data
  3992. size_t s = 0;
  3993. for( ; s < artsInBackpack; ++s)
  3994. {
  3995. if (s < artsInBackpack)
  3996. {
  3997. auto slotID = ArtifactPosition(GameConstants::BACKPACK_START + (s + backpackPos)%artsInBackpack);
  3998. const CArtifactInstance *art = curHero->getArt(slotID);
  3999. assert(art);
  4000. if(!vstd::contains(toOmit, art))
  4001. {
  4002. if(s - omitedSoFar < backpack.size())
  4003. setSlotData(backpack[s-omitedSoFar], slotID);
  4004. }
  4005. else
  4006. {
  4007. toOmit -= art;
  4008. omitedSoFar++;
  4009. continue;
  4010. }
  4011. }
  4012. }
  4013. for( ; s - omitedSoFar < backpack.size(); s++)
  4014. eraseSlotData(backpack[s-omitedSoFar], ArtifactPosition(GameConstants::BACKPACK_START + s));
  4015. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  4016. if(highlightModeCallback)
  4017. {
  4018. for(auto & elem : backpack)
  4019. {
  4020. if(elem->marked)
  4021. {
  4022. highlightModeCallback(elem);
  4023. break;
  4024. }
  4025. }
  4026. }
  4027. //blocking scrolling if there is not enough artifacts to scroll
  4028. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  4029. leftArtRoll->block(!scrollingPossible);
  4030. rightArtRoll->block(!scrollingPossible);
  4031. safeRedraw();
  4032. }
  4033. /**
  4034. * Marks possible slots where a given artifact can be placed, except backpack.
  4035. *
  4036. * @param art Artifact checked against.
  4037. */
  4038. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art)
  4039. {
  4040. for(CArtifactsOfHero *aoh : commonInfo->participants)
  4041. for(CArtPlace *place : aoh->artWorn)
  4042. place->selectSlot(art->canBePutAt(ArtifactLocation(aoh->curHero, place->slotID), true));
  4043. safeRedraw();
  4044. }
  4045. /**
  4046. * Unamarks all slots.
  4047. */
  4048. void CArtifactsOfHero::unmarkSlots(bool withRedraw /*= true*/)
  4049. {
  4050. if(commonInfo)
  4051. for(CArtifactsOfHero *aoh : commonInfo->participants)
  4052. aoh->unmarkLocalSlots(false);
  4053. else
  4054. unmarkLocalSlots(false);\
  4055. if(withRedraw)
  4056. safeRedraw();
  4057. }
  4058. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw /*= true*/)
  4059. {
  4060. for(CArtPlace *place : artWorn)
  4061. place->selectSlot(false);
  4062. for(CArtPlace *place : backpack)
  4063. place->selectSlot(false);
  4064. if(withRedraw)
  4065. safeRedraw();
  4066. }
  4067. /**
  4068. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  4069. */
  4070. void CArtifactsOfHero::setSlotData(CArtPlace* artPlace, ArtifactPosition slotID)
  4071. {
  4072. if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  4073. {
  4074. return;
  4075. }
  4076. artPlace->pickSlot(false);
  4077. artPlace->slotID = slotID;
  4078. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  4079. {
  4080. artPlace->setArtifact(asi->artifact);
  4081. artPlace->lockSlot(asi->locked);
  4082. }
  4083. else
  4084. artPlace->setArtifact(nullptr);
  4085. }
  4086. /**
  4087. * Makes given artifact slot appear as empty with a certain slot ID.
  4088. */
  4089. void CArtifactsOfHero::eraseSlotData (CArtPlace* artPlace, ArtifactPosition slotID)
  4090. {
  4091. artPlace->pickSlot(false);
  4092. artPlace->slotID = slotID;
  4093. artPlace->setArtifact(nullptr);
  4094. }
  4095. CArtifactsOfHero::CArtifactsOfHero(std::vector<CArtPlace *> ArtWorn, std::vector<CArtPlace *> Backpack,
  4096. CAdventureMapButton *leftScroll, CAdventureMapButton *rightScroll, bool createCommonPart):
  4097. curHero(nullptr),
  4098. artWorn(ArtWorn), backpack(Backpack),
  4099. backpackPos(0), commonInfo(nullptr), updateState(false),
  4100. leftArtRoll(leftScroll), rightArtRoll(rightScroll),
  4101. allowedAssembling(true), highlightModeCallback(nullptr)
  4102. {
  4103. if(createCommonPart)
  4104. {
  4105. commonInfo = new CArtifactsOfHero::SCommonPart;
  4106. commonInfo->participants.insert(this);
  4107. }
  4108. // Init slots for worn artifacts.
  4109. for (size_t g = 0; g < artWorn.size() ; g++)
  4110. {
  4111. artWorn[g]->ourOwner = this;
  4112. eraseSlotData(artWorn[g], ArtifactPosition(g));
  4113. }
  4114. // Init slots for the backpack.
  4115. for(size_t s=0; s<backpack.size(); ++s)
  4116. {
  4117. backpack[s]->ourOwner = this;
  4118. eraseSlotData(backpack[s], ArtifactPosition(GameConstants::BACKPACK_START + s));
  4119. }
  4120. leftArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1);
  4121. rightArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1);
  4122. }
  4123. CArtifactsOfHero::CArtifactsOfHero(const Point& position, bool createCommonPart /*= false*/)
  4124. : curHero(nullptr), backpackPos(0), commonInfo(nullptr), updateState(false), allowedAssembling(true), highlightModeCallback(nullptr)
  4125. {
  4126. if(createCommonPart)
  4127. {
  4128. commonInfo = new CArtifactsOfHero::SCommonPart;
  4129. commonInfo->participants.insert(this);
  4130. }
  4131. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4132. pos += position;
  4133. artWorn.resize(19);
  4134. std::vector<Point> slotPos;
  4135. slotPos += Point(509,30), Point(567,240), Point(509,80),
  4136. Point(383,68), Point(564,183), Point(509,130),
  4137. Point(431,68), Point(610,183), Point(515,295),
  4138. Point(383,143), Point(399,194), Point(415,245),
  4139. Point(431,296), Point(564,30), Point(610,30),
  4140. Point(610,76), Point(610,122), Point(610,310),
  4141. Point(381,296);
  4142. // Create slots for worn artifacts.
  4143. for (size_t g = 0; g < GameConstants::BACKPACK_START ; g++)
  4144. {
  4145. artWorn[g] = new CArtPlace(slotPos[g]);
  4146. artWorn[g]->ourOwner = this;
  4147. eraseSlotData(artWorn[g], ArtifactPosition(g));
  4148. }
  4149. // Create slots for the backpack.
  4150. for(size_t s=0; s<5; ++s)
  4151. {
  4152. auto add = new CArtPlace(Point(403 + 46 * s, 365));
  4153. add->ourOwner = this;
  4154. eraseSlotData(add, ArtifactPosition(GameConstants::BACKPACK_START + s));
  4155. backpack.push_back(add);
  4156. }
  4157. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), 379, 364, "hsbtns3.def", SDLK_LEFT);
  4158. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), 632, 364, "hsbtns5.def", SDLK_RIGHT);
  4159. }
  4160. CArtifactsOfHero::~CArtifactsOfHero()
  4161. {
  4162. dispose();
  4163. }
  4164. void CArtifactsOfHero::updateParentWindow()
  4165. {
  4166. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt()))
  4167. {
  4168. if(updateState)
  4169. chw->curHero = curHero;
  4170. else
  4171. chw->update(curHero, true);
  4172. }
  4173. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt()))
  4174. {
  4175. //use our copy of hero to draw window
  4176. if(cew->heroInst[0]->id == curHero->id)
  4177. cew->heroInst[0] = curHero;
  4178. else
  4179. cew->heroInst[1] = curHero;
  4180. if(!updateState)
  4181. {
  4182. cew->deactivate();
  4183. // for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  4184. // {
  4185. // if(cew->heroInst[g] == curHero)
  4186. // {
  4187. // cew->artifs[g]->setHero(curHero);
  4188. // }
  4189. // }
  4190. cew->prepareBackground();
  4191. cew->redraw();
  4192. cew->activate();
  4193. }
  4194. }
  4195. }
  4196. void CArtifactsOfHero::safeRedraw()
  4197. {
  4198. if (active)
  4199. {
  4200. if(parent)
  4201. parent->redraw();
  4202. else
  4203. redraw();
  4204. }
  4205. }
  4206. void CArtifactsOfHero::realizeCurrentTransaction()
  4207. {
  4208. assert(commonInfo->src.AOH);
  4209. assert(commonInfo->dst.AOH);
  4210. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  4211. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  4212. }
  4213. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  4214. {
  4215. bool isCurHeroSrc = src.isHolder(curHero),
  4216. isCurHeroDst = dst.isHolder(curHero);
  4217. if(isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  4218. updateSlot(src.slot);
  4219. if(isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START)
  4220. updateSlot(dst.slot);
  4221. if(isCurHeroSrc || isCurHeroDst) //we need to update all slots, artifact might be combined and affect more slots
  4222. updateWornSlots(false);
  4223. if (!src.isHolder(curHero) && !isCurHeroDst)
  4224. return;
  4225. if(commonInfo->src == src) //artifact was taken from us
  4226. {
  4227. assert(commonInfo->dst == dst //expected movement from slot ot slot
  4228. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START //artifact moved back to backpack (eg. to make place for art we are moving)
  4229. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  4230. commonInfo->reset();
  4231. unmarkSlots();
  4232. }
  4233. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  4234. {
  4235. assert(dst.slot >= GameConstants::BACKPACK_START);
  4236. commonInfo->reset();
  4237. CArtPlace *ap = nullptr;
  4238. for(CArtifactsOfHero *aoh : commonInfo->participants)
  4239. {
  4240. if(dst.isHolder(aoh->curHero))
  4241. {
  4242. commonInfo->src.AOH = aoh;
  4243. if((ap = aoh->getArtPlace(dst.slot)))
  4244. break;
  4245. }
  4246. }
  4247. if(ap)
  4248. {
  4249. ap->select();
  4250. }
  4251. else
  4252. {
  4253. commonInfo->src.art = dst.getArt();
  4254. commonInfo->src.slotID = dst.slot;
  4255. assert(commonInfo->src.AOH);
  4256. CCS->curh->dragAndDropCursor(new CAnimImage("artifact", dst.getArt()->artType->iconIndex));
  4257. markPossibleSlots(dst.getArt());
  4258. }
  4259. }
  4260. else if(src.slot >= GameConstants::BACKPACK_START &&
  4261. src.slot < commonInfo->src.slotID &&
  4262. src.isHolder(commonInfo->src.AOH->curHero)) //artifact taken from before currently picked one
  4263. {
  4264. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  4265. vstd::advance(commonInfo->src.slotID, -1);
  4266. assert(commonInfo->src.valid());
  4267. }
  4268. else
  4269. {
  4270. //when moving one artifact onto another it leads to two art movements: dst->backapck; src->dst
  4271. // however after first movement we pick the art from backpack and the second movement coming when
  4272. // we have a different artifact may look surprising... but it's valid.
  4273. }
  4274. updateParentWindow();
  4275. int shift = 0;
  4276. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4277. // shift++;
  4278. //
  4279. if(src.slot < GameConstants::BACKPACK_START && dst.slot - GameConstants::BACKPACK_START < backpackPos)
  4280. shift++;
  4281. if(dst.slot < GameConstants::BACKPACK_START && src.slot - GameConstants::BACKPACK_START < backpackPos)
  4282. shift--;
  4283. if( (isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  4284. || (isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START) )
  4285. scrollBackpack(shift); //update backpack slots
  4286. }
  4287. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  4288. {
  4289. if(al.isHolder(curHero))
  4290. {
  4291. if(al.slot < GameConstants::BACKPACK_START)
  4292. updateWornSlots(0);
  4293. else
  4294. scrollBackpack(0); //update backpack slots
  4295. }
  4296. }
  4297. CArtPlace * CArtifactsOfHero::getArtPlace(int slot)
  4298. {
  4299. if(slot < GameConstants::BACKPACK_START)
  4300. {
  4301. return artWorn[slot];
  4302. }
  4303. else
  4304. {
  4305. for(CArtPlace *ap : backpack)
  4306. if(ap->slotID == slot)
  4307. return ap;
  4308. }
  4309. return nullptr;
  4310. }
  4311. void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al)
  4312. {
  4313. if(al.isHolder(curHero))
  4314. updateWornSlots();
  4315. }
  4316. void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al)
  4317. {
  4318. if(al.isHolder(curHero))
  4319. updateWornSlots();
  4320. }
  4321. void CArtifactsOfHero::updateWornSlots(bool redrawParent /*= true*/)
  4322. {
  4323. for(int i = 0; i < artWorn.size(); i++)
  4324. updateSlot(ArtifactPosition(i));
  4325. if(redrawParent)
  4326. updateParentWindow();
  4327. }
  4328. const CGHeroInstance * CArtifactsOfHero::getHero() const
  4329. {
  4330. return curHero;
  4331. }
  4332. void CArtifactsOfHero::updateSlot(ArtifactPosition slotID)
  4333. {
  4334. setSlotData(getArtPlace(slotID), slotID);
  4335. }
  4336. void CExchangeWindow::questlog(int whichHero)
  4337. {
  4338. CCS->curh->dragAndDropCursor(nullptr);
  4339. }
  4340. void CExchangeWindow::prepareBackground()
  4341. {
  4342. //printing heroes' names and levels
  4343. auto genTitle = [](const CGHeroInstance *h)
  4344. {
  4345. return boost::str(boost::format(CGI->generaltexth->allTexts[138])
  4346. % h->name % h->level % h->type->heroClass->name);
  4347. };
  4348. new CLabel(147, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[0]));
  4349. new CLabel(653, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[1]));
  4350. //printing primary skills
  4351. for(int g=0; g<4; ++g)
  4352. new CAnimImage("PSKIL32", g, 0, 385, 19 + 36*g);
  4353. //heroes related thing
  4354. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4355. {
  4356. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  4357. //printing primary skills' amounts
  4358. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  4359. new CLabel(352 + 93 * b, 35 + 36 * m, FONT_SMALL, CENTER, Colors::WHITE,
  4360. boost::lexical_cast<std::string>(heroWArt.getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(m))));
  4361. //printing secondary skills
  4362. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  4363. {
  4364. int id = heroInst[b]->secSkills[m].first;
  4365. int level = heroInst[b]->secSkills[m].second;
  4366. new CAnimImage("SECSK32", id*3 + level + 2 , 0, 32 + 36 * m + 454 * b, 88);
  4367. }
  4368. //hero's specialty
  4369. new CAnimImage("UN32", heroInst[b]->type->imageIndex, 0, 67 + 490*b, 45);
  4370. //experience
  4371. new CAnimImage("PSKIL32", 4, 0, 103 + 490*b, 45);
  4372. new CLabel(119 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->exp));
  4373. //mana points
  4374. new CAnimImage("PSKIL32", 5, 0, 139 + 490*b, 45);
  4375. new CLabel(155 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->mana));
  4376. }
  4377. //printing portraits
  4378. new CAnimImage("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
  4379. new CAnimImage("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
  4380. }
  4381. CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID):
  4382. CWindowObject(PLAYER_COLORED | BORDERED, "TRADE2")
  4383. {
  4384. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4385. char bufor[400];
  4386. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  4387. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  4388. prepareBackground();
  4389. artifs[0] = new CArtifactsOfHero(Point(-334, 150));
  4390. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  4391. artifs[0]->commonInfo->participants.insert(artifs[0]);
  4392. artifs[0]->setHero(heroInst[0]);
  4393. artifs[1] = new CArtifactsOfHero(Point(96, 150));
  4394. artifs[1]->commonInfo = artifs[0]->commonInfo;
  4395. artifs[1]->commonInfo->participants.insert(artifs[1]);
  4396. artifs[1]->setHero(heroInst[1]);
  4397. artSets.push_back(artifs[0]);
  4398. artSets.push_back(artifs[1]);
  4399. //primary skills
  4400. for(int g=0; g<4; ++g)
  4401. {
  4402. //primary skill's clickable areas
  4403. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  4404. primSkillAreas[g]->pos = genRect(32, 140, pos.x + 329, pos.y + 19 + 36 * g);
  4405. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  4406. primSkillAreas[g]->type = g;
  4407. primSkillAreas[g]->bonusValue = -1;
  4408. primSkillAreas[g]->baseType = 0;
  4409. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[g].c_str());
  4410. primSkillAreas[g]->hoverText = std::string(bufor);
  4411. }
  4412. //heroes related thing
  4413. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4414. {
  4415. //secondary skill's clickable areas
  4416. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  4417. {
  4418. int skill = heroInst[b]->secSkills[g].first,
  4419. level = heroInst[b]->secSkills[g].second; // <1, 3>
  4420. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  4421. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  4422. secSkillAreas[b][g]->baseType = 1;
  4423. secSkillAreas[b][g]->type = skill;
  4424. secSkillAreas[b][g]->bonusValue = level;
  4425. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  4426. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level - 1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  4427. secSkillAreas[b][g]->hoverText = std::string(bufor);
  4428. }
  4429. portrait[b] = new CHeroArea(257 + 228*b, 13, heroInst[b]);
  4430. specialty[b] = new LRClickableAreaWText();
  4431. specialty[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  4432. specialty[b]->hoverText = CGI->generaltexth->heroscrn[27];
  4433. specialty[b]->text = heroInst[b]->type->specDescr;
  4434. experience[b] = new LRClickableAreaWText();
  4435. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  4436. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  4437. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  4438. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  4439. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  4440. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  4441. spellPoints[b] = new LRClickableAreaWText();
  4442. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  4443. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  4444. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), heroInst[b]->name.c_str(), heroInst[b]->mana, heroInst[b]->manaLimit());
  4445. spellPoints[b]->text = std::string(bufor);
  4446. //setting morale
  4447. morale[b] = new MoraleLuckBox(true, genRect(32, 32, 176 + 490*b, 39), true);
  4448. morale[b]->set(heroInst[b]);
  4449. //setting luck
  4450. luck[b] = new MoraleLuckBox(false, genRect(32, 32, 212 + 490*b, 39), true);
  4451. luck[b]->set(heroInst[b]);
  4452. }
  4453. //buttons
  4454. quit = new CAdventureMapButton(CGI->generaltexth->zelp[600], boost::bind(&CExchangeWindow::close, this), 732, 567, "IOKAY.DEF", SDLK_RETURN);
  4455. if(queryID.getNum() > 0)
  4456. quit->callback += [=]{ LOCPLINT->cb->selectionMade(0, queryID); };
  4457. questlogButton[0] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 0), 10, 44, "hsbtns4.def");
  4458. questlogButton[1] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 1), 740, 44, "hsbtns4.def");
  4459. Rect barRect(5, 578, 725, 18);
  4460. ourBar = new CGStatusBar(new CPicture(*background, barRect, 5, 578, false));
  4461. //garrison interface
  4462. garr = new CGarrisonInt(69, 131, 4, Point(418,0), *background, Point(69,131), heroInst[0],heroInst[1], true, true);
  4463. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 10, 132, "TSBTNS.DEF"));
  4464. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 740, 132, "TSBTNS.DEF"));
  4465. }
  4466. CExchangeWindow::~CExchangeWindow() //d-tor
  4467. {
  4468. delete artifs[0]->commonInfo;
  4469. artifs[0]->commonInfo = nullptr;
  4470. artifs[1]->commonInfo = nullptr;
  4471. }
  4472. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const std::function<void()> &onBuy):
  4473. CWindowObject(PLAYER_COLORED, "TPSHIP")
  4474. {
  4475. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4476. bgWater = new CPicture("TPSHIPBK", 100, 69);
  4477. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  4478. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  4479. bgShip = new CAnimImage(boatFilenames[boatType], 0, 7, 120, 96, CShowableAnim::USE_RLE);
  4480. bgShip->center(waterCenter);
  4481. // Create resource icons and costs.
  4482. std::string goldValue = boost::lexical_cast<std::string>(cost[Res::GOLD]);
  4483. std::string woodValue = boost::lexical_cast<std::string>(cost[Res::WOOD]);
  4484. goldCost = new CLabel(118, 294, FONT_SMALL, CENTER, Colors::WHITE, goldValue);
  4485. woodCost = new CLabel(212, 294, FONT_SMALL, CENTER, Colors::WHITE, woodValue);
  4486. goldPic = new CAnimImage("RESOURCE", Res::GOLD, 0, 100, 244);
  4487. woodPic = new CAnimImage("RESOURCE", Res::WOOD, 0, 196, 244);
  4488. quit = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CShipyardWindow::close, this), 224, 312, "ICANCEL", SDLK_RETURN);
  4489. build = new CAdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CShipyardWindow::close, this), 42, 312, "IBUY30", SDLK_RETURN);
  4490. build->callback += onBuy;
  4491. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, 1))
  4492. {
  4493. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  4494. {
  4495. build->block(true);
  4496. break;
  4497. }
  4498. }
  4499. statusBar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4500. title = new CLabel(164, 27, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  4501. costLabel = new CLabel(164, 220, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  4502. }
  4503. CPuzzleWindow::CPuzzleWindow(const int3 &GrailPos, double discoveredRatio):
  4504. CWindowObject(PLAYER_COLORED | BORDERED, "PUZZLE"),
  4505. grailPos(GrailPos),
  4506. currentAlpha(SDL_ALPHA_OPAQUE)
  4507. {
  4508. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4509. CCS->soundh->playSound(soundBase::OBELISK);
  4510. quitb = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CPuzzleWindow::close, this), 670, 538, "IOK6432.DEF", SDLK_RETURN);
  4511. quitb->assignedKeys.insert(SDLK_ESCAPE);
  4512. quitb->borderColor = Colors::METALLIC_GOLD;
  4513. quitb->borderEnabled = true;
  4514. new CPicture("PUZZLOGO", 607, 3);
  4515. new CLabel(700, 95, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[463]);
  4516. new CResDataBar("ARESBAR.bmp", 3, 575, 32, 2, 85, 85);
  4517. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  4518. auto & puzzleMap = CGI->townh->factions[faction]->puzzleMap;
  4519. for(auto & elem : puzzleMap)
  4520. {
  4521. const SPuzzleInfo & info = elem;
  4522. auto piece = new CPicture(info.filename, info.x, info.y);
  4523. //piece that will slowly disappear
  4524. if(info.whenUncovered <= GameConstants::PUZZLE_MAP_PIECES * discoveredRatio)
  4525. {
  4526. piecesToRemove.push_back(piece);
  4527. piece->needRefresh = true;
  4528. piece->recActions = piece->recActions & ~SHOWALL;
  4529. }
  4530. }
  4531. }
  4532. void CPuzzleWindow::showAll(SDL_Surface * to)
  4533. {
  4534. int3 moveInt = int3(8, 9, 0);
  4535. Rect mapRect = genRect(544, 591, pos.x + 8, pos.y + 7);
  4536. CGI->mh->terrainRect
  4537. (grailPos - moveInt, adventureInt->anim,
  4538. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  4539. to, &mapRect, 0, 0, true, moveInt);
  4540. CWindowObject::showAll(to);
  4541. }
  4542. void CPuzzleWindow::show(SDL_Surface * to)
  4543. {
  4544. static int animSpeed = 2;
  4545. if (currentAlpha < animSpeed)
  4546. {
  4547. //animation done
  4548. for(auto & piece : piecesToRemove)
  4549. delete piece;
  4550. piecesToRemove.clear();
  4551. }
  4552. else
  4553. {
  4554. //update disappearing puzzles
  4555. for(auto & piece : piecesToRemove)
  4556. piece->setAlpha(currentAlpha);
  4557. currentAlpha -= animSpeed;
  4558. }
  4559. CWindowObject::show(to);
  4560. }
  4561. void CTransformerWindow::CItem::move()
  4562. {
  4563. if (left)
  4564. moveBy(Point(289, 0));
  4565. else
  4566. moveBy(Point(-289, 0));
  4567. left = !left;
  4568. }
  4569. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  4570. {
  4571. if(previousState && (!down))
  4572. {
  4573. move();
  4574. parent->showAll(screen2);
  4575. }
  4576. }
  4577. void CTransformerWindow::CItem::update()
  4578. {
  4579. icon->setFrame(parent->army->getCreature(SlotID(id))->idNumber + 2);
  4580. }
  4581. CTransformerWindow::CItem::CItem(CTransformerWindow * parent, int size, int id):
  4582. id(id), size(size), parent(parent)
  4583. {
  4584. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4585. addUsedEvents(LCLICK);
  4586. left = true;
  4587. pos.w = 58;
  4588. pos.h = 64;
  4589. pos.x += 45 + (id%3)*83 + id/6*83;
  4590. pos.y += 109 + (id/3)*98;
  4591. icon = new CAnimImage("TWCRPORT", parent->army->getCreature(SlotID(id))->idNumber + 2);
  4592. new CLabel(28, 76,FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(size));//stack size
  4593. }
  4594. void CTransformerWindow::makeDeal()
  4595. {
  4596. for (auto & elem : items)
  4597. if (!elem->left)
  4598. LOCPLINT->cb->trade(town, EMarketMode::CREATURE_UNDEAD, elem->id, 0, 0, hero);
  4599. }
  4600. void CTransformerWindow::addAll()
  4601. {
  4602. for (auto & elem : items)
  4603. if (elem->left)
  4604. elem->move();
  4605. showAll(screen2);
  4606. }
  4607. void CTransformerWindow::updateGarrisons()
  4608. {
  4609. for(auto & item : items)
  4610. {
  4611. item->update();
  4612. }
  4613. }
  4614. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):
  4615. CWindowObject(PLAYER_COLORED, "SKTRNBK"),
  4616. hero(_hero),
  4617. town(_town)
  4618. {
  4619. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4620. if (hero)
  4621. army = hero;
  4622. else
  4623. army = town;
  4624. for (int i=0; i<GameConstants::ARMY_SIZE; i++ )
  4625. if ( army->getCreature(SlotID(i)) )
  4626. items.push_back(new CItem(this, army->getStackCount(SlotID(i)), i));
  4627. all = new CAdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  4628. convert= new CAdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  4629. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CTransformerWindow::close, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  4630. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4631. new CLabel(153, 29,FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  4632. new CLabel(153+295, 29, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  4633. new CTextBox(CGI->generaltexth->allTexts[487], Rect(26, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//move creatures to create skeletons
  4634. new CTextBox(CGI->generaltexth->allTexts[488], Rect(320, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//creatures here will become skeletons
  4635. }
  4636. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  4637. {
  4638. if(previousState && (!down))
  4639. {
  4640. if ( state() != 2 )
  4641. return;
  4642. auto win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(Res::GOLD) >= 2000);
  4643. GH.pushInt(win);
  4644. }
  4645. }
  4646. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  4647. {
  4648. if(down)
  4649. {
  4650. CRClickPopup::createAndPush(CGI->generaltexth->skillInfoTexts[ID][0],
  4651. new CComponent(CComponent::secskill, ID, 1));
  4652. }
  4653. }
  4654. void CUniversityWindow::CItem::hover(bool on)
  4655. {
  4656. if (on)
  4657. GH.statusbar->print(CGI->generaltexth->skillName[ID]);
  4658. else
  4659. GH.statusbar->clear();
  4660. }
  4661. int CUniversityWindow::CItem::state()
  4662. {
  4663. if (parent->hero->getSecSkillLevel(SecondarySkill(ID)))//hero know this skill
  4664. return 1;
  4665. if (!parent->hero->canLearnSkill())//can't learn more skills
  4666. return 0;
  4667. if (parent->hero->type->heroClass->secSkillProbability[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  4668. return 0;
  4669. return 2;
  4670. }
  4671. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  4672. {
  4673. CPicture * bar;
  4674. switch (state())
  4675. {
  4676. case 0: bar = parent->red;
  4677. break;
  4678. case 1: bar = parent->yellow;
  4679. break;
  4680. case 2: bar = parent->green;
  4681. break;
  4682. default:bar = nullptr;
  4683. break;
  4684. }
  4685. assert(bar);
  4686. blitAtLoc(bar->bg, -28, -22, to);
  4687. blitAtLoc(bar->bg, -28, 48, to);
  4688. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, Colors::WHITE,to);//Name
  4689. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, Colors::WHITE,to);//Level(always basic)
  4690. CAnimImage::showAll(to);
  4691. }
  4692. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  4693. CAnimImage ("SECSKILL", _ID*3+3, 0, X, Y),
  4694. ID(_ID),
  4695. parent(_parent)
  4696. {
  4697. addUsedEvents(LCLICK | RCLICK | HOVER);
  4698. }
  4699. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):
  4700. CWindowObject(PLAYER_COLORED, "UNIVERS1"),
  4701. hero(_hero),
  4702. market(_market)
  4703. {
  4704. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4705. green = new CPicture("UNIVGREN.PCX");
  4706. yellow = new CPicture("UNIVGOLD.PCX");//bars
  4707. red = new CPicture("UNIVRED.PCX");
  4708. green->recActions =
  4709. yellow->recActions =
  4710. red->recActions = DISPOSE;
  4711. CIntObject * titlePic = nullptr;
  4712. if (market->o->ID == Obj::TOWN)
  4713. titlePic = new CAnimImage(CGI->townh->factions[ETownType::CONFLUX]->town->clientInfo.buildingsIcons, BuildingID::MAGIC_UNIVERSITY);
  4714. else
  4715. titlePic = new CPicture("UNIVBLDG");
  4716. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  4717. //Clerk speech
  4718. new CTextBox(CGI->generaltexth->allTexts[603], Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);
  4719. //University
  4720. new CLabel(231, 26, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[602]);
  4721. std::vector<int> list = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  4722. assert(list.size() == 4);
  4723. for (int i=0; i<list.size(); i++)//prepare clickable items
  4724. items.push_back(new CItem(this, list[i], 54+i*104, 234));
  4725. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[632],
  4726. boost::bind(&CUniversityWindow::close, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  4727. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4728. }
  4729. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):
  4730. CWindowObject(PLAYER_COLORED, "UNIVERS2.PCX"),
  4731. parent(PARENT)
  4732. {
  4733. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4734. std::string text = CGI->generaltexth->allTexts[608];
  4735. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  4736. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  4737. boost::replace_first(text, "%d", "2000");
  4738. new CTextBox(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);//Clerk speech
  4739. new CLabel(230, 37, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth-> skillName[SKILL]);//Skill name
  4740. new CAnimImage("SECSKILL", SKILL*3+3, 0, 211, 51);//skill
  4741. new CLabel(230, 107, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->levels[1]);//Skill level
  4742. new CAnimImage("RESOURCE", Res::GOLD, 0, 210, 210);//gold
  4743. new CLabel(230, 267, FONT_SMALL, CENTER, Colors::WHITE, "2000");//Cost
  4744. std::string hoverText = CGI->generaltexth->allTexts[609];
  4745. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  4746. text = CGI->generaltexth->zelp[633].second;
  4747. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  4748. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  4749. boost::replace_first(text, "%d", "2000");
  4750. confirm= new CAdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  4751. 148,299,"IBY6432.DEF",SDLK_RETURN);
  4752. confirm->block(!available);
  4753. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CUnivConfirmWindow::close, this),
  4754. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  4755. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4756. }
  4757. void CUnivConfirmWindow::makeDeal(int skill)
  4758. {
  4759. LOCPLINT->cb->trade(parent->market->o, EMarketMode::RESOURCE_SKILL, 6, skill, 1, parent->hero);
  4760. close();
  4761. }
  4762. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  4763. CWindowObject(PLAYER_COLORED, "APHLFTBK"),
  4764. fort(object),
  4765. hero(visitor)
  4766. {
  4767. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4768. slotsCount=7;
  4769. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  4770. new CLabel(325, 32, FONT_BIG, CENTER, Colors::YELLOW, fort->hoverName);//Hill Fort
  4771. heroPic = new CHeroArea(30, 60, hero);
  4772. currState.resize(slotsCount+1);
  4773. costs.resize(slotsCount);
  4774. totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
  4775. std::vector<std::string> files;
  4776. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  4777. for (int i=0; i<slotsCount; i++)
  4778. {
  4779. currState[i] = getState(SlotID(i));
  4780. upgrade[i] = new CAdventureMapButton(getTextForSlot(SlotID(i)),"",boost::bind(&CHillFortWindow::makeDeal, this, SlotID(i)),
  4781. 107+i*76, 171, "", SDLK_1+i, &files);
  4782. upgrade[i]->block(currState[i] == -1);
  4783. }
  4784. files.clear();
  4785. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  4786. currState[slotsCount] = getState(SlotID(slotsCount));
  4787. upgradeAll = new CAdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, SlotID(slotsCount)),
  4788. 30, 231, "", SDLK_0, &files);
  4789. quit = new CAdventureMapButton("","",boost::bind(&CHillFortWindow::close, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  4790. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4791. garr = new CGarrisonInt(108, 60, 18, Point(),background->bg,Point(108,60),hero,nullptr);
  4792. updateGarrisons();
  4793. }
  4794. void CHillFortWindow::updateGarrisons()
  4795. {
  4796. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  4797. totalSumm[i]=0;
  4798. for (int i=0; i<slotsCount; i++)
  4799. {
  4800. costs[i].clear();
  4801. int newState = getState(SlotID(i));
  4802. if (newState != -1)
  4803. {
  4804. UpgradeInfo info;
  4805. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  4806. if (info.newID.size())//we have upgrades here - update costs
  4807. {
  4808. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  4809. totalSumm += costs[i];
  4810. }
  4811. }
  4812. currState[i] = newState;
  4813. upgrade[i]->setIndex(newState);
  4814. upgrade[i]->block(currState[i] == -1);
  4815. upgrade[i]->hoverTexts[0] = getTextForSlot(SlotID(i));
  4816. }
  4817. int newState = getState(SlotID(slotsCount));
  4818. currState[slotsCount] = newState;
  4819. upgradeAll->setIndex(newState);
  4820. garr->recreateSlots();
  4821. }
  4822. void CHillFortWindow::makeDeal(SlotID slot)
  4823. {
  4824. assert(slot.getNum()>=0);
  4825. int offset = (slot.getNum() == slotsCount)?2:0;
  4826. switch (currState[slot.getNum()])
  4827. {
  4828. case 0:
  4829. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  4830. std::vector<CComponent*>(), soundBase::sound_todo);
  4831. break;
  4832. case 1:
  4833. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  4834. std::vector<CComponent*>(), soundBase::sound_todo);
  4835. break;
  4836. case 2:
  4837. for (int i=0; i<slotsCount; i++)
  4838. if ( slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  4839. {
  4840. UpgradeInfo info;
  4841. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  4842. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  4843. }
  4844. break;
  4845. }
  4846. }
  4847. void CHillFortWindow::showAll (SDL_Surface *to)
  4848. {
  4849. CWindowObject::showAll(to);
  4850. for ( int i=0; i<slotsCount; i++)
  4851. {
  4852. if ( currState[i] == 0 || currState[i] == 2 )
  4853. {
  4854. if ( costs[i].size() )//we have several elements
  4855. {
  4856. int curY = 128;//reverse iterator is used to display gold as first element
  4857. for(int j = costs[i].size()-1; j >= 0; j--)
  4858. {
  4859. int val = costs[i][j];
  4860. if(!val) continue;
  4861. blitAtLoc(resources->ourImages[j].bitmap, 104+76*i, curY, to);
  4862. printToLoc(boost::lexical_cast<std::string>(val), 168+76*i, curY+16, FONT_SMALL, Colors::WHITE, to);
  4863. curY += 20;
  4864. }
  4865. }
  4866. else//free upgrade - print gold image and "Free" text
  4867. {
  4868. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  4869. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, Colors::WHITE, to);
  4870. }
  4871. }
  4872. }
  4873. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  4874. {
  4875. if (totalSumm[i])//this resource is used - display it
  4876. {
  4877. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  4878. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, Colors::WHITE, to);
  4879. }
  4880. }
  4881. }
  4882. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  4883. {
  4884. if ( !hero->getCreature(slot) )//we dont have creature here
  4885. return "";
  4886. std::string str = CGI->generaltexth->allTexts[318];
  4887. int amount = hero->getStackCount(slot);
  4888. if ( amount == 1 )
  4889. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  4890. else
  4891. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  4892. return str;
  4893. }
  4894. int CHillFortWindow::getState(SlotID slot)
  4895. {
  4896. TResources myRes = LOCPLINT->cb->getResourceAmount();
  4897. if ( slot.getNum() == slotsCount )//"Upgrade all" slot
  4898. {
  4899. bool allUpgraded = true;//All creatures are upgraded?
  4900. for (int i=0; i<slotsCount; i++)
  4901. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  4902. if (allUpgraded)
  4903. return 1;
  4904. if(!totalSumm.canBeAfforded(myRes))
  4905. return 0;
  4906. return 2;
  4907. }
  4908. if (hero->slotEmpty(slot))//no creature here
  4909. return -1;
  4910. UpgradeInfo info;
  4911. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  4912. if (!info.newID.size())//already upgraded
  4913. return 1;
  4914. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  4915. return 0;
  4916. return 2;//can upgrade
  4917. }
  4918. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  4919. CWindowObject(PLAYER_COLORED | BORDERED, "TpRank"),
  4920. owner(_owner)
  4921. {
  4922. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4923. type |= BLOCK_ADV_HOTKEYS;
  4924. SThievesGuildInfo tgi; //info to be displayed
  4925. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  4926. exitb = new CAdventureMapButton (CGI->generaltexth->allTexts[600], "", boost::bind(&CThievesGuildWindow::close,this), 748, 556, "TPMAGE1", SDLK_RETURN);
  4927. exitb->assignedKeys.insert(SDLK_ESCAPE);
  4928. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  4929. resdatabar = new CMinorResDataBar();
  4930. resdatabar->pos.x += pos.x;
  4931. resdatabar->pos.y += pos.y;
  4932. //data for information table:
  4933. // fields[row][column] = list of id's of players for this box
  4934. static std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
  4935. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  4936. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  4937. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  4938. //printing texts & descriptions to background
  4939. for(int g=0; g<12; ++g)
  4940. {
  4941. int posY[] = {400, 460, 510};
  4942. int y;
  4943. if(g < 9)
  4944. y = 52 + 32*g;
  4945. else
  4946. y = posY[g-9];
  4947. std::string text = CGI->generaltexth->jktexts[24+g];
  4948. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  4949. new CLabel(135, y, FONT_MEDIUM, CENTER, Colors::YELLOW, text);
  4950. }
  4951. for(int g=1; g<tgi.playerColors.size(); ++g)
  4952. new CAnimImage("PRSTRIPS", g-1, 0, 250 + 66*g, 7);
  4953. for(int g=0; g<tgi.playerColors.size(); ++g)
  4954. new CLabel(283 + 66*g, 24, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]);
  4955. //printing flags
  4956. for(int g = 0; g < ARRAY_COUNT(fields); ++g) //by lines
  4957. {
  4958. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  4959. {
  4960. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  4961. //position of box
  4962. int xpos = 259 + 66 * b;
  4963. int ypos = 41 + 32 * g;
  4964. size_t rowLength[2]; //size of each row
  4965. rowLength[0] = std::min<size_t>(players.size(), 4);
  4966. rowLength[1] = players.size() - rowLength[0];
  4967. for (size_t j=0; j< 2; j++)
  4968. {
  4969. // origin of this row | offset for 2nd row| shift right for short rows
  4970. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  4971. int rowStartX = xpos + (j ? 6 + (rowLength[j] < 3 ? 12 : 0) : 24 - 6 * rowLength[j]);
  4972. int rowStartY = ypos + (j ? 4 : 0);
  4973. for (size_t i=0; i< rowLength[j]; i++)
  4974. {
  4975. new CAnimImage("itgflags", players[i + j*4].getNum(), 0, rowStartX + i*12, rowStartY);
  4976. }
  4977. }
  4978. }
  4979. }
  4980. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  4981. //printing best hero
  4982. int counter = 0;
  4983. for(auto & iter : tgi.colorToBestHero)
  4984. {
  4985. if(iter.second.portrait >= 0)
  4986. {
  4987. new CPicture(colorToBox[iter.first.getNum()], 253 + 66 * counter, 334);
  4988. new CAnimImage("PortraitsSmall", iter.second.portrait, 0, 260 + 66 * counter, 360);
  4989. //TODO: r-click info:
  4990. // - r-click on hero
  4991. // - r-click on primary skill label
  4992. if(iter.second.details)
  4993. {
  4994. new CTextBox(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  4995. 0, FONT_TINY, TOPLEFT, Colors::WHITE);
  4996. for (int i=0; i<iter.second.details->primskills.size(); ++i)
  4997. {
  4998. new CLabel(310 + 66 * counter, 407 + 11*i, FONT_TINY, BOTTOMRIGHT, Colors::WHITE,
  4999. boost::lexical_cast<std::string>(iter.second.details->primskills[i]));
  5000. }
  5001. }
  5002. }
  5003. counter++;
  5004. }
  5005. //printing best creature
  5006. counter = 0;
  5007. for(auto & it : tgi.bestCreature)
  5008. {
  5009. if(it.second >= 0)
  5010. new CAnimImage("TWCRPORT", it.second+2, 0, 255 + 66 * counter, 479);
  5011. counter++;
  5012. }
  5013. //printing personality
  5014. counter = 0;
  5015. for(auto & it : tgi.personality)
  5016. {
  5017. std::string text;
  5018. if(it.second == EAiTactic::NONE)
  5019. {
  5020. text = CGI->generaltexth->arraytxt[172];
  5021. }
  5022. else if(it.second != EAiTactic::RANDOM)
  5023. {
  5024. text = CGI->generaltexth->arraytxt[168 + it.second];
  5025. }
  5026. new CLabel(283 + 66*counter, 459, FONT_SMALL, CENTER, Colors::WHITE, text);
  5027. counter++;
  5028. }
  5029. }
  5030. void MoraleLuckBox::set(const IBonusBearer *node)
  5031. {
  5032. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5033. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  5034. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  5035. 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.
  5036. const int componentType[] = {CComponent::luck, CComponent::morale};
  5037. const int hoverTextBase[] = {7, 4};
  5038. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  5039. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  5040. int mrlt = -9;
  5041. TModDescr mrl;
  5042. if (node)
  5043. {
  5044. node->getModifiersWDescr(mrl, bonusType[morale]);
  5045. bonusValue = (node->*getValue[morale])();
  5046. }
  5047. else
  5048. bonusValue = 0;
  5049. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  5050. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  5051. baseType = componentType[morale];
  5052. text = CGI->generaltexth->arraytxt[textId[morale]];
  5053. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  5054. if (!mrl.size())
  5055. text += CGI->generaltexth->arraytxt[noneTxtId];
  5056. else
  5057. {
  5058. if (node->hasBonusOfType (Bonus::UNDEAD) || node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING)) //it's a creature window
  5059. {
  5060. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  5061. }
  5062. else
  5063. {
  5064. for(auto & elem : mrl)
  5065. {
  5066. if (elem.first) //no bonuses with value 0
  5067. text += "\n" + elem.second;
  5068. }
  5069. }
  5070. }
  5071. std::string imageName;
  5072. if (small)
  5073. imageName = morale ? "IMRL30": "ILCK30";
  5074. else
  5075. imageName = morale ? "IMRL42" : "ILCK42";
  5076. delete image;
  5077. image = new CAnimImage(imageName, bonusValue + 3);
  5078. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  5079. }
  5080. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small):
  5081. image(nullptr),
  5082. morale(Morale),
  5083. small(Small)
  5084. {
  5085. bonusValue = 0;
  5086. pos = r + pos;
  5087. }
  5088. CArtifactHolder::CArtifactHolder()
  5089. {
  5090. }
  5091. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  5092. {
  5093. for(CArtifactsOfHero *aoh : artSets)
  5094. aoh->artifactRemoved(artLoc);
  5095. }
  5096. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  5097. {
  5098. CArtifactsOfHero *destaoh = nullptr;
  5099. for(CArtifactsOfHero *aoh : artSets)
  5100. {
  5101. aoh->artifactMoved(artLoc, destLoc);
  5102. aoh->redraw();
  5103. if(destLoc.isHolder(aoh->getHero()))
  5104. destaoh = aoh;
  5105. }
  5106. //Make sure the status bar is updated so it does not display old text
  5107. if(destaoh != nullptr && destaoh->getArtPlace(destLoc.slot) != nullptr)
  5108. {
  5109. destaoh->getArtPlace(destLoc.slot)->hover(true);
  5110. }
  5111. }
  5112. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  5113. {
  5114. for(CArtifactsOfHero *aoh : artSets)
  5115. aoh->artifactDisassembled(artLoc);
  5116. }
  5117. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  5118. {
  5119. for(CArtifactsOfHero *aoh : artSets)
  5120. aoh->artifactAssembled(artLoc);
  5121. }
  5122. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  5123. {
  5124. slotID = ArtifactPosition::PRE_FIRST;
  5125. AOH = nullptr;
  5126. art = nullptr;
  5127. }
  5128. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  5129. {
  5130. clear();
  5131. }
  5132. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CArtPlace *place, bool dontTakeBackpack)
  5133. {
  5134. slotID = place->slotID;
  5135. AOH = place->ourOwner;
  5136. if(slotID >= 19 && dontTakeBackpack)
  5137. art = nullptr;
  5138. else
  5139. art = place->ourArt;
  5140. }
  5141. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  5142. {
  5143. if(!AOH)
  5144. return false;
  5145. bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
  5146. //assert(al.getArt() == art);
  5147. return ret;
  5148. }
  5149. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  5150. {
  5151. assert(AOH && art);
  5152. return art == AOH->curHero->getArt(slotID);
  5153. }
  5154. void CRClickPopup::clickRight(tribool down, bool previousState)
  5155. {
  5156. if(down)
  5157. return;
  5158. close();
  5159. }
  5160. void CRClickPopup::close()
  5161. {
  5162. GH.popIntTotally(this);
  5163. }
  5164. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  5165. {
  5166. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  5167. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  5168. temp->center(Point(GH.current->motion)); //center on mouse
  5169. temp->fitToScreen(10);
  5170. auto rcpi = new CRClickPopupInt(temp,true);
  5171. GH.pushInt(rcpi);
  5172. }
  5173. void CRClickPopup::createAndPush(const std::string &txt, CComponent * component)
  5174. {
  5175. CInfoWindow::TCompsInfo intComps;
  5176. intComps.push_back(component);
  5177. createAndPush(txt, intComps);
  5178. }
  5179. Point CInfoBoxPopup::toScreen(Point p)
  5180. {
  5181. vstd::abetween(p.x, adventureInt->terrain.pos.x + 100, adventureInt->terrain.pos.x + adventureInt->terrain.pos.w - 100);
  5182. vstd::abetween(p.y, adventureInt->terrain.pos.y + 100, adventureInt->terrain.pos.y + adventureInt->terrain.pos.h - 100);
  5183. return p;
  5184. }
  5185. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town):
  5186. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  5187. {
  5188. InfoAboutTown iah;
  5189. LOCPLINT->cb->getTownInfo(town, iah);
  5190. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5191. new CTownTooltip(Point(9, 10), iah);
  5192. }
  5193. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero):
  5194. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "HEROQVBK", toScreen(position))
  5195. {
  5196. InfoAboutHero iah;
  5197. LOCPLINT->cb->getHeroInfo(hero, iah);
  5198. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5199. new CHeroTooltip(Point(9, 10), iah);
  5200. }
  5201. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr):
  5202. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  5203. {
  5204. InfoAboutTown iah;
  5205. LOCPLINT->cb->getTownInfo(garr, iah);
  5206. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5207. new CArmyTooltip(Point(9, 10), iah);
  5208. }
  5209. CIntObject * CRClickPopup::createInfoWin(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  5210. {
  5211. if(!specific)
  5212. specific = adventureInt->selection;
  5213. assert(specific);
  5214. switch(specific->ID)
  5215. {
  5216. case Obj::HERO:
  5217. return new CInfoBoxPopup(position, dynamic_cast<const CGHeroInstance *>(specific));
  5218. case Obj::TOWN:
  5219. return new CInfoBoxPopup(position, dynamic_cast<const CGTownInstance *>(specific));
  5220. case Obj::GARRISON:
  5221. case Obj::GARRISON2:
  5222. return new CInfoBoxPopup(position, dynamic_cast<const CGGarrison *>(specific));
  5223. default:
  5224. return nullptr;
  5225. }
  5226. }
  5227. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  5228. {
  5229. CIntObject *iWin = createInfoWin(p, obj); //try get custom infowindow for this obj
  5230. if(iWin)
  5231. GH.pushInt(iWin);
  5232. else
  5233. CRClickPopup::createAndPush(obj->getHoverText());
  5234. }
  5235. CRClickPopup::CRClickPopup()
  5236. {
  5237. addUsedEvents(RCLICK);
  5238. }
  5239. CRClickPopup::~CRClickPopup()
  5240. {
  5241. }
  5242. void CRClickPopupInt::show(SDL_Surface * to)
  5243. {
  5244. inner->show(to);
  5245. }
  5246. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  5247. {
  5248. CCS->curh->hide();
  5249. inner = our;
  5250. delInner = deleteInt;
  5251. }
  5252. CRClickPopupInt::~CRClickPopupInt()
  5253. {
  5254. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  5255. // //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...?)
  5256. // if(LOCPLINT && inner == adventureInt->heroWindow)
  5257. // adventureInt->heroWindow->dispose();
  5258. if(delInner)
  5259. delete inner;
  5260. CCS->curh->show();
  5261. }
  5262. void CRClickPopupInt::showAll(SDL_Surface * to)
  5263. {
  5264. inner->showAll(to);
  5265. }