GUIClasses.cpp 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364
  1. #include "StdInc.h"
  2. #include "GUIClasses.h"
  3. #include "gui/SDL_Extensions.h"
  4. #include "CAdvmapInterface.h"
  5. #include "CBitmapHandler.h"
  6. #include "CDefHandler.h"
  7. #include "battle/CBattleInterface.h"
  8. #include "battle/CBattleInterfaceClasses.h"
  9. #include "../CCallback.h"
  10. #include "CCastleInterface.h"
  11. #include "CCreatureWindow.h"
  12. #include "gui/CCursorHandler.h"
  13. #include "CGameInfo.h"
  14. #include "CHeroWindow.h"
  15. #include "CMessage.h"
  16. #include "../lib/CConfigHandler.h"
  17. #include "battle/CCreatureAnimation.h"
  18. #include "CPlayerInterface.h"
  19. #include "Graphics.h"
  20. #include "CAnimation.h"
  21. #include "../lib/CArtHandler.h"
  22. #include "../lib/CBuildingHandler.h"
  23. #include "../lib/CGeneralTextHandler.h"
  24. #include "../lib/CHeroHandler.h"
  25. #include "../lib/CModHandler.h"
  26. #include "../lib/CSpellHandler.h"
  27. #include "../lib/CTownHandler.h"
  28. #include "../lib/CondSh.h"
  29. #include "../lib/mapping/CMap.h"
  30. #include "mapHandler.h"
  31. #include "../lib/CStopWatch.h"
  32. #include "../lib/NetPacksBase.h"
  33. #include "CSpellWindow.h"
  34. #include "CHeroWindow.h"
  35. #include "CVideoHandler.h"
  36. #include "../lib/StartInfo.h"
  37. #include "CPreGame.h"
  38. #include "../lib/HeroBonus.h"
  39. #include "../lib/CCreatureHandler.h"
  40. #include "CMusicHandler.h"
  41. #include "../lib/BattleState.h"
  42. #include "../lib/CGameState.h"
  43. #include "../lib/GameConstants.h"
  44. #include "gui/CGuiHandler.h"
  45. /*
  46. * GUIClasses.cpp, part of VCMI engine
  47. *
  48. * Authors: listed in file AUTHORS in main folder
  49. *
  50. * License: GNU General Public License v2.0 or later
  51. * Full text of license available in license.txt file, in main folder
  52. *
  53. */
  54. using namespace boost::assign;
  55. using namespace CSDL_Ext;
  56. extern std::queue<SDL_Event> events;
  57. extern boost::mutex eventsM;
  58. std::list<CFocusable*> CFocusable::focusables;
  59. CFocusable * CFocusable::inputWithFocus;
  60. #undef min
  61. #undef max
  62. void CSelWindow::selectionChange(unsigned to)
  63. {
  64. for (unsigned i=0;i<components.size();i++)
  65. {
  66. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  67. if (!pom)
  68. continue;
  69. pom->select(i==to);
  70. }
  71. redraw();
  72. }
  73. CSelWindow::CSelWindow(const std::string &Text, PlayerColor player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, QueryID askID)
  74. {
  75. OBJ_CONSTRUCTION_CAPTURING_ALL;
  76. ID = askID;
  77. for(int i=0;i<Buttons.size();i++)
  78. {
  79. buttons.push_back(new CAdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  80. if(!i && askID.getNum() >= 0)
  81. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  82. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  83. }
  84. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  85. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  86. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  87. if(buttons.size() > 1 && askID.getNum() >= 0) //cancel button functionality
  88. buttons.back()->callback += boost::bind(&CCallback::selectionMade,LOCPLINT->cb.get(),0,askID);
  89. for(int i=0;i<comps.size();i++)
  90. {
  91. comps[i]->recActions = 255;
  92. addChild(comps[i]);
  93. components.push_back(comps[i]);
  94. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  95. if(i<9)
  96. comps[i]->assignedKeys.insert(SDLK_1+i);
  97. }
  98. CMessage::drawIWindow(this, Text, player);
  99. }
  100. void CSelWindow::madeChoice()
  101. {
  102. if(ID.getNum() < 0)
  103. return;
  104. int ret = -1;
  105. for (int i=0;i<components.size();i++)
  106. {
  107. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  108. {
  109. ret = i;
  110. }
  111. }
  112. LOCPLINT->cb->selectionMade(ret+1,ID);
  113. }
  114. void CArmyTooltip::init(const InfoAboutArmy &army)
  115. {
  116. OBJ_CONSTRUCTION_CAPTURING_ALL;
  117. new CLabel(66, 2, FONT_SMALL, TOPLEFT, Colors::WHITE, army.name);
  118. std::vector<Point> slotsPos;
  119. slotsPos.push_back(Point(36,73));
  120. slotsPos.push_back(Point(72,73));
  121. slotsPos.push_back(Point(108,73));
  122. slotsPos.push_back(Point(18,122));
  123. slotsPos.push_back(Point(54,122));
  124. slotsPos.push_back(Point(90,122));
  125. slotsPos.push_back(Point(126,122));
  126. for(auto & slot : army.army)
  127. {
  128. if(slot.first.getNum() >= GameConstants::ARMY_SIZE)
  129. {
  130. logGlobal->warnStream() << "Warning: " << army.name << " has stack in slot " << slot.first;
  131. continue;
  132. }
  133. new CAnimImage("CPRSMALL", slot.second.type->iconIndex, 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y);
  134. std::string subtitle;
  135. if(army.army.isDetailed)
  136. subtitle = boost::lexical_cast<std::string>(slot.second.count);
  137. else
  138. {
  139. //if =0 - we have no information about stack size at all
  140. if (slot.second.count)
  141. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  142. }
  143. new CLabel(slotsPos[slot.first.getNum()].x + 17, slotsPos[slot.first.getNum()].y + 41, FONT_TINY, CENTER, Colors::WHITE, subtitle);
  144. }
  145. }
  146. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy &army):
  147. CIntObject(0, pos)
  148. {
  149. init(army);
  150. }
  151. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  152. CIntObject(0, pos)
  153. {
  154. init(InfoAboutArmy(army, true));
  155. }
  156. void CHeroTooltip::init(const InfoAboutHero &hero)
  157. {
  158. OBJ_CONSTRUCTION_CAPTURING_ALL;
  159. new CAnimImage("PortraitsLarge", hero.portrait, 0, 3, 2);
  160. if(hero.details)
  161. {
  162. for (size_t i = 0; i < hero.details->primskills.size(); i++)
  163. new CLabel(75 + 28 * i, 58, FONT_SMALL, CENTER, Colors::WHITE,
  164. boost::lexical_cast<std::string>(hero.details->primskills[i]));
  165. new CLabel(158, 98, FONT_TINY, CENTER, Colors::WHITE,
  166. boost::lexical_cast<std::string>(hero.details->mana));
  167. new CAnimImage("IMRL22", hero.details->morale + 3, 0, 5, 74);
  168. new CAnimImage("ILCK22", hero.details->luck + 3, 0, 5, 91);
  169. }
  170. }
  171. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  172. CArmyTooltip(pos, hero)
  173. {
  174. init(hero);
  175. }
  176. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  177. CArmyTooltip(pos, InfoAboutHero(hero, true))
  178. {
  179. init(InfoAboutHero(hero, true));
  180. }
  181. void CTownTooltip::init(const InfoAboutTown &town)
  182. {
  183. OBJ_CONSTRUCTION_CAPTURING_ALL;
  184. //order of icons in def: fort, citadel, castle, no fort
  185. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  186. new CAnimImage("ITMCLS", fortIndex, 0, 105, 31);
  187. assert(town.tType);
  188. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  189. new CAnimImage("itpt", iconIndex, 0, 3, 2);
  190. if(town.details)
  191. {
  192. new CAnimImage("ITMTLS", town.details->hallLevel, 0, 67, 31);
  193. if (town.details->goldIncome)
  194. new CLabel(157, 58, FONT_TINY, CENTER, Colors::WHITE,
  195. boost::lexical_cast<std::string>(town.details->goldIncome));
  196. if(town.details->garrisonedHero) //garrisoned hero icon
  197. new CPicture("TOWNQKGH", 149, 76);
  198. if(town.details->customRes)//silo is built
  199. {
  200. if (town.tType->primaryRes == Res::WOOD_AND_ORE )// wood & ore
  201. {
  202. new CAnimImage("SMALRES", Res::WOOD, 0, 7, 75);
  203. new CAnimImage("SMALRES", Res::ORE , 0, 7, 88);
  204. }
  205. else
  206. new CAnimImage("SMALRES", town.tType->primaryRes, 0, 7, 81);
  207. }
  208. }
  209. }
  210. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown &town):
  211. CArmyTooltip(pos, town)
  212. {
  213. init(town);
  214. }
  215. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town):
  216. CArmyTooltip(pos, InfoAboutTown(town, true))
  217. {
  218. init(InfoAboutTown(town, true));
  219. }
  220. CInfoWindow::CInfoWindow(std::string Text, PlayerColor player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  221. {
  222. OBJ_CONSTRUCTION_CAPTURING_ALL;
  223. type |= BLOCK_ADV_HOTKEYS;
  224. ID = QueryID(-1);
  225. for(auto & Button : Buttons)
  226. {
  227. CAdventureMapButton *button = new CAdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Button.first);
  228. button->borderColor = Colors::METALLIC_GOLD;
  229. button->borderEnabled = true;
  230. button->callback.add(Button.second); //each button will close the window apart from call-defined actions
  231. buttons.push_back(button);
  232. }
  233. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::WHITE);
  234. if(!text->slider)
  235. {
  236. text->resize(text->label->textSize);
  237. }
  238. if(buttons.size())
  239. {
  240. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  241. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  242. }
  243. for(auto & comp : comps)
  244. {
  245. comp->recActions = 0xff;
  246. addChild(comp);
  247. comp->recActions &= ~(SHOWALL | UPDATE);
  248. components.push_back(comp);
  249. }
  250. setDelComps(delComps);
  251. CMessage::drawIWindow(this,Text,player);
  252. }
  253. CInfoWindow::CInfoWindow()
  254. {
  255. ID = QueryID(-1);
  256. setDelComps(false);
  257. text = nullptr;
  258. }
  259. void CInfoWindow::close()
  260. {
  261. GH.popIntTotally(this);
  262. if(LOCPLINT)
  263. LOCPLINT->showingDialog->setn(false);
  264. }
  265. void CInfoWindow::show(SDL_Surface * to)
  266. {
  267. CIntObject::show(to);
  268. }
  269. CInfoWindow::~CInfoWindow()
  270. {
  271. if(!delComps)
  272. {
  273. for (auto & elem : components)
  274. removeChild(elem);
  275. }
  276. }
  277. void CInfoWindow::showAll(SDL_Surface * to)
  278. {
  279. CSimpleWindow::show(to);
  280. CIntObject::showAll(to);
  281. }
  282. void CInfoWindow::showInfoDialog(const std::string &text, const std::vector<CComponent *> *components, bool DelComps, PlayerColor player)
  283. {
  284. CInfoWindow * window = CInfoWindow::create(text, player, components, DelComps);
  285. GH.pushInt(window);
  286. }
  287. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, PlayerColor player)
  288. {
  289. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  290. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  291. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  292. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  293. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  294. for(auto & elem : onYes.funcs)
  295. temp->buttons[0]->callback += elem;
  296. for(auto & elem : onNo.funcs)
  297. temp->buttons[1]->callback += elem;
  298. GH.pushInt(temp);
  299. }
  300. void CInfoWindow::showOkDialog(const std::string & text, const std::vector<CComponent*> *components, const boost::function<void()> & onOk, bool delComps, PlayerColor player)
  301. {
  302. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  303. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  304. CInfoWindow * temp = new CInfoWindow(text, player, *components, pom, delComps);
  305. temp->buttons[0]->callback += onOk;
  306. GH.pushInt(temp);
  307. }
  308. CInfoWindow * CInfoWindow::create(const std::string &text, PlayerColor playerID /*= 1*/, const std::vector<CComponent*> *components /*= nullptr*/, bool DelComps)
  309. {
  310. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  311. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  312. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  313. return ret;
  314. }
  315. std::string CInfoWindow::genText(std::string title, std::string description)
  316. {
  317. return std::string("{") + title + "}" + "\n\n" + description;
  318. }
  319. void CInfoWindow::setDelComps(bool DelComps)
  320. {
  321. delComps = DelComps;
  322. for(CComponent *comp : components)
  323. {
  324. if(delComps)
  325. comp->recActions |= DISPOSE;
  326. else
  327. comp->recActions &= ~DISPOSE;
  328. }
  329. }
  330. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  331. :free(Free),bitmap(Bitmap)
  332. {
  333. init(x, y);
  334. }
  335. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  336. : free(Free),bitmap(Bitmap)
  337. {
  338. switch(alignment)
  339. {
  340. case BOTTOMRIGHT:
  341. init(p.x - Bitmap->w, p.y - Bitmap->h);
  342. break;
  343. case CENTER:
  344. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  345. break;
  346. case TOPLEFT:
  347. init(p.x, p.y);
  348. break;
  349. default:
  350. assert(0); //not implemented
  351. }
  352. }
  353. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  354. {
  355. CCS->curh->hide();
  356. free=Free;
  357. bitmap=Bitmap;
  358. if(bitmap)
  359. {
  360. pos.x = screen->w/2 - bitmap->w/2;
  361. pos.y = screen->h/2 - bitmap->h/2;
  362. pos.h = bitmap->h;
  363. pos.w = bitmap->w;
  364. }
  365. }
  366. void CInfoPopup::close()
  367. {
  368. if(free)
  369. SDL_FreeSurface(bitmap);
  370. GH.popIntTotally(this);
  371. }
  372. void CInfoPopup::show(SDL_Surface * to)
  373. {
  374. blitAt(bitmap,pos.x,pos.y,to);
  375. }
  376. CInfoPopup::~CInfoPopup()
  377. {
  378. CCS->curh->show();
  379. }
  380. void CInfoPopup::init(int x, int y)
  381. {
  382. CCS->curh->hide();
  383. pos.x = x;
  384. pos.y = y;
  385. pos.h = bitmap->h;
  386. pos.w = bitmap->w;
  387. // Put the window back on screen if necessary
  388. vstd::amax(pos.x, 0);
  389. vstd::amax(pos.y, 0);
  390. vstd::amin(pos.x, screen->w - bitmap->w);
  391. vstd::amin(pos.y, screen->h - bitmap->h);
  392. }
  393. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  394. {
  395. OBJ_CONSTRUCTION_CAPTURING_ALL;
  396. pos.x+=x;
  397. pos.y+=y;
  398. TFaction faction = cre->faction;
  399. assert(CGI->townh->factions.size() > faction);
  400. if(Big)
  401. bg = new CPicture(CGI->townh->factions[faction]->creatureBg130);
  402. else
  403. bg = new CPicture(CGI->townh->factions[faction]->creatureBg120);
  404. bg->needRefresh = true;
  405. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  406. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  407. anim->startPreview(cre->hasBonusOfType(Bonus::SIEGE_WEAPON));
  408. pos.w = bg->pos.w;
  409. pos.h = bg->pos.h;
  410. }
  411. CRecruitmentWindow::CCreatureCard::CCreatureCard(CRecruitmentWindow *window, const CCreature *crea, int totalAmount):
  412. CIntObject(LCLICK | RCLICK),
  413. parent(window),
  414. selected(false),
  415. creature(crea),
  416. amount(totalAmount)
  417. {
  418. OBJ_CONSTRUCTION_CAPTURING_ALL;
  419. pic = new CCreaturePic(1,1, creature, true, true);
  420. // 1 + 1 px for borders
  421. pos.w = pic->pos.w + 2;
  422. pos.h = pic->pos.h + 2;
  423. }
  424. void CRecruitmentWindow::CCreatureCard::select(bool on)
  425. {
  426. selected = on;
  427. redraw();
  428. }
  429. void CRecruitmentWindow::CCreatureCard::clickLeft(tribool down, bool previousState)
  430. {
  431. if (down)
  432. parent->select(this);
  433. }
  434. void CRecruitmentWindow::CCreatureCard::clickRight(tribool down, bool previousState)
  435. {
  436. if (down)
  437. GH.pushInt(createCreWindow(creature->idNumber, CCreatureWindow::OTHER, 0));
  438. }
  439. void CRecruitmentWindow::CCreatureCard::showAll(SDL_Surface * to)
  440. {
  441. CIntObject::showAll(to);
  442. if (selected)
  443. drawBorder(to, pos, int3(248, 0, 0));
  444. else
  445. drawBorder(to, pos, int3(232, 212, 120));
  446. }
  447. CRecruitmentWindow::CCostBox::CCostBox(Rect position, std::string title)
  448. {
  449. type |= REDRAW_PARENT;
  450. pos = position + pos;
  451. OBJ_CONSTRUCTION_CAPTURING_ALL;
  452. new CLabel(pos.w/2, 10, FONT_SMALL, CENTER, Colors::WHITE, title);
  453. }
  454. void CRecruitmentWindow::CCostBox::set(TResources res)
  455. {
  456. //just update values
  457. for(auto & item : resources)
  458. {
  459. item.second.first->setText(boost::lexical_cast<std::string>(res[item.first]));
  460. }
  461. }
  462. void CRecruitmentWindow::CCostBox::createItems(TResources res)
  463. {
  464. OBJ_CONSTRUCTION_CAPTURING_ALL;
  465. for(auto & curr : resources)
  466. {
  467. delete curr.second.first;
  468. delete curr.second.second;
  469. }
  470. resources.clear();
  471. TResources::nziterator iter(res);
  472. while (iter.valid())
  473. {
  474. CAnimImage * image = new CAnimImage("RESOURCE", iter->resType);
  475. CLabel * text = new CLabel(15, 43, FONT_SMALL, CENTER, Colors::WHITE, "0");
  476. resources.insert(std::make_pair(iter->resType, std::make_pair(text, image)));
  477. iter++;
  478. }
  479. if (!resources.empty())
  480. {
  481. int curx = pos.w / 2 - (16 * resources.size()) - (8 * (resources.size() - 1));
  482. //reverse to display gold as first resource
  483. for (auto & res : boost::adaptors::reverse(resources))
  484. {
  485. res.second.first->moveBy(Point(curx, 22));
  486. res.second.second->moveBy(Point(curx, 22));
  487. curx += 48;
  488. }
  489. }
  490. redraw();
  491. }
  492. void CRecruitmentWindow::select(CCreatureCard *card)
  493. {
  494. if (card == selected)
  495. return;
  496. if (selected)
  497. selected->select(false);
  498. selected = card;
  499. if (selected)
  500. selected->select(true);
  501. if (card)
  502. {
  503. si32 maxAmount = card->creature->maxAmount(LOCPLINT->cb->getResourceAmount());
  504. vstd::amin(maxAmount, card->amount);
  505. slider->setAmount(maxAmount);
  506. if(slider->value != maxAmount)
  507. slider->moveTo(maxAmount);
  508. else // if slider already at 0 - emulate call to sliderMoved()
  509. sliderMoved(maxAmount);
  510. costPerTroopValue->createItems(card->creature->cost);
  511. totalCostValue->createItems(card->creature->cost);
  512. costPerTroopValue->set(card->creature->cost);
  513. totalCostValue->set(card->creature->cost * maxAmount);
  514. //Recruit %s
  515. title->setText(boost::str(boost::format(CGI->generaltexth->tcommands[21]) % card->creature->namePl));
  516. maxButton->block(maxAmount == 0);
  517. slider->block(maxAmount == 0);
  518. }
  519. }
  520. void CRecruitmentWindow::buy()
  521. {
  522. CreatureID crid = selected->creature->idNumber;
  523. SlotID dstslot = dst-> getSlotFor(crid);
  524. if(!dstslot.validSlot() && !vstd::contains(CGI->arth->bigArtifacts,CGI->arth->creatureToMachineID(crid))) //no available slot
  525. {
  526. std::string txt;
  527. if(dst->ID == Obj::HERO)
  528. {
  529. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  530. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  531. }
  532. else
  533. {
  534. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  535. }
  536. LOCPLINT->showInfoDialog(txt);
  537. return;
  538. }
  539. onRecruit(crid, slider->value);
  540. if(level >= 0)
  541. close();
  542. }
  543. void CRecruitmentWindow::showAll(SDL_Surface * to)
  544. {
  545. CWindowObject::showAll(to);
  546. // recruit\total values
  547. drawBorder(to, pos.x + 172, pos.y + 222, 67, 42, int3(239,215,123));
  548. drawBorder(to, pos.x + 246, pos.y + 222, 67, 42, int3(239,215,123));
  549. //cost boxes
  550. drawBorder(to, pos.x + 64, pos.y + 222, 99, 76, int3(239,215,123));
  551. drawBorder(to, pos.x + 322, pos.y + 222, 99, 76, int3(239,215,123));
  552. //buttons borders
  553. drawBorder(to, pos.x + 133, pos.y + 312, 66, 34, int3(173,142,66));
  554. drawBorder(to, pos.x + 211, pos.y + 312, 66, 34, int3(173,142,66));
  555. drawBorder(to, pos.x + 289, pos.y + 312, 66, 34, int3(173,142,66));
  556. }
  557. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const std::function<void(CreatureID,int)> &Recruit, int y_offset):
  558. CWindowObject(PLAYER_COLORED, "TPRCRT"),
  559. onRecruit(Recruit),
  560. level(Level),
  561. dst(Dst),
  562. selected(nullptr),
  563. dwelling(Dwelling)
  564. {
  565. moveBy(Point(0, y_offset));
  566. OBJ_CONSTRUCTION_CAPTURING_ALL;
  567. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  568. slider = new CSlider(176,279,135,nullptr,0,0,0,true);
  569. slider->moved = boost::bind(&CRecruitmentWindow::sliderMoved,this, _1);
  570. maxButton = new CAdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CSlider::moveToMax,slider),134,313,"IRCBTNS.DEF",SDLK_m);
  571. buyButton = new CAdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::buy,this),212,313,"IBY6432.DEF",SDLK_RETURN);
  572. cancelButton = new CAdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::close,this),290,313,"ICN6432.DEF",SDLK_ESCAPE);
  573. title = new CLabel(243, 32, FONT_BIG, CENTER, Colors::YELLOW);
  574. availableValue = new CLabel(205, 253, FONT_SMALL, CENTER, Colors::WHITE);
  575. toRecruitValue = new CLabel(279, 253, FONT_SMALL, CENTER, Colors::WHITE);
  576. costPerTroopValue = new CCostBox(Rect(65, 222, 97, 74), CGI->generaltexth->allTexts[346]);
  577. totalCostValue = new CCostBox(Rect(323, 222, 97, 74), CGI->generaltexth->allTexts[466]);
  578. new CLabel(205, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[465]); //available t
  579. new CLabel(279, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[16]); //recruit t
  580. availableCreaturesChanged();
  581. }
  582. void CRecruitmentWindow::availableCreaturesChanged()
  583. {
  584. OBJ_CONSTRUCTION_CAPTURING_ALL;
  585. size_t selectedIndex = 0;
  586. if (!cards.empty() && selected) // find position of selected item
  587. selectedIndex = std::find(cards.begin(), cards.end(), selected) - cards.begin();
  588. //deselect card
  589. select(nullptr);
  590. //delete old cards
  591. for(auto & card : cards)
  592. delete card;
  593. cards.clear();
  594. for(int i=0; i<dwelling->creatures.size(); i++)
  595. {
  596. //find appropriate level
  597. if(level >= 0 && i != level)
  598. continue;
  599. int amount = dwelling->creatures[i].first;
  600. //create new cards
  601. for(auto & creature : boost::adaptors::reverse(dwelling->creatures[i].second))
  602. cards.push_back(new CCreatureCard(this, CGI->creh->creatures[creature], amount));
  603. }
  604. assert(!cards.empty());
  605. const int creatureWidth = 102;
  606. //normal distance between cards - 18px
  607. int requiredSpace = 18;
  608. //maximum distance we can use without reaching window borders
  609. int availableSpace = pos.w - 50 - creatureWidth * cards.size();
  610. if (cards.size() > 1) // avoid division by zero
  611. availableSpace /= cards.size() - 1;
  612. else
  613. availableSpace = 0;
  614. assert(availableSpace >= 0);
  615. const int spaceBetween = std::min(requiredSpace, availableSpace);
  616. const int totalCreatureWidth = spaceBetween + creatureWidth;
  617. //now we know total amount of cards and can move them to correct position
  618. int curx = pos.w / 2 - (creatureWidth*cards.size()/2) - (spaceBetween*(cards.size()-1)/2);
  619. for(auto & card : cards)
  620. {
  621. card->moveBy(Point(curx, 64));
  622. curx += totalCreatureWidth;
  623. }
  624. //restore selection
  625. select(cards[selectedIndex]);
  626. if(slider->value == slider->amount)
  627. slider->moveTo(slider->amount);
  628. else // if slider already at 0 - emulate call to sliderMoved()
  629. sliderMoved(slider->amount);
  630. }
  631. void CRecruitmentWindow::sliderMoved(int to)
  632. {
  633. if (!selected)
  634. return;
  635. buyButton->block(!to);
  636. availableValue->setText(boost::lexical_cast<std::string>(selected->amount - to));
  637. toRecruitValue->setText(boost::lexical_cast<std::string>(to));
  638. totalCostValue->set(selected->creature->cost * to);
  639. }
  640. CSplitWindow::CSplitWindow(const CCreature * creature, std::function<void(int, int)> callback_,
  641. int leftMin_, int rightMin_, int leftAmount_, int rightAmount_):
  642. CWindowObject(PLAYER_COLORED, "GPUCRDIV"),
  643. callback(callback_),
  644. leftAmount(leftAmount_),
  645. rightAmount(rightAmount_),
  646. leftMin(leftMin_),
  647. rightMin(rightMin_),
  648. slider(nullptr)
  649. {
  650. OBJ_CONSTRUCTION_CAPTURING_ALL;
  651. int total = leftAmount + rightAmount;
  652. int leftMax = total - rightMin;
  653. int rightMax = total - leftMin;
  654. ok = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::apply, this), 20, 263, "IOK6432", SDLK_RETURN);
  655. cancel = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::close, this), 214, 263, "ICN6432", SDLK_ESCAPE);
  656. int sliderPositions = total - leftMin - rightMin;
  657. leftInput = new CTextInput(Rect(20, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, true));
  658. rightInput = new CTextInput(Rect(176, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, false));
  659. //add filters to allow only number input
  660. leftInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax));
  661. rightInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax));
  662. leftInput->setText(boost::lexical_cast<std::string>(leftAmount), false);
  663. rightInput->setText(boost::lexical_cast<std::string>(rightAmount), false);
  664. animLeft = new CCreaturePic(20, 54, creature, true, false);
  665. animRight = new CCreaturePic(177, 54,creature, true, false);
  666. slider = new CSlider(21, 194, 257, boost::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPositions, rightAmount - rightMin, true);
  667. std::string title = CGI->generaltexth->allTexts[256];
  668. boost::algorithm::replace_first(title,"%s", creature->namePl);
  669. new CLabel(150, 34, FONT_BIG, CENTER, Colors::YELLOW, title);
  670. }
  671. void CSplitWindow::setAmountText(std::string text, bool left)
  672. {
  673. try
  674. {
  675. setAmount(boost::lexical_cast<int>(text), left);
  676. slider->moveTo(rightAmount - rightMin);
  677. }
  678. catch(boost::bad_lexical_cast &)
  679. {
  680. }
  681. }
  682. void CSplitWindow::setAmount(int value, bool left)
  683. {
  684. int total = leftAmount + rightAmount;
  685. leftAmount = left ? value : total - value;
  686. rightAmount = left ? total - value : value;
  687. leftInput->setText(boost::lexical_cast<std::string>(leftAmount));
  688. rightInput->setText(boost::lexical_cast<std::string>(rightAmount));
  689. }
  690. void CSplitWindow::apply()
  691. {
  692. callback(leftAmount, rightAmount);
  693. close();
  694. }
  695. void CSplitWindow::sliderMoved(int to)
  696. {
  697. setAmount(rightMin + to, false);
  698. }
  699. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, std::function<void(ui32)> callback):
  700. CWindowObject(PLAYER_COLORED, "LVLUPBKG"),
  701. cb(callback)
  702. {
  703. OBJ_CONSTRUCTION_CAPTURING_ALL;
  704. LOCPLINT->showingDialog->setn(true);
  705. new CAnimImage("PortraitsLarge", hero->portrait, 0, 170, 66);
  706. new CAdventureMapButton("", "", boost::bind(&CLevelWindow::close, this), 297, 413, "IOKAY", SDLK_RETURN);
  707. //%s has gained a level.
  708. new CLabel(192, 33, FONT_MEDIUM, CENTER, Colors::WHITE,
  709. boost::str(boost::format(CGI->generaltexth->allTexts[444]) % hero->name));
  710. //%s is now a level %d %s.
  711. new CLabel(192, 162, FONT_MEDIUM, CENTER, Colors::WHITE,
  712. boost::str(boost::format(CGI->generaltexth->allTexts[445]) % hero->name % hero->level % hero->type->heroClass->name));
  713. new CAnimImage("PSKIL42", pskill, 0, 174, 190);
  714. new CLabel(192, 253, FONT_MEDIUM, CENTER, Colors::WHITE,
  715. CGI->generaltexth->primarySkillNames[pskill] + " +1");
  716. if (!skills.empty())
  717. {
  718. std::vector<CSelectableComponent *> comps;
  719. for(auto & skill : skills)
  720. {
  721. comps.push_back(new CSelectableComponent(
  722. CComponent::secskill,
  723. skill,
  724. hero->getSecSkillLevel( SecondarySkill(skill) )+1,
  725. CComponent::medium));
  726. }
  727. box = new CComponentBox(comps, Rect(75, 300, pos.w - 150, 100));
  728. }
  729. else
  730. box = nullptr;
  731. }
  732. CLevelWindow::~CLevelWindow()
  733. {
  734. //FIXME: call callback if there was nothing to select?
  735. if (box && box->selectedIndex() != -1)
  736. cb(box->selectedIndex());
  737. LOCPLINT->showingDialog->setn(false);
  738. }
  739. void CMinorResDataBar::show(SDL_Surface * to)
  740. {
  741. }
  742. void CMinorResDataBar::showAll(SDL_Surface * to)
  743. {
  744. blitAt(bg,pos.x,pos.y,to);
  745. for (Res::ERes i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  746. {
  747. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  748. graphics->fonts[FONT_SMALL]->renderTextCenter(to, text, Colors::WHITE, Point(pos.x + 50 + 76 * i, pos.y + pos.h/2));
  749. }
  750. std::vector<std::string> temp;
  751. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  752. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  753. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  754. std::string datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  755. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  756. graphics->fonts[FONT_SMALL]->renderTextCenter(to, processStr(datetext,temp), Colors::WHITE, Point(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2));
  757. }
  758. CMinorResDataBar::CMinorResDataBar()
  759. {
  760. bg = BitmapHandler::loadBitmap("KRESBAR.bmp");
  761. CSDL_Ext::setDefaultColorKey(bg);
  762. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  763. pos.x = 7;
  764. pos.y = 575;
  765. pos.w = bg->w;
  766. pos.h = bg->h;
  767. }
  768. CMinorResDataBar::~CMinorResDataBar()
  769. {
  770. SDL_FreeSurface(bg);
  771. }
  772. CObjectListWindow::CItem::CItem(CObjectListWindow *_parent, size_t _id, std::string _text):
  773. parent(_parent),
  774. index(_id)
  775. {
  776. OBJ_CONSTRUCTION_CAPTURING_ALL;
  777. border = new CPicture("TPGATES");
  778. pos = border->pos;
  779. addUsedEvents(LCLICK);
  780. type |= REDRAW_PARENT;
  781. text = new CLabel(pos.w/2, pos.h/2, FONT_SMALL, CENTER, Colors::WHITE, _text);
  782. select(index == parent->selected);
  783. }
  784. void CObjectListWindow::CItem::select(bool on)
  785. {
  786. if (on)
  787. border->recActions = 255;
  788. else
  789. border->recActions = ~(UPDATE | SHOWALL);
  790. redraw();
  791. }
  792. void CObjectListWindow::CItem::clickLeft(tribool down, bool previousState)
  793. {
  794. if( previousState && !down)
  795. parent->changeSelection(index);
  796. }
  797. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CIntObject * titlePic, std::string _title, std::string _descr,
  798. std::function<void(int)> Callback):
  799. CWindowObject(PLAYER_COLORED, "TPGATE"),
  800. onSelect(Callback)
  801. {
  802. items.reserve(_items.size());
  803. for(int id : _items)
  804. {
  805. items.push_back(std::make_pair(id, CGI->mh->map->objects[id]->getObjectName()));
  806. }
  807. init(titlePic, _title, _descr);
  808. }
  809. CObjectListWindow::CObjectListWindow(const std::vector<std::string> &_items, CIntObject * titlePic, std::string _title, std::string _descr,
  810. std::function<void(int)> Callback):
  811. CWindowObject(PLAYER_COLORED, "TPGATE"),
  812. onSelect(Callback)
  813. {
  814. items.reserve(_items.size());
  815. for (size_t i=0; i<_items.size(); i++)
  816. items.push_back(std::make_pair(int(i), _items[i]));
  817. init(titlePic, _title, _descr);
  818. }
  819. void CObjectListWindow::init(CIntObject * titlePic, std::string _title, std::string _descr)
  820. {
  821. OBJ_CONSTRUCTION_CAPTURING_ALL;
  822. title = new CLabel(152, 27, FONT_BIG, CENTER, Colors::YELLOW, _title);
  823. descr = new CLabel(145, 133, FONT_SMALL, CENTER, Colors::WHITE, _descr);
  824. ok = new CAdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  825. ok->block(true);
  826. exit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  827. if (titlePic)
  828. {
  829. titleImage = titlePic;
  830. addChild(titleImage);
  831. titleImage->recActions = defActions;
  832. titleImage->pos.x = pos.w/2 + pos.x - titleImage->pos.w/2;
  833. titleImage->pos.y =75 + pos.y - titleImage->pos.h/2;
  834. }
  835. list = new CListBox(boost::bind(&CObjectListWindow::genItem, this, _1), CListBox::DestroyFunc(),
  836. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  837. list->type |= REDRAW_PARENT;
  838. }
  839. CIntObject * CObjectListWindow::genItem(size_t index)
  840. {
  841. if (index < items.size())
  842. return new CItem(this, index, items[index].second);
  843. return nullptr;
  844. }
  845. void CObjectListWindow::elementSelected()
  846. {
  847. std::function<void(int)> toCall = onSelect;//save
  848. int where = items[selected].first; //required variables
  849. GH.popIntTotally(this);//then destroy window
  850. toCall(where);//and send selected object
  851. }
  852. void CObjectListWindow::changeSelection(size_t which)
  853. {
  854. ok->block(false);
  855. if (selected == which)
  856. return;
  857. std::list< CIntObject * > elements = list->getItems();
  858. for(CIntObject * element : elements)
  859. {
  860. CItem *item;
  861. if ( (item = dynamic_cast<CItem*>(element)) )
  862. {
  863. if (item->index == selected)
  864. item->select(false);
  865. if (item->index == which)
  866. item->select(true);
  867. }
  868. }
  869. selected = which;
  870. }
  871. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  872. {
  873. if(key.state != SDL_PRESSED)
  874. return;
  875. int sel = selected;
  876. switch(key.keysym.sym)
  877. {
  878. break; case SDLK_UP:
  879. sel -=1;
  880. break; case SDLK_DOWN:
  881. sel +=1;
  882. break; case SDLK_PAGEUP:
  883. sel -=9;
  884. break; case SDLK_PAGEDOWN:
  885. sel +=9;
  886. break; case SDLK_HOME:
  887. sel = 0;
  888. break; case SDLK_END:
  889. sel = items.size();
  890. break; default:
  891. return;
  892. }
  893. vstd::abetween(sel, 0, items.size()-1);
  894. list->scrollTo(sel);
  895. changeSelection(sel);
  896. }
  897. CTradeWindow::CTradeableItem::CTradeableItem(Point pos, EType Type, int ID, bool Left, int Serial):
  898. CIntObject(LCLICK | HOVER | RCLICK, pos),
  899. type(EType(-1)),// set to invalid, will be corrected in setType
  900. id(ID),
  901. serial(Serial),
  902. left(Left)
  903. {
  904. downSelection = false;
  905. hlp = nullptr;
  906. image = nullptr;
  907. setType(Type);
  908. }
  909. void CTradeWindow::CTradeableItem::setType(EType newType)
  910. {
  911. if (type != newType)
  912. {
  913. OBJ_CONSTRUCTION_CAPTURING_ALL;
  914. type = newType;
  915. delete image;
  916. if (getIndex() < 0)
  917. {
  918. image = new CAnimImage(getFilename(), 0);
  919. image->disable();
  920. }
  921. else
  922. image = new CAnimImage(getFilename(), getIndex());
  923. }
  924. }
  925. void CTradeWindow::CTradeableItem::setID(int newID)
  926. {
  927. if (id != newID)
  928. {
  929. id = newID;
  930. if (image)
  931. {
  932. int index = getIndex();
  933. if (index < 0)
  934. image->disable();
  935. else
  936. {
  937. image->enable();
  938. image->setFrame(index);
  939. }
  940. }
  941. }
  942. }
  943. std::string CTradeWindow::CTradeableItem::getFilename()
  944. {
  945. switch(type)
  946. {
  947. case RESOURCE:
  948. return "RESOURCE";
  949. case PLAYER:
  950. return "CREST58";
  951. case ARTIFACT_TYPE:
  952. case ARTIFACT_PLACEHOLDER:
  953. case ARTIFACT_INSTANCE:
  954. return "artifact";
  955. case CREATURE:
  956. return "TWCRPORT";
  957. default:
  958. return "";
  959. }
  960. }
  961. int CTradeWindow::CTradeableItem::getIndex()
  962. {
  963. if (id < 0)
  964. return -1;
  965. switch(type)
  966. {
  967. case RESOURCE:
  968. case PLAYER:
  969. return id;
  970. case ARTIFACT_TYPE:
  971. case ARTIFACT_INSTANCE:
  972. case ARTIFACT_PLACEHOLDER:
  973. return VLC->arth->artifacts[id]->iconIndex;
  974. case CREATURE:
  975. return VLC->creh->creatures[id]->iconIndex;
  976. default:
  977. return -1;
  978. }
  979. }
  980. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  981. {
  982. Point posToBitmap;
  983. Point posToSubCenter;
  984. switch(type)
  985. {
  986. case RESOURCE:
  987. posToBitmap = Point(19,9);
  988. posToSubCenter = Point(36, 59);
  989. break;
  990. case CREATURE_PLACEHOLDER:
  991. case CREATURE:
  992. posToSubCenter = Point(29, 76);
  993. if(downSelection)
  994. posToSubCenter.y += 5;
  995. break;
  996. case PLAYER:
  997. posToSubCenter = Point(31, 76);
  998. break;
  999. case ARTIFACT_PLACEHOLDER:
  1000. case ARTIFACT_INSTANCE:
  1001. posToSubCenter = Point(19, 55);
  1002. if(downSelection)
  1003. posToSubCenter.y += 8;
  1004. break;
  1005. case ARTIFACT_TYPE:
  1006. posToSubCenter = Point(19, 58);
  1007. break;
  1008. }
  1009. if (image)
  1010. {
  1011. image->moveTo(pos.topLeft() + posToBitmap);
  1012. CIntObject::showAll(to);
  1013. }
  1014. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, Colors::WHITE, to);
  1015. }
  1016. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  1017. {
  1018. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  1019. assert(mw);
  1020. if(down)
  1021. {
  1022. if(type == ARTIFACT_PLACEHOLDER)
  1023. {
  1024. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  1025. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  1026. {
  1027. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this, movedArt);
  1028. }
  1029. else if(const CArtifactInstance *art = getArtInstance())
  1030. {
  1031. aw->arts->commonInfo->src.AOH = aw->arts;
  1032. aw->arts->commonInfo->src.art = art;
  1033. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  1034. aw->arts->markPossibleSlots(art);
  1035. //aw->arts->commonInfo->dst.AOH = aw->arts;
  1036. CCS->curh->dragAndDropCursor(new CAnimImage("artifact", art->artType->iconIndex));
  1037. aw->arts->artifactsOnAltar.erase(art);
  1038. setID(-1);
  1039. subtitle = "";
  1040. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  1041. }
  1042. aw->calcTotalExp();
  1043. return;
  1044. }
  1045. if(left)
  1046. {
  1047. if(mw->hLeft != this)
  1048. mw->hLeft = this;
  1049. else
  1050. return;
  1051. }
  1052. else
  1053. {
  1054. if(mw->hRight != this)
  1055. mw->hRight = this;
  1056. else
  1057. return;
  1058. }
  1059. mw->selectionChanged(left);
  1060. }
  1061. }
  1062. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  1063. {
  1064. Rect oldPos = pos;
  1065. std::string oldSub = subtitle;
  1066. downSelection = true;
  1067. moveTo(dstPos);
  1068. subtitle = customSub;
  1069. showAll(to);
  1070. downSelection = false;
  1071. moveTo(oldPos.topLeft());
  1072. subtitle = oldSub;
  1073. }
  1074. void CTradeWindow::CTradeableItem::hover(bool on)
  1075. {
  1076. if(!on)
  1077. {
  1078. GH.statusbar->clear();
  1079. return;
  1080. }
  1081. switch(type)
  1082. {
  1083. case CREATURE:
  1084. case CREATURE_PLACEHOLDER:
  1085. GH.statusbar->setText(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  1086. break;
  1087. case ARTIFACT_PLACEHOLDER:
  1088. if(id < 0)
  1089. GH.statusbar->setText(CGI->generaltexth->zelp[582].first);
  1090. else
  1091. GH.statusbar->setText(CGI->arth->artifacts[id]->Name());
  1092. break;
  1093. }
  1094. }
  1095. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  1096. {
  1097. if(down)
  1098. {
  1099. switch(type)
  1100. {
  1101. case CREATURE:
  1102. case CREATURE_PLACEHOLDER:
  1103. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  1104. break;
  1105. case ARTIFACT_TYPE:
  1106. case ARTIFACT_PLACEHOLDER:
  1107. if(id >= 0)
  1108. adventureInt->handleRightClick(CGI->arth->artifacts[id]->Description(), down);
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. std::string CTradeWindow::CTradeableItem::getName(int number /*= -1*/) const
  1114. {
  1115. switch(type)
  1116. {
  1117. case PLAYER:
  1118. return CGI->generaltexth->capColors[id];
  1119. case RESOURCE:
  1120. return CGI->generaltexth->restypes[id];
  1121. case CREATURE:
  1122. if(number == 1)
  1123. return CGI->creh->creatures[id]->nameSing;
  1124. else
  1125. return CGI->creh->creatures[id]->namePl;
  1126. case ARTIFACT_TYPE:
  1127. case ARTIFACT_INSTANCE:
  1128. return CGI->arth->artifacts[id]->Name();
  1129. }
  1130. assert(0);
  1131. return "";
  1132. }
  1133. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  1134. {
  1135. switch(type)
  1136. {
  1137. case ARTIFACT_PLACEHOLDER:
  1138. case ARTIFACT_INSTANCE:
  1139. return (const CArtifactInstance *)hlp;
  1140. default:
  1141. return nullptr;
  1142. }
  1143. }
  1144. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  1145. {
  1146. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  1147. hlp = art;
  1148. if(art)
  1149. setID(art->artType->id);
  1150. else
  1151. setID(-1);
  1152. }
  1153. CTradeWindow::CTradeWindow(std::string bgName, const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode):
  1154. CWindowObject(PLAYER_COLORED, bgName),
  1155. market(Market),
  1156. hero(Hero),
  1157. arts(nullptr),
  1158. hLeft(nullptr),
  1159. hRight(nullptr),
  1160. readyToTrade(false)
  1161. {
  1162. type |= BLOCK_ADV_HOTKEYS;
  1163. mode = Mode;
  1164. initTypes();
  1165. }
  1166. void CTradeWindow::initTypes()
  1167. {
  1168. switch(mode)
  1169. {
  1170. case EMarketMode::RESOURCE_RESOURCE:
  1171. itemsType[1] = RESOURCE;
  1172. itemsType[0] = RESOURCE;
  1173. break;
  1174. case EMarketMode::RESOURCE_PLAYER:
  1175. itemsType[1] = RESOURCE;
  1176. itemsType[0] = PLAYER;
  1177. break;
  1178. case EMarketMode::CREATURE_RESOURCE:
  1179. itemsType[1] = CREATURE;
  1180. itemsType[0] = RESOURCE;
  1181. break;
  1182. case EMarketMode::RESOURCE_ARTIFACT:
  1183. itemsType[1] = RESOURCE;
  1184. itemsType[0] = ARTIFACT_TYPE;
  1185. break;
  1186. case EMarketMode::ARTIFACT_RESOURCE:
  1187. itemsType[1] = ARTIFACT_INSTANCE;
  1188. itemsType[0] = RESOURCE;
  1189. break;
  1190. case EMarketMode::CREATURE_EXP:
  1191. itemsType[1] = CREATURE;
  1192. itemsType[0] = CREATURE_PLACEHOLDER;
  1193. break;
  1194. case EMarketMode::ARTIFACT_EXP:
  1195. itemsType[1] = ARTIFACT_TYPE;
  1196. itemsType[0] = ARTIFACT_PLACEHOLDER;
  1197. break;
  1198. }
  1199. }
  1200. void CTradeWindow::initItems(bool Left)
  1201. {
  1202. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  1203. {
  1204. int xOffset = 0, yOffset = 0;
  1205. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1206. {
  1207. xOffset = -361;
  1208. yOffset = +46;
  1209. auto hlp = new CTradeableItem(Point(137, 469), itemsType[Left], -1, 1, 0);
  1210. hlp->recActions &= ~(UPDATE | SHOWALL);
  1211. items[Left].push_back(hlp);
  1212. }
  1213. else //ARTIFACT_EXP
  1214. {
  1215. xOffset = -363;
  1216. yOffset = -12;
  1217. }
  1218. BLOCK_CAPTURING;
  1219. arts = new CArtifactsOfHero(Point(pos.x+xOffset, pos.y+yOffset));
  1220. arts->commonInfo = new CArtifactsOfHero::SCommonPart;
  1221. arts->commonInfo->participants.insert(arts);
  1222. arts->recActions = 255;
  1223. arts->setHero(hero);
  1224. arts->allowedAssembling = false;
  1225. addChild(arts);
  1226. artSets.push_back(arts);
  1227. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1228. arts->highlightModeCallback = boost::bind(&CTradeWindow::artifactSelected, this, _1);
  1229. return;
  1230. }
  1231. std::vector<int> *ids = getItemsIds(Left);
  1232. std::vector<Rect> pos;
  1233. int amount = -1;
  1234. getPositionsFor(pos, Left, itemsType[Left]);
  1235. if(Left || !ids)
  1236. amount = 7;
  1237. else
  1238. amount = ids->size();
  1239. if(ids)
  1240. vstd::amin(amount, ids->size());
  1241. for(int j=0; j<amount; j++)
  1242. {
  1243. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  1244. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  1245. continue;
  1246. auto hlp = new CTradeableItem(pos[j].topLeft(), itemsType[Left], id, Left, j);
  1247. hlp->pos = pos[j] + this->pos.topLeft();
  1248. items[Left].push_back(hlp);
  1249. }
  1250. initSubs(Left);
  1251. }
  1252. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  1253. {
  1254. std::vector<int> *ids = nullptr;
  1255. if(mode == EMarketMode::ARTIFACT_EXP)
  1256. return new std::vector<int>(22, -1);
  1257. if(Left)
  1258. {
  1259. switch(itemsType[1])
  1260. {
  1261. case CREATURE:
  1262. ids = new std::vector<int>;
  1263. for(int i = 0; i < 7; i++)
  1264. {
  1265. if(const CCreature *c = hero->getCreature(SlotID(i)))
  1266. ids->push_back(c->idNumber);
  1267. else
  1268. ids->push_back(-1);
  1269. }
  1270. break;
  1271. }
  1272. }
  1273. else
  1274. {
  1275. switch(itemsType[0])
  1276. {
  1277. case PLAYER:
  1278. ids = new std::vector<int>;
  1279. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)
  1280. if(PlayerColor(i) != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(PlayerColor(i)) == EPlayerStatus::INGAME)
  1281. ids->push_back(i);
  1282. break;
  1283. case ARTIFACT_TYPE:
  1284. ids = new std::vector<int>(market->availableItemsIds(mode));
  1285. break;
  1286. }
  1287. }
  1288. return ids;
  1289. }
  1290. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  1291. {
  1292. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  1293. {
  1294. //22 boxes, 5 in row, last row: two boxes centered
  1295. int h, w, x, y, dx, dy;
  1296. h = w = 44;
  1297. x = 317;
  1298. y = 53;
  1299. dx = 54;
  1300. dy = 70;
  1301. for (int i = 0; i < 4 ; i++)
  1302. for (int j = 0; j < 5 ; j++)
  1303. poss += Rect(x + dx*j, y + dy*i, w, h);
  1304. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  1305. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  1306. }
  1307. else
  1308. {
  1309. //seven boxes:
  1310. // X X X
  1311. // X X X
  1312. // X
  1313. int h, w, x, y, dx, dy;
  1314. int leftToRightOffset;
  1315. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  1316. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  1317. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  1318. genRect(h, w, x + dx, y + 2*dy);
  1319. if(!Left)
  1320. {
  1321. for(Rect &r : poss)
  1322. r.x += leftToRightOffset;
  1323. }
  1324. }
  1325. }
  1326. void CTradeWindow::initSubs(bool Left)
  1327. {
  1328. for(CTradeableItem *t : items[Left])
  1329. {
  1330. if(Left)
  1331. {
  1332. switch(itemsType[1])
  1333. {
  1334. case CREATURE:
  1335. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(SlotID(t->serial)));
  1336. break;
  1337. case RESOURCE:
  1338. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(t->serial)));
  1339. break;
  1340. }
  1341. }
  1342. else //right side
  1343. {
  1344. if(itemsType[0] == PLAYER)
  1345. {
  1346. t->subtitle = CGI->generaltexth->capColors[t->id];
  1347. }
  1348. else if(hLeft)//artifact, creature
  1349. {
  1350. int h1, h2; //hlp variables for getting offer
  1351. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  1352. if(t->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  1353. {
  1354. std::ostringstream oss;
  1355. oss << h2;
  1356. if(h1!=1)
  1357. oss << "/" << h1;
  1358. t->subtitle = oss.str();
  1359. }
  1360. else
  1361. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  1362. }
  1363. else
  1364. t->subtitle = "";
  1365. }
  1366. }
  1367. }
  1368. void CTradeWindow::showAll(SDL_Surface * to)
  1369. {
  1370. CWindowObject::showAll(to);
  1371. if(hRight)
  1372. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  1373. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  1374. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  1375. if(readyToTrade)
  1376. {
  1377. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  1378. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  1379. }
  1380. }
  1381. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  1382. {
  1383. for(CTradeableItem *t : toRemove)
  1384. removeItem(t);
  1385. }
  1386. void CTradeWindow::removeItem(CTradeableItem * t)
  1387. {
  1388. items[t->left] -= t;
  1389. delete t;
  1390. if(hRight == t)
  1391. {
  1392. hRight = nullptr;
  1393. selectionChanged(false);
  1394. }
  1395. }
  1396. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  1397. {
  1398. for(CTradeableItem *t : items[1])
  1399. if(!hero->getStackCount(SlotID(t->serial)))
  1400. toRemove.insert(t);
  1401. }
  1402. void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
  1403. {
  1404. const IMarket *m = market;
  1405. const CGHeroInstance *h = hero;
  1406. CTradeWindow *nwindow = nullptr;
  1407. GH.popIntTotally(this);
  1408. switch(Mode)
  1409. {
  1410. case EMarketMode::CREATURE_EXP:
  1411. case EMarketMode::ARTIFACT_EXP:
  1412. nwindow = new CAltarWindow(m, h, Mode);
  1413. break;
  1414. default:
  1415. nwindow = new CMarketplaceWindow(m, h, Mode);
  1416. break;
  1417. }
  1418. GH.pushInt(nwindow);
  1419. }
  1420. void CTradeWindow::artifactSelected(CArtPlace *slot)
  1421. {
  1422. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  1423. items[1][0]->setArtInstance(slot->ourArt);
  1424. if(slot->ourArt)
  1425. hLeft = items[1][0];
  1426. else
  1427. hLeft = nullptr;
  1428. selectionChanged(true);
  1429. }
  1430. std::string CMarketplaceWindow::getBackgroundForMode(EMarketMode::EMarketMode mode)
  1431. {
  1432. switch(mode)
  1433. {
  1434. case EMarketMode::RESOURCE_RESOURCE:
  1435. return "TPMRKRES.bmp";
  1436. case EMarketMode::RESOURCE_PLAYER:
  1437. return "TPMRKPTS.bmp";
  1438. case EMarketMode::CREATURE_RESOURCE:
  1439. return "TPMRKCRS.bmp";
  1440. case EMarketMode::RESOURCE_ARTIFACT:
  1441. return "TPMRKABS.bmp";
  1442. case EMarketMode::ARTIFACT_RESOURCE:
  1443. return "TPMRKASS.bmp";
  1444. }
  1445. assert(0);
  1446. return "";
  1447. }
  1448. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  1449. : CTradeWindow(getBackgroundForMode(Mode), Market, Hero, Mode)
  1450. {
  1451. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1452. madeTransaction = false;
  1453. bool sliderNeeded = true;
  1454. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1455. std::string title;
  1456. if (market->o->ID == Obj::TOWN)
  1457. {
  1458. switch (mode)
  1459. {
  1460. break; case EMarketMode::CREATURE_RESOURCE:
  1461. title = CGI->townh->factions[ETownType::STRONGHOLD]->town->buildings[BuildingID::FREELANCERS_GUILD]->Name();
  1462. break; case EMarketMode::RESOURCE_ARTIFACT:
  1463. title = CGI->townh->factions[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->Name();
  1464. sliderNeeded = false;
  1465. break; case EMarketMode::ARTIFACT_RESOURCE:
  1466. title = CGI->townh->factions[market->o->subID]->town->buildings[BuildingID::ARTIFACT_MERCHANT]->Name();
  1467. sliderNeeded = false;
  1468. break; default:
  1469. title = CGI->generaltexth->allTexts[158];
  1470. }
  1471. }
  1472. else
  1473. {
  1474. switch (market->o->ID)
  1475. {
  1476. break; case Obj::BLACK_MARKET: title = CGI->generaltexth->allTexts[349];
  1477. break; case Obj::TRADING_POST: title = CGI->generaltexth->allTexts[159];
  1478. break; case Obj::TRADING_POST_SNOW: title = CGI->generaltexth->allTexts[159];
  1479. break; default: title = market->o->getObjectName();
  1480. }
  1481. }
  1482. new CLabel(300, 27, FONT_BIG, CENTER, Colors::YELLOW, title);
  1483. initItems(false);
  1484. initItems(true);
  1485. ok = new CAdventureMapButton(CGI->generaltexth->zelp[600],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  1486. ok->assignedKeys.insert(SDLK_ESCAPE);
  1487. deal = new CAdventureMapButton(CGI->generaltexth->zelp[595],boost::bind(&CMarketplaceWindow::makeDeal,this),307,520,"TPMRKB.DEF");
  1488. deal->block(true);
  1489. if(sliderNeeded)
  1490. {
  1491. slider = new CSlider(231,490,137,nullptr,0,0);
  1492. slider->moved = boost::bind(&CMarketplaceWindow::sliderMoved,this,_1);
  1493. max = new CAdventureMapButton(CGI->generaltexth->zelp[596],boost::bind(&CMarketplaceWindow::setMax,this),229,520,"IRCBTNS.DEF");
  1494. max->block(true);
  1495. }
  1496. else
  1497. {
  1498. slider = nullptr;
  1499. max = nullptr;
  1500. deal->moveBy(Point(-30, 0));
  1501. }
  1502. Rect traderTextRect;
  1503. //left side
  1504. switch(Mode)
  1505. {
  1506. case EMarketMode::RESOURCE_RESOURCE:
  1507. case EMarketMode::RESOURCE_PLAYER:
  1508. case EMarketMode::RESOURCE_ARTIFACT:
  1509. new CLabel(154, 148, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[270]);
  1510. break;
  1511. case EMarketMode::CREATURE_RESOURCE:
  1512. //%s's Creatures
  1513. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::WHITE,
  1514. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  1515. break;
  1516. case EMarketMode::ARTIFACT_RESOURCE:
  1517. //%s's Artifacts
  1518. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::WHITE,
  1519. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  1520. break;
  1521. }
  1522. //right side
  1523. switch(Mode)
  1524. {
  1525. case EMarketMode::RESOURCE_RESOURCE:
  1526. case EMarketMode::CREATURE_RESOURCE:
  1527. case EMarketMode::RESOURCE_ARTIFACT:
  1528. case EMarketMode::ARTIFACT_RESOURCE:
  1529. new CLabel(445, 148, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[168]);
  1530. traderTextRect = Rect(316, 48, 260, 75);
  1531. break;
  1532. case EMarketMode::RESOURCE_PLAYER:
  1533. new CLabel(445, 55, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[169]);
  1534. traderTextRect = Rect(28, 48, 260, 75);
  1535. break;
  1536. }
  1537. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  1538. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  1539. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  1540. new CAdventureMapButton(CGI->generaltexth->zelp[612],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_PLAYER), 18, 520,"TPMRKBU1.DEF");
  1541. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  1542. new CAdventureMapButton(CGI->generaltexth->zelp[605],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_RESOURCE), 516, 450 + specialOffset,"TPMRKBU5.DEF");
  1543. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  1544. new CAdventureMapButton(CGI->generaltexth->zelp[599],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::CREATURE_RESOURCE), 516, 485,"TPMRKBU4.DEF"); //was y=450, changed to not overlap res-res in some conditions
  1545. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  1546. new CAdventureMapButton(CGI->generaltexth->zelp[598],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_ARTIFACT), 18, 450 + specialOffset,"TPMRKBU2.DEF");
  1547. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  1548. new CAdventureMapButton(CGI->generaltexth->zelp[613],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::ARTIFACT_RESOURCE), 18, 485,"TPMRKBU3.DEF"); //was y=450, changed to not overlap res-art in some conditions
  1549. updateTraderText();
  1550. }
  1551. CMarketplaceWindow::~CMarketplaceWindow()
  1552. {
  1553. hLeft = hRight = nullptr;
  1554. for(auto & elem : items[1])
  1555. delete elem;
  1556. for(auto & elem : items[0])
  1557. delete elem;
  1558. items[1].clear();
  1559. items[0].clear();
  1560. }
  1561. void CMarketplaceWindow::setMax()
  1562. {
  1563. slider->moveToMax();
  1564. }
  1565. void CMarketplaceWindow::makeDeal()
  1566. {
  1567. int sliderValue = 0;
  1568. if(slider)
  1569. sliderValue = slider->value;
  1570. else
  1571. sliderValue = !deal->isBlocked(); //should always be 1
  1572. if(!sliderValue)
  1573. return;
  1574. int leftIdToSend = -1;
  1575. switch (mode)
  1576. {
  1577. case EMarketMode::CREATURE_RESOURCE:
  1578. leftIdToSend = hLeft->serial;
  1579. break;
  1580. case EMarketMode::ARTIFACT_RESOURCE:
  1581. leftIdToSend = hLeft->getArtInstance()->id.getNum();
  1582. break;
  1583. default:
  1584. leftIdToSend = hLeft->id;
  1585. break;
  1586. }
  1587. if(slider)
  1588. {
  1589. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->value*r1, hero);
  1590. slider->moveTo(0);
  1591. }
  1592. else
  1593. {
  1594. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  1595. }
  1596. madeTransaction = true;
  1597. hLeft = nullptr;
  1598. hRight = nullptr;
  1599. selectionChanged(true);
  1600. }
  1601. void CMarketplaceWindow::sliderMoved( int to )
  1602. {
  1603. redraw();
  1604. }
  1605. void CMarketplaceWindow::selectionChanged(bool side)
  1606. {
  1607. readyToTrade = hLeft && hRight;
  1608. if(mode == EMarketMode::RESOURCE_RESOURCE)
  1609. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  1610. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  1611. arts->unmarkSlots(false);
  1612. if(readyToTrade)
  1613. {
  1614. int soldItemId = hLeft->id;
  1615. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  1616. if(slider)
  1617. {
  1618. int newAmount = -1;
  1619. if(itemsType[1] == RESOURCE)
  1620. newAmount = LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId));
  1621. else if(itemsType[1] == CREATURE)
  1622. newAmount = hero->getStackCount(SlotID(hLeft->serial)) - (hero->Slots().size() == 1 && hero->needsLastStack());
  1623. else
  1624. assert(0);
  1625. slider->setAmount(newAmount / r1);
  1626. slider->moveTo(0);
  1627. max->block(false);
  1628. deal->block(false);
  1629. }
  1630. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  1631. {
  1632. deal->block(LOCPLINT->cb->getResourceAmount(static_cast<Res::ERes>(soldItemId)) < r1);
  1633. }
  1634. else
  1635. deal->block(false);
  1636. }
  1637. else
  1638. {
  1639. if(slider)
  1640. {
  1641. max->block(true);
  1642. slider->setAmount(0);
  1643. slider->moveTo(0);
  1644. }
  1645. deal->block(true);
  1646. }
  1647. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  1648. initSubs(false);
  1649. updateTraderText();
  1650. redraw();
  1651. }
  1652. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  1653. {
  1654. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  1655. }
  1656. void CMarketplaceWindow::garrisonChanged()
  1657. {
  1658. if(mode != EMarketMode::CREATURE_RESOURCE)
  1659. return;
  1660. std::set<CTradeableItem *> toRemove;
  1661. getEmptySlots(toRemove);
  1662. removeItems(toRemove);
  1663. initSubs(true);
  1664. }
  1665. void CMarketplaceWindow::artifactsChanged(bool Left)
  1666. {
  1667. assert(!Left);
  1668. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  1669. return;
  1670. std::vector<int> available = market->availableItemsIds(mode);
  1671. std::set<CTradeableItem *> toRemove;
  1672. for(CTradeableItem *t : items[0])
  1673. if(!vstd::contains(available, t->id))
  1674. toRemove.insert(t);
  1675. removeItems(toRemove);
  1676. redraw();
  1677. }
  1678. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  1679. {
  1680. if(Left)
  1681. {
  1682. switch(itemsType[1])
  1683. {
  1684. case RESOURCE:
  1685. case CREATURE:
  1686. {
  1687. int val = slider
  1688. ? slider->value * r1
  1689. : (((deal->isBlocked())) ? 0 : r1);
  1690. return boost::lexical_cast<std::string>(val);
  1691. }
  1692. case ARTIFACT_INSTANCE:
  1693. return ((deal->isBlocked()) ? "0" : "1");
  1694. }
  1695. }
  1696. else
  1697. {
  1698. switch(itemsType[0])
  1699. {
  1700. case RESOURCE:
  1701. if(slider)
  1702. return boost::lexical_cast<std::string>( slider->value * r2 );
  1703. else
  1704. return boost::lexical_cast<std::string>(r2);
  1705. case ARTIFACT_TYPE:
  1706. return ((deal->isBlocked()) ? "0" : "1");
  1707. case PLAYER:
  1708. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  1709. }
  1710. }
  1711. return "???";
  1712. }
  1713. Point CMarketplaceWindow::selectionOffset(bool Left) const
  1714. {
  1715. if(Left)
  1716. {
  1717. switch(itemsType[1])
  1718. {
  1719. case RESOURCE:
  1720. return Point(122, 446);
  1721. case CREATURE:
  1722. return Point(128, 450);
  1723. case ARTIFACT_INSTANCE:
  1724. return Point(134, 466);
  1725. }
  1726. }
  1727. else
  1728. {
  1729. switch(itemsType[0])
  1730. {
  1731. case RESOURCE:
  1732. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1733. return Point(410, 469);
  1734. else
  1735. return Point(410, 446);
  1736. case ARTIFACT_TYPE:
  1737. return Point(425, 447);
  1738. case PLAYER:
  1739. return Point(417, 451);
  1740. }
  1741. }
  1742. assert(0);
  1743. return Point(0,0);
  1744. }
  1745. void CMarketplaceWindow::resourceChanged(int type, int val)
  1746. {
  1747. initSubs(true);
  1748. }
  1749. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  1750. {
  1751. switch(type)
  1752. {
  1753. case RESOURCE:
  1754. dx = 82;
  1755. dy = 79;
  1756. x = 39;
  1757. y = 180;
  1758. h = 66;
  1759. w = 74;
  1760. break;
  1761. case PLAYER:
  1762. dx = 83;
  1763. dy = 118;
  1764. h = 64;
  1765. w = 58;
  1766. x = 44;
  1767. y = 83;
  1768. assert(Right);
  1769. break;
  1770. case CREATURE://45,123
  1771. x = 45;
  1772. y = 123;
  1773. w = 58;
  1774. h = 64;
  1775. dx = 83;
  1776. dy = 98;
  1777. assert(!Right);
  1778. break;
  1779. case ARTIFACT_TYPE://45,123
  1780. x = 340-289;
  1781. y = 180;
  1782. w = 44;
  1783. h = 44;
  1784. dx = 83;
  1785. dy = 79;
  1786. break;
  1787. }
  1788. leftToRightOffset = 289;
  1789. }
  1790. void CMarketplaceWindow::updateTraderText()
  1791. {
  1792. if(readyToTrade)
  1793. {
  1794. if(mode == EMarketMode::RESOURCE_PLAYER)
  1795. {
  1796. //I can give %s to the %s player.
  1797. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  1798. }
  1799. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  1800. {
  1801. //I can offer you the %s for %d %s of %s.
  1802. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  1803. }
  1804. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  1805. {
  1806. //I can offer you %d %s of %s for %d %s of %s.
  1807. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[157]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  1808. }
  1809. else if(mode == EMarketMode::CREATURE_RESOURCE)
  1810. {
  1811. //I can offer you %d %s of %s for %d %s.
  1812. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  1813. }
  1814. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1815. {
  1816. //I can offer you %d %s of %s for your %s.
  1817. traderText->setText(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  1818. }
  1819. return;
  1820. }
  1821. int gnrtxtnr = -1;
  1822. if(madeTransaction)
  1823. {
  1824. if(mode == EMarketMode::RESOURCE_PLAYER)
  1825. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  1826. else
  1827. gnrtxtnr = 162; //You have received quite a bargain. I expect to make no profit on the deal. Can I interest you in any of my other wares?
  1828. }
  1829. else
  1830. {
  1831. if(mode == EMarketMode::RESOURCE_PLAYER)
  1832. gnrtxtnr = 167; //If you'd like to give any of your resources to another player, click on the item you wish to give and to whom.
  1833. else
  1834. gnrtxtnr = 163; //Please inspect our fine wares. If you feel like offering a trade, click on the items you wish to trade with and for.
  1835. }
  1836. traderText->setText(CGI->generaltexth->allTexts[gnrtxtnr]);
  1837. }
  1838. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= nullptr*/, EMarketMode::EMarketMode Mode)
  1839. :CTradeWindow((Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, Mode)
  1840. {
  1841. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1842. if(Mode == EMarketMode::CREATURE_EXP)
  1843. {
  1844. //%s's Creatures
  1845. new CLabel(155, 30, FONT_SMALL, CENTER, Colors::YELLOW,
  1846. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  1847. //Altar of Sacrifice
  1848. new CLabel(450, 30, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[479]);
  1849. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  1850. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  1851. slider = new CSlider(231,481,137,nullptr,0,0);
  1852. slider->moved = boost::bind(&CAltarWindow::sliderMoved,this,_1);
  1853. max = new CAdventureMapButton(CGI->generaltexth->zelp[578],boost::bind(&CSlider::moveToMax, slider),147,520,"IRCBTNS.DEF");
  1854. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  1855. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[579],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTARMY.DEF");
  1856. sacrificeBackpack = nullptr;
  1857. initItems(true);
  1858. mimicCres();
  1859. artIcon = nullptr;
  1860. }
  1861. else
  1862. {
  1863. //Sacrifice artifacts for experience
  1864. new CLabel(450, 34, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[477]);
  1865. //%s's Creatures
  1866. new CLabel(302, 423, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[478]);
  1867. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[571], boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTFILL.DEF");
  1868. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  1869. sacrificeBackpack = new CAdventureMapButton(CGI->generaltexth->zelp[570],boost::bind(&CAltarWindow::SacrificeBackpack,this),147,520,"ALTEMBK.DEF");
  1870. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  1871. slider = nullptr;
  1872. max = nullptr;
  1873. initItems(true);
  1874. initItems(false);
  1875. artIcon = new CAnimImage("ARTIFACT", 0, 0, 281, 442);
  1876. artIcon->disable();
  1877. }
  1878. //Experience needed to reach next level
  1879. new CTextBox(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  1880. //Total experience on the Altar
  1881. new CTextBox(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, CENTER, Colors::YELLOW);
  1882. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1883. ok = new CAdventureMapButton(CGI->generaltexth->zelp[568],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  1884. ok->assignedKeys.insert(SDLK_ESCAPE);
  1885. deal = new CAdventureMapButton(CGI->generaltexth->zelp[585],boost::bind(&CAltarWindow::makeDeal,this),269,520,"ALTSACR.DEF");
  1886. if(Hero->getAlignment() != ::EAlignment::EVIL && Mode == EMarketMode::CREATURE_EXP)
  1887. new CAdventureMapButton(CGI->generaltexth->zelp[580], boost::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP), 516, 421, "ALTART.DEF");
  1888. if(Hero->getAlignment() != ::EAlignment::GOOD && Mode == EMarketMode::ARTIFACT_EXP)
  1889. new CAdventureMapButton(CGI->generaltexth->zelp[572], boost::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP), 516, 421, "ALTSACC.DEF");
  1890. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  1891. getExpValues();
  1892. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  1893. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  1894. setExpToLevel();
  1895. calcTotalExp();
  1896. blockTrade();
  1897. }
  1898. CAltarWindow::~CAltarWindow()
  1899. {
  1900. }
  1901. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  1902. {
  1903. leftToRightOffset = 289;
  1904. x = 45;
  1905. y = 110;
  1906. w = 58;
  1907. h = 64;
  1908. dx = 83;
  1909. dy = 98;
  1910. }
  1911. void CAltarWindow::sliderMoved(int to)
  1912. {
  1913. sacrificedUnits[hLeft->serial] = to;
  1914. updateRight(hRight);
  1915. deal->block(!to);
  1916. calcTotalExp();
  1917. redraw();
  1918. }
  1919. void CAltarWindow::makeDeal()
  1920. {
  1921. if(mode == EMarketMode::CREATURE_EXP)
  1922. {
  1923. blockTrade();
  1924. slider->value = 0;
  1925. std::vector<int> toSacrifice = sacrificedUnits;
  1926. for (int i = 0; i < toSacrifice.size(); i++)
  1927. {
  1928. if(toSacrifice[i])
  1929. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  1930. }
  1931. for(int& val : sacrificedUnits)
  1932. val = 0;
  1933. for(CTradeableItem *t : items[0])
  1934. {
  1935. t->setType(CREATURE_PLACEHOLDER);
  1936. t->subtitle = "";
  1937. }
  1938. }
  1939. else
  1940. {
  1941. for(const CArtifactInstance *art : arts->artifactsOnAltar) //sacrifice each artifact on the list
  1942. {
  1943. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  1944. }
  1945. arts->artifactsOnAltar.clear();
  1946. for(CTradeableItem *t : items[0])
  1947. {
  1948. t->setID(-1);
  1949. t->subtitle = "";
  1950. }
  1951. arts->commonInfo->reset();
  1952. //arts->scrollBackpack(0);
  1953. deal->block(true);
  1954. }
  1955. calcTotalExp();
  1956. }
  1957. void CAltarWindow::SacrificeAll()
  1958. {
  1959. if(mode == EMarketMode::CREATURE_EXP)
  1960. {
  1961. bool movedAnything = false;
  1962. for(CTradeableItem *t : items[1])
  1963. sacrificedUnits[t->serial] = hero->getStackCount(SlotID(t->serial));
  1964. sacrificedUnits[items[1].front()->serial]--;
  1965. for(CTradeableItem *t : items[0])
  1966. {
  1967. updateRight(t);
  1968. if(t->type == CREATURE)
  1969. movedAnything = true;
  1970. }
  1971. deal->block(!movedAnything);
  1972. calcTotalExp();
  1973. }
  1974. else
  1975. {
  1976. for(auto i = hero->artifactsWorn.cbegin(); i != hero->artifactsWorn.cend(); i++)
  1977. {
  1978. if(i->second.artifact->artType->id != ArtifactID::ART_LOCK) //ignore locks from assembled artifacts
  1979. moveFromSlotToAltar(i->first, nullptr, i->second.artifact);
  1980. }
  1981. SacrificeBackpack();
  1982. }
  1983. redraw();
  1984. }
  1985. void CAltarWindow::selectionChanged(bool side)
  1986. {
  1987. if(mode != EMarketMode::CREATURE_EXP)
  1988. return;
  1989. CTradeableItem *&selected = side ? hLeft : hRight;
  1990. CTradeableItem *&theOther = side ? hRight : hLeft;
  1991. theOther = *std::find_if(items[!side].begin(), items[!side].end(), [&](const CTradeableItem * item)
  1992. {
  1993. return item->serial == selected->serial;
  1994. });
  1995. int stackCount = 0;
  1996. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  1997. if(hero->getStackCount(SlotID(i)) > sacrificedUnits[i])
  1998. stackCount++;
  1999. slider->setAmount(hero->getStackCount(SlotID(hLeft->serial)) - (stackCount == 1));
  2000. slider->block(!slider->amount);
  2001. slider->value = sacrificedUnits[hLeft->serial];
  2002. max->block(!slider->amount);
  2003. readyToTrade = true;
  2004. redraw();
  2005. }
  2006. void CAltarWindow::mimicCres()
  2007. {
  2008. std::vector<Rect> positions;
  2009. getPositionsFor(positions, false, CREATURE);
  2010. for(CTradeableItem *t : items[1])
  2011. {
  2012. auto hlp = new CTradeableItem(positions[t->serial].topLeft(), CREATURE_PLACEHOLDER, t->id, false, t->serial);
  2013. hlp->pos = positions[t->serial] + this->pos.topLeft();
  2014. items[0].push_back(hlp);
  2015. }
  2016. }
  2017. Point CAltarWindow::selectionOffset(bool Left) const
  2018. {
  2019. if(Left)
  2020. return Point(150, 421);
  2021. else
  2022. return Point(396, 421);
  2023. }
  2024. std::string CAltarWindow::selectionSubtitle(bool Left) const
  2025. {
  2026. if(Left && slider && hLeft)
  2027. return boost::lexical_cast<std::string>(slider->value);
  2028. else if(!Left && hRight)
  2029. return hRight->subtitle;
  2030. else
  2031. return "";
  2032. }
  2033. void CAltarWindow::artifactsChanged(bool left)
  2034. {
  2035. }
  2036. void CAltarWindow::garrisonChanged()
  2037. {
  2038. if(mode != EMarketMode::CREATURE_EXP)
  2039. return;
  2040. std::set<CTradeableItem *> empty;
  2041. getEmptySlots(empty);
  2042. for(CTradeableItem *t : empty)
  2043. {
  2044. removeItem(*std::find_if(items[0].begin(), items[0].end(), [&](const CTradeableItem * item)
  2045. {
  2046. return item->serial == t->serial;
  2047. }));
  2048. }
  2049. initSubs(true);
  2050. getExpValues();
  2051. }
  2052. void CAltarWindow::getExpValues()
  2053. {
  2054. int dump;
  2055. for(CTradeableItem *t : items[1])
  2056. if(t->id >= 0)
  2057. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], EMarketMode::CREATURE_EXP);
  2058. }
  2059. void CAltarWindow::calcTotalExp()
  2060. {
  2061. int val = 0;
  2062. if(mode == EMarketMode::CREATURE_EXP)
  2063. {
  2064. for (int i = 0; i < sacrificedUnits.size(); i++)
  2065. {
  2066. val += expPerUnit[i] * sacrificedUnits[i];
  2067. }
  2068. }
  2069. else
  2070. {
  2071. for(const CArtifactInstance *art : arts->artifactsOnAltar)
  2072. {
  2073. int dmp, valOfArt;
  2074. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  2075. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  2076. }
  2077. }
  2078. val = hero->calculateXp(val);
  2079. expOnAltar->setText(boost::lexical_cast<std::string>(val));
  2080. }
  2081. void CAltarWindow::setExpToLevel()
  2082. {
  2083. expToLevel->setText(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  2084. }
  2085. void CAltarWindow::blockTrade()
  2086. {
  2087. hLeft = hRight = nullptr;
  2088. readyToTrade = false;
  2089. if(slider)
  2090. {
  2091. slider->block(true);
  2092. max->block(true);
  2093. }
  2094. deal->block(true);
  2095. }
  2096. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  2097. {
  2098. int val = sacrificedUnits[toUpdate->serial];
  2099. toUpdate->setType(val ? CREATURE : CREATURE_PLACEHOLDER);
  2100. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  2101. }
  2102. int CAltarWindow::firstFreeSlot()
  2103. {
  2104. int ret = -1;
  2105. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  2106. return ret < items[0].size() ? ret : -1;
  2107. }
  2108. void CAltarWindow::SacrificeBackpack()
  2109. {
  2110. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  2111. for (auto & elem : hero->artifactsInBackpack)
  2112. {
  2113. if(vstd::contains(toOmmit, elem.artifact))
  2114. {
  2115. toOmmit -= elem.artifact;
  2116. continue;
  2117. }
  2118. putOnAltar(nullptr, elem.artifact);
  2119. }
  2120. arts->scrollBackpack(0);
  2121. calcTotalExp();
  2122. }
  2123. void CAltarWindow::artifactPicked()
  2124. {
  2125. redraw();
  2126. }
  2127. void CAltarWindow::showAll(SDL_Surface * to)
  2128. {
  2129. CTradeWindow::showAll(to);
  2130. if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  2131. {
  2132. artIcon->setFrame(arts->commonInfo->src.art->artType->iconIndex);
  2133. artIcon->showAll(to);
  2134. int dmp, val;
  2135. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  2136. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, Colors::WHITE, to);
  2137. }
  2138. }
  2139. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  2140. {
  2141. int artID = art->artType->id;
  2142. if(artID != 1 && artID < 7) //special art
  2143. {
  2144. logGlobal->warnStream() << "Cannot put special artifact on altar!";
  2145. return false;
  2146. }
  2147. if(!altarSlot)
  2148. {
  2149. int slotIndex = firstFreeSlot();
  2150. if(slotIndex < 0)
  2151. {
  2152. logGlobal->warnStream() << "No free slots on altar!";
  2153. return false;
  2154. }
  2155. altarSlot = items[0][slotIndex];
  2156. }
  2157. int dmp, val;
  2158. market->getOffer(artID, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  2159. arts->artifactsOnAltar.insert(art);
  2160. altarSlot->setArtInstance(art);
  2161. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  2162. deal->block(false);
  2163. return true;
  2164. }
  2165. void CAltarWindow::moveFromSlotToAltar(ArtifactPosition slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  2166. {
  2167. auto freeBackpackSlot = ArtifactPosition(hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  2168. if(arts->commonInfo->src.art)
  2169. {
  2170. arts->commonInfo->dst.slotID = freeBackpackSlot;
  2171. arts->commonInfo->dst.AOH = arts;
  2172. }
  2173. if(putOnAltar(altarSlot, art))
  2174. {
  2175. if(slotID < GameConstants::BACKPACK_START)
  2176. LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slotID), ArtifactLocation(hero, freeBackpackSlot));
  2177. else
  2178. {
  2179. arts->commonInfo->src.clear();
  2180. arts->commonInfo->dst.clear();
  2181. CCS->curh->dragAndDropCursor(nullptr);
  2182. arts->unmarkSlots(false);
  2183. }
  2184. }
  2185. }
  2186. void CSystemOptionsWindow::setMusicVolume( int newVolume )
  2187. {
  2188. Settings volume = settings.write["general"]["music"];
  2189. volume->Float() = newVolume;
  2190. }
  2191. void CSystemOptionsWindow::setSoundVolume( int newVolume )
  2192. {
  2193. Settings volume = settings.write["general"]["sound"];
  2194. volume->Float() = newVolume;
  2195. }
  2196. void CSystemOptionsWindow::setHeroMoveSpeed( int newSpeed )
  2197. {
  2198. Settings speed = settings.write["adventure"]["heroSpeed"];
  2199. speed->Float() = newSpeed;
  2200. }
  2201. void CSystemOptionsWindow::setMapScrollingSpeed( int newSpeed )
  2202. {
  2203. Settings speed = settings.write["adventure"]["scrollSpeed"];
  2204. speed->Float() = newSpeed;
  2205. }
  2206. CSystemOptionsWindow::CSystemOptionsWindow():
  2207. CWindowObject(PLAYER_COLORED, "SysOpBck"),
  2208. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  2209. {
  2210. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2211. title = new CLabel(242, 32, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[568]);
  2212. const JsonNode & texts = CGI->generaltexth->localizedTexts["systemOptions"];
  2213. //left window section
  2214. leftGroup = new CLabelGroup(FONT_MEDIUM, CENTER, Colors::YELLOW);
  2215. leftGroup->add(122, 64, CGI->generaltexth->allTexts[569]);
  2216. leftGroup->add(122, 130, CGI->generaltexth->allTexts[570]);
  2217. leftGroup->add(122, 196, CGI->generaltexth->allTexts[571]);
  2218. leftGroup->add(122, 262, texts["resolutionButton"]["label"].String());
  2219. leftGroup->add(122, 347, CGI->generaltexth->allTexts[394]);
  2220. leftGroup->add(122, 412, CGI->generaltexth->allTexts[395]);
  2221. //right section
  2222. rightGroup = new CLabelGroup(FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  2223. rightGroup->add(282, 57, CGI->generaltexth->allTexts[572]);
  2224. rightGroup->add(282, 89, CGI->generaltexth->allTexts[573]);
  2225. rightGroup->add(282, 121, CGI->generaltexth->allTexts[574]);
  2226. rightGroup->add(282, 153, CGI->generaltexth->allTexts[577]);
  2227. rightGroup->add(282, 185, texts["creatureWindowButton"]["label"].String());
  2228. rightGroup->add(282, 217, texts["fullscreenButton"]["label"].String());
  2229. //setting up buttons
  2230. load = new CAdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second,
  2231. boost::bind(&CSystemOptionsWindow::bloadf, this), 246, 298, "SOLOAD.DEF", SDLK_l);
  2232. load->swappedImages = true;
  2233. load->update();
  2234. save = new CAdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second,
  2235. boost::bind(&CSystemOptionsWindow::bsavef, this), 357, 298, "SOSAVE.DEF", SDLK_s);
  2236. save->swappedImages = true;
  2237. save->update();
  2238. restart = new CAdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second,
  2239. boost::bind(&CSystemOptionsWindow::brestartf, this), 246, 357, "SORSTRT", SDLK_r);
  2240. restart->swappedImages = true;
  2241. restart->update();
  2242. mainMenu = new CAdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second,
  2243. boost::bind(&CSystemOptionsWindow::bmainmenuf, this), 357, 357, "SOMAIN.DEF", SDLK_m);
  2244. mainMenu->swappedImages = true;
  2245. mainMenu->update();
  2246. quitGame = new CAdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second,
  2247. boost::bind(&CSystemOptionsWindow::bquitf, this), 246, 415, "soquit.def", SDLK_q);
  2248. quitGame->swappedImages = true;
  2249. quitGame->update();
  2250. backToMap = new CAdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second,
  2251. boost::bind(&CSystemOptionsWindow::breturnf, this), 357, 415, "soretrn.def", SDLK_RETURN);
  2252. backToMap->swappedImages = true;
  2253. backToMap->update();
  2254. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  2255. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  2256. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", 28, 77, 1);
  2257. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", 76, 77, 2);
  2258. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", 124, 77, 4);
  2259. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", 172, 77, 8);
  2260. heroMoveSpeed->select(settings["adventure"]["heroSpeed"].Float(), 1);
  2261. heroMoveSpeed->onChange = boost::bind(&CSystemOptionsWindow::setHeroMoveSpeed, this, _1);
  2262. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  2263. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", 28, 210, 1);
  2264. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", 92, 210, 2);
  2265. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", 156, 210, 4);
  2266. mapScrollSpeed->select(settings["adventure"]["scrollSpeed"].Float(), 1);
  2267. mapScrollSpeed->onChange = boost::bind(&CSystemOptionsWindow::setMapScrollingSpeed, this, _1);
  2268. musicVolume = new CHighlightableButtonsGroup(0, true);
  2269. for(int i=0; i<10; ++i)
  2270. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", 29 + 19*i, 359, i*11);
  2271. musicVolume->select(CCS->musich->getVolume(), 1);
  2272. musicVolume->onChange = boost::bind(&CSystemOptionsWindow::setMusicVolume, this, _1);
  2273. effectsVolume = new CHighlightableButtonsGroup(0, true);
  2274. for(int i=0; i<10; ++i)
  2275. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", 29 + 19*i, 425, i*11);
  2276. effectsVolume->select(CCS->soundh->getVolume(), 1);
  2277. effectsVolume->onChange = boost::bind(&CSystemOptionsWindow::setSoundVolume, this, _1);
  2278. showReminder = new CHighlightableButton(
  2279. boost::bind(&CSystemOptionsWindow::toggleReminder, this, true), boost::bind(&CSystemOptionsWindow::toggleReminder, this, false),
  2280. std::map<int,std::string>(), CGI->generaltexth->zelp[361].second, false, "sysopchk.def", nullptr, 246, 87, false);
  2281. quickCombat = new CHighlightableButton(
  2282. boost::bind(&CSystemOptionsWindow::toggleQuickCombat, this, true), boost::bind(&CSystemOptionsWindow::toggleQuickCombat, this, false),
  2283. std::map<int,std::string>(), CGI->generaltexth->zelp[362].second, false, "sysopchk.def", nullptr, 246, 87+32, false);
  2284. spellbookAnim = new CHighlightableButton(
  2285. boost::bind(&CSystemOptionsWindow::toggleSpellbookAnim, this, true), boost::bind(&CSystemOptionsWindow::toggleSpellbookAnim, this, false),
  2286. std::map<int,std::string>(), CGI->generaltexth->zelp[364].second, false, "sysopchk.def", nullptr, 246, 87+64, false);
  2287. newCreatureWin = new CHighlightableButton(
  2288. boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, true), boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, false),
  2289. std::map<int,std::string>(), texts["creatureWindowButton"]["help"].String(), false, "sysopchk.def", nullptr, 246, 183, false);
  2290. fullscreen = new CHighlightableButton(
  2291. boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, true), boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, false),
  2292. std::map<int,std::string>(), texts["fullscreenButton"]["help"].String(), false, "sysopchk.def", nullptr, 246, 215, false);
  2293. showReminder->select(settings["adventure"]["heroReminder"].Bool());
  2294. quickCombat->select(settings["adventure"]["quickCombat"].Bool());
  2295. spellbookAnim->select(settings["video"]["spellbookAnimation"].Bool());
  2296. newCreatureWin->select(settings["general"]["classicCreatureWindow"].Bool());
  2297. fullscreen->select(settings["video"]["fullscreen"].Bool());
  2298. onFullscreenChanged([&](const JsonNode &newState){ fullscreen->select(newState.Bool());});
  2299. gameResButton = new CAdventureMapButton("", texts["resolutionButton"]["help"].String(),
  2300. boost::bind(&CSystemOptionsWindow::selectGameRes, this),
  2301. 28, 275,"SYSOB12", SDLK_g);
  2302. std::string resText;
  2303. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["width"].Float());
  2304. resText += "x";
  2305. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["height"].Float());
  2306. gameResLabel = new CLabel(170, 292, FONT_MEDIUM, CENTER, Colors::YELLOW, resText);
  2307. }
  2308. void CSystemOptionsWindow::selectGameRes()
  2309. {
  2310. std::vector<std::string> items;
  2311. const JsonNode & texts = CGI->generaltexth->localizedTexts["systemOptions"]["resolutionMenu"];
  2312. for( config::CConfigHandler::GuiOptionsMap::value_type& value : conf.guiOptions)
  2313. {
  2314. std::string resX = boost::lexical_cast<std::string>(value.first.first);
  2315. std::string resY = boost::lexical_cast<std::string>(value.first.second);
  2316. items.push_back(resX + 'x' + resY);
  2317. }
  2318. GH.pushInt(new CObjectListWindow(items, nullptr, texts["label"].String(), texts["help"].String(),
  2319. boost::bind(&CSystemOptionsWindow::setGameRes, this, _1)));
  2320. }
  2321. void CSystemOptionsWindow::setGameRes(int index)
  2322. {
  2323. auto iter = conf.guiOptions.begin();
  2324. std::advance(iter, index);
  2325. //do not set resolution to illegal one (0x0)
  2326. assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
  2327. Settings gameRes = settings.write["video"]["screenRes"];
  2328. gameRes["width"].Float() = iter->first.first;
  2329. gameRes["height"].Float() = iter->first.second;
  2330. std::string resText;
  2331. resText += boost::lexical_cast<std::string>(iter->first.first);
  2332. resText += "x";
  2333. resText += boost::lexical_cast<std::string>(iter->first.second);
  2334. gameResLabel->setText(resText);
  2335. }
  2336. void CSystemOptionsWindow::toggleReminder(bool on)
  2337. {
  2338. Settings heroReminder = settings.write["adventure"]["heroReminder"];
  2339. heroReminder->Bool() = on;
  2340. }
  2341. void CSystemOptionsWindow::toggleQuickCombat(bool on)
  2342. {
  2343. Settings quickCombat = settings.write["adventure"]["quickCombat"];
  2344. quickCombat->Bool() = on;
  2345. }
  2346. void CSystemOptionsWindow::toggleSpellbookAnim(bool on)
  2347. {
  2348. Settings quickCombat = settings.write["video"]["spellbookAnimation"];
  2349. quickCombat->Bool() = on;
  2350. }
  2351. void CSystemOptionsWindow::toggleCreatureWin(bool on)
  2352. {
  2353. Settings classicCreatureWindow = settings.write["general"]["classicCreatureWindow"];
  2354. classicCreatureWindow->Bool() = on;
  2355. }
  2356. void CSystemOptionsWindow::toggleFullscreen(bool on)
  2357. {
  2358. Settings fullscreen = settings.write["video"]["fullscreen"];
  2359. fullscreen->Bool() = on;
  2360. }
  2361. void CSystemOptionsWindow::bquitf()
  2362. {
  2363. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this]{ closeAndPushEvent(SDL_QUIT); }, 0);
  2364. }
  2365. void CSystemOptionsWindow::breturnf()
  2366. {
  2367. GH.popIntTotally(this);
  2368. }
  2369. void CSystemOptionsWindow::bmainmenuf()
  2370. {
  2371. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this]{ closeAndPushEvent(SDL_USEREVENT, RETURN_TO_MAIN_MENU); }, 0);
  2372. }
  2373. void CSystemOptionsWindow::bloadf()
  2374. {
  2375. GH.popIntTotally(this);
  2376. LOCPLINT->proposeLoadingGame();
  2377. }
  2378. void CSystemOptionsWindow::bsavef()
  2379. {
  2380. GH.popIntTotally(this);
  2381. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  2382. }
  2383. void CSystemOptionsWindow::brestartf()
  2384. {
  2385. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this]{ closeAndPushEvent(SDL_USEREVENT, RESTART_GAME); }, 0);
  2386. }
  2387. void CSystemOptionsWindow::closeAndPushEvent(int eventType, int code /*= 0*/)
  2388. {
  2389. GH.popIntTotally(this);
  2390. GH.pushSDLEvent(eventType, code);
  2391. }
  2392. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj):
  2393. CWindowObject(PLAYER_COLORED, "TPTAVERN"),
  2394. tavernObj(TavernObj)
  2395. {
  2396. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2397. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  2398. if(h.size() < 2)
  2399. h.resize(2, nullptr);
  2400. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  2401. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  2402. selected = 0;
  2403. if (!h[0])
  2404. selected = 1;
  2405. if (!h[0] && !h[1])
  2406. selected = -1;
  2407. oldSelected = -1;
  2408. new CLabel(200, 35, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[37]);
  2409. new CLabel(320, 328, FONT_SMALL, CENTER, Colors::WHITE, "2500");
  2410. new CTextBox(LOCPLINT->cb->getTavernGossip(tavernObj), Rect(32, 190, 330, 68), 0, FONT_SMALL, CENTER, Colors::WHITE);
  2411. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  2412. cancel = new CAdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  2413. recruit = new CAdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  2414. thiefGuild = new CAdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  2415. if(LOCPLINT->cb->getResourceAmount(Res::GOLD) < 2500) //not enough gold
  2416. {
  2417. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  2418. recruit->block(true);
  2419. }
  2420. else if(LOCPLINT->castleInt && LOCPLINT->cb->howManyHeroes(true) >= VLC->modh->settings.MAX_HEROES_AVAILABLE_PER_PLAYER)
  2421. {
  2422. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  2423. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(true)));
  2424. recruit->block(true);
  2425. }
  2426. else if((!LOCPLINT->castleInt) && LOCPLINT->cb->howManyHeroes(false) >= VLC->modh->settings.MAX_HEROES_ON_MAP_PER_PLAYER)
  2427. {
  2428. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  2429. boost::algorithm::replace_first(recruit->hoverTexts[0], "%d", boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(false)));
  2430. recruit->block(true);
  2431. }
  2432. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  2433. {
  2434. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  2435. recruit->block(true);
  2436. }
  2437. else
  2438. {
  2439. if(selected == -1)
  2440. recruit->block(true);
  2441. }
  2442. if (LOCPLINT->castleInt)
  2443. CCS->videoh->open(LOCPLINT->castleInt->town->town->clientInfo.tavernVideo);
  2444. else
  2445. CCS->videoh->open("TAVERN.BIK");
  2446. }
  2447. void CTavernWindow::recruitb()
  2448. {
  2449. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  2450. const CGObjectInstance *obj = tavernObj;
  2451. close();
  2452. LOCPLINT->cb->recruitHero(obj, toBuy);
  2453. }
  2454. void CTavernWindow::thievesguildb()
  2455. {
  2456. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  2457. }
  2458. CTavernWindow::~CTavernWindow()
  2459. {
  2460. CCS->videoh->close();
  2461. }
  2462. void CTavernWindow::show(SDL_Surface * to)
  2463. {
  2464. CWindowObject::show(to);
  2465. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  2466. if(selected >= 0)
  2467. {
  2468. HeroPortrait *sel = selected ? h2 : h1;
  2469. if (selected != oldSelected && !recruit->isBlocked())
  2470. {
  2471. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  2472. oldSelected = selected;
  2473. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  2474. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  2475. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  2476. }
  2477. printAtMiddleWBLoc(sel->description, 146, 395, FONT_SMALL, 200, Colors::WHITE, to);
  2478. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  2479. }
  2480. }
  2481. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  2482. {
  2483. if(previousState && !down && h)
  2484. *_sel = _id;
  2485. }
  2486. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  2487. {
  2488. if(down && h)
  2489. {
  2490. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  2491. }
  2492. }
  2493. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  2494. : h(H), _sel(&sel), _id(id)
  2495. {
  2496. addUsedEvents(LCLICK | RCLICK | HOVER);
  2497. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2498. h = H;
  2499. pos.x += x;
  2500. pos.y += y;
  2501. pos.w = 58;
  2502. pos.h = 64;
  2503. if(H)
  2504. {
  2505. hoverName = CGI->generaltexth->tavernInfo[4];
  2506. boost::algorithm::replace_first(hoverName,"%s",H->name);
  2507. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  2508. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  2509. if(vstd::contains(h->artifactsWorn, ArtifactPosition(i)))
  2510. artifs--;
  2511. description = CGI->generaltexth->allTexts[215];
  2512. boost::algorithm::replace_first(description, "%s", h->name);
  2513. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(h->level));
  2514. boost::algorithm::replace_first(description, "%s", h->type->heroClass->name);
  2515. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(artifs));
  2516. new CAnimImage("portraitsLarge", h->portrait);
  2517. }
  2518. }
  2519. void CTavernWindow::HeroPortrait::hover( bool on )
  2520. {
  2521. //Hoverable::hover(on);
  2522. if(on)
  2523. GH.statusbar->setText(hoverName);
  2524. else
  2525. GH.statusbar->clear();
  2526. }
  2527. void CInGameConsole::show(SDL_Surface * to)
  2528. {
  2529. int number = 0;
  2530. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  2531. boost::unique_lock<boost::mutex> lock(texts_mx);
  2532. for(auto it = texts.begin(); it != texts.end(); ++it, ++number)
  2533. {
  2534. Point leftBottomCorner(0, screen->h);
  2535. if(LOCPLINT->battleInt)
  2536. {
  2537. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  2538. }
  2539. graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, it->first, Colors::GREEN,
  2540. Point(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20));
  2541. if(SDL_GetTicks() - it->second > defaultTimeout)
  2542. {
  2543. toDel.push_back(it);
  2544. }
  2545. }
  2546. for(auto & elem : toDel)
  2547. {
  2548. texts.erase(elem);
  2549. }
  2550. }
  2551. void CInGameConsole::print(const std::string &txt)
  2552. {
  2553. boost::unique_lock<boost::mutex> lock(texts_mx);
  2554. int lineLen = conf.go()->ac.outputLineLength;
  2555. if(txt.size() < lineLen)
  2556. {
  2557. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  2558. if(texts.size() > maxDisplayedTexts)
  2559. {
  2560. texts.pop_front();
  2561. }
  2562. }
  2563. else
  2564. {
  2565. assert(lineLen);
  2566. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  2567. {
  2568. std::string part = txt.substr(g * lineLen, lineLen);
  2569. if(part.size() == 0)
  2570. break;
  2571. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  2572. if(texts.size() > maxDisplayedTexts)
  2573. {
  2574. texts.pop_front();
  2575. }
  2576. }
  2577. }
  2578. }
  2579. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  2580. {
  2581. if(key.type != SDL_KEYDOWN) return;
  2582. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  2583. switch(key.keysym.sym)
  2584. {
  2585. case SDLK_TAB:
  2586. case SDLK_ESCAPE:
  2587. {
  2588. if(captureAllKeys)
  2589. {
  2590. captureAllKeys = false;
  2591. endEnteringText(false);
  2592. }
  2593. else if(SDLK_TAB)
  2594. {
  2595. captureAllKeys = true;
  2596. startEnteringText();
  2597. }
  2598. break;
  2599. }
  2600. case SDLK_RETURN: //enter key
  2601. {
  2602. if(enteredText.size() > 0 && captureAllKeys)
  2603. {
  2604. captureAllKeys = false;
  2605. endEnteringText(true);
  2606. CCS->soundh->playSound("CHAT");
  2607. }
  2608. break;
  2609. }
  2610. case SDLK_BACKSPACE:
  2611. {
  2612. if(enteredText.size() > 1)
  2613. {
  2614. Unicode::trimRight(enteredText,2);
  2615. enteredText += '_';
  2616. refreshEnteredText();
  2617. }
  2618. break;
  2619. }
  2620. case SDLK_UP: //up arrow
  2621. {
  2622. if(previouslyEntered.size() == 0)
  2623. break;
  2624. if(prevEntDisp == -1)
  2625. {
  2626. prevEntDisp = previouslyEntered.size() - 1;
  2627. enteredText = previouslyEntered[prevEntDisp] + "_";
  2628. refreshEnteredText();
  2629. }
  2630. else if( prevEntDisp > 0)
  2631. {
  2632. --prevEntDisp;
  2633. enteredText = previouslyEntered[prevEntDisp] + "_";
  2634. refreshEnteredText();
  2635. }
  2636. break;
  2637. }
  2638. case SDLK_DOWN: //down arrow
  2639. {
  2640. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  2641. {
  2642. ++prevEntDisp;
  2643. enteredText = previouslyEntered[prevEntDisp] + "_";
  2644. refreshEnteredText();
  2645. }
  2646. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  2647. {
  2648. prevEntDisp = -1;
  2649. enteredText = "_";
  2650. refreshEnteredText();
  2651. }
  2652. break;
  2653. }
  2654. default:
  2655. {
  2656. #ifdef VCMI_SDL1
  2657. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  2658. {
  2659. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  2660. {
  2661. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  2662. enteredText += "_";
  2663. refreshEnteredText();
  2664. }
  2665. }
  2666. #endif // VCMI_SDL1
  2667. break;
  2668. }
  2669. }
  2670. }
  2671. #ifndef VCMI_SDL1
  2672. void CInGameConsole::textInputed(const SDL_TextInputEvent & event)
  2673. {
  2674. if(!captureAllKeys || enteredText.size() == 0)
  2675. return;
  2676. enteredText.resize(enteredText.size()-1);
  2677. enteredText += event.text;
  2678. enteredText += "_";
  2679. refreshEnteredText();
  2680. }
  2681. void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
  2682. {
  2683. //do nothing here
  2684. }
  2685. #endif // VCMI_SDL1
  2686. void CInGameConsole::startEnteringText()
  2687. {
  2688. CSDL_Ext::startTextInput(&pos);
  2689. enteredText = "_";
  2690. if(GH.topInt() == adventureInt)
  2691. {
  2692. GH.statusbar->alignment = TOPLEFT;
  2693. GH.statusbar->setText(enteredText);
  2694. //Prevent changes to the text from mouse interaction with the adventure map
  2695. GH.statusbar->lock(true);
  2696. }
  2697. else if(LOCPLINT->battleInt)
  2698. {
  2699. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  2700. }
  2701. }
  2702. void CInGameConsole::endEnteringText(bool printEnteredText)
  2703. {
  2704. CSDL_Ext::stopTextInput();
  2705. prevEntDisp = -1;
  2706. if(printEnteredText)
  2707. {
  2708. std::string txt = enteredText.substr(0, enteredText.size()-1);
  2709. LOCPLINT->cb->sendMessage(txt);
  2710. previouslyEntered.push_back(txt);
  2711. //print(txt);
  2712. }
  2713. enteredText = "";
  2714. if(GH.topInt() == adventureInt)
  2715. {
  2716. GH.statusbar->alignment = CENTER;
  2717. GH.statusbar->lock(false);
  2718. GH.statusbar->clear();
  2719. }
  2720. else if(LOCPLINT->battleInt)
  2721. {
  2722. LOCPLINT->battleInt->console->ingcAlter = "";
  2723. }
  2724. }
  2725. void CInGameConsole::refreshEnteredText()
  2726. {
  2727. if(GH.topInt() == adventureInt)
  2728. {
  2729. GH.statusbar->lock(false);
  2730. GH.statusbar->clear();
  2731. GH.statusbar->setText(enteredText);
  2732. GH.statusbar->lock(true);
  2733. }
  2734. else if(LOCPLINT->battleInt)
  2735. {
  2736. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  2737. }
  2738. }
  2739. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  2740. {
  2741. #ifdef VCMI_SDL1
  2742. addUsedEvents(KEYBOARD);
  2743. #else
  2744. addUsedEvents(KEYBOARD | TEXTINPUT);
  2745. #endif
  2746. }
  2747. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  2748. {
  2749. if((!down) && previousState)
  2750. {
  2751. std::vector<CComponent*> comp(1, createComponent());
  2752. LOCPLINT->showInfoDialog(text, comp);
  2753. }
  2754. }
  2755. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  2756. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  2757. {
  2758. }
  2759. CComponent * LRClickableAreaWTextComp::createComponent() const
  2760. {
  2761. if(baseType >= 0)
  2762. return new CComponent(CComponent::Etype(baseType), type, bonusValue);
  2763. else
  2764. return nullptr;
  2765. }
  2766. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  2767. {
  2768. if(down)
  2769. {
  2770. if(CComponent *comp = createComponent())
  2771. {
  2772. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  2773. return;
  2774. }
  2775. }
  2776. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occurred
  2777. }
  2778. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  2779. {
  2780. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2781. addUsedEvents(LCLICK | RCLICK | HOVER);
  2782. pos.x += x; pos.w = 58;
  2783. pos.y += y; pos.h = 64;
  2784. if (hero)
  2785. new CAnimImage("PortraitsLarge", hero->portrait);
  2786. }
  2787. void CHeroArea::clickLeft(tribool down, bool previousState)
  2788. {
  2789. if((!down) && previousState && hero)
  2790. LOCPLINT->openHeroWindow(hero);
  2791. }
  2792. void CHeroArea::clickRight(tribool down, bool previousState)
  2793. {
  2794. if((!down) && previousState && hero)
  2795. LOCPLINT->openHeroWindow(hero);
  2796. }
  2797. void CHeroArea::hover(bool on)
  2798. {
  2799. if (on && hero)
  2800. GH.statusbar->setText(hero->getObjectName());
  2801. else
  2802. GH.statusbar->clear();
  2803. }
  2804. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  2805. {
  2806. if((!down) && previousState && town)
  2807. {
  2808. LOCPLINT->openTownWindow(town);
  2809. if ( type == 2 )
  2810. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::VILLAGE_HALL);
  2811. else if ( type == 3 && town->fortLevel() )
  2812. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::FORT);
  2813. }
  2814. }
  2815. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  2816. {
  2817. if((!down) && previousState && town)
  2818. LOCPLINT->openTownWindow(town);//TODO: popup?
  2819. }
  2820. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  2821. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  2822. {
  2823. }
  2824. void CExchangeWindow::questlog(int whichHero)
  2825. {
  2826. CCS->curh->dragAndDropCursor(nullptr);
  2827. }
  2828. void CExchangeWindow::prepareBackground()
  2829. {
  2830. //printing heroes' names and levels
  2831. auto genTitle = [](const CGHeroInstance *h)
  2832. {
  2833. return boost::str(boost::format(CGI->generaltexth->allTexts[138])
  2834. % h->name % h->level % h->type->heroClass->name);
  2835. };
  2836. new CLabel(147, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[0]));
  2837. new CLabel(653, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[1]));
  2838. //printing primary skills
  2839. for(int g=0; g<4; ++g)
  2840. new CAnimImage("PSKIL32", g, 0, 385, 19 + 36*g);
  2841. //heroes related thing
  2842. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  2843. {
  2844. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  2845. //printing primary skills' amounts
  2846. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  2847. new CLabel(352 + 93 * b, 35 + 36 * m, FONT_SMALL, CENTER, Colors::WHITE,
  2848. boost::lexical_cast<std::string>(heroWArt.getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(m))));
  2849. //printing secondary skills
  2850. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  2851. {
  2852. int id = heroInst[b]->secSkills[m].first;
  2853. int level = heroInst[b]->secSkills[m].second;
  2854. new CAnimImage("SECSK32", id*3 + level + 2 , 0, 32 + 36 * m + 454 * b, 88);
  2855. }
  2856. //hero's specialty
  2857. new CAnimImage("UN32", heroInst[b]->type->imageIndex, 0, 67 + 490*b, 45);
  2858. //experience
  2859. new CAnimImage("PSKIL32", 4, 0, 103 + 490*b, 45);
  2860. new CLabel(119 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->exp));
  2861. //mana points
  2862. new CAnimImage("PSKIL32", 5, 0, 139 + 490*b, 45);
  2863. new CLabel(155 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->mana));
  2864. }
  2865. //printing portraits
  2866. new CAnimImage("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
  2867. new CAnimImage("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
  2868. }
  2869. CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID):
  2870. CWindowObject(PLAYER_COLORED | BORDERED, "TRADE2")
  2871. {
  2872. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2873. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  2874. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  2875. prepareBackground();
  2876. artifs[0] = new CArtifactsOfHero(Point(-334, 150));
  2877. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  2878. artifs[0]->commonInfo->participants.insert(artifs[0]);
  2879. artifs[0]->setHero(heroInst[0]);
  2880. artifs[1] = new CArtifactsOfHero(Point(96, 150));
  2881. artifs[1]->commonInfo = artifs[0]->commonInfo;
  2882. artifs[1]->commonInfo->participants.insert(artifs[1]);
  2883. artifs[1]->setHero(heroInst[1]);
  2884. artSets.push_back(artifs[0]);
  2885. artSets.push_back(artifs[1]);
  2886. //primary skills
  2887. for(int g=0; g<4; ++g)
  2888. {
  2889. //primary skill's clickable areas
  2890. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  2891. primSkillAreas[g]->pos = genRect(32, 140, pos.x + 329, pos.y + 19 + 36 * g);
  2892. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  2893. primSkillAreas[g]->type = g;
  2894. primSkillAreas[g]->bonusValue = -1;
  2895. primSkillAreas[g]->baseType = 0;
  2896. primSkillAreas[g]->hoverText = CGI->generaltexth->heroscrn[1];
  2897. boost::replace_first(primSkillAreas[g]->hoverText, "%s", CGI->generaltexth->primarySkillNames[g]);
  2898. }
  2899. //heroes related thing
  2900. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  2901. {
  2902. //secondary skill's clickable areas
  2903. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  2904. {
  2905. int skill = heroInst[b]->secSkills[g].first,
  2906. level = heroInst[b]->secSkills[g].second; // <1, 3>
  2907. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  2908. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  2909. secSkillAreas[b][g]->baseType = 1;
  2910. secSkillAreas[b][g]->type = skill;
  2911. secSkillAreas[b][g]->bonusValue = level;
  2912. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  2913. secSkillAreas[b][g]->hoverText = CGI->generaltexth->heroscrn[21];
  2914. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->generaltexth->levels[level - 1]);
  2915. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->generaltexth->skillName[skill]);
  2916. }
  2917. portrait[b] = new CHeroArea(257 + 228*b, 13, heroInst[b]);
  2918. specialty[b] = new LRClickableAreaWText();
  2919. specialty[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  2920. specialty[b]->hoverText = CGI->generaltexth->heroscrn[27];
  2921. specialty[b]->text = heroInst[b]->type->specDescr;
  2922. experience[b] = new LRClickableAreaWText();
  2923. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  2924. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  2925. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  2926. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  2927. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  2928. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  2929. spellPoints[b] = new LRClickableAreaWText();
  2930. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  2931. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  2932. spellPoints[b]->text = CGI->generaltexth->allTexts[205];
  2933. boost::algorithm::replace_first(spellPoints[b]->text, "%s", heroInst[b]->name);
  2934. boost::algorithm::replace_first(spellPoints[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->mana));
  2935. boost::algorithm::replace_first(spellPoints[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->manaLimit()));
  2936. //setting morale
  2937. morale[b] = new MoraleLuckBox(true, genRect(32, 32, 176 + 490*b, 39), true);
  2938. morale[b]->set(heroInst[b]);
  2939. //setting luck
  2940. luck[b] = new MoraleLuckBox(false, genRect(32, 32, 212 + 490*b, 39), true);
  2941. luck[b]->set(heroInst[b]);
  2942. }
  2943. //buttons
  2944. quit = new CAdventureMapButton(CGI->generaltexth->zelp[600], boost::bind(&CExchangeWindow::close, this), 732, 567, "IOKAY.DEF", SDLK_RETURN);
  2945. if(queryID.getNum() > 0)
  2946. quit->callback += [=]{ LOCPLINT->cb->selectionMade(0, queryID); };
  2947. questlogButton[0] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 0), 10, 44, "hsbtns4.def");
  2948. questlogButton[1] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 1), 740, 44, "hsbtns4.def");
  2949. Rect barRect(5, 578, 725, 18);
  2950. ourBar = new CGStatusBar(new CPicture(*background, barRect, 5, 578, false));
  2951. //garrison interface
  2952. garr = new CGarrisonInt(69, 131, 4, Point(418,0), *background, Point(69,131), heroInst[0],heroInst[1], true, true);
  2953. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 10, 132, "TSBTNS.DEF"));
  2954. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 740, 132, "TSBTNS.DEF"));
  2955. }
  2956. CExchangeWindow::~CExchangeWindow() //d-tor
  2957. {
  2958. delete artifs[0]->commonInfo;
  2959. artifs[0]->commonInfo = nullptr;
  2960. artifs[1]->commonInfo = nullptr;
  2961. }
  2962. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const std::function<void()> &onBuy):
  2963. CWindowObject(PLAYER_COLORED, "TPSHIP")
  2964. {
  2965. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2966. bgWater = new CPicture("TPSHIPBK", 100, 69);
  2967. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  2968. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  2969. bgShip = new CAnimImage(boatFilenames[boatType], 0, 7, 120, 96, CShowableAnim::USE_RLE);
  2970. bgShip->center(waterCenter);
  2971. // Create resource icons and costs.
  2972. std::string goldValue = boost::lexical_cast<std::string>(cost[Res::GOLD]);
  2973. std::string woodValue = boost::lexical_cast<std::string>(cost[Res::WOOD]);
  2974. goldCost = new CLabel(118, 294, FONT_SMALL, CENTER, Colors::WHITE, goldValue);
  2975. woodCost = new CLabel(212, 294, FONT_SMALL, CENTER, Colors::WHITE, woodValue);
  2976. goldPic = new CAnimImage("RESOURCE", Res::GOLD, 0, 100, 244);
  2977. woodPic = new CAnimImage("RESOURCE", Res::WOOD, 0, 196, 244);
  2978. quit = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CShipyardWindow::close, this), 224, 312, "ICANCEL", SDLK_RETURN);
  2979. build = new CAdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CShipyardWindow::close, this), 42, 312, "IBUY30", SDLK_RETURN);
  2980. build->callback += onBuy;
  2981. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, 1))
  2982. {
  2983. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  2984. {
  2985. build->block(true);
  2986. break;
  2987. }
  2988. }
  2989. statusBar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  2990. title = new CLabel(164, 27, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  2991. costLabel = new CLabel(164, 220, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  2992. }
  2993. CPuzzleWindow::CPuzzleWindow(const int3 &GrailPos, double discoveredRatio):
  2994. CWindowObject(PLAYER_COLORED | BORDERED, "PUZZLE"),
  2995. grailPos(GrailPos),
  2996. currentAlpha(SDL_ALPHA_OPAQUE)
  2997. {
  2998. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2999. CCS->soundh->playSound(soundBase::OBELISK);
  3000. quitb = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CPuzzleWindow::close, this), 670, 538, "IOK6432.DEF", SDLK_RETURN);
  3001. quitb->assignedKeys.insert(SDLK_ESCAPE);
  3002. quitb->borderColor = Colors::METALLIC_GOLD;
  3003. quitb->borderEnabled = true;
  3004. new CPicture("PUZZLOGO", 607, 3);
  3005. new CLabel(700, 95, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[463]);
  3006. new CResDataBar("ARESBAR.bmp", 3, 575, 32, 2, 85, 85);
  3007. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  3008. auto & puzzleMap = CGI->townh->factions[faction]->puzzleMap;
  3009. for(auto & elem : puzzleMap)
  3010. {
  3011. const SPuzzleInfo & info = elem;
  3012. auto piece = new CPicture(info.filename, info.x, info.y);
  3013. //piece that will slowly disappear
  3014. if(info.whenUncovered <= GameConstants::PUZZLE_MAP_PIECES * discoveredRatio)
  3015. {
  3016. piecesToRemove.push_back(piece);
  3017. piece->needRefresh = true;
  3018. piece->recActions = piece->recActions & ~SHOWALL;
  3019. #ifndef VCMI_SDL1
  3020. SDL_SetSurfaceBlendMode(piece->bg,SDL_BLENDMODE_BLEND);
  3021. #endif // VCMI_SDL1
  3022. }
  3023. }
  3024. }
  3025. void CPuzzleWindow::showAll(SDL_Surface * to)
  3026. {
  3027. int3 moveInt = int3(8, 9, 0);
  3028. Rect mapRect = genRect(544, 591, pos.x + 8, pos.y + 7);
  3029. CGI->mh->terrainRect
  3030. (grailPos - moveInt, adventureInt->anim,
  3031. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  3032. to, &mapRect, 0, 0, true, moveInt);
  3033. CWindowObject::showAll(to);
  3034. }
  3035. void CPuzzleWindow::show(SDL_Surface * to)
  3036. {
  3037. static int animSpeed = 2;
  3038. if (currentAlpha < animSpeed)
  3039. {
  3040. //animation done
  3041. for(auto & piece : piecesToRemove)
  3042. delete piece;
  3043. piecesToRemove.clear();
  3044. }
  3045. else
  3046. {
  3047. //update disappearing puzzles
  3048. for(auto & piece : piecesToRemove)
  3049. piece->setAlpha(currentAlpha);
  3050. currentAlpha -= animSpeed;
  3051. }
  3052. CWindowObject::show(to);
  3053. }
  3054. void CTransformerWindow::CItem::move()
  3055. {
  3056. if (left)
  3057. moveBy(Point(289, 0));
  3058. else
  3059. moveBy(Point(-289, 0));
  3060. left = !left;
  3061. }
  3062. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  3063. {
  3064. if(previousState && (!down))
  3065. {
  3066. move();
  3067. parent->showAll(screen2);
  3068. }
  3069. }
  3070. void CTransformerWindow::CItem::update()
  3071. {
  3072. icon->setFrame(parent->army->getCreature(SlotID(id))->idNumber + 2);
  3073. }
  3074. CTransformerWindow::CItem::CItem(CTransformerWindow * parent, int size, int id):
  3075. id(id), size(size), parent(parent)
  3076. {
  3077. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3078. addUsedEvents(LCLICK);
  3079. left = true;
  3080. pos.w = 58;
  3081. pos.h = 64;
  3082. pos.x += 45 + (id%3)*83 + id/6*83;
  3083. pos.y += 109 + (id/3)*98;
  3084. icon = new CAnimImage("TWCRPORT", parent->army->getCreature(SlotID(id))->idNumber + 2);
  3085. new CLabel(28, 76,FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(size));//stack size
  3086. }
  3087. void CTransformerWindow::makeDeal()
  3088. {
  3089. for (auto & elem : items)
  3090. if (!elem->left)
  3091. LOCPLINT->cb->trade(town, EMarketMode::CREATURE_UNDEAD, elem->id, 0, 0, hero);
  3092. }
  3093. void CTransformerWindow::addAll()
  3094. {
  3095. for (auto & elem : items)
  3096. if (elem->left)
  3097. elem->move();
  3098. showAll(screen2);
  3099. }
  3100. void CTransformerWindow::updateGarrisons()
  3101. {
  3102. for(auto & item : items)
  3103. {
  3104. item->update();
  3105. }
  3106. }
  3107. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):
  3108. CWindowObject(PLAYER_COLORED, "SKTRNBK"),
  3109. hero(_hero),
  3110. town(_town)
  3111. {
  3112. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3113. if (hero)
  3114. army = hero;
  3115. else
  3116. army = town;
  3117. for (int i=0; i<GameConstants::ARMY_SIZE; i++ )
  3118. if ( army->getCreature(SlotID(i)) )
  3119. items.push_back(new CItem(this, army->getStackCount(SlotID(i)), i));
  3120. all = new CAdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  3121. convert= new CAdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  3122. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CTransformerWindow::close, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  3123. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  3124. new CLabel(153, 29,FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  3125. new CLabel(153+295, 29, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  3126. new CTextBox(CGI->generaltexth->allTexts[487], Rect(26, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//move creatures to create skeletons
  3127. new CTextBox(CGI->generaltexth->allTexts[488], Rect(320, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//creatures here will become skeletons
  3128. }
  3129. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  3130. {
  3131. if(previousState && (!down))
  3132. {
  3133. if ( state() != 2 )
  3134. return;
  3135. auto win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(Res::GOLD) >= 2000);
  3136. GH.pushInt(win);
  3137. }
  3138. }
  3139. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  3140. {
  3141. if(down)
  3142. {
  3143. CRClickPopup::createAndPush(CGI->generaltexth->skillInfoTexts[ID][0],
  3144. new CComponent(CComponent::secskill, ID, 1));
  3145. }
  3146. }
  3147. void CUniversityWindow::CItem::hover(bool on)
  3148. {
  3149. if (on)
  3150. GH.statusbar->setText(CGI->generaltexth->skillName[ID]);
  3151. else
  3152. GH.statusbar->clear();
  3153. }
  3154. int CUniversityWindow::CItem::state()
  3155. {
  3156. if (parent->hero->getSecSkillLevel(SecondarySkill(ID)))//hero know this skill
  3157. return 1;
  3158. if (!parent->hero->canLearnSkill())//can't learn more skills
  3159. return 0;
  3160. if (parent->hero->type->heroClass->secSkillProbability[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  3161. return 0;
  3162. return 2;
  3163. }
  3164. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  3165. {
  3166. CPicture * bar;
  3167. switch (state())
  3168. {
  3169. case 0: bar = parent->red;
  3170. break;
  3171. case 1: bar = parent->yellow;
  3172. break;
  3173. case 2: bar = parent->green;
  3174. break;
  3175. default:bar = nullptr;
  3176. break;
  3177. }
  3178. assert(bar);
  3179. blitAtLoc(bar->bg, -28, -22, to);
  3180. blitAtLoc(bar->bg, -28, 48, to);
  3181. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, Colors::WHITE,to);//Name
  3182. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, Colors::WHITE,to);//Level(always basic)
  3183. CAnimImage::showAll(to);
  3184. }
  3185. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  3186. CAnimImage ("SECSKILL", _ID*3+3, 0, X, Y),
  3187. ID(_ID),
  3188. parent(_parent)
  3189. {
  3190. addUsedEvents(LCLICK | RCLICK | HOVER);
  3191. }
  3192. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):
  3193. CWindowObject(PLAYER_COLORED, "UNIVERS1"),
  3194. hero(_hero),
  3195. market(_market)
  3196. {
  3197. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3198. green = new CPicture("UNIVGREN.PCX");
  3199. yellow = new CPicture("UNIVGOLD.PCX");//bars
  3200. red = new CPicture("UNIVRED.PCX");
  3201. green->recActions =
  3202. yellow->recActions =
  3203. red->recActions = DISPOSE;
  3204. CIntObject * titlePic = nullptr;
  3205. if (market->o->ID == Obj::TOWN)
  3206. titlePic = new CAnimImage(CGI->townh->factions[ETownType::CONFLUX]->town->clientInfo.buildingsIcons, BuildingID::MAGIC_UNIVERSITY);
  3207. else
  3208. titlePic = new CPicture("UNIVBLDG");
  3209. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  3210. //Clerk speech
  3211. new CTextBox(CGI->generaltexth->allTexts[603], Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);
  3212. //University
  3213. new CLabel(231, 26, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[602]);
  3214. std::vector<int> list = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  3215. assert(list.size() == 4);
  3216. for (int i=0; i<list.size(); i++)//prepare clickable items
  3217. items.push_back(new CItem(this, list[i], 54+i*104, 234));
  3218. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[632],
  3219. boost::bind(&CUniversityWindow::close, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  3220. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  3221. }
  3222. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):
  3223. CWindowObject(PLAYER_COLORED, "UNIVERS2.PCX"),
  3224. parent(PARENT)
  3225. {
  3226. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3227. std::string text = CGI->generaltexth->allTexts[608];
  3228. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  3229. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  3230. boost::replace_first(text, "%d", "2000");
  3231. new CTextBox(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);//Clerk speech
  3232. new CLabel(230, 37, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth-> skillName[SKILL]);//Skill name
  3233. new CAnimImage("SECSKILL", SKILL*3+3, 0, 211, 51);//skill
  3234. new CLabel(230, 107, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->levels[1]);//Skill level
  3235. new CAnimImage("RESOURCE", Res::GOLD, 0, 210, 210);//gold
  3236. new CLabel(230, 267, FONT_SMALL, CENTER, Colors::WHITE, "2000");//Cost
  3237. std::string hoverText = CGI->generaltexth->allTexts[609];
  3238. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  3239. text = CGI->generaltexth->zelp[633].second;
  3240. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  3241. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  3242. boost::replace_first(text, "%d", "2000");
  3243. confirm= new CAdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  3244. 148,299,"IBY6432.DEF",SDLK_RETURN);
  3245. confirm->block(!available);
  3246. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CUnivConfirmWindow::close, this),
  3247. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  3248. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  3249. }
  3250. void CUnivConfirmWindow::makeDeal(int skill)
  3251. {
  3252. LOCPLINT->cb->trade(parent->market->o, EMarketMode::RESOURCE_SKILL, 6, skill, 1, parent->hero);
  3253. close();
  3254. }
  3255. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  3256. CWindowObject(PLAYER_COLORED, "APHLFTBK"),
  3257. fort(object),
  3258. hero(visitor)
  3259. {
  3260. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3261. slotsCount=7;
  3262. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  3263. new CLabel(325, 32, FONT_BIG, CENTER, Colors::YELLOW, fort->getObjectName());//Hill Fort
  3264. heroPic = new CHeroArea(30, 60, hero);
  3265. currState.resize(slotsCount+1);
  3266. costs.resize(slotsCount);
  3267. totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
  3268. std::vector<std::string> files;
  3269. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  3270. for (int i=0; i<slotsCount; i++)
  3271. {
  3272. currState[i] = getState(SlotID(i));
  3273. upgrade[i] = new CAdventureMapButton(getTextForSlot(SlotID(i)),"",boost::bind(&CHillFortWindow::makeDeal, this, SlotID(i)),
  3274. 107+i*76, 171, "", SDLK_1+i, &files);
  3275. upgrade[i]->block(currState[i] == -1);
  3276. }
  3277. files.clear();
  3278. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  3279. currState[slotsCount] = getState(SlotID(slotsCount));
  3280. upgradeAll = new CAdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, SlotID(slotsCount)),
  3281. 30, 231, "", SDLK_0, &files);
  3282. quit = new CAdventureMapButton("","",boost::bind(&CHillFortWindow::close, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  3283. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  3284. garr = new CGarrisonInt(108, 60, 18, Point(),background->bg,Point(108,60),hero,nullptr);
  3285. updateGarrisons();
  3286. }
  3287. void CHillFortWindow::updateGarrisons()
  3288. {
  3289. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  3290. totalSumm[i]=0;
  3291. for (int i=0; i<slotsCount; i++)
  3292. {
  3293. costs[i].clear();
  3294. int newState = getState(SlotID(i));
  3295. if (newState != -1)
  3296. {
  3297. UpgradeInfo info;
  3298. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  3299. if (info.newID.size())//we have upgrades here - update costs
  3300. {
  3301. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  3302. totalSumm += costs[i];
  3303. }
  3304. }
  3305. currState[i] = newState;
  3306. upgrade[i]->setIndex(newState);
  3307. upgrade[i]->block(currState[i] == -1);
  3308. upgrade[i]->hoverTexts[0] = getTextForSlot(SlotID(i));
  3309. }
  3310. int newState = getState(SlotID(slotsCount));
  3311. currState[slotsCount] = newState;
  3312. upgradeAll->setIndex(newState);
  3313. garr->recreateSlots();
  3314. }
  3315. void CHillFortWindow::makeDeal(SlotID slot)
  3316. {
  3317. assert(slot.getNum()>=0);
  3318. int offset = (slot.getNum() == slotsCount)?2:0;
  3319. switch (currState[slot.getNum()])
  3320. {
  3321. case 0:
  3322. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  3323. std::vector<CComponent*>(), soundBase::sound_todo);
  3324. break;
  3325. case 1:
  3326. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  3327. std::vector<CComponent*>(), soundBase::sound_todo);
  3328. break;
  3329. case 2:
  3330. for (int i=0; i<slotsCount; i++)
  3331. if ( slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  3332. {
  3333. UpgradeInfo info;
  3334. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  3335. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  3336. }
  3337. break;
  3338. }
  3339. }
  3340. void CHillFortWindow::showAll (SDL_Surface *to)
  3341. {
  3342. CWindowObject::showAll(to);
  3343. for ( int i=0; i<slotsCount; i++)
  3344. {
  3345. if ( currState[i] == 0 || currState[i] == 2 )
  3346. {
  3347. if ( costs[i].size() )//we have several elements
  3348. {
  3349. int curY = 128;//reverse iterator is used to display gold as first element
  3350. for(int j = costs[i].size()-1; j >= 0; j--)
  3351. {
  3352. int val = costs[i][j];
  3353. if(!val) continue;
  3354. blitAtLoc(resources->ourImages[j].bitmap, 104+76*i, curY, to);
  3355. printToLoc(boost::lexical_cast<std::string>(val), 168+76*i, curY+16, FONT_SMALL, Colors::WHITE, to);
  3356. curY += 20;
  3357. }
  3358. }
  3359. else//free upgrade - print gold image and "Free" text
  3360. {
  3361. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  3362. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, Colors::WHITE, to);
  3363. }
  3364. }
  3365. }
  3366. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  3367. {
  3368. if (totalSumm[i])//this resource is used - display it
  3369. {
  3370. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  3371. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, Colors::WHITE, to);
  3372. }
  3373. }
  3374. }
  3375. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  3376. {
  3377. if ( !hero->getCreature(slot) )//we don`t have creature here
  3378. return "";
  3379. std::string str = CGI->generaltexth->allTexts[318];
  3380. int amount = hero->getStackCount(slot);
  3381. if ( amount == 1 )
  3382. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  3383. else
  3384. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  3385. return str;
  3386. }
  3387. int CHillFortWindow::getState(SlotID slot)
  3388. {
  3389. TResources myRes = LOCPLINT->cb->getResourceAmount();
  3390. if ( slot.getNum() == slotsCount )//"Upgrade all" slot
  3391. {
  3392. bool allUpgraded = true;//All creatures are upgraded?
  3393. for (int i=0; i<slotsCount; i++)
  3394. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  3395. if (allUpgraded)
  3396. return 1;
  3397. if(!totalSumm.canBeAfforded(myRes))
  3398. return 0;
  3399. return 2;
  3400. }
  3401. if (hero->slotEmpty(slot))//no creature here
  3402. return -1;
  3403. UpgradeInfo info;
  3404. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  3405. if (!info.newID.size())//already upgraded
  3406. return 1;
  3407. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  3408. return 0;
  3409. return 2;//can upgrade
  3410. }
  3411. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  3412. CWindowObject(PLAYER_COLORED | BORDERED, "TpRank"),
  3413. owner(_owner)
  3414. {
  3415. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3416. type |= BLOCK_ADV_HOTKEYS;
  3417. SThievesGuildInfo tgi; //info to be displayed
  3418. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  3419. exitb = new CAdventureMapButton (CGI->generaltexth->allTexts[600], "", boost::bind(&CThievesGuildWindow::close,this), 748, 556, "TPMAGE1", SDLK_RETURN);
  3420. exitb->assignedKeys.insert(SDLK_ESCAPE);
  3421. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  3422. resdatabar = new CMinorResDataBar();
  3423. resdatabar->pos.x += pos.x;
  3424. resdatabar->pos.y += pos.y;
  3425. //data for information table:
  3426. // fields[row][column] = list of id's of players for this box
  3427. static std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
  3428. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  3429. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  3430. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  3431. //printing texts & descriptions to background
  3432. for(int g=0; g<12; ++g)
  3433. {
  3434. int posY[] = {400, 460, 510};
  3435. int y;
  3436. if(g < 9)
  3437. y = 52 + 32*g;
  3438. else
  3439. y = posY[g-9];
  3440. std::string text = CGI->generaltexth->jktexts[24+g];
  3441. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  3442. new CLabel(135, y, FONT_MEDIUM, CENTER, Colors::YELLOW, text);
  3443. }
  3444. for(int g=1; g<tgi.playerColors.size(); ++g)
  3445. new CAnimImage("PRSTRIPS", g-1, 0, 250 + 66*g, 7);
  3446. for(int g=0; g<tgi.playerColors.size(); ++g)
  3447. new CLabel(283 + 66*g, 24, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]);
  3448. //printing flags
  3449. for(int g = 0; g < ARRAY_COUNT(fields); ++g) //by lines
  3450. {
  3451. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  3452. {
  3453. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  3454. //position of box
  3455. int xpos = 259 + 66 * b;
  3456. int ypos = 41 + 32 * g;
  3457. size_t rowLength[2]; //size of each row
  3458. rowLength[0] = std::min<size_t>(players.size(), 4);
  3459. rowLength[1] = players.size() - rowLength[0];
  3460. for (size_t j=0; j< 2; j++)
  3461. {
  3462. // origin of this row | offset for 2nd row| shift right for short rows
  3463. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  3464. int rowStartX = xpos + (j ? 6 + (rowLength[j] < 3 ? 12 : 0) : 24 - 6 * rowLength[j]);
  3465. int rowStartY = ypos + (j ? 4 : 0);
  3466. for (size_t i=0; i< rowLength[j]; i++)
  3467. {
  3468. new CAnimImage("itgflags", players[i + j*4].getNum(), 0, rowStartX + i*12, rowStartY);
  3469. }
  3470. }
  3471. }
  3472. }
  3473. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  3474. //printing best hero
  3475. int counter = 0;
  3476. for(auto & iter : tgi.colorToBestHero)
  3477. {
  3478. if(iter.second.portrait >= 0)
  3479. {
  3480. new CPicture(colorToBox[iter.first.getNum()], 253 + 66 * counter, 334);
  3481. new CAnimImage("PortraitsSmall", iter.second.portrait, 0, 260 + 66 * counter, 360);
  3482. //TODO: r-click info:
  3483. // - r-click on hero
  3484. // - r-click on primary skill label
  3485. if(iter.second.details)
  3486. {
  3487. new CTextBox(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  3488. 0, FONT_TINY, TOPLEFT, Colors::WHITE);
  3489. for (int i=0; i<iter.second.details->primskills.size(); ++i)
  3490. {
  3491. new CLabel(310 + 66 * counter, 407 + 11*i, FONT_TINY, BOTTOMRIGHT, Colors::WHITE,
  3492. boost::lexical_cast<std::string>(iter.second.details->primskills[i]));
  3493. }
  3494. }
  3495. }
  3496. counter++;
  3497. }
  3498. //printing best creature
  3499. counter = 0;
  3500. for(auto & it : tgi.bestCreature)
  3501. {
  3502. if(it.second >= 0)
  3503. new CAnimImage("TWCRPORT", it.second+2, 0, 255 + 66 * counter, 479);
  3504. counter++;
  3505. }
  3506. //printing personality
  3507. counter = 0;
  3508. for(auto & it : tgi.personality)
  3509. {
  3510. std::string text;
  3511. if(it.second == EAiTactic::NONE)
  3512. {
  3513. text = CGI->generaltexth->arraytxt[172];
  3514. }
  3515. else if(it.second != EAiTactic::RANDOM)
  3516. {
  3517. text = CGI->generaltexth->arraytxt[168 + it.second];
  3518. }
  3519. new CLabel(283 + 66*counter, 459, FONT_SMALL, CENTER, Colors::WHITE, text);
  3520. counter++;
  3521. }
  3522. }
  3523. void MoraleLuckBox::set(const IBonusBearer *node)
  3524. {
  3525. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3526. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  3527. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  3528. const int neutralDescr[] = {60, 86}; //eg {Neutral Morale} \n\n Neutral morale means your armies will neither be blessed with extra attacks or freeze in combat.
  3529. const int componentType[] = {CComponent::luck, CComponent::morale};
  3530. const int hoverTextBase[] = {7, 4};
  3531. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  3532. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  3533. int mrlt = -9;
  3534. TModDescr mrl;
  3535. if (node)
  3536. {
  3537. node->getModifiersWDescr(mrl, bonusType[morale]);
  3538. bonusValue = (node->*getValue[morale])();
  3539. }
  3540. else
  3541. bonusValue = 0;
  3542. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  3543. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  3544. baseType = componentType[morale];
  3545. text = CGI->generaltexth->arraytxt[textId[morale]];
  3546. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  3547. if (!mrl.size())
  3548. text += CGI->generaltexth->arraytxt[noneTxtId];
  3549. else
  3550. {
  3551. //it's a creature window
  3552. if ((morale && node->hasBonusOfType(Bonus::UNDEAD)) ||
  3553. node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING))
  3554. {
  3555. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  3556. }
  3557. else
  3558. {
  3559. for(auto & elem : mrl)
  3560. {
  3561. if (elem.first) //no bonuses with value 0
  3562. text += "\n" + elem.second;
  3563. }
  3564. }
  3565. }
  3566. std::string imageName;
  3567. if (small)
  3568. imageName = morale ? "IMRL30": "ILCK30";
  3569. else
  3570. imageName = morale ? "IMRL42" : "ILCK42";
  3571. delete image;
  3572. image = new CAnimImage(imageName, bonusValue + 3);
  3573. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  3574. }
  3575. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small):
  3576. image(nullptr),
  3577. morale(Morale),
  3578. small(Small)
  3579. {
  3580. bonusValue = 0;
  3581. pos = r + pos;
  3582. }
  3583. void CRClickPopup::clickRight(tribool down, bool previousState)
  3584. {
  3585. if(down)
  3586. return;
  3587. close();
  3588. }
  3589. void CRClickPopup::close()
  3590. {
  3591. GH.popIntTotally(this);
  3592. }
  3593. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  3594. {
  3595. PlayerColor player = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1); //if no player, then use blue
  3596. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  3597. temp->center(Point(GH.current->motion)); //center on mouse
  3598. temp->fitToScreen(10);
  3599. auto rcpi = new CRClickPopupInt(temp,true);
  3600. GH.pushInt(rcpi);
  3601. }
  3602. void CRClickPopup::createAndPush(const std::string &txt, CComponent * component)
  3603. {
  3604. CInfoWindow::TCompsInfo intComps;
  3605. intComps.push_back(component);
  3606. createAndPush(txt, intComps);
  3607. }
  3608. Point CInfoBoxPopup::toScreen(Point p)
  3609. {
  3610. vstd::abetween(p.x, adventureInt->terrain.pos.x + 100, adventureInt->terrain.pos.x + adventureInt->terrain.pos.w - 100);
  3611. vstd::abetween(p.y, adventureInt->terrain.pos.y + 100, adventureInt->terrain.pos.y + adventureInt->terrain.pos.h - 100);
  3612. return p;
  3613. }
  3614. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town):
  3615. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  3616. {
  3617. InfoAboutTown iah;
  3618. LOCPLINT->cb->getTownInfo(town, iah);
  3619. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3620. new CTownTooltip(Point(9, 10), iah);
  3621. }
  3622. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero):
  3623. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "HEROQVBK", toScreen(position))
  3624. {
  3625. InfoAboutHero iah;
  3626. LOCPLINT->cb->getHeroInfo(hero, iah);
  3627. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3628. new CHeroTooltip(Point(9, 10), iah);
  3629. }
  3630. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr):
  3631. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  3632. {
  3633. InfoAboutTown iah;
  3634. LOCPLINT->cb->getTownInfo(garr, iah);
  3635. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3636. new CArmyTooltip(Point(9, 10), iah);
  3637. }
  3638. CIntObject * CRClickPopup::createInfoWin(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  3639. {
  3640. if(!specific)
  3641. specific = adventureInt->selection;
  3642. assert(specific);
  3643. switch(specific->ID)
  3644. {
  3645. case Obj::HERO:
  3646. return new CInfoBoxPopup(position, dynamic_cast<const CGHeroInstance *>(specific));
  3647. case Obj::TOWN:
  3648. return new CInfoBoxPopup(position, dynamic_cast<const CGTownInstance *>(specific));
  3649. case Obj::GARRISON:
  3650. case Obj::GARRISON2:
  3651. return new CInfoBoxPopup(position, dynamic_cast<const CGGarrison *>(specific));
  3652. default:
  3653. return nullptr;
  3654. }
  3655. }
  3656. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  3657. {
  3658. CIntObject *iWin = createInfoWin(p, obj); //try get custom infowindow for this obj
  3659. if(iWin)
  3660. GH.pushInt(iWin);
  3661. else
  3662. {
  3663. if (adventureInt->curHero())
  3664. CRClickPopup::createAndPush(obj->getHoverText(adventureInt->curHero()));
  3665. else
  3666. CRClickPopup::createAndPush(obj->getHoverText(LOCPLINT->playerID));
  3667. }
  3668. }
  3669. CRClickPopup::CRClickPopup()
  3670. {
  3671. addUsedEvents(RCLICK);
  3672. }
  3673. CRClickPopup::~CRClickPopup()
  3674. {
  3675. }
  3676. void CRClickPopupInt::show(SDL_Surface * to)
  3677. {
  3678. inner->show(to);
  3679. }
  3680. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  3681. {
  3682. CCS->curh->hide();
  3683. inner = our;
  3684. delInner = deleteInt;
  3685. }
  3686. CRClickPopupInt::~CRClickPopupInt()
  3687. {
  3688. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  3689. // //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...?)
  3690. // if(LOCPLINT && inner == adventureInt->heroWindow)
  3691. // adventureInt->heroWindow->dispose();
  3692. if(delInner)
  3693. delete inner;
  3694. CCS->curh->show();
  3695. }
  3696. void CRClickPopupInt::showAll(SDL_Surface * to)
  3697. {
  3698. inner->showAll(to);
  3699. }