GUIClasses.cpp 146 KB

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