GUIClasses.cpp 190 KB

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