EditorUI.as 78 KB

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