GUIClasses.cpp 185 KB

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