EditorUI.as 48 KB

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