EditorUI.as 58 KB

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