EditorUI.as 77 KB

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