GUIClasses.cpp 143 KB

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