EditorUI.as 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  1. // Urho3D editor user interface
  2. XMLFile@ uiStyle;
  3. XMLFile@ iconStyle;
  4. UIElement@ uiMenuBar;
  5. UIElement@ quickMenu;
  6. Menu@ recentSceneMenu;
  7. Window@ mruScenesPopup;
  8. Array<QuickMenuItem@> quickMenuItems;
  9. FileSelector@ uiFileSelector;
  10. String consoleCommandInterpreter;
  11. Window@ contextMenu;
  12. float stepColoringGroupUpdate = 100; // ms
  13. float timeToNextColoringGroupUpdate = 0;
  14. const StringHash UI_ELEMENT_TYPE("UIElement");
  15. const StringHash WINDOW_TYPE("Window");
  16. const StringHash MENU_TYPE("Menu");
  17. const StringHash TEXT_TYPE("Text");
  18. const StringHash CURSOR_TYPE("Cursor");
  19. const String AUTO_STYLE(""); // Empty string means auto style, i.e. applying style according to UI-element's type automatically
  20. const String TEMP_SCENE_NAME("_tempscene_.xml");
  21. const String TEMP_BINARY_SCENE_NAME("_tempscene_.bin");
  22. const StringHash CALLBACK_VAR("Callback");
  23. const StringHash INDENT_MODIFIED_BY_ICON_VAR("IconIndented");
  24. const StringHash VAR_CONTEXT_MENU_HANDLER("ContextMenuHandler");
  25. const int SHOW_POPUP_INDICATOR = -1;
  26. const uint MAX_QUICK_MENU_ITEMS = 10;
  27. const uint maxRecentSceneCount = 5;
  28. Array<String> uiSceneFilters = {"*.xml", "*.bin", "*.*"};
  29. Array<String> uiElementFilters = {"*.xml"};
  30. Array<String> uiAllFilters = {"*.*"};
  31. Array<String> uiScriptFilters = {"*.as", "*.*"};
  32. Array<String> uiParticleFilters = {"*.xml"};
  33. Array<String> uiRenderPathFilters = {"*.xml"};
  34. Array<String> uiExportPathFilters = {"*.obj"};
  35. uint uiSceneFilter = 0;
  36. uint uiElementFilter = 0;
  37. uint uiNodeFilter = 0;
  38. uint uiImportFilter = 0;
  39. uint uiScriptFilter = 0;
  40. uint uiParticleFilter = 0;
  41. uint uiRenderPathFilter = 0;
  42. uint uiExportFilter = 0;
  43. String uiScenePath = fileSystem.programDir + "Data/Scenes";
  44. String uiElementPath = fileSystem.programDir + "Data/UI";
  45. String uiNodePath = fileSystem.programDir + "Data/Objects";
  46. String uiImportPath;
  47. String uiExportPath;
  48. String uiScriptPath = fileSystem.programDir + "Data/Scripts";
  49. String uiParticlePath = fileSystem.programDir + "Data/Particles";
  50. String uiRenderPathPath = fileSystem.programDir + "CoreData/RenderPaths";
  51. Array<String> uiRecentScenes;
  52. String screenshotDir = fileSystem.programDir + "Screenshots";
  53. bool uiFaded = false;
  54. float uiMinOpacity = 0.3;
  55. float uiMaxOpacity = 0.7;
  56. bool uiHidden = false;
  57. void CreateUI()
  58. {
  59. // Remove all existing UI content in case we are reloading the editor script
  60. /// \todo The console will not be properly recreated as it has already been created once
  61. ui.root.RemoveAllChildren();
  62. uiStyle = GetEditorUIXMLFile("UI/DefaultStyle.xml");
  63. ui.root.defaultStyle = uiStyle;
  64. iconStyle = GetEditorUIXMLFile("UI/EditorIcons.xml");
  65. CreateCursor();
  66. CreateMenuBar();
  67. CreateToolBar();
  68. CreateSecondaryToolBar();
  69. CreateQuickMenu();
  70. CreateContextMenu();
  71. CreateHierarchyWindow();
  72. CreateAttributeInspectorWindow();
  73. CreateEditorSettingsDialog();
  74. CreateEditorPreferencesDialog();
  75. CreateMaterialEditor();
  76. CreateParticleEffectEditor();
  77. CreateSpawnEditor();
  78. CreateSoundTypeEditor();
  79. CreateStatsBar();
  80. CreateConsole();
  81. CreateDebugHud();
  82. CreateResourceBrowser();
  83. CreateCamera();
  84. CreateLayerEditor();
  85. CreateColorWheel();
  86. SubscribeToEvent("ScreenMode", "ResizeUI");
  87. SubscribeToEvent("MenuSelected", "HandleMenuSelected");
  88. SubscribeToEvent("ChangeLanguage", "HandleChangeLanguage");
  89. SubscribeToEvent("WheelChangeColor", "HandleWheelChangeColor");
  90. SubscribeToEvent("WheelSelectColor", "HandleWheelSelectColor");
  91. SubscribeToEvent("WheelDiscardColor", "HandleWheelDiscardColor");
  92. }
  93. void ResizeUI()
  94. {
  95. // Resize menu bar
  96. uiMenuBar.SetFixedWidth(graphics.width);
  97. // Resize tool bar
  98. toolBar.SetFixedWidth(graphics.width);
  99. // Resize secondary tool bar
  100. secondaryToolBar.SetFixedHeight(graphics.height);
  101. // Relayout stats bar
  102. Font@ font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  103. if (graphics.width >= 1200)
  104. {
  105. SetupStatsBarText(editorModeText, font, 35, 64, HA_LEFT, VA_TOP);
  106. SetupStatsBarText(renderStatsText, font, -4, 64, HA_RIGHT, VA_TOP);
  107. }
  108. else
  109. {
  110. SetupStatsBarText(editorModeText, font, 35, 64, HA_LEFT, VA_TOP);
  111. SetupStatsBarText(renderStatsText, font, 35, 78, HA_LEFT, VA_TOP);
  112. }
  113. // Relayout windows
  114. Array<UIElement@> children = ui.root.GetChildren();
  115. for (uint i = 0; i < children.length; ++i)
  116. {
  117. if (children[i].type == WINDOW_TYPE)
  118. AdjustPosition(children[i]);
  119. }
  120. // Relayout root UI element
  121. editorUIElement.SetSize(graphics.width, graphics.height);
  122. // Set new viewport area and reset the viewport layout
  123. viewportArea = IntRect(0, 0, graphics.width, graphics.height);
  124. SetViewportMode(viewportMode);
  125. }
  126. void AdjustPosition(Window@ window)
  127. {
  128. IntVector2 position = window.position;
  129. IntVector2 size = window.size;
  130. IntVector2 extend = position + size;
  131. if (extend.x > graphics.width)
  132. position.x = Max(10, graphics.width - size.x - 10);
  133. if (extend.y > graphics.height)
  134. position.y = Max(100, graphics.height - size.y - 10);
  135. window.position = position;
  136. }
  137. void CreateCursor()
  138. {
  139. Cursor@ cursor = Cursor("Cursor");
  140. cursor.SetStyleAuto(uiStyle);
  141. cursor.SetPosition(graphics.width / 2, graphics.height / 2);
  142. ui.cursor = cursor;
  143. if (GetPlatform() == "Android" || GetPlatform() == "iOS")
  144. ui.cursor.visible = false;
  145. }
  146. // AngelScript does not support closures (yet), but funcdef should do just fine as a workaround for a few cases here for now
  147. funcdef bool MENU_CALLBACK();
  148. Array<MENU_CALLBACK@> menuCallbacks;
  149. MENU_CALLBACK@ messageBoxCallback;
  150. void HandleQuickSearchChange(StringHash eventType, VariantMap& eventData)
  151. {
  152. LineEdit@ search = eventData["Element"].GetPtr();
  153. if (search is null)
  154. return;
  155. PerformQuickMenuSearch(search.text.ToLower().Trimmed());
  156. }
  157. void HandleQuickSearchFinish(StringHash eventType, VariantMap& eventData)
  158. {
  159. Menu@ menu = quickMenu.GetChild("ResultsMenu", true);
  160. if (menu is null)
  161. return;
  162. String query = eventData["Text"].GetString();
  163. if (query.length <= 0)
  164. return;
  165. Array<QuickMenuItem@> filtered;
  166. {
  167. QuickMenuItem@ qi;
  168. for (uint x=0; x < quickMenuItems.length; x++)
  169. {
  170. @qi = quickMenuItems[x];
  171. int find = qi.action.Find(query, 0, false);
  172. if (find > -1)
  173. {
  174. qi.sortScore = find;
  175. filtered.Push(qi);
  176. }
  177. }
  178. }
  179. filtered.Sort();
  180. if (!filtered.empty)
  181. {
  182. VariantMap data;
  183. Menu@ item = CreateMenuItem(filtered[0].action, filtered[0].callback);
  184. data["Element"] = item;
  185. item.SendEvent("MenuSelected", data);
  186. }
  187. }
  188. void PerformQuickMenuSearch(const String&in query)
  189. {
  190. Menu@ menu = quickMenu.GetChild("ResultsMenu", true);
  191. if (menu is null)
  192. return;
  193. menu.RemoveAllChildren();
  194. uint limit = 0;
  195. if (query.length > 0)
  196. {
  197. int lastIndex = 0;
  198. uint score = 0;
  199. int index = 0;
  200. Array<QuickMenuItem@> filtered;
  201. {
  202. QuickMenuItem@ qi;
  203. for (uint x=0; x < quickMenuItems.length; x++)
  204. {
  205. @qi = quickMenuItems[x];
  206. int find = qi.action.Find(query, 0, false);
  207. if (find > -1)
  208. {
  209. qi.sortScore = find;
  210. filtered.Push(qi);
  211. }
  212. }
  213. }
  214. filtered.Sort();
  215. {
  216. QuickMenuItem@ qi;
  217. limit = filtered.length > MAX_QUICK_MENU_ITEMS ? MAX_QUICK_MENU_ITEMS : filtered.length;
  218. for (uint x=0; x < limit; x++)
  219. {
  220. @qi = filtered[x];
  221. Menu@ item = CreateMenuItem(qi.action, qi.callback);
  222. item.SetMaxSize(1000,16);
  223. menu.AddChild(item);
  224. }
  225. }
  226. }
  227. menu.visible = limit > 0;
  228. menu.SetFixedHeight(limit * 16);
  229. quickMenu.BringToFront();
  230. quickMenu.SetFixedHeight(limit*16 + 62 + (menu.visible ? 6 : 0));
  231. }
  232. class QuickMenuItem
  233. {
  234. String action;
  235. MENU_CALLBACK@ callback;
  236. uint sortScore = 0;
  237. QuickMenuItem(){}
  238. QuickMenuItem(String action, MENU_CALLBACK@ callback)
  239. {
  240. this.action = action;
  241. this.callback = callback;
  242. }
  243. int opCmp(QuickMenuItem@ b)
  244. {
  245. return sortScore - b.sortScore;
  246. }
  247. }
  248. /// Create popup search menu.
  249. void CreateQuickMenu()
  250. {
  251. if (quickMenu !is null)
  252. return;
  253. quickMenu = LoadEditorUI("UI/EditorQuickMenu.xml");
  254. quickMenu.enabled = false;
  255. quickMenu.visible = false;
  256. quickMenu.opacity = uiMaxOpacity;
  257. // Handle a dummy search in the quick menu to finalize its initial size to empty
  258. PerformQuickMenuSearch("");
  259. ui.root.AddChild(quickMenu);
  260. LineEdit@ search = quickMenu.GetChild("Search", true);
  261. SubscribeToEvent(search, "TextChanged", "HandleQuickSearchChange");
  262. SubscribeToEvent(search, "TextFinished", "HandleQuickSearchFinish");
  263. UIElement@ closeButton = quickMenu.GetChild("CloseButton", true);
  264. SubscribeToEvent(closeButton, "Pressed", "ToggleQuickMenu");
  265. }
  266. void ToggleQuickMenu()
  267. {
  268. quickMenu.enabled = !quickMenu.enabled && ui.cursor.visible;
  269. quickMenu.visible = quickMenu.enabled;
  270. if (quickMenu.enabled)
  271. {
  272. quickMenu.position = ui.cursorPosition - IntVector2(20,70);
  273. LineEdit@ search = quickMenu.GetChild("Search", true);
  274. search.text = "";
  275. search.focus = true;
  276. }
  277. }
  278. /// Create top menu bar.
  279. void CreateMenuBar()
  280. {
  281. uiMenuBar = BorderImage("MenuBar");
  282. ui.root.AddChild(uiMenuBar);
  283. uiMenuBar.enabled = true;
  284. uiMenuBar.style = "EditorMenuBar";
  285. uiMenuBar.SetLayout(LM_HORIZONTAL);
  286. uiMenuBar.opacity = uiMaxOpacity;
  287. uiMenuBar.SetFixedWidth(graphics.width);
  288. {
  289. Menu@ menu = CreateMenu("File");
  290. Window@ popup = menu.popup;
  291. popup.AddChild(CreateMenuItem("New scene", @ResetScene, 'N', QUAL_SHIFT | QUAL_CTRL));
  292. popup.AddChild(CreateMenuItem("Open scene...", @PickFile, 'O', QUAL_CTRL));
  293. popup.AddChild(CreateMenuItem("Save scene", @SaveSceneWithExistingName, 'S', QUAL_CTRL));
  294. popup.AddChild(CreateMenuItem("Save scene as...", @PickFile, 'S', QUAL_SHIFT | QUAL_CTRL));
  295. recentSceneMenu = CreateMenuItem("Open recent scene", null, SHOW_POPUP_INDICATOR);
  296. popup.AddChild(recentSceneMenu);
  297. mruScenesPopup = CreatePopup(recentSceneMenu);
  298. PopulateMruScenes();
  299. CreateChildDivider(popup);
  300. Menu@ childMenu = CreateMenuItem("menu Load node", null, SHOW_POPUP_INDICATOR);
  301. Window@ childPopup = CreatePopup(childMenu);
  302. childPopup.AddChild(CreateMenuItem("As replicated...", @PickFile, 0, 0, true, "Load node as replicated..."));
  303. childPopup.AddChild(CreateMenuItem("As local...", @PickFile, 0, 0, true, "Load node as local..."));
  304. popup.AddChild(childMenu);
  305. popup.AddChild(CreateMenuItem("Save node as...", @PickFile));
  306. CreateChildDivider(popup);
  307. popup.AddChild(CreateMenuItem("Import model...", @PickFile));
  308. popup.AddChild(CreateMenuItem("Import scene...", @PickFile));
  309. CreateChildDivider(popup);
  310. popup.AddChild(CreateMenuItem("Export scene to OBJ...", @PickFile));
  311. popup.AddChild(CreateMenuItem("Export selected to OBJ...", @PickFile));
  312. CreateChildDivider(popup);
  313. popup.AddChild(CreateMenuItem("Run script...", @PickFile));
  314. popup.AddChild(CreateMenuItem("Set resource path...", @PickFile));
  315. CreateChildDivider(popup);
  316. popup.AddChild(CreateMenuItem("Exit", @Exit));
  317. FinalizedPopupMenu(popup);
  318. uiMenuBar.AddChild(menu);
  319. }
  320. {
  321. Menu@ menu = CreateMenu("Edit");
  322. Window@ popup = menu.popup;
  323. popup.AddChild(CreateMenuItem("Undo", @Undo, 'Z', QUAL_CTRL));
  324. popup.AddChild(CreateMenuItem("Redo", @Redo, 'Y', QUAL_CTRL));
  325. CreateChildDivider(popup);
  326. popup.AddChild(CreateMenuItem("Cut", @Cut, 'X', QUAL_CTRL));
  327. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  328. popup.AddChild(CreateMenuItem("Duplicate", @Duplicate, 'D', QUAL_CTRL));
  329. else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
  330. popup.AddChild(CreateMenuItem("Duplicate", @Duplicate, 'D', QUAL_SHIFT));
  331. popup.AddChild(CreateMenuItem("Copy", @Copy, 'C', QUAL_CTRL));
  332. popup.AddChild(CreateMenuItem("Paste", @Paste, 'V', QUAL_CTRL));
  333. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  334. popup.AddChild(CreateMenuItem("Delete", @Delete, KEY_DELETE, QUAL_ANY));
  335. else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
  336. popup.AddChild(CreateMenuItem("Delete", @BlenderModeDelete, 'X', QUAL_ANY));
  337. popup.AddChild(CreateMenuItem("Select all", @SelectAll, 'A', QUAL_CTRL));
  338. popup.AddChild(CreateMenuItem("Deselect all", @DeselectAll, 'A', QUAL_SHIFT | QUAL_CTRL));
  339. CreateChildDivider(popup);
  340. popup.AddChild(CreateMenuItem("Reset to default", @ResetToDefault));
  341. CreateChildDivider(popup);
  342. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  343. {
  344. popup.AddChild(CreateMenuItem("Reset position", @SceneResetPosition, '1' , QUAL_ALT));
  345. popup.AddChild(CreateMenuItem("Reset rotation", @SceneResetRotation, '2' , QUAL_ALT));
  346. popup.AddChild(CreateMenuItem("Reset scale", @SceneResetScale, '3' , QUAL_ALT));
  347. popup.AddChild(CreateMenuItem("Reset transform", @SceneResetTransform, 'Q' , QUAL_ALT));
  348. }
  349. else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
  350. {
  351. popup.AddChild(CreateMenuItem("Reset position", @SceneResetPosition, 'G' , QUAL_ALT));
  352. popup.AddChild(CreateMenuItem("Reset rotation", @SceneResetRotation, 'R', QUAL_ALT));
  353. popup.AddChild(CreateMenuItem("Reset scale", @SceneResetScale, 'S', QUAL_ALT));
  354. popup.AddChild(CreateMenuItem("Reset transform", @SceneResetTransform, 'Q' , QUAL_ALT));
  355. }
  356. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  357. {
  358. popup.AddChild(CreateMenuItem("Enable/disable", @SceneToggleEnable, 'E', QUAL_CTRL));
  359. popup.AddChild(CreateMenuItem("Enable all", @SceneEnableAllNodes, 'E', QUAL_ALT));
  360. }
  361. else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
  362. {
  363. popup.AddChild(CreateMenuItem("Enable/disable", @SceneToggleEnable, 'H'));
  364. popup.AddChild(CreateMenuItem("Enable all", @SceneEnableAllNodes, 'H', QUAL_ALT));
  365. }
  366. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  367. popup.AddChild(CreateMenuItem("Unparent", @SceneUnparent, 'U', QUAL_CTRL));
  368. else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
  369. popup.AddChild(CreateMenuItem("Unparent", @SceneUnparent, 'P', QUAL_ALT));
  370. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  371. popup.AddChild(CreateMenuItem("Parent to last", @NodesParentToLastSelected, 'U'));
  372. else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
  373. popup.AddChild(CreateMenuItem("Parent to last", @NodesParentToLastSelected, 'P', QUAL_CTRL));
  374. CreateChildDivider(popup);
  375. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  376. popup.AddChild(CreateMenuItem("Toggle update", @ToggleSceneUpdate, 'P', QUAL_CTRL));
  377. //else if (hotKeyMode == HOT_KEYS_MODE_BLENDER)
  378. // popup.AddChild(CreateMenuItem("Toggle update", @ToggleSceneUpdate, 'P', QUAL_CTRL));
  379. if (hotKeyMode == HOTKEYS_MODE_BLENDER)
  380. {
  381. popup.AddChild(CreateMenuItem("Move to layer", @ShowLayerMover, 'M'));
  382. popup.AddChild(CreateMenuItem("Smart Duplicate", @SceneSmartDuplicateNode, 'D', QUAL_ALT));
  383. popup.AddChild(CreateMenuItem("View closer", @ViewCloser, KEY_KP_PERIOD));
  384. }
  385. popup.AddChild(CreateMenuItem("Color wheel", @ColorWheelBuildMenuSelectTypeColor, 'W', QUAL_ALT));
  386. popup.AddChild(CreateMenuItem("Show components icons", @ViewDebugIcons, 'I', QUAL_ALT));
  387. CreateChildDivider(popup);
  388. popup.AddChild(CreateMenuItem("Stop test animation", @StopTestAnimation));
  389. CreateChildDivider(popup);
  390. popup.AddChild(CreateMenuItem("Rebuild navigation data", @SceneRebuildNavigation));
  391. popup.AddChild(CreateMenuItem("Render Zone Cubemap", @SceneRenderZoneCubemaps));
  392. popup.AddChild(CreateMenuItem("Add children to SM-group", @SceneAddChildrenStaticModelGroup));
  393. Menu@ childMenu = CreateMenuItem("Set children as spline path", null, SHOW_POPUP_INDICATOR);
  394. Window@ childPopup = CreatePopup(childMenu);
  395. childPopup.AddChild(CreateMenuItem("Non-cyclic", @SetSplinePath, 0, 0, true, "Set non-cyclic spline path"));
  396. childPopup.AddChild(CreateMenuItem("Cyclic", @SetSplinePath, 0, 0, true, "Set cyclic spline path"));
  397. popup.AddChild(childMenu);
  398. FinalizedPopupMenu(popup);
  399. uiMenuBar.AddChild(menu);
  400. }
  401. {
  402. Menu@ menu = CreateMenu("Create");
  403. Window@ popup = menu.popup;
  404. popup.AddChild(CreateMenuItem("Replicated node", @PickNode, 0, 0, true, "Create Replicated node"));
  405. popup.AddChild(CreateMenuItem("Local node", @PickNode, 0, 0, true, "Create Local node"));
  406. CreateChildDivider(popup);
  407. Menu@ childMenu = CreateMenuItem("Component", null, SHOW_POPUP_INDICATOR);
  408. Window@ childPopup = CreatePopup(childMenu);
  409. String[] objectCategories = GetObjectCategories();
  410. for (uint i = 0; i < objectCategories.length; ++i)
  411. {
  412. // Skip the UI category for the component menus
  413. if (objectCategories[i] == "UI")
  414. continue;
  415. Menu@ menu = CreateMenuItem(objectCategories[i], null, SHOW_POPUP_INDICATOR);
  416. Window@ popup = CreatePopup(menu);
  417. String[] componentTypes = GetObjectsByCategory(objectCategories[i]);
  418. for (uint j = 0; j < componentTypes.length; ++j)
  419. popup.AddChild(CreateIconizedMenuItem(componentTypes[j], @PickComponent, 0, 0, "", true, "Create " + componentTypes[j]));
  420. childPopup.AddChild(menu);
  421. }
  422. FinalizedPopupMenu(childPopup);
  423. popup.AddChild(childMenu);
  424. childMenu = CreateMenuItem("Builtin object", null, SHOW_POPUP_INDICATOR);
  425. childPopup = CreatePopup(childMenu);
  426. String[] objects = { "Box", "Cone", "Cylinder", "Plane", "Pyramid", "Sphere", "TeaPot", "Torus" };
  427. for (uint i = 0; i < objects.length; ++i)
  428. childPopup.AddChild(CreateIconizedMenuItem(objects[i], @PickBuiltinObject, 0, 0, "Node", true, "Create " + objects[i]));
  429. popup.AddChild(childMenu);
  430. CreateChildDivider(popup);
  431. childMenu = CreateMenuItem("UI-element", null, SHOW_POPUP_INDICATOR);
  432. childPopup = CreatePopup(childMenu);
  433. String[] uiElementTypes = GetObjectsByCategory("UI");
  434. for (uint i = 0; i < uiElementTypes.length; ++i)
  435. {
  436. if (uiElementTypes[i] != "UIElement")
  437. childPopup.AddChild(CreateIconizedMenuItem(uiElementTypes[i], @PickUIElement, 0, 0, "", true, "Create " + uiElementTypes[i]));
  438. }
  439. CreateChildDivider(childPopup);
  440. childPopup.AddChild(CreateIconizedMenuItem("UIElement", @PickUIElement));
  441. popup.AddChild(childMenu);
  442. FinalizedPopupMenu(popup);
  443. uiMenuBar.AddChild(menu);
  444. }
  445. {
  446. Menu@ menu = CreateMenu("UI-layout");
  447. Window@ popup = menu.popup;
  448. popup.AddChild(CreateMenuItem("Open UI-layout...", @PickFile, 'O', QUAL_ALT));
  449. popup.AddChild(CreateMenuItem("Save UI-layout", @SaveUILayoutWithExistingName, 'S', QUAL_ALT));
  450. popup.AddChild(CreateMenuItem("Save UI-layout as...", @PickFile));
  451. CreateChildDivider(popup);
  452. popup.AddChild(CreateMenuItem("Close UI-layout", @CloseUILayout, 'C', QUAL_ALT));
  453. popup.AddChild(CreateMenuItem("Close all UI-layouts", @CloseAllUILayouts));
  454. CreateChildDivider(popup);
  455. popup.AddChild(CreateMenuItem("Load child element...", @PickFile));
  456. popup.AddChild(CreateMenuItem("Save child element as...", @PickFile));
  457. CreateChildDivider(popup);
  458. popup.AddChild(CreateMenuItem("Set default style...", @PickFile));
  459. FinalizedPopupMenu(popup);
  460. uiMenuBar.AddChild(menu);
  461. }
  462. {
  463. Menu@ menu = CreateMenu("View");
  464. Window@ popup = menu.popup;
  465. popup.AddChild(CreateMenuItem("Hierarchy", @ToggleHierarchyWindow, 'H', QUAL_CTRL));
  466. popup.AddChild(CreateMenuItem("Attribute inspector", @ToggleAttributeInspectorWindow, 'I', QUAL_CTRL));
  467. popup.AddChild(CreateMenuItem("Resource browser", @ToggleResourceBrowserWindow, 'B', QUAL_CTRL));
  468. popup.AddChild(CreateMenuItem("Material editor", @ToggleMaterialEditor));
  469. popup.AddChild(CreateMenuItem("Particle editor", @ToggleParticleEffectEditor));
  470. popup.AddChild(CreateMenuItem("Spawn editor", @ToggleSpawnEditor));
  471. popup.AddChild(CreateMenuItem("Sound Type editor", @ToggleSoundTypeEditor));
  472. popup.AddChild(CreateMenuItem("Editor settings", @ToggleEditorSettingsDialog));
  473. popup.AddChild(CreateMenuItem("Editor preferences", @ToggleEditorPreferencesDialog));
  474. CreateChildDivider(popup);
  475. popup.AddChild(CreateMenuItem("Hide editor", @ToggleUI, KEY_F12, QUAL_ANY));
  476. FinalizedPopupMenu(popup);
  477. uiMenuBar.AddChild(menu);
  478. }
  479. BorderImage@ spacer = BorderImage("MenuBarSpacer");
  480. uiMenuBar.AddChild(spacer);
  481. spacer.style = "EditorMenuBar";
  482. BorderImage@ logo = BorderImage("Logo");
  483. logo.texture = cache.GetResource("Texture2D", "Textures/Logo.png");
  484. logo.SetFixedWidth(64);
  485. uiMenuBar.AddChild(logo);
  486. }
  487. bool Exit()
  488. {
  489. ui.cursor.shape = CS_BUSY;
  490. if (messageBoxCallback is null)
  491. {
  492. String message;
  493. if (sceneModified)
  494. message = "Scene has been modified.\n";
  495. bool uiLayoutModified = false;
  496. for (uint i = 0; i < editorUIElement.numChildren; ++i)
  497. {
  498. UIElement@ element = editorUIElement.children[i];
  499. if (element !is null && element.vars[MODIFIED_VAR].GetBool())
  500. {
  501. uiLayoutModified = true;
  502. message += "UI layout has been modified.\n";
  503. break;
  504. }
  505. }
  506. if (sceneModified || uiLayoutModified)
  507. {
  508. MessageBox@ messageBox = MessageBox(message + "Continue to exit?", "Warning");
  509. if (messageBox.window !is null)
  510. {
  511. Button@ cancelButton = messageBox.window.GetChild("CancelButton", true);
  512. cancelButton.visible = true;
  513. cancelButton.focus = true;
  514. SubscribeToEvent(messageBox, "MessageACK", "HandleMessageAcknowledgement");
  515. messageBoxCallback = @Exit;
  516. return false;
  517. }
  518. }
  519. }
  520. else
  521. messageBoxCallback = null;
  522. engine.Exit();
  523. return true;
  524. }
  525. void HandleExitRequested()
  526. {
  527. if (!ui.HasModalElement())
  528. Exit();
  529. }
  530. bool PickFile()
  531. {
  532. Menu@ menu = GetEventSender();
  533. if (menu is null)
  534. return false;
  535. String action = menu.name;
  536. if (action.empty)
  537. return false;
  538. // File (Scene related)
  539. if (action == "Open scene...")
  540. {
  541. CreateFileSelector("Open scene", "Open", "Cancel", uiScenePath, uiSceneFilters, uiSceneFilter);
  542. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleOpenSceneFile");
  543. }
  544. else if (action == "Save scene as..." || action == "Save scene")
  545. {
  546. CreateFileSelector("Save scene as", "Save", "Cancel", uiScenePath, uiSceneFilters, uiSceneFilter);
  547. uiFileSelector.fileName = GetFileNameAndExtension(editorScene.fileName);
  548. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveSceneFile");
  549. }
  550. else if (action == "As replicated..." || action == "Load node as replicated...")
  551. {
  552. instantiateMode = REPLICATED;
  553. CreateFileSelector("fileSelector Load node", "Load", "Cancel", uiNodePath, uiSceneFilters, uiNodeFilter);
  554. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleLoadNodeFile");
  555. }
  556. else if (action == "As local..." || action == "Load node as local...")
  557. {
  558. instantiateMode = LOCAL;
  559. CreateFileSelector("Load node", "Load", "Cancel", uiNodePath, uiSceneFilters, uiNodeFilter);
  560. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleLoadNodeFile");
  561. }
  562. else if (action == "Save node as...")
  563. {
  564. if (editNode !is null && editNode !is editorScene)
  565. {
  566. CreateFileSelector("Save node", "Save", "Cancel", uiNodePath, uiSceneFilters, uiNodeFilter);
  567. uiFileSelector.fileName = GetFileNameAndExtension(instantiateFileName);
  568. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveNodeFile");
  569. }
  570. }
  571. else if (action == "Import model...")
  572. {
  573. CreateFileSelector("Import model", "Import", "Cancel", uiImportPath, uiAllFilters, uiImportFilter);
  574. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleImportModel");
  575. }
  576. else if (action == "Import scene...")
  577. {
  578. CreateFileSelector("Import scene", "Import", "Cancel", uiImportPath, uiAllFilters, uiImportFilter);
  579. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleImportScene");
  580. }
  581. else if (action == "Export scene to OBJ..." || action == "Export selected to OBJ...")
  582. {
  583. // Set these up together to share the "export settings" options
  584. if (action == "Export scene to OBJ...")
  585. {
  586. CreateFileSelector("Export scene to OBJ", "Save", "Cancel", uiExportPath, uiExportPathFilters, uiExportFilter);
  587. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleExportSceneOBJ");
  588. }
  589. else if (action == "Export selected to OBJ...")
  590. {
  591. CreateFileSelector("Export selected to OBJ", "Save", "Cancel", uiExportPath, uiExportPathFilters, uiExportFilter);
  592. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleExportSelectedOBJ");
  593. }
  594. Window@ window = uiFileSelector.window;
  595. UIElement@ optionsGroup = UIElement();
  596. optionsGroup.maxHeight = 30;
  597. optionsGroup.layoutMode = LM_HORIZONTAL;
  598. window.defaultStyle = uiStyle;
  599. window.style = AUTO_STYLE;
  600. CheckBox@ checkRightHanded = CheckBox();
  601. checkRightHanded.checked = objExportRightHanded_;
  602. checkRightHanded.defaultStyle = uiStyle;
  603. checkRightHanded.style = AUTO_STYLE;
  604. SubscribeToEvent(checkRightHanded, "Toggled", "HandleOBJRightHandedChanged");
  605. optionsGroup.AddChild(checkRightHanded);
  606. Text@ lblRightHanded = Text();
  607. lblRightHanded.defaultStyle = uiStyle;
  608. lblRightHanded.style = AUTO_STYLE;
  609. lblRightHanded.text = " Right handed";
  610. optionsGroup.AddChild(lblRightHanded);
  611. CheckBox@ checkZUp = CheckBox();
  612. checkZUp.checked = objExportZUp_;
  613. checkZUp.defaultStyle = uiStyle;
  614. checkZUp.style = AUTO_STYLE;
  615. SubscribeToEvent(checkZUp, "Toggled", "HandleOBJZUpChanged");
  616. optionsGroup.AddChild(checkZUp);
  617. Text@ lblZUp = Text();
  618. lblZUp.defaultStyle = uiStyle;
  619. lblZUp.style = AUTO_STYLE;
  620. lblZUp.text = " Z Axis Up";
  621. optionsGroup.AddChild(lblZUp);
  622. window.AddChild(optionsGroup);
  623. }
  624. else if (action == "Run script...")
  625. {
  626. CreateFileSelector("Run script", "Run", "Cancel", uiScriptPath, uiScriptFilters, uiScriptFilter);
  627. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleRunScript");
  628. }
  629. else if (action == "Set resource path...")
  630. {
  631. CreateFileSelector("Set resource path", "Set", "Cancel", sceneResourcePath, uiAllFilters, 0);
  632. uiFileSelector.directoryMode = true;
  633. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleResourcePath");
  634. }
  635. // UI-element
  636. else if (action == "Open UI-layout...")
  637. {
  638. CreateFileSelector("Open UI-layout", "Open", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  639. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleOpenUILayoutFile");
  640. }
  641. else if (action == "Save UI-layout as..." || action == "Save UI-layout")
  642. {
  643. if (editUIElement !is null)
  644. {
  645. UIElement@ element = GetTopLevelUIElement(editUIElement);
  646. if (element is null)
  647. return false;
  648. CreateFileSelector("Save UI-layout as", "Save", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  649. uiFileSelector.fileName = GetFileNameAndExtension(element.GetVar(FILENAME_VAR).GetString());
  650. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveUILayoutFile");
  651. }
  652. }
  653. else if (action == "Load child element...")
  654. {
  655. if (editUIElement !is null)
  656. {
  657. CreateFileSelector("Load child element", "Load", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  658. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleLoadChildUIElementFile");
  659. }
  660. }
  661. else if (action == "Save child element as...")
  662. {
  663. if (editUIElement !is null)
  664. {
  665. CreateFileSelector("Save child element", "Save", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  666. uiFileSelector.fileName = GetFileNameAndExtension(editUIElement.GetVar(CHILD_ELEMENT_FILENAME_VAR).GetString());
  667. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveChildUIElementFile");
  668. }
  669. }
  670. else if (action == "Set default style...")
  671. {
  672. CreateFileSelector("Set default style", "Set", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  673. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleUIElementDefaultStyle");
  674. }
  675. return true;
  676. }
  677. bool PickNode()
  678. {
  679. Menu@ menu = GetEventSender();
  680. if (menu is null)
  681. return false;
  682. String action = GetActionName(menu.name);
  683. if (action.empty)
  684. return false;
  685. CreateNode(action == "Replicated node" ? REPLICATED : LOCAL);
  686. return true;
  687. }
  688. bool PickComponent()
  689. {
  690. if (editNodes.empty)
  691. return false;
  692. Menu@ menu = GetEventSender();
  693. if (menu is null)
  694. return false;
  695. String action = GetActionName(menu.name);
  696. if (action.empty)
  697. return false;
  698. CreateComponent(action);
  699. return true;
  700. }
  701. bool PickBuiltinObject()
  702. {
  703. Menu@ menu = GetEventSender();
  704. if (menu is null)
  705. return false;
  706. String action = GetActionName(menu.name);
  707. if (action.empty)
  708. return false;
  709. CreateBuiltinObject(action);
  710. return true;
  711. }
  712. bool PickUIElement()
  713. {
  714. Menu@ menu = GetEventSender();
  715. if (menu is null)
  716. return false;
  717. String action = GetActionName(menu.name);
  718. if (action.empty)
  719. return false;
  720. return NewUIElement(action);
  721. }
  722. // When calling items from the quick menu, they have "Create" prepended for clarity. Strip that now to get the object name to create
  723. String GetActionName(const String&in name)
  724. {
  725. if (name.StartsWith("Create"))
  726. return name.Substring(7);
  727. else
  728. return name;
  729. }
  730. void HandleMenuSelected(StringHash eventType, VariantMap& eventData)
  731. {
  732. Menu@ menu = eventData["Element"].GetPtr();
  733. if (menu is null)
  734. return;
  735. HandlePopup(menu);
  736. quickMenu.visible = false;
  737. quickMenu.enabled = false;
  738. // Execute the callback if available
  739. Variant variant = menu.GetVar(CALLBACK_VAR);
  740. if (!variant.empty)
  741. menuCallbacks[variant.GetUInt()]();
  742. }
  743. Menu@ CreateMenuItem(const String&in title, MENU_CALLBACK@ callback = null, int accelKey = 0, int accelQual = 0, bool addToQuickMenu = true, String quickMenuText="", bool autoLocalize = true)
  744. {
  745. Menu@ menu = Menu(title);
  746. menu.defaultStyle = uiStyle;
  747. menu.style = AUTO_STYLE;
  748. menu.SetLayout(LM_HORIZONTAL, 0, IntRect(8, 2, 8, 2));
  749. if (accelKey > 0)
  750. menu.SetAccelerator(accelKey, accelQual);
  751. if (callback !is null)
  752. {
  753. menu.vars[CALLBACK_VAR] = menuCallbacks.length;
  754. menuCallbacks.Push(callback);
  755. }
  756. Text@ menuText = Text();
  757. menu.AddChild(menuText);
  758. menuText.style = "EditorMenuText";
  759. menuText.text = title;
  760. menuText.autoLocalizable = autoLocalize;
  761. if (addToQuickMenu)
  762. AddQuickMenuItem(callback, quickMenuText.empty ? title : quickMenuText);
  763. if (accelKey != 0)
  764. {
  765. UIElement@ spacer = UIElement();
  766. spacer.minWidth = menuText.indentSpacing;
  767. spacer.height = menuText.height;
  768. menu.AddChild(spacer);
  769. menu.AddChild(CreateAccelKeyText(accelKey, accelQual));
  770. }
  771. return menu;
  772. }
  773. void AddQuickMenuItem(MENU_CALLBACK@ callback, String text)
  774. {
  775. if (callback is null)
  776. return;
  777. bool exists = false;
  778. for (uint i=0;i<quickMenuItems.length;++i)
  779. {
  780. if (quickMenuItems[i].action == text)
  781. {
  782. exists = true;
  783. break;
  784. }
  785. }
  786. if (!exists)
  787. quickMenuItems.Push(QuickMenuItem(text, callback));
  788. }
  789. Menu@ CreateIconizedMenuItem(const String&in title, MENU_CALLBACK@ callback = null, int accelKey = 0, int accelQual = 0, const String&in iconType = "", bool addToQuickMenu=true, String quickMenuText="")
  790. {
  791. Menu@ menu = Menu(title);
  792. menu.defaultStyle = uiStyle;
  793. menu.style = AUTO_STYLE;
  794. menu.SetLayout(LM_VERTICAL, 0, IntRect(8, 2, 8, 2));
  795. if (accelKey > 0)
  796. menu.SetAccelerator(accelKey, accelQual);
  797. if (callback !is null)
  798. {
  799. menu.vars[CALLBACK_VAR] = menuCallbacks.length;
  800. menuCallbacks.Push(callback);
  801. }
  802. Text@ menuText = Text();
  803. menu.AddChild(menuText);
  804. menuText.style = "EditorMenuText";
  805. menuText.text = title;
  806. // If icon type is not provided, use the title instead
  807. IconizeUIElement(menuText, iconType.empty ? title : iconType);
  808. if (addToQuickMenu)
  809. AddQuickMenuItem(callback, quickMenuText.empty ? title : quickMenuText);
  810. if (accelKey != 0)
  811. {
  812. menuText.layoutMode = LM_HORIZONTAL;
  813. menuText.AddChild(CreateAccelKeyText(accelKey, accelQual));
  814. }
  815. return menu;
  816. }
  817. /// Create a child divider in parent with vertical layout mode. It works on other parent element as well, not just parent menu.
  818. void CreateChildDivider(UIElement@ parent)
  819. {
  820. BorderImage@ divider = parent.CreateChild("BorderImage", "Divider");
  821. divider.style = "EditorDivider";
  822. }
  823. Window@ CreatePopup(Menu@ baseMenu)
  824. {
  825. Window@ popup = Window();
  826. popup.defaultStyle = uiStyle;
  827. popup.style = AUTO_STYLE;
  828. popup.SetLayout(LM_VERTICAL, 1, IntRect(2, 6, 2, 6));
  829. baseMenu.popup = popup;
  830. baseMenu.popupOffset = IntVector2(0, baseMenu.height);
  831. return popup;
  832. }
  833. Menu@ CreateMenu(const String&in title)
  834. {
  835. Menu@ menu = CreateMenuItem(title);
  836. Text@ text = menu.children[0];
  837. menu.maxWidth = text.width + 20;
  838. CreatePopup(menu);
  839. return menu;
  840. }
  841. void HandleChangeLanguage(StringHash eventType, VariantMap& eventData)
  842. {
  843. Array<UIElement@> children = uiMenuBar.GetChildren();
  844. for (uint i = 0; i < children.length - 2; ++i) // last 2 elements is not menu
  845. {
  846. // dirty hack: force recalc text size
  847. children[i].maxWidth = 1000;
  848. Text@ text = children[i].children[0];
  849. text.minWidth = 0;
  850. text.maxWidth = 1;
  851. text.ApplyAttributes();
  852. children[i].maxWidth = text.width + 20;
  853. }
  854. RebuildResourceDatabase();
  855. }
  856. Text@ CreateAccelKeyText(int accelKey, int accelQual)
  857. {
  858. Text@ accelKeyText = Text();
  859. accelKeyText.defaultStyle = uiStyle;
  860. accelKeyText.style = "EditorMenuText";
  861. accelKeyText.textAlignment = HA_RIGHT;
  862. String text;
  863. if (accelKey == KEY_DELETE)
  864. text = "Del";
  865. else if (accelKey == KEY_SPACE)
  866. text = "Space";
  867. // Cannot use range as the key constants below do not appear to be in sequence
  868. else if (accelKey == KEY_F1)
  869. text = "F1";
  870. else if (accelKey == KEY_F2)
  871. text = "F2";
  872. else if (accelKey == KEY_F3)
  873. text = "F3";
  874. else if (accelKey == KEY_F4)
  875. text = "F4";
  876. else if (accelKey == KEY_F5)
  877. text = "F5";
  878. else if (accelKey == KEY_F6)
  879. text = "F6";
  880. else if (accelKey == KEY_F7)
  881. text = "F7";
  882. else if (accelKey == KEY_F8)
  883. text = "F8";
  884. else if (accelKey == KEY_F9)
  885. text = "F9";
  886. else if (accelKey == KEY_F10)
  887. text = "F10";
  888. else if (accelKey == KEY_F11)
  889. text = "F11";
  890. else if (accelKey == KEY_F12)
  891. text = "F12";
  892. else if (accelKey == SHOW_POPUP_INDICATOR)
  893. text = ">";
  894. else
  895. text.AppendUTF8(accelKey);
  896. if (accelQual & QUAL_ALT > 0)
  897. text = "Alt+" + text;
  898. if (accelQual & QUAL_SHIFT > 0)
  899. text = "Shift+" + text;
  900. if (accelQual & QUAL_CTRL > 0)
  901. text = "Ctrl+" + text;
  902. accelKeyText.text = text;
  903. return accelKeyText;
  904. }
  905. void FinalizedPopupMenu(Window@ popup)
  906. {
  907. // Find the maximum menu text width
  908. Array<UIElement@> children = popup.GetChildren();
  909. int maxWidth = 0;
  910. for (uint i = 0; i < children.length; ++i)
  911. {
  912. UIElement@ element = children[i];
  913. if (element.type != MENU_TYPE) // Skip if not menu item
  914. continue;
  915. int width = element.children[0].width;
  916. if (width > maxWidth)
  917. maxWidth = width;
  918. }
  919. // Adjust the indent spacing to slightly wider than the maximum width
  920. maxWidth += 20;
  921. for (uint i = 0; i < children.length; ++i)
  922. {
  923. UIElement@ element = children[i];
  924. if (element.type != MENU_TYPE)
  925. continue;
  926. Menu@ menu = element;
  927. Text@ menuText = menu.children[0];
  928. if (menuText.numChildren == 1) // Skip if menu text does not have accel
  929. menuText.children[0].indentSpacing = maxWidth;
  930. // Adjust the popup offset taking the indentation into effect
  931. if (menu.popup !is null)
  932. menu.popupOffset = IntVector2(menu.width, 0);
  933. }
  934. }
  935. void CreateFileSelector(const String&in title, const String&in ok, const String&in cancel, const String&in initialPath, Array<String>@ filters,
  936. uint initialFilter, bool autoLocalizeTitle = true)
  937. {
  938. // Within the editor UI, the file selector is a kind of a "singleton". When the previous one is overwritten, also
  939. // the events subscribed from it are disconnected, so new ones are safe to subscribe.
  940. uiFileSelector = FileSelector();
  941. uiFileSelector.defaultStyle = uiStyle;
  942. uiFileSelector.title = title;
  943. uiFileSelector.titleText.autoLocalizable = autoLocalizeTitle;
  944. uiFileSelector.path = initialPath;
  945. uiFileSelector.SetButtonTexts(ok, cancel);
  946. Text@ okText = cast<Text>(uiFileSelector.okButton.children[0]);
  947. okText.autoLocalizable = true;
  948. Text@ cancelText = cast<Text>(uiFileSelector.cancelButton.children[0]);
  949. cancelText.autoLocalizable = true;
  950. uiFileSelector.SetFilters(filters, initialFilter);
  951. CenterDialog(uiFileSelector.window);
  952. }
  953. void CloseFileSelector(uint&out filterIndex, String&out path)
  954. {
  955. // Save filter & path for next time
  956. filterIndex = uiFileSelector.filterIndex;
  957. path = uiFileSelector.path;
  958. uiFileSelector = null;
  959. }
  960. void CloseFileSelector()
  961. {
  962. uiFileSelector = null;
  963. }
  964. void CreateConsole()
  965. {
  966. Console@ console = engine.CreateConsole();
  967. console.defaultStyle = uiStyle;
  968. console.commandInterpreter = consoleCommandInterpreter;
  969. console.numBufferedRows = 100;
  970. console.autoVisibleOnError = true;
  971. }
  972. void CreateDebugHud()
  973. {
  974. engine.CreateDebugHud();
  975. debugHud.defaultStyle = uiStyle;
  976. debugHud.mode = DEBUGHUD_SHOW_NONE;
  977. }
  978. void CenterDialog(UIElement@ element)
  979. {
  980. IntVector2 size = element.size;
  981. element.SetPosition((graphics.width - size.x) / 2, (graphics.height - size.y) / 2);
  982. }
  983. void CreateContextMenu()
  984. {
  985. contextMenu = LoadEditorUI("UI/EditorContextMenu.xml");
  986. ui.root.AddChild(contextMenu);
  987. }
  988. void UpdateWindowTitle()
  989. {
  990. String sceneName = GetFileNameAndExtension(editorScene.fileName);
  991. if (sceneName.empty || sceneName == TEMP_SCENE_NAME || sceneName == TEMP_BINARY_SCENE_NAME)
  992. sceneName = "Untitled";
  993. if (sceneModified)
  994. sceneName += "*";
  995. graphics.windowTitle = "Urho3D editor - " + sceneName;
  996. }
  997. void HandlePopup(Menu@ menu)
  998. {
  999. // Close the top level menu now unless the selected menu item has another popup
  1000. if (menu.popup !is null)
  1001. return;
  1002. for (;;)
  1003. {
  1004. UIElement@ menuParent = menu.parent;
  1005. if (menuParent is null)
  1006. break;
  1007. Menu@ nextMenu = menuParent.vars["Origin"].GetPtr();
  1008. if (nextMenu is null)
  1009. break;
  1010. else
  1011. menu = nextMenu;
  1012. }
  1013. if (menu.parent is uiMenuBar)
  1014. menu.showPopup = false;
  1015. }
  1016. String ExtractFileName(VariantMap& eventData, bool forSave = false)
  1017. {
  1018. String fileName;
  1019. // Check for OK
  1020. if (eventData["OK"].GetBool())
  1021. {
  1022. String filter = eventData["Filter"].GetString();
  1023. fileName = eventData["FileName"].GetString();
  1024. // Add default extension for saving if not specified
  1025. if (GetExtension(fileName).empty && forSave && filter != "*.*")
  1026. fileName = fileName + filter.Substring(1);
  1027. }
  1028. return fileName;
  1029. }
  1030. void HandleOpenSceneFile(StringHash eventType, VariantMap& eventData)
  1031. {
  1032. CloseFileSelector(uiSceneFilter, uiScenePath);
  1033. LoadScene(ExtractFileName(eventData));
  1034. }
  1035. void HandleSaveSceneFile(StringHash eventType, VariantMap& eventData)
  1036. {
  1037. CloseFileSelector(uiSceneFilter, uiScenePath);
  1038. SaveScene(ExtractFileName(eventData, true));
  1039. }
  1040. void HandleLoadNodeFile(StringHash eventType, VariantMap& eventData)
  1041. {
  1042. CloseFileSelector(uiNodeFilter, uiNodePath);
  1043. LoadNode(ExtractFileName(eventData));
  1044. }
  1045. void HandleSaveNodeFile(StringHash eventType, VariantMap& eventData)
  1046. {
  1047. CloseFileSelector(uiNodeFilter, uiNodePath);
  1048. SaveNode(ExtractFileName(eventData, true));
  1049. }
  1050. void HandleImportModel(StringHash eventType, VariantMap& eventData)
  1051. {
  1052. CloseFileSelector(uiImportFilter, uiImportPath);
  1053. ImportModel(ExtractFileName(eventData));
  1054. }
  1055. void HandleImportScene(StringHash eventType, VariantMap& eventData)
  1056. {
  1057. CloseFileSelector(uiImportFilter, uiImportPath);
  1058. ImportScene(ExtractFileName(eventData));
  1059. }
  1060. void HandleExportSceneOBJ(StringHash eventType, VariantMap& eventData)
  1061. {
  1062. CloseFileSelector(uiExportFilter, uiExportPath);
  1063. ExportSceneToOBJ(ExtractFileName(eventData));
  1064. }
  1065. void HandleExportSelectedOBJ(StringHash eventType, VariantMap& eventData)
  1066. {
  1067. CloseFileSelector(uiExportFilter, uiExportPath);
  1068. ExportSelectedToOBJ(ExtractFileName(eventData));
  1069. }
  1070. void ExecuteScript(const String&in fileName)
  1071. {
  1072. if (fileName.empty)
  1073. return;
  1074. File@ file = File(fileName, FILE_READ);
  1075. if (file.open)
  1076. {
  1077. String scriptCode;
  1078. while (!file.eof)
  1079. scriptCode += file.ReadLine() + "\n";
  1080. file.Close();
  1081. if (script.Execute(scriptCode))
  1082. log.Info("Script " + fileName + " ran successfully");
  1083. }
  1084. }
  1085. void HandleRunScript(StringHash eventType, VariantMap& eventData)
  1086. {
  1087. CloseFileSelector(uiScriptFilter, uiScriptPath);
  1088. ExecuteScript(ExtractFileName(eventData));
  1089. }
  1090. void HandleResourcePath(StringHash eventType, VariantMap& eventData)
  1091. {
  1092. String pathName = uiFileSelector.path;
  1093. CloseFileSelector();
  1094. if (eventData["OK"].GetBool())
  1095. SetResourcePath(pathName, false);
  1096. }
  1097. void HandleOpenUILayoutFile(StringHash eventType, VariantMap& eventData)
  1098. {
  1099. CloseFileSelector(uiElementFilter, uiElementPath);
  1100. OpenUILayout(ExtractFileName(eventData));
  1101. }
  1102. void HandleSaveUILayoutFile(StringHash eventType, VariantMap& eventData)
  1103. {
  1104. CloseFileSelector(uiElementFilter, uiElementPath);
  1105. SaveUILayout(ExtractFileName(eventData, true));
  1106. }
  1107. void HandleLoadChildUIElementFile(StringHash eventType, VariantMap& eventData)
  1108. {
  1109. CloseFileSelector(uiElementFilter, uiElementPath);
  1110. LoadChildUIElement(ExtractFileName(eventData));
  1111. }
  1112. void HandleSaveChildUIElementFile(StringHash eventType, VariantMap& eventData)
  1113. {
  1114. CloseFileSelector(uiElementFilter, uiElementPath);
  1115. SaveChildUIElement(ExtractFileName(eventData, true));
  1116. }
  1117. void HandleUIElementDefaultStyle(StringHash eventType, VariantMap& eventData)
  1118. {
  1119. CloseFileSelector(uiElementFilter, uiElementPath);
  1120. SetUIElementDefaultStyle(ExtractFileName(eventData));
  1121. }
  1122. void HandleHotKeysBlender( VariantMap& eventData)
  1123. {
  1124. int key = eventData["Key"].GetInt();
  1125. int viewDirection = eventData["Qualifiers"].GetInt() == QUAL_CTRL ? -1 : 1;
  1126. if (key == KEY_ESC)
  1127. {
  1128. if (uiHidden)
  1129. UnhideUI();
  1130. else if (console.visible)
  1131. console.visible = false;
  1132. else if (contextMenu.visible)
  1133. CloseContextMenu();
  1134. else if (quickMenu.visible)
  1135. {
  1136. quickMenu.visible = false;
  1137. quickMenu.enabled = false;
  1138. }
  1139. else
  1140. {
  1141. UIElement@ front = ui.frontElement;
  1142. if (front is settingsDialog || front is preferencesDialog)
  1143. {
  1144. ui.focusElement = null;
  1145. front.visible = false;
  1146. }
  1147. }
  1148. }
  1149. // Ignore other keys when UI has a modal element
  1150. else if (ui.HasModalElement())
  1151. return;
  1152. else if (key == KEY_F1)
  1153. console.Toggle();
  1154. else if (key == KEY_F2)
  1155. ToggleRenderingDebug();
  1156. else if (key == KEY_F3)
  1157. TogglePhysicsDebug();
  1158. else if (key == KEY_F4)
  1159. ToggleOctreeDebug();
  1160. else if (key == KEY_F11)
  1161. {
  1162. Image@ screenshot = Image();
  1163. graphics.TakeScreenShot(screenshot);
  1164. if (!fileSystem.DirExists(screenshotDir))
  1165. fileSystem.CreateDir(screenshotDir);
  1166. screenshot.SavePNG(screenshotDir + "/Screenshot_" +
  1167. time.timeStamp.Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
  1168. }
  1169. else if (key == KEY_KP_1 && ui.focusElement is null) // Front view
  1170. {
  1171. Vector3 center = Vector3(0,0,0);
  1172. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1173. center = SelectedNodesCenterPoint();
  1174. Vector3 pos = cameraNode.worldPosition - center;
  1175. cameraNode.worldPosition = center - Vector3(0.0, 0.0, pos.length * viewDirection);
  1176. cameraNode.direction = Vector3(0, 0, viewDirection);
  1177. ReacquireCameraYawPitch();
  1178. }
  1179. else if (key == KEY_KP_3 && ui.focusElement is null) // Side view
  1180. {
  1181. Vector3 center = Vector3(0,0,0);
  1182. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1183. center = SelectedNodesCenterPoint();
  1184. Vector3 pos = cameraNode.worldPosition - center;
  1185. cameraNode.worldPosition = center - Vector3(pos.length * -viewDirection, 0.0, 0.0);
  1186. cameraNode.direction = Vector3(-viewDirection, 0, 0);
  1187. ReacquireCameraYawPitch();
  1188. }
  1189. else if (key == KEY_KP_7 && ui.focusElement is null) // Top view
  1190. {
  1191. Vector3 center = Vector3(0,0,0);
  1192. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1193. center = SelectedNodesCenterPoint();
  1194. Vector3 pos = cameraNode.worldPosition - center;
  1195. cameraNode.worldPosition = center - Vector3(0.0, pos.length * -viewDirection, 0.0);
  1196. cameraNode.direction = Vector3(0, -viewDirection, 0);
  1197. ReacquireCameraYawPitch();
  1198. }
  1199. else if (key == KEY_KP_5 && ui.focusElement is null)
  1200. {
  1201. activeViewport.camera.zoom = 1;
  1202. activeViewport.ToggleOrthographic();
  1203. }
  1204. else if (key == '4')
  1205. editMode = EDIT_SELECT;
  1206. else if (key == '5')
  1207. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1208. else if (key == '6')
  1209. {
  1210. --pickMode;
  1211. if (pickMode < PICK_GEOMETRIES)
  1212. pickMode = MAX_PICK_MODES - 1;
  1213. }
  1214. else if (key == '7')
  1215. {
  1216. ++pickMode;
  1217. if (pickMode >= MAX_PICK_MODES)
  1218. pickMode = PICK_GEOMETRIES;
  1219. }
  1220. else if (key == 'Z' && eventData["Qualifiers"].GetInt() != QUAL_CTRL)
  1221. {
  1222. fillMode = FillMode(fillMode + 1);
  1223. if (fillMode > FILL_POINT)
  1224. fillMode = FILL_SOLID;
  1225. // Update camera fill mode
  1226. SetFillMode(fillMode);
  1227. }
  1228. else if (key == KEY_SPACE)
  1229. {
  1230. if (ui.cursor.visible && ui.focusElement is null)
  1231. ToggleQuickMenu();
  1232. }
  1233. else
  1234. {
  1235. SteppedObjectManipulation(key);
  1236. }
  1237. if ((ui.focusElement is null) && (selectedNodes.length > 0) && !cameraFlyMode)
  1238. {
  1239. if (eventData["Qualifiers"].GetInt() == QUAL_ALT) // reset transformations
  1240. {
  1241. if (key == KEY_G)
  1242. SceneResetPosition();
  1243. else if (key == KEY_R)
  1244. SceneResetRotation();
  1245. else if (key == KEY_S)
  1246. SceneResetScale();
  1247. else if (key == KEY_F)
  1248. {
  1249. Vector3 center = Vector3(0,0,0);
  1250. if (selectedNodes.length > 0)
  1251. center = SelectedNodesCenterPoint();
  1252. cameraNode.LookAt(center);
  1253. ReacquireCameraYawPitch();
  1254. }
  1255. }
  1256. else if (eventData["Qualifiers"].GetInt() != QUAL_CTRL) // set transformations
  1257. {
  1258. if (key == KEY_G)
  1259. {
  1260. editMode = EDIT_MOVE;
  1261. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1262. }
  1263. else if (key == KEY_R)
  1264. {
  1265. editMode = EDIT_ROTATE;
  1266. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1267. }
  1268. else if (key == KEY_S)
  1269. {
  1270. editMode = EDIT_SCALE;
  1271. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1272. }
  1273. else if (key == KEY_F)
  1274. {
  1275. if (camera.orthographic)
  1276. {
  1277. viewCloser = true;
  1278. }
  1279. else
  1280. {
  1281. Vector3 center = Vector3(0,0,0);
  1282. if (selectedNodes.length > 0)
  1283. center = SelectedNodesCenterPoint();
  1284. cameraNode.LookAt(center);
  1285. ReacquireCameraYawPitch();
  1286. }
  1287. }
  1288. }
  1289. }
  1290. toolBarDirty = true;
  1291. }
  1292. void HandleHotKeysStandard(VariantMap& eventData)
  1293. {
  1294. int key = eventData["Key"].GetInt();
  1295. int viewDirection = eventData["Qualifiers"].GetInt() == QUAL_CTRL ? -1 : 1;
  1296. if (key == KEY_ESC)
  1297. {
  1298. if (uiHidden)
  1299. UnhideUI();
  1300. else if (console.visible)
  1301. console.visible = false;
  1302. else if (contextMenu.visible)
  1303. CloseContextMenu();
  1304. else if (quickMenu.visible)
  1305. {
  1306. quickMenu.visible = false;
  1307. quickMenu.enabled = false;
  1308. }
  1309. else
  1310. {
  1311. UIElement@ front = ui.frontElement;
  1312. if (front is settingsDialog || front is preferencesDialog)
  1313. {
  1314. ui.focusElement = null;
  1315. front.visible = false;
  1316. }
  1317. }
  1318. }
  1319. // Ignore other keys when UI has a modal element
  1320. else if (ui.HasModalElement())
  1321. return;
  1322. else if (key == KEY_F1)
  1323. console.Toggle();
  1324. else if (key == KEY_F2)
  1325. ToggleRenderingDebug();
  1326. else if (key == KEY_F3)
  1327. TogglePhysicsDebug();
  1328. else if (key == KEY_F4)
  1329. ToggleOctreeDebug();
  1330. else if (key == KEY_F11)
  1331. {
  1332. Image@ screenshot = Image();
  1333. graphics.TakeScreenShot(screenshot);
  1334. if (!fileSystem.DirExists(screenshotDir))
  1335. fileSystem.CreateDir(screenshotDir);
  1336. screenshot.SavePNG(screenshotDir + "/Screenshot_" +
  1337. time.timeStamp.Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
  1338. }
  1339. else if (key == KEY_KP_1 && ui.focusElement is null) // Front view
  1340. {
  1341. Vector3 center = Vector3(0,0,0);
  1342. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1343. center = SelectedNodesCenterPoint();
  1344. Vector3 pos = cameraNode.worldPosition - center;
  1345. cameraNode.worldPosition = center - Vector3(0.0, 0.0, pos.length * viewDirection);
  1346. cameraNode.direction = Vector3(0, 0, viewDirection);
  1347. ReacquireCameraYawPitch();
  1348. }
  1349. else if (key == KEY_KP_3 && ui.focusElement is null) // Side view
  1350. {
  1351. Vector3 center = Vector3(0,0,0);
  1352. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1353. center = SelectedNodesCenterPoint();
  1354. Vector3 pos = cameraNode.worldPosition - center;
  1355. cameraNode.worldPosition = center - Vector3(pos.length * -viewDirection, 0.0, 0.0);
  1356. cameraNode.direction = Vector3(-viewDirection, 0, 0);
  1357. ReacquireCameraYawPitch();
  1358. }
  1359. else if (key == KEY_KP_7 && ui.focusElement is null) // Top view
  1360. {
  1361. Vector3 center = Vector3(0,0,0);
  1362. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1363. center = SelectedNodesCenterPoint();
  1364. Vector3 pos = cameraNode.worldPosition - center;
  1365. cameraNode.worldPosition = center - Vector3(0.0, pos.length * -viewDirection, 0.0);
  1366. cameraNode.direction = Vector3(0, -viewDirection, 0);
  1367. ReacquireCameraYawPitch();
  1368. }
  1369. else if (key == KEY_KP_5 && ui.focusElement is null)
  1370. {
  1371. activeViewport.ToggleOrthographic();
  1372. }
  1373. else if (eventData["Qualifiers"].GetInt() == QUAL_CTRL)
  1374. {
  1375. if (key == '1')
  1376. editMode = EDIT_MOVE;
  1377. else if (key == '2')
  1378. editMode = EDIT_ROTATE;
  1379. else if (key == '3')
  1380. editMode = EDIT_SCALE;
  1381. else if (key == '4')
  1382. editMode = EDIT_SELECT;
  1383. else if (key == '5')
  1384. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1385. else if (key == '6')
  1386. {
  1387. --pickMode;
  1388. if (pickMode < PICK_GEOMETRIES)
  1389. pickMode = MAX_PICK_MODES - 1;
  1390. }
  1391. else if (key == '7')
  1392. {
  1393. ++pickMode;
  1394. if (pickMode >= MAX_PICK_MODES)
  1395. pickMode = PICK_GEOMETRIES;
  1396. }
  1397. else if (key == 'W')
  1398. {
  1399. fillMode = FillMode(fillMode + 1);
  1400. if (fillMode > FILL_POINT)
  1401. fillMode = FILL_SOLID;
  1402. // Update camera fill mode
  1403. SetFillMode(fillMode);
  1404. }
  1405. else if (key == KEY_SPACE)
  1406. {
  1407. if (ui.cursor.visible)
  1408. ToggleQuickMenu();
  1409. }
  1410. else
  1411. SteppedObjectManipulation(key);
  1412. toolBarDirty = true;
  1413. }
  1414. }
  1415. void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  1416. {
  1417. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  1418. {
  1419. HandleHotKeysStandard(eventData);
  1420. }
  1421. else if( hotKeyMode == HOTKEYS_MODE_BLENDER)
  1422. {
  1423. HandleHotKeysBlender(eventData);
  1424. }
  1425. }
  1426. void UnfadeUI()
  1427. {
  1428. FadeUI(false);
  1429. }
  1430. void FadeUI(bool fade = true)
  1431. {
  1432. if (uiHidden || uiFaded == fade)
  1433. return;
  1434. float opacity = (uiFaded = fade) ? uiMinOpacity : uiMaxOpacity;
  1435. Array<UIElement@> children = ui.root.GetChildren();
  1436. for (uint i = 0; i < children.length; ++i)
  1437. {
  1438. // Texts, popup&modal windows (which are anyway only in ui.modalRoot), and editorUIElement are excluded
  1439. if (children[i].type != TEXT_TYPE && children[i] !is editorUIElement)
  1440. children[i].opacity = opacity;
  1441. }
  1442. }
  1443. bool ToggleUI()
  1444. {
  1445. HideUI(!uiHidden);
  1446. return true;
  1447. }
  1448. void UnhideUI()
  1449. {
  1450. HideUI(false);
  1451. }
  1452. void HideUI(bool hide = true)
  1453. {
  1454. if (uiHidden == hide)
  1455. return;
  1456. // Note: we could set ui.root.visible = false and it would hide the whole hierarchy.
  1457. // However in this case we need the editorUIElement to stay visible
  1458. bool visible = !(uiHidden = hide);
  1459. Array<UIElement@> children = ui.root.GetChildren();
  1460. for (uint i = 0; i < children.length; ++i)
  1461. {
  1462. // Cursor and editorUIElement are excluded
  1463. if (children[i].type != CURSOR_TYPE && children[i] !is editorUIElement)
  1464. {
  1465. if (visible)
  1466. {
  1467. if (!children[i].visible)
  1468. children[i].visible = children[i].vars["HideUI"].GetBool();
  1469. }
  1470. else
  1471. {
  1472. children[i].vars["HideUI"] = children[i].visible;
  1473. children[i].visible = false;
  1474. }
  1475. }
  1476. }
  1477. }
  1478. void IconizeUIElement(UIElement@ element, const String&in iconType)
  1479. {
  1480. // Check if the icon has been created before
  1481. BorderImage@ icon = element.GetChild("Icon");
  1482. // If iconType is empty, it is a request to remove the existing icon
  1483. if (iconType.empty)
  1484. {
  1485. // Remove the icon if it exists
  1486. if (icon !is null)
  1487. icon.Remove();
  1488. // Revert back the indent but only if it is indented by this function
  1489. if (element.vars[INDENT_MODIFIED_BY_ICON_VAR].GetBool())
  1490. element.indent = 0;
  1491. return;
  1492. }
  1493. // The UI element must itself has been indented to reserve the space for the icon
  1494. if (element.indent == 0)
  1495. {
  1496. element.indent = 1;
  1497. element.vars[INDENT_MODIFIED_BY_ICON_VAR] = true;
  1498. }
  1499. // If no icon yet then create one with the correct indent and size in respect to the UI element
  1500. if (icon is null)
  1501. {
  1502. // The icon is placed at one indent level less than the UI element
  1503. icon = BorderImage("Icon");
  1504. icon.indent = element.indent - 1;
  1505. icon.SetFixedSize(element.indentWidth - 2, 14);
  1506. element.InsertChild(0, icon); // Ensure icon is added as the first child
  1507. }
  1508. // Set the icon type
  1509. if (!icon.SetStyle(iconType, iconStyle))
  1510. icon.SetStyle("Unknown", iconStyle); // If fails then use an 'unknown' icon type
  1511. icon.color = Color(1,1,1,1); // Reset to enabled color
  1512. }
  1513. void SetIconEnabledColor(UIElement@ element, bool enabled, bool partial = false)
  1514. {
  1515. BorderImage@ icon = element.GetChild("Icon");
  1516. if (icon !is null)
  1517. {
  1518. if (partial)
  1519. {
  1520. icon.colors[C_TOPLEFT] = Color(1,1,1,1);
  1521. icon.colors[C_BOTTOMLEFT] = Color(1,1,1,1);
  1522. icon.colors[C_TOPRIGHT] = Color(1,0,0,1);
  1523. icon.colors[C_BOTTOMRIGHT] = Color(1,0,0,1);
  1524. }
  1525. else
  1526. icon.color = enabled ? Color(1,1,1,1) : Color(1,0,0,1);
  1527. }
  1528. }
  1529. void UpdateDirtyUI()
  1530. {
  1531. UpdateDirtyToolBar();
  1532. // Perform hierarchy selection latently after the new selections are finalized (used in undo/redo action)
  1533. if (!hierarchyUpdateSelections.empty)
  1534. {
  1535. hierarchyList.SetSelections(hierarchyUpdateSelections);
  1536. hierarchyUpdateSelections.Clear();
  1537. }
  1538. // Perform some event-triggered updates latently in case a large hierarchy was changed
  1539. if (attributesFullDirty || attributesDirty)
  1540. UpdateAttributeInspector(attributesFullDirty);
  1541. }
  1542. void HandleMessageAcknowledgement(StringHash eventType, VariantMap& eventData)
  1543. {
  1544. if (eventData["Ok"].GetBool())
  1545. messageBoxCallback();
  1546. else
  1547. messageBoxCallback = null;
  1548. }
  1549. void PopulateMruScenes()
  1550. {
  1551. mruScenesPopup.RemoveAllChildren();
  1552. if (uiRecentScenes.length > 0)
  1553. {
  1554. recentSceneMenu.enabled = true;
  1555. for (uint i=0; i < uiRecentScenes.length; ++i)
  1556. mruScenesPopup.AddChild(CreateMenuItem(uiRecentScenes[i], @LoadMostRecentScene, 0, 0, false, "", false));
  1557. }
  1558. else
  1559. recentSceneMenu.enabled = false;
  1560. }
  1561. bool LoadMostRecentScene()
  1562. {
  1563. Menu@ menu = GetEventSender();
  1564. if (menu is null)
  1565. return false;
  1566. Text@ text = menu.GetChildren()[0];
  1567. if (text is null)
  1568. return false;
  1569. return LoadScene(text.text);
  1570. }
  1571. // Set from click to false if opening menu procedurally.
  1572. void OpenContextMenu(bool fromClick=true)
  1573. {
  1574. if (contextMenu is null)
  1575. return;
  1576. contextMenu.enabled = true;
  1577. contextMenu.visible = true;
  1578. contextMenu.BringToFront();
  1579. if (fromClick)
  1580. contextMenuActionWaitFrame=true;
  1581. }
  1582. void CloseContextMenu()
  1583. {
  1584. if (contextMenu is null)
  1585. return;
  1586. contextMenu.enabled = false;
  1587. contextMenu.visible = false;
  1588. }
  1589. void ActivateContextMenu(Array<UIElement@> actions)
  1590. {
  1591. contextMenu.RemoveAllChildren();
  1592. for (uint i=0; i< actions.length; ++i)
  1593. {
  1594. contextMenu.AddChild(actions[i]);
  1595. }
  1596. contextMenu.SetFixedHeight(24*actions.length+6);
  1597. contextMenu.position = ui.cursor.screenPosition + IntVector2(10,-10);
  1598. OpenContextMenu();
  1599. }
  1600. Menu@ CreateContextMenuItem(String text, String handler, String menuName = "", bool autoLocalize = true)
  1601. {
  1602. Menu@ menu = Menu();
  1603. menu.defaultStyle = uiStyle;
  1604. menu.style = AUTO_STYLE;
  1605. menu.name = menuName;
  1606. menu.SetLayout(LM_HORIZONTAL, 0, IntRect(8, 2, 8, 2));
  1607. Text@ menuText = Text();
  1608. menuText.style = "EditorMenuText";
  1609. menu.AddChild(menuText);
  1610. menuText.text = text;
  1611. menuText.autoLocalizable = autoLocalize;
  1612. menu.vars[VAR_CONTEXT_MENU_HANDLER] = handler;
  1613. SubscribeToEvent(menu, "Released", "ContextMenuEventWrapper");
  1614. return menu;
  1615. }
  1616. void ContextMenuEventWrapper(StringHash eventType, VariantMap& eventData)
  1617. {
  1618. UIElement@ uiElement = eventData["Element"].GetPtr();
  1619. if (uiElement is null)
  1620. return;
  1621. String handler = uiElement.vars[VAR_CONTEXT_MENU_HANDLER].GetString();
  1622. if (!handler.empty)
  1623. {
  1624. SubscribeToEvent(uiElement, "Released", handler);
  1625. uiElement.SendEvent("Released", eventData);
  1626. }
  1627. CloseContextMenu();
  1628. }
  1629. /// Load a UI XML file used by the editor
  1630. XMLFile@ GetEditorUIXMLFile(const String&in fileName)
  1631. {
  1632. // Prefer the executable path to avoid using the user's resource path, which may point
  1633. // to an outdated Urho installation
  1634. String fullFileName = fileSystem.programDir + "Data/" + fileName;
  1635. if (fileSystem.FileExists(fullFileName))
  1636. {
  1637. File@ file = File(fullFileName, FILE_READ);
  1638. XMLFile@ xml = XMLFile();
  1639. xml.name = fileName;
  1640. if (xml.Load(file))
  1641. return xml;
  1642. }
  1643. // Fallback to resource system
  1644. return cache.GetResource("XMLFile", fileName);
  1645. }
  1646. /// Load an UI layout used by the editor
  1647. UIElement@ LoadEditorUI(const String&in fileName)
  1648. {
  1649. return ui.LoadLayout(GetEditorUIXMLFile(fileName));
  1650. }
  1651. /// Set node children as a spline path, either cyclic or non-cyclic
  1652. bool SetSplinePath()
  1653. {
  1654. Menu@ menu = GetEventSender();
  1655. if (menu is null)
  1656. return false;
  1657. return SceneSetChildrenSplinePath(menu.name == "Cyclic");
  1658. }
  1659. bool ColorWheelBuildMenuSelectTypeColor()
  1660. {
  1661. if (selectedNodes.empty && selectedComponents.empty) return false;
  1662. editMode = EDIT_SELECT;
  1663. // do coloring only for single selected object
  1664. // start with trying to find single component
  1665. if (selectedComponents.length == 1)
  1666. {
  1667. coloringComponent = selectedComponents[0];
  1668. }
  1669. // else try to get first component from selected node
  1670. else if (selectedNodes.length == 1)
  1671. {
  1672. Array<Component@> components = selectedNodes[0].GetComponents();
  1673. if (components.length > 0)
  1674. {
  1675. coloringComponent = components[0];
  1676. }
  1677. }
  1678. else
  1679. return false;
  1680. if (coloringComponent is null) return false;
  1681. Array<UIElement@> actions;
  1682. if (coloringComponent.typeName == "Light")
  1683. {
  1684. actions.Push(CreateContextMenuItem("Light color", "HandleColorWheelMenu", "menuLightColor"));
  1685. actions.Push(CreateContextMenuItem("Specular intensity", "HandleColorWheelMenu", "menuSpecularIntensity"));
  1686. actions.Push(CreateContextMenuItem("Brightness multiplier", "HandleColorWheelMenu", "menuBrightnessMultiplier"));
  1687. actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
  1688. }
  1689. else if (coloringComponent.typeName == "StaticModel")
  1690. {
  1691. actions.Push(CreateContextMenuItem("Diffuse color", "HandleColorWheelMenu", "menuDiffuseColor"));
  1692. actions.Push(CreateContextMenuItem("Specular color", "HandleColorWheelMenu", "menuSpecularColor"));
  1693. actions.Push(CreateContextMenuItem("Emissive color", "HandleColorWheelMenu", "menuEmissiveColor"));
  1694. actions.Push(CreateContextMenuItem("Environment map color", "HandleColorWheelMenu", "menuEnvironmentMapColor"));
  1695. actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
  1696. }
  1697. else if (coloringComponent.typeName == "Zone")
  1698. {
  1699. actions.Push(CreateContextMenuItem("Ambient color", "HandleColorWheelMenu", "menuAmbientColor"));
  1700. actions.Push(CreateContextMenuItem("Fog color", "HandleColorWheelMenu", "menuFogColor"));
  1701. actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
  1702. }
  1703. if (actions.length > 0) {
  1704. ActivateContextMenu(actions);
  1705. return true;
  1706. }
  1707. return false;
  1708. }
  1709. void HandleColorWheelMenu()
  1710. {
  1711. ColorWheelSetupBehaviorForColoring();
  1712. }
  1713. // color was changed, update color of all colorGroup for immediate preview;
  1714. void HandleWheelChangeColor(StringHash eventType, VariantMap& eventData)
  1715. {
  1716. if (timeToNextColoringGroupUpdate > time.systemTime) return;
  1717. if (coloringComponent !is null)
  1718. {
  1719. Color c = eventData["Color"].GetColor(); // current ColorWheel
  1720. // preview new color
  1721. if (coloringComponent.typeName == "Light")
  1722. {
  1723. Light@ light = cast<Light>(coloringComponent);
  1724. if (light !is null)
  1725. {
  1726. if (coloringPropertyName == "menuLightColor")
  1727. {
  1728. light.color = c;
  1729. }
  1730. else if (coloringPropertyName == "menuSpecularIntensity")
  1731. {
  1732. // multiply out
  1733. light.specularIntensity = c.Value() * 10.0f;
  1734. }
  1735. else if (coloringPropertyName == "menuBrightnessMultiplier")
  1736. {
  1737. light.brightness = c.Value() * 10.0f;
  1738. }
  1739. attributesDirty = true;
  1740. }
  1741. }
  1742. else if (coloringComponent.typeName == "StaticModel")
  1743. {
  1744. StaticModel@ model = cast<StaticModel>(coloringComponent);
  1745. if (model !is null)
  1746. {
  1747. Material@ mat = model.materials[0];
  1748. if (mat !is null)
  1749. {
  1750. if (coloringPropertyName == "menuDiffuseColor")
  1751. {
  1752. Variant oldValue = mat.shaderParameters["MatDiffColor"];
  1753. Variant newValue;
  1754. String valueString;
  1755. valueString += String(c.r).Substring(0,5);
  1756. valueString += " ";
  1757. valueString += String(c.g).Substring(0,5);
  1758. valueString += " ";
  1759. valueString += String(c.b).Substring(0,5);
  1760. valueString += " ";
  1761. valueString += String(c.a).Substring(0,5);
  1762. newValue.FromString(oldValue.type, valueString);
  1763. mat.shaderParameters["MatDiffColor"] = newValue;
  1764. }
  1765. else if (coloringPropertyName == "menuSpecularColor")
  1766. {
  1767. Variant oldValue = mat.shaderParameters["MatSpecColor"];
  1768. Variant newValue;
  1769. String valueString;
  1770. valueString += String(c.r).Substring(0,5);
  1771. valueString += " ";
  1772. valueString += String(c.g).Substring(0,5);
  1773. valueString += " ";
  1774. valueString += String(c.b).Substring(0,5);
  1775. valueString += " ";
  1776. valueString += String(c.a * 128).Substring(0,5);
  1777. newValue.FromString(oldValue.type, valueString);
  1778. mat.shaderParameters["MatSpecColor"] = newValue;
  1779. }
  1780. else if (coloringPropertyName == "menuEmissiveColor")
  1781. {
  1782. Variant oldValue = mat.shaderParameters["MatEmissiveColor"];
  1783. Variant newValue;
  1784. String valueString;
  1785. valueString += String(c.r).Substring(0,5);
  1786. valueString += " ";
  1787. valueString += String(c.g).Substring(0,5);
  1788. valueString += " ";
  1789. valueString += String(c.b).Substring(0,5);
  1790. valueString += " ";
  1791. valueString += String(c.a).Substring(0,5);
  1792. newValue.FromString(oldValue.type, valueString);
  1793. mat.shaderParameters["MatEmissiveColor"] = newValue;
  1794. }
  1795. else if (coloringPropertyName == "menuEnvironmentMapColor")
  1796. {
  1797. Variant oldValue = mat.shaderParameters["MatEnvMapColor"];
  1798. Variant newValue;
  1799. String valueString;
  1800. valueString += String(c.r).Substring(0,5);
  1801. valueString += " ";
  1802. valueString += String(c.g).Substring(0,5);
  1803. valueString += " ";
  1804. valueString += String(c.b).Substring(0,5);
  1805. valueString += " ";
  1806. valueString += String(c.a).Substring(0,5);
  1807. newValue.FromString(oldValue.type, valueString);
  1808. mat.shaderParameters["MatEnvMapColor"] = newValue;
  1809. }
  1810. }
  1811. }
  1812. }
  1813. else if (coloringComponent.typeName == "Zone")
  1814. {
  1815. Zone@ zone = cast<Zone>(coloringComponent);
  1816. if (zone !is null)
  1817. {
  1818. if (coloringPropertyName == "menuAmbientColor")
  1819. {
  1820. zone.ambientColor = c;
  1821. }
  1822. else if (coloringPropertyName == "menuFogColor")
  1823. {
  1824. zone.fogColor = c;
  1825. }
  1826. attributesDirty = true;
  1827. }
  1828. }
  1829. }
  1830. timeToNextColoringGroupUpdate = time.systemTime + stepColoringGroupUpdate;
  1831. }
  1832. // Return old colors, wheel was closed or color discarded
  1833. void HandleWheelDiscardColor(StringHash eventType, VariantMap& eventData)
  1834. {
  1835. if (coloringComponent !is null)
  1836. {
  1837. //Color oldColor = eventData["Color"].GetColor(); //Old color from ColorWheel from ShowColorWheelWithColor(old)
  1838. Color oldColor = coloringOldColor;
  1839. // preview new color
  1840. if (coloringComponent.typeName == "Light")
  1841. {
  1842. Light@ light = cast<Light>(coloringComponent);
  1843. if (light !is null)
  1844. {
  1845. if (coloringPropertyName == "menuLightColor")
  1846. {
  1847. light.color = oldColor;
  1848. }
  1849. else if (coloringPropertyName == "menuSpecularIntensity")
  1850. {
  1851. light.specularIntensity = coloringOldScalar * 10.0f;
  1852. }
  1853. else if (coloringPropertyName == "menuBrightnessMultiplier")
  1854. {
  1855. light.brightness = coloringOldScalar * 10.0f;
  1856. }
  1857. attributesDirty = true;
  1858. }
  1859. }
  1860. else if (coloringComponent.typeName == "StaticModel")
  1861. {
  1862. StaticModel@ model = cast<StaticModel>(coloringComponent);
  1863. if (model !is null)
  1864. {
  1865. Material@ mat = model.materials[0];
  1866. if (mat !is null)
  1867. {
  1868. if (coloringPropertyName == "menuDiffuseColor")
  1869. {
  1870. Variant oldValue = mat.shaderParameters["MatDiffColor"];
  1871. Variant newValue;
  1872. String valueString;
  1873. valueString += String(oldColor.r).Substring(0,5);
  1874. valueString += " ";
  1875. valueString += String(oldColor.g).Substring(0,5);
  1876. valueString += " ";
  1877. valueString += String(oldColor.b).Substring(0,5);
  1878. valueString += " ";
  1879. valueString += String(oldColor.a).Substring(0,5);
  1880. newValue.FromString(oldValue.type, valueString);
  1881. mat.shaderParameters["MatDiffColor"] = newValue;
  1882. }
  1883. else if (coloringPropertyName == "menuSpecularColor")
  1884. {
  1885. Variant oldValue = mat.shaderParameters["MatSpecColor"];
  1886. Variant newValue;
  1887. String valueString;
  1888. valueString += String(oldColor.r).Substring(0,5);
  1889. valueString += " ";
  1890. valueString += String(oldColor.g).Substring(0,5);
  1891. valueString += " ";
  1892. valueString += String(oldColor.b).Substring(0,5);
  1893. valueString += " ";
  1894. valueString += String(coloringOldScalar).Substring(0,5);
  1895. newValue.FromString(oldValue.type, valueString);
  1896. mat.shaderParameters["MatSpecColor"] = newValue;
  1897. }
  1898. else if (coloringPropertyName == "menuEmissiveColor")
  1899. {
  1900. Variant oldValue = mat.shaderParameters["MatEmissiveColor"];
  1901. Variant newValue;
  1902. String valueString;
  1903. valueString += String(oldColor.r).Substring(0,5);
  1904. valueString += " ";
  1905. valueString += String(oldColor.g).Substring(0,5);
  1906. valueString += " ";
  1907. valueString += String(oldColor.b).Substring(0,5);
  1908. valueString += " ";
  1909. valueString += String(oldColor.a).Substring(0,5);
  1910. newValue.FromString(oldValue.type, valueString);
  1911. mat.shaderParameters["MatEmissiveColor"] = newValue;
  1912. }
  1913. else if (coloringPropertyName == "menuEnvironmentMapColor")
  1914. {
  1915. Variant oldValue = mat.shaderParameters["MatEnvMapColor"];
  1916. Variant newValue;
  1917. String valueString;
  1918. valueString += String(oldColor.r).Substring(0,5);
  1919. valueString += " ";
  1920. valueString += String(oldColor.g).Substring(0,5);
  1921. valueString += " ";
  1922. valueString += String(oldColor.b).Substring(0,5);
  1923. valueString += " ";
  1924. valueString += String(oldColor.a).Substring(0,5);
  1925. newValue.FromString(oldValue.type, valueString);
  1926. mat.shaderParameters["MatEnvMapColor"] = newValue;
  1927. }
  1928. }
  1929. }
  1930. }
  1931. else if (coloringComponent.typeName == "Zone")
  1932. {
  1933. Zone@ zone = cast<Zone>(coloringComponent);
  1934. if (zone !is null)
  1935. {
  1936. if (coloringPropertyName == "menuAmbientColor")
  1937. {
  1938. zone.ambientColor = oldColor;
  1939. }
  1940. else if (coloringPropertyName == "menuFogColor")
  1941. {
  1942. zone.fogColor = oldColor;
  1943. }
  1944. attributesDirty = true;
  1945. }
  1946. }
  1947. }
  1948. }
  1949. // Applying color wheel changes to material
  1950. void HandleWheelSelectColor(StringHash eventType, VariantMap& eventData)
  1951. {
  1952. if (coloringComponent !is null)
  1953. if (coloringComponent.typeName == "StaticModel")
  1954. {
  1955. Color c = eventData["Color"].GetColor(); //Selected color from ColorWheel
  1956. StaticModel@ model = cast<StaticModel>(coloringComponent);
  1957. if (model !is null)
  1958. {
  1959. Material@ mat = model.materials[0];
  1960. if (mat !is null)
  1961. {
  1962. editMaterial = mat;
  1963. SaveMaterial();
  1964. }
  1965. }
  1966. }
  1967. }
  1968. bool ViewDebugIcons()
  1969. {
  1970. debugIconsShow = !debugIconsShow;
  1971. return true;
  1972. }