GUIClasses.cpp 198 KB

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