EditorUI.as 78 KB

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