GUIClasses.cpp 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342
  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. int oldFrom = from;
  1133. if(selected < 0) //no selection, do nothing
  1134. return;
  1135. if(selected < from) //scroll up
  1136. from = selected;
  1137. else if(from + SIZE <= selected)
  1138. from = selected - SIZE + 1;
  1139. amin(from, size() - SIZE);
  1140. amax(from, 0);
  1141. }
  1142. CHeroList::CHeroList(int Size)
  1143. :CList(Size)
  1144. {
  1145. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  1146. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  1147. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  1148. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  1149. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  1150. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  1151. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  1152. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  1153. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  1154. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  1155. //32px per hero
  1156. posmobx = pos.x+1;
  1157. posmoby = pos.y+arrup->height+1;
  1158. posporx = pos.x+mobile->width+2;
  1159. pospory = pos.y+arrup->height;
  1160. posmanx = pos.x+1+50+mobile->width;
  1161. posmany = pos.y+arrup->height+1;
  1162. from = 0;
  1163. selected = -1;
  1164. pressed = indeterminate;
  1165. }
  1166. void CHeroList::init()
  1167. {
  1168. int w = pos.w+1, h = pos.h+4;
  1169. bg = CSDL_Ext::newSurface(w,h,screen);
  1170. SDL_BlitSurface(adventureInt->bg,&genRect(w,h,pos.x,pos.y),bg,&genRect(w,h,0,0));
  1171. }
  1172. void CHeroList::genList()
  1173. {
  1174. //int howMany = LOCPLINT->cb->howManyHeroes();
  1175. //for (int i=0;i<howMany;i++)
  1176. //{
  1177. // const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  1178. // if(!h->inTownGarrison)
  1179. // items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  1180. //}
  1181. }
  1182. void CHeroList::select(int which)
  1183. {
  1184. if (which<0)
  1185. {
  1186. selected = which;
  1187. adventureInt->selection = NULL;
  1188. adventureInt->terrain.currentPath = NULL;
  1189. draw(screen);
  1190. adventureInt->infoBar.draw(screen);
  1191. }
  1192. if (which>=LOCPLINT->wanderingHeroes.size())
  1193. return;
  1194. selected = which;
  1195. adventureInt->select(LOCPLINT->wanderingHeroes[which]);
  1196. }
  1197. void CHeroList::clickLeft(tribool down, bool previousState)
  1198. {
  1199. if (down)
  1200. {
  1201. /***************************ARROWS*****************************************/
  1202. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1203. {
  1204. if(from>0)
  1205. {
  1206. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  1207. pressed = true;
  1208. }
  1209. return;
  1210. }
  1211. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1212. {
  1213. if(LOCPLINT->wanderingHeroes.size()-from>SIZE)
  1214. {
  1215. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  1216. pressed = false;
  1217. }
  1218. return;
  1219. }
  1220. /***************************HEROES*****************************************/
  1221. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1222. hx-=pos.x;
  1223. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1224. int ny = hy/32;
  1225. if (ny>=SIZE || ny<0)
  1226. return;
  1227. if ( (ny+from)==selected && (adventureInt->selection->ID == HEROI_TYPE))
  1228. LOCPLINT->openHeroWindow(LOCPLINT->wanderingHeroes[selected]);//print hero screen
  1229. select(ny+from);
  1230. }
  1231. else
  1232. {
  1233. if (indeterminate(pressed))
  1234. return;
  1235. if (pressed) //up
  1236. {
  1237. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  1238. pressed = indeterminate;
  1239. if (!down)
  1240. {
  1241. from--;
  1242. if (from<0)
  1243. from=0;
  1244. draw(screen);
  1245. }
  1246. }
  1247. else if (!pressed) //down
  1248. {
  1249. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  1250. pressed = indeterminate;
  1251. if (!down)
  1252. {
  1253. from++;
  1254. //if (from<items.size()-5)
  1255. // from=items.size()-5;
  1256. draw(screen);
  1257. }
  1258. }
  1259. else
  1260. throw 0;
  1261. }
  1262. }
  1263. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1264. {
  1265. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1266. {
  1267. if (from>0)
  1268. adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  1269. else
  1270. adventureInt->statusbar.clear();
  1271. return;
  1272. }
  1273. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1274. {
  1275. if ((LOCPLINT->wanderingHeroes.size()-from) > SIZE)
  1276. adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1277. else
  1278. adventureInt->statusbar.clear();
  1279. return;
  1280. }
  1281. //if not buttons then heroes
  1282. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1283. hx-=pos.x;
  1284. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1285. int ny = hy/32;
  1286. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1287. {
  1288. adventureInt->statusbar.clear();
  1289. return;
  1290. }
  1291. std::vector<std::string> temp;
  1292. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->name);
  1293. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->type->heroClass->name);
  1294. adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1295. //select(ny+from);
  1296. }
  1297. void CHeroList::clickRight(tribool down, bool previousState)
  1298. {
  1299. if (down)
  1300. {
  1301. /***************************ARROWS*****************************************/
  1302. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1303. {
  1304. adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1305. }
  1306. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->wanderingHeroes.size()-from>5))
  1307. {
  1308. adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1309. }
  1310. else
  1311. {
  1312. //if not buttons then heroes
  1313. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1314. hx-=pos.x;
  1315. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1316. int ny = hy/32;
  1317. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1318. {
  1319. return;
  1320. }
  1321. //show popup
  1322. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[LOCPLINT->wanderingHeroes[from+ny]->subID],
  1323. GH.current->motion.x-graphics->heroWins[LOCPLINT->wanderingHeroes[from+ny]->subID]->w,
  1324. GH.current->motion.y-graphics->heroWins[LOCPLINT->wanderingHeroes[from+ny]->subID]->h,
  1325. false);
  1326. GH.pushInt(ip);
  1327. }
  1328. }
  1329. else
  1330. {
  1331. adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  1332. adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  1333. }
  1334. }
  1335. void CHeroList::hover (bool on)
  1336. {
  1337. }
  1338. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1339. {
  1340. }
  1341. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1342. {
  1343. if(toRemove) //remove specific hero
  1344. LOCPLINT->wanderingHeroes -= toRemove;
  1345. else
  1346. LOCPLINT->recreateHeroTownList();
  1347. if(selected >= LOCPLINT->wanderingHeroes.size())
  1348. select(LOCPLINT->wanderingHeroes.size()-1);
  1349. if(toRemove)
  1350. {
  1351. if(LOCPLINT->wanderingHeroes.size() == 0)
  1352. adventureInt->townList.select(0);
  1353. else
  1354. select(selected);
  1355. }
  1356. }
  1357. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1358. {
  1359. int ser = -1;
  1360. for(int i=0; i<LOCPLINT->wanderingHeroes.size() && ser<0; i++)
  1361. if(LOCPLINT->wanderingHeroes[i]->subID == which->subID)
  1362. ser = i;
  1363. ser -= from;
  1364. if(ser<0 || ser >= SIZE) return;
  1365. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1366. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1367. }
  1368. void CHeroList::draw(SDL_Surface * to)
  1369. {
  1370. for (int iT=0+from;iT<SIZE+from;iT++)
  1371. {
  1372. int i = iT-from;
  1373. if (iT>=LOCPLINT->wanderingHeroes.size())
  1374. {
  1375. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1376. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1377. blitAt(empty,posporx,pospory+i*32,to);
  1378. continue;
  1379. }
  1380. const CGHeroInstance *cur = LOCPLINT->wanderingHeroes[iT];
  1381. int pom = cur->movement / 100;
  1382. if (pom>25) pom=25;
  1383. if (pom<0) pom=0;
  1384. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1385. pom = cur->mana / 5;
  1386. if (pom>25) pom=25;
  1387. if (pom<0) pom=0;
  1388. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1389. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1390. blitAt(temp,posporx,pospory+i*32,to);
  1391. if ((selected == iT) && (adventureInt->selection->ID == HEROI_TYPE))
  1392. {
  1393. blitAt(selection,posporx,pospory+i*32,to);
  1394. }
  1395. //TODO: support for custom portraits
  1396. }
  1397. if (from>0)
  1398. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1399. else
  1400. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1401. if (LOCPLINT->wanderingHeroes.size()-from > SIZE)
  1402. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1403. else
  1404. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1405. }
  1406. int CHeroList::getPosOfHero(const CGHeroInstance* h)
  1407. {
  1408. return vstd::findPos(LOCPLINT->wanderingHeroes, h, std::equal_to<const CGHeroInstance*>());
  1409. }
  1410. void CHeroList::show( SDL_Surface * to )
  1411. {
  1412. }
  1413. int CHeroList::size()
  1414. {
  1415. return LOCPLINT->wanderingHeroes.size();
  1416. }
  1417. CTownList::~CTownList()
  1418. {
  1419. delete arrup;
  1420. delete arrdo;
  1421. }
  1422. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1423. :CList(Size)
  1424. {
  1425. arrup = CDefHandler::giveDef(arrupg);
  1426. arrdo = CDefHandler::giveDef(arrdog);
  1427. pos.x = x;
  1428. pos.y = y;
  1429. pos.w = std::max(arrdo->width, arrup->width);
  1430. pos.h = arrdo->height + arrup->height + Size*32;
  1431. arrupp.x=x;
  1432. arrupp.y=y;
  1433. arrupp.w=arrup->width;
  1434. arrupp.h=arrup->height;
  1435. arrdop.x=x;
  1436. arrdop.y=y+arrup->height+32*SIZE;
  1437. arrdop.w=arrdo->width;
  1438. arrdop.h=arrdo->height;
  1439. posporx = arrdop.x;
  1440. pospory = arrupp.y + arrupp.h;
  1441. pressed = indeterminate;
  1442. from = 0;
  1443. selected = -1;
  1444. }
  1445. void CTownList::genList()
  1446. {
  1447. // LOCPLINT->towns.clear();
  1448. // int howMany = LOCPLINT->cb->howManyTowns();
  1449. // for (int i=0;i<howMany;i++)
  1450. // {
  1451. // LOCPLINT->towns.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1452. // }
  1453. }
  1454. void CTownList::select(int which)
  1455. {
  1456. if (which>=LOCPLINT->towns.size())
  1457. return;
  1458. selected = which;
  1459. if(!fun.empty())
  1460. fun();
  1461. }
  1462. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1463. {
  1464. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1465. {
  1466. if (from>0)
  1467. GH.statusbar->print(CGI->generaltexth->zelp[306].first);
  1468. else
  1469. GH.statusbar->clear();
  1470. return;
  1471. }
  1472. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1473. {
  1474. if ((LOCPLINT->towns.size()-from) > SIZE)
  1475. GH.statusbar->print(CGI->generaltexth->zelp[307].first);
  1476. else
  1477. GH.statusbar->clear();
  1478. return;
  1479. }
  1480. //if not buttons then towns
  1481. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1482. hx-=pos.x;
  1483. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1484. int ny = hy/32;
  1485. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1486. {
  1487. GH.statusbar->clear();
  1488. return;
  1489. };
  1490. std::string temp = CGI->generaltexth->tcommands[4];
  1491. boost::algorithm::replace_first(temp,"%s",LOCPLINT->towns[from+ny]->name);
  1492. temp += ", "+LOCPLINT->towns[from+ny]->town->Name();
  1493. GH.statusbar->print(temp);
  1494. }
  1495. void CTownList::clickLeft(tribool down, bool previousState)
  1496. {
  1497. if (down)
  1498. {
  1499. /***************************ARROWS*****************************************/
  1500. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1501. {
  1502. if(from>0)
  1503. {
  1504. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1505. pressed = true;
  1506. }
  1507. return;
  1508. }
  1509. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1510. {
  1511. if(LOCPLINT->towns.size()-from > SIZE)
  1512. {
  1513. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1514. pressed = false;
  1515. }
  1516. return;
  1517. }
  1518. /***************************TOWNS*****************************************/
  1519. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1520. hx-=pos.x;
  1521. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1522. int ny = hy/32;
  1523. if (ny>=SIZE || ny<0)
  1524. return;
  1525. if(GH.topInt() == adventureInt
  1526. && (ny+from)==selected
  1527. && adventureInt->selection->ID == TOWNI_TYPE
  1528. )
  1529. LOCPLINT->openTownWindow(LOCPLINT->towns[selected]);//print town screen
  1530. else
  1531. select(ny+from);
  1532. }
  1533. else
  1534. {
  1535. if (indeterminate(pressed))
  1536. return;
  1537. if (pressed) //up
  1538. {
  1539. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1540. pressed = indeterminate;
  1541. if (!down)
  1542. {
  1543. from--;
  1544. if (from<0)
  1545. from=0;
  1546. draw(screenBuf);
  1547. }
  1548. }
  1549. else if (!pressed) //down
  1550. {
  1551. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1552. pressed = indeterminate;
  1553. if (!down)
  1554. {
  1555. from++;
  1556. //if (from<LOCPLINT->towns.size()-5)
  1557. // from=LOCPLINT->towns.size()-5;
  1558. draw(screenBuf);
  1559. }
  1560. }
  1561. else
  1562. throw 0;
  1563. }
  1564. }
  1565. void CTownList::clickRight(tribool down, bool previousState)
  1566. {
  1567. if (down)
  1568. {
  1569. /***************************ARROWS*****************************************/
  1570. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1571. {
  1572. adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1573. }
  1574. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->towns.size()-from>5))
  1575. {
  1576. adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1577. }
  1578. //if not buttons then towns
  1579. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1580. hx-=pos.x;
  1581. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1582. int ny = hy/32;
  1583. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1584. {
  1585. return;
  1586. }
  1587. //show popup
  1588. CInfoPopup * ip = new CInfoPopup(
  1589. graphics->townWins[LOCPLINT->towns[from+ny]->id],
  1590. GH.current->motion.x-graphics->townWins[LOCPLINT->towns[from+ny]->id]->w,
  1591. GH.current->motion.y-graphics->townWins[LOCPLINT->towns[from+ny]->id]->h,
  1592. false);
  1593. GH.pushInt(ip);
  1594. }
  1595. else
  1596. {
  1597. adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  1598. adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  1599. }
  1600. }
  1601. void CTownList::hover (bool on)
  1602. {
  1603. }
  1604. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1605. {
  1606. }
  1607. void CTownList::draw(SDL_Surface * to)
  1608. {
  1609. for (int iT=0+from;iT<SIZE+from;iT++)
  1610. {
  1611. int i = iT-from;
  1612. if (iT>=LOCPLINT->towns.size())
  1613. {
  1614. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1615. continue;
  1616. }
  1617. blitAt(graphics->getPic(LOCPLINT->towns[iT]->subID,LOCPLINT->towns[iT]->hasFort(),LOCPLINT->towns[iT]->builded),posporx,pospory+i*32,to);
  1618. if ((selected == iT) && (adventureInt->selection->ID == TOWNI_TYPE))
  1619. {
  1620. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1621. }
  1622. }
  1623. if (from>0)
  1624. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1625. else
  1626. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1627. if (LOCPLINT->towns.size()-from>SIZE)
  1628. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1629. else
  1630. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1631. }
  1632. void CTownList::show( SDL_Surface * to )
  1633. {
  1634. }
  1635. int CTownList::size()
  1636. {
  1637. return LOCPLINT->towns.size();
  1638. }
  1639. CCreaturePic::CCreaturePic(const CCreature *cre, bool Big)
  1640. :c(cre),big(Big)
  1641. {
  1642. anim = new CCreatureAnimation(cre->animDefName);
  1643. }
  1644. CCreaturePic::~CCreaturePic()
  1645. {
  1646. delete anim;
  1647. }
  1648. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  1649. {
  1650. SDL_Rect dst;
  1651. if(big)
  1652. {
  1653. blitAt(graphics->backgrounds[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1654. dst = genRect(130,100,x,y);
  1655. }
  1656. else
  1657. {
  1658. blitAt(graphics->backgroundsm[c->faction],x,y,to);//curx-50,pos.y+130-65);
  1659. dst = genRect(120,100,x,y);
  1660. }
  1661. if(c->isDoubleWide())
  1662. x-=15;
  1663. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  1664. }
  1665. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  1666. {
  1667. //TODO: write
  1668. return NULL;
  1669. }
  1670. void CRecruitmentWindow::close()
  1671. {
  1672. GH.popIntTotally(this);
  1673. }
  1674. void CRecruitmentWindow::Max()
  1675. {
  1676. slider->moveTo(slider->amount);
  1677. }
  1678. void CRecruitmentWindow::Buy()
  1679. {
  1680. int crid = creatures[which].ID,
  1681. dstslot = dst->army.getSlotFor(crid);
  1682. if(dstslot < 0) //no available slot
  1683. {
  1684. std::string txt;
  1685. if(dst->ID == HEROI_TYPE)
  1686. {
  1687. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1688. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid].namePl : CGI->creh->creatures[crid].nameSing);
  1689. }
  1690. else
  1691. {
  1692. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1693. }
  1694. LOCPLINT->showInfoDialog(txt);
  1695. return;
  1696. }
  1697. recruit(crid, slider->value);
  1698. if(level >= 0)
  1699. close();
  1700. else
  1701. slider->moveTo(0);
  1702. }
  1703. void CRecruitmentWindow::Cancel()
  1704. {
  1705. close();
  1706. }
  1707. void CRecruitmentWindow::sliderMoved(int to)
  1708. {
  1709. buy->block(!to);
  1710. }
  1711. void CRecruitmentWindow::clickLeft(tribool down, bool previousState)
  1712. {
  1713. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1714. for(int i=0;i<creatures.size();i++)
  1715. {
  1716. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1717. if(isItIn(&genRect(132,sCREATURE_WIDTH,pos.x+curx,pos.y+64),GH.current->motion.x,GH.current->motion.y))
  1718. {
  1719. which = i;
  1720. int newAmount = std::min(amounts[i],creatures[i].amount);
  1721. slider->setAmount(newAmount);
  1722. if(slider->value > newAmount)
  1723. slider->moveTo(newAmount);
  1724. else
  1725. slider->moveTo(slider->value);
  1726. curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1727. for(int j=0;j<creatures.size();j++)
  1728. {
  1729. if(which==j)
  1730. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(255,0,0));
  1731. else
  1732. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(239,215,123));
  1733. curx += TOTAL_CREATURE_WIDTH;
  1734. }
  1735. break;
  1736. }
  1737. curx += TOTAL_CREATURE_WIDTH;
  1738. }
  1739. }
  1740. void CRecruitmentWindow::clickRight(tribool down, bool previousState)
  1741. {
  1742. if(down)
  1743. {
  1744. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1745. for(int i=0;i<creatures.size();i++)
  1746. {
  1747. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1748. if(isItIn(&genRect(132,sCREATURE_WIDTH,pos.x+curx,pos.y+64),GH.current->motion.x,GH.current->motion.y))
  1749. {
  1750. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID, 0, 0, NULL, NULL, NULL, NULL);
  1751. GH.pushInt(popup);
  1752. break;
  1753. }
  1754. curx += TOTAL_CREATURE_WIDTH;
  1755. }
  1756. }
  1757. }
  1758. void CRecruitmentWindow::activate()
  1759. {
  1760. activateLClick();
  1761. activateRClick();
  1762. buy->activate();
  1763. max->activate();
  1764. cancel->activate();
  1765. slider->activate();
  1766. GH.statusbar = bar;
  1767. }
  1768. void CRecruitmentWindow::deactivate()
  1769. {
  1770. deactivateLClick();
  1771. deactivateRClick();
  1772. buy->deactivate();
  1773. max->deactivate();
  1774. cancel->deactivate();
  1775. slider->deactivate();
  1776. }
  1777. void CRecruitmentWindow::show(SDL_Surface * to)
  1778. {
  1779. static char animCounter=0; //animation counter - for determining appropriate animation frame to be shown
  1780. blitAt(bitmap,pos.x,pos.y,to);
  1781. buy->show(to);
  1782. max->show(to);
  1783. cancel->show(to);
  1784. slider->show(to);
  1785. char pom[15];
  1786. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1787. printAtMiddle(pom,pos.x+205,pos.y+254,FONT_SMALL,zwykly,to);
  1788. SDL_itoa(slider->value,pom,10); //recruit
  1789. printAtMiddle(pom,pos.x+279,pos.y+254,FONT_SMALL,zwykly,to);
  1790. 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"
  1791. int curx = pos.x+115-creatures[which].res.size()*16;
  1792. for(int i=0;i<creatures[which].res.size();i++)
  1793. {
  1794. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,to);
  1795. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,to);
  1796. SDL_itoa(creatures[which].res[i].second,pom,10);
  1797. printAtMiddle(pom,curx+14,pos.y+288,FONT_SMALL,zwykly,to);
  1798. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1799. printAtMiddle(pom,curx+12+258,pos.y+286,FONT_SMALL,zwykly,to);
  1800. curx+=32;
  1801. }
  1802. curx = pos.x + 192 + CREATURE_WIDTH - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1803. for(int i=0; i<creatures.size(); ++i)
  1804. {
  1805. creatures[i].pic->blitPic(to, curx-50, pos.y+130-65, !(animCounter%4));
  1806. curx += TOTAL_CREATURE_WIDTH;
  1807. }
  1808. ++animCounter;
  1809. bar->show(to);
  1810. }
  1811. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> &Recruit)
  1812. :recruit(Recruit), dwelling(Dwelling), dst(Dst), level(Level)
  1813. {
  1814. which = 0;
  1815. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  1816. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1817. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1818. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1819. SDL_FreeSurface(hhlp);
  1820. pos.x = screen->w/2 - bitmap->w/2;
  1821. pos.y = screen->h/2 - bitmap->h/2;
  1822. pos.w = bitmap->w;
  1823. pos.h = bitmap->h;
  1824. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  1825. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  1826. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  1827. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  1828. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecruitmentWindow::sliderMoved,this, _1),0,0,0,true);
  1829. initCres();
  1830. printAtMiddle(CGI->generaltexth->allTexts[346],113,233,FONT_SMALL,zwykly,bitmap); //cost per troop t
  1831. printAtMiddle(CGI->generaltexth->allTexts[465],205,233,FONT_SMALL,zwykly,bitmap); //available t
  1832. printAtMiddle(CGI->generaltexth->allTexts[16],279,233,FONT_SMALL,zwykly,bitmap); //recruit t
  1833. printAtMiddle(CGI->generaltexth->allTexts[466],373,233,FONT_SMALL,zwykly,bitmap); //total cost t
  1834. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  1835. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  1836. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  1837. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  1838. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  1839. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  1840. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  1841. //border for creatures
  1842. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1843. for(int i=0;i<creatures.size();i++)
  1844. {
  1845. creatures[i].pos.x = curx+1;
  1846. creatures[i].pos.y = 65;
  1847. creatures[i].pos.w = 100;
  1848. creatures[i].pos.h = 130;
  1849. if(which==i)
  1850. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(255,0,0));
  1851. else
  1852. drawBorder(bitmap,curx,64,CREATURE_WIDTH,132,int3(239,215,123));
  1853. curx += TOTAL_CREATURE_WIDTH;
  1854. }
  1855. if(!creatures[0].amount || !amounts[0])
  1856. {
  1857. max->block(true);
  1858. slider->block(true);
  1859. }
  1860. //buy->block(true); //not needed, will be blocked by initing slider on 0
  1861. }
  1862. CRecruitmentWindow::~CRecruitmentWindow()
  1863. {
  1864. cleanCres();
  1865. delete max;
  1866. delete buy;
  1867. delete cancel;
  1868. SDL_FreeSurface(bitmap);
  1869. delete slider;
  1870. delete bar;
  1871. }
  1872. void CRecruitmentWindow::initCres()
  1873. {
  1874. cleanCres();
  1875. for(int i=0; i<dwelling->creatures.size(); i++)
  1876. {
  1877. if(level >= 0 && i != level)
  1878. continue;
  1879. for(int j = dwelling->creatures[i].second.size() - 1; j >= 0 ; j--)
  1880. {
  1881. creatures.resize(creatures.size()+1);
  1882. creinfo &cur = creatures.back();
  1883. cur.amount = dwelling->creatures[i].first;
  1884. cur.ID = dwelling->creatures[i].second[j];
  1885. const CCreature *cre = &CGI->creh->creatures[cur.ID];
  1886. cur.pic = new CCreaturePic(cre);
  1887. for(int k=0; k<cre->cost.size(); k++)
  1888. if(cre->cost[k])
  1889. cur.res.push_back(std::make_pair(k,cre->cost[k]));
  1890. amounts.push_back(cre->maxAmount(LOCPLINT->cb->getResourceAmount()));
  1891. }
  1892. }
  1893. slider->setAmount(std::min(amounts[which],creatures[which].amount));
  1894. }
  1895. void CRecruitmentWindow::cleanCres()
  1896. {
  1897. for(int i=0;i<creatures.size();i++)
  1898. {
  1899. delete creatures[i].pic;
  1900. }
  1901. creatures.clear();
  1902. amounts.clear();
  1903. }
  1904. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1905. {
  1906. last = Last;
  1907. which = 1;
  1908. c=cid;
  1909. slider = NULL;
  1910. gar = Owner;
  1911. SDL_Surface *hhlp = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1912. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1913. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1914. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1915. SDL_FreeSurface(hhlp);
  1916. pos.x = screen->w/2 - bitmap->w/2;
  1917. pos.y = screen->h/2 - bitmap->h/2;
  1918. pos.w = bitmap->w;
  1919. pos.h = bitmap->h;
  1920. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1921. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1922. int sliderPositions = max - (last>=0) - (last==2);
  1923. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),0,sliderPositions,val,true);
  1924. a1 = max-val;
  1925. a2 = val;
  1926. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  1927. anim->anim->setType(1);
  1928. std::string title = CGI->generaltexth->allTexts[256];
  1929. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  1930. printAtMiddle(title,150,34,FONT_BIG,tytulowy,bitmap);
  1931. }
  1932. CSplitWindow::~CSplitWindow() //d-tor
  1933. {
  1934. SDL_FreeSurface(bitmap);
  1935. delete ok;
  1936. delete cancel;
  1937. delete slider;
  1938. delete anim;
  1939. }
  1940. void CSplitWindow::activate()
  1941. {
  1942. activateLClick();
  1943. activateKeys();
  1944. ok->activate();
  1945. cancel->activate();
  1946. slider->activate();
  1947. }
  1948. void CSplitWindow::deactivate()
  1949. {
  1950. deactivateLClick();
  1951. deactivateKeys();
  1952. ok->deactivate();
  1953. cancel->deactivate();
  1954. slider->deactivate();
  1955. }
  1956. void CSplitWindow::split()
  1957. {
  1958. gar->splitStacks(a2);
  1959. close();
  1960. }
  1961. void CSplitWindow::close()
  1962. {
  1963. GH.popIntTotally(this);
  1964. }
  1965. void CSplitWindow::sliderMoved(int to)
  1966. {
  1967. int all = a1+a2;
  1968. a2 = to + (last==1 || last==2);
  1969. if(slider)
  1970. a1 = all - a2;
  1971. }
  1972. void CSplitWindow::show(SDL_Surface * to)
  1973. {
  1974. blitAt(bitmap,pos.x,pos.y,to);
  1975. ok->show(to);
  1976. cancel->show(to);
  1977. slider->show(to);
  1978. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,FONT_BIG,zwykly,to);
  1979. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,FONT_BIG,zwykly,to);
  1980. anim->blitPic(to,pos.x+20,pos.y+54,false);
  1981. anim->blitPic(to,pos.x+177,pos.y+54,false);
  1982. }
  1983. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1984. {
  1985. SDLKey k = key.keysym.sym;
  1986. if (isNumKey(k)) //convert numpad number to normal digit
  1987. k = numToDigit(k);
  1988. if(key.state != SDL_PRESSED)
  1989. return;
  1990. int &cur = (which ? a2 : a1),
  1991. &sec = (which ? a1 : a2),
  1992. ncur = cur;
  1993. if (k == SDLK_BACKSPACE)
  1994. {
  1995. ncur /= 10;
  1996. }
  1997. else if(k == SDLK_TAB)
  1998. {
  1999. which = !which;
  2000. }
  2001. else
  2002. {
  2003. int number = k - SDLK_0;
  2004. if (number < 0 || number > 9) //not a number pressed
  2005. {
  2006. return;
  2007. }
  2008. ncur = cur*10 + number;
  2009. }
  2010. int delta = ncur - cur;
  2011. if(delta > sec)
  2012. {
  2013. cur += sec;
  2014. sec = 0;
  2015. }
  2016. slider->moveTo(which ? ncur : a1+a2-ncur);
  2017. }
  2018. void CSplitWindow::clickLeft(tribool down, bool previousState)
  2019. {
  2020. if(down)
  2021. {
  2022. Point click(GH.current->motion.x,GH.current->motion.y);
  2023. click = click - pos.topLeft();
  2024. if(Rect(19,216,105,40).isIn(click)) //left picture
  2025. which = 0;
  2026. else if(Rect(175,216,105,40).isIn(click)) //right picture
  2027. which = 1;
  2028. }
  2029. }
  2030. void CCreInfoWindow::show(SDL_Surface * to)
  2031. {
  2032. char pom[15];
  2033. blitAt(bitmap,pos.x,pos.y,to);
  2034. anim->blitPic(to,pos.x+21,pos.y+48,(type) && !(anf%4));
  2035. if(++anf==4)
  2036. anf=0;
  2037. if(count.size())
  2038. printTo(count.c_str(),pos.x+114,pos.y+174,FONT_TIMES,zwykly,to);
  2039. if(upgrade)
  2040. upgrade->show(to);
  2041. if(dismiss)
  2042. dismiss->show(to);
  2043. if(ok)
  2044. ok->show(to);
  2045. }
  2046. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  2047. : type(Type), dsm(Dsm), dismiss(0), upgrade(0), ok(0)
  2048. {
  2049. //active = false;
  2050. anf = 0;
  2051. c = &CGI->creh->creatures[Cid];
  2052. SDL_Surface *hhlp = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  2053. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2054. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  2055. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2056. SDL_FreeSurface(hhlp);
  2057. pos.x = screen->w/2 - bitmap->w/2;
  2058. pos.y = screen->h/2 - bitmap->h/2;
  2059. pos.w = bitmap->w;
  2060. pos.h = bitmap->h;
  2061. anim = new CCreaturePic(c);
  2062. if(!type) anim->anim->setType(2);
  2063. char pom[75];int hlp=0;
  2064. if(creatureCount)
  2065. {
  2066. SDL_itoa(creatureCount,pom,10);
  2067. count = pom;
  2068. }
  2069. printAtMiddle(c->namePl,149,30,FONT_SMALL,tytulowy,bitmap); //creature name
  2070. //atttack
  2071. printAt(CGI->generaltexth->primarySkillNames[0],155,48,FONT_SMALL,zwykly,bitmap);
  2072. SDL_itoa(c->attack,pom,10);
  2073. if(State && State->attackBonus)
  2074. {
  2075. int hlp;
  2076. if(c->attack > 0)
  2077. hlp = log10f(c->attack)+2;
  2078. else
  2079. hlp = 2;
  2080. pom[hlp-1] = ' '; pom[hlp] = '(';
  2081. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  2082. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  2083. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2084. }
  2085. printTo(pom,276,61,FONT_SMALL,zwykly,bitmap);
  2086. //defense
  2087. printAt(CGI->generaltexth->primarySkillNames[1],155,67,FONT_SMALL,zwykly,bitmap);
  2088. SDL_itoa(c->defence,pom,10);
  2089. if(State && State->defenseBonus)
  2090. {
  2091. int hlp;
  2092. if(c->defence > 0)
  2093. hlp = log10f(c->defence)+2;
  2094. else
  2095. hlp = 2;
  2096. pom[hlp-1] = ' '; pom[hlp] = '(';
  2097. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  2098. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  2099. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2100. }
  2101. printTo(pom,276,80,FONT_SMALL,zwykly,bitmap);
  2102. //shots
  2103. if(c->shots)
  2104. {
  2105. printAt(CGI->generaltexth->allTexts[198], 155, 86, FONT_SMALL, zwykly, bitmap);
  2106. if(State && State->shotsLeft >= 0)
  2107. sprintf(pom,"%d (%d)", c->shots, State->shotsLeft);
  2108. else
  2109. SDL_itoa(c->shots, pom, 10);
  2110. printTo(pom, 276, 99, FONT_SMALL, zwykly, bitmap);
  2111. }
  2112. //damage
  2113. int dmgMin = c->damageMin * (State ? State->dmgMultiplier : 1);
  2114. int dmgMax = c->damageMax * (State ? State->dmgMultiplier : 1);
  2115. printAt(CGI->generaltexth->allTexts[199], 155, 105, FONT_SMALL, zwykly, bitmap);
  2116. SDL_itoa(dmgMin, pom, 10);
  2117. if(dmgMin > 0)
  2118. hlp = log10f(dmgMin) + 2;
  2119. else
  2120. hlp = 2;
  2121. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  2122. SDL_itoa(dmgMax, pom+hlp+2, 10);
  2123. printTo(pom, 276, 118, FONT_SMALL, zwykly, bitmap);
  2124. //health
  2125. printAt(CGI->generaltexth->allTexts[388],155,124,FONT_SMALL,zwykly,bitmap);
  2126. if(State && State->healthBonus)
  2127. sprintf(pom,"%d (%d)",c->hitPoints, c->hitPoints + State->healthBonus);
  2128. else
  2129. SDL_itoa(c->hitPoints,pom,10);
  2130. printTo(pom,276,137,FONT_SMALL,zwykly,bitmap);
  2131. //remaining health
  2132. if(State && State->currentHealth)
  2133. {
  2134. printAt(CGI->generaltexth->allTexts[200],155,143,FONT_SMALL,zwykly,bitmap);
  2135. SDL_itoa(State->currentHealth,pom,10);
  2136. printTo(pom,276,156,FONT_SMALL,zwykly,bitmap);
  2137. }
  2138. //speed
  2139. printAt(CGI->generaltexth->zelp[441].first,155,162,FONT_SMALL,zwykly,bitmap);
  2140. SDL_itoa(c->speed,pom,10);
  2141. if(State && State->speedBonus)
  2142. {
  2143. int hlp;
  2144. if(c->speed > 0)
  2145. hlp = log10f(c->speed)+2;
  2146. else
  2147. hlp = 2;
  2148. pom[hlp-1] = ' '; pom[hlp] = '(';
  2149. SDL_itoa(c->speed + State->speedBonus, pom+hlp+1, 10);
  2150. hlp += 2+(int)log10f(c->speed + State->speedBonus);
  2151. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2152. }
  2153. printTo(pom,276,175,FONT_SMALL,zwykly,bitmap);
  2154. //luck and morale
  2155. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  2156. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  2157. //print abilities text - if r-click popup
  2158. if(type)
  2159. {
  2160. if(Upg && ui)
  2161. {
  2162. bool enough = true;
  2163. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  2164. {
  2165. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  2166. enough = false;
  2167. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  2168. }
  2169. if(enough)
  2170. {
  2171. CFunctionList<void()> fs[2];
  2172. fs[0] += Upg;
  2173. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  2174. CFunctionList<void()> cfl;
  2175. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false);
  2176. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF",SDLK_u);
  2177. }
  2178. else
  2179. {
  2180. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  2181. upgrade->callback.funcs.clear();
  2182. upgrade->bitmapOffset = 2;
  2183. }
  2184. }
  2185. if(Dsm)
  2186. {
  2187. CFunctionList<void()> fs[2];
  2188. //on dismiss confirmed
  2189. fs[0] += Dsm; //dismiss
  2190. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  2191. CFunctionList<void()> cfl;
  2192. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  2193. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF",SDLK_d);
  2194. }
  2195. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF",SDLK_RETURN);
  2196. }
  2197. else
  2198. {
  2199. printAtWB(c->abilityText,17,231,FONT_SMALL,35,zwykly,bitmap);
  2200. }
  2201. //spell effects
  2202. if(State)
  2203. {
  2204. int printed=0; //how many effect pics have been printed
  2205. for(std::set<int>::const_iterator it = State->effects.begin(); it!=State->effects.end(); ++it)
  2206. {
  2207. blitAt(graphics->spellEffectsPics->ourImages[*it + 1].bitmap, 127 + 52 * printed, 186, bitmap);
  2208. ++printed;
  2209. if(printed >= 3)
  2210. {
  2211. break;
  2212. }
  2213. }
  2214. }
  2215. }
  2216. CCreInfoWindow::~CCreInfoWindow()
  2217. {
  2218. SDL_FreeSurface(bitmap);
  2219. delete anim;
  2220. delete upgrade;
  2221. delete ok;
  2222. delete dismiss;
  2223. for(int i=0; i<upgResCost.size();i++)
  2224. delete upgResCost[i];
  2225. }
  2226. void CCreInfoWindow::activate()
  2227. {
  2228. //if(active) return;
  2229. //active = true;
  2230. if(!type)
  2231. activateRClick();
  2232. if(ok)
  2233. ok->activate();
  2234. if(dismiss)
  2235. dismiss->activate();
  2236. if(upgrade)
  2237. upgrade->activate();
  2238. }
  2239. void CCreInfoWindow::close()
  2240. {
  2241. GH.popIntTotally(this);
  2242. }
  2243. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  2244. {
  2245. if(down)
  2246. return;
  2247. close();
  2248. }
  2249. void CCreInfoWindow::dismissF()
  2250. {
  2251. dsm();
  2252. close();
  2253. }
  2254. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  2255. {
  2256. }
  2257. void CCreInfoWindow::deactivate()
  2258. {
  2259. //if(!active) return;
  2260. //active = false;
  2261. if(!type)
  2262. deactivateRClick();
  2263. if(ok)
  2264. ok->deactivate();
  2265. if(dismiss)
  2266. dismiss->deactivate();
  2267. if(upgrade)
  2268. upgrade->deactivate();
  2269. }
  2270. void CLevelWindow::close()
  2271. {
  2272. for(int i=0;i<comps.size();i++)
  2273. {
  2274. if(comps[i]->selected)
  2275. {
  2276. cb(i);
  2277. break;
  2278. }
  2279. }
  2280. GH.popIntTotally(this);
  2281. LOCPLINT->showingDialog->setn(false);
  2282. }
  2283. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  2284. {
  2285. LOCPLINT->showingDialog->setn(true);
  2286. heroPortrait = hero->portrait;
  2287. cb = callback;
  2288. for(int i=0;i<skills.size();i++)
  2289. {
  2290. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  2291. comps.back()->assignedKeys.insert(SDLK_1 + i);
  2292. }
  2293. SDL_Surface *hhlp = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  2294. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2295. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  2296. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2297. SDL_FreeSurface(hhlp);
  2298. pos.x = screen->w/2 - bitmap->w/2;
  2299. pos.y = screen->h/2 - bitmap->h/2;
  2300. pos.w = bitmap->w;
  2301. pos.h = bitmap->h;
  2302. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  2303. //draw window
  2304. char buf[100], buf2[100];
  2305. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  2306. sprintf(buf,buf2,hero->name.c_str());
  2307. printAtMiddle(buf,192,33,FONT_MEDIUM,zwykly,bitmap);
  2308. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  2309. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  2310. printAtMiddle(buf,192,162,FONT_MEDIUM,zwykly,bitmap);
  2311. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  2312. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,253,FONT_MEDIUM,zwykly,bitmap);
  2313. const Font *f = graphics->fonts[FONT_MEDIUM];
  2314. std::string text = CGI->generaltexth->allTexts[4];
  2315. int fontWidth = f->getWidth(text.c_str())/2;
  2316. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+fontWidth) )/2;
  2317. for(int i=0;i<comps.size();i++)
  2318. {
  2319. comps[i]->pos.x = curx+pos.x;
  2320. comps[i]->pos.y = 326+pos.y;
  2321. if( i < (comps.size()-1) )
  2322. {
  2323. curx += 44+21; //skill width + margin to "or"
  2324. printAtMiddle(text ,curx,346,FONT_MEDIUM,zwykly,bitmap);
  2325. curx += fontWidth+15;
  2326. }
  2327. }
  2328. if(comps.size() > 1)
  2329. {
  2330. ok->block(true);
  2331. }
  2332. else if(comps.size() == 1)
  2333. {
  2334. comps[0]->select(true);
  2335. }
  2336. }
  2337. void CLevelWindow::selectionChanged(unsigned to)
  2338. {
  2339. if(ok->blocked)
  2340. ok->block(false);
  2341. for(int i=0;i<comps.size();i++)
  2342. if(i==to)
  2343. comps[i]->select(true);
  2344. else
  2345. comps[i]->select(false);
  2346. }
  2347. CLevelWindow::~CLevelWindow()
  2348. {
  2349. delete ok;
  2350. for(int i=0;i<comps.size();i++)
  2351. delete comps[i];
  2352. SDL_FreeSurface(bitmap);
  2353. }
  2354. void CLevelWindow::activate()
  2355. {
  2356. ok->activate();
  2357. for(int i=0;i<comps.size();i++)
  2358. comps[i]->activate();
  2359. }
  2360. void CLevelWindow::deactivate()
  2361. {
  2362. ok->deactivate();
  2363. for(int i=0;i<comps.size();i++)
  2364. comps[i]->deactivate();
  2365. }
  2366. void CLevelWindow::show(SDL_Surface * to)
  2367. {
  2368. blitAt(bitmap,pos.x,pos.y,to);
  2369. blitAt(graphics->portraitLarge[heroPortrait],170+pos.x,66+pos.y,to);
  2370. ok->show(to);
  2371. for(int i=0;i<comps.size();i++)
  2372. comps[i]->show(to);
  2373. }
  2374. void CMinorResDataBar::show(SDL_Surface * to)
  2375. {
  2376. blitAt(bg,pos.x,pos.y,to);
  2377. char buf[30];
  2378. for (int i=0;i<7;i++)
  2379. {
  2380. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2381. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2382. }
  2383. std::vector<std::string> temp;
  2384. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  2385. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  2386. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  2387. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  2388. CGI->generaltexth->allTexts[62]
  2389. +": %s, "
  2390. + CGI->generaltexth->allTexts[63]
  2391. + ": %s, "
  2392. + CGI->generaltexth->allTexts[64]
  2393. + ": %s",temp)
  2394. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2395. }
  2396. CMinorResDataBar::CMinorResDataBar()
  2397. {
  2398. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  2399. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2400. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2401. pos.x = 7;
  2402. pos.y = 575;
  2403. pos.w = bg->w;
  2404. pos.h = bg->h;
  2405. }
  2406. CMinorResDataBar::~CMinorResDataBar()
  2407. {
  2408. SDL_FreeSurface(bg);
  2409. }
  2410. SDL_Surface * CCustomImgComponent::getImg()
  2411. {
  2412. return bmp;
  2413. }
  2414. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  2415. :bmp(sur), free(freeSur)
  2416. {
  2417. init(Type,Subtype,Val);
  2418. }
  2419. CCustomImgComponent::~CCustomImgComponent()
  2420. {
  2421. if(free)
  2422. SDL_FreeSurface(bmp);
  2423. }
  2424. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  2425. {
  2426. left = Left;
  2427. type = Type;
  2428. id = ID;
  2429. }
  2430. void CMarketplaceWindow::CTradeableItem::show(SDL_Surface * to)
  2431. {
  2432. SDL_Surface *hlp = getSurface();
  2433. blitAt(hlp,pos.x+19,pos.y+9,to);
  2434. }
  2435. void CMarketplaceWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  2436. {
  2437. CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(GH.topInt());
  2438. assert(mw);
  2439. if(down)
  2440. {
  2441. if(left)
  2442. {
  2443. if(mw->hLeft != this)
  2444. mw->hLeft = this;
  2445. else
  2446. return;
  2447. }
  2448. else
  2449. {
  2450. if(mw->hRight != this)
  2451. mw->hRight = this;
  2452. else
  2453. return;
  2454. }
  2455. mw->selectionChanged(left);
  2456. }
  2457. }
  2458. void CMarketplaceWindow::CTradeableItem::activate()
  2459. {
  2460. activateLClick();
  2461. }
  2462. void CMarketplaceWindow::CTradeableItem::deactivate()
  2463. {
  2464. deactivateLClick();
  2465. }
  2466. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  2467. {
  2468. switch(type)
  2469. {
  2470. case 0:
  2471. return graphics->resources32->ourImages[id].bitmap;
  2472. case 1:
  2473. return graphics->artDefs->ourImages[id].bitmap;
  2474. default:
  2475. return NULL;
  2476. }
  2477. }
  2478. static void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  2479. {
  2480. i.resize(amount);
  2481. for(int j=0;j<amount;j++)
  2482. {
  2483. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  2484. i[j]->pos = p[j];
  2485. }
  2486. }
  2487. void CMarketplaceWindow::setMode( int mode )
  2488. {
  2489. std::vector<SDL_Rect> lpos, rpos;
  2490. clear();
  2491. switch(mode)
  2492. {
  2493. case 0:
  2494. {
  2495. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  2496. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  2497. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  2498. bg = SDL_ConvertSurface(bg2,screen->format,0);
  2499. SDL_FreeSurface(bg2);
  2500. lpos += genRect(66,74,39,180), genRect(66,74,122,180), genRect(66,74,204,180),
  2501. genRect(66,74,39,259), genRect(66,74,122,259), genRect(66,74,204,259),
  2502. genRect(66,74,122,338);
  2503. for(int i=0;i<lpos.size();i++)
  2504. {
  2505. lpos[i].x += pos.x;
  2506. lpos[i].y += pos.y;
  2507. rpos.push_back(lpos[i]);
  2508. rpos[rpos.size()-1].x += 288;
  2509. }
  2510. initItems(left,lpos,0,7,true,NULL);
  2511. initItems(right,rpos,0,7,false,NULL);
  2512. printAtMiddle(CGI->generaltexth->allTexts[158],300,27,FONT_BIG,tytulowy,bg); //title
  2513. printAtMiddle(CGI->generaltexth->allTexts[270],154,148,FONT_SMALL,zwykly,bg); //kingdom res.
  2514. printAtMiddle(CGI->generaltexth->allTexts[168],445,147,FONT_SMALL,zwykly,bg); //available for trade
  2515. }
  2516. }
  2517. }
  2518. void CMarketplaceWindow::clear()
  2519. {
  2520. for(int i=0;i<left.size();i++)
  2521. delete left[i];
  2522. for(int i=0;i<right.size();i++)
  2523. delete right[i];
  2524. left.clear();
  2525. right.clear();
  2526. SDL_FreeSurface(bg);
  2527. }
  2528. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  2529. {
  2530. type = BLOCK_ADV_HOTKEYS;
  2531. mode = Mode;
  2532. bg = NULL;
  2533. ok = max = deal = NULL;
  2534. pos.x = screen->w/2 - 300;
  2535. pos.y = screen->h/2 - 296;
  2536. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  2537. setMode(mode);
  2538. hLeft = hRight = NULL;
  2539. ok = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH,this),pos.x+516,pos.y+520,"IOK6432.DEF",SDLK_RETURN);
  2540. ok->assignedKeys.insert(SDLK_ESCAPE);
  2541. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  2542. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  2543. max->block(true);
  2544. deal->block(true);
  2545. }
  2546. CMarketplaceWindow::~CMarketplaceWindow()
  2547. {
  2548. clear();
  2549. delete slider;
  2550. }
  2551. void CMarketplaceWindow::show(SDL_Surface * to)
  2552. {
  2553. blitAt(bg,pos,to);
  2554. if(hRight)
  2555. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2556. if(hLeft)
  2557. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2558. ok->show(to);
  2559. deal->show(to);
  2560. max->show(to);
  2561. slider->show(to);
  2562. for(int i=0;i<left.size();i++)
  2563. left[i]->show(to);
  2564. for(int i=0;i<right.size();i++)
  2565. right[i]->show(to);
  2566. if(mode==0)
  2567. {
  2568. char buf[15];
  2569. for(int i=0;i<left.size();i++)
  2570. {
  2571. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2572. printAtMiddle(buf,left[i]->pos.x+36,left[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2573. }
  2574. if(hLeft) //print prices
  2575. {
  2576. for(int i=0; i<right.size();i++)
  2577. {
  2578. if(right[i]->id != hLeft->id)
  2579. printAtMiddle(rSubs[i],right[i]->pos.x+36,right[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2580. else
  2581. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+36,right[i]->pos.y+57,FONT_SMALL,zwykly,to);
  2582. }
  2583. }
  2584. if(hLeft && hRight && hLeft->id!= hRight->id)
  2585. {
  2586. blitAt(hLeft->getSurface(),pos.x+141,pos.y+457,to);
  2587. blitAt(hRight->getSurface(),pos.x+429,pos.y+457,to);
  2588. SDL_itoa(slider->value * r1,buf,10);
  2589. printAtMiddle(buf,pos.x+156,pos.y+505,FONT_SMALL,zwykly,to);
  2590. SDL_itoa(slider->value * r2,buf,10);
  2591. printAtMiddle(buf,pos.x+443,pos.y+505,FONT_SMALL,zwykly,to);
  2592. }
  2593. }
  2594. }
  2595. void CMarketplaceWindow::activate()
  2596. {
  2597. for(int i=0;i<left.size();i++)
  2598. left[i]->activate();
  2599. for(int i=0;i<right.size();i++)
  2600. right[i]->activate();
  2601. ok->activate();
  2602. max->activate();
  2603. deal->activate();
  2604. slider->activate();
  2605. }
  2606. void CMarketplaceWindow::deactivate()
  2607. {
  2608. for(int i=0;i<left.size();i++)
  2609. left[i]->deactivate();
  2610. for(int i=0;i<right.size();i++)
  2611. right[i]->deactivate();
  2612. ok->deactivate();
  2613. max->deactivate();
  2614. deal->deactivate();
  2615. slider->deactivate();
  2616. }
  2617. void CMarketplaceWindow::setMax()
  2618. {
  2619. slider->moveTo(slider->amount);
  2620. }
  2621. void CMarketplaceWindow::makeDeal()
  2622. {
  2623. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  2624. slider->moveTo(0);
  2625. hLeft = NULL;
  2626. selectionChanged(true);
  2627. }
  2628. void CMarketplaceWindow::sliderMoved( int to )
  2629. {
  2630. }
  2631. void CMarketplaceWindow::selectionChanged(bool side)
  2632. {
  2633. if(hLeft && hRight && hLeft->id!= hRight->id)
  2634. {
  2635. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  2636. slider->setAmount(LOCPLINT->cb->getResourceAmount(hLeft->id) / r1);
  2637. slider->moveTo(0);
  2638. max->block(false);
  2639. deal->block(false);
  2640. }
  2641. else
  2642. {
  2643. max->block(true);
  2644. deal->block(true);
  2645. slider->setAmount(0);
  2646. slider->moveTo(0);
  2647. }
  2648. if(side && hLeft) //left selection changed, recalculate offers
  2649. {
  2650. rSubs.clear();
  2651. rSubs.resize(right.size());
  2652. int h1, h2;
  2653. for(int i=0;i<right.size();i++)
  2654. {
  2655. std::ostringstream oss;
  2656. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  2657. oss << h2;
  2658. if(h1!=1)
  2659. oss << "/" << h1;
  2660. rSubs[i] = oss.str();
  2661. }
  2662. }
  2663. }
  2664. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  2665. {
  2666. this->pos = pos;
  2667. SDL_Surface *hhlp = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  2668. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2669. background = SDL_ConvertSurface(hhlp,screen->format,0);
  2670. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  2671. SDL_FreeSurface(hhlp);
  2672. //printing texts
  2673. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 242, 32, FONT_BIG, tytulowy, background); //window title
  2674. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 64, FONT_MEDIUM, tytulowy, background); //hero speed
  2675. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 130, FONT_MEDIUM, tytulowy, background); //enemy speed
  2676. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 196, FONT_MEDIUM, tytulowy, background); //map scroll speed
  2677. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 262, FONT_MEDIUM, tytulowy, background); //video quality
  2678. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 347, FONT_MEDIUM, tytulowy, background); //music volume
  2679. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 412, FONT_MEDIUM, tytulowy, background); //effects volume
  2680. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 282, 57, FONT_MEDIUM, zwykly, background); //show move path
  2681. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 282, 89, FONT_MEDIUM, zwykly, background); //show hero reminder
  2682. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 282, 121, FONT_MEDIUM, zwykly, background); //quick combat
  2683. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 282, 153, FONT_MEDIUM, zwykly, background); //video subtitles
  2684. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 282, 185, FONT_MEDIUM, zwykly, background); //town building outlines
  2685. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 282, 217, FONT_MEDIUM, zwykly, background); //spell book animation
  2686. //setting up buttons
  2687. // 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);
  2688. // std::swap(save->imgs[0][0], load->imgs[0][1]);
  2689. 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);
  2690. std::swap(save->imgs[0][0], save->imgs[0][1]);
  2691. // 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);
  2692. // std::swap(save->imgs[0][0], restart->imgs[0][1]);
  2693. 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);
  2694. std::swap(mainMenu->imgs[0][0], mainMenu->imgs[0][1]);
  2695. 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);
  2696. std::swap(quitGame->imgs[0][0], quitGame->imgs[0][1]);
  2697. 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);
  2698. std::swap(backToMap->imgs[0][0], backToMap->imgs[0][1]);
  2699. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  2700. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  2701. 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);
  2702. 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);
  2703. 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);
  2704. 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);
  2705. heroMoveSpeed->select(owner->sysOpts.heroMoveSpeed, 1);
  2706. heroMoveSpeed->onChange = boost::bind(&SystemOptions::setHeroMoveSpeed, &owner->sysOpts, _1);
  2707. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  2708. 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);
  2709. 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);
  2710. 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);
  2711. mapScrollSpeed->select(owner->sysOpts.mapScrollingSpeed, 1);
  2712. mapScrollSpeed->onChange = boost::bind(&SystemOptions::setMapScrollingSpeed, &owner->sysOpts, _1);
  2713. musicVolume = new CHighlightableButtonsGroup(0, true);
  2714. for(int i=0; i<10; ++i)
  2715. {
  2716. 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);
  2717. }
  2718. musicVolume->select(CGI->musich->getVolume(), 1);
  2719. musicVolume->onChange = boost::bind(&SystemOptions::setMusicVolume, &owner->sysOpts, _1);
  2720. effectsVolume = new CHighlightableButtonsGroup(0, true);
  2721. for(int i=0; i<10; ++i)
  2722. {
  2723. 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);
  2724. }
  2725. effectsVolume->select(CGI->soundh->getVolume(), 1);
  2726. effectsVolume->onChange = boost::bind(&SystemOptions::setSoundVolume, &owner->sysOpts, _1);
  2727. }
  2728. CSystemOptionsWindow::~CSystemOptionsWindow()
  2729. {
  2730. SDL_FreeSurface(background);
  2731. delete save;
  2732. delete quitGame;
  2733. delete backToMap;
  2734. delete mainMenu;
  2735. delete heroMoveSpeed;
  2736. delete mapScrollSpeed;
  2737. delete musicVolume;
  2738. delete effectsVolume;
  2739. }
  2740. void CSystemOptionsWindow::pushSDLEvent(int type, int usercode)
  2741. {
  2742. GH.popIntTotally(this);
  2743. SDL_Event event;
  2744. event.type = type;
  2745. event.user.code = usercode; // not necessarily used
  2746. SDL_PushEvent(&event);
  2747. }
  2748. void CSystemOptionsWindow::bquitf()
  2749. {
  2750. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_QUIT, 0), 0, false);
  2751. }
  2752. void CSystemOptionsWindow::breturnf()
  2753. {
  2754. GH.popIntTotally(this);
  2755. }
  2756. void CSystemOptionsWindow::bmainmenuf()
  2757. {
  2758. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_USEREVENT, 2), 0, false);
  2759. }
  2760. void CSystemOptionsWindow::bsavef()
  2761. {
  2762. GH.popIntTotally(this);
  2763. GH.pushInt(new CSelectionScreen(CMenuScreen::saveGame));
  2764. /*using namespace boost::posix_time;
  2765. std::ostringstream fnameStream;
  2766. fnameStream << second_clock::local_time();
  2767. std::string fname = fnameStream.str();
  2768. boost::algorithm::replace_all(fname,":","");
  2769. boost::algorithm::replace_all(fname," ","-");
  2770. 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);*/
  2771. }
  2772. void CSystemOptionsWindow::activate()
  2773. {
  2774. save->activate();
  2775. quitGame->activate();
  2776. backToMap->activate();
  2777. mainMenu->activate();
  2778. heroMoveSpeed->activate();
  2779. mapScrollSpeed->activate();
  2780. musicVolume->activate();
  2781. effectsVolume->activate();
  2782. }
  2783. void CSystemOptionsWindow::deactivate()
  2784. {
  2785. save->deactivate();
  2786. quitGame->deactivate();
  2787. backToMap->deactivate();
  2788. mainMenu->deactivate();
  2789. heroMoveSpeed->deactivate();
  2790. mapScrollSpeed->deactivate();
  2791. musicVolume->deactivate();
  2792. effectsVolume->deactivate();
  2793. }
  2794. void CSystemOptionsWindow::show(SDL_Surface *to)
  2795. {
  2796. SDL_BlitSurface(background, NULL, to, &pos);
  2797. save->show(to);
  2798. quitGame->show(to);
  2799. backToMap->show(to);
  2800. mainMenu->show(to);
  2801. heroMoveSpeed->show(to);
  2802. mapScrollSpeed->show(to);
  2803. musicVolume->show(to);
  2804. effectsVolume->show(to);
  2805. }
  2806. CTavernWindow::CTavernWindow(const CGHeroInstance *H1, const CGHeroInstance *H2, const std::string &gossip)
  2807. :h1(selected,0,72,299,H1),h2(selected,1,162,299,H2)
  2808. {
  2809. if(H1)
  2810. selected = 0;
  2811. else
  2812. selected = -1;
  2813. oldSelected = -1;
  2814. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPTAVERN.bmp");
  2815. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2816. bg = SDL_ConvertSurface(hhlp,screen->format,0);
  2817. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2818. SDL_FreeSurface(hhlp);
  2819. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,FONT_BIG,tytulowy,bg);
  2820. printAtMiddle("2500",320,328,FONT_SMALL,zwykly,bg);
  2821. // printAtMiddle(CGI->generaltexth->jktexts[38],146,283,FONT_BIG,tytulowy,bg); //what is this???
  2822. printAtMiddleWB(gossip,200,220,FONT_SMALL,50,zwykly,bg);
  2823. pos.w = bg->w;
  2824. pos.h = bg->h;
  2825. pos.x = (screen->w-bg->w)/2;
  2826. pos.y = (screen->h-bg->h)/2;
  2827. bar = new CStatusBar(pos.x+8, pos.y+478, "APHLFTRT.bmp", 380);
  2828. h1.pos.x += pos.x;
  2829. h2.pos.x += pos.x;
  2830. h1.pos.y += pos.y;
  2831. h2.pos.y += pos.y;
  2832. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), pos.x+310,pos.y+428, "ICANCEL.DEF", SDLK_ESCAPE);
  2833. recruit = new AdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), pos.x+272, pos.y+355, "TPTAV01.DEF", SDLK_RETURN);
  2834. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), pos.x+22, pos.y+428, "TPTAV02.DEF", SDLK_t);
  2835. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  2836. {
  2837. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  2838. recruit->block(2);
  2839. }
  2840. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  2841. {
  2842. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  2843. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  2844. recruit->block(2);
  2845. }
  2846. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  2847. {
  2848. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  2849. recruit->block(2);
  2850. }
  2851. else
  2852. {
  2853. if(!H1)
  2854. recruit->block(1);
  2855. }
  2856. #ifdef _WIN32
  2857. CGI->videoh->open("TAVERN.BIK");
  2858. #else
  2859. CGI->videoh->open("tavern.mjpg", true, false);
  2860. #endif
  2861. }
  2862. void CTavernWindow::recruitb()
  2863. {
  2864. const CGHeroInstance *toBuy = (selected ? h2 : h1).h;
  2865. close();
  2866. LOCPLINT->cb->recruitHero(LOCPLINT->castleInt->town,toBuy);
  2867. }
  2868. void CTavernWindow::thievesguildb()
  2869. {
  2870. GH.pushInt( new CThievesGuildWindow(LOCPLINT->castleInt->town) );
  2871. }
  2872. CTavernWindow::~CTavernWindow()
  2873. {
  2874. CGI->videoh->close();
  2875. SDL_FreeSurface(bg);
  2876. delete cancel;
  2877. delete thiefGuild;
  2878. delete recruit;
  2879. delete bar;
  2880. }
  2881. void CTavernWindow::activate()
  2882. {
  2883. thiefGuild->activate();
  2884. cancel->activate();
  2885. if(h1.h)
  2886. h1.activate();
  2887. if(h2.h)
  2888. h2.activate();
  2889. recruit->activate();
  2890. GH.statusbar = bar;
  2891. }
  2892. void CTavernWindow::deactivate()
  2893. {
  2894. thiefGuild->deactivate();
  2895. cancel->deactivate();
  2896. if(h1.h)
  2897. h1.deactivate();
  2898. if(h2.h)
  2899. h2.deactivate();
  2900. recruit->deactivate();
  2901. }
  2902. void CTavernWindow::close()
  2903. {
  2904. GH.popIntTotally(this);
  2905. }
  2906. void CTavernWindow::show(SDL_Surface * to)
  2907. {
  2908. blitAt(bg,pos.x,pos.y,to);
  2909. CGI->videoh->update(pos.x+70, pos.y+56, to, true, false);
  2910. if(h1.h)
  2911. h1.show(to);
  2912. if(h2.h)
  2913. h2.show(to);
  2914. thiefGuild->show(to);
  2915. cancel->show(to);
  2916. recruit->show(to);
  2917. bar->show(to);
  2918. if(selected >= 0)
  2919. {
  2920. HeroPortrait *sel = selected ? &h2 : &h1;
  2921. if (selected != oldSelected && !recruit->blocked)
  2922. {
  2923. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  2924. oldSelected = selected;
  2925. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  2926. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  2927. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  2928. }
  2929. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,FONT_SMALL,40,zwykly,to);
  2930. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  2931. }
  2932. }
  2933. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  2934. {
  2935. if(previousState && !down)
  2936. as();
  2937. //ClickableL::clickLeft(down);
  2938. }
  2939. void CTavernWindow::HeroPortrait::activate()
  2940. {
  2941. activateLClick();
  2942. activateRClick();
  2943. activateHover();
  2944. }
  2945. void CTavernWindow::HeroPortrait::deactivate()
  2946. {
  2947. deactivateLClick();
  2948. deactivateRClick();
  2949. deactivateHover();
  2950. }
  2951. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  2952. {
  2953. if(down)
  2954. {
  2955. adventureInt->heroWindow->setHero(h);
  2956. GH.pushInt(new CRClickPopupInt(adventureInt->heroWindow,false));
  2957. }
  2958. }
  2959. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  2960. :as(sel,id)
  2961. {
  2962. h = H;
  2963. pos.x = x;
  2964. pos.y = y;
  2965. pos.w = 58;
  2966. pos.h = 64;
  2967. if(H)
  2968. {
  2969. hoverName = CGI->generaltexth->tavernInfo[4];
  2970. boost::algorithm::replace_first(hoverName,"%s",H->name);
  2971. int artifs = h->artifWorn.size() + h->artifacts.size();
  2972. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  2973. if(vstd::contains(h->artifWorn,i))
  2974. artifs--;
  2975. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  2976. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  2977. descr[sizeof(descr)-1] = '\0';
  2978. }
  2979. }
  2980. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  2981. {
  2982. blitAt(graphics->portraitLarge[h->subID],pos,to);
  2983. }
  2984. void CTavernWindow::HeroPortrait::hover( bool on )
  2985. {
  2986. //Hoverable::hover(on);
  2987. if(on)
  2988. GH.statusbar->print(hoverName);
  2989. else
  2990. GH.statusbar->clear();
  2991. }
  2992. void CInGameConsole::activate()
  2993. {
  2994. activateKeys();
  2995. }
  2996. void CInGameConsole::deactivate()
  2997. {
  2998. deactivateKeys();
  2999. }
  3000. void CInGameConsole::show(SDL_Surface * to)
  3001. {
  3002. int number = 0;
  3003. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3004. texts_mx.lock();
  3005. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  3006. {
  3007. SDL_Color green = {0,0xff,0,0};
  3008. Point leftBottomCorner(0, screen->h);
  3009. if(LOCPLINT->battleInt)
  3010. {
  3011. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3012. }
  3013. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, FONT_MEDIUM, green);
  3014. if(SDL_GetTicks() - it->second > defaultTimeout)
  3015. {
  3016. toDel.push_back(it);
  3017. }
  3018. }
  3019. for(int it=0; it<toDel.size(); ++it)
  3020. {
  3021. texts.erase(toDel[it]);
  3022. }
  3023. texts_mx.unlock();
  3024. }
  3025. void CInGameConsole::print(const std::string &txt)
  3026. {
  3027. texts_mx.lock();
  3028. int lineLen = conf.go()->ac.outputLineLength;
  3029. if(txt.size() < lineLen)
  3030. {
  3031. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3032. if(texts.size() > maxDisplayedTexts)
  3033. {
  3034. texts.pop_front();
  3035. }
  3036. }
  3037. else
  3038. {
  3039. assert(lineLen);
  3040. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3041. {
  3042. std::string part = txt.substr(g * lineLen, lineLen);
  3043. if(part.size() == 0)
  3044. break;
  3045. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3046. if(texts.size() > maxDisplayedTexts)
  3047. {
  3048. texts.pop_front();
  3049. }
  3050. }
  3051. }
  3052. texts_mx.unlock();
  3053. }
  3054. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3055. {
  3056. if(key.type != SDL_KEYDOWN) return;
  3057. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3058. switch(key.keysym.sym)
  3059. {
  3060. case SDLK_TAB:
  3061. case SDLK_ESCAPE:
  3062. {
  3063. if(captureAllKeys)
  3064. {
  3065. captureAllKeys = false;
  3066. endEnteringText(false);
  3067. }
  3068. else if(SDLK_TAB)
  3069. {
  3070. captureAllKeys = true;
  3071. startEnteringText();
  3072. }
  3073. break;
  3074. }
  3075. case SDLK_RETURN: //enter key
  3076. {
  3077. if(enteredText.size() > 0 && captureAllKeys)
  3078. {
  3079. captureAllKeys = false;
  3080. endEnteringText(true);
  3081. }
  3082. break;
  3083. }
  3084. case SDLK_BACKSPACE:
  3085. {
  3086. if(enteredText.size() > 1)
  3087. {
  3088. enteredText.resize(enteredText.size()-1);
  3089. enteredText[enteredText.size()-1] = '_';
  3090. refreshEnteredText();
  3091. }
  3092. break;
  3093. }
  3094. case SDLK_UP: //up arrow
  3095. {
  3096. if(previouslyEntered.size() == 0)
  3097. break;
  3098. if(prevEntDisp == -1)
  3099. {
  3100. prevEntDisp = previouslyEntered.size() - 1;
  3101. enteredText = previouslyEntered[prevEntDisp] + "_";
  3102. refreshEnteredText();
  3103. }
  3104. else if( prevEntDisp > 0)
  3105. {
  3106. --prevEntDisp;
  3107. enteredText = previouslyEntered[prevEntDisp] + "_";
  3108. refreshEnteredText();
  3109. }
  3110. break;
  3111. }
  3112. case SDLK_DOWN: //down arrow
  3113. {
  3114. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3115. {
  3116. ++prevEntDisp;
  3117. enteredText = previouslyEntered[prevEntDisp] + "_";
  3118. refreshEnteredText();
  3119. }
  3120. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3121. {
  3122. prevEntDisp = -1;
  3123. enteredText = "_";
  3124. refreshEnteredText();
  3125. }
  3126. break;
  3127. }
  3128. default:
  3129. {
  3130. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3131. {
  3132. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3133. {
  3134. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3135. enteredText += "_";
  3136. refreshEnteredText();
  3137. }
  3138. }
  3139. break;
  3140. }
  3141. }
  3142. }
  3143. void CInGameConsole::startEnteringText()
  3144. {
  3145. enteredText = "_";
  3146. if(GH.topInt() == adventureInt)
  3147. {
  3148. GH.statusbar->print(enteredText);
  3149. }
  3150. else if(LOCPLINT->battleInt)
  3151. {
  3152. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3153. }
  3154. }
  3155. void CInGameConsole::endEnteringText(bool printEnteredText)
  3156. {
  3157. prevEntDisp = -1;
  3158. if(printEnteredText)
  3159. {
  3160. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3161. LOCPLINT->cb->sendMessage(txt);
  3162. previouslyEntered.push_back(txt);
  3163. print(txt);
  3164. }
  3165. enteredText = "";
  3166. if(GH.topInt() == adventureInt)
  3167. {
  3168. GH.statusbar->clear();
  3169. }
  3170. else if(LOCPLINT->battleInt)
  3171. {
  3172. LOCPLINT->battleInt->console->ingcAlter = "";
  3173. }
  3174. }
  3175. void CInGameConsole::refreshEnteredText()
  3176. {
  3177. if(GH.topInt() == adventureInt)
  3178. {
  3179. GH.statusbar->print(enteredText);
  3180. }
  3181. else if(LOCPLINT->battleInt)
  3182. {
  3183. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3184. }
  3185. }
  3186. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3187. {
  3188. }
  3189. void CGarrisonWindow::close()
  3190. {
  3191. GH.popIntTotally(this);
  3192. }
  3193. void CGarrisonWindow::activate()
  3194. {
  3195. quit->activate();
  3196. garr->activate();
  3197. }
  3198. void CGarrisonWindow::deactivate()
  3199. {
  3200. quit->deactivate();
  3201. garr->deactivate();
  3202. }
  3203. void CGarrisonWindow::show(SDL_Surface * to)
  3204. {
  3205. blitAt(bg,pos,to);
  3206. quit->show(to);
  3207. garr->show(to);
  3208. blitAt(graphics->flags->ourImages[garr->odown->getOwner()].bitmap,pos.x+28,pos.y+124,to);
  3209. blitAt(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->odown)->portrait],pos.x+29,pos.y+222,to);
  3210. printAtMiddle(CGI->generaltexth->allTexts[709],pos.x+275,pos.y+30,FONT_BIG,tytulowy,to);
  3211. }
  3212. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits )
  3213. {
  3214. bg = BitmapHandler::loadBitmap("GARRISON.bmp");
  3215. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  3216. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  3217. pos.x = screen->w/2 - bg->w/2;
  3218. pos.y = screen->h/2 - bg->h/2;
  3219. pos.w = screen->w;
  3220. pos.h = screen->h;
  3221. garr = new CGarrisonInt(pos.x+92, pos.y+127, 4, Point(0,96), bg, Point(93,127), up, down, removableUnits);
  3222. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+88,pos.y+314,"IDV6432.DEF"));
  3223. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),pos.x+399,pos.y+314,"IOK6432.DEF",SDLK_RETURN);
  3224. }
  3225. CGarrisonWindow::~CGarrisonWindow()
  3226. {
  3227. SDL_FreeSurface(bg);
  3228. delete quit;
  3229. delete garr;
  3230. }
  3231. IShowActivable::IShowActivable()
  3232. {
  3233. type = 0;
  3234. }
  3235. CWindowWithGarrison::CWindowWithGarrison()
  3236. {
  3237. type |= WITH_GARRISON;
  3238. }
  3239. void CRClickPopupInt::show(SDL_Surface * to)
  3240. {
  3241. inner->show(to);
  3242. }
  3243. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  3244. {
  3245. CGI->curh->hide();
  3246. inner = our;
  3247. delInner = deleteInt;
  3248. }
  3249. CRClickPopupInt::~CRClickPopupInt()
  3250. {
  3251. //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  3252. //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...?)
  3253. if(LOCPLINT && inner == adventureInt->heroWindow)
  3254. adventureInt->heroWindow->dispose();
  3255. if(delInner)
  3256. delete inner;
  3257. CGI->curh->show();
  3258. }
  3259. CArtPlace::CArtPlace(const CArtifact* Art): active(false), marked(false), ourArt(Art)/*,
  3260. spellBook(false), warMachine1(false), warMachine2(false), warMachine3(false),
  3261. warMachine4(false),misc1(false), misc2(false), misc3(false), misc4(false),
  3262. misc5(false), feet(false), lRing(false), rRing(false), torso(false),
  3263. lHand(false), rHand(false), neck(false), shoulders(false), head(false) */
  3264. {
  3265. }
  3266. void CArtPlace::activate()
  3267. {
  3268. if(!active)
  3269. {
  3270. //activateLClick();
  3271. LRClickableAreaWTextComp::activate();
  3272. active = true;
  3273. }
  3274. }
  3275. void CArtPlace::clickLeft(tribool down, bool previousState)
  3276. {
  3277. //LRClickableAreaWTextComp::clickLeft(down);
  3278. // If clicked on spellbook, open it only if no artifact is held at the moment.
  3279. if(ourArt && !down && previousState && !ourOwner->commonInfo->srcAOH)
  3280. {
  3281. if(ourArt->id == 0)
  3282. {
  3283. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), ourOwner->curHero, LOCPLINT);
  3284. GH.pushInt(spellWindow);
  3285. }
  3286. }
  3287. if (!down && previousState)
  3288. {
  3289. if(ourArt && ourArt->id == 0)
  3290. return; //this is handled separately
  3291. if(!ourOwner->commonInfo->srcAOH) //nothing has been clicked
  3292. {
  3293. if(ourArt) //to prevent selecting empty slots (bugfix to what GrayFace reported)
  3294. {
  3295. if(ourArt->id == 3) //catapult cannot be highlighted
  3296. {
  3297. std::vector<SComponent *> catapult(1, new SComponent(SComponent::artifact, 3, 0));
  3298. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  3299. return;
  3300. }
  3301. select();
  3302. }
  3303. }
  3304. else //perform artifact substitution
  3305. {
  3306. if (slotID >= 19) // Backpack destination.
  3307. {
  3308. const CArtifact * cur = ourOwner->commonInfo->srcArtifact;
  3309. switch(cur->id)
  3310. {
  3311. case 3:
  3312. //should not happen, catapult cannot be selected
  3313. assert(cur->id != 3);
  3314. break;
  3315. case 4: case 5: case 6:
  3316. {
  3317. std::string text = CGI->generaltexth->allTexts[153];
  3318. boost::algorithm::replace_first(text, "%s", cur->Name());
  3319. LOCPLINT->showInfoDialog(text);
  3320. }
  3321. break;
  3322. default:
  3323. ourOwner->commonInfo->destAOH = ourOwner;
  3324. ourOwner->commonInfo->destSlotID = slotID;
  3325. ourOwner->commonInfo->destArtifact = NULL;
  3326. // Correction for backpack position when src lies before dest.
  3327. ourOwner->commonInfo->destSlotID +=
  3328. (ourOwner->commonInfo->srcAOH == ourOwner
  3329. && ourOwner->commonInfo->srcSlotID >= 19
  3330. && ourOwner->commonInfo->srcSlotID <= slotID);
  3331. LOCPLINT->cb->swapArtifacts(
  3332. ourOwner->commonInfo->srcAOH->curHero,
  3333. ourOwner->commonInfo->srcSlotID,
  3334. ourOwner->curHero,
  3335. ourOwner->commonInfo->destSlotID);
  3336. break;
  3337. }
  3338. }
  3339. //check if swap is possible
  3340. else if (this->fitsHere(ourOwner->commonInfo->srcArtifact))
  3341. {
  3342. ourOwner->commonInfo->destAOH = ourOwner;
  3343. ourOwner->commonInfo->destSlotID = slotID;
  3344. ourOwner->commonInfo->destArtifact = ourArt;
  3345. // Special case when the dest artifact can't be fit into the src slot.
  3346. CGHeroInstance *destHero = const_cast<CGHeroInstance *>(ourOwner->curHero);
  3347. CGI->arth->unequipArtifact(destHero->artifWorn, slotID);
  3348. const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->srcAOH;
  3349. ui16 srcSlotID = ourOwner->commonInfo->srcSlotID;
  3350. if (ourArt && srcSlotID < 19 && !ourArt->fitsAt(srcAOH->curHero->artifWorn, srcSlotID)) {
  3351. // Put dest artifact into owner's backpack.
  3352. ourOwner->commonInfo->srcAOH = ourOwner;
  3353. ourOwner->commonInfo->srcSlotID = ourOwner->curHero->artifacts.size() + 19;
  3354. }
  3355. LOCPLINT->cb->swapArtifacts(
  3356. srcAOH->curHero,
  3357. srcSlotID,
  3358. ourOwner->curHero,
  3359. slotID);
  3360. }
  3361. }
  3362. }
  3363. /*else if(!down && clicked)
  3364. {
  3365. if(ourArt && ourArt->id == 0)
  3366. return; //this is handled separately
  3367. deselect();
  3368. }*/
  3369. //ClickableL::clickLeft(down);
  3370. }
  3371. void CArtPlace::clickRight(tribool down, bool previousState)
  3372. {
  3373. if(ourArt && !locked() && text.size()) { //if there is no description or it's a lock, do nothing ;]
  3374. if (slotID < 19) {
  3375. selectedNo = false;
  3376. // If the artifact can be assembled, display dialog.
  3377. if (ourArt->constituentOf != NULL) {
  3378. BOOST_FOREACH(ui32 combination, *ourArt->constituentOf) {
  3379. if (ourArt->canBeAssembledTo(ourOwner->curHero->artifWorn, combination)) {
  3380. LOCPLINT->showArtifactAssemblyDialog(
  3381. ourArt->id,
  3382. combination,
  3383. true,
  3384. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, true, combination),
  3385. boost::bind(&CArtPlace::userSelectedNo, this));
  3386. if (!selectedNo)
  3387. return;
  3388. }
  3389. }
  3390. }
  3391. // Otherwise if the artifact can be diasassembled, display dialog.
  3392. if (ourArt->constituents != NULL) {
  3393. LOCPLINT->showArtifactAssemblyDialog(
  3394. ourArt->id,
  3395. 0,
  3396. false,
  3397. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, false, 0),
  3398. boost::bind(&CArtPlace::userSelectedNo, this));
  3399. if (!selectedNo)
  3400. return;
  3401. }
  3402. }
  3403. // Lastly just show the artifact description.
  3404. LRClickableAreaWTextComp::clickRight(down, previousState);
  3405. }
  3406. }
  3407. /**
  3408. * Helper function to catch when a user selects no in an artifact assembly dialog.
  3409. */
  3410. void CArtPlace::userSelectedNo ()
  3411. {
  3412. selectedNo = true;
  3413. }
  3414. /**
  3415. * Selects artifact slot so that the containing artifact looks like it's picked up.
  3416. */
  3417. void CArtPlace::select ()
  3418. {
  3419. if (locked())
  3420. return;
  3421. int backpackCorrection = -(slotID - 19 < ourOwner->backpackPos);
  3422. CGI->curh->dragAndDropCursor(graphics->artDefs->ourImages[ourArt->id].bitmap);
  3423. ourOwner->commonInfo->srcArtifact = ourArt;
  3424. ourOwner->commonInfo->srcSlotID = slotID;
  3425. ourOwner->commonInfo->srcAOH = ourOwner;
  3426. // Temporarily remove artifact from hero.
  3427. CGHeroInstance* hero = const_cast<CGHeroInstance*>(ourOwner->curHero);
  3428. if (slotID < 19)
  3429. CGI->arth->unequipArtifact(hero->artifWorn, slotID);
  3430. else
  3431. hero->artifacts.erase(hero->artifacts.begin() + (slotID - 19));
  3432. ourOwner->markPossibleSlots(ourArt);
  3433. hero->recreateArtBonuses();
  3434. // Update the hero bonuses.
  3435. CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt());
  3436. if (chw != NULL) {
  3437. chw->deactivate();
  3438. chw->setHero(hero);
  3439. chw->activate();
  3440. } else {
  3441. CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt());
  3442. assert(cew); // Either an exchange- or hero window should be active if an artifact slot is selected.
  3443. cew->deactivate();
  3444. for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  3445. {
  3446. if(cew->heroInst[g] == hero)
  3447. {
  3448. cew->artifs[g]->setHero(hero);
  3449. }
  3450. }
  3451. cew->prepareBackground();
  3452. cew->activate();
  3453. }
  3454. if (slotID >= 19)
  3455. ourOwner->scrollBackpack(backpackCorrection);
  3456. else
  3457. ourOwner->eraseSlotData(this, slotID);
  3458. }
  3459. /**
  3460. * Deselects the artifact slot. FIXME: Not used. Maybe it should?
  3461. */
  3462. void CArtPlace::deselect ()
  3463. {
  3464. CGI->curh->dragAndDropCursor(NULL);
  3465. ourOwner->unmarkSlots();
  3466. }
  3467. void CArtPlace::deactivate()
  3468. {
  3469. if(active)
  3470. {
  3471. active = false;
  3472. //deactivateLClick();
  3473. LRClickableAreaWTextComp::deactivate();
  3474. }
  3475. }
  3476. void CArtPlace::show(SDL_Surface *to)
  3477. {
  3478. if (ourArt)
  3479. blitAt(graphics->artDefs->ourImages[ourArt->id].bitmap, pos.x, pos.y, to);
  3480. if(marked && active)
  3481. {
  3482. // Draw vertical bars.
  3483. for (int i = 0; i < pos.h; ++i) {
  3484. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  3485. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  3486. }
  3487. // Draw horizontal bars.
  3488. for (int i = 0; i < pos.w; ++i) {
  3489. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  3490. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  3491. }
  3492. }
  3493. }
  3494. bool CArtPlace::fitsHere(const CArtifact * art)
  3495. {
  3496. // You can place 'no artifact' anywhere.
  3497. if(!art)
  3498. return true;
  3499. // Anything can but War Machines can be placed in backpack.
  3500. if (slotID >= 19)
  3501. return !CGI->arth->isBigArtifact(art->id);
  3502. return art->fitsAt(ourOwner->curHero->artifWorn, slotID);
  3503. }
  3504. CArtPlace::~CArtPlace()
  3505. {
  3506. deactivate();
  3507. }
  3508. void HoverableArea::hover (bool on)
  3509. {
  3510. if (on)
  3511. GH.statusbar->print(hoverText);
  3512. else if (GH.statusbar->getCurrent()==hoverText)
  3513. GH.statusbar->clear();
  3514. }
  3515. void HoverableArea::activate()
  3516. {
  3517. activateHover();
  3518. }
  3519. void HoverableArea::deactivate()
  3520. {
  3521. deactivateHover();
  3522. }
  3523. void LClickableArea::activate()
  3524. {
  3525. activateLClick();
  3526. }
  3527. void LClickableArea::deactivate()
  3528. {
  3529. deactivateLClick();
  3530. }
  3531. void LClickableArea::clickLeft(tribool down, bool previousState)
  3532. {
  3533. //if(!down)
  3534. //{
  3535. // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>());
  3536. //}
  3537. }
  3538. void RClickableArea::activate()
  3539. {
  3540. activateRClick();
  3541. }
  3542. void RClickableArea::deactivate()
  3543. {
  3544. deactivateRClick();
  3545. }
  3546. void RClickableArea::clickRight(tribool down, bool previousState)
  3547. {
  3548. //if(!down)
  3549. //{
  3550. // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector<SComponent*>());
  3551. //}
  3552. }
  3553. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  3554. {
  3555. if(!down && previousState)
  3556. {
  3557. LOCPLINT->showInfoDialog(text, std::vector<SComponent*>(), soundBase::sound_todo);
  3558. }
  3559. //ClickableL::clickLeft(down);
  3560. }
  3561. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  3562. {
  3563. adventureInt->handleRightClick(text, down, this);
  3564. }
  3565. void LRClickableAreaWText::activate()
  3566. {
  3567. LClickableArea::activate();
  3568. RClickableArea::activate();
  3569. activateHover();
  3570. }
  3571. void LRClickableAreaWText::deactivate()
  3572. {
  3573. LClickableArea::deactivate();
  3574. RClickableArea::deactivate();
  3575. deactivateHover();
  3576. }
  3577. void LClickableAreaHero::clickLeft(tribool down, bool previousState)
  3578. {
  3579. if(!down)
  3580. {
  3581. owner->deactivate();
  3582. const CGHeroInstance * buf = LOCPLINT->getWHero(id);
  3583. owner->setHero(buf);
  3584. owner->redrawCurBack();
  3585. owner->activate();
  3586. }
  3587. }
  3588. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  3589. {
  3590. if((!down) && previousState)
  3591. {
  3592. std::vector<SComponent*> comp(1, new SComponent(SComponent::Etype(baseType), type, bonus));
  3593. LOCPLINT->showInfoDialog(text, comp, soundBase::sound_todo);
  3594. }
  3595. //ClickableL::clickLeft(down);
  3596. }
  3597. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  3598. {
  3599. adventureInt->handleRightClick(text, down, this);
  3600. }
  3601. void LRClickableAreaWTextComp::activate()
  3602. {
  3603. LClickableArea::activate();
  3604. RClickableArea::activate();
  3605. activateHover();
  3606. }
  3607. void LRClickableAreaWTextComp::deactivate()
  3608. {
  3609. LClickableArea::deactivate();
  3610. RClickableArea::deactivate();
  3611. deactivateHover();
  3612. }
  3613. void LRClickableAreaOpenHero::clickLeft(tribool down, bool previousState)
  3614. {
  3615. if((!down) && previousState && hero)
  3616. LOCPLINT->openHeroWindow(hero);
  3617. }
  3618. void LRClickableAreaOpenHero::clickRight(tribool down, bool previousState)
  3619. {
  3620. if((!down) && previousState && hero)
  3621. LOCPLINT->openHeroWindow(hero);
  3622. }
  3623. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  3624. {
  3625. if((!down) && previousState && town)
  3626. LOCPLINT->openTownWindow(town);
  3627. }
  3628. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  3629. {
  3630. if((!down) && previousState && town)
  3631. LOCPLINT->openTownWindow(town);
  3632. }
  3633. void CArtifactsOfHero::activate()
  3634. {
  3635. for(size_t f=0; f<artWorn.size(); ++f)
  3636. {
  3637. if(artWorn[f])
  3638. artWorn[f]->activate();
  3639. }
  3640. for(size_t f=0; f<backpack.size(); ++f)
  3641. {
  3642. if(backpack[f])
  3643. backpack[f]->activate();
  3644. }
  3645. leftArtRoll->activate();
  3646. rightArtRoll->activate();
  3647. }
  3648. void CArtifactsOfHero::deactivate()
  3649. {
  3650. for(size_t f=0; f<artWorn.size(); ++f)
  3651. {
  3652. if(artWorn[f])
  3653. artWorn[f]->deactivate();
  3654. }
  3655. for(size_t f=0; f<backpack.size(); ++f)
  3656. {
  3657. if(backpack[f])
  3658. backpack[f]->deactivate();
  3659. }
  3660. leftArtRoll->deactivate();
  3661. rightArtRoll->deactivate();
  3662. }
  3663. void CArtifactsOfHero::show(SDL_Surface * to)
  3664. {
  3665. for(size_t d=0; d<artWorn.size(); ++d)
  3666. {
  3667. artWorn[d]->show(to);
  3668. }
  3669. for(size_t d=0; d<backpack.size(); ++d)
  3670. {
  3671. backpack[d]->show(to);
  3672. }
  3673. leftArtRoll->show(to);
  3674. rightArtRoll->show(to);
  3675. }
  3676. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  3677. {
  3678. // An update is made, rather than initialization.
  3679. if (curHero == hero) {
  3680. // Compensate backpack pos if an artifact was insertad before it.
  3681. if (commonInfo->destSlotID >= 19 && commonInfo->destAOH == this
  3682. && commonInfo->destSlotID - 19 < backpackPos)
  3683. {
  3684. backpackPos++;
  3685. }
  3686. if (updateState && commonInfo->srcAOH == this) {
  3687. curHero = hero;
  3688. // A swap was made, make the replaced artifact the current selected.
  3689. if (commonInfo->destSlotID < 19 && commonInfo->destArtifact) {
  3690. // Temporarily remove artifact from hero.
  3691. CGHeroInstance * nonconstCurHero = const_cast<CGHeroInstance *>(curHero);
  3692. if (commonInfo->srcSlotID < 19)
  3693. CGI->arth->unequipArtifact(nonconstCurHero->artifWorn, commonInfo->srcSlotID);
  3694. else
  3695. nonconstCurHero->artifacts.erase(nonconstCurHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  3696. nonconstCurHero->recreateArtBonuses();
  3697. // Source <- Dest
  3698. commonInfo->srcArtifact = commonInfo->destArtifact;
  3699. // Reset destination parameters.
  3700. commonInfo->destAOH = NULL;
  3701. commonInfo->destArtifact = NULL;
  3702. commonInfo->destSlotID = -1;
  3703. CGI->curh->dragAndDropCursor(
  3704. graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  3705. markPossibleSlots(commonInfo->srcArtifact);
  3706. } else if (commonInfo->destAOH != NULL) {
  3707. // Reset all parameters.
  3708. commonInfo->srcAOH = NULL;
  3709. commonInfo->srcArtifact = NULL;
  3710. commonInfo->srcSlotID = -1;
  3711. commonInfo->destAOH = NULL;
  3712. commonInfo->destArtifact = NULL;
  3713. commonInfo->destSlotID = -1;
  3714. CGI->curh->dragAndDropCursor(NULL);
  3715. unmarkSlots();
  3716. }
  3717. }
  3718. } else {
  3719. rollback();
  3720. }
  3721. curHero = hero;
  3722. if (curHero->artifacts.size() > 0)
  3723. backpackPos %= curHero->artifacts.size();
  3724. else
  3725. backpackPos = 0;
  3726. // Fill the slots for worn artifacts and backpack.
  3727. for (int g = 0; g < 19 ; g++)
  3728. setSlotData(artWorn[g], g);
  3729. scrollBackpack(0);
  3730. //blocking scrolling if there is not enough artifacts to scroll
  3731. leftArtRoll->block(curHero->artifacts.size() <= backpack.size());
  3732. rightArtRoll->block(curHero->artifacts.size() <= backpack.size());
  3733. }
  3734. /**
  3735. * Any held artifacts, marked slots etc. will be restored to it's original way.
  3736. */
  3737. void CArtifactsOfHero::rollback()
  3738. {
  3739. if (curHero != NULL) {
  3740. // Restore any held artifact to it's original position.
  3741. if (commonInfo->srcArtifact && commonInfo->srcAOH == this) {
  3742. CGHeroInstance * hero = const_cast<CGHeroInstance *>(curHero);
  3743. if (commonInfo->srcSlotID != -1) {
  3744. // Put a held artifact back to it's spot.
  3745. if (commonInfo->srcSlotID < 19)
  3746. CGI->arth->equipArtifact(hero->artifWorn, commonInfo->srcSlotID, commonInfo->srcArtifact->id);
  3747. else
  3748. hero->artifacts.insert(hero->artifacts.begin() + (commonInfo->srcSlotID - 19), commonInfo->srcArtifact->id);
  3749. } else { // Held swapped artifact.
  3750. // Wear the artifact in a suitable spot.
  3751. ui16 i = 0;
  3752. for (; i < 19; i++) {
  3753. if (artWorn[i]->fitsHere(commonInfo->srcArtifact)
  3754. && curHero->artifWorn.find(i) == curHero->artifWorn.end())
  3755. {
  3756. CGI->arth->equipArtifact(hero->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. hero->artifacts.push_back(commonInfo->srcArtifact->id);
  3763. }
  3764. hero->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 LRClickableAreaWTextComp();
  4129. morale[b]->pos = genRect(32, 32, pos.x + 177 + 490*b, pos.y + 45);
  4130. std::vector<std::pair<int,std::string> > mrl = heroInst[b]->getCurrentMoraleModifiers();
  4131. int mrlv = heroInst[b]->getCurrentMorale();
  4132. int mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad morale[b], 0 - neutral, 1 - good
  4133. morale[b]->hoverText = CGI->generaltexth->heroscrn[4 - mrlt];
  4134. morale[b]->baseType = SComponent::morale;
  4135. morale[b]->bonus = mrlv;
  4136. morale[b]->text = CGI->generaltexth->arraytxt[88];
  4137. boost::algorithm::replace_first(morale[b]->text,"%s",CGI->generaltexth->arraytxt[86-mrlt]);
  4138. if (!mrl.size())
  4139. morale[b]->text += CGI->generaltexth->arraytxt[108];
  4140. else
  4141. for(int it=0; it < mrl.size(); it++)
  4142. morale[b]->text += "\n" + mrl[it].second;
  4143. //setting luck
  4144. luck[b] = new LRClickableAreaWTextComp();
  4145. luck[b]->pos = genRect(32, 32, pos.x + 213 + 490*b, pos.y + 45);
  4146. mrl = heroInst[b]->getCurrentLuckModifiers();
  4147. mrlv = heroInst[b]->getCurrentLuck();
  4148. mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad luck[b], 0 - neutral, 1 - good
  4149. luck[b]->hoverText = CGI->generaltexth->heroscrn[7 - mrlt];
  4150. luck[b]->baseType = SComponent::luck;
  4151. luck[b]->bonus = mrlv;
  4152. luck[b]->text = CGI->generaltexth->arraytxt[62];
  4153. boost::algorithm::replace_first(luck[b]->text,"%s",CGI->generaltexth->arraytxt[60-mrlt]);
  4154. if (!mrl.size())
  4155. luck[b]->text += CGI->generaltexth->arraytxt[77];
  4156. else
  4157. for(int it=0; it < mrl.size(); it++)
  4158. luck[b]->text += "\n" + mrl[it].second;
  4159. }
  4160. //buttons
  4161. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), pos.x+732, pos.y+567, "IOKAY.DEF", SDLK_RETURN);
  4162. questlogButton[0] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 0), pos.x+10, pos.y+44, "hsbtns4.def");
  4163. questlogButton[1] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 1), pos.x+740, pos.y+44, "hsbtns4.def");
  4164. //statusbar
  4165. ourBar = new CStatusBar(pos.x + 3, pos.y + 577, "TSTATBAR.bmp", 726);
  4166. //garrison interface
  4167. garr = new CGarrisonInt(pos.x + 69, pos.y + 131, 4, Point(418,0), bg, Point(69,131), heroInst[0],heroInst[1], true, true);
  4168. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+10,pos.y+132,"TSBTNS.DEF"));
  4169. garr->splitButtons.push_back(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+740,pos.y+132,"TSBTNS.DEF"));
  4170. }
  4171. CExchangeWindow::~CExchangeWindow() //d-tor
  4172. {
  4173. SDL_FreeSurface(bg);
  4174. delete quit;
  4175. //warning: don't experiment with these =NULL lines, they prevent heap corruption!
  4176. artifs[0]->rollback();
  4177. artifs[1]->rollback();
  4178. delete artifs[0]->commonInfo;
  4179. artifs[0]->commonInfo = NULL;
  4180. delete artifs[0];
  4181. artifs[1]->commonInfo = NULL;
  4182. delete artifs[1];
  4183. delete garr;
  4184. delete ourBar;
  4185. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4186. {
  4187. for(int b=0; b<secSkillAreas[g].size(); ++b)
  4188. {
  4189. delete secSkillAreas[g][b];
  4190. }
  4191. }
  4192. for(int b=0; b<primSkillAreas.size(); ++b)
  4193. {
  4194. delete primSkillAreas[b];
  4195. }
  4196. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  4197. {
  4198. delete questlogButton[g];
  4199. }
  4200. for(int g=0; g<ARRAY_COUNT(morale); g++)
  4201. delete morale[g];
  4202. for(int g=0; g<ARRAY_COUNT(luck); g++)
  4203. delete luck[g];
  4204. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  4205. delete portrait[g];
  4206. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  4207. delete spellPoints[g];
  4208. for(int g=0; g<ARRAY_COUNT(experience); g++)
  4209. delete experience[g];
  4210. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  4211. delete speciality[g];
  4212. }
  4213. void CShipyardWindow::activate()
  4214. {
  4215. build->activate();
  4216. quit->activate();
  4217. }
  4218. void CShipyardWindow::deactivate()
  4219. {
  4220. build->deactivate();
  4221. quit->deactivate();
  4222. }
  4223. void CShipyardWindow::show( SDL_Surface * to )
  4224. {
  4225. blitAt(bg,pos,to);
  4226. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->boatAnims[boat]->ourImages[21 + frame++/8%8].bitmap, NULL, to, &genRect(64, 96, pos.x+110, pos.y+85));
  4227. build->show(to);
  4228. quit->show(to);
  4229. }
  4230. CShipyardWindow::~CShipyardWindow()
  4231. {
  4232. delete build;
  4233. delete quit;
  4234. }
  4235. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy)
  4236. {
  4237. boat = boatType;
  4238. frame = 0;
  4239. SDL_Surface * bgtemp; //loaded as 8bpp surface
  4240. bgtemp = BitmapHandler::loadBitmap("TPSHIP.bmp");
  4241. pos.x = screen->w/2 - bgtemp->w/2;
  4242. pos.y = screen->h/2 - bgtemp->h/2;
  4243. pos.w = bgtemp->w;
  4244. pos.h = bgtemp->h;
  4245. SDL_SetColorKey(bgtemp,SDL_SRCCOLORKEY,SDL_MapRGB(bgtemp->format,0,255,255));
  4246. graphics->blueToPlayersAdv(bgtemp, LOCPLINT->playerID);
  4247. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  4248. SDL_FreeSurface(bgtemp);
  4249. bgtemp = BitmapHandler::loadBitmap("TPSHIPBK.bmp");
  4250. blitAt(bgtemp, 100, 69, bg);
  4251. SDL_FreeSurface(bgtemp);
  4252. // Draw resource icons and costs.
  4253. std::string goldCost = boost::lexical_cast<std::string>(1000);
  4254. std::string woodCost = boost::lexical_cast<std::string>(10);
  4255. blitAt(graphics->resources32->ourImages[6].bitmap, 100, 244, bg);
  4256. printAtMiddle(goldCost.c_str(), 118, 294, FONT_SMALL, zwykly, bg);
  4257. blitAt(graphics->resources32->ourImages[0].bitmap, 196, 244, bg);
  4258. printAtMiddle(woodCost.c_str(), 212, 294, FONT_SMALL, zwykly, bg);
  4259. bool affordable = true;
  4260. for(int i = 0; i < cost.size(); i++)
  4261. {
  4262. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  4263. {
  4264. affordable = false;
  4265. break;
  4266. }
  4267. }
  4268. quit = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+224, pos.y+312, "ICANCEL.DEF", SDLK_RETURN);
  4269. build = new AdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+42, pos.y+312, "IBY6432.DEF", SDLK_RETURN);
  4270. build->callback += onBuy;
  4271. if(!affordable)
  4272. build->block(true);
  4273. printAtMiddle(CGI->generaltexth->jktexts[13], 164, 27, FONT_BIG, tytulowy, bg); //Build A New Ship
  4274. printAtMiddle(CGI->generaltexth->jktexts[14], 164, 220, FONT_MEDIUM, zwykly, bg); //Resource cost:
  4275. }
  4276. CPuzzleWindow::CPuzzleWindow(const int3 &grailPos, float discoveredRatio)
  4277. :animCount(0)
  4278. {
  4279. CDefHandler * arrows = CDefHandler::giveDef("ADAG.DEF");
  4280. alphaTransform(arrows->ourImages[0].bitmap);
  4281. SDL_Surface * back = BitmapHandler::loadBitmap("PUZZLE.BMP", false);
  4282. graphics->blueToPlayersAdv(back, LOCPLINT->playerID);
  4283. //make transparency black
  4284. back->format->palette->colors[0].b = back->format->palette->colors[0].r = back->format->palette->colors[0].g = 0;
  4285. //the rest
  4286. background = SDL_ConvertSurface(back, screen->format, back->flags);
  4287. SDL_FreeSurface(back);
  4288. pos = genRect(background->h, background->w, (conf.cc.resx - background->w) / 2, (conf.cc.resy - background->h) / 2);
  4289. quitb = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+670, pos.y+538, "IOK6432.DEF", SDLK_RETURN);
  4290. resdatabar = new CResDataBar("ZRESBAR.bmp", pos.x+3, pos.y+575, 32, 2, 85, 85);
  4291. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  4292. //printing necessary things to background
  4293. int3 moveInt = int3(8, 9, 0);
  4294. CGI->mh->terrainRect
  4295. (grailPos - moveInt, adventureInt->anim,
  4296. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  4297. background, &genRect(544, 591, 8, 8), 0, 0, true);
  4298. //printing X sign
  4299. {
  4300. int x = 32*moveInt.x + 8,
  4301. y = 32*moveInt.y + 8;
  4302. if (x<0 || y<0 || x>pos.w || y>pos.h)
  4303. {
  4304. }
  4305. else
  4306. {
  4307. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[0].bitmap, NULL, background, &genRect(32, 32, x, y));
  4308. }
  4309. }
  4310. delete arrows;
  4311. int faction = LOCPLINT->cb->getStartInfo()->playerInfos[LOCPLINT->serialID].castle;
  4312. std::vector<SPuzzleInfo> puzzlesToPrint;
  4313. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  4314. {
  4315. if(CGI->heroh->puzzleInfo[faction][g].whenUncovered > PUZZLES_PER_FACTION * discoveredRatio)
  4316. {
  4317. puzzlesToPrint.push_back(CGI->heroh->puzzleInfo[faction][g]);
  4318. }
  4319. else
  4320. {
  4321. SDL_Surface *buf = BitmapHandler::loadBitmap(CGI->heroh->puzzleInfo[faction][g].filename);
  4322. puzzlesToPullBack.push_back( std::make_pair(buf, &CGI->heroh->puzzleInfo[faction][g]) );
  4323. }
  4324. }
  4325. for(int b = 0; b < puzzlesToPrint.size(); ++b)
  4326. {
  4327. SDL_Surface * puzzle = BitmapHandler::loadBitmap(puzzlesToPrint[b].filename);
  4328. blitAt(puzzle, puzzlesToPrint[b].x, puzzlesToPrint[b].y, background);
  4329. SDL_FreeSurface(puzzle);
  4330. }
  4331. }
  4332. CPuzzleWindow::~CPuzzleWindow()
  4333. {
  4334. delete quitb;
  4335. delete resdatabar;
  4336. SDL_FreeSurface(background);
  4337. for(int g = 0; g < puzzlesToPullBack.size(); ++g)
  4338. SDL_FreeSurface( puzzlesToPullBack[g].first );
  4339. }
  4340. void CPuzzleWindow::activate()
  4341. {
  4342. quitb->activate();
  4343. }
  4344. void CPuzzleWindow::deactivate()
  4345. {
  4346. quitb->deactivate();
  4347. }
  4348. void CPuzzleWindow::show(SDL_Surface * to)
  4349. {
  4350. blitAt(background, pos.x, pos.y, to);
  4351. quitb->show(to);
  4352. resdatabar->draw(to);
  4353. //blitting disappearing puzzles
  4354. for(int g=0; g<2; ++g)
  4355. if(animCount != 255)
  4356. ++animCount;
  4357. if(animCount != 255)
  4358. {
  4359. for(int b = 0; b < puzzlesToPullBack.size(); ++b)
  4360. {
  4361. int xPos = puzzlesToPullBack[b].second->x + pos.x,
  4362. yPos = puzzlesToPullBack[b].second->y + pos.y;
  4363. SDL_Surface *from = puzzlesToPullBack[b].first;
  4364. SDL_SetAlpha(from, SDL_SRCALPHA, 255 - animCount);
  4365. blitAt(from, xPos, yPos, to);
  4366. }
  4367. }
  4368. //disappearing puzzles blitted
  4369. //printing border around window
  4370. if(screen->w != 800 || screen->h !=600)
  4371. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  4372. }
  4373. bool CShopWindow::swapItem (ui16 which, bool choose)
  4374. {
  4375. bool itemFound = false;
  4376. if (choose == true) //choose item
  4377. {
  4378. if (chosen.count(which))
  4379. {
  4380. itemFound = true;
  4381. chosen[which] = available[which];
  4382. available.erase(which);
  4383. }
  4384. }
  4385. else //return item to available list
  4386. {
  4387. if (available.count(which))
  4388. {
  4389. itemFound = true;
  4390. available[which] = chosen[which];
  4391. chosen.erase(which);
  4392. }
  4393. }
  4394. return itemFound;
  4395. }
  4396. CArtMerchantWindow::CArtMerchantWindow()
  4397. {
  4398. }
  4399. CArtMerchantWindow::~CArtMerchantWindow(){}
  4400. void CArtMerchantWindow::activate() {}
  4401. void CArtMerchantWindow::deactivate() {}
  4402. void CArtMerchantWindow::show(SDL_Surface * to) {}
  4403. void CArtMerchantWindow::Buy() {}
  4404. void CThievesGuildWindow::activate()
  4405. {
  4406. CIntObject::activate();
  4407. GH.statusbar = statusBar;
  4408. }
  4409. void CThievesGuildWindow::show(SDL_Surface * to)
  4410. {
  4411. blitAt(background, pos.x, pos.y, to);
  4412. statusBar->show(to);
  4413. exitb->show(to);
  4414. resdatabar->show(to);
  4415. //showing border around window
  4416. if(screen->w != 800 || screen->h !=600)
  4417. {
  4418. CMessage::drawBorder(LOCPLINT->playerID, to, pos.w + 28, pos.h + 28, pos.x-14, pos.y-15);
  4419. }
  4420. }
  4421. void CThievesGuildWindow::bexitf()
  4422. {
  4423. GH.popIntTotally(this);
  4424. }
  4425. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner)
  4426. :owner(_owner)
  4427. {
  4428. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4429. SThievesGuildInfo tgi; //info to be displayed
  4430. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  4431. pos = center(Rect(0,0,800,600));// Rect( (conf.cc.resx - 800) / 2, (conf.cc.resy - 600) / 2, 800, 600 );
  4432. //loading backround and converting to more bpp form
  4433. SDL_Surface * bg = background = BitmapHandler::loadBitmap("TpRank.bmp", false);
  4434. background = newSurface(bg->w, bg->h);
  4435. blitAt(bg, 0, 0, background);
  4436. SDL_FreeSurface(bg);
  4437. exitb = new AdventureMapButton (std::string(), std::string(), boost::bind(&CThievesGuildWindow::bexitf,this), 748, 556, "HSBTNS.def", SDLK_RETURN);
  4438. statusBar = new CStatusBar(3, 555, "TStatBar.bmp", 742);
  4439. resdatabar = new CMinorResDataBar();
  4440. resdatabar->pos.x += pos.x - 3;
  4441. resdatabar->pos.y += pos.y;
  4442. static std::vector< std::list< ui8 > > SThievesGuildInfo::* fields[] = { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  4443. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks, &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army,
  4444. &SThievesGuildInfo::income};
  4445. //printing texts & descriptions to background
  4446. for(int g=0; g<12; ++g)
  4447. {
  4448. int y;
  4449. if(g == 9) //best hero
  4450. {
  4451. y = 400;
  4452. }
  4453. else if(g == 10) //personality
  4454. {
  4455. y = 460;
  4456. }
  4457. else if(g == 11) //best monster
  4458. {
  4459. y = 510;
  4460. }
  4461. else
  4462. {
  4463. y = 52 + 32*g;
  4464. }
  4465. printAtMiddle(CGI->generaltexth->jktexts[24+g], 135, y, FONT_MEDIUM, tytulowy, background);
  4466. }
  4467. CDefHandler * strips = CDefHandler::giveDef("PRSTRIPS.DEF");
  4468. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRPINK.BMP"};
  4469. for(int g=0; g<tgi.playerColors.size(); ++g)
  4470. {
  4471. if(g > 0)
  4472. {
  4473. blitAt(strips->ourImages[g-1].bitmap, 250 + 66*g, 7, background);
  4474. }
  4475. printAtMiddle(CGI->generaltexth->jktexts[16+g], 283 + 66*g, 20, FONT_MEDIUM, tytulowy, background);
  4476. SDL_Surface * box = BitmapHandler::loadBitmap(colorToBox[tgi.playerColors[g]]);
  4477. blitAt(box, 253 + 66*g, 334, background);
  4478. SDL_FreeSurface(box);
  4479. }
  4480. delete strips;
  4481. CDefHandler * flagPictures = CDefHandler::giveDef("itgflags.def");
  4482. //printing flags
  4483. for(int g=0; g<ARRAY_COUNT(fields); ++g) //by lines
  4484. {
  4485. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  4486. {
  4487. std::list<ui8> players = (tgi .* fields[g])[b]; //get players with this place in this line
  4488. //std::sort(players.begin(), players.end());
  4489. int counter = 0;
  4490. for(std::list<ui8>::const_iterator it = players.begin(); it != players.end(); ++it)
  4491. {
  4492. int xpos = 259 + 66 * b + 12 * (counter % 4) + 6 * (counter / 4);
  4493. int ypos = 41 + 32 * g + 4 * (counter / 4);
  4494. blitAt(flagPictures->ourImages[*it].bitmap, xpos, ypos, background);
  4495. counter++;
  4496. }
  4497. }
  4498. }
  4499. delete flagPictures;
  4500. flagPictures = NULL;
  4501. //printing best hero
  4502. int counter = 0;
  4503. for(std::map<ui8, InfoAboutHero>::const_iterator it = tgi.colorToBestHero.begin(); it != tgi.colorToBestHero.end(); ++it)
  4504. {
  4505. blitAt(graphics->portraitSmall[it->second.portrait], 260 + 66 * counter, 360, background);
  4506. counter++;
  4507. //printing stats
  4508. if(it->second.details)
  4509. {
  4510. for (int i=0; i<it->second.details->primskills.size(); ++i)
  4511. {
  4512. printAt(CGI->generaltexth->allTexts[380+i], 191 + 66*counter, 394 + 11*i, FONT_SMALL, zwykly, background);
  4513. std::ostringstream skill;
  4514. skill << it->second.details->primskills[i];
  4515. printTo(skill.str(), 244 + 66 * counter, 410 + 11*i, FONT_SMALL, zwykly, background);
  4516. }
  4517. }
  4518. }
  4519. //printing best creature
  4520. counter = 0;
  4521. for(std::map<ui8, si32>::const_iterator it = tgi.bestCreature.begin(); it != tgi.bestCreature.end(); ++it)
  4522. {
  4523. blitAt(graphics->bigImgs[it->second], 255 + 66 * counter, 479, background);
  4524. counter++;
  4525. }
  4526. //printing personality
  4527. counter = 0;
  4528. for(std::map<ui8, si8>::const_iterator it = tgi.personality.begin(); it != tgi.personality.end(); ++it)
  4529. {
  4530. int toPrint = 0;
  4531. if(it->second == -1)
  4532. {
  4533. toPrint = 172;
  4534. }
  4535. else
  4536. {
  4537. toPrint = 168 + it->second;
  4538. }
  4539. printAtMiddle(CGI->generaltexth->arraytxt[toPrint], 283 + 66*counter, 459, FONT_SMALL, zwykly, background);
  4540. counter++;
  4541. }
  4542. }
  4543. CThievesGuildWindow::~CThievesGuildWindow()
  4544. {
  4545. SDL_FreeSurface(background);
  4546. // delete exitb;
  4547. // delete statusBar;
  4548. // delete resdatabar;
  4549. }
  4550. void MoraleLuckBox::set( bool morale, const CGHeroInstance *hero, int slot /*= -1*/ )
  4551. {
  4552. int mrlv = -9, mrlt = -9;
  4553. std::vector<std::pair<int,std::string> > mrl;
  4554. if(morale)
  4555. {
  4556. //setting morale
  4557. mrl = hero->getCurrentMoraleModifiers();
  4558. mrlv = hero->getCurrentMorale();
  4559. mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad morale, 0 - neutral, 1 - good
  4560. hoverText = CGI->generaltexth->heroscrn[4 - mrlt];
  4561. baseType = SComponent::morale;
  4562. bonus = mrlv;
  4563. text = CGI->generaltexth->arraytxt[88];
  4564. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[86-mrlt]);
  4565. if (!mrl.size())
  4566. text += CGI->generaltexth->arraytxt[108];
  4567. else
  4568. for(int it=0; it < mrl.size(); it++)
  4569. text += "\n" + mrl[it].second;
  4570. }
  4571. else
  4572. {
  4573. //setting luck
  4574. mrl = hero->getCurrentLuckModifiers();
  4575. mrlv = hero->getCurrentLuck();
  4576. mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad luck, 0 - neutral, 1 - good
  4577. hoverText = CGI->generaltexth->heroscrn[7 - mrlt];
  4578. baseType = SComponent::luck;
  4579. bonus = mrlv;
  4580. text = CGI->generaltexth->arraytxt[62];
  4581. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[60-mrlt]);
  4582. if (!mrl.size())
  4583. text += CGI->generaltexth->arraytxt[77];
  4584. else
  4585. for(int it=0; it < mrl.size(); it++)
  4586. text += "\n" + mrl[it].second;
  4587. }
  4588. }
  4589. void CLabel::showAll(SDL_Surface * to)
  4590. {
  4591. CIntObject::showAll(to);
  4592. if(!text.length())
  4593. return;
  4594. 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
  4595. printer[alignment](text, pos.x + textOffset.x, pos.y + textOffset.y, font, color, to, false);
  4596. }
  4597. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= zwykly*/, const std::string &Text /*= ""*/)
  4598. :font(Font), color(Color), text(Text), alignment(Align)
  4599. {
  4600. autoRedraw = true;
  4601. pos.x += x;
  4602. pos.y += y;
  4603. pos.w = pos.h = 0;
  4604. bg = NULL;
  4605. }
  4606. void CLabel::setTxt(const std::string &Txt)
  4607. {
  4608. text = Txt;
  4609. if(autoRedraw)
  4610. {
  4611. if(bg || !parent)
  4612. redraw();
  4613. else
  4614. parent->redraw();
  4615. }
  4616. }
  4617. void CGStatusBar::print(const std::string & Text)
  4618. {
  4619. setTxt(Text);
  4620. }
  4621. void CGStatusBar::clear()
  4622. {
  4623. setTxt("");
  4624. }
  4625. std::string CGStatusBar::getCurrent()
  4626. {
  4627. return text;
  4628. }
  4629. CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= zwykly*/, const std::string &Text /*= ""*/)
  4630. : CLabel(x, y, Font, Align, Color, Text)
  4631. {
  4632. init();
  4633. }
  4634. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= zwykly*/)
  4635. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  4636. {
  4637. init();
  4638. bg = BG;
  4639. moveChildren(bg, bg->parent, this);
  4640. pos = bg->pos;
  4641. switch(Align)
  4642. {
  4643. case CENTER:
  4644. textOffset = Point(pos.w/2, pos.h/2);
  4645. break;
  4646. case BOTTOMRIGHT:
  4647. textOffset = Point(pos.w, pos.h);
  4648. break;
  4649. }
  4650. }
  4651. CGStatusBar::~CGStatusBar()
  4652. {
  4653. GH.statusbar = oldStatusBar;
  4654. }
  4655. void CGStatusBar::show(SDL_Surface * to)
  4656. {
  4657. }
  4658. void CGStatusBar::init()
  4659. {
  4660. oldStatusBar = GH.statusbar;
  4661. GH.statusbar = this;
  4662. }
  4663. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  4664. :cb(CB)
  4665. {
  4666. focus = false;
  4667. pos += Pos;
  4668. OBJ_CONSTRUCTION;
  4669. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  4670. used = LCLICK | KEYBOARD;
  4671. giveFocus();
  4672. }
  4673. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  4674. {
  4675. focus = false;
  4676. pos += Pos;
  4677. OBJ_CONSTRUCTION;
  4678. bg = new CPicture(Pos, 0, true);
  4679. Rect hlp = Pos;
  4680. SDL_BlitSurface(srf, &hlp, *bg, NULL);
  4681. pos.w = bg->pos.w;
  4682. pos.h = bg->pos.h;
  4683. bg->pos = pos;
  4684. used = LCLICK | KEYBOARD;
  4685. giveFocus();
  4686. }
  4687. void CTextInput::showAll( SDL_Surface * to )
  4688. {
  4689. CIntObject::showAll(to);
  4690. const std::string toPrint = focus ? text + "_" : text;
  4691. CSDL_Ext::printAt(toPrint, pos.x, pos.y, FONT_SMALL, zwykly, to);
  4692. }
  4693. void CTextInput::clickLeft( tribool down, bool previousState )
  4694. {
  4695. if(down && !focus)
  4696. giveFocus();
  4697. }
  4698. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  4699. {
  4700. if(!focus || key.state != SDL_PRESSED)
  4701. return;
  4702. if(key.keysym.sym == SDLK_TAB)
  4703. {
  4704. moveFocus();
  4705. GH.current = NULL;
  4706. return;
  4707. }
  4708. switch(key.keysym.sym)
  4709. {
  4710. case SDLK_BACKSPACE:
  4711. if(text.size())
  4712. text.resize(text.size()-1);
  4713. break;
  4714. default:
  4715. char c = key.keysym.unicode; //TODO 16-/>8
  4716. static const std::string forbiddenChars = "<>:\"/\\|?*"; //if we are entering a filename, some special characters won't be allowed
  4717. if(!vstd::contains(forbiddenChars,c) && std::isprint(c))
  4718. text += c;
  4719. break;
  4720. }
  4721. redraw();
  4722. cb(text);
  4723. }
  4724. void CTextInput::setText( const std::string &nText, bool callCb )
  4725. {
  4726. text = nText;
  4727. redraw();
  4728. if(callCb)
  4729. cb(text);
  4730. }
  4731. CTextInput::~CTextInput()
  4732. {
  4733. }
  4734. CFocusable::CFocusable()
  4735. {
  4736. focusables.push_back(this);
  4737. }
  4738. CFocusable::~CFocusable()
  4739. {
  4740. if(inputWithFocus == this)
  4741. inputWithFocus = NULL;
  4742. focusables -= this;
  4743. }
  4744. void CFocusable::giveFocus()
  4745. {
  4746. if(inputWithFocus)
  4747. {
  4748. inputWithFocus->focus = false;
  4749. inputWithFocus->redraw();
  4750. }
  4751. focus = true;
  4752. inputWithFocus = this;
  4753. redraw();
  4754. }
  4755. void CFocusable::moveFocus()
  4756. {
  4757. std::list<CFocusable*>::iterator i = vstd::find(focusables, this),
  4758. ourIt = i;
  4759. for(i++; i != ourIt; i++)
  4760. {
  4761. if(i == focusables.end())
  4762. i = focusables.begin();
  4763. if((*i)->active)
  4764. {
  4765. (*i)->giveFocus();
  4766. break;;
  4767. }
  4768. }
  4769. }