GUIClasses.cpp 191 KB

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