guiFormSpecMenu.cpp 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864
  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 <algorithm>
  18. #include <iterator>
  19. #include <sstream>
  20. #include <limits>
  21. #include "guiFormSpecMenu.h"
  22. #include "guiTable.h"
  23. #include "constants.h"
  24. #include "gamedef.h"
  25. #include "keycode.h"
  26. #include "util/strfnd.h"
  27. #include <IGUICheckBox.h>
  28. #include <IGUIEditBox.h>
  29. #include <IGUIButton.h>
  30. #include <IGUIStaticText.h>
  31. #include <IGUIFont.h>
  32. #include <IGUITabControl.h>
  33. #include <IGUIComboBox.h>
  34. #include "client/renderingengine.h"
  35. #include "log.h"
  36. #include "client/tile.h" // ITextureSource
  37. #include "hud.h" // drawItemStack
  38. #include "filesys.h"
  39. #include "gettime.h"
  40. #include "gettext.h"
  41. #include "scripting_server.h"
  42. #include "porting.h"
  43. #include "settings.h"
  44. #include "client.h"
  45. #include "fontengine.h"
  46. #include "util/hex.h"
  47. #include "util/numeric.h"
  48. #include "util/string.h" // for parseColorString()
  49. #include "irrlicht_changes/static_text.h"
  50. #include "guiscalingfilter.h"
  51. #include "guiEditBoxWithScrollbar.h"
  52. #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
  53. #include "intlGUIEditBox.h"
  54. #endif
  55. #define MY_CHECKPOS(a,b) \
  56. if (v_pos.size() != 2) { \
  57. errorstream<< "Invalid pos for element " << a << "specified: \"" \
  58. << parts[b] << "\"" << std::endl; \
  59. return; \
  60. }
  61. #define MY_CHECKGEOM(a,b) \
  62. if (v_geom.size() != 2) { \
  63. errorstream<< "Invalid pos for element " << a << "specified: \"" \
  64. << parts[b] << "\"" << std::endl; \
  65. return; \
  66. }
  67. /*
  68. GUIFormSpecMenu
  69. */
  70. static unsigned int font_line_height(gui::IGUIFont *font)
  71. {
  72. return font->getDimension(L"Ay").Height + font->getKerningHeight();
  73. }
  74. inline u32 clamp_u8(s32 value)
  75. {
  76. return (u32) MYMIN(MYMAX(value, 0), 255);
  77. }
  78. GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
  79. gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
  80. Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
  81. bool remap_dbl_click) :
  82. GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
  83. m_invmgr(client),
  84. m_tsrc(tsrc),
  85. m_client(client),
  86. m_form_src(fsrc),
  87. m_text_dst(tdst),
  88. m_joystick(joystick),
  89. m_remap_dbl_click(remap_dbl_click)
  90. #ifdef __ANDROID__
  91. , m_JavaDialogFieldName("")
  92. #endif
  93. {
  94. current_keys_pending.key_down = false;
  95. current_keys_pending.key_up = false;
  96. current_keys_pending.key_enter = false;
  97. current_keys_pending.key_escape = false;
  98. m_doubleclickdetect[0].time = 0;
  99. m_doubleclickdetect[1].time = 0;
  100. m_doubleclickdetect[0].pos = v2s32(0, 0);
  101. m_doubleclickdetect[1].pos = v2s32(0, 0);
  102. m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
  103. m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
  104. }
  105. GUIFormSpecMenu::~GUIFormSpecMenu()
  106. {
  107. removeChildren();
  108. for (auto &table_it : m_tables) {
  109. table_it.second->drop();
  110. }
  111. delete m_selected_item;
  112. delete m_form_src;
  113. delete m_text_dst;
  114. }
  115. void GUIFormSpecMenu::removeChildren()
  116. {
  117. const core::list<gui::IGUIElement*> &children = getChildren();
  118. while(!children.empty()) {
  119. (*children.getLast())->remove();
  120. }
  121. if(m_tooltip_element) {
  122. m_tooltip_element->remove();
  123. m_tooltip_element->drop();
  124. m_tooltip_element = NULL;
  125. }
  126. }
  127. void GUIFormSpecMenu::setInitialFocus()
  128. {
  129. // Set initial focus according to following order of precedence:
  130. // 1. first empty editbox
  131. // 2. first editbox
  132. // 3. first table
  133. // 4. last button
  134. // 5. first focusable (not statictext, not tabheader)
  135. // 6. first child element
  136. core::list<gui::IGUIElement*> children = getChildren();
  137. // in case "children" contains any NULL elements, remove them
  138. for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
  139. it != children.end();) {
  140. if (*it)
  141. ++it;
  142. else
  143. it = children.erase(it);
  144. }
  145. // 1. first empty editbox
  146. for (gui::IGUIElement *it : children) {
  147. if (it->getType() == gui::EGUIET_EDIT_BOX
  148. && it->getText()[0] == 0) {
  149. Environment->setFocus(it);
  150. return;
  151. }
  152. }
  153. // 2. first editbox
  154. for (gui::IGUIElement *it : children) {
  155. if (it->getType() == gui::EGUIET_EDIT_BOX) {
  156. Environment->setFocus(it);
  157. return;
  158. }
  159. }
  160. // 3. first table
  161. for (gui::IGUIElement *it : children) {
  162. if (it->getTypeName() == std::string("GUITable")) {
  163. Environment->setFocus(it);
  164. return;
  165. }
  166. }
  167. // 4. last button
  168. for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
  169. it != children.end(); --it) {
  170. if ((*it)->getType() == gui::EGUIET_BUTTON) {
  171. Environment->setFocus(*it);
  172. return;
  173. }
  174. }
  175. // 5. first focusable (not statictext, not tabheader)
  176. for (gui::IGUIElement *it : children) {
  177. if (it->getType() != gui::EGUIET_STATIC_TEXT &&
  178. it->getType() != gui::EGUIET_TAB_CONTROL) {
  179. Environment->setFocus(it);
  180. return;
  181. }
  182. }
  183. // 6. first child element
  184. if (children.empty())
  185. Environment->setFocus(this);
  186. else
  187. Environment->setFocus(*(children.begin()));
  188. }
  189. GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
  190. {
  191. for (auto &table : m_tables) {
  192. if (tablename == table.first.fname)
  193. return table.second;
  194. }
  195. return 0;
  196. }
  197. std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
  198. {
  199. for (auto &dropdown : m_dropdowns) {
  200. if (name == dropdown.first.fname)
  201. return &dropdown.second;
  202. }
  203. return NULL;
  204. }
  205. void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
  206. {
  207. std::vector<std::string> parts = split(element,',');
  208. if (((parts.size() == 2) || parts.size() == 3) ||
  209. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  210. {
  211. if (parts[1].find(';') != std::string::npos)
  212. parts[1] = parts[1].substr(0,parts[1].find(';'));
  213. data->invsize.X = MYMAX(0, stof(parts[0]));
  214. data->invsize.Y = MYMAX(0, stof(parts[1]));
  215. lockSize(false);
  216. if (parts.size() == 3) {
  217. if (parts[2] == "true") {
  218. lockSize(true,v2u32(800,600));
  219. }
  220. }
  221. data->explicit_size = true;
  222. return;
  223. }
  224. errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
  225. }
  226. void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
  227. {
  228. std::vector<std::string> parts = split(element, ',');
  229. if (parts.size() >= 2) {
  230. if (parts[1].find(';') != std::string::npos)
  231. parts[1] = parts[1].substr(0, parts[1].find(';'));
  232. container_stack.push(pos_offset);
  233. pos_offset.X += MYMAX(0, stof(parts[0]));
  234. pos_offset.Y += MYMAX(0, stof(parts[1]));
  235. return;
  236. }
  237. errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
  238. }
  239. void GUIFormSpecMenu::parseContainerEnd(parserData* data)
  240. {
  241. if (container_stack.empty()) {
  242. errorstream<< "Invalid container end element, no matching container start element" << std::endl;
  243. } else {
  244. pos_offset = container_stack.top();
  245. container_stack.pop();
  246. }
  247. }
  248. void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
  249. {
  250. if (m_client == 0) {
  251. warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
  252. return;
  253. }
  254. std::vector<std::string> parts = split(element,';');
  255. if (((parts.size() == 4) || (parts.size() == 5)) ||
  256. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  257. {
  258. std::string location = parts[0];
  259. std::string listname = parts[1];
  260. std::vector<std::string> v_pos = split(parts[2],',');
  261. std::vector<std::string> v_geom = split(parts[3],',');
  262. std::string startindex;
  263. if (parts.size() == 5)
  264. startindex = parts[4];
  265. MY_CHECKPOS("list",2);
  266. MY_CHECKGEOM("list",3);
  267. InventoryLocation loc;
  268. if(location == "context" || location == "current_name")
  269. loc = m_current_inventory_location;
  270. else
  271. loc.deSerialize(location);
  272. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  273. pos.X += stof(v_pos[0]) * (float)spacing.X;
  274. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  275. v2s32 geom;
  276. geom.X = stoi(v_geom[0]);
  277. geom.Y = stoi(v_geom[1]);
  278. s32 start_i = 0;
  279. if (!startindex.empty())
  280. start_i = stoi(startindex);
  281. if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
  282. errorstream<< "Invalid list element: '" << element << "'" << std::endl;
  283. return;
  284. }
  285. if(!data->explicit_size)
  286. warningstream<<"invalid use of list without a size[] element"<<std::endl;
  287. m_inventorylists.emplace_back(loc, listname, pos, geom, start_i);
  288. return;
  289. }
  290. errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
  291. }
  292. void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element)
  293. {
  294. if (m_client == 0) {
  295. errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
  296. return;
  297. }
  298. std::vector<std::string> parts = split(element, ';');
  299. if (parts.size() == 2) {
  300. std::string location = parts[0];
  301. std::string listname = parts[1];
  302. InventoryLocation loc;
  303. if (location == "context" || location == "current_name")
  304. loc = m_current_inventory_location;
  305. else
  306. loc.deSerialize(location);
  307. m_inventory_rings.emplace_back(loc, listname);
  308. return;
  309. }
  310. if (element.empty() && m_inventorylists.size() > 1) {
  311. size_t siz = m_inventorylists.size();
  312. // insert the last two inv list elements into the list ring
  313. const ListDrawSpec &spa = m_inventorylists[siz - 2];
  314. const ListDrawSpec &spb = m_inventorylists[siz - 1];
  315. m_inventory_rings.emplace_back(spa.inventoryloc, spa.listname);
  316. m_inventory_rings.emplace_back(spb.inventoryloc, spb.listname);
  317. return;
  318. }
  319. errorstream<< "Invalid list ring element(" << parts.size() << ", "
  320. << m_inventorylists.size() << "): '" << element << "'" << std::endl;
  321. }
  322. void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
  323. {
  324. std::vector<std::string> parts = split(element,';');
  325. if (((parts.size() >= 3) && (parts.size() <= 4)) ||
  326. ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
  327. {
  328. std::vector<std::string> v_pos = split(parts[0],',');
  329. std::string name = parts[1];
  330. std::string label = parts[2];
  331. std::string selected;
  332. if (parts.size() >= 4)
  333. selected = parts[3];
  334. MY_CHECKPOS("checkbox",0);
  335. v2s32 pos = padding + pos_offset * spacing;
  336. pos.X += stof(v_pos[0]) * (float) spacing.X;
  337. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  338. bool fselected = false;
  339. if (selected == "true")
  340. fselected = true;
  341. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  342. core::rect<s32> rect = core::rect<s32>(
  343. pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
  344. pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
  345. pos.Y + ((imgsize.Y/2) + m_btn_height));
  346. FieldSpec spec(
  347. name,
  348. wlabel, //Needed for displaying text on MSVC
  349. wlabel,
  350. 258+m_fields.size()
  351. );
  352. spec.ftype = f_CheckBox;
  353. gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
  354. spec.fid, spec.flabel.c_str());
  355. if (spec.fname == data->focused_fieldname) {
  356. Environment->setFocus(e);
  357. }
  358. m_checkboxes.emplace_back(spec,e);
  359. m_fields.push_back(spec);
  360. return;
  361. }
  362. errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
  363. }
  364. void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
  365. {
  366. std::vector<std::string> parts = split(element,';');
  367. if (parts.size() >= 5) {
  368. std::vector<std::string> v_pos = split(parts[0],',');
  369. std::vector<std::string> v_dim = split(parts[1],',');
  370. std::string name = parts[3];
  371. std::string value = parts[4];
  372. MY_CHECKPOS("scrollbar",0);
  373. v2s32 pos = padding + pos_offset * spacing;
  374. pos.X += stof(v_pos[0]) * (float) spacing.X;
  375. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  376. if (v_dim.size() != 2) {
  377. errorstream<< "Invalid size for element " << "scrollbar"
  378. << "specified: \"" << parts[1] << "\"" << std::endl;
  379. return;
  380. }
  381. v2s32 dim;
  382. dim.X = stof(v_dim[0]) * (float) spacing.X;
  383. dim.Y = stof(v_dim[1]) * (float) spacing.Y;
  384. core::rect<s32> rect =
  385. core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
  386. FieldSpec spec(
  387. name,
  388. L"",
  389. L"",
  390. 258+m_fields.size()
  391. );
  392. bool is_horizontal = true;
  393. if (parts[2] == "vertical")
  394. is_horizontal = false;
  395. spec.ftype = f_ScrollBar;
  396. spec.send = true;
  397. gui::IGUIScrollBar* e =
  398. Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
  399. e->setMax(1000);
  400. e->setMin(0);
  401. e->setPos(stoi(parts[4]));
  402. e->setSmallStep(10);
  403. e->setLargeStep(100);
  404. m_scrollbars.emplace_back(spec,e);
  405. m_fields.push_back(spec);
  406. return;
  407. }
  408. errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
  409. }
  410. void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
  411. {
  412. std::vector<std::string> parts = split(element,';');
  413. if ((parts.size() == 3) ||
  414. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  415. {
  416. std::vector<std::string> v_pos = split(parts[0],',');
  417. std::vector<std::string> v_geom = split(parts[1],',');
  418. std::string name = unescape_string(parts[2]);
  419. MY_CHECKPOS("image", 0);
  420. MY_CHECKGEOM("image", 1);
  421. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  422. pos.X += stof(v_pos[0]) * (float) spacing.X;
  423. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  424. v2s32 geom;
  425. geom.X = stof(v_geom[0]) * (float)imgsize.X;
  426. geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
  427. if (!data->explicit_size)
  428. warningstream<<"invalid use of image without a size[] element"<<std::endl;
  429. m_images.emplace_back(name, pos, geom);
  430. return;
  431. }
  432. if (parts.size() == 2) {
  433. std::vector<std::string> v_pos = split(parts[0],',');
  434. std::string name = unescape_string(parts[1]);
  435. MY_CHECKPOS("image", 0);
  436. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  437. pos.X += stof(v_pos[0]) * (float) spacing.X;
  438. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  439. if (!data->explicit_size)
  440. warningstream<<"invalid use of image without a size[] element"<<std::endl;
  441. m_images.emplace_back(name, pos);
  442. return;
  443. }
  444. errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
  445. }
  446. void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
  447. {
  448. std::vector<std::string> parts = split(element,';');
  449. if ((parts.size() == 3) ||
  450. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  451. {
  452. std::vector<std::string> v_pos = split(parts[0],',');
  453. std::vector<std::string> v_geom = split(parts[1],',');
  454. std::string name = parts[2];
  455. MY_CHECKPOS("itemimage",0);
  456. MY_CHECKGEOM("itemimage",1);
  457. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  458. pos.X += stof(v_pos[0]) * (float) spacing.X;
  459. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  460. v2s32 geom;
  461. geom.X = stof(v_geom[0]) * (float)imgsize.X;
  462. geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
  463. if(!data->explicit_size)
  464. warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
  465. m_itemimages.emplace_back("", name, pos, geom);
  466. return;
  467. }
  468. errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
  469. }
  470. void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
  471. const std::string &type)
  472. {
  473. std::vector<std::string> parts = split(element,';');
  474. if ((parts.size() == 4) ||
  475. ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
  476. {
  477. std::vector<std::string> v_pos = split(parts[0],',');
  478. std::vector<std::string> v_geom = split(parts[1],',');
  479. std::string name = parts[2];
  480. std::string label = parts[3];
  481. MY_CHECKPOS("button",0);
  482. MY_CHECKGEOM("button",1);
  483. v2s32 pos = padding + pos_offset * spacing;
  484. pos.X += stof(v_pos[0]) * (float)spacing.X;
  485. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  486. v2s32 geom;
  487. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  488. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  489. core::rect<s32> rect =
  490. core::rect<s32>(pos.X, pos.Y - m_btn_height,
  491. pos.X + geom.X, pos.Y + m_btn_height);
  492. if(!data->explicit_size)
  493. warningstream<<"invalid use of button without a size[] element"<<std::endl;
  494. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  495. FieldSpec spec(
  496. name,
  497. wlabel,
  498. L"",
  499. 258+m_fields.size()
  500. );
  501. spec.ftype = f_Button;
  502. if(type == "button_exit")
  503. spec.is_exit = true;
  504. gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
  505. spec.flabel.c_str());
  506. if (spec.fname == data->focused_fieldname) {
  507. Environment->setFocus(e);
  508. }
  509. m_fields.push_back(spec);
  510. return;
  511. }
  512. errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
  513. }
  514. void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
  515. {
  516. std::vector<std::string> parts = split(element,';');
  517. if (((parts.size() == 3) || (parts.size() == 4)) ||
  518. ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
  519. {
  520. std::vector<std::string> v_pos = split(parts[0],',');
  521. std::vector<std::string> v_geom = split(parts[1],',');
  522. std::string name = unescape_string(parts[2]);
  523. MY_CHECKPOS("background",0);
  524. MY_CHECKGEOM("background",1);
  525. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  526. pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X - (float)imgsize.X)/2;
  527. pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y - (float)imgsize.Y)/2;
  528. v2s32 geom;
  529. geom.X = stof(v_geom[0]) * (float)spacing.X;
  530. geom.Y = stof(v_geom[1]) * (float)spacing.Y;
  531. if (!data->explicit_size)
  532. warningstream<<"invalid use of background without a size[] element"<<std::endl;
  533. bool clip = false;
  534. if (parts.size() == 4 && is_yes(parts[3])) {
  535. pos.X = stoi(v_pos[0]); //acts as offset
  536. pos.Y = stoi(v_pos[1]); //acts as offset
  537. clip = true;
  538. }
  539. m_backgrounds.emplace_back(name, pos, geom, clip);
  540. return;
  541. }
  542. errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
  543. }
  544. void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
  545. {
  546. std::vector<std::string> parts = split(element,';');
  547. data->table_options.clear();
  548. for (const std::string &part : parts) {
  549. // Parse table option
  550. std::string opt = unescape_string(part);
  551. data->table_options.push_back(GUITable::splitOption(opt));
  552. }
  553. }
  554. void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
  555. {
  556. std::vector<std::string> parts = split(element,';');
  557. data->table_columns.clear();
  558. for (const std::string &part : parts) {
  559. std::vector<std::string> col_parts = split(part,',');
  560. GUITable::TableColumn column;
  561. // Parse column type
  562. if (!col_parts.empty())
  563. column.type = col_parts[0];
  564. // Parse column options
  565. for (size_t j = 1; j < col_parts.size(); ++j) {
  566. std::string opt = unescape_string(col_parts[j]);
  567. column.options.push_back(GUITable::splitOption(opt));
  568. }
  569. data->table_columns.push_back(column);
  570. }
  571. }
  572. void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
  573. {
  574. std::vector<std::string> parts = split(element,';');
  575. if (((parts.size() == 4) || (parts.size() == 5)) ||
  576. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  577. {
  578. std::vector<std::string> v_pos = split(parts[0],',');
  579. std::vector<std::string> v_geom = split(parts[1],',');
  580. std::string name = parts[2];
  581. std::vector<std::string> items = split(parts[3],',');
  582. std::string str_initial_selection;
  583. std::string str_transparent = "false";
  584. if (parts.size() >= 5)
  585. str_initial_selection = parts[4];
  586. MY_CHECKPOS("table",0);
  587. MY_CHECKGEOM("table",1);
  588. v2s32 pos = padding + pos_offset * spacing;
  589. pos.X += stof(v_pos[0]) * (float)spacing.X;
  590. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  591. v2s32 geom;
  592. geom.X = stof(v_geom[0]) * (float)spacing.X;
  593. geom.Y = stof(v_geom[1]) * (float)spacing.Y;
  594. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  595. FieldSpec spec(
  596. name,
  597. L"",
  598. L"",
  599. 258+m_fields.size()
  600. );
  601. spec.ftype = f_Table;
  602. for (std::string &item : items) {
  603. item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
  604. }
  605. //now really show table
  606. GUITable *e = new GUITable(Environment, this, spec.fid, rect,
  607. m_tsrc);
  608. if (spec.fname == data->focused_fieldname) {
  609. Environment->setFocus(e);
  610. }
  611. e->setTable(data->table_options, data->table_columns, items);
  612. if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
  613. e->setDynamicData(data->table_dyndata[name]);
  614. }
  615. if (!str_initial_selection.empty() && str_initial_selection != "0")
  616. e->setSelected(stoi(str_initial_selection));
  617. m_tables.emplace_back(spec, e);
  618. m_fields.push_back(spec);
  619. return;
  620. }
  621. errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
  622. }
  623. void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
  624. {
  625. std::vector<std::string> parts = split(element,';');
  626. if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
  627. ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
  628. {
  629. std::vector<std::string> v_pos = split(parts[0],',');
  630. std::vector<std::string> v_geom = split(parts[1],',');
  631. std::string name = parts[2];
  632. std::vector<std::string> items = split(parts[3],',');
  633. std::string str_initial_selection;
  634. std::string str_transparent = "false";
  635. if (parts.size() >= 5)
  636. str_initial_selection = parts[4];
  637. if (parts.size() >= 6)
  638. str_transparent = parts[5];
  639. MY_CHECKPOS("textlist",0);
  640. MY_CHECKGEOM("textlist",1);
  641. v2s32 pos = padding + pos_offset * spacing;
  642. pos.X += stof(v_pos[0]) * (float)spacing.X;
  643. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  644. v2s32 geom;
  645. geom.X = stof(v_geom[0]) * (float)spacing.X;
  646. geom.Y = stof(v_geom[1]) * (float)spacing.Y;
  647. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  648. FieldSpec spec(
  649. name,
  650. L"",
  651. L"",
  652. 258+m_fields.size()
  653. );
  654. spec.ftype = f_Table;
  655. for (std::string &item : items) {
  656. item = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(item))));
  657. }
  658. //now really show list
  659. GUITable *e = new GUITable(Environment, this, spec.fid, rect,
  660. m_tsrc);
  661. if (spec.fname == data->focused_fieldname) {
  662. Environment->setFocus(e);
  663. }
  664. e->setTextList(items, is_yes(str_transparent));
  665. if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
  666. e->setDynamicData(data->table_dyndata[name]);
  667. }
  668. if (!str_initial_selection.empty() && str_initial_selection != "0")
  669. e->setSelected(stoi(str_initial_selection));
  670. m_tables.emplace_back(spec, e);
  671. m_fields.push_back(spec);
  672. return;
  673. }
  674. errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
  675. }
  676. void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
  677. {
  678. std::vector<std::string> parts = split(element,';');
  679. if ((parts.size() == 5) ||
  680. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  681. {
  682. std::vector<std::string> v_pos = split(parts[0],',');
  683. std::string name = parts[2];
  684. std::vector<std::string> items = split(parts[3],',');
  685. std::string str_initial_selection;
  686. str_initial_selection = parts[4];
  687. MY_CHECKPOS("dropdown",0);
  688. v2s32 pos = padding + pos_offset * spacing;
  689. pos.X += stof(v_pos[0]) * (float)spacing.X;
  690. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  691. s32 width = stof(parts[1]) * (float)spacing.Y;
  692. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
  693. pos.X + width, pos.Y + (m_btn_height * 2));
  694. FieldSpec spec(
  695. name,
  696. L"",
  697. L"",
  698. 258+m_fields.size()
  699. );
  700. spec.ftype = f_DropDown;
  701. spec.send = true;
  702. //now really show list
  703. gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
  704. if (spec.fname == data->focused_fieldname) {
  705. Environment->setFocus(e);
  706. }
  707. for (const std::string &item : items) {
  708. e->addItem(unescape_translate(unescape_string(
  709. utf8_to_wide(item))).c_str());
  710. }
  711. if (!str_initial_selection.empty())
  712. e->setSelected(stoi(str_initial_selection)-1);
  713. m_fields.push_back(spec);
  714. m_dropdowns.emplace_back(spec, std::vector<std::string>());
  715. std::vector<std::string> &values = m_dropdowns.back().second;
  716. for (const std::string &item : items) {
  717. values.push_back(unescape_string(item));
  718. }
  719. return;
  720. }
  721. errorstream << "Invalid dropdown element(" << parts.size() << "): '"
  722. << element << "'" << std::endl;
  723. }
  724. void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
  725. {
  726. std::vector<std::string> parts = split(element,';');
  727. if (parts.size() == 2 ||
  728. (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
  729. field_close_on_enter[parts[0]] = is_yes(parts[1]);
  730. }
  731. }
  732. void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
  733. {
  734. std::vector<std::string> parts = split(element,';');
  735. if ((parts.size() == 4) || (parts.size() == 5) ||
  736. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  737. {
  738. std::vector<std::string> v_pos = split(parts[0],',');
  739. std::vector<std::string> v_geom = split(parts[1],',');
  740. std::string name = parts[2];
  741. std::string label = parts[3];
  742. MY_CHECKPOS("pwdfield",0);
  743. MY_CHECKGEOM("pwdfield",1);
  744. v2s32 pos = pos_offset * spacing;
  745. pos.X += stof(v_pos[0]) * (float)spacing.X;
  746. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  747. v2s32 geom;
  748. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  749. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  750. pos.Y -= m_btn_height;
  751. geom.Y = m_btn_height*2;
  752. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  753. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  754. FieldSpec spec(
  755. name,
  756. wlabel,
  757. L"",
  758. 258+m_fields.size()
  759. );
  760. spec.send = true;
  761. gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
  762. if (spec.fname == data->focused_fieldname) {
  763. Environment->setFocus(e);
  764. }
  765. if (label.length() >= 1)
  766. {
  767. int font_height = g_fontengine->getTextHeight();
  768. rect.UpperLeftCorner.Y -= font_height;
  769. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
  770. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
  771. }
  772. e->setPasswordBox(true,L'*');
  773. irr::SEvent evt;
  774. evt.EventType = EET_KEY_INPUT_EVENT;
  775. evt.KeyInput.Key = KEY_END;
  776. evt.KeyInput.Char = 0;
  777. evt.KeyInput.Control = false;
  778. evt.KeyInput.Shift = false;
  779. evt.KeyInput.PressedDown = true;
  780. e->OnEvent(evt);
  781. if (parts.size() >= 5) {
  782. // TODO: remove after 2016-11-03
  783. warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
  784. " instead of the 5th param" << std::endl;
  785. field_close_on_enter[name] = is_yes(parts[4]);
  786. }
  787. m_fields.push_back(spec);
  788. return;
  789. }
  790. errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
  791. }
  792. void GUIFormSpecMenu::parseSimpleField(parserData* data,
  793. std::vector<std::string> &parts)
  794. {
  795. std::string name = parts[0];
  796. std::string label = parts[1];
  797. std::string default_val = parts[2];
  798. core::rect<s32> rect;
  799. if(data->explicit_size)
  800. warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
  801. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  802. pos.Y = ((m_fields.size()+2)*60);
  803. v2s32 size = DesiredRect.getSize();
  804. rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
  805. (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
  806. if(m_form_src)
  807. default_val = m_form_src->resolveText(default_val);
  808. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  809. FieldSpec spec(
  810. name,
  811. wlabel,
  812. utf8_to_wide(unescape_string(default_val)),
  813. 258+m_fields.size()
  814. );
  815. if (name.empty()) {
  816. // spec field id to 0, this stops submit searching for a value that isn't there
  817. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
  818. } else {
  819. spec.send = true;
  820. gui::IGUIElement *e;
  821. #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
  822. if (g_settings->getBool("freetype")) {
  823. e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
  824. true, Environment, this, spec.fid, rect);
  825. e->drop();
  826. } else {
  827. #else
  828. {
  829. #endif
  830. e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
  831. }
  832. if (spec.fname == data->focused_fieldname) {
  833. Environment->setFocus(e);
  834. }
  835. irr::SEvent evt;
  836. evt.EventType = EET_KEY_INPUT_EVENT;
  837. evt.KeyInput.Key = KEY_END;
  838. evt.KeyInput.Char = 0;
  839. evt.KeyInput.Control = 0;
  840. evt.KeyInput.Shift = 0;
  841. evt.KeyInput.PressedDown = true;
  842. e->OnEvent(evt);
  843. if (label.length() >= 1)
  844. {
  845. int font_height = g_fontengine->getTextHeight();
  846. rect.UpperLeftCorner.Y -= font_height;
  847. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
  848. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
  849. }
  850. }
  851. if (parts.size() >= 4) {
  852. // TODO: remove after 2016-11-03
  853. warningstream << "field/simple: use field_close_on_enter[name, enabled]" <<
  854. " instead of the 4th param" << std::endl;
  855. field_close_on_enter[name] = is_yes(parts[3]);
  856. }
  857. m_fields.push_back(spec);
  858. }
  859. void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
  860. const std::string &type)
  861. {
  862. std::vector<std::string> v_pos = split(parts[0],',');
  863. std::vector<std::string> v_geom = split(parts[1],',');
  864. std::string name = parts[2];
  865. std::string label = parts[3];
  866. std::string default_val = parts[4];
  867. MY_CHECKPOS(type,0);
  868. MY_CHECKGEOM(type,1);
  869. v2s32 pos = pos_offset * spacing;
  870. pos.X += stof(v_pos[0]) * (float) spacing.X;
  871. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  872. v2s32 geom;
  873. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  874. if (type == "textarea")
  875. {
  876. geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
  877. pos.Y += m_btn_height;
  878. }
  879. else
  880. {
  881. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  882. pos.Y -= m_btn_height;
  883. geom.Y = m_btn_height*2;
  884. }
  885. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  886. if(!data->explicit_size)
  887. warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
  888. if(m_form_src)
  889. default_val = m_form_src->resolveText(default_val);
  890. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  891. FieldSpec spec(
  892. name,
  893. wlabel,
  894. utf8_to_wide(unescape_string(default_val)),
  895. 258+m_fields.size()
  896. );
  897. bool is_editable = !name.empty();
  898. if (is_editable)
  899. spec.send = true;
  900. gui::IGUIEditBox *e = nullptr;
  901. const wchar_t *text = spec.fdefault.empty() ?
  902. wlabel.c_str() : spec.fdefault.c_str();
  903. #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
  904. if (g_settings->getBool("freetype")) {
  905. e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(text,
  906. true, Environment, this, spec.fid, rect, is_editable, true);
  907. e->drop();
  908. } else {
  909. #else
  910. {
  911. #endif
  912. e = new GUIEditBoxWithScrollBar(text, true,
  913. Environment, this, spec.fid, rect, is_editable, true);
  914. }
  915. if (is_editable && spec.fname == data->focused_fieldname)
  916. Environment->setFocus(e);
  917. if (e) {
  918. if (type == "textarea")
  919. {
  920. e->setMultiLine(true);
  921. e->setWordWrap(true);
  922. e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
  923. } else {
  924. irr::SEvent evt;
  925. evt.EventType = EET_KEY_INPUT_EVENT;
  926. evt.KeyInput.Key = KEY_END;
  927. evt.KeyInput.Char = 0;
  928. evt.KeyInput.Control = 0;
  929. evt.KeyInput.Shift = 0;
  930. evt.KeyInput.PressedDown = true;
  931. e->OnEvent(evt);
  932. }
  933. }
  934. if (is_editable && !label.empty()) {
  935. int font_height = g_fontengine->getTextHeight();
  936. rect.UpperLeftCorner.Y -= font_height;
  937. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
  938. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
  939. }
  940. if (parts.size() >= 6) {
  941. // TODO: remove after 2016-11-03
  942. warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
  943. " instead of the 6th param" << std::endl;
  944. field_close_on_enter[name] = is_yes(parts[5]);
  945. }
  946. m_fields.push_back(spec);
  947. }
  948. void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
  949. const std::string &type)
  950. {
  951. std::vector<std::string> parts = split(element,';');
  952. if (parts.size() == 3 || parts.size() == 4) {
  953. parseSimpleField(data,parts);
  954. return;
  955. }
  956. if ((parts.size() == 5) || (parts.size() == 6) ||
  957. ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
  958. {
  959. parseTextArea(data,parts,type);
  960. return;
  961. }
  962. errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
  963. }
  964. void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
  965. {
  966. std::vector<std::string> parts = split(element,';');
  967. if ((parts.size() == 2) ||
  968. ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
  969. {
  970. std::vector<std::string> v_pos = split(parts[0],',');
  971. std::string text = parts[1];
  972. MY_CHECKPOS("label",0);
  973. v2s32 pos = padding + pos_offset * spacing;
  974. pos.X += stof(v_pos[0]) * (float)spacing.X;
  975. pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
  976. if(!data->explicit_size)
  977. warningstream<<"invalid use of label without a size[] element"<<std::endl;
  978. std::vector<std::string> lines = split(text, '\n');
  979. for (unsigned int i = 0; i != lines.size(); i++) {
  980. // Lines are spaced at the nominal distance of
  981. // 2/5 inventory slot, even if the font doesn't
  982. // quite match that. This provides consistent
  983. // form layout, at the expense of sometimes
  984. // having sub-optimal spacing for the font.
  985. // We multiply by 2 and then divide by 5, rather
  986. // than multiply by 0.4, to get exact results
  987. // in the integer cases: 0.4 is not exactly
  988. // representable in binary floating point.
  989. s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
  990. std::wstring wlabel = utf8_to_wide(unescape_string(lines[i]));
  991. core::rect<s32> rect = core::rect<s32>(
  992. pos.X, posy - m_btn_height,
  993. pos.X + m_font->getDimension(wlabel.c_str()).Width,
  994. posy + m_btn_height);
  995. FieldSpec spec(
  996. "",
  997. wlabel,
  998. L"",
  999. 258+m_fields.size()
  1000. );
  1001. gui::IGUIStaticText *e =
  1002. addStaticText(Environment, spec.flabel.c_str(),
  1003. rect, false, false, this, spec.fid);
  1004. e->setTextAlignment(gui::EGUIA_UPPERLEFT,
  1005. gui::EGUIA_CENTER);
  1006. m_fields.push_back(spec);
  1007. }
  1008. return;
  1009. }
  1010. errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
  1011. }
  1012. void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
  1013. {
  1014. std::vector<std::string> parts = split(element,';');
  1015. if ((parts.size() == 2) ||
  1016. ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1017. {
  1018. std::vector<std::string> v_pos = split(parts[0],',');
  1019. std::wstring text = unescape_translate(
  1020. unescape_string(utf8_to_wide(parts[1])));
  1021. MY_CHECKPOS("vertlabel",1);
  1022. v2s32 pos = padding + pos_offset * spacing;
  1023. pos.X += stof(v_pos[0]) * (float)spacing.X;
  1024. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  1025. core::rect<s32> rect = core::rect<s32>(
  1026. pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
  1027. pos.X+15, pos.Y +
  1028. font_line_height(m_font)
  1029. * (text.length()+1)
  1030. +((imgsize.Y/2)- m_btn_height));
  1031. //actually text.length() would be correct but adding +1 avoids to break all mods
  1032. if(!data->explicit_size)
  1033. warningstream<<"invalid use of label without a size[] element"<<std::endl;
  1034. std::wstring label;
  1035. for (wchar_t i : text) {
  1036. label += i;
  1037. label += L"\n";
  1038. }
  1039. FieldSpec spec(
  1040. "",
  1041. label,
  1042. L"",
  1043. 258+m_fields.size()
  1044. );
  1045. gui::IGUIStaticText *t =
  1046. addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
  1047. t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
  1048. m_fields.push_back(spec);
  1049. return;
  1050. }
  1051. errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
  1052. }
  1053. void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
  1054. const std::string &type)
  1055. {
  1056. std::vector<std::string> parts = split(element,';');
  1057. if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
  1058. ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1059. {
  1060. std::vector<std::string> v_pos = split(parts[0],',');
  1061. std::vector<std::string> v_geom = split(parts[1],',');
  1062. std::string image_name = parts[2];
  1063. std::string name = parts[3];
  1064. std::string label = parts[4];
  1065. MY_CHECKPOS("imagebutton",0);
  1066. MY_CHECKGEOM("imagebutton",1);
  1067. v2s32 pos = padding + pos_offset * spacing;
  1068. pos.X += stof(v_pos[0]) * (float)spacing.X;
  1069. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  1070. v2s32 geom;
  1071. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  1072. geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
  1073. bool noclip = false;
  1074. bool drawborder = true;
  1075. std::string pressed_image_name;
  1076. if (parts.size() >= 7) {
  1077. if (parts[5] == "true")
  1078. noclip = true;
  1079. if (parts[6] == "false")
  1080. drawborder = false;
  1081. }
  1082. if (parts.size() >= 8) {
  1083. pressed_image_name = parts[7];
  1084. }
  1085. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1086. if(!data->explicit_size)
  1087. warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
  1088. image_name = unescape_string(image_name);
  1089. pressed_image_name = unescape_string(pressed_image_name);
  1090. std::wstring wlabel = utf8_to_wide(unescape_string(label));
  1091. FieldSpec spec(
  1092. name,
  1093. wlabel,
  1094. utf8_to_wide(image_name),
  1095. 258+m_fields.size()
  1096. );
  1097. spec.ftype = f_Button;
  1098. if(type == "image_button_exit")
  1099. spec.is_exit = true;
  1100. video::ITexture *texture = 0;
  1101. video::ITexture *pressed_texture = 0;
  1102. texture = m_tsrc->getTexture(image_name);
  1103. if (!pressed_image_name.empty())
  1104. pressed_texture = m_tsrc->getTexture(pressed_image_name);
  1105. else
  1106. pressed_texture = texture;
  1107. gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
  1108. if (spec.fname == data->focused_fieldname) {
  1109. Environment->setFocus(e);
  1110. }
  1111. e->setUseAlphaChannel(true);
  1112. e->setImage(guiScalingImageButton(
  1113. Environment->getVideoDriver(), texture, geom.X, geom.Y));
  1114. e->setPressedImage(guiScalingImageButton(
  1115. Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
  1116. e->setScaleImage(true);
  1117. e->setNotClipped(noclip);
  1118. e->setDrawBorder(drawborder);
  1119. m_fields.push_back(spec);
  1120. return;
  1121. }
  1122. errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
  1123. }
  1124. void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
  1125. {
  1126. std::vector<std::string> parts = split(element,';');
  1127. if (((parts.size() == 4) || (parts.size() == 6)) ||
  1128. ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1129. {
  1130. std::vector<std::string> v_pos = split(parts[0],',');
  1131. std::string name = parts[1];
  1132. std::vector<std::string> buttons = split(parts[2],',');
  1133. std::string str_index = parts[3];
  1134. bool show_background = true;
  1135. bool show_border = true;
  1136. int tab_index = stoi(str_index) -1;
  1137. MY_CHECKPOS("tabheader",0);
  1138. if (parts.size() == 6) {
  1139. if (parts[4] == "true")
  1140. show_background = false;
  1141. if (parts[5] == "false")
  1142. show_border = false;
  1143. }
  1144. FieldSpec spec(
  1145. name,
  1146. L"",
  1147. L"",
  1148. 258+m_fields.size()
  1149. );
  1150. spec.ftype = f_TabHeader;
  1151. v2s32 pos = pos_offset * spacing;
  1152. pos.X += stof(v_pos[0]) * (float)spacing.X;
  1153. pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
  1154. v2s32 geom;
  1155. geom.X = DesiredRect.getWidth();
  1156. geom.Y = m_btn_height*2;
  1157. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
  1158. pos.Y+geom.Y);
  1159. gui::IGUITabControl *e = Environment->addTabControl(rect, this,
  1160. show_background, show_border, spec.fid);
  1161. e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
  1162. irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
  1163. e->setTabHeight(m_btn_height*2);
  1164. if (spec.fname == data->focused_fieldname) {
  1165. Environment->setFocus(e);
  1166. }
  1167. e->setNotClipped(true);
  1168. for (const std::string &button : buttons) {
  1169. e->addTab(unescape_translate(unescape_string(
  1170. utf8_to_wide(button))).c_str(), -1);
  1171. }
  1172. if ((tab_index >= 0) &&
  1173. (buttons.size() < INT_MAX) &&
  1174. (tab_index < (int) buttons.size()))
  1175. e->setActiveTab(tab_index);
  1176. m_fields.push_back(spec);
  1177. return;
  1178. }
  1179. errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
  1180. << element << "'" << std::endl;
  1181. }
  1182. void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
  1183. {
  1184. if (m_client == 0) {
  1185. warningstream << "invalid use of item_image_button with m_client==0"
  1186. << std::endl;
  1187. return;
  1188. }
  1189. std::vector<std::string> parts = split(element,';');
  1190. if ((parts.size() == 5) ||
  1191. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1192. {
  1193. std::vector<std::string> v_pos = split(parts[0],',');
  1194. std::vector<std::string> v_geom = split(parts[1],',');
  1195. std::string item_name = parts[2];
  1196. std::string name = parts[3];
  1197. std::string label = parts[4];
  1198. label = unescape_string(label);
  1199. item_name = unescape_string(item_name);
  1200. MY_CHECKPOS("itemimagebutton",0);
  1201. MY_CHECKGEOM("itemimagebutton",1);
  1202. v2s32 pos = padding + pos_offset * spacing;
  1203. pos.X += stof(v_pos[0]) * (float)spacing.X;
  1204. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  1205. v2s32 geom;
  1206. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  1207. geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
  1208. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1209. if(!data->explicit_size)
  1210. warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
  1211. IItemDefManager *idef = m_client->idef();
  1212. ItemStack item;
  1213. item.deSerialize(item_name, idef);
  1214. m_tooltips[name] =
  1215. TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
  1216. m_default_tooltip_bgcolor,
  1217. m_default_tooltip_color);
  1218. FieldSpec spec(
  1219. name,
  1220. utf8_to_wide(label),
  1221. utf8_to_wide(item_name),
  1222. 258 + m_fields.size()
  1223. );
  1224. gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
  1225. if (spec.fname == data->focused_fieldname) {
  1226. Environment->setFocus(e);
  1227. }
  1228. spec.ftype = f_Button;
  1229. rect+=data->basepos-padding;
  1230. spec.rect=rect;
  1231. m_fields.push_back(spec);
  1232. pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  1233. pos.X += stof(v_pos[0]) * (float) spacing.X;
  1234. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  1235. m_itemimages.emplace_back("", item_name, e, pos, geom);
  1236. m_static_texts.emplace_back(utf8_to_wide(label), rect, e);
  1237. return;
  1238. }
  1239. errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
  1240. }
  1241. void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
  1242. {
  1243. std::vector<std::string> parts = split(element,';');
  1244. if ((parts.size() == 3) ||
  1245. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1246. {
  1247. std::vector<std::string> v_pos = split(parts[0],',');
  1248. std::vector<std::string> v_geom = split(parts[1],',');
  1249. MY_CHECKPOS("box",0);
  1250. MY_CHECKGEOM("box",1);
  1251. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  1252. pos.X += stof(v_pos[0]) * (float) spacing.X;
  1253. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  1254. v2s32 geom;
  1255. geom.X = stof(v_geom[0]) * (float)spacing.X;
  1256. geom.Y = stof(v_geom[1]) * (float)spacing.Y;
  1257. video::SColor tmp_color;
  1258. if (parseColorString(parts[2], tmp_color, false)) {
  1259. BoxDrawSpec spec(pos, geom, tmp_color);
  1260. m_boxes.push_back(spec);
  1261. }
  1262. else {
  1263. errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
  1264. }
  1265. return;
  1266. }
  1267. errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
  1268. }
  1269. void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
  1270. {
  1271. std::vector<std::string> parts = split(element,';');
  1272. if (((parts.size() == 1) || (parts.size() == 2)) ||
  1273. ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION))) {
  1274. parseColorString(parts[0], m_bgcolor, false);
  1275. if (parts.size() == 2) {
  1276. std::string fullscreen = parts[1];
  1277. m_bgfullscreen = is_yes(fullscreen);
  1278. }
  1279. return;
  1280. }
  1281. errorstream << "Invalid bgcolor element(" << parts.size() << "): '" << element << "'"
  1282. << std::endl;
  1283. }
  1284. void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
  1285. {
  1286. std::vector<std::string> parts = split(element,';');
  1287. if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
  1288. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1289. {
  1290. parseColorString(parts[0], m_slotbg_n, false);
  1291. parseColorString(parts[1], m_slotbg_h, false);
  1292. if (parts.size() >= 3) {
  1293. if (parseColorString(parts[2], m_slotbordercolor, false)) {
  1294. m_slotborder = true;
  1295. }
  1296. }
  1297. if (parts.size() == 5) {
  1298. video::SColor tmp_color;
  1299. if (parseColorString(parts[3], tmp_color, false))
  1300. m_default_tooltip_bgcolor = tmp_color;
  1301. if (parseColorString(parts[4], tmp_color, false))
  1302. m_default_tooltip_color = tmp_color;
  1303. }
  1304. return;
  1305. }
  1306. errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
  1307. }
  1308. void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
  1309. {
  1310. std::vector<std::string> parts = split(element,';');
  1311. if (parts.size() == 2) {
  1312. std::string name = parts[0];
  1313. m_tooltips[name] = TooltipSpec(utf8_to_wide(unescape_string(parts[1])),
  1314. m_default_tooltip_bgcolor, m_default_tooltip_color);
  1315. return;
  1316. }
  1317. if (parts.size() == 4) {
  1318. std::string name = parts[0];
  1319. video::SColor tmp_color1, tmp_color2;
  1320. if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
  1321. m_tooltips[name] = TooltipSpec(utf8_to_wide(unescape_string(parts[1])),
  1322. tmp_color1, tmp_color2);
  1323. return;
  1324. }
  1325. }
  1326. errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
  1327. }
  1328. bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
  1329. {
  1330. //some prechecks
  1331. if (data.empty())
  1332. return false;
  1333. std::vector<std::string> parts = split(data,'[');
  1334. if (parts.size() < 2) {
  1335. return false;
  1336. }
  1337. if (parts[0] != "formspec_version") {
  1338. return false;
  1339. }
  1340. if (is_number(parts[1])) {
  1341. m_formspec_version = mystoi(parts[1]);
  1342. return true;
  1343. }
  1344. return false;
  1345. }
  1346. bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
  1347. {
  1348. if (element.empty())
  1349. return false;
  1350. std::vector<std::string> parts = split(element,'[');
  1351. if (parts.size() < 2)
  1352. return false;
  1353. std::string type = trim(parts[0]);
  1354. std::string description = trim(parts[1]);
  1355. if (type != "size" && type != "invsize")
  1356. return false;
  1357. if (type == "invsize")
  1358. log_deprecated("Deprecated formspec element \"invsize\" is used");
  1359. parseSize(data, description);
  1360. return true;
  1361. }
  1362. bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
  1363. {
  1364. if (element.empty())
  1365. return false;
  1366. std::vector<std::string> parts = split(element, '[');
  1367. if (parts.size() != 2)
  1368. return false;
  1369. std::string type = trim(parts[0]);
  1370. std::string description = trim(parts[1]);
  1371. if (type != "position")
  1372. return false;
  1373. parsePosition(data, description);
  1374. return true;
  1375. }
  1376. void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
  1377. {
  1378. std::vector<std::string> parts = split(element, ',');
  1379. if (parts.size() == 2) {
  1380. data->offset.X = stof(parts[0]);
  1381. data->offset.Y = stof(parts[1]);
  1382. return;
  1383. }
  1384. errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
  1385. }
  1386. bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
  1387. {
  1388. if (element.empty())
  1389. return false;
  1390. std::vector<std::string> parts = split(element, '[');
  1391. if (parts.size() != 2)
  1392. return false;
  1393. std::string type = trim(parts[0]);
  1394. std::string description = trim(parts[1]);
  1395. if (type != "anchor")
  1396. return false;
  1397. parseAnchor(data, description);
  1398. return true;
  1399. }
  1400. void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
  1401. {
  1402. std::vector<std::string> parts = split(element, ',');
  1403. if (parts.size() == 2) {
  1404. data->anchor.X = stof(parts[0]);
  1405. data->anchor.Y = stof(parts[1]);
  1406. return;
  1407. }
  1408. errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
  1409. << "'" << std::endl;
  1410. }
  1411. void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
  1412. {
  1413. //some prechecks
  1414. if (element.empty())
  1415. return;
  1416. std::vector<std::string> parts = split(element,'[');
  1417. // ugly workaround to keep compatibility
  1418. if (parts.size() > 2) {
  1419. if (trim(parts[0]) == "image") {
  1420. for (unsigned int i=2;i< parts.size(); i++) {
  1421. parts[1] += "[" + parts[i];
  1422. }
  1423. }
  1424. else { return; }
  1425. }
  1426. if (parts.size() < 2) {
  1427. return;
  1428. }
  1429. std::string type = trim(parts[0]);
  1430. std::string description = trim(parts[1]);
  1431. if (type == "container") {
  1432. parseContainer(data, description);
  1433. return;
  1434. }
  1435. if (type == "container_end") {
  1436. parseContainerEnd(data);
  1437. return;
  1438. }
  1439. if (type == "list") {
  1440. parseList(data, description);
  1441. return;
  1442. }
  1443. if (type == "listring") {
  1444. parseListRing(data, description);
  1445. return;
  1446. }
  1447. if (type == "checkbox") {
  1448. parseCheckbox(data, description);
  1449. return;
  1450. }
  1451. if (type == "image") {
  1452. parseImage(data, description);
  1453. return;
  1454. }
  1455. if (type == "item_image") {
  1456. parseItemImage(data, description);
  1457. return;
  1458. }
  1459. if (type == "button" || type == "button_exit") {
  1460. parseButton(data, description, type);
  1461. return;
  1462. }
  1463. if (type == "background") {
  1464. parseBackground(data,description);
  1465. return;
  1466. }
  1467. if (type == "tableoptions"){
  1468. parseTableOptions(data,description);
  1469. return;
  1470. }
  1471. if (type == "tablecolumns"){
  1472. parseTableColumns(data,description);
  1473. return;
  1474. }
  1475. if (type == "table"){
  1476. parseTable(data,description);
  1477. return;
  1478. }
  1479. if (type == "textlist"){
  1480. parseTextList(data,description);
  1481. return;
  1482. }
  1483. if (type == "dropdown"){
  1484. parseDropDown(data,description);
  1485. return;
  1486. }
  1487. if (type == "field_close_on_enter") {
  1488. parseFieldCloseOnEnter(data, description);
  1489. return;
  1490. }
  1491. if (type == "pwdfield") {
  1492. parsePwdField(data,description);
  1493. return;
  1494. }
  1495. if ((type == "field") || (type == "textarea")){
  1496. parseField(data,description,type);
  1497. return;
  1498. }
  1499. if (type == "label") {
  1500. parseLabel(data,description);
  1501. return;
  1502. }
  1503. if (type == "vertlabel") {
  1504. parseVertLabel(data,description);
  1505. return;
  1506. }
  1507. if (type == "item_image_button") {
  1508. parseItemImageButton(data,description);
  1509. return;
  1510. }
  1511. if ((type == "image_button") || (type == "image_button_exit")) {
  1512. parseImageButton(data,description,type);
  1513. return;
  1514. }
  1515. if (type == "tabheader") {
  1516. parseTabHeader(data,description);
  1517. return;
  1518. }
  1519. if (type == "box") {
  1520. parseBox(data,description);
  1521. return;
  1522. }
  1523. if (type == "bgcolor") {
  1524. parseBackgroundColor(data,description);
  1525. return;
  1526. }
  1527. if (type == "listcolors") {
  1528. parseListColors(data,description);
  1529. return;
  1530. }
  1531. if (type == "tooltip") {
  1532. parseTooltip(data,description);
  1533. return;
  1534. }
  1535. if (type == "scrollbar") {
  1536. parseScrollBar(data, description);
  1537. return;
  1538. }
  1539. // Ignore others
  1540. infostream << "Unknown DrawSpec: type=" << type << ", data=\"" << description << "\""
  1541. << std::endl;
  1542. }
  1543. void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
  1544. {
  1545. /* useless to regenerate without a screensize */
  1546. if ((screensize.X <= 0) || (screensize.Y <= 0)) {
  1547. return;
  1548. }
  1549. parserData mydata;
  1550. //preserve tables
  1551. for (auto &m_table : m_tables) {
  1552. std::string tablename = m_table.first.fname;
  1553. GUITable *table = m_table.second;
  1554. mydata.table_dyndata[tablename] = table->getDynamicData();
  1555. }
  1556. //set focus
  1557. if (!m_focused_element.empty())
  1558. mydata.focused_fieldname = m_focused_element;
  1559. //preserve focus
  1560. gui::IGUIElement *focused_element = Environment->getFocus();
  1561. if (focused_element && focused_element->getParent() == this) {
  1562. s32 focused_id = focused_element->getID();
  1563. if (focused_id > 257) {
  1564. for (const GUIFormSpecMenu::FieldSpec &field : m_fields) {
  1565. if (field.fid == focused_id) {
  1566. mydata.focused_fieldname = field.fname;
  1567. break;
  1568. }
  1569. }
  1570. }
  1571. }
  1572. // Remove children
  1573. removeChildren();
  1574. for (auto &table_it : m_tables) {
  1575. table_it.second->drop();
  1576. }
  1577. mydata.size= v2s32(100,100);
  1578. mydata.screensize = screensize;
  1579. mydata.offset = v2f32(0.5f, 0.5f);
  1580. mydata.anchor = v2f32(0.5f, 0.5f);
  1581. // Base position of contents of form
  1582. mydata.basepos = getBasePos();
  1583. /* Convert m_init_draw_spec to m_inventorylists */
  1584. m_inventorylists.clear();
  1585. m_images.clear();
  1586. m_backgrounds.clear();
  1587. m_itemimages.clear();
  1588. m_tables.clear();
  1589. m_checkboxes.clear();
  1590. m_scrollbars.clear();
  1591. m_fields.clear();
  1592. m_boxes.clear();
  1593. m_tooltips.clear();
  1594. m_inventory_rings.clear();
  1595. m_static_texts.clear();
  1596. m_dropdowns.clear();
  1597. m_bgfullscreen = false;
  1598. {
  1599. v3f formspec_bgcolor = g_settings->getV3F("formspec_default_bg_color");
  1600. m_bgcolor = video::SColor(
  1601. (u8) clamp_u8(g_settings->getS32("formspec_default_bg_opacity")),
  1602. clamp_u8(myround(formspec_bgcolor.X)),
  1603. clamp_u8(myround(formspec_bgcolor.Y)),
  1604. clamp_u8(myround(formspec_bgcolor.Z))
  1605. );
  1606. }
  1607. {
  1608. v3f formspec_bgcolor = g_settings->getV3F("formspec_fullscreen_bg_color");
  1609. m_fullscreen_bgcolor = video::SColor(
  1610. (u8) clamp_u8(g_settings->getS32("formspec_fullscreen_bg_opacity")),
  1611. clamp_u8(myround(formspec_bgcolor.X)),
  1612. clamp_u8(myround(formspec_bgcolor.Y)),
  1613. clamp_u8(myround(formspec_bgcolor.Z))
  1614. );
  1615. }
  1616. m_slotbg_n = video::SColor(255,128,128,128);
  1617. m_slotbg_h = video::SColor(255,192,192,192);
  1618. m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
  1619. m_default_tooltip_color = video::SColor(255,255,255,255);
  1620. m_slotbordercolor = video::SColor(200,0,0,0);
  1621. m_slotborder = false;
  1622. // Add tooltip
  1623. {
  1624. assert(!m_tooltip_element);
  1625. // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
  1626. m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
  1627. m_tooltip_element->enableOverrideColor(true);
  1628. m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
  1629. m_tooltip_element->setDrawBackground(true);
  1630. m_tooltip_element->setDrawBorder(true);
  1631. m_tooltip_element->setOverrideColor(m_default_tooltip_color);
  1632. m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
  1633. m_tooltip_element->setWordWrap(false);
  1634. //we're not parent so no autograb for this one!
  1635. m_tooltip_element->grab();
  1636. }
  1637. std::vector<std::string> elements = split(m_formspec_string,']');
  1638. unsigned int i = 0;
  1639. /* try to read version from first element only */
  1640. if (!elements.empty()) {
  1641. if ( parseVersionDirect(elements[0]) ) {
  1642. i++;
  1643. }
  1644. }
  1645. /* we need size first in order to calculate image scale */
  1646. mydata.explicit_size = false;
  1647. for (; i< elements.size(); i++) {
  1648. if (!parseSizeDirect(&mydata, elements[i])) {
  1649. break;
  1650. }
  1651. }
  1652. /* "position" element is always after "size" element if it used */
  1653. for (; i< elements.size(); i++) {
  1654. if (!parsePositionDirect(&mydata, elements[i])) {
  1655. break;
  1656. }
  1657. }
  1658. /* "anchor" element is always after "position" (or "size" element) if it used */
  1659. for (; i< elements.size(); i++) {
  1660. if (!parseAnchorDirect(&mydata, elements[i])) {
  1661. break;
  1662. }
  1663. }
  1664. if (mydata.explicit_size) {
  1665. // compute scaling for specified form size
  1666. if (m_lock) {
  1667. v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
  1668. v2u32 delta = current_screensize - m_lockscreensize;
  1669. if (current_screensize.Y > m_lockscreensize.Y)
  1670. delta.Y /= 2;
  1671. else
  1672. delta.Y = 0;
  1673. if (current_screensize.X > m_lockscreensize.X)
  1674. delta.X /= 2;
  1675. else
  1676. delta.X = 0;
  1677. offset = v2s32(delta.X,delta.Y);
  1678. mydata.screensize = m_lockscreensize;
  1679. } else {
  1680. offset = v2s32(0,0);
  1681. }
  1682. double gui_scaling = g_settings->getFloat("gui_scaling");
  1683. double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
  1684. double use_imgsize;
  1685. if (m_lock) {
  1686. // In fixed-size mode, inventory image size
  1687. // is 0.53 inch multiplied by the gui_scaling
  1688. // config parameter. This magic size is chosen
  1689. // to make the main menu (15.5 inventory images
  1690. // wide, including border) just fit into the
  1691. // default window (800 pixels wide) at 96 DPI
  1692. // and default scaling (1.00).
  1693. use_imgsize = 0.5555 * screen_dpi * gui_scaling;
  1694. } else {
  1695. // In variable-size mode, we prefer to make the
  1696. // inventory image size 1/15 of screen height,
  1697. // multiplied by the gui_scaling config parameter.
  1698. // If the preferred size won't fit the whole
  1699. // form on the screen, either horizontally or
  1700. // vertically, then we scale it down to fit.
  1701. // (The magic numbers in the computation of what
  1702. // fits arise from the scaling factors in the
  1703. // following stanza, including the form border,
  1704. // help text space, and 0.1 inventory slot spare.)
  1705. // However, a minimum size is also set, that
  1706. // the image size can't be less than 0.3 inch
  1707. // multiplied by gui_scaling, even if this means
  1708. // the form doesn't fit the screen.
  1709. double prefer_imgsize = mydata.screensize.Y / 15 *
  1710. gui_scaling;
  1711. double fitx_imgsize = mydata.screensize.X /
  1712. ((5.0/4.0) * (0.5 + mydata.invsize.X));
  1713. double fity_imgsize = mydata.screensize.Y /
  1714. ((15.0/13.0) * (0.85 * mydata.invsize.Y));
  1715. double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
  1716. double min_imgsize = 0.3 * screen_dpi * gui_scaling;
  1717. use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
  1718. MYMIN(fitx_imgsize, fity_imgsize)));
  1719. }
  1720. // Everything else is scaled in proportion to the
  1721. // inventory image size. The inventory slot spacing
  1722. // is 5/4 image size horizontally and 15/13 image size
  1723. // vertically. The padding around the form (incorporating
  1724. // the border of the outer inventory slots) is 3/8
  1725. // image size. Font height (baseline to baseline)
  1726. // is 2/5 vertical inventory slot spacing, and button
  1727. // half-height is 7/8 of font height.
  1728. imgsize = v2s32(use_imgsize, use_imgsize);
  1729. spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
  1730. padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
  1731. m_btn_height = use_imgsize*15.0/13 * 0.35;
  1732. m_font = g_fontengine->getFont();
  1733. mydata.size = v2s32(
  1734. padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
  1735. padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
  1736. );
  1737. DesiredRect = mydata.rect = core::rect<s32>(
  1738. (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
  1739. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
  1740. (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
  1741. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
  1742. );
  1743. } else {
  1744. // Non-size[] form must consist only of text fields and
  1745. // implicit "Proceed" button. Use default font, and
  1746. // temporary form size which will be recalculated below.
  1747. m_font = g_fontengine->getFont();
  1748. m_btn_height = font_line_height(m_font) * 0.875;
  1749. DesiredRect = core::rect<s32>(
  1750. (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
  1751. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
  1752. (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
  1753. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
  1754. );
  1755. }
  1756. recalculateAbsolutePosition(false);
  1757. mydata.basepos = getBasePos();
  1758. m_tooltip_element->setOverrideFont(m_font);
  1759. gui::IGUISkin* skin = Environment->getSkin();
  1760. sanity_check(skin);
  1761. gui::IGUIFont *old_font = skin->getFont();
  1762. skin->setFont(m_font);
  1763. pos_offset = v2s32();
  1764. for (; i< elements.size(); i++) {
  1765. parseElement(&mydata, elements[i]);
  1766. }
  1767. if (!container_stack.empty()) {
  1768. errorstream << "Invalid formspec string: container was never closed!"
  1769. << std::endl;
  1770. }
  1771. // If there are fields without explicit size[], add a "Proceed"
  1772. // button and adjust size to fit all the fields.
  1773. if (!m_fields.empty() && !mydata.explicit_size) {
  1774. mydata.rect = core::rect<s32>(
  1775. mydata.screensize.X/2 - 580/2,
  1776. mydata.screensize.Y/2 - 300/2,
  1777. mydata.screensize.X/2 + 580/2,
  1778. mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
  1779. );
  1780. DesiredRect = mydata.rect;
  1781. recalculateAbsolutePosition(false);
  1782. mydata.basepos = getBasePos();
  1783. {
  1784. v2s32 pos = mydata.basepos;
  1785. pos.Y = ((m_fields.size()+2)*60);
  1786. v2s32 size = DesiredRect.getSize();
  1787. mydata.rect =
  1788. core::rect<s32>(size.X/2-70, pos.Y,
  1789. (size.X/2-70)+140, pos.Y + (m_btn_height*2));
  1790. const wchar_t *text = wgettext("Proceed");
  1791. Environment->addButton(mydata.rect, this, 257, text);
  1792. delete[] text;
  1793. }
  1794. }
  1795. //set initial focus if parser didn't set it
  1796. focused_element = Environment->getFocus();
  1797. if (!focused_element
  1798. || !isMyChild(focused_element)
  1799. || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
  1800. setInitialFocus();
  1801. skin->setFont(old_font);
  1802. }
  1803. #ifdef __ANDROID__
  1804. bool GUIFormSpecMenu::getAndroidUIInput()
  1805. {
  1806. /* no dialog shown */
  1807. if (m_JavaDialogFieldName == "") {
  1808. return false;
  1809. }
  1810. /* still waiting */
  1811. if (porting::getInputDialogState() == -1) {
  1812. return true;
  1813. }
  1814. std::string fieldname = m_JavaDialogFieldName;
  1815. m_JavaDialogFieldName = "";
  1816. /* no value abort dialog processing */
  1817. if (porting::getInputDialogState() != 0) {
  1818. return false;
  1819. }
  1820. for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
  1821. iter != m_fields.end(); ++iter) {
  1822. if (iter->fname != fieldname) {
  1823. continue;
  1824. }
  1825. IGUIElement* tochange = getElementFromId(iter->fid);
  1826. if (tochange == 0) {
  1827. return false;
  1828. }
  1829. if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
  1830. return false;
  1831. }
  1832. std::string text = porting::getInputDialogValue();
  1833. ((gui::IGUIEditBox*) tochange)->
  1834. setText(utf8_to_wide(text).c_str());
  1835. }
  1836. return false;
  1837. }
  1838. #endif
  1839. GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
  1840. {
  1841. core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
  1842. for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
  1843. for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
  1844. s32 item_i = i + s.start_item_i;
  1845. s32 x = (i%s.geom.X) * spacing.X;
  1846. s32 y = (i/s.geom.X) * spacing.Y;
  1847. v2s32 p0(x,y);
  1848. core::rect<s32> rect = imgrect + s.pos + p0;
  1849. if(rect.isPointInside(p))
  1850. {
  1851. return ItemSpec(s.inventoryloc, s.listname, item_i);
  1852. }
  1853. }
  1854. }
  1855. return ItemSpec(InventoryLocation(), "", -1);
  1856. }
  1857. void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
  1858. bool &item_hovered)
  1859. {
  1860. video::IVideoDriver* driver = Environment->getVideoDriver();
  1861. Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
  1862. if(!inv){
  1863. warningstream<<"GUIFormSpecMenu::drawList(): "
  1864. <<"The inventory location "
  1865. <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
  1866. <<std::endl;
  1867. return;
  1868. }
  1869. InventoryList *ilist = inv->getList(s.listname);
  1870. if(!ilist){
  1871. warningstream<<"GUIFormSpecMenu::drawList(): "
  1872. <<"The inventory list \""<<s.listname<<"\" @ \""
  1873. <<s.inventoryloc.dump()<<"\" doesn't exist"
  1874. <<std::endl;
  1875. return;
  1876. }
  1877. core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
  1878. for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
  1879. {
  1880. s32 item_i = i + s.start_item_i;
  1881. if(item_i >= (s32) ilist->getSize())
  1882. break;
  1883. s32 x = (i%s.geom.X) * spacing.X;
  1884. s32 y = (i/s.geom.X) * spacing.Y;
  1885. v2s32 p(x,y);
  1886. core::rect<s32> rect = imgrect + s.pos + p;
  1887. ItemStack item;
  1888. if(ilist)
  1889. item = ilist->getItem(item_i);
  1890. bool selected = m_selected_item
  1891. && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
  1892. && m_selected_item->listname == s.listname
  1893. && m_selected_item->i == item_i;
  1894. bool hovering = rect.isPointInside(m_pointer);
  1895. ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
  1896. (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
  1897. if (phase == 0) {
  1898. if (hovering) {
  1899. item_hovered = true;
  1900. driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
  1901. } else {
  1902. driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
  1903. }
  1904. }
  1905. //Draw inv slot borders
  1906. if (m_slotborder) {
  1907. s32 x1 = rect.UpperLeftCorner.X;
  1908. s32 y1 = rect.UpperLeftCorner.Y;
  1909. s32 x2 = rect.LowerRightCorner.X;
  1910. s32 y2 = rect.LowerRightCorner.Y;
  1911. s32 border = 1;
  1912. driver->draw2DRectangle(m_slotbordercolor,
  1913. core::rect<s32>(v2s32(x1 - border, y1 - border),
  1914. v2s32(x2 + border, y1)), NULL);
  1915. driver->draw2DRectangle(m_slotbordercolor,
  1916. core::rect<s32>(v2s32(x1 - border, y2),
  1917. v2s32(x2 + border, y2 + border)), NULL);
  1918. driver->draw2DRectangle(m_slotbordercolor,
  1919. core::rect<s32>(v2s32(x1 - border, y1),
  1920. v2s32(x1, y2)), NULL);
  1921. driver->draw2DRectangle(m_slotbordercolor,
  1922. core::rect<s32>(v2s32(x2, y1),
  1923. v2s32(x2 + border, y2)), NULL);
  1924. }
  1925. if(phase == 1)
  1926. {
  1927. // Draw item stack
  1928. if(selected)
  1929. {
  1930. item.takeItem(m_selected_amount);
  1931. }
  1932. if(!item.empty())
  1933. {
  1934. drawItemStack(driver, m_font, item,
  1935. rect, &AbsoluteClippingRect, m_client,
  1936. rotation_kind);
  1937. }
  1938. // Draw tooltip
  1939. std::wstring tooltip_text;
  1940. if (hovering && !m_selected_item) {
  1941. const std::string &desc = item.metadata.getString("description");
  1942. if (desc.empty())
  1943. tooltip_text =
  1944. utf8_to_wide(item.getDefinition(m_client->idef()).description);
  1945. else
  1946. tooltip_text = utf8_to_wide(desc);
  1947. if (!item.name.empty()) {
  1948. if (tooltip_text.empty())
  1949. tooltip_text = utf8_to_wide(item.name);
  1950. if (m_tooltip_append_itemname)
  1951. tooltip_text += utf8_to_wide(" [" + item.name + "]");
  1952. }
  1953. }
  1954. if (!tooltip_text.empty()) {
  1955. showTooltip(tooltip_text, m_default_tooltip_color,
  1956. m_default_tooltip_bgcolor);
  1957. }
  1958. }
  1959. }
  1960. }
  1961. void GUIFormSpecMenu::drawSelectedItem()
  1962. {
  1963. video::IVideoDriver* driver = Environment->getVideoDriver();
  1964. if (!m_selected_item) {
  1965. drawItemStack(driver, m_font, ItemStack(),
  1966. core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
  1967. NULL, m_client, IT_ROT_DRAGGED);
  1968. return;
  1969. }
  1970. Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
  1971. sanity_check(inv);
  1972. InventoryList *list = inv->getList(m_selected_item->listname);
  1973. sanity_check(list);
  1974. ItemStack stack = list->getItem(m_selected_item->i);
  1975. stack.count = m_selected_amount;
  1976. core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
  1977. core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
  1978. rect.constrainTo(driver->getViewPort());
  1979. drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
  1980. }
  1981. void GUIFormSpecMenu::drawMenu()
  1982. {
  1983. if (m_form_src) {
  1984. const std::string &newform = m_form_src->getForm();
  1985. if (newform != m_formspec_string) {
  1986. m_formspec_string = newform;
  1987. regenerateGui(m_screensize_old);
  1988. }
  1989. }
  1990. gui::IGUISkin* skin = Environment->getSkin();
  1991. sanity_check(skin != NULL);
  1992. gui::IGUIFont *old_font = skin->getFont();
  1993. skin->setFont(m_font);
  1994. updateSelectedItem();
  1995. video::IVideoDriver* driver = Environment->getVideoDriver();
  1996. v2u32 screenSize = driver->getScreenSize();
  1997. core::rect<s32> allbg(0, 0, screenSize.X, screenSize.Y);
  1998. if (m_bgfullscreen)
  1999. driver->draw2DRectangle(m_fullscreen_bgcolor, allbg, &allbg);
  2000. else
  2001. driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
  2002. m_tooltip_element->setVisible(false);
  2003. /*
  2004. Draw backgrounds
  2005. */
  2006. for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_backgrounds) {
  2007. video::ITexture *texture = m_tsrc->getTexture(spec.name);
  2008. if (texture != 0) {
  2009. // Image size on screen
  2010. core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
  2011. // Image rectangle on screen
  2012. core::rect<s32> rect = imgrect + spec.pos;
  2013. if (spec.clip) {
  2014. core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
  2015. rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
  2016. AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
  2017. AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
  2018. AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
  2019. }
  2020. const video::SColor color(255,255,255,255);
  2021. const video::SColor colors[] = {color,color,color,color};
  2022. draw2DImageFilterScaled(driver, texture, rect,
  2023. core::rect<s32>(core::position2d<s32>(0,0),
  2024. core::dimension2di(texture->getOriginalSize())),
  2025. NULL/*&AbsoluteClippingRect*/, colors, true);
  2026. } else {
  2027. errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
  2028. errorstream << "\t" << spec.name << std::endl;
  2029. }
  2030. }
  2031. /*
  2032. Draw Boxes
  2033. */
  2034. for (const GUIFormSpecMenu::BoxDrawSpec &spec : m_boxes) {
  2035. irr::video::SColor todraw = spec.color;
  2036. todraw.setAlpha(140);
  2037. core::rect<s32> rect(spec.pos.X,spec.pos.Y,
  2038. spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
  2039. driver->draw2DRectangle(todraw, rect, 0);
  2040. }
  2041. /*
  2042. Call base class
  2043. */
  2044. gui::IGUIElement::draw();
  2045. /*
  2046. Draw images
  2047. */
  2048. for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_images) {
  2049. video::ITexture *texture = m_tsrc->getTexture(spec.name);
  2050. if (texture != 0) {
  2051. const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
  2052. // Image size on screen
  2053. core::rect<s32> imgrect;
  2054. if (spec.scale)
  2055. imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
  2056. else {
  2057. imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
  2058. }
  2059. // Image rectangle on screen
  2060. core::rect<s32> rect = imgrect + spec.pos;
  2061. const video::SColor color(255,255,255,255);
  2062. const video::SColor colors[] = {color,color,color,color};
  2063. draw2DImageFilterScaled(driver, texture, rect,
  2064. core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
  2065. NULL/*&AbsoluteClippingRect*/, colors, true);
  2066. }
  2067. else {
  2068. errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
  2069. errorstream << "\t" << spec.name << std::endl;
  2070. }
  2071. }
  2072. /*
  2073. Draw item images
  2074. */
  2075. for (const GUIFormSpecMenu::ImageDrawSpec &spec : m_itemimages) {
  2076. if (m_client == 0)
  2077. break;
  2078. IItemDefManager *idef = m_client->idef();
  2079. ItemStack item;
  2080. item.deSerialize(spec.item_name, idef);
  2081. core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
  2082. // Viewport rectangle on screen
  2083. core::rect<s32> rect = imgrect + spec.pos;
  2084. if (spec.parent_button && spec.parent_button->isPressed()) {
  2085. #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
  2086. rect += core::dimension2d<s32>(
  2087. 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
  2088. #else
  2089. rect += core::dimension2d<s32>(
  2090. skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
  2091. skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
  2092. #endif
  2093. }
  2094. drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
  2095. m_client, IT_ROT_NONE);
  2096. }
  2097. /*
  2098. Draw items
  2099. Phase 0: Item slot rectangles
  2100. Phase 1: Item images; prepare tooltip
  2101. */
  2102. bool item_hovered = false;
  2103. int start_phase = 0;
  2104. for (int phase = start_phase; phase <= 1; phase++) {
  2105. for (const GUIFormSpecMenu::ListDrawSpec &spec : m_inventorylists) {
  2106. drawList(spec, phase, item_hovered);
  2107. }
  2108. }
  2109. if (!item_hovered) {
  2110. drawItemStack(driver, m_font, ItemStack(),
  2111. core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
  2112. NULL, m_client, IT_ROT_HOVERED);
  2113. }
  2114. /* TODO find way to show tooltips on touchscreen */
  2115. #ifndef HAVE_TOUCHSCREENGUI
  2116. m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
  2117. #endif
  2118. /*
  2119. Draw static text elements
  2120. */
  2121. for (const GUIFormSpecMenu::StaticTextSpec &spec : m_static_texts) {
  2122. core::rect<s32> rect = spec.rect;
  2123. if (spec.parent_button && spec.parent_button->isPressed()) {
  2124. #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
  2125. rect += core::dimension2d<s32>(
  2126. 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
  2127. #else
  2128. // Use image offset instead of text's because its a bit smaller
  2129. // and fits better, also TEXT_OFFSET_X is always 0
  2130. rect += core::dimension2d<s32>(
  2131. skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
  2132. skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
  2133. #endif
  2134. }
  2135. video::SColor color(255, 255, 255, 255);
  2136. m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
  2137. }
  2138. /*
  2139. Draw fields/buttons tooltips
  2140. */
  2141. gui::IGUIElement *hovered =
  2142. Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
  2143. if (hovered != NULL) {
  2144. s32 id = hovered->getID();
  2145. u64 delta = 0;
  2146. if (id == -1) {
  2147. m_old_tooltip_id = id;
  2148. } else {
  2149. if (id == m_old_tooltip_id) {
  2150. delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
  2151. } else {
  2152. m_hovered_time = porting::getTimeMs();
  2153. m_old_tooltip_id = id;
  2154. }
  2155. }
  2156. // Find and update the current tooltip
  2157. if (id != -1 && delta >= m_tooltip_show_delay) {
  2158. for (const FieldSpec &field : m_fields) {
  2159. if (field.fid != id)
  2160. continue;
  2161. const std::wstring &text = m_tooltips[field.fname].tooltip;
  2162. if (!text.empty())
  2163. showTooltip(text, m_tooltips[field.fname].color,
  2164. m_tooltips[field.fname].bgcolor);
  2165. break;
  2166. }
  2167. }
  2168. }
  2169. m_tooltip_element->draw();
  2170. /*
  2171. Draw dragged item stack
  2172. */
  2173. drawSelectedItem();
  2174. skin->setFont(old_font);
  2175. }
  2176. void GUIFormSpecMenu::showTooltip(const std::wstring &text,
  2177. const irr::video::SColor &color, const irr::video::SColor &bgcolor)
  2178. {
  2179. const std::wstring ntext = translate_string(text);
  2180. m_tooltip_element->setOverrideColor(color);
  2181. m_tooltip_element->setBackgroundColor(bgcolor);
  2182. setStaticText(m_tooltip_element, ntext.c_str());
  2183. // Tooltip size and offset
  2184. s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
  2185. #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
  2186. std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
  2187. s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
  2188. #else
  2189. s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
  2190. #endif
  2191. v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
  2192. int tooltip_offset_x = m_btn_height;
  2193. int tooltip_offset_y = m_btn_height;
  2194. #ifdef __ANDROID__
  2195. tooltip_offset_x *= 3;
  2196. tooltip_offset_y = 0;
  2197. if (m_pointer.X > (s32)screenSize.X / 2)
  2198. tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
  2199. #endif
  2200. // Calculate and set the tooltip position
  2201. s32 tooltip_x = m_pointer.X + tooltip_offset_x;
  2202. s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
  2203. if (tooltip_x + tooltip_width > (s32)screenSize.X)
  2204. tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
  2205. if (tooltip_y + tooltip_height > (s32)screenSize.Y)
  2206. tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
  2207. m_tooltip_element->setRelativePosition(
  2208. core::rect<s32>(
  2209. core::position2d<s32>(tooltip_x, tooltip_y),
  2210. core::dimension2d<s32>(tooltip_width, tooltip_height)
  2211. )
  2212. );
  2213. // Display the tooltip
  2214. m_tooltip_element->setVisible(true);
  2215. bringToFront(m_tooltip_element);
  2216. }
  2217. void GUIFormSpecMenu::updateSelectedItem()
  2218. {
  2219. // If the selected stack has become empty for some reason, deselect it.
  2220. // If the selected stack has become inaccessible, deselect it.
  2221. // If the selected stack has become smaller, adjust m_selected_amount.
  2222. ItemStack selected = verifySelectedItem();
  2223. // WARNING: BLACK MAGIC
  2224. // See if there is a stack suited for our current guess.
  2225. // If such stack does not exist, clear the guess.
  2226. if (!m_selected_content_guess.name.empty() &&
  2227. selected.name == m_selected_content_guess.name &&
  2228. selected.count == m_selected_content_guess.count){
  2229. // Selected item fits the guess. Skip the black magic.
  2230. } else if (!m_selected_content_guess.name.empty()) {
  2231. bool found = false;
  2232. for(u32 i=0; i<m_inventorylists.size() && !found; i++){
  2233. const ListDrawSpec &s = m_inventorylists[i];
  2234. Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
  2235. if(!inv)
  2236. continue;
  2237. InventoryList *list = inv->getList(s.listname);
  2238. if(!list)
  2239. continue;
  2240. for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
  2241. u32 item_i = i + s.start_item_i;
  2242. if(item_i >= list->getSize())
  2243. continue;
  2244. ItemStack stack = list->getItem(item_i);
  2245. if(stack.name == m_selected_content_guess.name &&
  2246. stack.count == m_selected_content_guess.count){
  2247. found = true;
  2248. infostream<<"Client: Changing selected content guess to "
  2249. <<s.inventoryloc.dump()<<" "<<s.listname
  2250. <<" "<<item_i<<std::endl;
  2251. delete m_selected_item;
  2252. m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
  2253. m_selected_amount = stack.count;
  2254. }
  2255. }
  2256. }
  2257. if(!found){
  2258. infostream<<"Client: Discarding selected content guess: "
  2259. <<m_selected_content_guess.getItemString()<<std::endl;
  2260. m_selected_content_guess.name = "";
  2261. }
  2262. }
  2263. // If craftresult is nonempty and nothing else is selected, select it now.
  2264. if(!m_selected_item)
  2265. {
  2266. for (const GUIFormSpecMenu::ListDrawSpec &s : m_inventorylists) {
  2267. if(s.listname == "craftpreview")
  2268. {
  2269. Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
  2270. InventoryList *list = inv->getList("craftresult");
  2271. if(list && list->getSize() >= 1 && !list->getItem(0).empty())
  2272. {
  2273. m_selected_item = new ItemSpec;
  2274. m_selected_item->inventoryloc = s.inventoryloc;
  2275. m_selected_item->listname = "craftresult";
  2276. m_selected_item->i = 0;
  2277. m_selected_amount = 0;
  2278. m_selected_dragging = false;
  2279. break;
  2280. }
  2281. }
  2282. }
  2283. }
  2284. // If craftresult is selected, keep the whole stack selected
  2285. if(m_selected_item && m_selected_item->listname == "craftresult")
  2286. {
  2287. m_selected_amount = verifySelectedItem().count;
  2288. }
  2289. }
  2290. ItemStack GUIFormSpecMenu::verifySelectedItem()
  2291. {
  2292. // If the selected stack has become empty for some reason, deselect it.
  2293. // If the selected stack has become inaccessible, deselect it.
  2294. // If the selected stack has become smaller, adjust m_selected_amount.
  2295. // Return the selected stack.
  2296. if(m_selected_item)
  2297. {
  2298. if(m_selected_item->isValid())
  2299. {
  2300. Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
  2301. if(inv)
  2302. {
  2303. InventoryList *list = inv->getList(m_selected_item->listname);
  2304. if(list && (u32) m_selected_item->i < list->getSize())
  2305. {
  2306. ItemStack stack = list->getItem(m_selected_item->i);
  2307. if(m_selected_amount > stack.count)
  2308. m_selected_amount = stack.count;
  2309. if(!stack.empty())
  2310. return stack;
  2311. }
  2312. }
  2313. }
  2314. // selection was not valid
  2315. delete m_selected_item;
  2316. m_selected_item = NULL;
  2317. m_selected_amount = 0;
  2318. m_selected_dragging = false;
  2319. }
  2320. return ItemStack();
  2321. }
  2322. void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
  2323. {
  2324. if(m_text_dst)
  2325. {
  2326. StringMap fields;
  2327. if (quitmode == quit_mode_accept) {
  2328. fields["quit"] = "true";
  2329. }
  2330. if (quitmode == quit_mode_cancel) {
  2331. fields["quit"] = "true";
  2332. m_text_dst->gotText(fields);
  2333. return;
  2334. }
  2335. if (current_keys_pending.key_down) {
  2336. fields["key_down"] = "true";
  2337. current_keys_pending.key_down = false;
  2338. }
  2339. if (current_keys_pending.key_up) {
  2340. fields["key_up"] = "true";
  2341. current_keys_pending.key_up = false;
  2342. }
  2343. if (current_keys_pending.key_enter) {
  2344. fields["key_enter"] = "true";
  2345. current_keys_pending.key_enter = false;
  2346. }
  2347. if (!current_field_enter_pending.empty()) {
  2348. fields["key_enter_field"] = current_field_enter_pending;
  2349. current_field_enter_pending = "";
  2350. }
  2351. if (current_keys_pending.key_escape) {
  2352. fields["key_escape"] = "true";
  2353. current_keys_pending.key_escape = false;
  2354. }
  2355. for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
  2356. if(s.send) {
  2357. std::string name = s.fname;
  2358. if (s.ftype == f_Button) {
  2359. fields[name] = wide_to_utf8(s.flabel);
  2360. } else if (s.ftype == f_Table) {
  2361. GUITable *table = getTable(s.fname);
  2362. if (table) {
  2363. fields[name] = table->checkEvent();
  2364. }
  2365. }
  2366. else if(s.ftype == f_DropDown) {
  2367. // no dynamic cast possible due to some distributions shipped
  2368. // without rtti support in irrlicht
  2369. IGUIElement * element = getElementFromId(s.fid);
  2370. gui::IGUIComboBox *e = NULL;
  2371. if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
  2372. e = static_cast<gui::IGUIComboBox*>(element);
  2373. }
  2374. s32 selected = e->getSelected();
  2375. if (selected >= 0) {
  2376. std::vector<std::string> *dropdown_values =
  2377. getDropDownValues(s.fname);
  2378. if (dropdown_values && selected < (s32)dropdown_values->size()) {
  2379. fields[name] = (*dropdown_values)[selected];
  2380. }
  2381. }
  2382. }
  2383. else if (s.ftype == f_TabHeader) {
  2384. // no dynamic cast possible due to some distributions shipped
  2385. // without rttzi support in irrlicht
  2386. IGUIElement * element = getElementFromId(s.fid);
  2387. gui::IGUITabControl *e = NULL;
  2388. if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
  2389. e = static_cast<gui::IGUITabControl *>(element);
  2390. }
  2391. if (e != 0) {
  2392. std::stringstream ss;
  2393. ss << (e->getActiveTab() +1);
  2394. fields[name] = ss.str();
  2395. }
  2396. }
  2397. else if (s.ftype == f_CheckBox) {
  2398. // no dynamic cast possible due to some distributions shipped
  2399. // without rtti support in irrlicht
  2400. IGUIElement * element = getElementFromId(s.fid);
  2401. gui::IGUICheckBox *e = NULL;
  2402. if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
  2403. e = static_cast<gui::IGUICheckBox*>(element);
  2404. }
  2405. if (e != 0) {
  2406. if (e->isChecked())
  2407. fields[name] = "true";
  2408. else
  2409. fields[name] = "false";
  2410. }
  2411. }
  2412. else if (s.ftype == f_ScrollBar) {
  2413. // no dynamic cast possible due to some distributions shipped
  2414. // without rtti support in irrlicht
  2415. IGUIElement * element = getElementFromId(s.fid);
  2416. gui::IGUIScrollBar *e = NULL;
  2417. if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
  2418. e = static_cast<gui::IGUIScrollBar*>(element);
  2419. }
  2420. if (e != 0) {
  2421. std::stringstream os;
  2422. os << e->getPos();
  2423. if (s.fdefault == L"Changed")
  2424. fields[name] = "CHG:" + os.str();
  2425. else
  2426. fields[name] = "VAL:" + os.str();
  2427. }
  2428. }
  2429. else
  2430. {
  2431. IGUIElement* e = getElementFromId(s.fid);
  2432. if(e != NULL) {
  2433. fields[name] = wide_to_utf8(e->getText());
  2434. }
  2435. }
  2436. }
  2437. }
  2438. m_text_dst->gotText(fields);
  2439. }
  2440. }
  2441. static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
  2442. {
  2443. while(tocheck != NULL) {
  2444. if (tocheck == parent) {
  2445. return true;
  2446. }
  2447. tocheck = tocheck->getParent();
  2448. }
  2449. return false;
  2450. }
  2451. bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
  2452. {
  2453. // The IGUITabControl renders visually using the skin's selected
  2454. // font, which we override for the duration of form drawing,
  2455. // but computes tab hotspots based on how it would have rendered
  2456. // using the font that is selected at the time of button release.
  2457. // To make these two consistent, temporarily override the skin's
  2458. // font while the IGUITabControl is processing the event.
  2459. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  2460. event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
  2461. s32 x = event.MouseInput.X;
  2462. s32 y = event.MouseInput.Y;
  2463. gui::IGUIElement *hovered =
  2464. Environment->getRootGUIElement()->getElementFromPoint(
  2465. core::position2d<s32>(x, y));
  2466. if (hovered && isMyChild(hovered) &&
  2467. hovered->getType() == gui::EGUIET_TAB_CONTROL) {
  2468. gui::IGUISkin* skin = Environment->getSkin();
  2469. sanity_check(skin != NULL);
  2470. gui::IGUIFont *old_font = skin->getFont();
  2471. skin->setFont(m_font);
  2472. bool retval = hovered->OnEvent(event);
  2473. skin->setFont(old_font);
  2474. return retval;
  2475. }
  2476. }
  2477. // Fix Esc/Return key being eaten by checkboxen and tables
  2478. if(event.EventType==EET_KEY_INPUT_EVENT) {
  2479. KeyPress kp(event.KeyInput);
  2480. if (kp == EscapeKey || kp == CancelKey
  2481. || kp == getKeySetting("keymap_inventory")
  2482. || event.KeyInput.Key==KEY_RETURN) {
  2483. gui::IGUIElement *focused = Environment->getFocus();
  2484. if (focused && isMyChild(focused) &&
  2485. (focused->getType() == gui::EGUIET_LIST_BOX ||
  2486. focused->getType() == gui::EGUIET_CHECK_BOX)) {
  2487. OnEvent(event);
  2488. return true;
  2489. }
  2490. }
  2491. }
  2492. // Mouse wheel events: send to hovered element instead of focused
  2493. if(event.EventType==EET_MOUSE_INPUT_EVENT
  2494. && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
  2495. s32 x = event.MouseInput.X;
  2496. s32 y = event.MouseInput.Y;
  2497. gui::IGUIElement *hovered =
  2498. Environment->getRootGUIElement()->getElementFromPoint(
  2499. core::position2d<s32>(x, y));
  2500. if (hovered && isMyChild(hovered)) {
  2501. hovered->OnEvent(event);
  2502. return true;
  2503. }
  2504. }
  2505. if (event.EventType == EET_MOUSE_INPUT_EVENT) {
  2506. s32 x = event.MouseInput.X;
  2507. s32 y = event.MouseInput.Y;
  2508. gui::IGUIElement *hovered =
  2509. Environment->getRootGUIElement()->getElementFromPoint(
  2510. core::position2d<s32>(x, y));
  2511. if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  2512. m_old_tooltip_id = -1;
  2513. }
  2514. if (!isChild(hovered,this)) {
  2515. if (DoubleClickDetection(event)) {
  2516. return true;
  2517. }
  2518. }
  2519. }
  2520. #ifdef __ANDROID__
  2521. // display software keyboard when clicking edit boxes
  2522. if (event.EventType == EET_MOUSE_INPUT_EVENT
  2523. && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  2524. gui::IGUIElement *hovered =
  2525. Environment->getRootGUIElement()->getElementFromPoint(
  2526. core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
  2527. if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
  2528. bool retval = hovered->OnEvent(event);
  2529. if (retval) {
  2530. Environment->setFocus(hovered);
  2531. }
  2532. m_JavaDialogFieldName = getNameByID(hovered->getID());
  2533. std::string message = gettext("Enter ");
  2534. std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
  2535. if (label == "") {
  2536. label = "text";
  2537. }
  2538. message += gettext(label) + ":";
  2539. /* single line text input */
  2540. int type = 2;
  2541. /* multi line text input */
  2542. if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled()) {
  2543. type = 1;
  2544. }
  2545. /* passwords are always single line */
  2546. if (((gui::IGUIEditBox*) hovered)->isPasswordBox()) {
  2547. type = 3;
  2548. }
  2549. porting::showInputDialog(gettext("ok"), "",
  2550. wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
  2551. type);
  2552. return retval;
  2553. }
  2554. }
  2555. if (event.EventType == EET_TOUCH_INPUT_EVENT)
  2556. {
  2557. SEvent translated;
  2558. memset(&translated, 0, sizeof(SEvent));
  2559. translated.EventType = EET_MOUSE_INPUT_EVENT;
  2560. gui::IGUIElement* root = Environment->getRootGUIElement();
  2561. if (!root) {
  2562. errorstream
  2563. << "GUIFormSpecMenu::preprocessEvent unable to get root element"
  2564. << std::endl;
  2565. return false;
  2566. }
  2567. gui::IGUIElement* hovered = root->getElementFromPoint(
  2568. core::position2d<s32>(
  2569. event.TouchInput.X,
  2570. event.TouchInput.Y));
  2571. translated.MouseInput.X = event.TouchInput.X;
  2572. translated.MouseInput.Y = event.TouchInput.Y;
  2573. translated.MouseInput.Control = false;
  2574. bool dont_send_event = false;
  2575. if (event.TouchInput.touchedCount == 1) {
  2576. switch (event.TouchInput.Event) {
  2577. case ETIE_PRESSED_DOWN:
  2578. m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
  2579. translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
  2580. translated.MouseInput.ButtonStates = EMBSM_LEFT;
  2581. m_down_pos = m_pointer;
  2582. break;
  2583. case ETIE_MOVED:
  2584. m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
  2585. translated.MouseInput.Event = EMIE_MOUSE_MOVED;
  2586. translated.MouseInput.ButtonStates = EMBSM_LEFT;
  2587. break;
  2588. case ETIE_LEFT_UP:
  2589. translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
  2590. translated.MouseInput.ButtonStates = 0;
  2591. hovered = root->getElementFromPoint(m_down_pos);
  2592. /* we don't have a valid pointer element use last
  2593. * known pointer pos */
  2594. translated.MouseInput.X = m_pointer.X;
  2595. translated.MouseInput.Y = m_pointer.Y;
  2596. /* reset down pos */
  2597. m_down_pos = v2s32(0,0);
  2598. break;
  2599. default:
  2600. dont_send_event = true;
  2601. //this is not supposed to happen
  2602. errorstream
  2603. << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
  2604. << event.TouchInput.Event << std::endl;
  2605. }
  2606. } else if ( (event.TouchInput.touchedCount == 2) &&
  2607. (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
  2608. hovered = root->getElementFromPoint(m_down_pos);
  2609. translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
  2610. translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
  2611. translated.MouseInput.X = m_pointer.X;
  2612. translated.MouseInput.Y = m_pointer.Y;
  2613. if (hovered) {
  2614. hovered->OnEvent(translated);
  2615. }
  2616. translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
  2617. translated.MouseInput.ButtonStates = EMBSM_LEFT;
  2618. if (hovered) {
  2619. hovered->OnEvent(translated);
  2620. }
  2621. dont_send_event = true;
  2622. }
  2623. /* ignore unhandled 2 touch events ... accidental moving for example */
  2624. else if (event.TouchInput.touchedCount == 2) {
  2625. dont_send_event = true;
  2626. }
  2627. else if (event.TouchInput.touchedCount > 2) {
  2628. errorstream
  2629. << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
  2630. << event.TouchInput.touchedCount << " ignoring them" << std::endl;
  2631. }
  2632. if (dont_send_event) {
  2633. return true;
  2634. }
  2635. /* check if translated event needs to be preprocessed again */
  2636. if (preprocessEvent(translated)) {
  2637. return true;
  2638. }
  2639. if (hovered) {
  2640. grab();
  2641. bool retval = hovered->OnEvent(translated);
  2642. if (event.TouchInput.Event == ETIE_LEFT_UP) {
  2643. /* reset pointer */
  2644. m_pointer = v2s32(0,0);
  2645. }
  2646. drop();
  2647. return retval;
  2648. }
  2649. }
  2650. #endif
  2651. if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
  2652. /* TODO add a check like:
  2653. if (event.JoystickEvent != joystick_we_listen_for)
  2654. return false;
  2655. */
  2656. bool handled = m_joystick->handleEvent(event.JoystickEvent);
  2657. if (handled) {
  2658. if (m_joystick->wasKeyDown(KeyType::ESC)) {
  2659. tryClose();
  2660. } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
  2661. if (m_allowclose) {
  2662. acceptInput(quit_mode_accept);
  2663. quitMenu();
  2664. }
  2665. }
  2666. }
  2667. return handled;
  2668. }
  2669. return false;
  2670. }
  2671. /******************************************************************************/
  2672. bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
  2673. {
  2674. /* The following code is for capturing double-clicks of the mouse button
  2675. * and translating the double-click into an EET_KEY_INPUT_EVENT event
  2676. * -- which closes the form -- under some circumstances.
  2677. *
  2678. * There have been many github issues reporting this as a bug even though it
  2679. * was an intended feature. For this reason, remapping the double-click as
  2680. * an ESC must be explicitly set when creating this class via the
  2681. * /p remap_dbl_click parameter of the constructor.
  2682. */
  2683. if (!m_remap_dbl_click)
  2684. return false;
  2685. if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  2686. m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
  2687. m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
  2688. m_doubleclickdetect[1].pos = m_pointer;
  2689. m_doubleclickdetect[1].time = porting::getTimeMs();
  2690. }
  2691. else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
  2692. u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
  2693. if (delta > 400) {
  2694. return false;
  2695. }
  2696. double squaredistance =
  2697. m_doubleclickdetect[0].pos
  2698. .getDistanceFromSQ(m_doubleclickdetect[1].pos);
  2699. if (squaredistance > (30*30)) {
  2700. return false;
  2701. }
  2702. SEvent* translated = new SEvent();
  2703. assert(translated != 0);
  2704. //translate doubleclick to escape
  2705. memset(translated, 0, sizeof(SEvent));
  2706. translated->EventType = irr::EET_KEY_INPUT_EVENT;
  2707. translated->KeyInput.Key = KEY_ESCAPE;
  2708. translated->KeyInput.Control = false;
  2709. translated->KeyInput.Shift = false;
  2710. translated->KeyInput.PressedDown = true;
  2711. translated->KeyInput.Char = 0;
  2712. OnEvent(*translated);
  2713. // no need to send the key up event as we're already deleted
  2714. // and no one else did notice this event
  2715. delete translated;
  2716. return true;
  2717. }
  2718. return false;
  2719. }
  2720. void GUIFormSpecMenu::tryClose()
  2721. {
  2722. if (m_allowclose) {
  2723. doPause = false;
  2724. acceptInput(quit_mode_cancel);
  2725. quitMenu();
  2726. } else {
  2727. m_text_dst->gotText(L"MenuQuit");
  2728. }
  2729. }
  2730. bool GUIFormSpecMenu::OnEvent(const SEvent& event)
  2731. {
  2732. if (event.EventType==EET_KEY_INPUT_EVENT) {
  2733. KeyPress kp(event.KeyInput);
  2734. if (event.KeyInput.PressedDown && (
  2735. (kp == EscapeKey) || (kp == CancelKey) ||
  2736. ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
  2737. tryClose();
  2738. return true;
  2739. }
  2740. if (m_client != NULL && event.KeyInput.PressedDown &&
  2741. (kp == getKeySetting("keymap_screenshot"))) {
  2742. m_client->makeScreenshot();
  2743. }
  2744. if (event.KeyInput.PressedDown &&
  2745. (event.KeyInput.Key==KEY_RETURN ||
  2746. event.KeyInput.Key==KEY_UP ||
  2747. event.KeyInput.Key==KEY_DOWN)
  2748. ) {
  2749. switch (event.KeyInput.Key) {
  2750. case KEY_RETURN:
  2751. current_keys_pending.key_enter = true;
  2752. break;
  2753. case KEY_UP:
  2754. current_keys_pending.key_up = true;
  2755. break;
  2756. case KEY_DOWN:
  2757. current_keys_pending.key_down = true;
  2758. break;
  2759. break;
  2760. default:
  2761. //can't happen at all!
  2762. FATAL_ERROR("Reached a source line that can't ever been reached");
  2763. break;
  2764. }
  2765. if (current_keys_pending.key_enter && m_allowclose) {
  2766. acceptInput(quit_mode_accept);
  2767. quitMenu();
  2768. } else {
  2769. acceptInput();
  2770. }
  2771. return true;
  2772. }
  2773. }
  2774. /* Mouse event other than movement, or crossing the border of inventory
  2775. field while holding right mouse button
  2776. */
  2777. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  2778. (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
  2779. (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
  2780. event.MouseInput.isRightPressed() &&
  2781. getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
  2782. // Get selected item and hovered/clicked item (s)
  2783. m_old_tooltip_id = -1;
  2784. updateSelectedItem();
  2785. ItemSpec s = getItemAtPos(m_pointer);
  2786. Inventory *inv_selected = NULL;
  2787. Inventory *inv_s = NULL;
  2788. InventoryList *list_s = NULL;
  2789. if (m_selected_item) {
  2790. inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
  2791. sanity_check(inv_selected);
  2792. sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
  2793. }
  2794. u32 s_count = 0;
  2795. if (s.isValid())
  2796. do { // breakable
  2797. inv_s = m_invmgr->getInventory(s.inventoryloc);
  2798. if (!inv_s) {
  2799. errorstream << "InventoryMenu: The selected inventory location "
  2800. << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
  2801. << std::endl;
  2802. s.i = -1; // make it invalid again
  2803. break;
  2804. }
  2805. list_s = inv_s->getList(s.listname);
  2806. if (list_s == NULL) {
  2807. verbosestream << "InventoryMenu: The selected inventory list \""
  2808. << s.listname << "\" does not exist" << std::endl;
  2809. s.i = -1; // make it invalid again
  2810. break;
  2811. }
  2812. if ((u32)s.i >= list_s->getSize()) {
  2813. infostream << "InventoryMenu: The selected inventory list \""
  2814. << s.listname << "\" is too small (i=" << s.i << ", size="
  2815. << list_s->getSize() << ")" << std::endl;
  2816. s.i = -1; // make it invalid again
  2817. break;
  2818. }
  2819. s_count = list_s->getItem(s.i).count;
  2820. } while(0);
  2821. bool identical = (m_selected_item != NULL) && s.isValid() &&
  2822. (inv_selected == inv_s) &&
  2823. (m_selected_item->listname == s.listname) &&
  2824. (m_selected_item->i == s.i);
  2825. // buttons: 0 = left, 1 = right, 2 = middle
  2826. // up/down: 0 = down (press), 1 = up (release), 2 = unknown event, -1 movement
  2827. int button = 0;
  2828. int updown = 2;
  2829. if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
  2830. { button = 0; updown = 0; }
  2831. else if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
  2832. { button = 1; updown = 0; }
  2833. else if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
  2834. { button = 2; updown = 0; }
  2835. else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
  2836. { button = 0; updown = 1; }
  2837. else if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
  2838. { button = 1; updown = 1; }
  2839. else if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
  2840. { button = 2; updown = 1; }
  2841. else if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
  2842. { updown = -1;}
  2843. // Set this number to a positive value to generate a move action
  2844. // from m_selected_item to s.
  2845. u32 move_amount = 0;
  2846. // Set this number to a positive value to generate a move action
  2847. // from s to the next inventory ring.
  2848. u32 shift_move_amount = 0;
  2849. // Set this number to a positive value to generate a drop action
  2850. // from m_selected_item.
  2851. u32 drop_amount = 0;
  2852. // Set this number to a positive value to generate a craft action at s.
  2853. u32 craft_amount = 0;
  2854. if (updown == 0) {
  2855. // Some mouse button has been pressed
  2856. //infostream<<"Mouse button "<<button<<" pressed at p=("
  2857. // <<p.X<<","<<p.Y<<")"<<std::endl;
  2858. m_selected_dragging = false;
  2859. if (s.isValid() && s.listname == "craftpreview") {
  2860. // Craft preview has been clicked: craft
  2861. craft_amount = (button == 2 ? 10 : 1);
  2862. } else if (m_selected_item == NULL) {
  2863. if (s_count != 0) {
  2864. // Non-empty stack has been clicked: select or shift-move it
  2865. m_selected_item = new ItemSpec(s);
  2866. u32 count;
  2867. if (button == 1) // right
  2868. count = (s_count + 1) / 2;
  2869. else if (button == 2) // middle
  2870. count = MYMIN(s_count, 10);
  2871. else // left
  2872. count = s_count;
  2873. if (!event.MouseInput.Shift) {
  2874. // no shift: select item
  2875. m_selected_amount = count;
  2876. m_selected_dragging = true;
  2877. m_auto_place = false;
  2878. } else {
  2879. // shift pressed: move item
  2880. if (button != 1)
  2881. shift_move_amount = count;
  2882. else // count of 1 at left click like after drag & drop
  2883. shift_move_amount = 1;
  2884. }
  2885. }
  2886. } else { // m_selected_item != NULL
  2887. assert(m_selected_amount >= 1);
  2888. if (s.isValid()) {
  2889. // Clicked a slot: move
  2890. if (button == 1) // right
  2891. move_amount = 1;
  2892. else if (button == 2) // middle
  2893. move_amount = MYMIN(m_selected_amount, 10);
  2894. else // left
  2895. move_amount = m_selected_amount;
  2896. if (identical) {
  2897. if (move_amount >= m_selected_amount)
  2898. m_selected_amount = 0;
  2899. else
  2900. m_selected_amount -= move_amount;
  2901. move_amount = 0;
  2902. }
  2903. }
  2904. else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
  2905. // Clicked outside of the window: drop
  2906. if (button == 1) // right
  2907. drop_amount = 1;
  2908. else if (button == 2) // middle
  2909. drop_amount = MYMIN(m_selected_amount, 10);
  2910. else // left
  2911. drop_amount = m_selected_amount;
  2912. }
  2913. }
  2914. }
  2915. else if (updown == 1) {
  2916. // Some mouse button has been released
  2917. //infostream<<"Mouse button "<<button<<" released at p=("
  2918. // <<p.X<<","<<p.Y<<")"<<std::endl;
  2919. if (m_selected_item != NULL && m_selected_dragging && s.isValid()) {
  2920. if (!identical) {
  2921. // Dragged to different slot: move all selected
  2922. move_amount = m_selected_amount;
  2923. }
  2924. } else if (m_selected_item != NULL && m_selected_dragging &&
  2925. !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
  2926. // Dragged outside of window: drop all selected
  2927. drop_amount = m_selected_amount;
  2928. }
  2929. m_selected_dragging = false;
  2930. // Keep track of whether the mouse button be released
  2931. // One click is drag without dropping. Click + release
  2932. // + click changes to drop item when moved mode
  2933. if (m_selected_item)
  2934. m_auto_place = true;
  2935. } else if (updown == -1) {
  2936. // Mouse has been moved and rmb is down and mouse pointer just
  2937. // entered a new inventory field (checked in the entry-if, this
  2938. // is the only action here that is generated by mouse movement)
  2939. if (m_selected_item != NULL && s.isValid()) {
  2940. // Move 1 item
  2941. // TODO: middle mouse to move 10 items might be handy
  2942. if (m_auto_place) {
  2943. // Only move an item if the destination slot is empty
  2944. // or contains the same item type as what is going to be
  2945. // moved
  2946. InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
  2947. InventoryList *list_to = list_s;
  2948. assert(list_from && list_to);
  2949. ItemStack stack_from = list_from->getItem(m_selected_item->i);
  2950. ItemStack stack_to = list_to->getItem(s.i);
  2951. if (stack_to.empty() || stack_to.name == stack_from.name)
  2952. move_amount = 1;
  2953. }
  2954. }
  2955. }
  2956. // Possibly send inventory action to server
  2957. if (move_amount > 0) {
  2958. // Send IAction::Move
  2959. assert(m_selected_item && m_selected_item->isValid());
  2960. assert(s.isValid());
  2961. assert(inv_selected && inv_s);
  2962. InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
  2963. InventoryList *list_to = list_s;
  2964. assert(list_from && list_to);
  2965. ItemStack stack_from = list_from->getItem(m_selected_item->i);
  2966. ItemStack stack_to = list_to->getItem(s.i);
  2967. // Check how many items can be moved
  2968. move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
  2969. ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
  2970. // If source stack cannot be added to destination stack at all,
  2971. // they are swapped
  2972. if ((leftover.count == stack_from.count) &&
  2973. (leftover.name == stack_from.name)) {
  2974. m_selected_amount = stack_to.count;
  2975. // In case the server doesn't directly swap them but instead
  2976. // moves stack_to somewhere else, set this
  2977. m_selected_content_guess = stack_to;
  2978. m_selected_content_guess_inventory = s.inventoryloc;
  2979. }
  2980. // Source stack goes fully into destination stack
  2981. else if (leftover.empty()) {
  2982. m_selected_amount -= move_amount;
  2983. m_selected_content_guess = ItemStack(); // Clear
  2984. }
  2985. // Source stack goes partly into destination stack
  2986. else {
  2987. move_amount -= leftover.count;
  2988. m_selected_amount -= move_amount;
  2989. m_selected_content_guess = ItemStack(); // Clear
  2990. }
  2991. infostream << "Handing IAction::Move to manager" << std::endl;
  2992. IMoveAction *a = new IMoveAction();
  2993. a->count = move_amount;
  2994. a->from_inv = m_selected_item->inventoryloc;
  2995. a->from_list = m_selected_item->listname;
  2996. a->from_i = m_selected_item->i;
  2997. a->to_inv = s.inventoryloc;
  2998. a->to_list = s.listname;
  2999. a->to_i = s.i;
  3000. m_invmgr->inventoryAction(a);
  3001. } else if (shift_move_amount > 0) {
  3002. u32 mis = m_inventory_rings.size();
  3003. u32 i = 0;
  3004. for (; i < mis; i++) {
  3005. const ListRingSpec &sp = m_inventory_rings[i];
  3006. if (sp.inventoryloc == s.inventoryloc
  3007. && sp.listname == s.listname)
  3008. break;
  3009. }
  3010. do {
  3011. if (i >= mis) // if not found
  3012. break;
  3013. u32 to_inv_ind = (i + 1) % mis;
  3014. const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
  3015. InventoryList *list_from = list_s;
  3016. if (!s.isValid())
  3017. break;
  3018. Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
  3019. if (!inv_to)
  3020. break;
  3021. InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
  3022. if (!list_to)
  3023. break;
  3024. ItemStack stack_from = list_from->getItem(s.i);
  3025. assert(shift_move_amount <= stack_from.count);
  3026. if (m_client->getProtoVersion() >= 25) {
  3027. infostream << "Handing IAction::Move to manager" << std::endl;
  3028. IMoveAction *a = new IMoveAction();
  3029. a->count = shift_move_amount;
  3030. a->from_inv = s.inventoryloc;
  3031. a->from_list = s.listname;
  3032. a->from_i = s.i;
  3033. a->to_inv = to_inv_sp.inventoryloc;
  3034. a->to_list = to_inv_sp.listname;
  3035. a->move_somewhere = true;
  3036. m_invmgr->inventoryAction(a);
  3037. } else {
  3038. // find a place (or more than one) to add the new item
  3039. u32 ilt_size = list_to->getSize();
  3040. ItemStack leftover;
  3041. for (u32 slot_to = 0; slot_to < ilt_size
  3042. && shift_move_amount > 0; slot_to++) {
  3043. list_to->itemFits(slot_to, stack_from, &leftover);
  3044. if (leftover.count < stack_from.count) {
  3045. infostream << "Handing IAction::Move to manager" << std::endl;
  3046. IMoveAction *a = new IMoveAction();
  3047. a->count = MYMIN(shift_move_amount,
  3048. (u32) (stack_from.count - leftover.count));
  3049. shift_move_amount -= a->count;
  3050. a->from_inv = s.inventoryloc;
  3051. a->from_list = s.listname;
  3052. a->from_i = s.i;
  3053. a->to_inv = to_inv_sp.inventoryloc;
  3054. a->to_list = to_inv_sp.listname;
  3055. a->to_i = slot_to;
  3056. m_invmgr->inventoryAction(a);
  3057. stack_from = leftover;
  3058. }
  3059. }
  3060. }
  3061. } while (0);
  3062. } else if (drop_amount > 0) {
  3063. m_selected_content_guess = ItemStack(); // Clear
  3064. // Send IAction::Drop
  3065. assert(m_selected_item && m_selected_item->isValid());
  3066. assert(inv_selected);
  3067. InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
  3068. assert(list_from);
  3069. ItemStack stack_from = list_from->getItem(m_selected_item->i);
  3070. // Check how many items can be dropped
  3071. drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
  3072. assert(drop_amount > 0 && drop_amount <= m_selected_amount);
  3073. m_selected_amount -= drop_amount;
  3074. infostream << "Handing IAction::Drop to manager" << std::endl;
  3075. IDropAction *a = new IDropAction();
  3076. a->count = drop_amount;
  3077. a->from_inv = m_selected_item->inventoryloc;
  3078. a->from_list = m_selected_item->listname;
  3079. a->from_i = m_selected_item->i;
  3080. m_invmgr->inventoryAction(a);
  3081. } else if (craft_amount > 0) {
  3082. m_selected_content_guess = ItemStack(); // Clear
  3083. // Send IAction::Craft
  3084. assert(s.isValid());
  3085. assert(inv_s);
  3086. infostream << "Handing IAction::Craft to manager" << std::endl;
  3087. ICraftAction *a = new ICraftAction();
  3088. a->count = craft_amount;
  3089. a->craft_inv = s.inventoryloc;
  3090. m_invmgr->inventoryAction(a);
  3091. }
  3092. // If m_selected_amount has been decreased to zero, deselect
  3093. if (m_selected_amount == 0) {
  3094. delete m_selected_item;
  3095. m_selected_item = NULL;
  3096. m_selected_amount = 0;
  3097. m_selected_dragging = false;
  3098. m_selected_content_guess = ItemStack();
  3099. }
  3100. m_old_pointer = m_pointer;
  3101. }
  3102. if (event.EventType == EET_GUI_EVENT) {
  3103. if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
  3104. && isVisible()) {
  3105. // find the element that was clicked
  3106. for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
  3107. if ((s.ftype == f_TabHeader) &&
  3108. (s.fid == event.GUIEvent.Caller->getID())) {
  3109. s.send = true;
  3110. acceptInput();
  3111. s.send = false;
  3112. return true;
  3113. }
  3114. }
  3115. }
  3116. if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
  3117. && isVisible()) {
  3118. if (!canTakeFocus(event.GUIEvent.Element)) {
  3119. infostream<<"GUIFormSpecMenu: Not allowing focus change."
  3120. <<std::endl;
  3121. // Returning true disables focus change
  3122. return true;
  3123. }
  3124. }
  3125. if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
  3126. (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
  3127. (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
  3128. (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
  3129. unsigned int btn_id = event.GUIEvent.Caller->getID();
  3130. if (btn_id == 257) {
  3131. if (m_allowclose) {
  3132. acceptInput(quit_mode_accept);
  3133. quitMenu();
  3134. } else {
  3135. acceptInput();
  3136. m_text_dst->gotText(L"ExitButton");
  3137. }
  3138. // quitMenu deallocates menu
  3139. return true;
  3140. }
  3141. // find the element that was clicked
  3142. for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
  3143. // if its a button, set the send field so
  3144. // lua knows which button was pressed
  3145. if ((s.ftype == f_Button || s.ftype == f_CheckBox) &&
  3146. s.fid == event.GUIEvent.Caller->getID()) {
  3147. s.send = true;
  3148. if (s.is_exit) {
  3149. if (m_allowclose) {
  3150. acceptInput(quit_mode_accept);
  3151. quitMenu();
  3152. } else {
  3153. m_text_dst->gotText(L"ExitButton");
  3154. }
  3155. return true;
  3156. }
  3157. acceptInput(quit_mode_no);
  3158. s.send = false;
  3159. return true;
  3160. } else if ((s.ftype == f_DropDown) &&
  3161. (s.fid == event.GUIEvent.Caller->getID())) {
  3162. // only send the changed dropdown
  3163. for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
  3164. if (s2.ftype == f_DropDown) {
  3165. s2.send = false;
  3166. }
  3167. }
  3168. s.send = true;
  3169. acceptInput(quit_mode_no);
  3170. // revert configuration to make sure dropdowns are sent on
  3171. // regular button click
  3172. for (GUIFormSpecMenu::FieldSpec &s2 : m_fields) {
  3173. if (s2.ftype == f_DropDown) {
  3174. s2.send = true;
  3175. }
  3176. }
  3177. return true;
  3178. } else if ((s.ftype == f_ScrollBar) &&
  3179. (s.fid == event.GUIEvent.Caller->getID())) {
  3180. s.fdefault = L"Changed";
  3181. acceptInput(quit_mode_no);
  3182. s.fdefault = L"";
  3183. }
  3184. }
  3185. }
  3186. if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
  3187. if (event.GUIEvent.Caller->getID() > 257) {
  3188. bool close_on_enter = true;
  3189. for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
  3190. if (s.ftype == f_Unknown &&
  3191. s.fid == event.GUIEvent.Caller->getID()) {
  3192. current_field_enter_pending = s.fname;
  3193. std::unordered_map<std::string, bool>::const_iterator it =
  3194. field_close_on_enter.find(s.fname);
  3195. if (it != field_close_on_enter.end())
  3196. close_on_enter = (*it).second;
  3197. break;
  3198. }
  3199. }
  3200. if (m_allowclose && close_on_enter) {
  3201. current_keys_pending.key_enter = true;
  3202. acceptInput(quit_mode_accept);
  3203. quitMenu();
  3204. } else {
  3205. current_keys_pending.key_enter = true;
  3206. acceptInput();
  3207. }
  3208. // quitMenu deallocates menu
  3209. return true;
  3210. }
  3211. }
  3212. if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
  3213. int current_id = event.GUIEvent.Caller->getID();
  3214. if (current_id > 257) {
  3215. // find the element that was clicked
  3216. for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
  3217. // if it's a table, set the send field
  3218. // so lua knows which table was changed
  3219. if ((s.ftype == f_Table) && (s.fid == current_id)) {
  3220. s.send = true;
  3221. acceptInput();
  3222. s.send=false;
  3223. }
  3224. }
  3225. return true;
  3226. }
  3227. }
  3228. }
  3229. return Parent ? Parent->OnEvent(event) : false;
  3230. }
  3231. /**
  3232. * get name of element by element id
  3233. * @param id of element
  3234. * @return name string or empty string
  3235. */
  3236. std::string GUIFormSpecMenu::getNameByID(s32 id)
  3237. {
  3238. for (FieldSpec &spec : m_fields) {
  3239. if (spec.fid == id) {
  3240. return spec.fname;
  3241. }
  3242. }
  3243. return "";
  3244. }
  3245. /**
  3246. * get label of element by id
  3247. * @param id of element
  3248. * @return label string or empty string
  3249. */
  3250. std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
  3251. {
  3252. for (FieldSpec &spec : m_fields) {
  3253. if (spec.fid == id) {
  3254. return spec.flabel;
  3255. }
  3256. }
  3257. return L"";
  3258. }