GUIClasses.cpp 185 KB

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