GUIClasses.cpp 158 KB

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