EditorUI.as 77 KB

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