GUIClasses.cpp 184 KB

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