guiFormSpecMenu.cpp 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <cstdlib>
  17. #include <cmath>
  18. #include <algorithm>
  19. #include <iterator>
  20. #include <limits>
  21. #include <sstream>
  22. #include "guiFormSpecMenu.h"
  23. #include "constants.h"
  24. #include "gamedef.h"
  25. #include "client/keycode.h"
  26. #include "util/strfnd.h"
  27. #include <IGUIButton.h>
  28. #include <IGUICheckBox.h>
  29. #include <IGUIComboBox.h>
  30. #include <IGUIEditBox.h>
  31. #include <IGUIFont.h>
  32. #include <IGUITabControl.h>
  33. #include <IGUIImage.h>
  34. #include <IAnimatedMeshSceneNode.h>
  35. #include "client/renderingengine.h"
  36. #include "log.h"
  37. #include "client/tile.h" // ITextureSource
  38. #include "client/hud.h" // drawItemStack
  39. #include "filesys.h"
  40. #include "gettime.h"
  41. #include "gettext.h"
  42. #include "scripting_server.h"
  43. #include "mainmenumanager.h"
  44. #include "porting.h"
  45. #include "settings.h"
  46. #include "client/client.h"
  47. #include "client/fontengine.h"
  48. #include "client/sound.h"
  49. #include "util/hex.h"
  50. #include "util/numeric.h"
  51. #include "util/string.h" // for parseColorString()
  52. #include "irrlicht_changes/static_text.h"
  53. #include "client/guiscalingfilter.h"
  54. #include "guiAnimatedImage.h"
  55. #include "guiBackgroundImage.h"
  56. #include "guiBox.h"
  57. #include "guiButton.h"
  58. #include "guiButtonImage.h"
  59. #include "guiButtonItemImage.h"
  60. #include "guiEditBoxWithScrollbar.h"
  61. #include "guiInventoryList.h"
  62. #include "guiItemImage.h"
  63. #include "guiScrollContainer.h"
  64. #include "guiHyperText.h"
  65. #include "guiScene.h"
  66. #define MY_CHECKPOS(a,b) \
  67. if (v_pos.size() != 2) { \
  68. errorstream<< "Invalid pos for element " << a << " specified: \"" \
  69. << parts[b] << "\"" << std::endl; \
  70. return; \
  71. }
  72. #define MY_CHECKGEOM(a,b) \
  73. if (v_geom.size() != 2) { \
  74. errorstream<< "Invalid geometry for element " << a << \
  75. " specified: \"" << parts[b] << "\"" << std::endl; \
  76. return; \
  77. }
  78. #define MY_CHECKCLIENT(a) \
  79. if (!m_client) { \
  80. errorstream << "Attempted to use element " << a << " with m_client == nullptr." << std::endl; \
  81. return; \
  82. }
  83. /*
  84. GUIFormSpecMenu
  85. */
  86. static unsigned int font_line_height(gui::IGUIFont *font)
  87. {
  88. return font->getDimension(L"Ay").Height + font->getKerningHeight();
  89. }
  90. inline u32 clamp_u8(s32 value)
  91. {
  92. return (u32) MYMIN(MYMAX(value, 0), 255);
  93. }
  94. GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
  95. gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
  96. Client *client, gui::IGUIEnvironment *guienv, ISimpleTextureSource *tsrc,
  97. ISoundManager *sound_manager, IFormSource *fsrc, TextDest *tdst,
  98. const std::string &formspecPrepend, bool remap_dbl_click):
  99. GUIModalMenu(guienv, parent, id, menumgr, remap_dbl_click),
  100. m_invmgr(client),
  101. m_tsrc(tsrc),
  102. m_sound_manager(sound_manager),
  103. m_client(client),
  104. m_formspec_prepend(formspecPrepend),
  105. m_form_src(fsrc),
  106. m_text_dst(tdst),
  107. m_joystick(joystick)
  108. {
  109. current_keys_pending.key_down = false;
  110. current_keys_pending.key_up = false;
  111. current_keys_pending.key_enter = false;
  112. current_keys_pending.key_escape = false;
  113. m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
  114. m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
  115. }
  116. GUIFormSpecMenu::~GUIFormSpecMenu()
  117. {
  118. removeAll();
  119. delete m_selected_item;
  120. delete m_form_src;
  121. delete m_text_dst;
  122. }
  123. void GUIFormSpecMenu::create(GUIFormSpecMenu *&cur_formspec, Client *client,
  124. gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
  125. TextDest *txt_dest, const std::string &formspecPrepend, ISoundManager *sound_manager)
  126. {
  127. if (cur_formspec && cur_formspec->getReferenceCount() == 1) {
  128. /*
  129. Why reference count == 1? Reason:
  130. 1 on creation (see "drop()" remark below)
  131. +1 for being a guiroot child
  132. +1 when focused (CGUIEnvironment::setFocus)
  133. Hence re-create the formspec when it's existing without any parent.
  134. */
  135. cur_formspec->drop();
  136. cur_formspec = nullptr;
  137. }
  138. if (cur_formspec == nullptr) {
  139. cur_formspec = new GUIFormSpecMenu(joystick, guiroot, -1, &g_menumgr,
  140. client, guienv, client->getTextureSource(), sound_manager, fs_src,
  141. txt_dest, formspecPrepend);
  142. /*
  143. Caution: do not call (*cur_formspec)->drop() here --
  144. the reference might outlive the menu, so we will
  145. periodically check if *cur_formspec is the only
  146. remaining reference (i.e. the menu was removed)
  147. and delete it in that case.
  148. */
  149. } else {
  150. cur_formspec->setFormspecPrepend(formspecPrepend);
  151. cur_formspec->setFormSource(fs_src);
  152. cur_formspec->setTextDest(txt_dest);
  153. }
  154. cur_formspec->doPause = false;
  155. }
  156. void GUIFormSpecMenu::removeTooltip()
  157. {
  158. if (m_tooltip_element) {
  159. m_tooltip_element->remove();
  160. m_tooltip_element->drop();
  161. m_tooltip_element = nullptr;
  162. }
  163. }
  164. void GUIFormSpecMenu::setInitialFocus()
  165. {
  166. // Set initial focus according to following order of precedence:
  167. // 1. first empty editbox
  168. // 2. first editbox
  169. // 3. first table
  170. // 4. last button
  171. // 5. first focusable (not statictext, not tabheader)
  172. // 6. first child element
  173. const auto& children = getChildren();
  174. // 1. first empty editbox
  175. for (gui::IGUIElement *it : children) {
  176. if (it->getType() == gui::EGUIET_EDIT_BOX
  177. && it->getText()[0] == 0) {
  178. Environment->setFocus(it);
  179. return;
  180. }
  181. }
  182. // 2. first editbox
  183. for (gui::IGUIElement *it : children) {
  184. if (it->getType() == gui::EGUIET_EDIT_BOX) {
  185. Environment->setFocus(it);
  186. return;
  187. }
  188. }
  189. // 3. first table
  190. for (gui::IGUIElement *it : children) {
  191. if (it->getTypeName() == std::string("GUITable")) {
  192. Environment->setFocus(it);
  193. return;
  194. }
  195. }
  196. // 4. last button
  197. for (auto it = children.rbegin(); it != children.rend(); ++it) {
  198. if ((*it)->getType() == gui::EGUIET_BUTTON) {
  199. Environment->setFocus(*it);
  200. return;
  201. }
  202. }
  203. // 5. first focusable (not statictext, not tabheader)
  204. for (gui::IGUIElement *it : children) {
  205. if (it->getType() != gui::EGUIET_STATIC_TEXT &&
  206. it->getType() != gui::EGUIET_TAB_CONTROL) {
  207. Environment->setFocus(it);
  208. return;
  209. }
  210. }
  211. // 6. first child element
  212. if (children.empty())
  213. Environment->setFocus(this);
  214. else
  215. Environment->setFocus(children.front());
  216. }
  217. GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
  218. {
  219. for (auto &table : m_tables) {
  220. if (tablename == table.first.fname)
  221. return table.second;
  222. }
  223. return 0;
  224. }
  225. std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
  226. {
  227. for (auto &dropdown : m_dropdowns) {
  228. if (name == dropdown.first.fname)
  229. return &dropdown.second;
  230. }
  231. return NULL;
  232. }
  233. // This will only return a meaningful value if called after drawMenu().
  234. core::rect<s32> GUIFormSpecMenu::getAbsoluteRect()
  235. {
  236. core::rect<s32> rect = AbsoluteRect;
  237. rect.UpperLeftCorner.Y += m_tabheader_upper_edge;
  238. return rect;
  239. }
  240. v2s32 GUIFormSpecMenu::getElementBasePos(const std::vector<std::string> *v_pos)
  241. {
  242. v2f32 pos_f = v2f32(padding.X, padding.Y) + pos_offset * spacing;
  243. if (v_pos) {
  244. pos_f.X += stof((*v_pos)[0]) * spacing.X;
  245. pos_f.Y += stof((*v_pos)[1]) * spacing.Y;
  246. }
  247. return v2s32(pos_f.X, pos_f.Y);
  248. }
  249. v2s32 GUIFormSpecMenu::getRealCoordinateBasePos(const std::vector<std::string> &v_pos)
  250. {
  251. return v2s32((stof(v_pos[0]) + pos_offset.X) * imgsize.X,
  252. (stof(v_pos[1]) + pos_offset.Y) * imgsize.Y);
  253. }
  254. v2s32 GUIFormSpecMenu::getRealCoordinateGeometry(const std::vector<std::string> &v_geom)
  255. {
  256. return v2s32(stof(v_geom[0]) * imgsize.X, stof(v_geom[1]) * imgsize.Y);
  257. }
  258. bool GUIFormSpecMenu::precheckElement(const std::string &name, const std::string &element,
  259. size_t args_min, size_t args_max, std::vector<std::string> &parts)
  260. {
  261. parts = split(element, ';');
  262. if (parts.size() >= args_min && (parts.size() <= args_max || m_formspec_version > FORMSPEC_API_VERSION))
  263. return true;
  264. errorstream << "Invalid " << name << " element(" << parts.size() << "): '" << element << "'" << std::endl;
  265. return false;
  266. }
  267. void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
  268. {
  269. // Note: do not use precheckElement due to "," separator.
  270. std::vector<std::string> parts = split(element,',');
  271. if (((parts.size() == 2) || parts.size() == 3) ||
  272. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  273. {
  274. if (parts[1].find(';') != std::string::npos)
  275. parts[1] = parts[1].substr(0,parts[1].find(';'));
  276. data->invsize.X = MYMAX(0, stof(parts[0]));
  277. data->invsize.Y = MYMAX(0, stof(parts[1]));
  278. lockSize(false);
  279. if (!g_settings->getBool("enable_touch") && parts.size() == 3) {
  280. if (parts[2] == "true") {
  281. lockSize(true,v2u32(800,600));
  282. }
  283. }
  284. data->explicit_size = true;
  285. return;
  286. }
  287. errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
  288. }
  289. void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
  290. {
  291. std::vector<std::string> parts = split(element, ',');
  292. if (parts.size() >= 2) {
  293. if (parts[1].find(';') != std::string::npos)
  294. parts[1] = parts[1].substr(0, parts[1].find(';'));
  295. container_stack.push(pos_offset);
  296. pos_offset.X += stof(parts[0]);
  297. pos_offset.Y += stof(parts[1]);
  298. return;
  299. }
  300. errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
  301. }
  302. void GUIFormSpecMenu::parseContainerEnd(parserData* data)
  303. {
  304. if (container_stack.empty()) {
  305. errorstream<< "Invalid container end element, no matching container start element" << std::endl;
  306. } else {
  307. pos_offset = container_stack.top();
  308. container_stack.pop();
  309. }
  310. }
  311. void GUIFormSpecMenu::parseScrollContainer(parserData *data, const std::string &element)
  312. {
  313. std::vector<std::string> parts;
  314. if (!precheckElement("scroll_container start", element, 4, 5, parts))
  315. return;
  316. std::vector<std::string> v_pos = split(parts[0], ',');
  317. std::vector<std::string> v_geom = split(parts[1], ',');
  318. std::string scrollbar_name = parts[2];
  319. std::string orientation = parts[3];
  320. f32 scroll_factor = 0.1f;
  321. if (parts.size() >= 5 && !parts[4].empty())
  322. scroll_factor = stof(parts[4]);
  323. MY_CHECKPOS("scroll_container", 0);
  324. MY_CHECKGEOM("scroll_container", 1);
  325. v2s32 pos = getRealCoordinateBasePos(v_pos);
  326. v2s32 geom = getRealCoordinateGeometry(v_geom);
  327. if (orientation == "vertical")
  328. scroll_factor *= -imgsize.Y;
  329. else if (orientation == "horizontal")
  330. scroll_factor *= -imgsize.X;
  331. else
  332. warningstream << "GUIFormSpecMenu::parseScrollContainer(): "
  333. << "Invalid scroll_container orientation: " << orientation
  334. << std::endl;
  335. // old parent (at first: this)
  336. // ^ is parent of clipper
  337. // ^ is parent of mover
  338. // ^ is parent of other elements
  339. // make clipper
  340. core::rect<s32> rect_clipper = core::rect<s32>(pos, pos + geom);
  341. gui::IGUIElement *clipper = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
  342. data->current_parent, 0, rect_clipper);
  343. // make mover
  344. FieldSpec spec_mover(
  345. "",
  346. L"",
  347. L"",
  348. 258 + m_fields.size()
  349. );
  350. core::rect<s32> rect_mover = core::rect<s32>(0, 0, geom.X, geom.Y);
  351. GUIScrollContainer *mover = new GUIScrollContainer(Environment,
  352. clipper, spec_mover.fid, rect_mover, orientation, scroll_factor);
  353. data->current_parent = mover;
  354. m_scroll_containers.emplace_back(scrollbar_name, mover);
  355. m_fields.push_back(spec_mover);
  356. clipper->drop();
  357. // remove interferring offset of normal containers
  358. container_stack.push(pos_offset);
  359. pos_offset.X = 0.0f;
  360. pos_offset.Y = 0.0f;
  361. }
  362. void GUIFormSpecMenu::parseScrollContainerEnd(parserData *data)
  363. {
  364. if (data->current_parent == this || data->current_parent->getParent() == this ||
  365. container_stack.empty()) {
  366. errorstream << "Invalid scroll_container end element, "
  367. << "no matching scroll_container start element" << std::endl;
  368. return;
  369. }
  370. if (pos_offset.getLengthSQ() != 0.0f) {
  371. // pos_offset is only set by containers and scroll_containers.
  372. // scroll_containers always set it to 0,0 which means that if it is
  373. // not 0,0, it is a normal container that was opened last, not a
  374. // scroll_container
  375. errorstream << "Invalid scroll_container end element, "
  376. << "an inner container was left open" << std::endl;
  377. return;
  378. }
  379. data->current_parent = data->current_parent->getParent()->getParent();
  380. pos_offset = container_stack.top();
  381. container_stack.pop();
  382. }
  383. void GUIFormSpecMenu::parseList(parserData *data, const std::string &element)
  384. {
  385. MY_CHECKCLIENT("list");
  386. std::vector<std::string> parts;
  387. if (!precheckElement("list", element, 4, 5, parts))
  388. return;
  389. std::string location = parts[0];
  390. std::string listname = parts[1];
  391. std::vector<std::string> v_pos = split(parts[2],',');
  392. std::vector<std::string> v_geom = split(parts[3],',');
  393. std::string startindex;
  394. if (parts.size() == 5)
  395. startindex = parts[4];
  396. MY_CHECKPOS("list",2);
  397. MY_CHECKGEOM("list",3);
  398. InventoryLocation loc;
  399. if (location == "context" || location == "current_name")
  400. loc = m_current_inventory_location;
  401. else
  402. loc.deSerialize(location);
  403. v2s32 geom;
  404. geom.X = stoi(v_geom[0]);
  405. geom.Y = stoi(v_geom[1]);
  406. s32 start_i = 0;
  407. if (!startindex.empty())
  408. start_i = stoi(startindex);
  409. if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
  410. errorstream << "Invalid list element: '" << element << "'" << std::endl;
  411. return;
  412. }
  413. if (!data->explicit_size)
  414. warningstream << "invalid use of list without a size[] element" << std::endl;
  415. FieldSpec spec(
  416. "",
  417. L"",
  418. L"",
  419. 258 + m_fields.size(),
  420. 3
  421. );
  422. auto style = getDefaultStyleForElement("list", spec.fname);
  423. v2f32 slot_scale = style.getVector2f(StyleSpec::SIZE, v2f32(0, 0));
  424. v2f32 slot_size(
  425. slot_scale.X <= 0 ? imgsize.X : std::max<f32>(slot_scale.X * imgsize.X, 1),
  426. slot_scale.Y <= 0 ? imgsize.Y : std::max<f32>(slot_scale.Y * imgsize.Y, 1)
  427. );
  428. v2f32 slot_spacing = style.getVector2f(StyleSpec::SPACING, v2f32(-1, -1));
  429. v2f32 default_spacing = data->real_coordinates ?
  430. v2f32(imgsize.X * 0.25f, imgsize.Y * 0.25f) :
  431. v2f32(spacing.X - imgsize.X, spacing.Y - imgsize.Y);
  432. slot_spacing.X = slot_spacing.X < 0 ? default_spacing.X :
  433. imgsize.X * slot_spacing.X;
  434. slot_spacing.Y = slot_spacing.Y < 0 ? default_spacing.Y :
  435. imgsize.Y * slot_spacing.Y;
  436. slot_spacing += slot_size;
  437. v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos) :
  438. getElementBasePos(&v_pos);
  439. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
  440. pos.X + (geom.X - 1) * slot_spacing.X + slot_size.X,
  441. pos.Y + (geom.Y - 1) * slot_spacing.Y + slot_size.Y);
  442. GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent,
  443. spec.fid, rect, m_invmgr, loc, listname, geom, start_i,
  444. v2s32(slot_size.X, slot_size.Y), slot_spacing, this,
  445. data->inventorylist_options, m_font);
  446. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  447. m_inventorylists.push_back(e);
  448. m_fields.push_back(spec);
  449. }
  450. void GUIFormSpecMenu::parseListRing(parserData *data, const std::string &element)
  451. {
  452. MY_CHECKCLIENT("listring");
  453. std::vector<std::string> parts = split(element, ';');
  454. if (parts.size() == 2) {
  455. std::string location = parts[0];
  456. std::string listname = parts[1];
  457. InventoryLocation loc;
  458. if (location == "context" || location == "current_name")
  459. loc = m_current_inventory_location;
  460. else
  461. loc.deSerialize(location);
  462. m_inventory_rings.emplace_back(loc, listname);
  463. return;
  464. }
  465. if (element.empty() && m_inventorylists.size() > 1) {
  466. size_t siz = m_inventorylists.size();
  467. // insert the last two inv list elements into the list ring
  468. const GUIInventoryList *spa = m_inventorylists[siz - 2];
  469. const GUIInventoryList *spb = m_inventorylists[siz - 1];
  470. m_inventory_rings.emplace_back(spa->getInventoryloc(), spa->getListname());
  471. m_inventory_rings.emplace_back(spb->getInventoryloc(), spb->getListname());
  472. return;
  473. }
  474. errorstream<< "Invalid list ring element(" << parts.size() << ", "
  475. << m_inventorylists.size() << "): '" << element << "'" << std::endl;
  476. }
  477. void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
  478. {
  479. std::vector<std::string> parts;
  480. if (!precheckElement("checkbox", element, 3, 4, parts))
  481. return;
  482. std::vector<std::string> v_pos = split(parts[0],',');
  483. std::string name = parts[1];
  484. std::string label = parts[2];
  485. std::string selected;
  486. if (parts.size() >= 4)
  487. selected = parts[3];
  488. MY_CHECKPOS("checkbox",0);
  489. bool fselected = false;
  490. if (selected == "true")
  491. fselected = true;
  492. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  493. const core::dimension2d<u32> label_size = m_font->getDimension(wlabel.c_str());
  494. s32 cb_size = Environment->getSkin()->getSize(gui::EGDS_CHECK_BOX_WIDTH);
  495. s32 y_center = (std::max(label_size.Height, (u32)cb_size) + 1) / 2;
  496. v2s32 pos;
  497. core::rect<s32> rect;
  498. if (data->real_coordinates) {
  499. pos = getRealCoordinateBasePos(v_pos);
  500. rect = core::rect<s32>(
  501. pos.X,
  502. pos.Y - y_center,
  503. pos.X + label_size.Width + cb_size + 7,
  504. pos.Y + y_center
  505. );
  506. } else {
  507. pos = getElementBasePos(&v_pos);
  508. rect = core::rect<s32>(
  509. pos.X,
  510. pos.Y + imgsize.Y / 2 - y_center,
  511. pos.X + label_size.Width + cb_size + 7,
  512. pos.Y + imgsize.Y / 2 + y_center
  513. );
  514. }
  515. FieldSpec spec(
  516. name,
  517. wlabel, //Needed for displaying text on MSVC
  518. wlabel,
  519. 258+m_fields.size()
  520. );
  521. spec.ftype = f_CheckBox;
  522. gui::IGUICheckBox *e = Environment->addCheckBox(fselected, rect,
  523. data->current_parent, spec.fid, spec.flabel.c_str());
  524. auto style = getDefaultStyleForElement("checkbox", name);
  525. spec.sound = style.get(StyleSpec::Property::SOUND, "");
  526. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  527. if (spec.fname == m_focused_element) {
  528. Environment->setFocus(e);
  529. }
  530. e->grab();
  531. m_checkboxes.emplace_back(spec, e);
  532. m_fields.push_back(spec);
  533. }
  534. void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
  535. {
  536. std::vector<std::string> parts;
  537. if (!precheckElement("scrollbar", element, 5, 5, parts))
  538. return;
  539. std::vector<std::string> v_pos = split(parts[0],',');
  540. std::vector<std::string> v_geom = split(parts[1],',');
  541. std::string name = parts[3];
  542. std::string value = parts[4];
  543. MY_CHECKPOS("scrollbar",0);
  544. MY_CHECKGEOM("scrollbar",1);
  545. v2s32 pos;
  546. v2s32 dim;
  547. if (data->real_coordinates) {
  548. pos = getRealCoordinateBasePos(v_pos);
  549. dim = getRealCoordinateGeometry(v_geom);
  550. } else {
  551. pos = getElementBasePos(&v_pos);
  552. dim.X = stof(v_geom[0]) * spacing.X;
  553. dim.Y = stof(v_geom[1]) * spacing.Y;
  554. }
  555. core::rect<s32> rect =
  556. core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
  557. FieldSpec spec(
  558. name,
  559. L"",
  560. L"",
  561. 258+m_fields.size()
  562. );
  563. bool is_horizontal = true;
  564. if (parts[2] == "vertical")
  565. is_horizontal = false;
  566. spec.ftype = f_ScrollBar;
  567. spec.send = true;
  568. GUIScrollBar *e = new GUIScrollBar(Environment, data->current_parent,
  569. spec.fid, rect, is_horizontal, true, m_tsrc);
  570. auto style = getDefaultStyleForElement("scrollbar", name);
  571. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  572. e->setArrowsVisible(data->scrollbar_options.arrow_visiblity);
  573. s32 max = data->scrollbar_options.max;
  574. s32 min = data->scrollbar_options.min;
  575. e->setMax(max);
  576. e->setMin(min);
  577. e->setPos(stoi(value));
  578. e->setSmallStep(data->scrollbar_options.small_step);
  579. e->setLargeStep(data->scrollbar_options.large_step);
  580. s32 scrollbar_size = is_horizontal ? dim.X : dim.Y;
  581. e->setPageSize(scrollbar_size * (max - min + 1) / data->scrollbar_options.thumb_size);
  582. if (spec.fname == m_focused_element) {
  583. Environment->setFocus(e);
  584. }
  585. m_scrollbars.emplace_back(spec,e);
  586. m_fields.push_back(spec);
  587. }
  588. void GUIFormSpecMenu::parseScrollBarOptions(parserData* data, const std::string &element)
  589. {
  590. std::vector<std::string> parts = split(element, ';');
  591. if (parts.size() == 0) {
  592. warningstream << "Invalid scrollbaroptions element(" << parts.size() << "): '" <<
  593. element << "'" << std::endl;
  594. return;
  595. }
  596. for (const std::string &i : parts) {
  597. std::vector<std::string> options = split(i, '=');
  598. if (options.size() != 2) {
  599. warningstream << "Invalid scrollbaroptions option syntax: '" <<
  600. element << "'" << std::endl;
  601. continue; // Go to next option
  602. }
  603. if (options[0] == "max") {
  604. data->scrollbar_options.max = stoi(options[1]);
  605. continue;
  606. } else if (options[0] == "min") {
  607. data->scrollbar_options.min = stoi(options[1]);
  608. continue;
  609. } else if (options[0] == "smallstep") {
  610. int value = stoi(options[1]);
  611. data->scrollbar_options.small_step = value < 0 ? 10 : value;
  612. continue;
  613. } else if (options[0] == "largestep") {
  614. int value = stoi(options[1]);
  615. data->scrollbar_options.large_step = value < 0 ? 100 : value;
  616. continue;
  617. } else if (options[0] == "thumbsize") {
  618. int value = stoi(options[1]);
  619. data->scrollbar_options.thumb_size = value <= 0 ? 1 : value;
  620. continue;
  621. } else if (options[0] == "arrows") {
  622. auto value = trim(options[1]);
  623. if (value == "hide")
  624. data->scrollbar_options.arrow_visiblity = GUIScrollBar::HIDE;
  625. else if (value == "show")
  626. data->scrollbar_options.arrow_visiblity = GUIScrollBar::SHOW;
  627. else // Auto hide/show
  628. data->scrollbar_options.arrow_visiblity = GUIScrollBar::DEFAULT;
  629. continue;
  630. }
  631. warningstream << "Invalid scrollbaroptions option(" << options[0] <<
  632. "): '" << element << "'" << std::endl;
  633. }
  634. }
  635. void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
  636. {
  637. std::vector<std::string> parts;
  638. if (!precheckElement("image", element, 2, 4, parts))
  639. return;
  640. size_t offset = parts.size() >= 3;
  641. std::vector<std::string> v_pos = split(parts[0],',');
  642. MY_CHECKPOS("image", 0);
  643. std::vector<std::string> v_geom;
  644. if (parts.size() >= 3) {
  645. v_geom = split(parts[1],',');
  646. MY_CHECKGEOM("image", 1);
  647. }
  648. std::string name = unescape_string(parts[1 + offset]);
  649. video::ITexture *texture = m_tsrc->getTexture(name);
  650. v2s32 pos;
  651. v2s32 geom;
  652. if (parts.size() < 3) {
  653. if (texture != nullptr) {
  654. core::dimension2du dim = texture->getOriginalSize();
  655. geom.X = dim.Width;
  656. geom.Y = dim.Height;
  657. } else {
  658. geom = v2s32(0);
  659. }
  660. }
  661. if (data->real_coordinates) {
  662. pos = getRealCoordinateBasePos(v_pos);
  663. if (parts.size() >= 3)
  664. geom = getRealCoordinateGeometry(v_geom);
  665. } else {
  666. pos = getElementBasePos(&v_pos);
  667. if (parts.size() >= 3) {
  668. geom.X = stof(v_geom[0]) * (float)imgsize.X;
  669. geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
  670. }
  671. }
  672. if (!data->explicit_size)
  673. warningstream << "Invalid use of image without a size[] element" << std::endl;
  674. FieldSpec spec(
  675. name,
  676. L"",
  677. L"",
  678. 258 + m_fields.size(),
  679. 1
  680. );
  681. core::rect<s32> rect = core::rect<s32>(pos, pos + geom);
  682. core::rect<s32> middle;
  683. if (parts.size() >= 4)
  684. parseMiddleRect(parts[3], &middle);
  685. // Temporary fix for issue #12581 in 5.6.0.
  686. // Use legacy image when not rendering 9-slice image because GUIAnimatedImage
  687. // uses NNAA filter which causes visual artifacts when image uses alpha blending.
  688. gui::IGUIElement *e;
  689. if (middle.getArea() > 0) {
  690. GUIAnimatedImage *image = new GUIAnimatedImage(Environment, data->current_parent,
  691. spec.fid, rect);
  692. image->setTexture(texture);
  693. image->setMiddleRect(middle);
  694. e = image;
  695. }
  696. else {
  697. gui::IGUIImage *image = Environment->addImage(rect, data->current_parent, spec.fid, nullptr, true);
  698. image->setImage(texture);
  699. image->setScaleImage(true);
  700. image->grab(); // compensate for drop in addImage
  701. e = image;
  702. }
  703. auto style = getDefaultStyleForElement("image", spec.fname);
  704. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
  705. // Animated images should let events through
  706. m_clickthrough_elements.push_back(e);
  707. m_fields.push_back(spec);
  708. }
  709. void GUIFormSpecMenu::parseAnimatedImage(parserData *data, const std::string &element)
  710. {
  711. std::vector<std::string> parts;
  712. if (!precheckElement("animated_image", element, 6, 8, parts))
  713. return;
  714. std::vector<std::string> v_pos = split(parts[0], ',');
  715. std::vector<std::string> v_geom = split(parts[1], ',');
  716. std::string name = parts[2];
  717. std::string texture_name = unescape_string(parts[3]);
  718. s32 frame_count = stoi(parts[4]);
  719. s32 frame_duration = stoi(parts[5]);
  720. MY_CHECKPOS("animated_image", 0);
  721. MY_CHECKGEOM("animated_image", 1);
  722. v2s32 pos;
  723. v2s32 geom;
  724. if (data->real_coordinates) {
  725. pos = getRealCoordinateBasePos(v_pos);
  726. geom = getRealCoordinateGeometry(v_geom);
  727. } else {
  728. pos = getElementBasePos(&v_pos);
  729. geom.X = stof(v_geom[0]) * (float)imgsize.X;
  730. geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
  731. }
  732. if (!data->explicit_size)
  733. warningstream << "Invalid use of animated_image without a size[] element"
  734. << std::endl;
  735. FieldSpec spec(
  736. name,
  737. L"",
  738. L"",
  739. 258 + m_fields.size()
  740. );
  741. spec.ftype = f_AnimatedImage;
  742. spec.send = true;
  743. core::rect<s32> rect = core::rect<s32>(pos, pos + geom);
  744. core::rect<s32> middle;
  745. if (parts.size() >= 8)
  746. parseMiddleRect(parts[7], &middle);
  747. GUIAnimatedImage *e = new GUIAnimatedImage(Environment, data->current_parent,
  748. spec.fid, rect);
  749. e->setTexture(m_tsrc->getTexture(texture_name));
  750. e->setMiddleRect(middle);
  751. e->setFrameDuration(frame_duration);
  752. e->setFrameCount(frame_count);
  753. if (parts.size() >= 7)
  754. e->setFrameIndex(stoi(parts[6]) - 1);
  755. auto style = getDefaultStyleForElement("animated_image", spec.fname, "image");
  756. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  757. // Animated images should let events through
  758. m_clickthrough_elements.push_back(e);
  759. m_fields.push_back(spec);
  760. }
  761. void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
  762. {
  763. std::vector<std::string> parts;
  764. if (!precheckElement("item_image", element, 3, 3, parts))
  765. return;
  766. std::vector<std::string> v_pos = split(parts[0],',');
  767. std::vector<std::string> v_geom = split(parts[1],',');
  768. std::string name = parts[2];
  769. MY_CHECKPOS("item_image",0);
  770. MY_CHECKGEOM("item_image",1);
  771. v2s32 pos;
  772. v2s32 geom;
  773. if (data->real_coordinates) {
  774. pos = getRealCoordinateBasePos(v_pos);
  775. geom = getRealCoordinateGeometry(v_geom);
  776. } else {
  777. pos = getElementBasePos(&v_pos);
  778. geom.X = stof(v_geom[0]) * (float)imgsize.X;
  779. geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
  780. }
  781. if(!data->explicit_size)
  782. warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
  783. FieldSpec spec(
  784. "",
  785. L"",
  786. L"",
  787. 258 + m_fields.size(),
  788. 2
  789. );
  790. spec.ftype = f_ItemImage;
  791. GUIItemImage *e = new GUIItemImage(Environment, data->current_parent, spec.fid,
  792. core::rect<s32>(pos, pos + geom), name, m_font, m_client);
  793. auto style = getDefaultStyleForElement("item_image", spec.fname);
  794. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  795. // item images should let events through
  796. m_clickthrough_elements.push_back(e);
  797. m_fields.push_back(spec);
  798. }
  799. void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
  800. const std::string &type)
  801. {
  802. std::vector<std::string> parts;
  803. if (!precheckElement("button", element, 4, 4, parts))
  804. return;
  805. std::vector<std::string> v_pos = split(parts[0],',');
  806. std::vector<std::string> v_geom = split(parts[1],',');
  807. std::string name = parts[2];
  808. std::string label = parts[3];
  809. MY_CHECKPOS("button",0);
  810. MY_CHECKGEOM("button",1);
  811. v2s32 pos;
  812. v2s32 geom;
  813. core::rect<s32> rect;
  814. if (data->real_coordinates) {
  815. pos = getRealCoordinateBasePos(v_pos);
  816. geom = getRealCoordinateGeometry(v_geom);
  817. rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
  818. pos.Y+geom.Y);
  819. } else {
  820. pos = getElementBasePos(&v_pos);
  821. geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
  822. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  823. rect = core::rect<s32>(pos.X, pos.Y - m_btn_height,
  824. pos.X + geom.X, pos.Y + m_btn_height);
  825. }
  826. if(!data->explicit_size)
  827. warningstream<<"invalid use of button without a size[] element"<<std::endl;
  828. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  829. FieldSpec spec(
  830. name,
  831. wlabel,
  832. L"",
  833. 258 + m_fields.size()
  834. );
  835. spec.ftype = f_Button;
  836. if(type == "button_exit")
  837. spec.is_exit = true;
  838. GUIButton *e = GUIButton::addButton(Environment, rect, m_tsrc,
  839. data->current_parent, spec.fid, spec.flabel.c_str());
  840. auto style = getStyleForElement(type, name, (type != "button") ? "button" : "");
  841. spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
  842. e->setStyles(style);
  843. if (spec.fname == m_focused_element) {
  844. Environment->setFocus(e);
  845. }
  846. m_fields.push_back(spec);
  847. }
  848. bool GUIFormSpecMenu::parseMiddleRect(const std::string &value, core::rect<s32> *parsed_rect)
  849. {
  850. core::rect<s32> rect;
  851. std::vector<std::string> v_rect = split(value, ',');
  852. if (v_rect.size() == 1) {
  853. s32 x = stoi(v_rect[0]);
  854. rect.UpperLeftCorner = core::vector2di(x, x);
  855. rect.LowerRightCorner = core::vector2di(-x, -x);
  856. } else if (v_rect.size() == 2) {
  857. s32 x = stoi(v_rect[0]);
  858. s32 y = stoi(v_rect[1]);
  859. rect.UpperLeftCorner = core::vector2di(x, y);
  860. rect.LowerRightCorner = core::vector2di(-x, -y);
  861. // `-x` is interpreted as `w - x`
  862. } else if (v_rect.size() == 4) {
  863. rect.UpperLeftCorner = core::vector2di(stoi(v_rect[0]), stoi(v_rect[1]));
  864. rect.LowerRightCorner = core::vector2di(stoi(v_rect[2]), stoi(v_rect[3]));
  865. } else {
  866. warningstream << "Invalid rectangle string format: \"" << value
  867. << "\"" << std::endl;
  868. return false;
  869. }
  870. *parsed_rect = rect;
  871. return true;
  872. }
  873. void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
  874. {
  875. std::vector<std::string> parts;
  876. if (!precheckElement("background", element, 3, 5, parts))
  877. return;
  878. std::vector<std::string> v_pos = split(parts[0],',');
  879. std::vector<std::string> v_geom = split(parts[1],',');
  880. std::string name = unescape_string(parts[2]);
  881. MY_CHECKPOS("background",0);
  882. MY_CHECKGEOM("background",1);
  883. v2s32 pos;
  884. v2s32 geom;
  885. if (data->real_coordinates) {
  886. pos = getRealCoordinateBasePos(v_pos);
  887. geom = getRealCoordinateGeometry(v_geom);
  888. } else {
  889. pos = getElementBasePos(&v_pos);
  890. pos.X -= (spacing.X - (float)imgsize.X) / 2;
  891. pos.Y -= (spacing.Y - (float)imgsize.Y) / 2;
  892. geom.X = stof(v_geom[0]) * spacing.X;
  893. geom.Y = stof(v_geom[1]) * spacing.Y;
  894. }
  895. bool clip = false;
  896. if (parts.size() >= 4 && is_yes(parts[3])) {
  897. if (data->real_coordinates) {
  898. pos = getRealCoordinateBasePos(v_pos) * -1;
  899. geom = v2s32(0, 0);
  900. } else {
  901. pos.X = stoi(v_pos[0]); //acts as offset
  902. pos.Y = stoi(v_pos[1]);
  903. }
  904. clip = true;
  905. }
  906. core::rect<s32> middle;
  907. if (parts.size() >= 5)
  908. parseMiddleRect(parts[4], &middle);
  909. if (!data->explicit_size && !clip)
  910. warningstream << "invalid use of unclipped background without a size[] element" << std::endl;
  911. FieldSpec spec(
  912. name,
  913. L"",
  914. L"",
  915. 258 + m_fields.size()
  916. );
  917. core::rect<s32> rect{};
  918. v2s32 autoclip_offset{};
  919. if (!clip) {
  920. // no auto_clip => position like normal image
  921. rect = core::rect<s32>(pos, pos + geom);
  922. } else {
  923. // element will be auto-clipped when drawing
  924. autoclip_offset = pos;
  925. }
  926. GUIBackgroundImage *e = new GUIBackgroundImage(Environment, data->background_parent.get(),
  927. spec.fid, rect, name, middle, m_tsrc, clip, autoclip_offset);
  928. FATAL_ERROR_IF(!e, "Failed to create background formspec element");
  929. e->setNotClipped(true);
  930. m_fields.push_back(spec);
  931. e->drop();
  932. }
  933. void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
  934. {
  935. std::vector<std::string> parts = split(element,';');
  936. data->table_options.clear();
  937. for (const std::string &part : parts) {
  938. // Parse table option
  939. std::string opt = unescape_string(part);
  940. data->table_options.push_back(GUITable::splitOption(opt));
  941. }
  942. }
  943. void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
  944. {
  945. std::vector<std::string> parts = split(element,';');
  946. data->table_columns.clear();
  947. for (const std::string &part : parts) {
  948. std::vector<std::string> col_parts = split(part,',');
  949. GUITable::TableColumn column;
  950. // Parse column type
  951. if (!col_parts.empty())
  952. column.type = col_parts[0];
  953. // Parse column options
  954. for (size_t j = 1; j < col_parts.size(); ++j) {
  955. std::string opt = unescape_string(col_parts[j]);
  956. column.options.push_back(GUITable::splitOption(opt));
  957. }
  958. data->table_columns.push_back(column);
  959. }
  960. }
  961. void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
  962. {
  963. std::vector<std::string> parts;
  964. if (!precheckElement("table", element, 4, 5, parts))
  965. return;
  966. std::vector<std::string> v_pos = split(parts[0],',');
  967. std::vector<std::string> v_geom = split(parts[1],',');
  968. std::string name = parts[2];
  969. std::vector<std::string> items = split(parts[3],',');
  970. std::string str_initial_selection;
  971. if (parts.size() >= 5)
  972. str_initial_selection = parts[4];
  973. MY_CHECKPOS("table",0);
  974. MY_CHECKGEOM("table",1);
  975. v2s32 pos;
  976. v2s32 geom;
  977. if (data->real_coordinates) {
  978. pos = getRealCoordinateBasePos(v_pos);
  979. geom = getRealCoordinateGeometry(v_geom);
  980. } else {
  981. pos = getElementBasePos(&v_pos);
  982. geom.X = stof(v_geom[0]) * spacing.X;
  983. geom.Y = stof(v_geom[1]) * spacing.Y;
  984. }
  985. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  986. FieldSpec spec(
  987. name,
  988. L"",
  989. L"",
  990. 258 + m_fields.size()
  991. );
  992. spec.ftype = f_Table;
  993. for (std::string &item : items) {
  994. item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
  995. }
  996. //now really show table
  997. GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
  998. rect, m_tsrc);
  999. if (spec.fname == m_focused_element) {
  1000. Environment->setFocus(e);
  1001. }
  1002. e->setTable(data->table_options, data->table_columns, items);
  1003. if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
  1004. e->setDynamicData(data->table_dyndata[name]);
  1005. }
  1006. if (!str_initial_selection.empty() && str_initial_selection != "0")
  1007. e->setSelected(stoi(str_initial_selection));
  1008. auto style = getDefaultStyleForElement("table", name);
  1009. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  1010. e->setOverrideFont(style.getFont());
  1011. m_tables.emplace_back(spec, e);
  1012. m_fields.push_back(spec);
  1013. }
  1014. void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
  1015. {
  1016. std::vector<std::string> parts;
  1017. if (!precheckElement("textlist", element, 4, 6, parts))
  1018. return;
  1019. std::vector<std::string> v_pos = split(parts[0],',');
  1020. std::vector<std::string> v_geom = split(parts[1],',');
  1021. std::string name = parts[2];
  1022. std::vector<std::string> items = split(parts[3],',');
  1023. std::string str_initial_selection;
  1024. std::string str_transparent = "false";
  1025. if (parts.size() >= 5)
  1026. str_initial_selection = parts[4];
  1027. if (parts.size() >= 6)
  1028. str_transparent = parts[5];
  1029. MY_CHECKPOS("textlist",0);
  1030. MY_CHECKGEOM("textlist",1);
  1031. v2s32 pos;
  1032. v2s32 geom;
  1033. if (data->real_coordinates) {
  1034. pos = getRealCoordinateBasePos(v_pos);
  1035. geom = getRealCoordinateGeometry(v_geom);
  1036. } else {
  1037. pos = getElementBasePos(&v_pos);
  1038. geom.X = stof(v_geom[0]) * spacing.X;
  1039. geom.Y = stof(v_geom[1]) * spacing.Y;
  1040. }
  1041. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1042. FieldSpec spec(
  1043. name,
  1044. L"",
  1045. L"",
  1046. 258 + m_fields.size()
  1047. );
  1048. spec.ftype = f_Table;
  1049. for (std::string &item : items) {
  1050. item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
  1051. }
  1052. //now really show list
  1053. GUITable *e = new GUITable(Environment, data->current_parent, spec.fid,
  1054. rect, m_tsrc);
  1055. if (spec.fname == m_focused_element) {
  1056. Environment->setFocus(e);
  1057. }
  1058. e->setTextList(items, is_yes(str_transparent));
  1059. if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
  1060. e->setDynamicData(data->table_dyndata[name]);
  1061. }
  1062. if (!str_initial_selection.empty() && str_initial_selection != "0")
  1063. e->setSelected(stoi(str_initial_selection));
  1064. auto style = getDefaultStyleForElement("textlist", name);
  1065. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  1066. e->setOverrideFont(style.getFont());
  1067. m_tables.emplace_back(spec, e);
  1068. m_fields.push_back(spec);
  1069. }
  1070. void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
  1071. {
  1072. std::vector<std::string> parts;
  1073. if (!precheckElement("dropdown", element, 5, 6, parts))
  1074. return;
  1075. std::vector<std::string> v_pos = split(parts[0], ',');
  1076. std::string name = parts[2];
  1077. std::vector<std::string> items = split(parts[3], ',');
  1078. std::string str_initial_selection = parts[4];
  1079. if (parts.size() >= 6 && is_yes(parts[5]))
  1080. m_dropdown_index_event[name] = true;
  1081. MY_CHECKPOS("dropdown",0);
  1082. v2s32 pos;
  1083. v2s32 geom;
  1084. core::rect<s32> rect;
  1085. if (data->real_coordinates) {
  1086. std::vector<std::string> v_geom = split(parts[1],',');
  1087. if (v_geom.size() == 1)
  1088. v_geom.emplace_back("1");
  1089. MY_CHECKGEOM("dropdown",1);
  1090. pos = getRealCoordinateBasePos(v_pos);
  1091. geom = getRealCoordinateGeometry(v_geom);
  1092. rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1093. } else {
  1094. pos = getElementBasePos(&v_pos);
  1095. s32 width = stof(parts[1]) * spacing.Y;
  1096. rect = core::rect<s32>(pos.X, pos.Y,
  1097. pos.X + width, pos.Y + (m_btn_height * 2));
  1098. }
  1099. FieldSpec spec(
  1100. name,
  1101. L"",
  1102. L"",
  1103. 258 + m_fields.size()
  1104. );
  1105. spec.ftype = f_DropDown;
  1106. spec.send = true;
  1107. //now really show list
  1108. gui::IGUIComboBox *e = Environment->addComboBox(rect, data->current_parent,
  1109. spec.fid);
  1110. if (spec.fname == m_focused_element) {
  1111. Environment->setFocus(e);
  1112. }
  1113. for (const std::string &item : items) {
  1114. e->addItem(unescape_translate(unescape_string(
  1115. utf8_to_wide(item))).c_str());
  1116. }
  1117. if (!str_initial_selection.empty())
  1118. e->setSelected(stoi(str_initial_selection)-1);
  1119. auto style = getDefaultStyleForElement("dropdown", name);
  1120. spec.sound = style.get(StyleSpec::Property::SOUND, "");
  1121. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  1122. m_fields.push_back(spec);
  1123. m_dropdowns.emplace_back(spec, std::vector<std::string>());
  1124. std::vector<std::string> &values = m_dropdowns.back().second;
  1125. for (const std::string &item : items) {
  1126. values.push_back(unescape_string(item));
  1127. }
  1128. }
  1129. void GUIFormSpecMenu::parseFieldEnterAfterEdit(parserData *data, const std::string &element)
  1130. {
  1131. std::vector<std::string> parts;
  1132. if (!precheckElement("field_enter_after_edit", element, 2, 2, parts))
  1133. return;
  1134. field_enter_after_edit[parts[0]] = is_yes(parts[1]);
  1135. }
  1136. void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
  1137. {
  1138. std::vector<std::string> parts;
  1139. if (!precheckElement("field_close_on_enter", element, 2, 2, parts))
  1140. return;
  1141. field_close_on_enter[parts[0]] = is_yes(parts[1]);
  1142. }
  1143. void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
  1144. {
  1145. std::vector<std::string> parts;
  1146. if (!precheckElement("pwdfield", element, 4, 4, parts))
  1147. return;
  1148. std::vector<std::string> v_pos = split(parts[0],',');
  1149. std::vector<std::string> v_geom = split(parts[1],',');
  1150. std::string name = parts[2];
  1151. std::string label = parts[3];
  1152. MY_CHECKPOS("pwdfield",0);
  1153. MY_CHECKGEOM("pwdfield",1);
  1154. v2s32 pos;
  1155. v2s32 geom;
  1156. if (data->real_coordinates) {
  1157. pos = getRealCoordinateBasePos(v_pos);
  1158. geom = getRealCoordinateGeometry(v_geom);
  1159. } else {
  1160. pos = getElementBasePos(&v_pos);
  1161. pos -= padding;
  1162. geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
  1163. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  1164. pos.Y -= m_btn_height;
  1165. geom.Y = m_btn_height*2;
  1166. }
  1167. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1168. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  1169. FieldSpec spec(
  1170. name,
  1171. wlabel,
  1172. L"",
  1173. 258 + m_fields.size(),
  1174. 0,
  1175. ECI_IBEAM
  1176. );
  1177. spec.send = true;
  1178. gui::IGUIEditBox *e = Environment->addEditBox(0, rect, true,
  1179. data->current_parent, spec.fid);
  1180. if (spec.fname == m_focused_element) {
  1181. Environment->setFocus(e);
  1182. }
  1183. if (label.length() >= 1) {
  1184. int font_height = g_fontengine->getTextHeight();
  1185. rect.UpperLeftCorner.Y -= font_height;
  1186. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
  1187. gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
  1188. data->current_parent, 0);
  1189. }
  1190. e->setPasswordBox(true,L'*');
  1191. auto style = getDefaultStyleForElement("pwdfield", name, "field");
  1192. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  1193. e->setDrawBorder(style.getBool(StyleSpec::BORDER, true));
  1194. e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
  1195. e->setOverrideFont(style.getFont());
  1196. irr::SEvent evt;
  1197. evt.EventType = EET_KEY_INPUT_EVENT;
  1198. evt.KeyInput.Key = KEY_END;
  1199. evt.KeyInput.Char = 0;
  1200. evt.KeyInput.Control = false;
  1201. evt.KeyInput.Shift = false;
  1202. evt.KeyInput.PressedDown = true;
  1203. e->OnEvent(evt);
  1204. // Note: Before 5.2.0 "parts.size() >= 5" resulted in a
  1205. // warning referring to field_close_on_enter[]!
  1206. m_fields.push_back(spec);
  1207. }
  1208. void GUIFormSpecMenu::createTextField(parserData *data, FieldSpec &spec,
  1209. core::rect<s32> &rect, bool is_multiline)
  1210. {
  1211. bool is_editable = !spec.fname.empty();
  1212. if (!is_editable && !is_multiline) {
  1213. // spec field id to 0, this stops submit searching for a value that isn't there
  1214. gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
  1215. data->current_parent, 0);
  1216. return;
  1217. }
  1218. if (is_editable) {
  1219. spec.send = true;
  1220. } else if (is_multiline &&
  1221. spec.fdefault.empty() && !spec.flabel.empty()) {
  1222. // Multiline textareas: swap default and label for backwards compat
  1223. spec.flabel.swap(spec.fdefault);
  1224. }
  1225. gui::IGUIEditBox *e = nullptr;
  1226. if (is_multiline) {
  1227. e = new GUIEditBoxWithScrollBar(spec.fdefault.c_str(), true, Environment,
  1228. data->current_parent, spec.fid, rect, m_tsrc, is_editable, true);
  1229. } else if (is_editable) {
  1230. e = Environment->addEditBox(spec.fdefault.c_str(), rect, true,
  1231. data->current_parent, spec.fid);
  1232. e->grab();
  1233. }
  1234. auto style = getDefaultStyleForElement(is_multiline ? "textarea" : "field", spec.fname);
  1235. if (e) {
  1236. if (is_editable && spec.fname == m_focused_element)
  1237. Environment->setFocus(e);
  1238. if (is_multiline) {
  1239. e->setMultiLine(true);
  1240. e->setWordWrap(true);
  1241. e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
  1242. } else {
  1243. irr::SEvent evt;
  1244. evt.EventType = EET_KEY_INPUT_EVENT;
  1245. evt.KeyInput.Key = KEY_END;
  1246. evt.KeyInput.Char = 0;
  1247. evt.KeyInput.Control = 0;
  1248. evt.KeyInput.Shift = 0;
  1249. evt.KeyInput.PressedDown = true;
  1250. e->OnEvent(evt);
  1251. }
  1252. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  1253. e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
  1254. bool border = style.getBool(StyleSpec::BORDER, true);
  1255. e->setDrawBorder(border);
  1256. e->setDrawBackground(border);
  1257. e->setOverrideFont(style.getFont());
  1258. e->drop();
  1259. }
  1260. if (!spec.flabel.empty()) {
  1261. int font_height = g_fontengine->getTextHeight();
  1262. rect.UpperLeftCorner.Y -= font_height;
  1263. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
  1264. IGUIElement *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
  1265. rect, false, true, data->current_parent, 0);
  1266. if (t)
  1267. t->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  1268. }
  1269. }
  1270. void GUIFormSpecMenu::parseSimpleField(parserData *data,
  1271. std::vector<std::string> &parts)
  1272. {
  1273. std::string name = parts[0];
  1274. std::string label = parts[1];
  1275. std::string default_val = parts[2];
  1276. core::rect<s32> rect;
  1277. if (data->explicit_size)
  1278. warningstream << "invalid use of unpositioned \"field\" in inventory" << std::endl;
  1279. v2s32 pos = getElementBasePos(nullptr);
  1280. pos.Y = (data->simple_field_count + 2) * 60;
  1281. v2s32 size = DesiredRect.getSize();
  1282. rect = core::rect<s32>(
  1283. size.X / 2 - 150, pos.Y,
  1284. size.X / 2 - 150 + 300, pos.Y + m_btn_height * 2
  1285. );
  1286. if (m_form_src)
  1287. default_val = m_form_src->resolveText(default_val);
  1288. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  1289. FieldSpec spec(
  1290. name,
  1291. wlabel,
  1292. utf8_to_wide(unescape_string(default_val)),
  1293. 258 + m_fields.size(),
  1294. 0,
  1295. ECI_IBEAM
  1296. );
  1297. createTextField(data, spec, rect, false);
  1298. m_fields.push_back(spec);
  1299. data->simple_field_count++;
  1300. }
  1301. void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
  1302. const std::string &type)
  1303. {
  1304. std::vector<std::string> v_pos = split(parts[0],',');
  1305. std::vector<std::string> v_geom = split(parts[1],',');
  1306. std::string name = parts[2];
  1307. std::string label = parts[3];
  1308. std::string default_val = parts[4];
  1309. MY_CHECKPOS(type,0);
  1310. MY_CHECKGEOM(type,1);
  1311. v2s32 pos;
  1312. v2s32 geom;
  1313. if (data->real_coordinates) {
  1314. pos = getRealCoordinateBasePos(v_pos);
  1315. geom = getRealCoordinateGeometry(v_geom);
  1316. } else {
  1317. pos = getElementBasePos(&v_pos);
  1318. pos -= padding;
  1319. geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
  1320. if (type == "textarea")
  1321. {
  1322. geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
  1323. pos.Y += m_btn_height;
  1324. }
  1325. else
  1326. {
  1327. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  1328. pos.Y -= m_btn_height;
  1329. geom.Y = m_btn_height*2;
  1330. }
  1331. }
  1332. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1333. if(!data->explicit_size)
  1334. warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
  1335. if(m_form_src)
  1336. default_val = m_form_src->resolveText(default_val);
  1337. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  1338. FieldSpec spec(
  1339. name,
  1340. wlabel,
  1341. utf8_to_wide(unescape_string(default_val)),
  1342. 258 + m_fields.size(),
  1343. 0,
  1344. ECI_IBEAM
  1345. );
  1346. createTextField(data, spec, rect, type == "textarea");
  1347. // Note: Before 5.2.0 "parts.size() >= 6" resulted in a
  1348. // warning referring to field_close_on_enter[]!
  1349. m_fields.push_back(spec);
  1350. }
  1351. void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
  1352. const std::string &type)
  1353. {
  1354. std::vector<std::string> parts;
  1355. if (!precheckElement(type, element, 3, 5, parts))
  1356. return;
  1357. if (parts.size() == 3 || parts.size() == 4) {
  1358. parseSimpleField(data, parts);
  1359. return;
  1360. }
  1361. // Else: >= 5 arguments in "parts"
  1362. parseTextArea(data, parts, type);
  1363. }
  1364. void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &element)
  1365. {
  1366. std::vector<std::string> parts;
  1367. if (!precheckElement("hypertext", element, 4, 4, parts))
  1368. return;
  1369. std::vector<std::string> v_pos = split(parts[0], ',');
  1370. std::vector<std::string> v_geom = split(parts[1], ',');
  1371. std::string name = parts[2];
  1372. std::string text = parts[3];
  1373. MY_CHECKPOS("hypertext", 0);
  1374. MY_CHECKGEOM("hypertext", 1);
  1375. v2s32 pos;
  1376. v2s32 geom;
  1377. if (data->real_coordinates) {
  1378. pos = getRealCoordinateBasePos(v_pos);
  1379. geom = getRealCoordinateGeometry(v_geom);
  1380. } else {
  1381. pos = getElementBasePos(&v_pos);
  1382. pos -= padding;
  1383. geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
  1384. geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y - imgsize.Y);
  1385. pos.Y += m_btn_height;
  1386. }
  1387. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X + geom.X, pos.Y + geom.Y);
  1388. if(m_form_src)
  1389. text = m_form_src->resolveText(text);
  1390. FieldSpec spec(
  1391. name,
  1392. translate_string(utf8_to_wide(unescape_string(text))),
  1393. L"",
  1394. 258 + m_fields.size()
  1395. );
  1396. spec.ftype = f_HyperText;
  1397. auto style = getDefaultStyleForElement("hypertext", spec.fname);
  1398. spec.sound = style.get(StyleSpec::Property::SOUND, "");
  1399. GUIHyperText *e = new GUIHyperText(spec.flabel.c_str(), Environment,
  1400. data->current_parent, spec.fid, rect, m_client, m_tsrc);
  1401. e->drop();
  1402. m_fields.push_back(spec);
  1403. }
  1404. void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
  1405. {
  1406. std::vector<std::string> parts;
  1407. if (!precheckElement("label", element, 2, 2, parts))
  1408. return;
  1409. std::vector<std::string> v_pos = split(parts[0],',');
  1410. MY_CHECKPOS("label",0);
  1411. if(!data->explicit_size)
  1412. warningstream<<"invalid use of label without a size[] element"<<std::endl;
  1413. auto style = getDefaultStyleForElement("label", "");
  1414. gui::IGUIFont *font = style.getFont();
  1415. if (!font)
  1416. font = m_font;
  1417. EnrichedString str(unescape_string(utf8_to_wide(parts[1])));
  1418. size_t str_pos = 0;
  1419. for (size_t i = 0; str_pos < str.size(); ++i) {
  1420. // Split per line
  1421. size_t str_nl = str.getString().find(L'\n', str_pos);
  1422. if (str_nl == std::wstring::npos)
  1423. str_nl = str.getString().size();
  1424. EnrichedString line = str.substr(str_pos, str_nl - str_pos);
  1425. str_pos += line.size() + 1;
  1426. core::rect<s32> rect;
  1427. if (data->real_coordinates) {
  1428. // Lines are spaced at the distance of 1/2 imgsize.
  1429. // This alows lines that line up with the new elements
  1430. // easily without sacrificing good line distance. If
  1431. // it was one whole imgsize, it would have too much
  1432. // spacing.
  1433. v2s32 pos = getRealCoordinateBasePos(v_pos);
  1434. // Labels are positioned by their center, not their top.
  1435. pos.Y += (((float) imgsize.Y) / -2) + (((float) imgsize.Y) * i / 2);
  1436. rect = core::rect<s32>(
  1437. pos.X, pos.Y,
  1438. pos.X + font->getDimension(line.c_str()).Width,
  1439. pos.Y + imgsize.Y);
  1440. } else {
  1441. // Lines are spaced at the nominal distance of
  1442. // 2/5 inventory slot, even if the font doesn't
  1443. // quite match that. This provides consistent
  1444. // form layout, at the expense of sometimes
  1445. // having sub-optimal spacing for the font.
  1446. // We multiply by 2 and then divide by 5, rather
  1447. // than multiply by 0.4, to get exact results
  1448. // in the integer cases: 0.4 is not exactly
  1449. // representable in binary floating point.
  1450. v2s32 pos = getElementBasePos(nullptr);
  1451. pos.X += stof(v_pos[0]) * spacing.X;
  1452. pos.Y += (stof(v_pos[1]) + 7.0f / 30.0f) * spacing.Y;
  1453. pos.Y += ((float) i) * spacing.Y * 2.0 / 5.0;
  1454. rect = core::rect<s32>(
  1455. pos.X, pos.Y - m_btn_height,
  1456. pos.X + font->getDimension(line.c_str()).Width,
  1457. pos.Y + m_btn_height);
  1458. }
  1459. FieldSpec spec(
  1460. "",
  1461. L"",
  1462. L"",
  1463. 258 + m_fields.size(),
  1464. 4
  1465. );
  1466. gui::IGUIStaticText *e = gui::StaticText::add(Environment,
  1467. line, rect, false, false, data->current_parent,
  1468. spec.fid);
  1469. e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
  1470. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  1471. e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
  1472. e->setOverrideFont(font);
  1473. m_fields.push_back(spec);
  1474. // labels should let events through
  1475. e->grab();
  1476. m_clickthrough_elements.push_back(e);
  1477. }
  1478. }
  1479. void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
  1480. {
  1481. std::vector<std::string> parts;
  1482. if (!precheckElement("vertlabel", element, 2, 2, parts))
  1483. return;
  1484. std::vector<std::string> v_pos = split(parts[0],',');
  1485. std::wstring text = unescape_translate(
  1486. unescape_string(utf8_to_wide(parts[1])));
  1487. MY_CHECKPOS("vertlabel",1);
  1488. auto style = getDefaultStyleForElement("vertlabel", "", "label");
  1489. gui::IGUIFont *font = style.getFont();
  1490. if (!font)
  1491. font = m_font;
  1492. v2s32 pos;
  1493. core::rect<s32> rect;
  1494. if (data->real_coordinates) {
  1495. pos = getRealCoordinateBasePos(v_pos);
  1496. // Vertlabels are positioned by center, not left.
  1497. pos.X -= imgsize.X / 2;
  1498. // We use text.length + 1 because without it, the rect
  1499. // isn't quite tall enough and cuts off the text.
  1500. rect = core::rect<s32>(pos.X, pos.Y,
  1501. pos.X + imgsize.X,
  1502. pos.Y + font_line_height(font) *
  1503. (text.length() + 1));
  1504. } else {
  1505. pos = getElementBasePos(&v_pos);
  1506. // As above, the length must be one longer. The width of
  1507. // the rect (15 pixels) seems rather arbitrary, but
  1508. // changing it might break something.
  1509. rect = core::rect<s32>(
  1510. pos.X, pos.Y+((imgsize.Y/2) - m_btn_height),
  1511. pos.X+15, pos.Y +
  1512. font_line_height(font) *
  1513. (text.length() + 1) +
  1514. ((imgsize.Y/2) - m_btn_height));
  1515. }
  1516. if(!data->explicit_size)
  1517. warningstream<<"invalid use of label without a size[] element"<<std::endl;
  1518. std::wstring label;
  1519. for (wchar_t i : text) {
  1520. label += i;
  1521. label += L"\n";
  1522. }
  1523. FieldSpec spec(
  1524. "",
  1525. label,
  1526. L"",
  1527. 258 + m_fields.size()
  1528. );
  1529. gui::IGUIStaticText *e = gui::StaticText::add(Environment, spec.flabel.c_str(),
  1530. rect, false, false, data->current_parent, spec.fid);
  1531. e->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
  1532. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
  1533. e->setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
  1534. e->setOverrideFont(font);
  1535. m_fields.push_back(spec);
  1536. // vertlabels should let events through
  1537. e->grab();
  1538. m_clickthrough_elements.push_back(e);
  1539. }
  1540. void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
  1541. const std::string &type)
  1542. {
  1543. std::vector<std::string> parts;
  1544. if (!precheckElement("image_button", element, 5, 8, parts))
  1545. return;
  1546. if (parts.size() == 6) {
  1547. // Invalid argument count.
  1548. errorstream << "Invalid image_button element(" << parts.size() << "): '" << element << "'" << std::endl;
  1549. return;
  1550. }
  1551. std::vector<std::string> v_pos = split(parts[0],',');
  1552. std::vector<std::string> v_geom = split(parts[1],',');
  1553. std::string image_name = parts[2];
  1554. std::string name = parts[3];
  1555. std::string label = parts[4];
  1556. MY_CHECKPOS("image_button",0);
  1557. MY_CHECKGEOM("image_button",1);
  1558. std::string pressed_image_name;
  1559. if (parts.size() >= 8) {
  1560. pressed_image_name = parts[7];
  1561. }
  1562. v2s32 pos;
  1563. v2s32 geom;
  1564. if (data->real_coordinates) {
  1565. pos = getRealCoordinateBasePos(v_pos);
  1566. geom = getRealCoordinateGeometry(v_geom);
  1567. } else {
  1568. pos = getElementBasePos(&v_pos);
  1569. geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
  1570. geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
  1571. }
  1572. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
  1573. pos.Y+geom.Y);
  1574. if (!data->explicit_size)
  1575. warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
  1576. image_name = unescape_string(image_name);
  1577. pressed_image_name = unescape_string(pressed_image_name);
  1578. std::wstring wlabel = utf8_to_wide(unescape_string(label));
  1579. FieldSpec spec(
  1580. name,
  1581. wlabel,
  1582. utf8_to_wide(image_name),
  1583. 258 + m_fields.size()
  1584. );
  1585. spec.ftype = f_Button;
  1586. if (type == "image_button_exit")
  1587. spec.is_exit = true;
  1588. GUIButtonImage *e = GUIButtonImage::addButton(Environment, rect, m_tsrc,
  1589. data->current_parent, spec.fid, spec.flabel.c_str());
  1590. if (spec.fname == m_focused_element) {
  1591. Environment->setFocus(e);
  1592. }
  1593. auto style = getStyleForElement("image_button", spec.fname);
  1594. spec.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
  1595. // Override style properties with values specified directly in the element
  1596. if (!image_name.empty())
  1597. style[StyleSpec::STATE_DEFAULT].set(StyleSpec::FGIMG, image_name);
  1598. if (!pressed_image_name.empty())
  1599. style[StyleSpec::STATE_PRESSED].set(StyleSpec::FGIMG, pressed_image_name);
  1600. if (parts.size() >= 7) {
  1601. style[StyleSpec::STATE_DEFAULT].set(StyleSpec::NOCLIP, parts[5]);
  1602. style[StyleSpec::STATE_DEFAULT].set(StyleSpec::BORDER, parts[6]);
  1603. }
  1604. e->setStyles(style);
  1605. e->setScaleImage(true);
  1606. m_fields.push_back(spec);
  1607. }
  1608. void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
  1609. {
  1610. std::vector<std::string> parts;
  1611. if (!precheckElement("tabheader", element, 4, 7, parts))
  1612. return;
  1613. // Length 7: Additional "height" parameter after "pos". Only valid with real_coordinates.
  1614. // Note: New arguments for the "height" syntax cannot be added without breaking older clients.
  1615. if (parts.size() == 5 || (parts.size() == 7 && !data->real_coordinates)) {
  1616. errorstream << "Invalid tabheader element(" << parts.size() << "): '"
  1617. << element << "'" << std::endl;
  1618. return;
  1619. }
  1620. std::vector<std::string> v_pos = split(parts[0],',');
  1621. // If we're using real coordinates, add an extra field for height.
  1622. // Width is not here because tabs are the width of the text, and
  1623. // there's no reason to change that.
  1624. unsigned int i = 0;
  1625. std::vector<std::string> v_geom = {"1", "1"}; // Dummy width and height
  1626. bool auto_width = true;
  1627. if (parts.size() == 7) {
  1628. i++;
  1629. v_geom = split(parts[1], ',');
  1630. if (v_geom.size() == 1)
  1631. v_geom.insert(v_geom.begin(), "1"); // Dummy value
  1632. else
  1633. auto_width = false;
  1634. }
  1635. std::string name = parts[i+1];
  1636. std::vector<std::string> buttons = split(parts[i+2], ',');
  1637. std::string str_index = parts[i+3];
  1638. bool show_background = true;
  1639. bool show_border = true;
  1640. int tab_index = stoi(str_index) - 1;
  1641. MY_CHECKPOS("tabheader", 0);
  1642. if (parts.size() == 6 + i) {
  1643. if (parts[4+i] == "true")
  1644. show_background = false;
  1645. if (parts[5+i] == "false")
  1646. show_border = false;
  1647. }
  1648. FieldSpec spec(
  1649. name,
  1650. L"",
  1651. L"",
  1652. 258 + m_fields.size()
  1653. );
  1654. spec.ftype = f_TabHeader;
  1655. v2s32 pos;
  1656. v2s32 geom;
  1657. if (data->real_coordinates) {
  1658. pos = getRealCoordinateBasePos(v_pos);
  1659. geom = getRealCoordinateGeometry(v_geom);
  1660. // Set default height
  1661. if (parts.size() <= 6)
  1662. geom.Y = m_btn_height * 2;
  1663. pos.Y -= geom.Y; // TabHeader base pos is the bottom, not the top.
  1664. if (auto_width)
  1665. geom.X = DesiredRect.getWidth(); // Set automatic width
  1666. MY_CHECKGEOM("tabheader", 1);
  1667. } else {
  1668. v2f32 pos_f = pos_offset * spacing;
  1669. pos_f.X += stof(v_pos[0]) * spacing.X;
  1670. pos_f.Y += stof(v_pos[1]) * spacing.Y - m_btn_height * 2;
  1671. pos = v2s32(pos_f.X, pos_f.Y);
  1672. geom.Y = m_btn_height * 2;
  1673. geom.X = DesiredRect.getWidth();
  1674. }
  1675. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
  1676. pos.Y+geom.Y);
  1677. gui::IGUITabControl *e = Environment->addTabControl(rect,
  1678. data->current_parent, show_background, show_border, spec.fid);
  1679. e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
  1680. irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
  1681. e->setTabHeight(geom.Y);
  1682. auto style = getDefaultStyleForElement("tabheader", name);
  1683. spec.sound = style.get(StyleSpec::Property::SOUND, "");
  1684. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, true));
  1685. for (const std::string &button : buttons) {
  1686. auto tab = e->addTab(unescape_translate(unescape_string(
  1687. utf8_to_wide(button))).c_str(), -1);
  1688. if (style.isNotDefault(StyleSpec::BGCOLOR))
  1689. tab->setBackgroundColor(style.getColor(StyleSpec::BGCOLOR));
  1690. tab->setTextColor(style.getColor(StyleSpec::TEXTCOLOR, video::SColor(0xFFFFFFFF)));
  1691. }
  1692. if ((tab_index >= 0) &&
  1693. (buttons.size() < INT_MAX) &&
  1694. (tab_index < (int) buttons.size()))
  1695. e->setActiveTab(tab_index);
  1696. m_fields.push_back(spec);
  1697. m_tabheader_upper_edge = MYMIN(m_tabheader_upper_edge, rect.UpperLeftCorner.Y);
  1698. }
  1699. void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
  1700. {
  1701. MY_CHECKCLIENT("item_image_button");
  1702. std::vector<std::string> parts;
  1703. if (!precheckElement("item_image_button", element, 5, 5, parts))
  1704. return;
  1705. std::vector<std::string> v_pos = split(parts[0],',');
  1706. std::vector<std::string> v_geom = split(parts[1],',');
  1707. std::string item_name = parts[2];
  1708. std::string name = parts[3];
  1709. std::string label = parts[4];
  1710. label = unescape_string(label);
  1711. item_name = unescape_string(item_name);
  1712. MY_CHECKPOS("item_image_button",0);
  1713. MY_CHECKGEOM("item_image_button",1);
  1714. v2s32 pos;
  1715. v2s32 geom;
  1716. if (data->real_coordinates) {
  1717. pos = getRealCoordinateBasePos(v_pos);
  1718. geom = getRealCoordinateGeometry(v_geom);
  1719. } else {
  1720. pos = getElementBasePos(&v_pos);
  1721. geom.X = (stof(v_geom[0]) * spacing.X) - (spacing.X - imgsize.X);
  1722. geom.Y = (stof(v_geom[1]) * spacing.Y) - (spacing.Y - imgsize.Y);
  1723. }
  1724. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1725. if(!data->explicit_size)
  1726. warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
  1727. IItemDefManager *idef = m_client->idef();
  1728. ItemStack item;
  1729. item.deSerialize(item_name, idef);
  1730. m_tooltips[name] =
  1731. TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
  1732. m_default_tooltip_bgcolor,
  1733. m_default_tooltip_color);
  1734. // the spec for the button
  1735. FieldSpec spec_btn(
  1736. name,
  1737. utf8_to_wide(label),
  1738. utf8_to_wide(item_name),
  1739. 258 + m_fields.size(),
  1740. 2
  1741. );
  1742. GUIButtonItemImage *e_btn = GUIButtonItemImage::addButton(Environment,
  1743. rect, m_tsrc, data->current_parent, spec_btn.fid, spec_btn.flabel.c_str(),
  1744. item_name, m_client);
  1745. auto style = getStyleForElement("item_image_button", spec_btn.fname, "image_button");
  1746. spec_btn.sound = style[StyleSpec::STATE_DEFAULT].get(StyleSpec::Property::SOUND, "");
  1747. e_btn->setStyles(style);
  1748. if (spec_btn.fname == m_focused_element) {
  1749. Environment->setFocus(e_btn);
  1750. }
  1751. spec_btn.ftype = f_Button;
  1752. rect += data->basepos-padding;
  1753. spec_btn.rect = rect;
  1754. m_fields.push_back(spec_btn);
  1755. }
  1756. void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
  1757. {
  1758. std::vector<std::string> parts;
  1759. if (!precheckElement("box", element, 3, 3, parts))
  1760. return;
  1761. std::vector<std::string> v_pos = split(parts[0], ',');
  1762. std::vector<std::string> v_geom = split(parts[1], ',');
  1763. MY_CHECKPOS("box", 0);
  1764. MY_CHECKGEOM("box", 1);
  1765. v2s32 pos;
  1766. v2s32 geom;
  1767. if (data->real_coordinates) {
  1768. pos = getRealCoordinateBasePos(v_pos);
  1769. geom = getRealCoordinateGeometry(v_geom);
  1770. } else {
  1771. pos = getElementBasePos(&v_pos);
  1772. geom.X = stof(v_geom[0]) * spacing.X;
  1773. geom.Y = stof(v_geom[1]) * spacing.Y;
  1774. }
  1775. FieldSpec spec(
  1776. "",
  1777. L"",
  1778. L"",
  1779. 258 + m_fields.size(),
  1780. -2
  1781. );
  1782. spec.ftype = f_Box;
  1783. auto style = getDefaultStyleForElement("box", spec.fname);
  1784. video::SColor tmp_color;
  1785. std::array<video::SColor, 4> colors;
  1786. std::array<video::SColor, 4> bordercolors = {0x0, 0x0, 0x0, 0x0};
  1787. std::array<s32, 4> borderwidths = {0, 0, 0, 0};
  1788. if (parseColorString(parts[2], tmp_color, true, 0x8C)) {
  1789. colors = {tmp_color, tmp_color, tmp_color, tmp_color};
  1790. } else {
  1791. colors = style.getColorArray(StyleSpec::COLORS, {0x0, 0x0, 0x0, 0x0});
  1792. bordercolors = style.getColorArray(StyleSpec::BORDERCOLORS,
  1793. {0x0, 0x0, 0x0, 0x0});
  1794. borderwidths = style.getIntArray(StyleSpec::BORDERWIDTHS, {0, 0, 0, 0});
  1795. }
  1796. core::rect<s32> rect(pos, pos + geom);
  1797. GUIBox *e = new GUIBox(Environment, data->current_parent, spec.fid, rect,
  1798. colors, bordercolors, borderwidths);
  1799. e->setNotClipped(style.getBool(StyleSpec::NOCLIP, m_formspec_version < 3));
  1800. e->drop();
  1801. m_fields.push_back(spec);
  1802. }
  1803. void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
  1804. {
  1805. std::vector<std::string> parts;
  1806. if (!precheckElement("bgcolor", element, 1, 3, parts))
  1807. return;
  1808. const u32 parameter_count = parts.size();
  1809. if (parameter_count > 2 && m_formspec_version < 3) {
  1810. errorstream << "Invalid bgcolor element(" << parameter_count << "): '"
  1811. << element << "'" << std::endl;
  1812. return;
  1813. }
  1814. // bgcolor
  1815. if (parameter_count >= 1 && !parts[0].empty())
  1816. parseColorString(parts[0], m_bgcolor, false);
  1817. // fullscreen
  1818. if (parameter_count >= 2) {
  1819. if (parts[1] == "both") {
  1820. m_bgnonfullscreen = true;
  1821. m_bgfullscreen = true;
  1822. } else if (parts[1] == "neither") {
  1823. m_bgnonfullscreen = false;
  1824. m_bgfullscreen = false;
  1825. } else if (!parts[1].empty() || m_formspec_version < 3) {
  1826. m_bgfullscreen = is_yes(parts[1]);
  1827. m_bgnonfullscreen = !m_bgfullscreen;
  1828. }
  1829. }
  1830. // fbgcolor
  1831. if (parameter_count >= 3 && !parts[2].empty())
  1832. parseColorString(parts[2], m_fullscreen_bgcolor, false);
  1833. }
  1834. void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
  1835. {
  1836. std::vector<std::string> parts;
  1837. // Legacy Note: If clients older than 5.5.0-dev are supplied with additional arguments,
  1838. // the tooltip colors will be ignored.
  1839. if (!precheckElement("listcolors", element, 2, 5, parts))
  1840. return;
  1841. if (parts.size() == 4) {
  1842. // Invalid argument combination
  1843. errorstream << "Invalid listcolors element(" << parts.size() << "): '"
  1844. << element << "'" << std::endl;
  1845. return;
  1846. }
  1847. parseColorString(parts[0], data->inventorylist_options.slotbg_n, false);
  1848. parseColorString(parts[1], data->inventorylist_options.slotbg_h, false);
  1849. if (parts.size() >= 3) {
  1850. if (parseColorString(parts[2], data->inventorylist_options.slotbordercolor,
  1851. false)) {
  1852. data->inventorylist_options.slotborder = true;
  1853. }
  1854. }
  1855. if (parts.size() >= 5) {
  1856. video::SColor tmp_color;
  1857. if (parseColorString(parts[3], tmp_color, false))
  1858. m_default_tooltip_bgcolor = tmp_color;
  1859. if (parseColorString(parts[4], tmp_color, false))
  1860. m_default_tooltip_color = tmp_color;
  1861. }
  1862. // update all already parsed inventorylists
  1863. for (GUIInventoryList *e : m_inventorylists) {
  1864. e->setSlotBGColors(data->inventorylist_options.slotbg_n,
  1865. data->inventorylist_options.slotbg_h);
  1866. e->setSlotBorders(data->inventorylist_options.slotborder,
  1867. data->inventorylist_options.slotbordercolor);
  1868. }
  1869. }
  1870. void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
  1871. {
  1872. std::vector<std::string> parts;
  1873. if (!precheckElement("tooltip", element, 2, 5, parts))
  1874. return;
  1875. // Get mode and check size
  1876. bool rect_mode = parts[0].find(',') != std::string::npos;
  1877. size_t base_size = rect_mode ? 3 : 2;
  1878. if (parts.size() != base_size && parts.size() != base_size + 2) {
  1879. errorstream << "Invalid tooltip element(" << parts.size() << "): '"
  1880. << element << "'" << std::endl;
  1881. return;
  1882. }
  1883. // Read colors
  1884. video::SColor bgcolor = m_default_tooltip_bgcolor;
  1885. video::SColor color = m_default_tooltip_color;
  1886. if (parts.size() == base_size + 2 &&
  1887. (!parseColorString(parts[base_size], bgcolor, false) ||
  1888. !parseColorString(parts[base_size + 1], color, false))) {
  1889. errorstream << "Invalid color in tooltip element(" << parts.size()
  1890. << "): '" << element << "'" << std::endl;
  1891. return;
  1892. }
  1893. // Make tooltip spec
  1894. std::string text = unescape_string(parts[rect_mode ? 2 : 1]);
  1895. TooltipSpec spec(utf8_to_wide(text), bgcolor, color);
  1896. // Add tooltip
  1897. if (rect_mode) {
  1898. std::vector<std::string> v_pos = split(parts[0], ',');
  1899. std::vector<std::string> v_geom = split(parts[1], ',');
  1900. MY_CHECKPOS("tooltip", 0);
  1901. MY_CHECKGEOM("tooltip", 1);
  1902. v2s32 pos;
  1903. v2s32 geom;
  1904. if (data->real_coordinates) {
  1905. pos = getRealCoordinateBasePos(v_pos);
  1906. geom = getRealCoordinateGeometry(v_geom);
  1907. } else {
  1908. pos = getElementBasePos(&v_pos);
  1909. geom.X = stof(v_geom[0]) * spacing.X;
  1910. geom.Y = stof(v_geom[1]) * spacing.Y;
  1911. }
  1912. FieldSpec fieldspec(
  1913. "",
  1914. L"",
  1915. L"",
  1916. 258 + m_fields.size()
  1917. );
  1918. core::rect<s32> rect(pos, pos + geom);
  1919. gui::IGUIElement *e = new gui::IGUIElement(EGUIET_ELEMENT, Environment,
  1920. data->current_parent, fieldspec.fid, rect);
  1921. // the element the rect tooltip is bound to should not block mouse-clicks
  1922. e->setVisible(false);
  1923. m_fields.push_back(fieldspec);
  1924. m_tooltip_rects.emplace_back(e, spec);
  1925. } else {
  1926. m_tooltips[parts[0]] = spec;
  1927. }
  1928. }
  1929. bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
  1930. {
  1931. //some prechecks
  1932. if (data.empty())
  1933. return false;
  1934. std::vector<std::string> parts = split(data,'[');
  1935. if (parts.size() < 2) {
  1936. return false;
  1937. }
  1938. if (trim(parts[0]) != "formspec_version") {
  1939. return false;
  1940. }
  1941. if (is_number(parts[1])) {
  1942. m_formspec_version = mystoi(parts[1]);
  1943. return true;
  1944. }
  1945. return false;
  1946. }
  1947. bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
  1948. {
  1949. if (element.empty())
  1950. return false;
  1951. std::vector<std::string> parts = split(element,'[');
  1952. if (parts.size() < 2)
  1953. return false;
  1954. auto type = trim(parts[0]);
  1955. std::string description(trim(parts[1]));
  1956. if (type != "size" && type != "invsize")
  1957. return false;
  1958. if (type == "invsize")
  1959. warningstream << "Deprecated formspec element \"invsize\" is used" << std::endl;
  1960. parseSize(data, description);
  1961. return true;
  1962. }
  1963. bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
  1964. {
  1965. if (element.empty())
  1966. return false;
  1967. std::vector<std::string> parts = split(element, '[');
  1968. if (parts.size() != 2)
  1969. return false;
  1970. auto type = trim(parts[0]);
  1971. std::string description(trim(parts[1]));
  1972. if (type != "position")
  1973. return false;
  1974. parsePosition(data, description);
  1975. return true;
  1976. }
  1977. void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
  1978. {
  1979. std::vector<std::string> parts = split(element, ';');
  1980. if (parts.size() == 1 ||
  1981. (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) {
  1982. std::vector<std::string> v_geom = split(parts[0], ',');
  1983. MY_CHECKGEOM("position", 0);
  1984. data->offset.X = stof(v_geom[0]);
  1985. data->offset.Y = stof(v_geom[1]);
  1986. return;
  1987. }
  1988. errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
  1989. }
  1990. bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
  1991. {
  1992. if (element.empty())
  1993. return false;
  1994. std::vector<std::string> parts = split(element, '[');
  1995. if (parts.size() != 2)
  1996. return false;
  1997. auto type = trim(parts[0]);
  1998. std::string description(trim(parts[1]));
  1999. if (type != "anchor")
  2000. return false;
  2001. parseAnchor(data, description);
  2002. return true;
  2003. }
  2004. void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
  2005. {
  2006. std::vector<std::string> parts = split(element, ';');
  2007. if (parts.size() == 1 ||
  2008. (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) {
  2009. std::vector<std::string> v_geom = split(parts[0], ',');
  2010. MY_CHECKGEOM("anchor", 0);
  2011. data->anchor.X = stof(v_geom[0]);
  2012. data->anchor.Y = stof(v_geom[1]);
  2013. return;
  2014. }
  2015. errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
  2016. << "'" << std::endl;
  2017. }
  2018. bool GUIFormSpecMenu::parsePaddingDirect(parserData *data, const std::string &element)
  2019. {
  2020. if (element.empty())
  2021. return false;
  2022. std::vector<std::string> parts = split(element, '[');
  2023. if (parts.size() != 2)
  2024. return false;
  2025. auto type = trim(parts[0]);
  2026. std::string description(trim(parts[1]));
  2027. if (type != "padding")
  2028. return false;
  2029. parsePadding(data, description);
  2030. return true;
  2031. }
  2032. void GUIFormSpecMenu::parsePadding(parserData *data, const std::string &element)
  2033. {
  2034. std::vector<std::string> parts = split(element, ';');
  2035. if (parts.size() == 1 ||
  2036. (parts.size() > 1 && m_formspec_version > FORMSPEC_API_VERSION)) {
  2037. std::vector<std::string> v_geom = split(parts[0], ',');
  2038. MY_CHECKGEOM("padding", 0);
  2039. data->padding.X = stof(v_geom[0]);
  2040. data->padding.Y = stof(v_geom[1]);
  2041. return;
  2042. }
  2043. errorstream << "Invalid padding element (" << parts.size() << "): '" << element
  2044. << "'" << std::endl;
  2045. }
  2046. bool GUIFormSpecMenu::parseStyle(parserData *data, const std::string &element, bool style_type)
  2047. {
  2048. std::vector<std::string> parts = split(element, ';');
  2049. if (parts.size() < 2) {
  2050. errorstream << "Invalid style element (" << parts.size() << "): '" << element
  2051. << "'" << std::endl;
  2052. return false;
  2053. }
  2054. StyleSpec spec;
  2055. // Parse properties
  2056. for (size_t i = 1; i < parts.size(); i++) {
  2057. size_t equal_pos = parts[i].find('=');
  2058. if (equal_pos == std::string::npos) {
  2059. errorstream << "Invalid style element (Property missing value): '" << element
  2060. << "'" << std::endl;
  2061. return false;
  2062. }
  2063. std::string propname = trim(parts[i].substr(0, equal_pos));
  2064. std::string value = trim(unescape_string(parts[i].substr(equal_pos + 1)));
  2065. std::transform(propname.begin(), propname.end(), propname.begin(), ::tolower);
  2066. StyleSpec::Property prop = StyleSpec::GetPropertyByName(propname);
  2067. if (prop == StyleSpec::NONE) {
  2068. if (property_warned.find(propname) != property_warned.end()) {
  2069. warningstream << "Invalid style element (Unknown property " << propname << "): '"
  2070. << element
  2071. << "'" << std::endl;
  2072. property_warned.insert(propname);
  2073. }
  2074. continue;
  2075. }
  2076. spec.set(prop, value);
  2077. }
  2078. std::vector<std::string> selectors = split(parts[0], ',');
  2079. for (size_t sel = 0; sel < selectors.size(); sel++) {
  2080. std::string selector(trim(selectors[sel]));
  2081. // Copy the style properties to a new StyleSpec
  2082. // This allows a separate state mask per-selector
  2083. StyleSpec selector_spec = spec;
  2084. // Parse state information, if it exists
  2085. bool state_valid = true;
  2086. size_t state_pos = selector.find(':');
  2087. if (state_pos != std::string::npos) {
  2088. std::string state_str = selector.substr(state_pos + 1);
  2089. selector = selector.substr(0, state_pos);
  2090. if (state_str.empty()) {
  2091. errorstream << "Invalid style element (Invalid state): '" << element
  2092. << "'" << std::endl;
  2093. state_valid = false;
  2094. } else {
  2095. std::vector<std::string> states = split(state_str, '+');
  2096. for (std::string &state : states) {
  2097. StyleSpec::State converted = StyleSpec::getStateByName(state);
  2098. if (converted == StyleSpec::STATE_INVALID) {
  2099. infostream << "Unknown style state " << state <<
  2100. " in element '" << element << "'" << std::endl;
  2101. state_valid = false;
  2102. break;
  2103. }
  2104. selector_spec.addState(converted);
  2105. }
  2106. }
  2107. }
  2108. if (!state_valid) {
  2109. // Skip this selector
  2110. continue;
  2111. }
  2112. if (style_type) {
  2113. theme_by_type[selector].push_back(selector_spec);
  2114. } else {
  2115. theme_by_name[selector].push_back(selector_spec);
  2116. }
  2117. // Backwards-compatibility for existing _hovered/_pressed properties
  2118. if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)
  2119. || selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)
  2120. || selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
  2121. StyleSpec hover_spec;
  2122. hover_spec.addState(StyleSpec::STATE_HOVERED);
  2123. if (selector_spec.hasProperty(StyleSpec::BGCOLOR_HOVERED)) {
  2124. hover_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_HOVERED, ""));
  2125. }
  2126. if (selector_spec.hasProperty(StyleSpec::BGIMG_HOVERED)) {
  2127. hover_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_HOVERED, ""));
  2128. }
  2129. if (selector_spec.hasProperty(StyleSpec::FGIMG_HOVERED)) {
  2130. hover_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_HOVERED, ""));
  2131. }
  2132. if (style_type) {
  2133. theme_by_type[selector].push_back(hover_spec);
  2134. } else {
  2135. theme_by_name[selector].push_back(hover_spec);
  2136. }
  2137. }
  2138. if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)
  2139. || selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)
  2140. || selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
  2141. StyleSpec press_spec;
  2142. press_spec.addState(StyleSpec::STATE_PRESSED);
  2143. if (selector_spec.hasProperty(StyleSpec::BGCOLOR_PRESSED)) {
  2144. press_spec.set(StyleSpec::BGCOLOR, selector_spec.get(StyleSpec::BGCOLOR_PRESSED, ""));
  2145. }
  2146. if (selector_spec.hasProperty(StyleSpec::BGIMG_PRESSED)) {
  2147. press_spec.set(StyleSpec::BGIMG, selector_spec.get(StyleSpec::BGIMG_PRESSED, ""));
  2148. }
  2149. if (selector_spec.hasProperty(StyleSpec::FGIMG_PRESSED)) {
  2150. press_spec.set(StyleSpec::FGIMG, selector_spec.get(StyleSpec::FGIMG_PRESSED, ""));
  2151. }
  2152. if (style_type) {
  2153. theme_by_type[selector].push_back(press_spec);
  2154. } else {
  2155. theme_by_name[selector].push_back(press_spec);
  2156. }
  2157. }
  2158. }
  2159. return true;
  2160. }
  2161. void GUIFormSpecMenu::parseSetFocus(const std::string &element)
  2162. {
  2163. std::vector<std::string> parts;
  2164. if (!precheckElement("set_focus", element, 1, 2, parts))
  2165. return;
  2166. if (m_is_form_regenerated)
  2167. return; // Never focus on resizing
  2168. bool force_focus = parts.size() >= 2 && is_yes(parts[1]);
  2169. if (force_focus || m_text_dst->m_formname != m_last_formname)
  2170. setFocus(parts[0]);
  2171. }
  2172. void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
  2173. {
  2174. MY_CHECKCLIENT("model");
  2175. std::vector<std::string> parts;
  2176. if (!precheckElement("model", element, 5, 10, parts))
  2177. return;
  2178. // Avoid length checks by resizing
  2179. if (parts.size() < 10)
  2180. parts.resize(10);
  2181. std::vector<std::string> v_pos = split(parts[0], ',');
  2182. std::vector<std::string> v_geom = split(parts[1], ',');
  2183. std::string name = unescape_string(parts[2]);
  2184. std::string meshstr = unescape_string(parts[3]);
  2185. std::vector<std::string> textures = split(parts[4], ',');
  2186. std::vector<std::string> vec_rot = split(parts[5], ',');
  2187. bool inf_rotation = is_yes(parts[6]);
  2188. bool mousectrl = is_yes(parts[7]) || parts[7].empty(); // default true
  2189. std::vector<std::string> frame_loop = split(parts[8], ',');
  2190. std::string speed = unescape_string(parts[9]);
  2191. MY_CHECKPOS("model", 0);
  2192. MY_CHECKGEOM("model", 1);
  2193. v2s32 pos;
  2194. v2s32 geom;
  2195. if (data->real_coordinates) {
  2196. pos = getRealCoordinateBasePos(v_pos);
  2197. geom = getRealCoordinateGeometry(v_geom);
  2198. } else {
  2199. pos = getElementBasePos(&v_pos);
  2200. geom.X = stof(v_geom[0]) * (float)imgsize.X;
  2201. geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
  2202. }
  2203. if (!data->explicit_size)
  2204. warningstream << "invalid use of model without a size[] element" << std::endl;
  2205. scene::IAnimatedMesh *mesh = m_client->getMesh(meshstr);
  2206. if (!mesh) {
  2207. errorstream << "Invalid model element: Unable to load mesh:"
  2208. << std::endl << "\t" << meshstr << std::endl;
  2209. return;
  2210. }
  2211. FieldSpec spec(
  2212. name,
  2213. L"",
  2214. L"",
  2215. 258 + m_fields.size()
  2216. );
  2217. core::rect<s32> rect(pos, pos + geom);
  2218. GUIScene *e = new GUIScene(Environment, m_client->getSceneManager(),
  2219. data->current_parent, rect, spec.fid);
  2220. auto meshnode = e->setMesh(mesh);
  2221. for (u32 i = 0; i < textures.size() && i < meshnode->getMaterialCount(); ++i)
  2222. e->setTexture(i, m_tsrc->getTexture(unescape_string(textures[i])));
  2223. if (vec_rot.size() >= 2)
  2224. e->setRotation(v2f(stof(vec_rot[0]), stof(vec_rot[1])));
  2225. e->enableContinuousRotation(inf_rotation);
  2226. e->enableMouseControl(mousectrl);
  2227. s32 frame_loop_begin = 0;
  2228. s32 frame_loop_end = 0x7FFFFFFF;
  2229. if (frame_loop.size() == 2) {
  2230. frame_loop_begin = stoi(frame_loop[0]);
  2231. frame_loop_end = stoi(frame_loop[1]);
  2232. }
  2233. e->setFrameLoop(frame_loop_begin, frame_loop_end);
  2234. e->setAnimationSpeed(stof(speed));
  2235. auto style = getStyleForElement("model", spec.fname);
  2236. e->setStyles(style);
  2237. e->drop();
  2238. m_fields.push_back(spec);
  2239. }
  2240. void GUIFormSpecMenu::removeAll()
  2241. {
  2242. // Remove children
  2243. removeAllChildren();
  2244. removeTooltip();
  2245. for (auto &table_it : m_tables)
  2246. table_it.second->drop();
  2247. for (auto &inventorylist_it : m_inventorylists)
  2248. inventorylist_it->drop();
  2249. for (auto &checkbox_it : m_checkboxes)
  2250. checkbox_it.second->drop();
  2251. for (auto &scrollbar_it : m_scrollbars)
  2252. scrollbar_it.second->drop();
  2253. for (auto &tooltip_rect_it : m_tooltip_rects)
  2254. tooltip_rect_it.first->drop();
  2255. for (auto &clickthrough_it : m_clickthrough_elements)
  2256. clickthrough_it->drop();
  2257. for (auto &scroll_container_it : m_scroll_containers)
  2258. scroll_container_it.second->drop();
  2259. }
  2260. void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
  2261. {
  2262. //some prechecks
  2263. if (element.empty())
  2264. return;
  2265. if (parseVersionDirect(element))
  2266. return;
  2267. size_t pos = element.find('[');
  2268. if (pos == std::string::npos)
  2269. return;
  2270. std::string type = trim(element.substr(0, pos));
  2271. std::string description = element.substr(pos+1);
  2272. if (type == "container") {
  2273. parseContainer(data, description);
  2274. return;
  2275. }
  2276. if (type == "container_end") {
  2277. parseContainerEnd(data);
  2278. return;
  2279. }
  2280. if (type == "list") {
  2281. parseList(data, description);
  2282. return;
  2283. }
  2284. if (type == "listring") {
  2285. parseListRing(data, description);
  2286. return;
  2287. }
  2288. if (type == "checkbox") {
  2289. parseCheckbox(data, description);
  2290. return;
  2291. }
  2292. if (type == "image") {
  2293. parseImage(data, description);
  2294. return;
  2295. }
  2296. if (type == "animated_image") {
  2297. parseAnimatedImage(data, description);
  2298. return;
  2299. }
  2300. if (type == "item_image") {
  2301. parseItemImage(data, description);
  2302. return;
  2303. }
  2304. if (type == "button" || type == "button_exit") {
  2305. parseButton(data, description, type);
  2306. return;
  2307. }
  2308. if (type == "background" || type == "background9") {
  2309. parseBackground(data, description);
  2310. return;
  2311. }
  2312. if (type == "tableoptions"){
  2313. parseTableOptions(data,description);
  2314. return;
  2315. }
  2316. if (type == "tablecolumns"){
  2317. parseTableColumns(data,description);
  2318. return;
  2319. }
  2320. if (type == "table"){
  2321. parseTable(data,description);
  2322. return;
  2323. }
  2324. if (type == "textlist"){
  2325. parseTextList(data,description);
  2326. return;
  2327. }
  2328. if (type == "dropdown"){
  2329. parseDropDown(data,description);
  2330. return;
  2331. }
  2332. if (type == "field_enter_after_edit") {
  2333. parseFieldEnterAfterEdit(data, description);
  2334. return;
  2335. }
  2336. if (type == "field_close_on_enter") {
  2337. parseFieldCloseOnEnter(data, description);
  2338. return;
  2339. }
  2340. if (type == "pwdfield") {
  2341. parsePwdField(data,description);
  2342. return;
  2343. }
  2344. if ((type == "field") || (type == "textarea")){
  2345. parseField(data,description,type);
  2346. return;
  2347. }
  2348. if (type == "hypertext") {
  2349. parseHyperText(data,description);
  2350. return;
  2351. }
  2352. if (type == "label") {
  2353. parseLabel(data,description);
  2354. return;
  2355. }
  2356. if (type == "vertlabel") {
  2357. parseVertLabel(data,description);
  2358. return;
  2359. }
  2360. if (type == "item_image_button") {
  2361. parseItemImageButton(data,description);
  2362. return;
  2363. }
  2364. if ((type == "image_button") || (type == "image_button_exit")) {
  2365. parseImageButton(data,description,type);
  2366. return;
  2367. }
  2368. if (type == "tabheader") {
  2369. parseTabHeader(data,description);
  2370. return;
  2371. }
  2372. if (type == "box") {
  2373. parseBox(data,description);
  2374. return;
  2375. }
  2376. if (type == "bgcolor") {
  2377. parseBackgroundColor(data,description);
  2378. return;
  2379. }
  2380. if (type == "listcolors") {
  2381. parseListColors(data,description);
  2382. return;
  2383. }
  2384. if (type == "tooltip") {
  2385. parseTooltip(data,description);
  2386. return;
  2387. }
  2388. if (type == "scrollbar") {
  2389. parseScrollBar(data, description);
  2390. return;
  2391. }
  2392. if (type == "real_coordinates") {
  2393. data->real_coordinates = is_yes(description);
  2394. return;
  2395. }
  2396. if (type == "style") {
  2397. parseStyle(data, description, false);
  2398. return;
  2399. }
  2400. if (type == "style_type") {
  2401. parseStyle(data, description, true);
  2402. return;
  2403. }
  2404. if (type == "scrollbaroptions") {
  2405. parseScrollBarOptions(data, description);
  2406. return;
  2407. }
  2408. if (type == "scroll_container") {
  2409. parseScrollContainer(data, description);
  2410. return;
  2411. }
  2412. if (type == "scroll_container_end") {
  2413. parseScrollContainerEnd(data);
  2414. return;
  2415. }
  2416. if (type == "set_focus") {
  2417. parseSetFocus(description);
  2418. return;
  2419. }
  2420. if (type == "model") {
  2421. parseModel(data, description);
  2422. return;
  2423. }
  2424. // Ignore others
  2425. infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
  2426. << std::endl;
  2427. }
  2428. void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
  2429. {
  2430. // Useless to regenerate without a screensize
  2431. if ((screensize.X <= 0) || (screensize.Y <= 0)) {
  2432. return;
  2433. }
  2434. parserData mydata;
  2435. // Preserve stuff only on same form, not on a new form.
  2436. if (m_text_dst->m_formname == m_last_formname) {
  2437. // Preserve tables/textlists
  2438. for (auto &m_table : m_tables) {
  2439. std::string tablename = m_table.first.fname;
  2440. GUITable *table = m_table.second;
  2441. mydata.table_dyndata[tablename] = table->getDynamicData();
  2442. }
  2443. // Preserve focus
  2444. gui::IGUIElement *focused_element = Environment->getFocus();
  2445. if (focused_element && focused_element->getParent() == this) {
  2446. s32 focused_id = focused_element->getID();
  2447. if (focused_id > 257) {
  2448. for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
  2449. if (field.fid == focused_id) {
  2450. m_focused_element = field.fname;
  2451. break;
  2452. }
  2453. }
  2454. }
  2455. }
  2456. } else {
  2457. // Don't keep old focus value
  2458. m_focused_element = std::nullopt;
  2459. }
  2460. removeAll();
  2461. mydata.size = v2s32(100, 100);
  2462. mydata.screensize = screensize;
  2463. mydata.offset = v2f32(0.5f, 0.5f);
  2464. mydata.anchor = v2f32(0.5f, 0.5f);
  2465. mydata.padding = v2f32(0.05f, 0.05f);
  2466. mydata.simple_field_count = 0;
  2467. // Base position of contents of form
  2468. mydata.basepos = getBasePos();
  2469. // the parent for the parsed elements
  2470. mydata.current_parent = this;
  2471. m_inventorylists.clear();
  2472. m_tables.clear();
  2473. m_checkboxes.clear();
  2474. m_scrollbars.clear();
  2475. m_fields.clear();
  2476. m_tooltips.clear();
  2477. m_tooltip_rects.clear();
  2478. m_inventory_rings.clear();
  2479. m_dropdowns.clear();
  2480. m_scroll_containers.clear();
  2481. theme_by_name.clear();
  2482. theme_by_type.clear();
  2483. m_clickthrough_elements.clear();
  2484. field_enter_after_edit.clear();
  2485. field_close_on_enter.clear();
  2486. m_dropdown_index_event.clear();
  2487. m_bgnonfullscreen = true;
  2488. m_bgfullscreen = false;
  2489. m_formspec_version = 1;
  2490. m_bgcolor = video::SColor(140, 0, 0, 0);
  2491. m_tabheader_upper_edge = 0;
  2492. {
  2493. v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
  2494. m_fullscreen_bgcolor = video::SColor(
  2495. (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
  2496. clamp_u8(myround(formspec_bgcolor.X)),
  2497. clamp_u8(myround(formspec_bgcolor.Y)),
  2498. clamp_u8(myround(formspec_bgcolor.Z))
  2499. );
  2500. }
  2501. m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
  2502. m_default_tooltip_color = video::SColor(255,255,255,255);
  2503. // Add tooltip
  2504. {
  2505. assert(!m_tooltip_element);
  2506. // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
  2507. m_tooltip_element = gui::StaticText::add(Environment, L"",
  2508. core::rect<s32>(0, 0, 110, 18));
  2509. m_tooltip_element->enableOverrideColor(true);
  2510. m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
  2511. m_tooltip_element->setDrawBackground(true);
  2512. m_tooltip_element->setDrawBorder(true);
  2513. m_tooltip_element->setOverrideColor(m_default_tooltip_color);
  2514. m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
  2515. m_tooltip_element->setWordWrap(false);
  2516. //we're not parent so no autograb for this one!
  2517. m_tooltip_element->grab();
  2518. }
  2519. std::vector<std::string> elements = split(m_formspec_string,']');
  2520. unsigned int i = 0;
  2521. /* try to read version from first element only */
  2522. if (!elements.empty()) {
  2523. if (parseVersionDirect(elements[0])) {
  2524. i++;
  2525. }
  2526. }
  2527. /* we need size first in order to calculate image scale */
  2528. mydata.explicit_size = false;
  2529. for (; i< elements.size(); i++) {
  2530. if (!parseSizeDirect(&mydata, elements[i])) {
  2531. break;
  2532. }
  2533. }
  2534. /* "position" element is always after "size" element if it used */
  2535. for (; i< elements.size(); i++) {
  2536. if (!parsePositionDirect(&mydata, elements[i])) {
  2537. break;
  2538. }
  2539. }
  2540. /* "anchor" element is always after "position" (or "size" element) if it used */
  2541. for (; i< elements.size(); i++) {
  2542. if (!parseAnchorDirect(&mydata, elements[i])) {
  2543. break;
  2544. }
  2545. }
  2546. /* "padding" element is always after "anchor" and previous if it is used */
  2547. for (; i < elements.size(); i++) {
  2548. if (!parsePaddingDirect(&mydata, elements[i])) {
  2549. break;
  2550. }
  2551. }
  2552. /* "no_prepend" element is always after "padding" and previous if it used */
  2553. bool enable_prepends = true;
  2554. for (; i < elements.size(); i++) {
  2555. if (elements[i].empty())
  2556. break;
  2557. std::vector<std::string> parts = split(elements[i], '[');
  2558. if (trim(parts[0]) == "no_prepend")
  2559. enable_prepends = false;
  2560. else
  2561. break;
  2562. }
  2563. /* Copy of the "real_coordinates" element for after the form size. */
  2564. mydata.real_coordinates = m_formspec_version >= 2;
  2565. for (; i < elements.size(); i++) {
  2566. std::vector<std::string> parts = split(elements[i], '[');
  2567. auto name = trim(parts[0]);
  2568. if (name != "real_coordinates" || parts.size() != 2)
  2569. break; // Invalid format
  2570. mydata.real_coordinates = is_yes(trim(parts[1]));
  2571. }
  2572. if (mydata.explicit_size) {
  2573. // compute scaling for specified form size
  2574. if (m_lock) {
  2575. v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
  2576. v2u32 delta = current_screensize - m_lockscreensize;
  2577. if (current_screensize.Y > m_lockscreensize.Y)
  2578. delta.Y /= 2;
  2579. else
  2580. delta.Y = 0;
  2581. if (current_screensize.X > m_lockscreensize.X)
  2582. delta.X /= 2;
  2583. else
  2584. delta.X = 0;
  2585. offset = v2s32(delta.X,delta.Y);
  2586. mydata.screensize = m_lockscreensize;
  2587. } else {
  2588. offset = v2s32(0,0);
  2589. }
  2590. const double gui_scaling = g_settings->getFloat("gui_scaling", 0.5f, 42.0f);
  2591. const double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
  2592. double use_imgsize;
  2593. if (m_lock) {
  2594. // In fixed-size mode, inventory image size
  2595. // is 0.53 inch multiplied by the gui_scaling
  2596. // config parameter. This magic size is chosen
  2597. // to make the main menu (15.5 inventory images
  2598. // wide, including border) just fit into the
  2599. // default window (800 pixels wide) at 96 DPI
  2600. // and default scaling (1.00).
  2601. use_imgsize = 0.5555 * screen_dpi * gui_scaling;
  2602. } else {
  2603. // Variables for the maximum imgsize that can fit in the screen.
  2604. double fitx_imgsize;
  2605. double fity_imgsize;
  2606. v2f padded_screensize(
  2607. mydata.screensize.X * (1.0f - mydata.padding.X * 2.0f),
  2608. mydata.screensize.Y * (1.0f - mydata.padding.Y * 2.0f)
  2609. );
  2610. if (mydata.real_coordinates) {
  2611. fitx_imgsize = padded_screensize.X / mydata.invsize.X;
  2612. fity_imgsize = padded_screensize.Y / mydata.invsize.Y;
  2613. } else {
  2614. // The maximum imgsize in the old coordinate system also needs to
  2615. // factor in padding and spacing along with 0.1 inventory slot spare
  2616. // and help text space, hence the magic numbers.
  2617. fitx_imgsize = padded_screensize.X /
  2618. ((5.0 / 4.0) * (0.5 + mydata.invsize.X));
  2619. fity_imgsize = padded_screensize.Y /
  2620. ((15.0 / 13.0) * (0.85 + mydata.invsize.Y));
  2621. }
  2622. s32 min_screen_dim = std::min(padded_screensize.X, padded_screensize.Y);
  2623. double prefer_imgsize;
  2624. if (g_settings->getBool("enable_touch")) {
  2625. // The preferred imgsize should be larger to accommodate the
  2626. // smaller screensize.
  2627. prefer_imgsize = min_screen_dim / 10 * gui_scaling;
  2628. } else {
  2629. // Desktop computers have more space, so try to fit 15 coordinates.
  2630. prefer_imgsize = min_screen_dim / 15 * gui_scaling;
  2631. }
  2632. // Try to use the preferred imgsize, but if that's bigger than the maximum
  2633. // size, use the maximum size.
  2634. use_imgsize = std::min(prefer_imgsize,
  2635. std::min(fitx_imgsize, fity_imgsize));
  2636. }
  2637. // Everything else is scaled in proportion to the
  2638. // inventory image size. The inventory slot spacing
  2639. // is 5/4 image size horizontally and 15/13 image size
  2640. // vertically. The padding around the form (incorporating
  2641. // the border of the outer inventory slots) is 3/8
  2642. // image size. Font height (baseline to baseline)
  2643. // is 2/5 vertical inventory slot spacing, and button
  2644. // half-height is 7/8 of font height.
  2645. imgsize = v2s32(use_imgsize, use_imgsize);
  2646. spacing = v2f32(use_imgsize*5.0/4, use_imgsize*15.0/13);
  2647. padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
  2648. m_btn_height = use_imgsize*15.0/13 * 0.35;
  2649. m_font = g_fontengine->getFont();
  2650. if (mydata.real_coordinates) {
  2651. mydata.size = v2s32(
  2652. mydata.invsize.X*imgsize.X,
  2653. mydata.invsize.Y*imgsize.Y
  2654. );
  2655. } else {
  2656. mydata.size = v2s32(
  2657. padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
  2658. padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
  2659. );
  2660. }
  2661. DesiredRect = mydata.rect = core::rect<s32>(
  2662. (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
  2663. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
  2664. (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
  2665. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
  2666. );
  2667. } else {
  2668. // Non-size[] form must consist only of text fields and
  2669. // implicit "Proceed" button. Use default font, and
  2670. // temporary form size which will be recalculated below.
  2671. m_font = g_fontengine->getFont();
  2672. m_btn_height = font_line_height(m_font) * 0.875;
  2673. DesiredRect = core::rect<s32>(
  2674. (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
  2675. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
  2676. (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
  2677. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
  2678. );
  2679. }
  2680. recalculateAbsolutePosition(false);
  2681. mydata.basepos = getBasePos();
  2682. m_tooltip_element->setOverrideFont(m_font);
  2683. gui::IGUISkin *skin = Environment->getSkin();
  2684. sanity_check(skin);
  2685. gui::IGUIFont *old_font = skin->getFont();
  2686. skin->setFont(m_font);
  2687. // Add a new element that will hold all the background elements as its children.
  2688. // Because it is the first added element, all backgrounds will be behind all
  2689. // the other elements.
  2690. // (We use an arbitrarily big rect. The actual size is determined later by
  2691. // clipping to `this`.)
  2692. core::rect<s32> background_parent_rect(0, 0, 100000, 100000);
  2693. mydata.background_parent.reset(new gui::IGUIElement(EGUIET_ELEMENT, Environment,
  2694. this, -1, background_parent_rect));
  2695. pos_offset = v2f32();
  2696. // used for formspec versions < 3
  2697. std::list<IGUIElement *>::iterator legacy_sort_start = std::prev(Children.end()); // last element
  2698. if (enable_prepends) {
  2699. // Backup the coordinates so that prepends can use the coordinates of choice.
  2700. bool rc_backup = mydata.real_coordinates;
  2701. u16 version_backup = m_formspec_version;
  2702. mydata.real_coordinates = false; // Old coordinates by default.
  2703. std::vector<std::string> prepend_elements = split(m_formspec_prepend, ']');
  2704. for (const auto &element : prepend_elements)
  2705. parseElement(&mydata, element);
  2706. // legacy sorting for formspec versions < 3
  2707. if (m_formspec_version >= 3)
  2708. // prepends do not need to be reordered
  2709. legacy_sort_start = std::prev(Children.end()); // last element
  2710. else if (version_backup >= 3)
  2711. // only prepends elements have to be reordered
  2712. legacySortElements(legacy_sort_start);
  2713. m_formspec_version = version_backup;
  2714. mydata.real_coordinates = rc_backup; // Restore coordinates
  2715. }
  2716. for (; i< elements.size(); i++) {
  2717. parseElement(&mydata, elements[i]);
  2718. }
  2719. if (mydata.current_parent != this) {
  2720. errorstream << "Invalid formspec string: scroll_container was never closed!"
  2721. << std::endl;
  2722. } else if (!container_stack.empty()) {
  2723. errorstream << "Invalid formspec string: container was never closed!"
  2724. << std::endl;
  2725. }
  2726. // get the scrollbar elements for scroll_containers
  2727. for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers) {
  2728. for (const std::pair<FieldSpec, GUIScrollBar *> &b : m_scrollbars) {
  2729. if (c.first == b.first.fname) {
  2730. c.second->setScrollBar(b.second);
  2731. break;
  2732. }
  2733. }
  2734. }
  2735. // If there are fields without explicit size[], add a "Proceed"
  2736. // button and adjust size to fit all the fields.
  2737. if (mydata.simple_field_count > 0 && !mydata.explicit_size) {
  2738. mydata.rect = core::rect<s32>(
  2739. mydata.screensize.X / 2 - 580 / 2,
  2740. mydata.screensize.Y / 2 - 300 / 2,
  2741. mydata.screensize.X / 2 + 580 / 2,
  2742. mydata.screensize.Y / 2 + 240 / 2 + mydata.simple_field_count * 60
  2743. );
  2744. DesiredRect = mydata.rect;
  2745. recalculateAbsolutePosition(false);
  2746. mydata.basepos = getBasePos();
  2747. {
  2748. v2s32 pos = mydata.basepos;
  2749. pos.Y = (mydata.simple_field_count + 2) * 60;
  2750. v2s32 size = DesiredRect.getSize();
  2751. mydata.rect = core::rect<s32>(
  2752. size.X / 2 - 70, pos.Y,
  2753. size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2
  2754. );
  2755. GUIButton::addButton(Environment, mydata.rect, m_tsrc, this, 257,
  2756. wstrgettext("Proceed").c_str());
  2757. }
  2758. }
  2759. // Set initial focus if parser didn't set it
  2760. gui::IGUIElement *focused_element = Environment->getFocus();
  2761. if (!focused_element
  2762. || !isMyChild(focused_element)
  2763. || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
  2764. setInitialFocus();
  2765. skin->setFont(old_font);
  2766. // legacy sorting
  2767. if (m_formspec_version < 3)
  2768. legacySortElements(legacy_sort_start);
  2769. // Formname and regeneration setting
  2770. if (!m_is_form_regenerated) {
  2771. // Only set previous form name if we purposefully showed a new formspec
  2772. m_last_formname = m_text_dst->m_formname;
  2773. m_is_form_regenerated = true;
  2774. }
  2775. }
  2776. void GUIFormSpecMenu::legacySortElements(std::list<IGUIElement *>::iterator from)
  2777. {
  2778. /*
  2779. Draw order for formspec_version <= 2:
  2780. -3 bgcolor
  2781. -2 background
  2782. -1 box
  2783. 0 All other elements
  2784. 1 image
  2785. 2 item_image, item_image_button
  2786. 3 list
  2787. 4 label
  2788. */
  2789. if (from == Children.end())
  2790. from = Children.begin();
  2791. else
  2792. ++from;
  2793. std::list<IGUIElement *>::iterator to = Children.end();
  2794. // 1: Copy into a sortable container
  2795. std::vector<IGUIElement *> elements(from, to);
  2796. // 2: Sort the container
  2797. std::stable_sort(elements.begin(), elements.end(),
  2798. [this] (const IGUIElement *a, const IGUIElement *b) -> bool {
  2799. // TODO: getSpecByID is a linear search. It should made O(1), or cached here.
  2800. const FieldSpec *spec_a = getSpecByID(a->getID());
  2801. const FieldSpec *spec_b = getSpecByID(b->getID());
  2802. return spec_a && spec_b &&
  2803. spec_a->priority < spec_b->priority;
  2804. });
  2805. // 3: Re-assign the pointers
  2806. reorderChildren(from, to, elements);
  2807. }
  2808. #ifdef __ANDROID__
  2809. void GUIFormSpecMenu::getAndroidUIInput()
  2810. {
  2811. porting::AndroidDialogState dialogState = getAndroidUIInputState();
  2812. if (dialogState == porting::DIALOG_SHOWN) {
  2813. return;
  2814. } else if (dialogState == porting::DIALOG_CANCELED) {
  2815. m_jni_field_name.clear();
  2816. return;
  2817. }
  2818. porting::AndroidDialogType dialog_type = porting::getLastInputDialogType();
  2819. std::string fieldname = m_jni_field_name;
  2820. m_jni_field_name.clear();
  2821. for (const FieldSpec &field : m_fields) {
  2822. if (field.fname != fieldname)
  2823. continue; // Iterate until found
  2824. IGUIElement *element = getElementFromId(field.fid, true);
  2825. if (!element)
  2826. return;
  2827. auto element_type = element->getType();
  2828. if (dialog_type == porting::TEXT_INPUT && element_type == irr::gui::EGUIET_EDIT_BOX) {
  2829. gui::IGUIEditBox *editbox = (gui::IGUIEditBox *)element;
  2830. std::string text = porting::getInputDialogMessage();
  2831. editbox->setText(utf8_to_wide(text).c_str());
  2832. bool enter_after_edit = false;
  2833. auto iter = field_enter_after_edit.find(fieldname);
  2834. if (iter != field_enter_after_edit.end()) {
  2835. enter_after_edit = iter->second;
  2836. }
  2837. if (enter_after_edit && editbox->getParent()) {
  2838. SEvent enter;
  2839. enter.EventType = EET_GUI_EVENT;
  2840. enter.GUIEvent.Caller = editbox;
  2841. enter.GUIEvent.Element = nullptr;
  2842. enter.GUIEvent.EventType = gui::EGET_EDITBOX_ENTER;
  2843. editbox->getParent()->OnEvent(enter);
  2844. }
  2845. } else if (dialog_type == porting::SELECTION_INPUT &&
  2846. element_type == irr::gui::EGUIET_COMBO_BOX) {
  2847. auto dropdown = (gui::IGUIComboBox *) element;
  2848. int selected = porting::getInputDialogSelection();
  2849. dropdown->setAndSendSelected(selected);
  2850. }
  2851. return; // Early-return after found
  2852. }
  2853. }
  2854. #endif
  2855. GUIInventoryList::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
  2856. {
  2857. for (const GUIInventoryList *e : m_inventorylists) {
  2858. s32 item_index = e->getItemIndexAtPos(p);
  2859. if (item_index != -1)
  2860. return GUIInventoryList::ItemSpec(e->getInventoryloc(), e->getListname(),
  2861. item_index, e->getSlotSize());
  2862. }
  2863. return GUIInventoryList::ItemSpec(InventoryLocation(), "", -1, {0,0});
  2864. }
  2865. void GUIFormSpecMenu::drawSelectedItem()
  2866. {
  2867. video::IVideoDriver* driver = Environment->getVideoDriver();
  2868. if (!m_selected_item) {
  2869. // reset rotation time
  2870. drawItemStack(driver, m_font, ItemStack(),
  2871. core::rect<s32>(v2s32(0, 0), v2s32(0, 0)), NULL,
  2872. m_client, IT_ROT_DRAGGED);
  2873. return;
  2874. }
  2875. Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
  2876. sanity_check(inv);
  2877. InventoryList *list = inv->getList(m_selected_item->listname);
  2878. sanity_check(list);
  2879. ItemStack stack = list->getItem(m_selected_item->i);
  2880. stack.count = m_selected_amount;
  2881. v2s32 slotsize = m_selected_item->slotsize;
  2882. core::rect<s32> imgrect(0, 0, slotsize.X, slotsize.Y);
  2883. core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
  2884. rect.constrainTo(driver->getViewPort());
  2885. drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
  2886. }
  2887. void GUIFormSpecMenu::drawMenu()
  2888. {
  2889. if (m_form_src) {
  2890. const std::string &newform = m_form_src->getForm();
  2891. if (newform != m_formspec_string) {
  2892. m_formspec_string = newform;
  2893. m_is_form_regenerated = false;
  2894. regenerateGui(m_screensize_old);
  2895. }
  2896. }
  2897. gui::IGUISkin* skin = Environment->getSkin();
  2898. sanity_check(skin != NULL);
  2899. gui::IGUIFont *old_font = skin->getFont();
  2900. skin->setFont(m_font);
  2901. m_hovered_item_tooltips.clear();
  2902. updateSelectedItem();
  2903. video::IVideoDriver* driver = Environment->getVideoDriver();
  2904. /*
  2905. Draw background color
  2906. */
  2907. v2u32 screenSize = driver->getScreenSize();
  2908. core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
  2909. if (m_bgfullscreen)
  2910. driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
  2911. if (m_bgnonfullscreen)
  2912. driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
  2913. /*
  2914. Draw rect_mode tooltip
  2915. */
  2916. m_tooltip_element->setVisible(false);
  2917. for (const auto &pair : m_tooltip_rects) {
  2918. const core::rect<s32> &rect = pair.first->getAbsoluteClippingRect();
  2919. if (rect.getArea() > 0 && rect.isPointInside(m_pointer)) {
  2920. const std::wstring &text = pair.second.tooltip;
  2921. if (!text.empty()) {
  2922. showTooltip(text, pair.second.color, pair.second.bgcolor);
  2923. break;
  2924. }
  2925. }
  2926. }
  2927. // Some elements are only visible while being drawn
  2928. for (gui::IGUIElement *e : m_clickthrough_elements)
  2929. e->setVisible(true);
  2930. /*
  2931. This is where all the drawing happens.
  2932. */
  2933. for (auto child : Children)
  2934. if (child->isNotClipped() ||
  2935. AbsoluteClippingRect.isRectCollided(
  2936. child->getAbsolutePosition()))
  2937. child->draw();
  2938. for (gui::IGUIElement *e : m_clickthrough_elements)
  2939. e->setVisible(false);
  2940. // Draw hovered item tooltips
  2941. for (const std::string &tooltip : m_hovered_item_tooltips) {
  2942. showTooltip(utf8_to_wide(tooltip), m_default_tooltip_color,
  2943. m_default_tooltip_bgcolor);
  2944. }
  2945. if (m_hovered_item_tooltips.empty()) {
  2946. // reset rotation time
  2947. drawItemStack(driver, m_font, ItemStack(),
  2948. core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
  2949. NULL, m_client, IT_ROT_HOVERED);
  2950. }
  2951. /*
  2952. Draw fields/buttons tooltips and update the mouse cursor
  2953. */
  2954. gui::IGUIElement *hovered =
  2955. Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
  2956. gui::ICursorControl *cursor_control = RenderingEngine::get_raw_device()->
  2957. getCursorControl();
  2958. gui::ECURSOR_ICON current_cursor_icon = gui::ECI_NORMAL;
  2959. if (cursor_control)
  2960. current_cursor_icon = cursor_control->getActiveIcon();
  2961. bool hovered_element_found = false;
  2962. if (hovered) {
  2963. if (m_show_debug) {
  2964. core::rect<s32> rect = hovered->getAbsoluteClippingRect();
  2965. driver->draw2DRectangle(0x22FFFF00, rect, &rect);
  2966. }
  2967. // find the formspec-element of the hovered IGUIElement (a parent)
  2968. s32 id;
  2969. for (gui::IGUIElement *hovered_fselem = hovered; hovered_fselem;
  2970. hovered_fselem = hovered_fselem->getParent()) {
  2971. id = hovered_fselem->getID();
  2972. if (id != -1)
  2973. break;
  2974. }
  2975. u64 delta = 0;
  2976. if (id == -1) {
  2977. m_old_tooltip_id = id;
  2978. } else {
  2979. if (id == m_old_tooltip_id) {
  2980. delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
  2981. } else {
  2982. m_hovered_time = porting::getTimeMs();
  2983. m_old_tooltip_id = id;
  2984. }
  2985. }
  2986. // Find and update the current tooltip and cursor icon
  2987. if (id != -1) {
  2988. for (const FieldSpec &field : m_fields) {
  2989. if (field.fid != id)
  2990. continue;
  2991. if (delta >= m_tooltip_show_delay) {
  2992. const std::wstring &text = m_tooltips[field.fname].tooltip;
  2993. if (!text.empty())
  2994. showTooltip(text, m_tooltips[field.fname].color,
  2995. m_tooltips[field.fname].bgcolor);
  2996. }
  2997. if (cursor_control &&
  2998. field.ftype != f_HyperText && // Handled directly in guiHyperText
  2999. current_cursor_icon != field.fcursor_icon)
  3000. cursor_control->setActiveIcon(field.fcursor_icon);
  3001. hovered_element_found = true;
  3002. break;
  3003. }
  3004. }
  3005. }
  3006. if (!hovered_element_found) {
  3007. // no element is hovered
  3008. if (cursor_control && current_cursor_icon != ECI_NORMAL)
  3009. cursor_control->setActiveIcon(ECI_NORMAL);
  3010. }
  3011. m_tooltip_element->draw();
  3012. /*
  3013. Draw dragged item stack
  3014. */
  3015. drawSelectedItem();
  3016. skin->setFont(old_font);
  3017. }
  3018. void GUIFormSpecMenu::showTooltip(const std::wstring &text,
  3019. const irr::video::SColor &color, const irr::video::SColor &bgcolor)
  3020. {
  3021. EnrichedString ntext(text);
  3022. ntext.setDefaultColor(color);
  3023. if (!ntext.hasBackground())
  3024. ntext.setBackground(bgcolor);
  3025. setStaticText(m_tooltip_element, ntext);
  3026. // Tooltip size and offset
  3027. s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
  3028. s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
  3029. v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
  3030. int tooltip_offset_x = m_btn_height;
  3031. int tooltip_offset_y = m_btn_height;
  3032. if (m_pointer_type == PointerType::Touch) {
  3033. tooltip_offset_x *= 3;
  3034. tooltip_offset_y = 0;
  3035. if (m_pointer.X > (s32)screenSize.X / 2)
  3036. tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
  3037. }
  3038. // Calculate and set the tooltip position
  3039. s32 tooltip_x = m_pointer.X + tooltip_offset_x;
  3040. s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
  3041. if (tooltip_x + tooltip_width > (s32)screenSize.X)
  3042. tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
  3043. if (tooltip_y + tooltip_height > (s32)screenSize.Y)
  3044. tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
  3045. m_tooltip_element->setRelativePosition(
  3046. core::rect<s32>(
  3047. core::position2d<s32>(tooltip_x, tooltip_y),
  3048. core::dimension2d<s32>(tooltip_width, tooltip_height)
  3049. )
  3050. );
  3051. // Display the tooltip
  3052. m_tooltip_element->setVisible(true);
  3053. bringToFront(m_tooltip_element);
  3054. }
  3055. void GUIFormSpecMenu::updateSelectedItem()
  3056. {
  3057. // Don't update when dragging an item
  3058. if (m_selected_item && (m_selected_dragging || m_left_dragging))
  3059. return;
  3060. verifySelectedItem();
  3061. // If craftresult is not empty and nothing else is selected,
  3062. // try to move it somewhere or select it now
  3063. if (!m_selected_item || m_shift_move_after_craft) {
  3064. for (const GUIInventoryList *e : m_inventorylists) {
  3065. if (e->getListname() != "craftpreview")
  3066. continue;
  3067. Inventory *inv = m_invmgr->getInventory(e->getInventoryloc());
  3068. if (!inv)
  3069. continue;
  3070. InventoryList *list = inv->getList("craftresult");
  3071. if (!list || list->getSize() == 0)
  3072. continue;
  3073. const ItemStack &item = list->getItem(0);
  3074. if (item.empty())
  3075. continue;
  3076. GUIInventoryList::ItemSpec s = GUIInventoryList::ItemSpec();
  3077. s.inventoryloc = e->getInventoryloc();
  3078. s.listname = "craftresult";
  3079. s.i = 0;
  3080. s.slotsize = e->getSlotSize();
  3081. if (m_shift_move_after_craft) {
  3082. // Try to shift-move the crafted item to the next list in the ring after the "craft" list.
  3083. // We don't look for the "craftresult" list because it's a hidden list,
  3084. // and shouldn't be part of the formspec, thus it won't be in the list ring.
  3085. do {
  3086. s16 r = getNextInventoryRing(s.inventoryloc, "craft");
  3087. if (r < 0) // Not found
  3088. break;
  3089. const ListRingSpec &to_ring = m_inventory_rings[r];
  3090. Inventory *inv_to = m_invmgr->getInventory(to_ring.inventoryloc);
  3091. if (!inv_to)
  3092. break;
  3093. InventoryList *list_to = inv_to->getList(to_ring.listname);
  3094. if (!list_to)
  3095. break;
  3096. IMoveAction *a = new IMoveAction();
  3097. a->count = item.count;
  3098. a->from_inv = s.inventoryloc;
  3099. a->from_list = s.listname;
  3100. a->from_i = s.i;
  3101. a->to_inv = to_ring.inventoryloc;
  3102. a->to_list = to_ring.listname;
  3103. a->move_somewhere = true;
  3104. m_invmgr->inventoryAction(a);
  3105. } while (0);
  3106. m_shift_move_after_craft = false;
  3107. } else {
  3108. // Grab selected item from the crafting result list
  3109. m_selected_item = new GUIInventoryList::ItemSpec(s);
  3110. m_selected_amount = item.count;
  3111. m_selected_dragging = false;
  3112. }
  3113. break;
  3114. }
  3115. }
  3116. // If craftresult is selected, keep the whole stack selected
  3117. if (m_selected_item && m_selected_item->listname == "craftresult")
  3118. m_selected_amount = verifySelectedItem().count;
  3119. }
  3120. ItemStack GUIFormSpecMenu::verifySelectedItem()
  3121. {
  3122. // If the selected stack has become empty for some reason, deselect it.
  3123. // If the selected stack has become inaccessible, deselect it.
  3124. // If the selected stack has become smaller, adjust m_selected_amount.
  3125. // Return the selected stack.
  3126. if (m_selected_item) {
  3127. if (m_selected_item->isValid()) {
  3128. Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
  3129. if (inv) {
  3130. InventoryList *list = inv->getList(m_selected_item->listname);
  3131. if (list && (u32) m_selected_item->i < list->getSize()) {
  3132. ItemStack stack = list->getItem(m_selected_item->i);
  3133. if (!m_selected_swap.empty()) {
  3134. if (m_selected_swap.name == stack.name &&
  3135. m_selected_swap.count == stack.count)
  3136. m_selected_swap.clear();
  3137. } else {
  3138. m_selected_amount = std::min(m_selected_amount, stack.count);
  3139. }
  3140. if (!stack.empty())
  3141. return stack;
  3142. }
  3143. }
  3144. }
  3145. // selection was not valid
  3146. delete m_selected_item;
  3147. m_selected_item = nullptr;
  3148. m_selected_amount = 0;
  3149. m_selected_dragging = false;
  3150. }
  3151. return ItemStack();
  3152. }
  3153. s16 GUIFormSpecMenu::getNextInventoryRing(
  3154. const InventoryLocation &inventoryloc, const std::string &listname)
  3155. {
  3156. u16 rings = m_inventory_rings.size();
  3157. if (rings < 2)
  3158. return -1;
  3159. // Look for the source ring
  3160. s16 index = -1;
  3161. for (u16 i = 0; i < rings; i++) {
  3162. ListRingSpec &lr = m_inventory_rings[i];
  3163. if (lr.inventoryloc == inventoryloc && lr.listname == listname) {
  3164. // Set the index to the next ring
  3165. index = (i + 1) % rings;
  3166. break;
  3167. }
  3168. }
  3169. return index;
  3170. }
  3171. void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
  3172. {
  3173. if(m_text_dst)
  3174. {
  3175. StringMap fields;
  3176. if (quitmode == quit_mode_accept) {
  3177. fields["quit"] = "true";
  3178. }
  3179. if (quitmode == quit_mode_cancel) {
  3180. fields["quit"] = "true";
  3181. m_text_dst->gotText(fields);
  3182. return;
  3183. }
  3184. if (current_keys_pending.key_down) {
  3185. fields["key_down"] = "true";
  3186. current_keys_pending.key_down = false;
  3187. }
  3188. if (current_keys_pending.key_up) {
  3189. fields["key_up"] = "true";
  3190. current_keys_pending.key_up = false;
  3191. }
  3192. if (current_keys_pending.key_enter) {
  3193. fields["key_enter"] = "true";
  3194. current_keys_pending.key_enter = false;
  3195. }
  3196. if (!current_field_enter_pending.empty()) {
  3197. fields["key_enter_field"] = current_field_enter_pending;
  3198. current_field_enter_pending.clear();
  3199. }
  3200. if (current_keys_pending.key_escape) {
  3201. fields["key_escape"] = "true";
  3202. current_keys_pending.key_escape = false;
  3203. }
  3204. for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
  3205. if (s.send) {
  3206. std::string name = s.fname;
  3207. if (s.ftype == f_Button) {
  3208. fields[name] = wide_to_utf8(s.flabel);
  3209. } else if (s.ftype == f_Table) {
  3210. GUITable *table = getTable(s.fname);
  3211. if (table) {
  3212. fields[name] = table->checkEvent();
  3213. }
  3214. } else if (s.ftype == f_DropDown) {
  3215. // No dynamic cast possible due to some distributions shipped
  3216. // without rtti support in Irrlicht
  3217. IGUIElement *element = getElementFromId(s.fid, true);
  3218. gui::IGUIComboBox *e = NULL;
  3219. if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
  3220. e = static_cast<gui::IGUIComboBox *>(element);
  3221. } else {
  3222. warningstream << "GUIFormSpecMenu::acceptInput: dropdown "
  3223. << "field without dropdown element" << std::endl;
  3224. continue;
  3225. }
  3226. s32 selected = e->getSelected();
  3227. if (selected >= 0) {
  3228. if (m_dropdown_index_event.find(s.fname) !=
  3229. m_dropdown_index_event.end()) {
  3230. fields[name] = std::to_string(selected + 1);
  3231. } else {
  3232. std::vector<std::string> *dropdown_values =
  3233. getDropDownValues(s.fname);
  3234. if (dropdown_values && selected < (s32)dropdown_values->size())
  3235. fields[name] = (*dropdown_values)[selected];
  3236. }
  3237. }
  3238. } else if (s.ftype == f_TabHeader) {
  3239. // No dynamic cast possible due to some distributions shipped
  3240. // without rtti support in Irrlicht
  3241. IGUIElement *element = getElementFromId(s.fid, true);
  3242. gui::IGUITabControl *e = nullptr;
  3243. if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
  3244. e = static_cast<gui::IGUITabControl *>(element);
  3245. }
  3246. if (e != 0) {
  3247. fields[name] = itos(e->getActiveTab() + 1);
  3248. }
  3249. } else if (s.ftype == f_CheckBox) {
  3250. // No dynamic cast possible due to some distributions shipped
  3251. // without rtti support in Irrlicht
  3252. IGUIElement *element = getElementFromId(s.fid, true);
  3253. gui::IGUICheckBox *e = nullptr;
  3254. if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
  3255. e = static_cast<gui::IGUICheckBox*>(element);
  3256. }
  3257. if (e != 0) {
  3258. if (e->isChecked())
  3259. fields[name] = "true";
  3260. else
  3261. fields[name] = "false";
  3262. }
  3263. } else if (s.ftype == f_ScrollBar) {
  3264. // No dynamic cast possible due to some distributions shipped
  3265. // without rtti support in Irrlicht
  3266. IGUIElement *element = getElementFromId(s.fid, true);
  3267. GUIScrollBar *e = nullptr;
  3268. if (element && element->getType() == gui::EGUIET_ELEMENT)
  3269. e = static_cast<GUIScrollBar *>(element);
  3270. if (e) {
  3271. if (s.fdefault == L"Changed")
  3272. fields[name] = "CHG:" + itos(e->getPos());
  3273. else
  3274. fields[name] = "VAL:" + itos(e->getPos());
  3275. }
  3276. } else if (s.ftype == f_AnimatedImage) {
  3277. // No dynamic cast possible due to some distributions shipped
  3278. // without rtti support in Irrlicht
  3279. IGUIElement *element = getElementFromId(s.fid, true);
  3280. GUIAnimatedImage *e = nullptr;
  3281. if (element && element->getType() == gui::EGUIET_ELEMENT)
  3282. e = static_cast<GUIAnimatedImage *>(element);
  3283. if (e)
  3284. fields[name] = std::to_string(e->getFrameIndex() + 1);
  3285. } else {
  3286. IGUIElement *e = getElementFromId(s.fid, true);
  3287. if (e)
  3288. fields[name] = wide_to_utf8(e->getText());
  3289. }
  3290. }
  3291. }
  3292. m_text_dst->gotText(fields);
  3293. }
  3294. }
  3295. bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
  3296. {
  3297. // This must be done first so that GUIModalMenu can set m_pointer_type
  3298. // correctly.
  3299. if (GUIModalMenu::preprocessEvent(event))
  3300. return true;
  3301. // The IGUITabControl renders visually using the skin's selected
  3302. // font, which we override for the duration of form drawing,
  3303. // but computes tab hotspots based on how it would have rendered
  3304. // using the font that is selected at the time of button release.
  3305. // To make these two consistent, temporarily override the skin's
  3306. // font while the IGUITabControl is processing the event.
  3307. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  3308. event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
  3309. s32 x = event.MouseInput.X;
  3310. s32 y = event.MouseInput.Y;
  3311. gui::IGUIElement *hovered =
  3312. Environment->getRootGUIElement()->getElementFromPoint(
  3313. core::position2d<s32>(x, y));
  3314. if (hovered && isMyChild(hovered) &&
  3315. hovered->getType() == gui::EGUIET_TAB_CONTROL) {
  3316. gui::IGUISkin* skin = Environment->getSkin();
  3317. sanity_check(skin != NULL);
  3318. gui::IGUIFont *old_font = skin->getFont();
  3319. skin->setFont(m_font);
  3320. bool retval = hovered->OnEvent(event);
  3321. skin->setFont(old_font);
  3322. return retval;
  3323. }
  3324. }
  3325. // Fix Esc/Return key being eaten by checkboxen and tables
  3326. if (event.EventType == EET_KEY_INPUT_EVENT) {
  3327. KeyPress kp(event.KeyInput);
  3328. if (kp == EscapeKey || kp == CancelKey
  3329. || kp == getKeySetting("keymap_inventory")
  3330. || event.KeyInput.Key==KEY_RETURN) {
  3331. gui::IGUIElement *focused = Environment->getFocus();
  3332. if (focused && isMyChild(focused) &&
  3333. (focused->getType() == gui::EGUIET_LIST_BOX ||
  3334. focused->getType() == gui::EGUIET_CHECK_BOX) &&
  3335. (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
  3336. event.KeyInput.Key != KEY_RETURN)) {
  3337. OnEvent(event);
  3338. return true;
  3339. }
  3340. }
  3341. }
  3342. // Mouse wheel and move events: send to hovered element instead of focused
  3343. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  3344. (event.MouseInput.Event == EMIE_MOUSE_WHEEL ||
  3345. (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
  3346. event.MouseInput.ButtonStates == 0))) {
  3347. s32 x = event.MouseInput.X;
  3348. s32 y = event.MouseInput.Y;
  3349. gui::IGUIElement *hovered =
  3350. Environment->getRootGUIElement()->getElementFromPoint(
  3351. core::position2d<s32>(x, y));
  3352. if (hovered && isMyChild(hovered)) {
  3353. hovered->OnEvent(event);
  3354. return event.MouseInput.Event == EMIE_MOUSE_WHEEL;
  3355. }
  3356. }
  3357. if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
  3358. /* TODO add a check like:
  3359. if (event.JoystickEvent != joystick_we_listen_for)
  3360. return false;
  3361. */
  3362. bool handled = m_joystick->handleEvent(event.JoystickEvent);
  3363. if (handled) {
  3364. if (m_joystick->wasKeyDown(KeyType::ESC)) {
  3365. tryClose();
  3366. } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
  3367. if (m_allowclose) {
  3368. acceptInput(quit_mode_accept);
  3369. quitMenu();
  3370. }
  3371. }
  3372. }
  3373. return handled;
  3374. }
  3375. return false;
  3376. }
  3377. void GUIFormSpecMenu::tryClose()
  3378. {
  3379. if (m_allowclose) {
  3380. doPause = false;
  3381. acceptInput(quit_mode_cancel);
  3382. quitMenu();
  3383. } else {
  3384. m_text_dst->gotText(L"MenuQuit");
  3385. }
  3386. }
  3387. bool GUIFormSpecMenu::OnEvent(const SEvent& event)
  3388. {
  3389. if (event.EventType==EET_KEY_INPUT_EVENT) {
  3390. KeyPress kp(event.KeyInput);
  3391. if (event.KeyInput.PressedDown && (
  3392. (kp == EscapeKey) || (kp == CancelKey) ||
  3393. ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
  3394. tryClose();
  3395. return true;
  3396. }
  3397. if (m_client != NULL && event.KeyInput.PressedDown &&
  3398. (kp == getKeySetting("keymap_screenshot"))) {
  3399. m_client->makeScreenshot();
  3400. }
  3401. if (event.KeyInput.PressedDown && kp == getKeySetting("keymap_toggle_debug"))
  3402. m_show_debug = !m_show_debug;
  3403. if (event.KeyInput.PressedDown &&
  3404. (event.KeyInput.Key==KEY_RETURN ||
  3405. event.KeyInput.Key==KEY_UP ||
  3406. event.KeyInput.Key==KEY_DOWN)
  3407. ) {
  3408. switch (event.KeyInput.Key) {
  3409. case KEY_RETURN:
  3410. current_keys_pending.key_enter = true;
  3411. break;
  3412. case KEY_UP:
  3413. current_keys_pending.key_up = true;
  3414. break;
  3415. case KEY_DOWN:
  3416. current_keys_pending.key_down = true;
  3417. break;
  3418. break;
  3419. default:
  3420. //can't happen at all!
  3421. FATAL_ERROR("Reached a source line that can't ever been reached");
  3422. break;
  3423. }
  3424. if (current_keys_pending.key_enter && m_allowclose) {
  3425. acceptInput(quit_mode_accept);
  3426. quitMenu();
  3427. } else {
  3428. acceptInput();
  3429. }
  3430. return true;
  3431. }
  3432. }
  3433. /* Mouse event other than movement, or crossing the border of inventory
  3434. field while holding left, right, or middle mouse button
  3435. or touch event (for touch screen devices)
  3436. */
  3437. if ((event.EventType == EET_MOUSE_INPUT_EVENT &&
  3438. (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
  3439. ((event.MouseInput.isLeftPressed() ||
  3440. event.MouseInput.isRightPressed() ||
  3441. event.MouseInput.isMiddlePressed()) &&
  3442. getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) ||
  3443. event.EventType == EET_TOUCH_INPUT_EVENT) {
  3444. // Get selected item and hovered/clicked item (s)
  3445. m_old_tooltip_id = -1;
  3446. updateSelectedItem();
  3447. GUIInventoryList::ItemSpec s = getItemAtPos(m_pointer);
  3448. Inventory *inv_selected = NULL;
  3449. InventoryList *list_selected = NULL;
  3450. Inventory *inv_s = NULL;
  3451. InventoryList *list_s = NULL;
  3452. if (m_selected_item) {
  3453. inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
  3454. sanity_check(inv_selected);
  3455. list_selected = inv_selected->getList(m_selected_item->listname);
  3456. sanity_check(list_selected);
  3457. }
  3458. u32 s_count = 0;
  3459. if (s.isValid())
  3460. do { // breakable
  3461. inv_s = m_invmgr->getInventory(s.inventoryloc);
  3462. if (!inv_s) {
  3463. errorstream << "InventoryMenu: The selected inventory location "
  3464. << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
  3465. << std::endl;
  3466. s.i = -1; // make it invalid again
  3467. break;
  3468. }
  3469. list_s = inv_s->getList(s.listname);
  3470. if (list_s == NULL) {
  3471. verbosestream << "InventoryMenu: The selected inventory list \""
  3472. << s.listname << "\" does not exist" << std::endl;
  3473. s.i = -1; // make it invalid again
  3474. break;
  3475. }
  3476. if ((u32)s.i >= list_s->getSize()) {
  3477. infostream << "InventoryMenu: The selected inventory list \""
  3478. << s.listname << "\" is too small (i=" << s.i << ", size="
  3479. << list_s->getSize() << ")" << std::endl;
  3480. s.i = -1; // make it invalid again
  3481. break;
  3482. }
  3483. s_count = list_s->getItem(s.i).count;
  3484. } while(0);
  3485. // True if the hovered slot is the selected slot
  3486. bool identical = m_selected_item && s.isValid() && (*m_selected_item == s);
  3487. // True if the hovered slot is empty
  3488. bool empty = s.isValid() && list_s->getItem(s.i).empty();
  3489. // True if the hovered item would stack with the selected item
  3490. bool matching = false;
  3491. if (m_selected_item && s.isValid()) {
  3492. ItemStack a = list_selected->getItem(m_selected_item->i);
  3493. ItemStack b = list_s->getItem(s.i);
  3494. matching = a.stacksWith(b);
  3495. }
  3496. ButtonEventType button = BET_OTHER;
  3497. ButtonEventType updown = BET_OTHER;
  3498. bool mouse_shift = false;
  3499. if (event.EventType == EET_MOUSE_INPUT_EVENT) {
  3500. mouse_shift = event.MouseInput.Shift;
  3501. switch (event.MouseInput.Event) {
  3502. case EMIE_LMOUSE_PRESSED_DOWN:
  3503. button = BET_LEFT; updown = BET_DOWN;
  3504. break;
  3505. case EMIE_RMOUSE_PRESSED_DOWN:
  3506. button = BET_RIGHT; updown = BET_DOWN;
  3507. break;
  3508. case EMIE_MMOUSE_PRESSED_DOWN:
  3509. button = BET_MIDDLE; updown = BET_DOWN;
  3510. break;
  3511. case EMIE_MOUSE_WHEEL:
  3512. button = (event.MouseInput.Wheel > 0) ?
  3513. BET_WHEEL_UP : BET_WHEEL_DOWN;
  3514. updown = BET_DOWN;
  3515. break;
  3516. case EMIE_LMOUSE_LEFT_UP:
  3517. button = BET_LEFT; updown = BET_UP;
  3518. break;
  3519. case EMIE_RMOUSE_LEFT_UP:
  3520. button = BET_RIGHT; updown = BET_UP;
  3521. break;
  3522. case EMIE_MMOUSE_LEFT_UP:
  3523. button = BET_MIDDLE; updown = BET_UP;
  3524. break;
  3525. case EMIE_MOUSE_MOVED:
  3526. updown = BET_MOVE;
  3527. break;
  3528. default:
  3529. break;
  3530. }
  3531. }
  3532. // The second touch (see GUIModalMenu::preprocessEvent() function)
  3533. ButtonEventType touch = BET_OTHER;
  3534. if (event.EventType == EET_TOUCH_INPUT_EVENT) {
  3535. if (event.TouchInput.Event == ETIE_LEFT_UP)
  3536. touch = BET_RIGHT;
  3537. }
  3538. // Set this number to a positive value to generate a move action
  3539. // from m_selected_item to s.
  3540. u32 move_amount = 0;
  3541. // Set this number to a positive value to generate a move action
  3542. // from s to the next inventory ring.
  3543. u32 shift_move_amount = 0;
  3544. // Set this number to a positive value to generate a move action
  3545. // from s to m_selected_item.
  3546. u32 pickup_amount = 0;
  3547. // Set this number to a positive value to generate a drop action
  3548. // from m_selected_item.
  3549. u32 drop_amount = 0;
  3550. // Set this number to a positive value to generate a craft action at s.
  3551. u32 craft_amount = 0;
  3552. switch (updown) {
  3553. case BET_DOWN: {
  3554. // Some mouse button has been pressed
  3555. if (m_held_mouse_button != BET_OTHER)
  3556. break;
  3557. if (button == BET_LEFT || button == BET_RIGHT || button == BET_MIDDLE)
  3558. m_held_mouse_button = button;
  3559. if (!s.isValid()) {
  3560. if (m_selected_item && !getAbsoluteClippingRect().isPointInside(m_pointer)) {
  3561. // Clicked outside of the window: drop
  3562. if (button == BET_RIGHT || button == BET_WHEEL_UP)
  3563. drop_amount = 1;
  3564. else if (button == BET_MIDDLE)
  3565. drop_amount = MYMIN(m_selected_amount, 10);
  3566. else if (button == BET_LEFT)
  3567. drop_amount = m_selected_amount;
  3568. }
  3569. break;
  3570. }
  3571. if (s.listname == "craftpreview") {
  3572. // Craft preview has been clicked: craft
  3573. if (button == BET_MIDDLE)
  3574. craft_amount = 10;
  3575. else if (mouse_shift && button == BET_LEFT)
  3576. craft_amount = list_s->getItem(s.i).getStackMax(m_client->idef());
  3577. else
  3578. craft_amount = 1;
  3579. // Holding shift moves the crafted item to the inventory
  3580. m_shift_move_after_craft = mouse_shift;
  3581. } else if (!m_selected_item && button != BET_WHEEL_UP && !empty) {
  3582. // Non-empty stack has been clicked: select or shift-move it
  3583. u32 count = 0;
  3584. if (button == BET_RIGHT)
  3585. count = (s_count + 1) / 2;
  3586. else if (button == BET_MIDDLE)
  3587. count = MYMIN(s_count, 10);
  3588. else if (button == BET_WHEEL_DOWN)
  3589. count = 1;
  3590. else if (button == BET_LEFT)
  3591. count = s_count;
  3592. if (mouse_shift) {
  3593. // Shift pressed: move item, right click moves 1
  3594. shift_move_amount = button == BET_RIGHT ? 1 : count;
  3595. } else {
  3596. // No shift: select item
  3597. m_selected_item = new GUIInventoryList::ItemSpec(s);
  3598. m_selected_amount = count;
  3599. m_selected_dragging = button != BET_WHEEL_DOWN;
  3600. }
  3601. } else if (m_selected_item) {
  3602. // Clicked a slot: move
  3603. if (button == BET_RIGHT || button == BET_WHEEL_UP)
  3604. move_amount = 1;
  3605. else if (button == BET_WHEEL_DOWN)
  3606. pickup_amount = MYMIN(s_count, 1);
  3607. else if (button == BET_MIDDLE)
  3608. move_amount = MYMIN(m_selected_amount, 10);
  3609. else if (button == BET_LEFT)
  3610. move_amount = m_selected_amount;
  3611. if (mouse_shift && !identical && matching) {
  3612. // Shift-move all items the same as the selected item to the next list
  3613. move_amount = 0;
  3614. // Try to find somewhere to move the items to
  3615. s16 r = getNextInventoryRing(s.inventoryloc, s.listname);
  3616. if (r < 0) // Not found
  3617. break;
  3618. const ListRingSpec &to_ring = m_inventory_rings[r];
  3619. Inventory *inv_to = m_invmgr->getInventory(to_ring.inventoryloc);
  3620. if (!inv_to)
  3621. break;
  3622. InventoryList *list_to = inv_to->getList(to_ring.listname);
  3623. if (!list_to)
  3624. break;
  3625. ItemStack slct = list_selected->getItem(m_selected_item->i);
  3626. for (s32 i = 0; i < (s32)list_s->getSize(); i++) {
  3627. // Skip the selected slot
  3628. if (i == m_selected_item->i)
  3629. continue;
  3630. ItemStack item = list_s->getItem(i);
  3631. if (slct.stacksWith(item)) {
  3632. IMoveAction *a = new IMoveAction();
  3633. a->count = item.count;
  3634. a->from_inv = s.inventoryloc;
  3635. a->from_list = s.listname;
  3636. a->from_i = i;
  3637. a->to_inv = to_ring.inventoryloc;
  3638. a->to_list = to_ring.listname;
  3639. a->move_somewhere = true;
  3640. m_invmgr->inventoryAction(a);
  3641. }
  3642. }
  3643. } else if (button == BET_LEFT && (empty || matching)) {
  3644. // We don't know if the user is left-dragging, just moving
  3645. // the item, or doing a pickup-all via doubleclick, so assume
  3646. // that they are left-dragging, and wait for the next event
  3647. // before moving the item, or doing a pickup-all
  3648. m_left_dragging = true;
  3649. m_client->inhibit_inventory_revert = true;
  3650. m_left_drag_stack = list_selected->getItem(m_selected_item->i);
  3651. m_left_drag_amount = m_selected_amount;
  3652. m_left_drag_stacks.emplace_back(s, list_s->getItem(s.i));
  3653. move_amount = 0;
  3654. } else if (identical) {
  3655. // Change the selected amount instead of moving
  3656. if (button == BET_WHEEL_DOWN) {
  3657. if (m_selected_amount < s_count)
  3658. ++m_selected_amount;
  3659. } else if (button == BET_WHEEL_UP) {
  3660. if (m_selected_amount > 0)
  3661. --m_selected_amount;
  3662. } else {
  3663. if (move_amount >= m_selected_amount)
  3664. m_selected_amount = 0;
  3665. else
  3666. m_selected_amount -= move_amount;
  3667. }
  3668. move_amount = 0;
  3669. pickup_amount = 0;
  3670. }
  3671. }
  3672. break;
  3673. }
  3674. case BET_UP: {
  3675. // Some mouse button has been released
  3676. if (m_held_mouse_button != BET_OTHER && m_held_mouse_button != button)
  3677. break;
  3678. m_held_mouse_button = BET_OTHER;
  3679. if (m_selected_dragging && m_selected_item) {
  3680. if (s.isValid() && !identical && (empty || matching)) {
  3681. // Dragged to different slot: move all selected
  3682. move_amount = m_selected_amount;
  3683. } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
  3684. // Dragged outside of window: drop all selected
  3685. drop_amount = m_selected_amount;
  3686. }
  3687. }
  3688. m_selected_dragging = false;
  3689. if (m_left_dragging && button == BET_LEFT) {
  3690. m_left_dragging = false;
  3691. m_client->inhibit_inventory_revert = false;
  3692. if (m_left_drag_stacks.size() > 1) {
  3693. // Finalize the left-dragging
  3694. for (auto &ds : m_left_drag_stacks) {
  3695. if (ds.first == *m_selected_item) {
  3696. // This entry is needed to properly calculate the stack sizes.
  3697. // The stack already exists, hence no further action needed here.
  3698. continue;
  3699. }
  3700. // Check how many items we should move to this slot,
  3701. // it may be less than the full split
  3702. Inventory *inv_to = m_invmgr->getInventory(ds.first.inventoryloc);
  3703. InventoryList *list_to = inv_to->getList(ds.first.listname);
  3704. ItemStack stack_to = list_to->getItem(ds.first.i);
  3705. u16 amount = stack_to.count - ds.second.count;
  3706. IMoveAction *a = new IMoveAction();
  3707. a->count = amount;
  3708. a->from_inv = m_selected_item->inventoryloc;
  3709. a->from_list = m_selected_item->listname;
  3710. a->from_i = m_selected_item->i;
  3711. a->to_inv = ds.first.inventoryloc;
  3712. a->to_list = ds.first.listname;
  3713. a->to_i = ds.first.i;
  3714. m_invmgr->inventoryAction(a);
  3715. }
  3716. } else if (identical) {
  3717. // Put the selected item back where it came from
  3718. m_selected_amount = 0;
  3719. } else if (s.isValid()) {
  3720. // Move the selected item
  3721. move_amount = m_selected_amount;
  3722. }
  3723. m_left_drag_stacks.clear();
  3724. }
  3725. break;
  3726. }
  3727. case BET_MOVE: {
  3728. // Mouse button is down and mouse pointer entered a new inventory field
  3729. if (!s.isValid() || s.listname == "craftpreview")
  3730. break;
  3731. if (!m_selected_item && mouse_shift) {
  3732. // Shift-move items while dragging
  3733. if (m_held_mouse_button == BET_RIGHT)
  3734. shift_move_amount = 1;
  3735. else if (m_held_mouse_button == BET_MIDDLE)
  3736. shift_move_amount = MYMIN(s_count, 10);
  3737. else if (m_held_mouse_button == BET_LEFT)
  3738. shift_move_amount = s_count;
  3739. } else if (m_selected_item) {
  3740. if (m_held_mouse_button != BET_LEFT) {
  3741. // Move items if the destination slot is empty
  3742. // or contains the same item type as what is going to be moved
  3743. if (!m_selected_dragging && (empty || matching)) {
  3744. if (m_held_mouse_button == BET_RIGHT)
  3745. move_amount = 1;
  3746. else if (m_held_mouse_button == BET_MIDDLE)
  3747. move_amount = MYMIN(m_selected_amount, 10);
  3748. }
  3749. } else if (m_left_dragging && (empty || matching) &&
  3750. m_left_drag_amount > m_left_drag_stacks.size()) {
  3751. // Add the slot to the left-drag list if it doesn't exist
  3752. bool found = false;
  3753. for (auto &ds : m_left_drag_stacks) {
  3754. if (s == ds.first) {
  3755. found = true;
  3756. break;
  3757. }
  3758. }
  3759. if (!found) {
  3760. m_left_drag_stacks.emplace_back(s, list_s->getItem(s.i));
  3761. }
  3762. } else if (m_selected_dragging && matching && !identical) {
  3763. // Pickup items of the same type while dragging
  3764. pickup_amount = s_count;
  3765. }
  3766. } else if (m_held_mouse_button == BET_LEFT) {
  3767. // Start picking up items
  3768. m_selected_item = new GUIInventoryList::ItemSpec(s);
  3769. m_selected_amount = s_count;
  3770. m_selected_dragging = true;
  3771. }
  3772. break;
  3773. }
  3774. case BET_OTHER: {
  3775. // Some other mouse event has occured
  3776. // Currently only left-double-click should trigger this
  3777. if (!s.isValid() || event.EventType != EET_MOUSE_INPUT_EVENT ||
  3778. event.MouseInput.Event != EMIE_LMOUSE_DOUBLE_CLICK)
  3779. break;
  3780. // Only do the pickup all thing when putting down an item.
  3781. // Doubleclick events are triggered after press-down events, so if
  3782. // m_left_dragging is true here, the user just put down an itemstack,
  3783. // but didn't yet release the button to make it happen.
  3784. if (!m_left_dragging)
  3785. break;
  3786. // Abort left-dragging
  3787. m_left_dragging = false;
  3788. m_client->inhibit_inventory_revert = false;
  3789. m_left_drag_stacks.clear();
  3790. // Both the selected item and the hovered item need to be checked
  3791. // because we don't know exactly when the double-click happened
  3792. ItemStack slct;
  3793. if (!m_selected_item && !empty)
  3794. slct = list_s->getItem(s.i);
  3795. else if (m_selected_item && (identical || empty))
  3796. slct = list_selected->getItem(m_selected_item->i);
  3797. // Pickup all of the item from the list
  3798. if (slct.count > 0) {
  3799. for (s32 i = 0; i < (s32)list_s->getSize(); i++) {
  3800. // Skip the selected slot
  3801. if (i == s.i)
  3802. continue;
  3803. ItemStack item = list_s->getItem(i);
  3804. if (slct.stacksWith(item)) {
  3805. // Found a match, check if we can pick it up
  3806. bool full = false;
  3807. u16 amount = item.count;
  3808. ItemStack leftover = slct.addItem(item, m_client->idef());
  3809. if (!leftover.empty()) {
  3810. amount -= leftover.count;
  3811. full = true;
  3812. }
  3813. if (amount > 0) {
  3814. IMoveAction *a = new IMoveAction();
  3815. a->count = amount;
  3816. a->from_inv = s.inventoryloc;
  3817. a->from_list = s.listname;
  3818. a->from_i = i;
  3819. a->to_inv = s.inventoryloc;
  3820. a->to_list = s.listname;
  3821. a->to_i = s.i;
  3822. m_invmgr->inventoryAction(a);
  3823. if (m_selected_item)
  3824. m_selected_amount += amount;
  3825. }
  3826. if (full) // Stack is full, stop
  3827. break;
  3828. }
  3829. }
  3830. }
  3831. break;
  3832. }
  3833. default:
  3834. break;
  3835. }
  3836. if (touch == BET_RIGHT && m_selected_item && !m_left_dragging) {
  3837. if (!s.isValid()) {
  3838. // Not a valid slot
  3839. if (!getAbsoluteClippingRect().isPointInside(m_pointer))
  3840. // Is outside the menu
  3841. drop_amount = 1;
  3842. } else {
  3843. // Over a valid slot
  3844. move_amount = 1;
  3845. if (identical) {
  3846. // Change the selected amount instead of moving
  3847. if (move_amount >= m_selected_amount)
  3848. m_selected_amount = 0;
  3849. else
  3850. m_selected_amount -= move_amount;
  3851. move_amount = 0;
  3852. }
  3853. }
  3854. }
  3855. // Update left-dragged slots
  3856. if (m_left_dragging && m_left_drag_stacks.size() > 1) {
  3857. // The split amount will always at least one, because the number
  3858. // of slots will never be greater than the selected amount
  3859. u16 split_amount = m_left_drag_amount / m_left_drag_stacks.size();
  3860. u16 split_remaining = m_left_drag_amount % m_left_drag_stacks.size();
  3861. ItemStack stack_from = m_left_drag_stack;
  3862. m_selected_amount = m_left_drag_amount;
  3863. for (auto &ds : m_left_drag_stacks) {
  3864. Inventory *inv_to = m_invmgr->getInventory(ds.first.inventoryloc);
  3865. InventoryList *list_to = inv_to->getList(ds.first.listname);
  3866. if (ds.first == *m_selected_item) {
  3867. // Adding to the source stack, just change the selected amount
  3868. m_selected_amount -= split_amount + split_remaining;
  3869. } else {
  3870. // Reset the stack to its original state
  3871. list_to->changeItem(ds.first.i, ds.second);
  3872. // Add the new split to the stack
  3873. ItemStack add_stack = stack_from;
  3874. add_stack.count = split_amount;
  3875. ItemStack leftover = list_to->addItem(ds.first.i, add_stack);
  3876. // Remove the split items from the source stack
  3877. u16 moved = split_amount - leftover.count;
  3878. m_selected_amount -= moved;
  3879. stack_from.count -= moved;
  3880. }
  3881. }
  3882. // Save the adjusted source stack
  3883. list_selected->changeItem(m_selected_item->i, stack_from);
  3884. }
  3885. // Possibly send inventory action to server
  3886. if (move_amount > 0) {
  3887. // Send IAction::Move
  3888. assert(m_selected_item && m_selected_item->isValid());
  3889. assert(s.isValid());
  3890. assert(list_selected && list_s);
  3891. ItemStack stack_from = list_selected->getItem(m_selected_item->i);
  3892. ItemStack stack_to = list_s->getItem(s.i);
  3893. // Check how many items can be moved
  3894. move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
  3895. ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
  3896. // If source stack cannot be added to destination stack at all,
  3897. // they are swapped
  3898. if (leftover.count == stack_from.count && leftover.name == stack_from.name) {
  3899. if (m_selected_swap.empty()) {
  3900. m_selected_amount = stack_to.count;
  3901. m_selected_dragging = false;
  3902. // WARNING: BLACK MAGIC, BUT IN A REDUCED SET
  3903. // Skip next validation checks due async inventory calls
  3904. m_selected_swap = stack_to;
  3905. } else {
  3906. move_amount = 0;
  3907. }
  3908. }
  3909. // Source stack goes fully into destination stack
  3910. else if (leftover.empty()) {
  3911. m_selected_amount -= move_amount;
  3912. }
  3913. // Source stack goes partly into destination stack
  3914. else {
  3915. move_amount -= leftover.count;
  3916. m_selected_amount -= move_amount;
  3917. }
  3918. if (move_amount > 0) {
  3919. infostream << "Handing IAction::Move to manager" << std::endl;
  3920. IMoveAction *a = new IMoveAction();
  3921. a->count = move_amount;
  3922. a->from_inv = m_selected_item->inventoryloc;
  3923. a->from_list = m_selected_item->listname;
  3924. a->from_i = m_selected_item->i;
  3925. a->to_inv = s.inventoryloc;
  3926. a->to_list = s.listname;
  3927. a->to_i = s.i;
  3928. m_invmgr->inventoryAction(a);
  3929. }
  3930. } else if (pickup_amount > 0) {
  3931. // Send IAction::Move
  3932. assert(m_selected_item && m_selected_item->isValid());
  3933. assert(s.isValid());
  3934. assert(list_selected && list_s);
  3935. ItemStack stack_from = list_s->getItem(s.i);
  3936. ItemStack stack_to = list_selected->getItem(m_selected_item->i);
  3937. // Only move if the items are exactly the same,
  3938. // we shouldn't attempt to pickup different items
  3939. if (matching) {
  3940. // Check how many items can be moved
  3941. pickup_amount = stack_from.count = MYMIN(pickup_amount, stack_from.count);
  3942. ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
  3943. pickup_amount -= leftover.count;
  3944. } else {
  3945. pickup_amount = 0;
  3946. }
  3947. if (pickup_amount > 0) {
  3948. m_selected_amount += pickup_amount;
  3949. infostream << "Handing IAction::Move to manager" << std::endl;
  3950. IMoveAction *a = new IMoveAction();
  3951. a->count = pickup_amount;
  3952. a->from_inv = s.inventoryloc;
  3953. a->from_list = s.listname;
  3954. a->from_i = s.i;
  3955. a->to_inv = m_selected_item->inventoryloc;
  3956. a->to_list = m_selected_item->listname;
  3957. a->to_i = m_selected_item->i;
  3958. m_invmgr->inventoryAction(a);
  3959. }
  3960. } else if (shift_move_amount > 0) {
  3961. // Try to shift-move the item
  3962. do {
  3963. s16 r = getNextInventoryRing(s.inventoryloc, s.listname);
  3964. if (r < 0) // Not found
  3965. break;
  3966. const ListRingSpec &to_ring = m_inventory_rings[r];
  3967. InventoryList *list_from = list_s;
  3968. if (!s.isValid())
  3969. break;
  3970. Inventory *inv_to = m_invmgr->getInventory(to_ring.inventoryloc);
  3971. if (!inv_to)
  3972. break;
  3973. InventoryList *list_to = inv_to->getList(to_ring.listname);
  3974. if (!list_to)
  3975. break;
  3976. // Check how many items can be moved
  3977. ItemStack stack_from = list_from->getItem(s.i);
  3978. shift_move_amount = MYMIN(shift_move_amount, stack_from.count);
  3979. if (shift_move_amount == 0)
  3980. break;
  3981. infostream << "Handing IAction::Move to manager" << std::endl;
  3982. IMoveAction *a = new IMoveAction();
  3983. a->count = shift_move_amount;
  3984. a->from_inv = s.inventoryloc;
  3985. a->from_list = s.listname;
  3986. a->from_i = s.i;
  3987. a->to_inv = to_ring.inventoryloc;
  3988. a->to_list = to_ring.listname;
  3989. a->move_somewhere = true;
  3990. m_invmgr->inventoryAction(a);
  3991. } while (0);
  3992. } else if (drop_amount > 0) {
  3993. // Send IAction::Drop
  3994. assert(m_selected_item && m_selected_item->isValid());
  3995. assert(list_selected);
  3996. ItemStack stack_from = list_selected->getItem(m_selected_item->i);
  3997. // Check how many items can be dropped
  3998. drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
  3999. assert(drop_amount > 0 && drop_amount <= m_selected_amount);
  4000. m_selected_amount -= drop_amount;
  4001. infostream << "Handing IAction::Drop to manager" << std::endl;
  4002. IDropAction *a = new IDropAction();
  4003. a->count = drop_amount;
  4004. a->from_inv = m_selected_item->inventoryloc;
  4005. a->from_list = m_selected_item->listname;
  4006. a->from_i = m_selected_item->i;
  4007. m_invmgr->inventoryAction(a);
  4008. } else if (craft_amount > 0) {
  4009. assert(s.isValid());
  4010. // If there are no items selected or the selected item
  4011. // belongs to craftresult list, proceed with crafting
  4012. if (!m_selected_item ||
  4013. !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
  4014. assert(inv_s);
  4015. // Send IACTION_CRAFT
  4016. infostream << "Handing IACTION_CRAFT to manager" << std::endl;
  4017. ICraftAction *a = new ICraftAction();
  4018. a->count = craft_amount;
  4019. a->craft_inv = s.inventoryloc;
  4020. m_invmgr->inventoryAction(a);
  4021. }
  4022. }
  4023. // If m_selected_amount has been decreased to zero,
  4024. // and we are not left-dragging, deselect
  4025. if (m_selected_amount == 0 && !m_left_dragging) {
  4026. m_selected_swap.clear();
  4027. delete m_selected_item;
  4028. m_selected_item = nullptr;
  4029. m_selected_amount = 0;
  4030. m_selected_dragging = false;
  4031. }
  4032. m_old_pointer = m_pointer;
  4033. }
  4034. if (event.EventType == EET_GUI_EVENT) {
  4035. if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
  4036. && isVisible()) {
  4037. // find the element that was clicked
  4038. for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
  4039. if ((s.ftype == f_TabHeader) &&
  4040. (s.fid == event.GUIEvent.Caller->getID())) {
  4041. if (!s.sound.empty() && m_sound_manager)
  4042. m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
  4043. s.send = true;
  4044. acceptInput();
  4045. s.send = false;
  4046. return true;
  4047. }
  4048. }
  4049. }
  4050. if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
  4051. && isVisible()) {
  4052. if (!canTakeFocus(event.GUIEvent.Element)) {
  4053. infostream<<"GUIFormSpecMenu: Not allowing focus change."
  4054. <<std::endl;
  4055. // Returning true disables focus change
  4056. return true;
  4057. }
  4058. }
  4059. if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
  4060. (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
  4061. (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
  4062. (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
  4063. s32 caller_id = event.GUIEvent.Caller->getID();
  4064. if (caller_id == 257) {
  4065. if (m_allowclose) {
  4066. acceptInput(quit_mode_accept);
  4067. quitMenu();
  4068. } else {
  4069. acceptInput();
  4070. m_text_dst->gotText(L"ExitButton");
  4071. }
  4072. // quitMenu deallocates menu
  4073. return true;
  4074. }
  4075. // find the element that was clicked
  4076. for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
  4077. // if its a button, set the send field so
  4078. // lua knows which button was pressed
  4079. if (caller_id != s.fid)
  4080. continue;
  4081. if (s.ftype == f_Button || s.ftype == f_CheckBox) {
  4082. if (!s.sound.empty() && m_sound_manager)
  4083. m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
  4084. s.send = true;
  4085. if (s.is_exit) {
  4086. if (m_allowclose) {
  4087. acceptInput(quit_mode_accept);
  4088. quitMenu();
  4089. } else {
  4090. m_text_dst->gotText(L"ExitButton");
  4091. }
  4092. return true;
  4093. }
  4094. acceptInput(quit_mode_no);
  4095. s.send = false;
  4096. return true;
  4097. } else if (s.ftype == f_DropDown) {
  4098. // only send the changed dropdown
  4099. for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
  4100. if (s2.ftype == f_DropDown) {
  4101. s2.send = false;
  4102. }
  4103. }
  4104. if (!s.sound.empty() && m_sound_manager)
  4105. m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
  4106. s.send = true;
  4107. acceptInput(quit_mode_no);
  4108. // revert configuration to make sure dropdowns are sent on
  4109. // regular button click
  4110. for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
  4111. if (s2.ftype == f_DropDown) {
  4112. s2.send = true;
  4113. }
  4114. }
  4115. return true;
  4116. } else if (s.ftype == f_ScrollBar) {
  4117. s.fdefault = L"Changed";
  4118. acceptInput(quit_mode_no);
  4119. s.fdefault.clear();
  4120. } else if (s.ftype == f_Unknown || s.ftype == f_HyperText) {
  4121. if (!s.sound.empty() && m_sound_manager)
  4122. m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
  4123. s.send = true;
  4124. acceptInput();
  4125. s.send = false;
  4126. }
  4127. }
  4128. }
  4129. if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
  4130. // move scroll_containers
  4131. for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers)
  4132. c.second->onScrollEvent(event.GUIEvent.Caller);
  4133. }
  4134. if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
  4135. if (event.GUIEvent.Caller->getID() > 257) {
  4136. bool close_on_enter = true;
  4137. for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
  4138. if (s.ftype == f_Unknown &&
  4139. s.fid == event.GUIEvent.Caller->getID()) {
  4140. current_field_enter_pending = s.fname;
  4141. std::unordered_map<std::string, bool>::const_iterator it =
  4142. field_close_on_enter.find(s.fname);
  4143. if (it != field_close_on_enter.end())
  4144. close_on_enter = (*it).second;
  4145. break;
  4146. }
  4147. }
  4148. if (m_allowclose && close_on_enter) {
  4149. current_keys_pending.key_enter = true;
  4150. acceptInput(quit_mode_accept);
  4151. quitMenu();
  4152. } else {
  4153. current_keys_pending.key_enter = true;
  4154. acceptInput();
  4155. }
  4156. // quitMenu deallocates menu
  4157. return true;
  4158. }
  4159. }
  4160. if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
  4161. int current_id = event.GUIEvent.Caller->getID();
  4162. if (current_id > 257) {
  4163. // find the element that was clicked
  4164. for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
  4165. // if it's a table, set the send field
  4166. // so lua knows which table was changed
  4167. if ((s.ftype == f_Table) && (s.fid == current_id)) {
  4168. s.send = true;
  4169. acceptInput();
  4170. s.send=false;
  4171. }
  4172. }
  4173. return true;
  4174. }
  4175. }
  4176. }
  4177. if (m_second_touch)
  4178. return true; // Stop propagating the event
  4179. return Parent ? Parent->OnEvent(event) : false;
  4180. }
  4181. /**
  4182. * get name of element by element id
  4183. * @param id of element
  4184. * @return name string or empty string
  4185. */
  4186. std::string GUIFormSpecMenu::getNameByID(s32 id)
  4187. {
  4188. for (FieldSpec &spec : m_fields) {
  4189. if (spec.fid == id)
  4190. return spec.fname;
  4191. }
  4192. return "";
  4193. }
  4194. const GUIFormSpecMenu::FieldSpec *GUIFormSpecMenu::getSpecByID(s32 id)
  4195. {
  4196. for (FieldSpec &spec : m_fields) {
  4197. if (spec.fid == id)
  4198. return &spec;
  4199. }
  4200. return nullptr;
  4201. }
  4202. /**
  4203. * get label of element by id
  4204. * @param id of element
  4205. * @return label string or empty string
  4206. */
  4207. std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
  4208. {
  4209. for (FieldSpec &spec : m_fields) {
  4210. if (spec.fid == id)
  4211. return spec.flabel;
  4212. }
  4213. return L"";
  4214. }
  4215. StyleSpec GUIFormSpecMenu::getDefaultStyleForElement(const std::string &type,
  4216. const std::string &name, const std::string &parent_type) {
  4217. return getStyleForElement(type, name, parent_type)[StyleSpec::STATE_DEFAULT];
  4218. }
  4219. std::array<StyleSpec, StyleSpec::NUM_STATES> GUIFormSpecMenu::getStyleForElement(
  4220. const std::string &type, const std::string &name, const std::string &parent_type)
  4221. {
  4222. std::array<StyleSpec, StyleSpec::NUM_STATES> ret;
  4223. auto it = theme_by_type.find("*");
  4224. if (it != theme_by_type.end()) {
  4225. for (const StyleSpec &spec : it->second)
  4226. ret[(u32)spec.getState()] |= spec;
  4227. }
  4228. it = theme_by_name.find("*");
  4229. if (it != theme_by_name.end()) {
  4230. for (const StyleSpec &spec : it->second)
  4231. ret[(u32)spec.getState()] |= spec;
  4232. }
  4233. if (!parent_type.empty()) {
  4234. it = theme_by_type.find(parent_type);
  4235. if (it != theme_by_type.end()) {
  4236. for (const StyleSpec &spec : it->second)
  4237. ret[(u32)spec.getState()] |= spec;
  4238. }
  4239. }
  4240. it = theme_by_type.find(type);
  4241. if (it != theme_by_type.end()) {
  4242. for (const StyleSpec &spec : it->second)
  4243. ret[(u32)spec.getState()] |= spec;
  4244. }
  4245. it = theme_by_name.find(name);
  4246. if (it != theme_by_name.end()) {
  4247. for (const StyleSpec &spec : it->second)
  4248. ret[(u32)spec.getState()] |= spec;
  4249. }
  4250. return ret;
  4251. }