EditorUI.as 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  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...")
  550. {
  551. CreateFileSelector("Export scene to OBJ", "Save", "Cancel", uiExportPath, uiExportPathFilters, uiExportFilter);
  552. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleExportSceneOBJ");
  553. }
  554. else if (action == "Export selected to OBJ...")
  555. {
  556. CreateFileSelector("Export selected to OBJ", "Save", "Cancel", uiExportPath, uiExportPathFilters, uiExportFilter);
  557. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleExportSelectedOBJ");
  558. }
  559. else if (action == "Run script...")
  560. {
  561. CreateFileSelector("Run script", "Run", "Cancel", uiScriptPath, uiScriptFilters, uiScriptFilter);
  562. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleRunScript");
  563. }
  564. else if (action == "Set resource path...")
  565. {
  566. CreateFileSelector("Set resource path", "Set", "Cancel", sceneResourcePath, uiAllFilters, 0);
  567. uiFileSelector.directoryMode = true;
  568. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleResourcePath");
  569. }
  570. // UI-element
  571. else if (action == "Open UI-layout...")
  572. {
  573. CreateFileSelector("Open UI-layout", "Open", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  574. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleOpenUILayoutFile");
  575. }
  576. else if (action == "Save UI-layout as..." || action == "Save UI-layout")
  577. {
  578. if (editUIElement !is null)
  579. {
  580. UIElement@ element = GetTopLevelUIElement(editUIElement);
  581. if (element is null)
  582. return false;
  583. CreateFileSelector("Save UI-layout as", "Save", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  584. uiFileSelector.fileName = GetFileNameAndExtension(element.GetVar(FILENAME_VAR).GetString());
  585. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveUILayoutFile");
  586. }
  587. }
  588. else if (action == "Load child element...")
  589. {
  590. if (editUIElement !is null)
  591. {
  592. CreateFileSelector("Load child element", "Load", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  593. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleLoadChildUIElementFile");
  594. }
  595. }
  596. else if (action == "Save child element as...")
  597. {
  598. if (editUIElement !is null)
  599. {
  600. CreateFileSelector("Save child element", "Save", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  601. uiFileSelector.fileName = GetFileNameAndExtension(editUIElement.GetVar(CHILD_ELEMENT_FILENAME_VAR).GetString());
  602. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveChildUIElementFile");
  603. }
  604. }
  605. else if (action == "Set default style...")
  606. {
  607. CreateFileSelector("Set default style", "Set", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
  608. SubscribeToEvent(uiFileSelector, "FileSelected", "HandleUIElementDefaultStyle");
  609. }
  610. return true;
  611. }
  612. bool PickNode()
  613. {
  614. Menu@ menu = GetEventSender();
  615. if (menu is null)
  616. return false;
  617. String action = GetActionName(menu.name);
  618. if (action.empty)
  619. return false;
  620. CreateNode(action == "Replicated node" ? REPLICATED : LOCAL);
  621. return true;
  622. }
  623. bool PickComponent()
  624. {
  625. if (editNodes.empty)
  626. return false;
  627. Menu@ menu = GetEventSender();
  628. if (menu is null)
  629. return false;
  630. String action = GetActionName(menu.name);
  631. if (action.empty)
  632. return false;
  633. CreateComponent(action);
  634. return true;
  635. }
  636. bool PickBuiltinObject()
  637. {
  638. Menu@ menu = GetEventSender();
  639. if (menu is null)
  640. return false;
  641. String action = GetActionName(menu.name);
  642. if (action.empty)
  643. return false;
  644. CreateBuiltinObject(action);
  645. return true;
  646. }
  647. bool PickUIElement()
  648. {
  649. Menu@ menu = GetEventSender();
  650. if (menu is null)
  651. return false;
  652. String action = GetActionName(menu.name);
  653. if (action.empty)
  654. return false;
  655. return NewUIElement(action);
  656. }
  657. // When calling items from the quick menu, they have "Create" prepended for clarity. Strip that now to get the object name to create
  658. String GetActionName(const String&in name)
  659. {
  660. if (name.StartsWith("Create"))
  661. return name.Substring(7);
  662. else
  663. return name;
  664. }
  665. void HandleMenuSelected(StringHash eventType, VariantMap& eventData)
  666. {
  667. Menu@ menu = eventData["Element"].GetPtr();
  668. if (menu is null)
  669. return;
  670. HandlePopup(menu);
  671. quickMenu.visible = false;
  672. quickMenu.enabled = false;
  673. // Execute the callback if available
  674. Variant variant = menu.GetVar(CALLBACK_VAR);
  675. if (!variant.empty)
  676. menuCallbacks[variant.GetUInt()]();
  677. }
  678. Menu@ CreateMenuItem(const String&in title, MENU_CALLBACK@ callback = null, int accelKey = 0, int accelQual = 0, bool addToQuickMenu = true, String quickMenuText="", bool autoLocalize = true)
  679. {
  680. Menu@ menu = Menu(title);
  681. menu.defaultStyle = uiStyle;
  682. menu.style = AUTO_STYLE;
  683. menu.SetLayout(LM_HORIZONTAL, 0, IntRect(8, 2, 8, 2));
  684. if (accelKey > 0)
  685. menu.SetAccelerator(accelKey, accelQual);
  686. if (callback !is null)
  687. {
  688. menu.vars[CALLBACK_VAR] = menuCallbacks.length;
  689. menuCallbacks.Push(callback);
  690. }
  691. Text@ menuText = Text();
  692. menu.AddChild(menuText);
  693. menuText.style = "EditorMenuText";
  694. menuText.text = title;
  695. menuText.autoLocalizable = autoLocalize;
  696. if (addToQuickMenu)
  697. AddQuickMenuItem(callback, quickMenuText.empty ? title : quickMenuText);
  698. if (accelKey != 0)
  699. {
  700. UIElement@ spacer = UIElement();
  701. spacer.minWidth = menuText.indentSpacing;
  702. spacer.height = menuText.height;
  703. menu.AddChild(spacer);
  704. menu.AddChild(CreateAccelKeyText(accelKey, accelQual));
  705. }
  706. return menu;
  707. }
  708. void AddQuickMenuItem(MENU_CALLBACK@ callback, String text)
  709. {
  710. if (callback is null)
  711. return;
  712. bool exists = false;
  713. for (uint i=0;i<quickMenuItems.length;++i)
  714. {
  715. if (quickMenuItems[i].action == text)
  716. {
  717. exists = true;
  718. break;
  719. }
  720. }
  721. if (!exists)
  722. quickMenuItems.Push(QuickMenuItem(text, callback));
  723. }
  724. 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="")
  725. {
  726. Menu@ menu = Menu(title);
  727. menu.defaultStyle = uiStyle;
  728. menu.style = AUTO_STYLE;
  729. menu.SetLayout(LM_VERTICAL, 0, IntRect(8, 2, 8, 2));
  730. if (accelKey > 0)
  731. menu.SetAccelerator(accelKey, accelQual);
  732. if (callback !is null)
  733. {
  734. menu.vars[CALLBACK_VAR] = menuCallbacks.length;
  735. menuCallbacks.Push(callback);
  736. }
  737. Text@ menuText = Text();
  738. menu.AddChild(menuText);
  739. menuText.style = "EditorMenuText";
  740. menuText.text = title;
  741. // If icon type is not provided, use the title instead
  742. IconizeUIElement(menuText, iconType.empty ? title : iconType);
  743. if (addToQuickMenu)
  744. AddQuickMenuItem(callback, quickMenuText.empty ? title : quickMenuText);
  745. if (accelKey != 0)
  746. {
  747. menuText.layoutMode = LM_HORIZONTAL;
  748. menuText.AddChild(CreateAccelKeyText(accelKey, accelQual));
  749. }
  750. return menu;
  751. }
  752. /// Create a child divider in parent with vertical layout mode. It works on other parent element as well, not just parent menu.
  753. void CreateChildDivider(UIElement@ parent)
  754. {
  755. BorderImage@ divider = parent.CreateChild("BorderImage", "Divider");
  756. divider.style = "EditorDivider";
  757. }
  758. Window@ CreatePopup(Menu@ baseMenu)
  759. {
  760. Window@ popup = Window();
  761. popup.defaultStyle = uiStyle;
  762. popup.style = AUTO_STYLE;
  763. popup.SetLayout(LM_VERTICAL, 1, IntRect(2, 6, 2, 6));
  764. baseMenu.popup = popup;
  765. baseMenu.popupOffset = IntVector2(0, baseMenu.height);
  766. return popup;
  767. }
  768. Menu@ CreateMenu(const String&in title)
  769. {
  770. Menu@ menu = CreateMenuItem(title);
  771. Text@ text = menu.children[0];
  772. menu.maxWidth = text.width + 20;
  773. CreatePopup(menu);
  774. return menu;
  775. }
  776. void HandleChangeLanguage(StringHash eventType, VariantMap& eventData)
  777. {
  778. Array<UIElement@> children = uiMenuBar.GetChildren();
  779. for (uint i = 0; i < children.length - 2; ++i) // last 2 elements is not menu
  780. {
  781. // dirty hack: force recalc text size
  782. children[i].maxWidth = 1000;
  783. Text@ text = children[i].children[0];
  784. text.minWidth = 0;
  785. text.maxWidth = 1;
  786. text.ApplyAttributes();
  787. children[i].maxWidth = text.width + 20;
  788. }
  789. RebuildResourceDatabase();
  790. }
  791. Text@ CreateAccelKeyText(int accelKey, int accelQual)
  792. {
  793. Text@ accelKeyText = Text();
  794. accelKeyText.defaultStyle = uiStyle;
  795. accelKeyText.style = "EditorMenuText";
  796. accelKeyText.textAlignment = HA_RIGHT;
  797. String text;
  798. if (accelKey == KEY_DELETE)
  799. text = "Del";
  800. else if (accelKey == KEY_SPACE)
  801. text = "Space";
  802. // Cannot use range as the key constants below do not appear to be in sequence
  803. else if (accelKey == KEY_F1)
  804. text = "F1";
  805. else if (accelKey == KEY_F2)
  806. text = "F2";
  807. else if (accelKey == KEY_F3)
  808. text = "F3";
  809. else if (accelKey == KEY_F4)
  810. text = "F4";
  811. else if (accelKey == KEY_F5)
  812. text = "F5";
  813. else if (accelKey == KEY_F6)
  814. text = "F6";
  815. else if (accelKey == KEY_F7)
  816. text = "F7";
  817. else if (accelKey == KEY_F8)
  818. text = "F8";
  819. else if (accelKey == KEY_F9)
  820. text = "F9";
  821. else if (accelKey == KEY_F10)
  822. text = "F10";
  823. else if (accelKey == KEY_F11)
  824. text = "F11";
  825. else if (accelKey == KEY_F12)
  826. text = "F12";
  827. else if (accelKey == SHOW_POPUP_INDICATOR)
  828. text = ">";
  829. else
  830. text.AppendUTF8(accelKey);
  831. if (accelQual & QUAL_ALT > 0)
  832. text = "Alt+" + text;
  833. if (accelQual & QUAL_SHIFT > 0)
  834. text = "Shift+" + text;
  835. if (accelQual & QUAL_CTRL > 0)
  836. text = "Ctrl+" + text;
  837. accelKeyText.text = text;
  838. return accelKeyText;
  839. }
  840. void FinalizedPopupMenu(Window@ popup)
  841. {
  842. // Find the maximum menu text width
  843. Array<UIElement@> children = popup.GetChildren();
  844. int maxWidth = 0;
  845. for (uint i = 0; i < children.length; ++i)
  846. {
  847. UIElement@ element = children[i];
  848. if (element.type != MENU_TYPE) // Skip if not menu item
  849. continue;
  850. int width = element.children[0].width;
  851. if (width > maxWidth)
  852. maxWidth = width;
  853. }
  854. // Adjust the indent spacing to slightly wider than the maximum width
  855. maxWidth += 20;
  856. for (uint i = 0; i < children.length; ++i)
  857. {
  858. UIElement@ element = children[i];
  859. if (element.type != MENU_TYPE)
  860. continue;
  861. Menu@ menu = element;
  862. Text@ menuText = menu.children[0];
  863. if (menuText.numChildren == 1) // Skip if menu text does not have accel
  864. menuText.children[0].indentSpacing = maxWidth;
  865. // Adjust the popup offset taking the indentation into effect
  866. if (menu.popup !is null)
  867. menu.popupOffset = IntVector2(menu.width, 0);
  868. }
  869. }
  870. void CreateFileSelector(const String&in title, const String&in ok, const String&in cancel, const String&in initialPath, Array<String>@ filters,
  871. uint initialFilter, bool autoLocalizeTitle = true)
  872. {
  873. // Within the editor UI, the file selector is a kind of a "singleton". When the previous one is overwritten, also
  874. // the events subscribed from it are disconnected, so new ones are safe to subscribe.
  875. uiFileSelector = FileSelector();
  876. uiFileSelector.defaultStyle = uiStyle;
  877. uiFileSelector.title = title;
  878. uiFileSelector.titleText.autoLocalizable = autoLocalizeTitle;
  879. uiFileSelector.path = initialPath;
  880. uiFileSelector.SetButtonTexts(ok, cancel);
  881. Text@ okText = cast<Text>(uiFileSelector.okButton.children[0]);
  882. okText.autoLocalizable = true;
  883. Text@ cancelText = cast<Text>(uiFileSelector.cancelButton.children[0]);
  884. cancelText.autoLocalizable = true;
  885. uiFileSelector.SetFilters(filters, initialFilter);
  886. CenterDialog(uiFileSelector.window);
  887. }
  888. void CloseFileSelector(uint&out filterIndex, String&out path)
  889. {
  890. // Save filter & path for next time
  891. filterIndex = uiFileSelector.filterIndex;
  892. path = uiFileSelector.path;
  893. uiFileSelector = null;
  894. }
  895. void CloseFileSelector()
  896. {
  897. uiFileSelector = null;
  898. }
  899. void CreateConsole()
  900. {
  901. Console@ console = engine.CreateConsole();
  902. console.defaultStyle = uiStyle;
  903. console.commandInterpreter = consoleCommandInterpreter;
  904. console.numBufferedRows = 100;
  905. console.autoVisibleOnError = true;
  906. }
  907. void CreateDebugHud()
  908. {
  909. engine.CreateDebugHud();
  910. debugHud.defaultStyle = uiStyle;
  911. debugHud.mode = DEBUGHUD_SHOW_NONE;
  912. }
  913. void CenterDialog(UIElement@ element)
  914. {
  915. IntVector2 size = element.size;
  916. element.SetPosition((graphics.width - size.x) / 2, (graphics.height - size.y) / 2);
  917. }
  918. void CreateContextMenu()
  919. {
  920. contextMenu = LoadEditorUI("UI/EditorContextMenu.xml");
  921. ui.root.AddChild(contextMenu);
  922. }
  923. void UpdateWindowTitle()
  924. {
  925. String sceneName = GetFileNameAndExtension(editorScene.fileName);
  926. if (sceneName.empty || sceneName == TEMP_SCENE_NAME || sceneName == TEMP_BINARY_SCENE_NAME)
  927. sceneName = "Untitled";
  928. if (sceneModified)
  929. sceneName += "*";
  930. graphics.windowTitle = "Urho3D editor - " + sceneName;
  931. }
  932. void HandlePopup(Menu@ menu)
  933. {
  934. // Close the top level menu now unless the selected menu item has another popup
  935. if (menu.popup !is null)
  936. return;
  937. for (;;)
  938. {
  939. UIElement@ menuParent = menu.parent;
  940. if (menuParent is null)
  941. break;
  942. Menu@ nextMenu = menuParent.vars["Origin"].GetPtr();
  943. if (nextMenu is null)
  944. break;
  945. else
  946. menu = nextMenu;
  947. }
  948. if (menu.parent is uiMenuBar)
  949. menu.showPopup = false;
  950. }
  951. String ExtractFileName(VariantMap& eventData, bool forSave = false)
  952. {
  953. String fileName;
  954. // Check for OK
  955. if (eventData["OK"].GetBool())
  956. {
  957. String filter = eventData["Filter"].GetString();
  958. fileName = eventData["FileName"].GetString();
  959. // Add default extension for saving if not specified
  960. if (GetExtension(fileName).empty && forSave && filter != "*.*")
  961. fileName = fileName + filter.Substring(1);
  962. }
  963. return fileName;
  964. }
  965. void HandleOpenSceneFile(StringHash eventType, VariantMap& eventData)
  966. {
  967. CloseFileSelector(uiSceneFilter, uiScenePath);
  968. LoadScene(ExtractFileName(eventData));
  969. }
  970. void HandleSaveSceneFile(StringHash eventType, VariantMap& eventData)
  971. {
  972. CloseFileSelector(uiSceneFilter, uiScenePath);
  973. SaveScene(ExtractFileName(eventData, true));
  974. }
  975. void HandleLoadNodeFile(StringHash eventType, VariantMap& eventData)
  976. {
  977. CloseFileSelector(uiNodeFilter, uiNodePath);
  978. LoadNode(ExtractFileName(eventData));
  979. }
  980. void HandleSaveNodeFile(StringHash eventType, VariantMap& eventData)
  981. {
  982. CloseFileSelector(uiNodeFilter, uiNodePath);
  983. SaveNode(ExtractFileName(eventData, true));
  984. }
  985. void HandleImportModel(StringHash eventType, VariantMap& eventData)
  986. {
  987. CloseFileSelector(uiImportFilter, uiImportPath);
  988. ImportModel(ExtractFileName(eventData));
  989. }
  990. void HandleImportScene(StringHash eventType, VariantMap& eventData)
  991. {
  992. CloseFileSelector(uiImportFilter, uiImportPath);
  993. ImportScene(ExtractFileName(eventData));
  994. }
  995. void HandleExportSceneOBJ(StringHash eventType, VariantMap& eventData)
  996. {
  997. CloseFileSelector(uiExportFilter, uiExportPath);
  998. ExportSceneToOBJ(ExtractFileName(eventData));
  999. }
  1000. void HandleExportSelectedOBJ(StringHash eventType, VariantMap& eventData)
  1001. {
  1002. CloseFileSelector(uiExportFilter, uiExportPath);
  1003. ExportSelectedToOBJ(ExtractFileName(eventData));
  1004. }
  1005. void ExecuteScript(const String&in fileName)
  1006. {
  1007. if (fileName.empty)
  1008. return;
  1009. File@ file = File(fileName, FILE_READ);
  1010. if (file.open)
  1011. {
  1012. String scriptCode;
  1013. while (!file.eof)
  1014. scriptCode += file.ReadLine() + "\n";
  1015. file.Close();
  1016. if (script.Execute(scriptCode))
  1017. log.Info("Script " + fileName + " ran successfully");
  1018. }
  1019. }
  1020. void HandleRunScript(StringHash eventType, VariantMap& eventData)
  1021. {
  1022. CloseFileSelector(uiScriptFilter, uiScriptPath);
  1023. ExecuteScript(ExtractFileName(eventData));
  1024. }
  1025. void HandleResourcePath(StringHash eventType, VariantMap& eventData)
  1026. {
  1027. String pathName = uiFileSelector.path;
  1028. CloseFileSelector();
  1029. if (eventData["OK"].GetBool())
  1030. SetResourcePath(pathName, false);
  1031. }
  1032. void HandleOpenUILayoutFile(StringHash eventType, VariantMap& eventData)
  1033. {
  1034. CloseFileSelector(uiElementFilter, uiElementPath);
  1035. OpenUILayout(ExtractFileName(eventData));
  1036. }
  1037. void HandleSaveUILayoutFile(StringHash eventType, VariantMap& eventData)
  1038. {
  1039. CloseFileSelector(uiElementFilter, uiElementPath);
  1040. SaveUILayout(ExtractFileName(eventData, true));
  1041. }
  1042. void HandleLoadChildUIElementFile(StringHash eventType, VariantMap& eventData)
  1043. {
  1044. CloseFileSelector(uiElementFilter, uiElementPath);
  1045. LoadChildUIElement(ExtractFileName(eventData));
  1046. }
  1047. void HandleSaveChildUIElementFile(StringHash eventType, VariantMap& eventData)
  1048. {
  1049. CloseFileSelector(uiElementFilter, uiElementPath);
  1050. SaveChildUIElement(ExtractFileName(eventData, true));
  1051. }
  1052. void HandleUIElementDefaultStyle(StringHash eventType, VariantMap& eventData)
  1053. {
  1054. CloseFileSelector(uiElementFilter, uiElementPath);
  1055. SetUIElementDefaultStyle(ExtractFileName(eventData));
  1056. }
  1057. void HandleHotKeysBlender( VariantMap& eventData)
  1058. {
  1059. int key = eventData["Key"].GetInt();
  1060. int viewDirection = eventData["Qualifiers"].GetInt() == QUAL_CTRL ? -1 : 1;
  1061. if (key == KEY_ESC)
  1062. {
  1063. if (uiHidden)
  1064. UnhideUI();
  1065. else if (console.visible)
  1066. console.visible = false;
  1067. else if (contextMenu.visible)
  1068. CloseContextMenu();
  1069. else if (quickMenu.visible)
  1070. {
  1071. quickMenu.visible = false;
  1072. quickMenu.enabled = false;
  1073. }
  1074. else
  1075. {
  1076. UIElement@ front = ui.frontElement;
  1077. if (front is settingsDialog || front is preferencesDialog)
  1078. {
  1079. ui.focusElement = null;
  1080. front.visible = false;
  1081. }
  1082. }
  1083. }
  1084. // Ignore other keys when UI has a modal element
  1085. else if (ui.HasModalElement())
  1086. return;
  1087. else if (key == KEY_F1)
  1088. console.Toggle();
  1089. else if (key == KEY_F2)
  1090. ToggleRenderingDebug();
  1091. else if (key == KEY_F3)
  1092. TogglePhysicsDebug();
  1093. else if (key == KEY_F4)
  1094. ToggleOctreeDebug();
  1095. else if (key == KEY_F11)
  1096. {
  1097. Image@ screenshot = Image();
  1098. graphics.TakeScreenShot(screenshot);
  1099. if (!fileSystem.DirExists(screenshotDir))
  1100. fileSystem.CreateDir(screenshotDir);
  1101. screenshot.SavePNG(screenshotDir + "/Screenshot_" +
  1102. time.timeStamp.Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
  1103. }
  1104. else if (key == KEY_KP_1 && ui.focusElement is null) // Front view
  1105. {
  1106. Vector3 center = Vector3(0,0,0);
  1107. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1108. center = SelectedNodesCenterPoint();
  1109. Vector3 pos = cameraNode.worldPosition - center;
  1110. cameraNode.worldPosition = center - Vector3(0.0, 0.0, pos.length * viewDirection);
  1111. cameraNode.direction = Vector3(0, 0, viewDirection);
  1112. ReacquireCameraYawPitch();
  1113. }
  1114. else if (key == KEY_KP_3 && ui.focusElement is null) // Side view
  1115. {
  1116. Vector3 center = Vector3(0,0,0);
  1117. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1118. center = SelectedNodesCenterPoint();
  1119. Vector3 pos = cameraNode.worldPosition - center;
  1120. cameraNode.worldPosition = center - Vector3(pos.length * -viewDirection, 0.0, 0.0);
  1121. cameraNode.direction = Vector3(-viewDirection, 0, 0);
  1122. ReacquireCameraYawPitch();
  1123. }
  1124. else if (key == KEY_KP_7 && ui.focusElement is null) // Top view
  1125. {
  1126. Vector3 center = Vector3(0,0,0);
  1127. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1128. center = SelectedNodesCenterPoint();
  1129. Vector3 pos = cameraNode.worldPosition - center;
  1130. cameraNode.worldPosition = center - Vector3(0.0, pos.length * -viewDirection, 0.0);
  1131. cameraNode.direction = Vector3(0, -viewDirection, 0);
  1132. ReacquireCameraYawPitch();
  1133. }
  1134. else if (key == KEY_KP_5 && ui.focusElement is null)
  1135. {
  1136. activeViewport.camera.zoom = 1;
  1137. activeViewport.ToggleOrthographic();
  1138. }
  1139. else if (key == '4')
  1140. editMode = EDIT_SELECT;
  1141. else if (key == '5')
  1142. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1143. else if (key == '6')
  1144. {
  1145. --pickMode;
  1146. if (pickMode < PICK_GEOMETRIES)
  1147. pickMode = MAX_PICK_MODES - 1;
  1148. }
  1149. else if (key == '7')
  1150. {
  1151. ++pickMode;
  1152. if (pickMode >= MAX_PICK_MODES)
  1153. pickMode = PICK_GEOMETRIES;
  1154. }
  1155. else if (key == 'Z' && eventData["Qualifiers"].GetInt() != QUAL_CTRL)
  1156. {
  1157. fillMode = FillMode(fillMode + 1);
  1158. if (fillMode > FILL_POINT)
  1159. fillMode = FILL_SOLID;
  1160. // Update camera fill mode
  1161. SetFillMode(fillMode);
  1162. }
  1163. else if (key == KEY_SPACE)
  1164. {
  1165. if (ui.cursor.visible && ui.focusElement is null)
  1166. ToggleQuickMenu();
  1167. }
  1168. else
  1169. {
  1170. SteppedObjectManipulation(key);
  1171. }
  1172. if ((ui.focusElement is null) && (selectedNodes.length > 0) && !cameraFlyMode)
  1173. {
  1174. if (eventData["Qualifiers"].GetInt() == QUAL_ALT) // reset transformations
  1175. {
  1176. if (key == KEY_G)
  1177. SceneResetPosition();
  1178. else if (key == KEY_R)
  1179. SceneResetRotation();
  1180. else if (key == KEY_S)
  1181. SceneResetScale();
  1182. else if (key == KEY_F)
  1183. {
  1184. Vector3 center = Vector3(0,0,0);
  1185. if (selectedNodes.length > 0)
  1186. center = SelectedNodesCenterPoint();
  1187. cameraNode.LookAt(center);
  1188. ReacquireCameraYawPitch();
  1189. }
  1190. }
  1191. else if (eventData["Qualifiers"].GetInt() != QUAL_CTRL) // set transformations
  1192. {
  1193. if (key == KEY_G)
  1194. {
  1195. editMode = EDIT_MOVE;
  1196. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1197. }
  1198. else if (key == KEY_R)
  1199. {
  1200. editMode = EDIT_ROTATE;
  1201. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1202. }
  1203. else if (key == KEY_S)
  1204. {
  1205. editMode = EDIT_SCALE;
  1206. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1207. }
  1208. else if (key == KEY_F)
  1209. {
  1210. if (camera.orthographic)
  1211. {
  1212. viewCloser = true;
  1213. }
  1214. else
  1215. {
  1216. Vector3 center = Vector3(0,0,0);
  1217. if (selectedNodes.length > 0)
  1218. center = SelectedNodesCenterPoint();
  1219. cameraNode.LookAt(center);
  1220. ReacquireCameraYawPitch();
  1221. }
  1222. }
  1223. }
  1224. }
  1225. toolBarDirty = true;
  1226. }
  1227. void HandleHotKeysStandard(VariantMap& eventData)
  1228. {
  1229. int key = eventData["Key"].GetInt();
  1230. int viewDirection = eventData["Qualifiers"].GetInt() == QUAL_CTRL ? -1 : 1;
  1231. if (key == KEY_ESC)
  1232. {
  1233. if (uiHidden)
  1234. UnhideUI();
  1235. else if (console.visible)
  1236. console.visible = false;
  1237. else if (contextMenu.visible)
  1238. CloseContextMenu();
  1239. else if (quickMenu.visible)
  1240. {
  1241. quickMenu.visible = false;
  1242. quickMenu.enabled = false;
  1243. }
  1244. else
  1245. {
  1246. UIElement@ front = ui.frontElement;
  1247. if (front is settingsDialog || front is preferencesDialog)
  1248. {
  1249. ui.focusElement = null;
  1250. front.visible = false;
  1251. }
  1252. }
  1253. }
  1254. // Ignore other keys when UI has a modal element
  1255. else if (ui.HasModalElement())
  1256. return;
  1257. else if (key == KEY_F1)
  1258. console.Toggle();
  1259. else if (key == KEY_F2)
  1260. ToggleRenderingDebug();
  1261. else if (key == KEY_F3)
  1262. TogglePhysicsDebug();
  1263. else if (key == KEY_F4)
  1264. ToggleOctreeDebug();
  1265. else if (key == KEY_F11)
  1266. {
  1267. Image@ screenshot = Image();
  1268. graphics.TakeScreenShot(screenshot);
  1269. if (!fileSystem.DirExists(screenshotDir))
  1270. fileSystem.CreateDir(screenshotDir);
  1271. screenshot.SavePNG(screenshotDir + "/Screenshot_" +
  1272. time.timeStamp.Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
  1273. }
  1274. else if (key == KEY_KP_1 && ui.focusElement is null) // Front view
  1275. {
  1276. Vector3 center = Vector3(0,0,0);
  1277. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1278. center = SelectedNodesCenterPoint();
  1279. Vector3 pos = cameraNode.worldPosition - center;
  1280. cameraNode.worldPosition = center - Vector3(0.0, 0.0, pos.length * viewDirection);
  1281. cameraNode.direction = Vector3(0, 0, viewDirection);
  1282. ReacquireCameraYawPitch();
  1283. }
  1284. else if (key == KEY_KP_3 && ui.focusElement is null) // Side view
  1285. {
  1286. Vector3 center = Vector3(0,0,0);
  1287. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1288. center = SelectedNodesCenterPoint();
  1289. Vector3 pos = cameraNode.worldPosition - center;
  1290. cameraNode.worldPosition = center - Vector3(pos.length * -viewDirection, 0.0, 0.0);
  1291. cameraNode.direction = Vector3(-viewDirection, 0, 0);
  1292. ReacquireCameraYawPitch();
  1293. }
  1294. else if (key == KEY_KP_7 && ui.focusElement is null) // Top view
  1295. {
  1296. Vector3 center = Vector3(0,0,0);
  1297. if (selectedNodes.length > 0 || selectedComponents.length > 0)
  1298. center = SelectedNodesCenterPoint();
  1299. Vector3 pos = cameraNode.worldPosition - center;
  1300. cameraNode.worldPosition = center - Vector3(0.0, pos.length * -viewDirection, 0.0);
  1301. cameraNode.direction = Vector3(0, -viewDirection, 0);
  1302. ReacquireCameraYawPitch();
  1303. }
  1304. else if (key == KEY_KP_5 && ui.focusElement is null)
  1305. {
  1306. activeViewport.ToggleOrthographic();
  1307. }
  1308. else if (eventData["Qualifiers"].GetInt() == QUAL_CTRL)
  1309. {
  1310. if (key == '1')
  1311. editMode = EDIT_MOVE;
  1312. else if (key == '2')
  1313. editMode = EDIT_ROTATE;
  1314. else if (key == '3')
  1315. editMode = EDIT_SCALE;
  1316. else if (key == '4')
  1317. editMode = EDIT_SELECT;
  1318. else if (key == '5')
  1319. axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
  1320. else if (key == '6')
  1321. {
  1322. --pickMode;
  1323. if (pickMode < PICK_GEOMETRIES)
  1324. pickMode = MAX_PICK_MODES - 1;
  1325. }
  1326. else if (key == '7')
  1327. {
  1328. ++pickMode;
  1329. if (pickMode >= MAX_PICK_MODES)
  1330. pickMode = PICK_GEOMETRIES;
  1331. }
  1332. else if (key == 'W')
  1333. {
  1334. fillMode = FillMode(fillMode + 1);
  1335. if (fillMode > FILL_POINT)
  1336. fillMode = FILL_SOLID;
  1337. // Update camera fill mode
  1338. SetFillMode(fillMode);
  1339. }
  1340. else if (key == KEY_SPACE)
  1341. {
  1342. if (ui.cursor.visible)
  1343. ToggleQuickMenu();
  1344. }
  1345. else
  1346. SteppedObjectManipulation(key);
  1347. toolBarDirty = true;
  1348. }
  1349. }
  1350. void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  1351. {
  1352. if (hotKeyMode == HOTKEYS_MODE_STANDARD)
  1353. {
  1354. HandleHotKeysStandard(eventData);
  1355. }
  1356. else if( hotKeyMode == HOTKEYS_MODE_BLENDER)
  1357. {
  1358. HandleHotKeysBlender(eventData);
  1359. }
  1360. }
  1361. void UnfadeUI()
  1362. {
  1363. FadeUI(false);
  1364. }
  1365. void FadeUI(bool fade = true)
  1366. {
  1367. if (uiHidden || uiFaded == fade)
  1368. return;
  1369. float opacity = (uiFaded = fade) ? uiMinOpacity : uiMaxOpacity;
  1370. Array<UIElement@> children = ui.root.GetChildren();
  1371. for (uint i = 0; i < children.length; ++i)
  1372. {
  1373. // Texts, popup&modal windows (which are anyway only in ui.modalRoot), and editorUIElement are excluded
  1374. if (children[i].type != TEXT_TYPE && children[i] !is editorUIElement)
  1375. children[i].opacity = opacity;
  1376. }
  1377. }
  1378. bool ToggleUI()
  1379. {
  1380. HideUI(!uiHidden);
  1381. return true;
  1382. }
  1383. void UnhideUI()
  1384. {
  1385. HideUI(false);
  1386. }
  1387. void HideUI(bool hide = true)
  1388. {
  1389. if (uiHidden == hide)
  1390. return;
  1391. // Note: we could set ui.root.visible = false and it would hide the whole hierarchy.
  1392. // However in this case we need the editorUIElement to stay visible
  1393. bool visible = !(uiHidden = hide);
  1394. Array<UIElement@> children = ui.root.GetChildren();
  1395. for (uint i = 0; i < children.length; ++i)
  1396. {
  1397. // Cursor and editorUIElement are excluded
  1398. if (children[i].type != CURSOR_TYPE && children[i] !is editorUIElement)
  1399. {
  1400. if (visible)
  1401. {
  1402. if (!children[i].visible)
  1403. children[i].visible = children[i].vars["HideUI"].GetBool();
  1404. }
  1405. else
  1406. {
  1407. children[i].vars["HideUI"] = children[i].visible;
  1408. children[i].visible = false;
  1409. }
  1410. }
  1411. }
  1412. }
  1413. void IconizeUIElement(UIElement@ element, const String&in iconType)
  1414. {
  1415. // Check if the icon has been created before
  1416. BorderImage@ icon = element.GetChild("Icon");
  1417. // If iconType is empty, it is a request to remove the existing icon
  1418. if (iconType.empty)
  1419. {
  1420. // Remove the icon if it exists
  1421. if (icon !is null)
  1422. icon.Remove();
  1423. // Revert back the indent but only if it is indented by this function
  1424. if (element.vars[INDENT_MODIFIED_BY_ICON_VAR].GetBool())
  1425. element.indent = 0;
  1426. return;
  1427. }
  1428. // The UI element must itself has been indented to reserve the space for the icon
  1429. if (element.indent == 0)
  1430. {
  1431. element.indent = 1;
  1432. element.vars[INDENT_MODIFIED_BY_ICON_VAR] = true;
  1433. }
  1434. // If no icon yet then create one with the correct indent and size in respect to the UI element
  1435. if (icon is null)
  1436. {
  1437. // The icon is placed at one indent level less than the UI element
  1438. icon = BorderImage("Icon");
  1439. icon.indent = element.indent - 1;
  1440. icon.SetFixedSize(element.indentWidth - 2, 14);
  1441. element.InsertChild(0, icon); // Ensure icon is added as the first child
  1442. }
  1443. // Set the icon type
  1444. if (!icon.SetStyle(iconType, iconStyle))
  1445. icon.SetStyle("Unknown", iconStyle); // If fails then use an 'unknown' icon type
  1446. icon.color = Color(1,1,1,1); // Reset to enabled color
  1447. }
  1448. void SetIconEnabledColor(UIElement@ element, bool enabled, bool partial = false)
  1449. {
  1450. BorderImage@ icon = element.GetChild("Icon");
  1451. if (icon !is null)
  1452. {
  1453. if (partial)
  1454. {
  1455. icon.colors[C_TOPLEFT] = Color(1,1,1,1);
  1456. icon.colors[C_BOTTOMLEFT] = Color(1,1,1,1);
  1457. icon.colors[C_TOPRIGHT] = Color(1,0,0,1);
  1458. icon.colors[C_BOTTOMRIGHT] = Color(1,0,0,1);
  1459. }
  1460. else
  1461. icon.color = enabled ? Color(1,1,1,1) : Color(1,0,0,1);
  1462. }
  1463. }
  1464. void UpdateDirtyUI()
  1465. {
  1466. UpdateDirtyToolBar();
  1467. // Perform hierarchy selection latently after the new selections are finalized (used in undo/redo action)
  1468. if (!hierarchyUpdateSelections.empty)
  1469. {
  1470. hierarchyList.SetSelections(hierarchyUpdateSelections);
  1471. hierarchyUpdateSelections.Clear();
  1472. }
  1473. // Perform some event-triggered updates latently in case a large hierarchy was changed
  1474. if (attributesFullDirty || attributesDirty)
  1475. UpdateAttributeInspector(attributesFullDirty);
  1476. }
  1477. void HandleMessageAcknowledgement(StringHash eventType, VariantMap& eventData)
  1478. {
  1479. if (eventData["Ok"].GetBool())
  1480. messageBoxCallback();
  1481. else
  1482. messageBoxCallback = null;
  1483. }
  1484. void PopulateMruScenes()
  1485. {
  1486. mruScenesPopup.RemoveAllChildren();
  1487. if (uiRecentScenes.length > 0)
  1488. {
  1489. recentSceneMenu.enabled = true;
  1490. for (uint i=0; i < uiRecentScenes.length; ++i)
  1491. mruScenesPopup.AddChild(CreateMenuItem(uiRecentScenes[i], @LoadMostRecentScene, 0, 0, false, "", false));
  1492. }
  1493. else
  1494. recentSceneMenu.enabled = false;
  1495. }
  1496. bool LoadMostRecentScene()
  1497. {
  1498. Menu@ menu = GetEventSender();
  1499. if (menu is null)
  1500. return false;
  1501. Text@ text = menu.GetChildren()[0];
  1502. if (text is null)
  1503. return false;
  1504. return LoadScene(text.text);
  1505. }
  1506. // Set from click to false if opening menu procedurally.
  1507. void OpenContextMenu(bool fromClick=true)
  1508. {
  1509. if (contextMenu is null)
  1510. return;
  1511. contextMenu.enabled = true;
  1512. contextMenu.visible = true;
  1513. contextMenu.BringToFront();
  1514. if (fromClick)
  1515. contextMenuActionWaitFrame=true;
  1516. }
  1517. void CloseContextMenu()
  1518. {
  1519. if (contextMenu is null)
  1520. return;
  1521. contextMenu.enabled = false;
  1522. contextMenu.visible = false;
  1523. }
  1524. void ActivateContextMenu(Array<UIElement@> actions)
  1525. {
  1526. contextMenu.RemoveAllChildren();
  1527. for (uint i=0; i< actions.length; ++i)
  1528. {
  1529. contextMenu.AddChild(actions[i]);
  1530. }
  1531. contextMenu.SetFixedHeight(24*actions.length+6);
  1532. contextMenu.position = ui.cursor.screenPosition + IntVector2(10,-10);
  1533. OpenContextMenu();
  1534. }
  1535. Menu@ CreateContextMenuItem(String text, String handler, String menuName = "", bool autoLocalize = true)
  1536. {
  1537. Menu@ menu = Menu();
  1538. menu.defaultStyle = uiStyle;
  1539. menu.style = AUTO_STYLE;
  1540. menu.name = menuName;
  1541. menu.SetLayout(LM_HORIZONTAL, 0, IntRect(8, 2, 8, 2));
  1542. Text@ menuText = Text();
  1543. menuText.style = "EditorMenuText";
  1544. menu.AddChild(menuText);
  1545. menuText.text = text;
  1546. menuText.autoLocalizable = autoLocalize;
  1547. menu.vars[VAR_CONTEXT_MENU_HANDLER] = handler;
  1548. SubscribeToEvent(menu, "Released", "ContextMenuEventWrapper");
  1549. return menu;
  1550. }
  1551. void ContextMenuEventWrapper(StringHash eventType, VariantMap& eventData)
  1552. {
  1553. UIElement@ uiElement = eventData["Element"].GetPtr();
  1554. if (uiElement is null)
  1555. return;
  1556. String handler = uiElement.vars[VAR_CONTEXT_MENU_HANDLER].GetString();
  1557. if (!handler.empty)
  1558. {
  1559. SubscribeToEvent(uiElement, "Released", handler);
  1560. uiElement.SendEvent("Released", eventData);
  1561. }
  1562. CloseContextMenu();
  1563. }
  1564. /// Load a UI XML file used by the editor
  1565. XMLFile@ GetEditorUIXMLFile(const String&in fileName)
  1566. {
  1567. // Prefer the executable path to avoid using the user's resource path, which may point
  1568. // to an outdated Urho installation
  1569. String fullFileName = fileSystem.programDir + "Data/" + fileName;
  1570. if (fileSystem.FileExists(fullFileName))
  1571. {
  1572. File@ file = File(fullFileName, FILE_READ);
  1573. XMLFile@ xml = XMLFile();
  1574. xml.name = fileName;
  1575. if (xml.Load(file))
  1576. return xml;
  1577. }
  1578. // Fallback to resource system
  1579. return cache.GetResource("XMLFile", fileName);
  1580. }
  1581. /// Load an UI layout used by the editor
  1582. UIElement@ LoadEditorUI(const String&in fileName)
  1583. {
  1584. return ui.LoadLayout(GetEditorUIXMLFile(fileName));
  1585. }
  1586. /// Set node children as a spline path, either cyclic or non-cyclic
  1587. bool SetSplinePath()
  1588. {
  1589. Menu@ menu = GetEventSender();
  1590. if (menu is null)
  1591. return false;
  1592. return SceneSetChildrenSplinePath(menu.name == "Cyclic");
  1593. }
  1594. bool ColorWheelBuildMenuSelectTypeColor()
  1595. {
  1596. if (selectedNodes.empty && selectedComponents.empty) return false;
  1597. editMode = EDIT_SELECT;
  1598. // do coloring only for single selected object
  1599. // start with trying to find single component
  1600. if (selectedComponents.length == 1)
  1601. {
  1602. coloringComponent = selectedComponents[0];
  1603. }
  1604. // else try to get first component from selected node
  1605. else if (selectedNodes.length == 1)
  1606. {
  1607. Array<Component@> components = selectedNodes[0].GetComponents();
  1608. if (components.length > 0)
  1609. {
  1610. coloringComponent = components[0];
  1611. }
  1612. }
  1613. else
  1614. return false;
  1615. if (coloringComponent is null) return false;
  1616. Array<UIElement@> actions;
  1617. if (coloringComponent.typeName == "Light")
  1618. {
  1619. actions.Push(CreateContextMenuItem("Light color", "HandleColorWheelMenu", "menuLightColor"));
  1620. actions.Push(CreateContextMenuItem("Specular intensity", "HandleColorWheelMenu", "menuSpecularIntensity"));
  1621. actions.Push(CreateContextMenuItem("Brightness multiplier", "HandleColorWheelMenu", "menuBrightnessMultiplier"));
  1622. actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
  1623. }
  1624. else if (coloringComponent.typeName == "StaticModel")
  1625. {
  1626. actions.Push(CreateContextMenuItem("Diffuse color", "HandleColorWheelMenu", "menuDiffuseColor"));
  1627. actions.Push(CreateContextMenuItem("Specular color", "HandleColorWheelMenu", "menuSpecularColor"));
  1628. actions.Push(CreateContextMenuItem("Emissive color", "HandleColorWheelMenu", "menuEmissiveColor"));
  1629. actions.Push(CreateContextMenuItem("Environment map color", "HandleColorWheelMenu", "menuEnvironmentMapColor"));
  1630. actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
  1631. }
  1632. else if (coloringComponent.typeName == "Zone")
  1633. {
  1634. actions.Push(CreateContextMenuItem("Ambient color", "HandleColorWheelMenu", "menuAmbientColor"));
  1635. actions.Push(CreateContextMenuItem("Fog color", "HandleColorWheelMenu", "menuFogColor"));
  1636. actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
  1637. }
  1638. if (actions.length > 0) {
  1639. ActivateContextMenu(actions);
  1640. return true;
  1641. }
  1642. return false;
  1643. }
  1644. void HandleColorWheelMenu()
  1645. {
  1646. ColorWheelSetupBehaviorForColoring();
  1647. }
  1648. // color was changed, update color of all colorGroup for immediate preview;
  1649. void HandleWheelChangeColor(StringHash eventType, VariantMap& eventData)
  1650. {
  1651. if (timeToNextColoringGroupUpdate > time.systemTime) return;
  1652. if (coloringComponent !is null)
  1653. {
  1654. Color c = eventData["Color"].GetColor(); // current ColorWheel
  1655. // preview new color
  1656. if (coloringComponent.typeName == "Light")
  1657. {
  1658. Light@ light = cast<Light>(coloringComponent);
  1659. if (light !is null)
  1660. {
  1661. if (coloringPropertyName == "menuLightColor")
  1662. {
  1663. light.color = c;
  1664. }
  1665. else if (coloringPropertyName == "menuSpecularIntensity")
  1666. {
  1667. // multiply out
  1668. light.specularIntensity = c.Value() * 10.0f;
  1669. }
  1670. else if (coloringPropertyName == "menuBrightnessMultiplier")
  1671. {
  1672. light.brightness = c.Value() * 10.0f;
  1673. }
  1674. attributesDirty = true;
  1675. }
  1676. }
  1677. else if (coloringComponent.typeName == "StaticModel")
  1678. {
  1679. StaticModel@ model = cast<StaticModel>(coloringComponent);
  1680. if (model !is null)
  1681. {
  1682. Material@ mat = model.materials[0];
  1683. if (mat !is null)
  1684. {
  1685. if (coloringPropertyName == "menuDiffuseColor")
  1686. {
  1687. Variant oldValue = mat.shaderParameters["MatDiffColor"];
  1688. Variant newValue;
  1689. String valueString;
  1690. valueString += String(c.r).Substring(0,5);
  1691. valueString += " ";
  1692. valueString += String(c.g).Substring(0,5);
  1693. valueString += " ";
  1694. valueString += String(c.b).Substring(0,5);
  1695. valueString += " ";
  1696. valueString += String(c.a).Substring(0,5);
  1697. newValue.FromString(oldValue.type, valueString);
  1698. mat.shaderParameters["MatDiffColor"] = newValue;
  1699. }
  1700. else if (coloringPropertyName == "menuSpecularColor")
  1701. {
  1702. Variant oldValue = mat.shaderParameters["MatSpecColor"];
  1703. Variant newValue;
  1704. String valueString;
  1705. valueString += String(c.r).Substring(0,5);
  1706. valueString += " ";
  1707. valueString += String(c.g).Substring(0,5);
  1708. valueString += " ";
  1709. valueString += String(c.b).Substring(0,5);
  1710. valueString += " ";
  1711. valueString += String(c.a * 128).Substring(0,5);
  1712. newValue.FromString(oldValue.type, valueString);
  1713. mat.shaderParameters["MatSpecColor"] = newValue;
  1714. }
  1715. else if (coloringPropertyName == "menuEmissiveColor")
  1716. {
  1717. Variant oldValue = mat.shaderParameters["MatEmissiveColor"];
  1718. Variant newValue;
  1719. String valueString;
  1720. valueString += String(c.r).Substring(0,5);
  1721. valueString += " ";
  1722. valueString += String(c.g).Substring(0,5);
  1723. valueString += " ";
  1724. valueString += String(c.b).Substring(0,5);
  1725. valueString += " ";
  1726. valueString += String(c.a).Substring(0,5);
  1727. newValue.FromString(oldValue.type, valueString);
  1728. mat.shaderParameters["MatEmissiveColor"] = newValue;
  1729. }
  1730. else if (coloringPropertyName == "menuEnvironmentMapColor")
  1731. {
  1732. Variant oldValue = mat.shaderParameters["MatEnvMapColor"];
  1733. Variant newValue;
  1734. String valueString;
  1735. valueString += String(c.r).Substring(0,5);
  1736. valueString += " ";
  1737. valueString += String(c.g).Substring(0,5);
  1738. valueString += " ";
  1739. valueString += String(c.b).Substring(0,5);
  1740. valueString += " ";
  1741. valueString += String(c.a).Substring(0,5);
  1742. newValue.FromString(oldValue.type, valueString);
  1743. mat.shaderParameters["MatEnvMapColor"] = newValue;
  1744. }
  1745. }
  1746. }
  1747. }
  1748. else if (coloringComponent.typeName == "Zone")
  1749. {
  1750. Zone@ zone = cast<Zone>(coloringComponent);
  1751. if (zone !is null)
  1752. {
  1753. if (coloringPropertyName == "menuAmbientColor")
  1754. {
  1755. zone.ambientColor = c;
  1756. }
  1757. else if (coloringPropertyName == "menuFogColor")
  1758. {
  1759. zone.fogColor = c;
  1760. }
  1761. attributesDirty = true;
  1762. }
  1763. }
  1764. }
  1765. timeToNextColoringGroupUpdate = time.systemTime + stepColoringGroupUpdate;
  1766. }
  1767. // Return old colors, wheel was closed or color discarded
  1768. void HandleWheelDiscardColor(StringHash eventType, VariantMap& eventData)
  1769. {
  1770. if (coloringComponent !is null)
  1771. {
  1772. //Color oldColor = eventData["Color"].GetColor(); //Old color from ColorWheel from ShowColorWheelWithColor(old)
  1773. Color oldColor = coloringOldColor;
  1774. // preview new color
  1775. if (coloringComponent.typeName == "Light")
  1776. {
  1777. Light@ light = cast<Light>(coloringComponent);
  1778. if (light !is null)
  1779. {
  1780. if (coloringPropertyName == "menuLightColor")
  1781. {
  1782. light.color = oldColor;
  1783. }
  1784. else if (coloringPropertyName == "menuSpecularIntensity")
  1785. {
  1786. light.specularIntensity = coloringOldScalar * 10.0f;
  1787. }
  1788. else if (coloringPropertyName == "menuBrightnessMultiplier")
  1789. {
  1790. light.brightness = coloringOldScalar * 10.0f;
  1791. }
  1792. attributesDirty = true;
  1793. }
  1794. }
  1795. else if (coloringComponent.typeName == "StaticModel")
  1796. {
  1797. StaticModel@ model = cast<StaticModel>(coloringComponent);
  1798. if (model !is null)
  1799. {
  1800. Material@ mat = model.materials[0];
  1801. if (mat !is null)
  1802. {
  1803. if (coloringPropertyName == "menuDiffuseColor")
  1804. {
  1805. Variant oldValue = mat.shaderParameters["MatDiffColor"];
  1806. Variant newValue;
  1807. String valueString;
  1808. valueString += String(oldColor.r).Substring(0,5);
  1809. valueString += " ";
  1810. valueString += String(oldColor.g).Substring(0,5);
  1811. valueString += " ";
  1812. valueString += String(oldColor.b).Substring(0,5);
  1813. valueString += " ";
  1814. valueString += String(oldColor.a).Substring(0,5);
  1815. newValue.FromString(oldValue.type, valueString);
  1816. mat.shaderParameters["MatDiffColor"] = newValue;
  1817. }
  1818. else if (coloringPropertyName == "menuSpecularColor")
  1819. {
  1820. Variant oldValue = mat.shaderParameters["MatSpecColor"];
  1821. Variant newValue;
  1822. String valueString;
  1823. valueString += String(oldColor.r).Substring(0,5);
  1824. valueString += " ";
  1825. valueString += String(oldColor.g).Substring(0,5);
  1826. valueString += " ";
  1827. valueString += String(oldColor.b).Substring(0,5);
  1828. valueString += " ";
  1829. valueString += String(coloringOldScalar).Substring(0,5);
  1830. newValue.FromString(oldValue.type, valueString);
  1831. mat.shaderParameters["MatSpecColor"] = newValue;
  1832. }
  1833. else if (coloringPropertyName == "menuEmissiveColor")
  1834. {
  1835. Variant oldValue = mat.shaderParameters["MatEmissiveColor"];
  1836. Variant newValue;
  1837. String valueString;
  1838. valueString += String(oldColor.r).Substring(0,5);
  1839. valueString += " ";
  1840. valueString += String(oldColor.g).Substring(0,5);
  1841. valueString += " ";
  1842. valueString += String(oldColor.b).Substring(0,5);
  1843. valueString += " ";
  1844. valueString += String(oldColor.a).Substring(0,5);
  1845. newValue.FromString(oldValue.type, valueString);
  1846. mat.shaderParameters["MatEmissiveColor"] = newValue;
  1847. }
  1848. else if (coloringPropertyName == "menuEnvironmentMapColor")
  1849. {
  1850. Variant oldValue = mat.shaderParameters["MatEnvMapColor"];
  1851. Variant newValue;
  1852. String valueString;
  1853. valueString += String(oldColor.r).Substring(0,5);
  1854. valueString += " ";
  1855. valueString += String(oldColor.g).Substring(0,5);
  1856. valueString += " ";
  1857. valueString += String(oldColor.b).Substring(0,5);
  1858. valueString += " ";
  1859. valueString += String(oldColor.a).Substring(0,5);
  1860. newValue.FromString(oldValue.type, valueString);
  1861. mat.shaderParameters["MatEnvMapColor"] = newValue;
  1862. }
  1863. }
  1864. }
  1865. }
  1866. else if (coloringComponent.typeName == "Zone")
  1867. {
  1868. Zone@ zone = cast<Zone>(coloringComponent);
  1869. if (zone !is null)
  1870. {
  1871. if (coloringPropertyName == "menuAmbientColor")
  1872. {
  1873. zone.ambientColor = oldColor;
  1874. }
  1875. else if (coloringPropertyName == "menuFogColor")
  1876. {
  1877. zone.fogColor = oldColor;
  1878. }
  1879. attributesDirty = true;
  1880. }
  1881. }
  1882. }
  1883. }
  1884. // Applying color wheel changes to material
  1885. void HandleWheelSelectColor(StringHash eventType, VariantMap& eventData)
  1886. {
  1887. if (coloringComponent !is null)
  1888. if (coloringComponent.typeName == "StaticModel")
  1889. {
  1890. Color c = eventData["Color"].GetColor(); //Selected color from ColorWheel
  1891. StaticModel@ model = cast<StaticModel>(coloringComponent);
  1892. if (model !is null)
  1893. {
  1894. Material@ mat = model.materials[0];
  1895. if (mat !is null)
  1896. {
  1897. editMaterial = mat;
  1898. SaveMaterial();
  1899. }
  1900. }
  1901. }
  1902. }
  1903. bool ViewDebugIcons()
  1904. {
  1905. debugIconsShow = !debugIconsShow;
  1906. return true;
  1907. }