EditorUI.as 46 KB

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