| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268 |
- // Urho3D editor user interface
- XMLFile@ uiStyle;
- XMLFile@ iconStyle;
- UIElement@ uiMenuBar;
- UIElement@ quickMenu;
- Menu@ recentSceneMenu;
- Window@ mruScenesPopup;
- Array<QuickMenuItem@> quickMenuItems;
- FileSelector@ uiFileSelector;
- String consoleCommandInterpreter;
- Window@ contextMenu;
- uint stepColoringGroupUpdate = 100; // ms
- uint timeToNextColoringGroupUpdate = 0;
- const StringHash UI_ELEMENT_TYPE("UIElement");
- const StringHash WINDOW_TYPE("Window");
- const StringHash MENU_TYPE("Menu");
- const StringHash TEXT_TYPE("Text");
- const StringHash CURSOR_TYPE("Cursor");
- const String AUTO_STYLE(""); // Empty string means auto style, i.e. applying style according to UI-element's type automatically
- const String TEMP_SCENE_NAME("_tempscene_.xml");
- const String TEMP_BINARY_SCENE_NAME("_tempscene_.bin");
- const StringHash CALLBACK_VAR("Callback");
- const StringHash INDENT_MODIFIED_BY_ICON_VAR("IconIndented");
- const StringHash VAR_CONTEXT_MENU_HANDLER("ContextMenuHandler");
- const int SHOW_POPUP_INDICATOR = -1;
- const uint MAX_QUICK_MENU_ITEMS = 10;
- const uint maxRecentSceneCount = 5;
- Array<String> uiSceneFilters = {"*.xml", "*.json", "*.bin", "*.*"};
- Array<String> uiElementFilters = {"*.xml"};
- Array<String> uiAllFilters = {"*.*"};
- Array<String> uiScriptFilters = {"*.as", "*.*"};
- Array<String> uiParticleFilters = {"*.xml"};
- Array<String> uiRenderPathFilters = {"*.xml"};
- Array<String> uiExportPathFilters = {"*.obj"};
- uint uiSceneFilter = 0;
- uint uiElementFilter = 0;
- uint uiNodeFilter = 0;
- uint uiImportFilter = 0;
- uint uiScriptFilter = 0;
- uint uiParticleFilter = 0;
- uint uiRenderPathFilter = 0;
- uint uiExportFilter = 0;
- String uiScenePath = fileSystem.programDir + "Data/Scenes";
- String uiElementPath = fileSystem.programDir + "Data/UI";
- String uiNodePath = fileSystem.programDir + "Data/Objects";
- String uiImportPath;
- String uiExportPath;
- String uiScriptPath = fileSystem.programDir + "Data/Scripts";
- String uiParticlePath = fileSystem.programDir + "Data/Particles";
- String uiRenderPathPath = fileSystem.programDir + "CoreData/RenderPaths";
- Array<String> uiRecentScenes;
- String screenshotDir = fileSystem.programDir + "Screenshots";
- bool uiFaded = false;
- float uiMinOpacity = 0.3;
- float uiMaxOpacity = 0.7;
- bool uiHidden = false;
- TerrainEditor terrainEditor;
- void CreateUI()
- {
- // Remove all existing UI content in case we are reloading the editor script
- /// \todo The console will not be properly recreated as it has already been created once
- ui.root.RemoveAllChildren();
- uiStyle = GetEditorUIXMLFile("UI/DefaultStyle.xml");
- ui.root.defaultStyle = uiStyle;
- iconStyle = GetEditorUIXMLFile("UI/EditorIcons.xml");
-
- graphics.windowIcon = cache.GetResource("Image", "Textures/UrhoIcon.png");
- CreateCursor();
- CreateMenuBar();
- CreateToolBar();
- CreateSecondaryToolBar();
- CreateQuickMenu();
- CreateContextMenu();
- CreateHierarchyWindow();
- CreateAttributeInspectorWindow();
- CreateEditorSettingsDialog();
- CreateEditorPreferencesDialog();
- CreateMaterialEditor();
- CreateParticleEffectEditor();
- CreateSpawnEditor();
- CreateSoundTypeEditor();
- CreateStatsBar();
- CreateConsole();
- CreateDebugHud();
- CreateResourceBrowser();
- CreateCamera();
- CreateLayerEditor();
- CreateColorWheel();
- CreateDuplicatorEditor();
- terrainEditor.Create();
- SubscribeToEvent("ScreenMode", "ResizeUI");
- SubscribeToEvent("MenuSelected", "HandleMenuSelected");
- SubscribeToEvent("ChangeLanguage", "HandleChangeLanguage");
- SubscribeToEvent("WheelChangeColor", "HandleWheelChangeColor");
- SubscribeToEvent("WheelSelectColor", "HandleWheelSelectColor");
- SubscribeToEvent("WheelDiscardColor", "HandleWheelDiscardColor");
- }
- void ResizeUI()
- {
- // Resize menu bar
- uiMenuBar.SetFixedWidth(graphics.width);
- // Resize tool bar
- toolBar.SetFixedWidth(graphics.width);
- // Resize secondary tool bar
- secondaryToolBar.SetFixedHeight(graphics.height);
- // Relayout windows
- Array<UIElement@> children = ui.root.GetChildren();
- for (uint i = 0; i < children.length; ++i)
- {
- if (children[i].type == WINDOW_TYPE)
- AdjustPosition(children[i]);
- }
- // Relayout root UI element
- editorUIElement.SetSize(graphics.width, graphics.height);
- // Set new viewport area and reset the viewport layout
- viewportArea = IntRect(0, 0, graphics.width, graphics.height);
- SetViewportMode(viewportMode);
- }
- void AdjustPosition(Window@ window)
- {
- IntVector2 position = window.position;
- IntVector2 size = window.size;
- IntVector2 extend = position + size;
- if (extend.x > graphics.width)
- position.x = Max(10, graphics.width - size.x - 10);
- if (extend.y > graphics.height)
- position.y = Max(100, graphics.height - size.y - 10);
- window.position = position;
- }
- void CreateCursor()
- {
- Cursor@ cursor = Cursor("Cursor");
- cursor.SetStyleAuto(uiStyle);
- cursor.SetPosition(graphics.width / 2, graphics.height / 2);
- ui.cursor = cursor;
- if (GetPlatform() == "Android" || GetPlatform() == "iOS")
- ui.cursor.visible = false;
- }
- // AngelScript does not support closures (yet), but funcdef should do just fine as a workaround for a few cases here for now
- funcdef bool MENU_CALLBACK();
- Array<MENU_CALLBACK@> menuCallbacks;
- MENU_CALLBACK@ messageBoxCallback;
- void HandleQuickSearchChange(StringHash eventType, VariantMap& eventData)
- {
- LineEdit@ search = eventData["Element"].GetPtr();
- if (search is null)
- return;
- PerformQuickMenuSearch(search.text.ToLower().Trimmed());
- }
- void HandleQuickSearchFinish(StringHash eventType, VariantMap& eventData)
- {
- Menu@ menu = quickMenu.GetChild("ResultsMenu", true);
- if (menu is null)
- return;
- String query = eventData["Text"].GetString();
- if (query.length <= 0)
- return;
- Array<QuickMenuItem@> filtered;
- {
- QuickMenuItem@ qi;
- for (uint x=0; x < quickMenuItems.length; x++)
- {
- @qi = quickMenuItems[x];
- int find = qi.action.Find(query, 0, false);
- if (find > -1)
- {
- qi.sortScore = find;
- filtered.Push(qi);
- }
- }
- }
- filtered.Sort();
- if (!filtered.empty)
- {
- VariantMap data;
- Menu@ item = CreateMenuItem(filtered[0].action, filtered[0].callback);
- data["Element"] = item;
- item.SendEvent("MenuSelected", data);
- }
- }
- void PerformQuickMenuSearch(const String&in query)
- {
- Menu@ menu = quickMenu.GetChild("ResultsMenu", true);
- if (menu is null)
- return;
- menu.RemoveAllChildren();
- uint limit = 0;
- if (query.length > 0)
- {
- int lastIndex = 0;
- uint score = 0;
- int index = 0;
- Array<QuickMenuItem@> filtered;
- {
- QuickMenuItem@ qi;
- for (uint x=0; x < quickMenuItems.length; x++)
- {
- @qi = quickMenuItems[x];
- int find = qi.action.Find(query, 0, false);
- if (find > -1)
- {
- qi.sortScore = find;
- filtered.Push(qi);
- }
- }
- }
- filtered.Sort();
- {
- QuickMenuItem@ qi;
- limit = filtered.length > MAX_QUICK_MENU_ITEMS ? MAX_QUICK_MENU_ITEMS : filtered.length;
- for (uint x=0; x < limit; x++)
- {
- @qi = filtered[x];
- Menu@ item = CreateMenuItem(qi.action, qi.callback);
- item.SetMaxSize(1000,16);
- menu.AddChild(item);
- }
- }
- }
- menu.visible = limit > 0;
- menu.SetFixedHeight(limit * 16);
- quickMenu.BringToFront();
- quickMenu.SetFixedHeight(limit*16 + 62 + (menu.visible ? 6 : 0));
- }
- class QuickMenuItem
- {
- String action;
- MENU_CALLBACK@ callback;
- uint sortScore = 0;
- QuickMenuItem(){}
- QuickMenuItem(String action, MENU_CALLBACK@ callback)
- {
- this.action = action;
- this.callback = callback;
- }
- int opCmp(QuickMenuItem@ b)
- {
- return sortScore - b.sortScore;
- }
- }
- /// Create popup search menu.
- void CreateQuickMenu()
- {
- if (quickMenu !is null)
- return;
- quickMenu = LoadEditorUI("UI/EditorQuickMenu.xml");
- quickMenu.enabled = false;
- quickMenu.visible = false;
- quickMenu.opacity = uiMaxOpacity;
- // Handle a dummy search in the quick menu to finalize its initial size to empty
- PerformQuickMenuSearch("");
- ui.root.AddChild(quickMenu);
- LineEdit@ search = quickMenu.GetChild("Search", true);
- SubscribeToEvent(search, "TextChanged", "HandleQuickSearchChange");
- SubscribeToEvent(search, "TextFinished", "HandleQuickSearchFinish");
- UIElement@ closeButton = quickMenu.GetChild("CloseButton", true);
- SubscribeToEvent(closeButton, "Pressed", "ToggleQuickMenu");
- }
- void ToggleQuickMenu()
- {
- quickMenu.enabled = !quickMenu.enabled && ui.cursor.visible;
- quickMenu.visible = quickMenu.enabled;
- if (quickMenu.enabled)
- {
- quickMenu.position = ui.cursorPosition - IntVector2(20,70);
- LineEdit@ search = quickMenu.GetChild("Search", true);
- search.text = "";
- search.focus = true;
- }
- }
- /// Create top menu bar.
- void CreateMenuBar()
- {
- uiMenuBar = BorderImage("MenuBar");
- ui.root.AddChild(uiMenuBar);
- uiMenuBar.enabled = true;
- uiMenuBar.style = "EditorMenuBar";
- uiMenuBar.SetLayout(LM_HORIZONTAL);
- uiMenuBar.opacity = uiMaxOpacity;
- uiMenuBar.SetFixedWidth(graphics.width);
- {
- Menu@ menu = CreateMenu("File");
- Window@ popup = menu.popup;
- popup.AddChild(CreateMenuItem("New scene", @ResetScene, KEY_N, QUAL_SHIFT | QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Open scene...", @PickFile, KEY_O, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Save scene", @SaveSceneWithExistingName, KEY_S, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Save scene as...", @PickFile, KEY_S, QUAL_SHIFT | QUAL_CTRL));
- recentSceneMenu = CreateMenuItem("Open recent scene", null, SHOW_POPUP_INDICATOR);
- popup.AddChild(recentSceneMenu);
- mruScenesPopup = CreatePopup(recentSceneMenu);
- PopulateMruScenes();
- CreateChildDivider(popup);
- Menu@ childMenu = CreateMenuItem("menu Load node", null, SHOW_POPUP_INDICATOR);
- Window@ childPopup = CreatePopup(childMenu);
- childPopup.AddChild(CreateMenuItem("As replicated...", @PickFile, 0, 0, true, "Load node as replicated..."));
- childPopup.AddChild(CreateMenuItem("As local...", @PickFile, 0, 0, true, "Load node as local..."));
- popup.AddChild(childMenu);
- popup.AddChild(CreateMenuItem("Save node as...", @PickFile));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Import model...", @PickFile));
- popup.AddChild(CreateMenuItem("Import scene...", @PickFile));
- popup.AddChild(CreateMenuItem("Import animation...", @PickFile));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Export scene to OBJ...", @PickFile));
- popup.AddChild(CreateMenuItem("Export selected to OBJ...", @PickFile));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Run script...", @PickFile));
- popup.AddChild(CreateMenuItem("Set resource path...", @PickFile));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Exit", @Exit));
- FinalizedPopupMenu(popup);
- uiMenuBar.AddChild(menu);
- }
- {
- Menu@ menu = CreateMenu("Edit");
- Window@ popup = menu.popup;
- popup.AddChild(CreateMenuItem("Undo", @Undo, KEY_Z, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Redo", @Redo, KEY_Y, QUAL_CTRL));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Cut", @Cut, KEY_X, QUAL_CTRL));
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- popup.AddChild(CreateMenuItem("Duplicate", @Duplicate, KEY_D, QUAL_CTRL));
- else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
- popup.AddChild(CreateMenuItem("Duplicate", @Duplicate, KEY_D, QUAL_SHIFT));
- popup.AddChild(CreateMenuItem("Copy", @Copy, KEY_C, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Paste", @Paste, KEY_V, QUAL_CTRL));
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- popup.AddChild(CreateMenuItem("Delete", @Delete, KEY_DELETE, QUAL_ANY));
- else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
- popup.AddChild(CreateMenuItem("Delete", @BlenderModeDelete, KEY_X, QUAL_ANY));
- popup.AddChild(CreateMenuItem("Select all", @SelectAll, KEY_A, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Deselect all", @DeselectAll, KEY_A, QUAL_SHIFT | QUAL_CTRL));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Reset to default", @ResetToDefault));
- CreateChildDivider(popup);
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- {
- popup.AddChild(CreateMenuItem("Reset position", @SceneResetPosition, '1' , QUAL_ALT));
- popup.AddChild(CreateMenuItem("Reset rotation", @SceneResetRotation, '2' , QUAL_ALT));
- popup.AddChild(CreateMenuItem("Reset scale", @SceneResetScale, '3' , QUAL_ALT));
- popup.AddChild(CreateMenuItem("Reset transform", @SceneResetTransform, KEY_Q , QUAL_ALT));
- }
- else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
- {
- popup.AddChild(CreateMenuItem("Reset position", @SceneResetPosition, KEY_G , QUAL_ALT));
- popup.AddChild(CreateMenuItem("Reset rotation", @SceneResetRotation, KEY_R, QUAL_ALT));
- popup.AddChild(CreateMenuItem("Reset scale", @SceneResetScale, KEY_S, QUAL_ALT));
- popup.AddChild(CreateMenuItem("Reset transform", @SceneResetTransform, KEY_Q , QUAL_ALT));
- }
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- {
- popup.AddChild(CreateMenuItem("Enable/disable", @SceneToggleEnable, KEY_E, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Enable all", @SceneEnableAllNodes, KEY_E, QUAL_ALT));
- }
- else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
- {
- popup.AddChild(CreateMenuItem("Enable/disable", @SceneToggleEnable, KEY_H));
- popup.AddChild(CreateMenuItem("Enable all", @SceneEnableAllNodes, KEY_H, QUAL_ALT));
- }
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- popup.AddChild(CreateMenuItem("Unparent", @SceneUnparent, KEY_U, QUAL_CTRL));
- else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
- popup.AddChild(CreateMenuItem("Unparent", @SceneUnparent, KEY_P, QUAL_ALT));
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- popup.AddChild(CreateMenuItem("Parent to last", @NodesParentToLastSelected, KEY_U));
- else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
- popup.AddChild(CreateMenuItem("Parent to last", @NodesParentToLastSelected, KEY_P, QUAL_CTRL));
- CreateChildDivider(popup);
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- popup.AddChild(CreateMenuItem("Toggle update", @ToggleSceneUpdate, KEY_P, QUAL_CTRL));
- //else if (hotKeyMode == HOT_KEYS_MODE_BLENDER)
- // popup.AddChild(CreateMenuItem("Toggle update", @ToggleSceneUpdate, KEY_P, QUAL_CTRL));
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- {
- popup.AddChild(CreateMenuItem("View closer", @ViewCloser, KEY_F));
- }
- else if (hotKeyMode == HOTKEYS_MODE_BLENDER)
- {
- popup.AddChild(CreateMenuItem("Move to layer", @ShowLayerMover, KEY_M));
- popup.AddChild(CreateMenuItem("Smart Duplicate", @SceneSmartDuplicateNode, KEY_D, QUAL_ALT));
- popup.AddChild(CreateMenuItem("View closer", @ViewCloser, KEY_KP_PERIOD));
- }
- popup.AddChild(CreateMenuItem("Color wheel", @ColorWheelBuildMenuSelectTypeColor, KEY_W, QUAL_ALT));
- popup.AddChild(CreateMenuItem("Show components icons", @ViewDebugIcons, KEY_I, QUAL_ALT));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Stop test animation", @StopTestAnimation));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Rebuild navigation data", @SceneRebuildNavigation));
- popup.AddChild(CreateMenuItem("Render Zone Cubemap", @SceneRenderZoneCubemaps));
- popup.AddChild(CreateMenuItem("Add children to SM-group", @SceneAddChildrenStaticModelGroup));
- Menu@ childMenu = CreateMenuItem("Set children as spline path", null, SHOW_POPUP_INDICATOR);
- Window@ childPopup = CreatePopup(childMenu);
- childPopup.AddChild(CreateMenuItem("Non-cyclic", @SetSplinePath, 0, 0, true, "Set non-cyclic spline path"));
- childPopup.AddChild(CreateMenuItem("Cyclic", @SetSplinePath, 0, 0, true, "Set cyclic spline path"));
- popup.AddChild(childMenu);
- FinalizedPopupMenu(popup);
- uiMenuBar.AddChild(menu);
- }
- {
- Menu@ menu = CreateMenu("Create");
- Window@ popup = menu.popup;
- popup.AddChild(CreateMenuItem("Replicated node", @PickNode, 0, 0, true, "Create Replicated node"));
- popup.AddChild(CreateMenuItem("Local node", @PickNode, 0, 0, true, "Create Local node"));
- CreateChildDivider(popup);
- Menu@ childMenu = CreateMenuItem("Component", null, SHOW_POPUP_INDICATOR);
- Window@ childPopup = CreatePopup(childMenu);
- String[] objectCategories = GetObjectCategories();
- for (uint i = 0; i < objectCategories.length; ++i)
- {
- // Skip the UI category for the component menus
- if (objectCategories[i] == "UI")
- continue;
- Menu@ m = CreateMenuItem(objectCategories[i], null, SHOW_POPUP_INDICATOR);
- Window@ p = CreatePopup(m);
- String[] componentTypes = GetObjectsByCategory(objectCategories[i]);
- for (uint j = 0; j < componentTypes.length; ++j)
- p.AddChild(CreateIconizedMenuItem(componentTypes[j], @PickComponent, 0, 0, "", true, "Create " + componentTypes[j]));
- childPopup.AddChild(m);
- }
- FinalizedPopupMenu(childPopup);
- popup.AddChild(childMenu);
- childMenu = CreateMenuItem("Builtin object", null, SHOW_POPUP_INDICATOR);
- childPopup = CreatePopup(childMenu);
- String[] objects = { "Box", "Cone", "Cylinder", "Plane", "Pyramid", "Sphere", "TeaPot", "Torus" };
- for (uint i = 0; i < objects.length; ++i)
- childPopup.AddChild(CreateIconizedMenuItem(objects[i], @PickBuiltinObject, 0, 0, "Node", true, "Create " + objects[i]));
- popup.AddChild(childMenu);
- CreateChildDivider(popup);
- childMenu = CreateMenuItem("UI-element", null, SHOW_POPUP_INDICATOR);
- childPopup = CreatePopup(childMenu);
- String[] uiElementTypes = GetObjectsByCategory("UI");
- for (uint i = 0; i < uiElementTypes.length; ++i)
- {
- if (uiElementTypes[i] != "UIElement")
- childPopup.AddChild(CreateIconizedMenuItem(uiElementTypes[i], @PickUIElement, 0, 0, "", true, "Create " + uiElementTypes[i]));
- }
- CreateChildDivider(childPopup);
- childPopup.AddChild(CreateIconizedMenuItem("UIElement", @PickUIElement));
- popup.AddChild(childMenu);
- FinalizedPopupMenu(popup);
- uiMenuBar.AddChild(menu);
- }
- {
- Menu@ menu = CreateMenu("UI-layout");
- Window@ popup = menu.popup;
- popup.AddChild(CreateMenuItem("Open UI-layout...", @PickFile, KEY_O, QUAL_ALT));
- popup.AddChild(CreateMenuItem("Save UI-layout", @SaveUILayoutWithExistingName, KEY_S, QUAL_ALT));
- popup.AddChild(CreateMenuItem("Save UI-layout as...", @PickFile));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Close UI-layout", @CloseUILayout, KEY_C, QUAL_ALT));
- popup.AddChild(CreateMenuItem("Close all UI-layouts", @CloseAllUILayouts));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Load child element...", @PickFile));
- popup.AddChild(CreateMenuItem("Save child element as...", @PickFile));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Set default style...", @PickFile));
- FinalizedPopupMenu(popup);
- uiMenuBar.AddChild(menu);
- }
- {
- Menu@ menu = CreateMenu("View");
- Window@ popup = menu.popup;
- popup.AddChild(CreateMenuItem("Hierarchy", @ToggleHierarchyWindow, KEY_H, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Attribute inspector", @ToggleAttributeInspectorWindow, KEY_I, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Resource browser", @ToggleResourceBrowserWindow, KEY_B, QUAL_CTRL));
- popup.AddChild(CreateMenuItem("Material editor", @ToggleMaterialEditor));
- popup.AddChild(CreateMenuItem("Particle editor", @ToggleParticleEffectEditor));
- popup.AddChild(CreateMenuItem("Terrain editor", TerrainEditorShowCallback(terrainEditor.Show)));
- popup.AddChild(CreateMenuItem("Spawn editor", @ToggleSpawnEditor));
- popup.AddChild(CreateMenuItem("Duplicator editor", @ToggleDuplicatorEditor));
- popup.AddChild(CreateMenuItem("Sound Type editor", @ToggleSoundTypeEditor));
- popup.AddChild(CreateMenuItem("Editor settings", @ToggleEditorSettingsDialog));
- popup.AddChild(CreateMenuItem("Editor preferences", @ToggleEditorPreferencesDialog));
- CreateChildDivider(popup);
- popup.AddChild(CreateMenuItem("Hide editor", @ToggleUI, KEY_F12, QUAL_ANY));
- FinalizedPopupMenu(popup);
- uiMenuBar.AddChild(menu);
- }
- BorderImage@ spacer = BorderImage("MenuBarSpacer");
- uiMenuBar.AddChild(spacer);
- spacer.style = "EditorMenuBar";
- }
- bool Exit()
- {
- ui.cursor.shape = CS_BUSY;
- if (messageBoxCallback is null)
- {
- String message;
- if (sceneModified)
- message = "Scene has been modified.\n";
- bool uiLayoutModified = false;
- for (uint i = 0; i < editorUIElement.numChildren; ++i)
- {
- UIElement@ element = editorUIElement.children[i];
- if (element !is null && element.vars[MODIFIED_VAR].GetBool())
- {
- uiLayoutModified = true;
- message += "UI layout has been modified.\n";
- break;
- }
- }
- if (sceneModified || uiLayoutModified)
- {
- MessageBox@ messageBox = MessageBox(message + "Continue to exit?", "Warning");
- if (messageBox.window !is null)
- {
- Button@ cancelButton = messageBox.window.GetChild("CancelButton", true);
- cancelButton.visible = true;
- cancelButton.focus = true;
- SubscribeToEvent(messageBox, "MessageACK", "HandleMessageAcknowledgement");
- messageBoxCallback = @Exit;
- return false;
- }
- }
- }
- else
- messageBoxCallback = null;
- engine.Exit();
- return true;
- }
- void HandleExitRequested()
- {
- if (!ui.HasModalElement())
- Exit();
- }
- bool PickFile()
- {
- Menu@ menu = GetEventSender();
- if (menu is null)
- return false;
- String action = menu.name;
- if (action.empty)
- return false;
- // File (Scene related)
- if (action == "Open scene...")
- {
- CreateFileSelector("Open scene", "Open", "Cancel", uiScenePath, uiSceneFilters, uiSceneFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleOpenSceneFile");
- }
- else if (action == "Save scene as..." || action == "Save scene")
- {
- CreateFileSelector("Save scene as", "Save", "Cancel", uiScenePath, uiSceneFilters, uiSceneFilter);
- uiFileSelector.fileName = GetFileNameAndExtension(editorScene.fileName);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveSceneFile");
- }
- else if (action == "As replicated..." || action == "Load node as replicated...")
- {
- instantiateMode = REPLICATED;
- CreateFileSelector("fileSelector Load node", "Load", "Cancel", uiNodePath, uiSceneFilters, uiNodeFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleLoadNodeFile");
- }
- else if (action == "As local..." || action == "Load node as local...")
- {
- instantiateMode = LOCAL;
- CreateFileSelector("Load node", "Load", "Cancel", uiNodePath, uiSceneFilters, uiNodeFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleLoadNodeFile");
- }
- else if (action == "Save node as...")
- {
- if (editNode !is null && editNode !is editorScene)
- {
- CreateFileSelector("Save node", "Save", "Cancel", uiNodePath, uiSceneFilters, uiNodeFilter);
- uiFileSelector.fileName = GetFileNameAndExtension(instantiateFileName);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveNodeFile");
- }
- }
- else if (action == "Import model...")
- {
- CreateFileSelector("Import model", "Import", "Cancel", uiImportPath, uiAllFilters, uiImportFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleImportModel");
- }
- else if (action == "Import animation...")
- {
- CreateFileSelector("Import animation", "Import", "Cancel", uiImportPath, uiAllFilters, uiImportFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleImportAnimation");
- }
- else if (action == "Import scene...")
- {
- CreateFileSelector("Import scene", "Import", "Cancel", uiImportPath, uiAllFilters, uiImportFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleImportScene");
- }
- else if (action == "Export scene to OBJ..." || action == "Export selected to OBJ...")
- {
- // Set these up together to share the "export settings" options
- if (action == "Export scene to OBJ...")
- {
- CreateFileSelector("Export scene to OBJ", "Save", "Cancel", uiExportPath, uiExportPathFilters, uiExportFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleExportSceneOBJ");
- }
- else if (action == "Export selected to OBJ...")
- {
- CreateFileSelector("Export selected to OBJ", "Save", "Cancel", uiExportPath, uiExportPathFilters, uiExportFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleExportSelectedOBJ");
- }
- Window@ window = uiFileSelector.window;
- UIElement@ optionsGroup = UIElement();
- optionsGroup.maxHeight = 30;
- optionsGroup.layoutMode = LM_HORIZONTAL;
- window.defaultStyle = uiStyle;
- window.style = AUTO_STYLE;
- CheckBox@ checkRightHanded = CheckBox();
- checkRightHanded.checked = objExportRightHanded_;
- checkRightHanded.defaultStyle = uiStyle;
- checkRightHanded.style = AUTO_STYLE;
- SubscribeToEvent(checkRightHanded, "Toggled", "HandleOBJRightHandedChanged");
- optionsGroup.AddChild(checkRightHanded);
- Text@ lblRightHanded = Text();
- lblRightHanded.defaultStyle = uiStyle;
- lblRightHanded.style = AUTO_STYLE;
- lblRightHanded.text = " Right handed";
- optionsGroup.AddChild(lblRightHanded);
- CheckBox@ checkZUp = CheckBox();
- checkZUp.checked = objExportZUp_;
- checkZUp.defaultStyle = uiStyle;
- checkZUp.style = AUTO_STYLE;
- SubscribeToEvent(checkZUp, "Toggled", "HandleOBJZUpChanged");
- optionsGroup.AddChild(checkZUp);
- Text@ lblZUp = Text();
- lblZUp.defaultStyle = uiStyle;
- lblZUp.style = AUTO_STYLE;
- lblZUp.text = " Z Axis Up";
- optionsGroup.AddChild(lblZUp);
- window.AddChild(optionsGroup);
- }
- else if (action == "Run script...")
- {
- CreateFileSelector("Run script", "Run", "Cancel", uiScriptPath, uiScriptFilters, uiScriptFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleRunScript");
- }
- else if (action == "Set resource path...")
- {
- CreateFileSelector("Set resource path", "Set", "Cancel", sceneResourcePath, uiAllFilters, 0);
- uiFileSelector.directoryMode = true;
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleResourcePath");
- }
- // UI-element
- else if (action == "Open UI-layout...")
- {
- CreateFileSelector("Open UI-layout", "Open", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleOpenUILayoutFile");
- }
- else if (action == "Save UI-layout as..." || action == "Save UI-layout")
- {
- if (editUIElement !is null)
- {
- UIElement@ element = GetTopLevelUIElement(editUIElement);
- if (element is null)
- return false;
- CreateFileSelector("Save UI-layout as", "Save", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
- uiFileSelector.fileName = GetFileNameAndExtension(element.GetVar(FILENAME_VAR).GetString());
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveUILayoutFile");
- }
- }
- else if (action == "Load child element...")
- {
- if (editUIElement !is null)
- {
- CreateFileSelector("Load child element", "Load", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleLoadChildUIElementFile");
- }
- }
- else if (action == "Save child element as...")
- {
- if (editUIElement !is null)
- {
- CreateFileSelector("Save child element", "Save", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
- uiFileSelector.fileName = GetFileNameAndExtension(editUIElement.GetVar(CHILD_ELEMENT_FILENAME_VAR).GetString());
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleSaveChildUIElementFile");
- }
- }
- else if (action == "Set default style...")
- {
- CreateFileSelector("Set default style", "Set", "Cancel", uiElementPath, uiElementFilters, uiElementFilter);
- SubscribeToEvent(uiFileSelector, "FileSelected", "HandleUIElementDefaultStyle");
- }
- return true;
- }
- bool PickNode()
- {
- Menu@ menu = GetEventSender();
- if (menu is null)
- return false;
- String action = GetActionName(menu.name);
- if (action.empty)
- return false;
- CreateNode(action == "Replicated node" ? REPLICATED : LOCAL);
- return true;
- }
- bool PickComponent()
- {
- if (editNodes.empty)
- return false;
- Menu@ menu = GetEventSender();
- if (menu is null)
- return false;
- String action = GetActionName(menu.name);
- if (action.empty)
- return false;
- CreateComponent(action);
- return true;
- }
- bool PickBuiltinObject()
- {
- Menu@ menu = GetEventSender();
- if (menu is null)
- return false;
- String action = GetActionName(menu.name);
- if (action.empty)
- return false;
- CreateBuiltinObject(action);
- return true;
- }
- bool PickUIElement()
- {
- Menu@ menu = GetEventSender();
- if (menu is null)
- return false;
- String action = GetActionName(menu.name);
- if (action.empty)
- return false;
- return NewUIElement(action);
- }
- // When calling items from the quick menu, they have "Create" prepended for clarity. Strip that now to get the object name to create
- String GetActionName(const String&in name)
- {
- if (name.StartsWith("Create"))
- return name.Substring(7);
- else
- return name;
- }
- void HandleMenuSelected(StringHash eventType, VariantMap& eventData)
- {
- Menu@ menu = eventData["Element"].GetPtr();
- if (menu is null)
- return;
- HandlePopup(menu);
- quickMenu.visible = false;
- quickMenu.enabled = false;
- // Execute the callback if available
- Variant variant = menu.GetVar(CALLBACK_VAR);
- if (!variant.empty)
- menuCallbacks[variant.GetUInt()]();
- }
- Menu@ CreateMenuItem(const String&in title, MENU_CALLBACK@ callback = null, int accelKey = 0, int accelQual = 0, bool addToQuickMenu = true, String quickMenuText="", bool autoLocalize = true)
- {
- Menu@ menu = Menu(title);
- menu.defaultStyle = uiStyle;
- menu.style = AUTO_STYLE;
- menu.SetLayout(LM_HORIZONTAL, 0, IntRect(8, 2, 8, 2));
- if (accelKey > 0)
- menu.SetAccelerator(accelKey, accelQual);
- if (callback !is null)
- {
- menu.vars[CALLBACK_VAR] = menuCallbacks.length;
- menuCallbacks.Push(callback);
- }
- Text@ menuText = Text();
- menu.AddChild(menuText);
- menuText.style = "EditorMenuText";
- menuText.text = title;
- menuText.autoLocalizable = autoLocalize;
- if (addToQuickMenu)
- AddQuickMenuItem(callback, quickMenuText.empty ? title : quickMenuText);
- if (accelKey != 0)
- {
- UIElement@ spacer = UIElement();
- spacer.minWidth = menuText.indentSpacing;
- spacer.height = menuText.height;
- menu.AddChild(spacer);
- menu.AddChild(CreateAccelKeyText(accelKey, accelQual));
- }
- return menu;
- }
- void AddQuickMenuItem(MENU_CALLBACK@ callback, String text)
- {
- if (callback is null)
- return;
- bool exists = false;
- for (uint i=0;i<quickMenuItems.length;++i)
- {
- if (quickMenuItems[i].action == text)
- {
- exists = true;
- break;
- }
- }
- if (!exists)
- quickMenuItems.Push(QuickMenuItem(text, callback));
- }
- 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="")
- {
- Menu@ menu = Menu(title);
- menu.defaultStyle = uiStyle;
- menu.style = AUTO_STYLE;
- menu.SetLayout(LM_VERTICAL, 0, IntRect(8, 2, 8, 2));
- if (accelKey > 0)
- menu.SetAccelerator(accelKey, accelQual);
- if (callback !is null)
- {
- menu.vars[CALLBACK_VAR] = menuCallbacks.length;
- menuCallbacks.Push(callback);
- }
- Text@ menuText = Text();
- menu.AddChild(menuText);
- menuText.style = "EditorMenuText";
- menuText.text = title;
- // If icon type is not provided, use the title instead
- IconizeUIElement(menuText, iconType.empty ? title : iconType);
- if (addToQuickMenu)
- AddQuickMenuItem(callback, quickMenuText.empty ? title : quickMenuText);
- if (accelKey != 0)
- {
- menuText.layoutMode = LM_HORIZONTAL;
- menuText.AddChild(CreateAccelKeyText(accelKey, accelQual));
- }
- return menu;
- }
- /// Create a child divider in parent with vertical layout mode. It works on other parent element as well, not just parent menu.
- void CreateChildDivider(UIElement@ parent)
- {
- BorderImage@ divider = parent.CreateChild("BorderImage", "Divider");
- divider.style = "EditorDivider";
- }
- Window@ CreatePopup(Menu@ baseMenu)
- {
- Window@ popup = Window();
- popup.defaultStyle = uiStyle;
- popup.style = AUTO_STYLE;
- popup.SetLayout(LM_VERTICAL, 1, IntRect(2, 6, 2, 6));
- baseMenu.popup = popup;
- baseMenu.popupOffset = IntVector2(0, baseMenu.height);
- return popup;
- }
- Menu@ CreateMenu(const String&in title)
- {
- Menu@ menu = CreateMenuItem(title);
- Text@ text = menu.children[0];
- menu.maxWidth = text.width + 20;
- CreatePopup(menu);
- return menu;
- }
- void HandleChangeLanguage(StringHash eventType, VariantMap& eventData)
- {
- Array<UIElement@> children = uiMenuBar.GetChildren();
- for (uint i = 0; i < children.length - 2; ++i) // last 2 elements is not menu
- {
- // dirty hack: force recalc text size
- children[i].maxWidth = 1000;
- Text@ text = children[i].children[0];
- text.minWidth = 0;
- text.maxWidth = 1;
- text.ApplyAttributes();
- children[i].maxWidth = text.width + 20;
- }
- RebuildResourceDatabase();
- }
- Text@ CreateAccelKeyText(int accelKey, int accelQual)
- {
- Text@ accelKeyText = Text();
- accelKeyText.defaultStyle = uiStyle;
- accelKeyText.style = "EditorMenuText";
- accelKeyText.textAlignment = HA_RIGHT;
- String text;
- if (accelKey == KEY_DELETE)
- text = "Del";
- else if (accelKey == KEY_SPACE)
- text = "Space";
- // Cannot use range as the key constants below do not appear to be in sequence
- else if (accelKey == KEY_F1)
- text = "F1";
- else if (accelKey == KEY_F2)
- text = "F2";
- else if (accelKey == KEY_F3)
- text = "F3";
- else if (accelKey == KEY_F4)
- text = "F4";
- else if (accelKey == KEY_F5)
- text = "F5";
- else if (accelKey == KEY_F6)
- text = "F6";
- else if (accelKey == KEY_F7)
- text = "F7";
- else if (accelKey == KEY_F8)
- text = "F8";
- else if (accelKey == KEY_F9)
- text = "F9";
- else if (accelKey == KEY_F10)
- text = "F10";
- else if (accelKey == KEY_F11)
- text = "F11";
- else if (accelKey == KEY_F12)
- text = "F12";
- else if (accelKey == SHOW_POPUP_INDICATOR)
- text = ">";
- else if (accelKey == KEY_KP_PERIOD)
- text = "NumPad .";
- else
- text.AppendUTF8(accelKey);
- if (accelQual & QUAL_ALT > 0)
- text = "Alt+" + text;
- if (accelQual & QUAL_SHIFT > 0)
- text = "Shift+" + text;
- if (accelQual & QUAL_CTRL > 0)
- text = "Ctrl+" + text;
- accelKeyText.text = text;
- return accelKeyText;
- }
- void FinalizedPopupMenu(Window@ popup)
- {
- // Find the maximum menu text width
- Array<UIElement@> children = popup.GetChildren();
- int maxWidth = 0;
- for (uint i = 0; i < children.length; ++i)
- {
- UIElement@ element = children[i];
- if (element.type != MENU_TYPE) // Skip if not menu item
- continue;
- int width = element.children[0].width;
- if (width > maxWidth)
- maxWidth = width;
- }
- // Adjust the indent spacing to slightly wider than the maximum width
- maxWidth += 20;
- for (uint i = 0; i < children.length; ++i)
- {
- UIElement@ element = children[i];
- if (element.type != MENU_TYPE)
- continue;
- Menu@ menu = element;
- Text@ menuText = menu.children[0];
- if (menuText.numChildren == 1) // Skip if menu text does not have accel
- menuText.children[0].indentSpacing = maxWidth;
- // Adjust the popup offset taking the indentation into effect
- if (menu.popup !is null)
- menu.popupOffset = IntVector2(menu.width, 0);
- }
- }
- void CreateFileSelector(const String&in title, const String&in ok, const String&in cancel, const String&in initialPath, Array<String>@ filters,
- uint initialFilter, bool autoLocalizeTitle = true)
- {
- // Within the editor UI, the file selector is a kind of a "singleton". When the previous one is overwritten, also
- // the events subscribed from it are disconnected, so new ones are safe to subscribe.
- uiFileSelector = FileSelector();
- uiFileSelector.defaultStyle = uiStyle;
- uiFileSelector.title = title;
- uiFileSelector.titleText.autoLocalizable = autoLocalizeTitle;
- uiFileSelector.path = initialPath;
- uiFileSelector.SetButtonTexts(ok, cancel);
- Text@ okText = cast<Text>(uiFileSelector.okButton.children[0]);
- okText.autoLocalizable = true;
- Text@ cancelText = cast<Text>(uiFileSelector.cancelButton.children[0]);
- cancelText.autoLocalizable = true;
- uiFileSelector.SetFilters(filters, initialFilter);
- CenterDialog(uiFileSelector.window);
- }
- void CloseFileSelector(uint&out filterIndex, String&out path)
- {
- // Save filter & path for next time
- filterIndex = uiFileSelector.filterIndex;
- path = uiFileSelector.path;
- uiFileSelector = null;
- }
- void CloseFileSelector()
- {
- uiFileSelector = null;
- }
- void CreateConsole()
- {
- Console@ console = engine.CreateConsole();
- console.defaultStyle = uiStyle;
- console.commandInterpreter = consoleCommandInterpreter;
- console.numBufferedRows = 100;
- console.autoVisibleOnError = true;
- }
- void CreateDebugHud()
- {
- engine.CreateDebugHud();
- debugHud.defaultStyle = uiStyle;
- debugHud.mode = DEBUGHUD_SHOW_NONE;
- }
- void CenterDialog(UIElement@ element)
- {
- IntVector2 size = element.size;
- element.SetPosition((ui.root.width - size.x) / 2, (ui.root.height - size.y) / 2);
- }
- void CreateContextMenu()
- {
- contextMenu = LoadEditorUI("UI/EditorContextMenu.xml");
- ui.root.AddChild(contextMenu);
- }
- void UpdateWindowTitle()
- {
- String sceneName = GetFileNameAndExtension(editorScene.fileName);
- if (sceneName.empty || sceneName == TEMP_SCENE_NAME || sceneName == TEMP_BINARY_SCENE_NAME)
- sceneName = "Untitled";
- if (sceneModified)
- sceneName += "*";
- graphics.windowTitle = "Urho3D editor - " + sceneName;
- }
- void HandlePopup(Menu@ menu)
- {
- // Close the top level menu now unless the selected menu item has another popup
- if (menu.popup !is null)
- return;
- for (;;)
- {
- UIElement@ menuParent = menu.parent;
- if (menuParent is null)
- break;
- Menu@ nextMenu = menuParent.vars["Origin"].GetPtr();
- if (nextMenu is null)
- break;
- else
- menu = nextMenu;
- }
- if (menu.parent is uiMenuBar)
- menu.showPopup = false;
- }
- String ExtractFileName(VariantMap& eventData, bool forSave = false)
- {
- String fileName;
- // Check for OK
- if (eventData["OK"].GetBool())
- {
- String filter = eventData["Filter"].GetString();
- fileName = eventData["FileName"].GetString();
- // Add default extension for saving if not specified
- if (GetExtension(fileName).empty && forSave && filter != "*.*")
- fileName = fileName + filter.Substring(1);
- }
- return fileName;
- }
- void HandleOpenSceneFile(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiSceneFilter, uiScenePath);
- LoadScene(ExtractFileName(eventData));
- SendEvent(EDITOR_EVENT_SCENE_LOADED);
- }
- void HandleSaveSceneFile(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiSceneFilter, uiScenePath);
- SaveScene(ExtractFileName(eventData, true));
- }
- void HandleLoadNodeFile(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiNodeFilter, uiNodePath);
- LoadNode(ExtractFileName(eventData));
- }
- void HandleSaveNodeFile(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiNodeFilter, uiNodePath);
- SaveNode(ExtractFileName(eventData, true));
- }
- void HandleImportModel(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiImportFilter, uiImportPath);
- ImportModel(ExtractFileName(eventData));
- }
- void HandleImportAnimation(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiImportFilter, uiImportPath);
- ImportAnimation(ExtractFileName(eventData));
- }
- void HandleImportScene(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiImportFilter, uiImportPath);
- ImportScene(ExtractFileName(eventData));
- }
- void HandleExportSceneOBJ(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiExportFilter, uiExportPath);
- ExportSceneToOBJ(ExtractFileName(eventData));
- }
- void HandleExportSelectedOBJ(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiExportFilter, uiExportPath);
- ExportSelectedToOBJ(ExtractFileName(eventData));
- }
- void ExecuteScript(const String&in fileName)
- {
- if (fileName.empty)
- return;
- File@ file = File(fileName, FILE_READ);
- if (file.open)
- {
- String scriptCode;
- while (!file.eof)
- scriptCode += file.ReadLine() + "\n";
- file.Close();
- if (script.Execute(scriptCode))
- log.Info("Script " + fileName + " ran successfully");
- }
- }
- void HandleRunScript(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiScriptFilter, uiScriptPath);
- suppressSceneChanges = true;
- ExecuteScript(ExtractFileName(eventData));
- suppressSceneChanges = false;
- UpdateHierarchyItem(editorScene, true);
- UpdateHierarchyItem(editorUIElement, true);
- }
- void HandleResourcePath(StringHash eventType, VariantMap& eventData)
- {
- String pathName = uiFileSelector.path;
- CloseFileSelector();
- if (eventData["OK"].GetBool())
- SetResourcePath(pathName, false);
- }
- void HandleOpenUILayoutFile(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiElementFilter, uiElementPath);
- OpenUILayout(ExtractFileName(eventData));
- }
- void HandleSaveUILayoutFile(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiElementFilter, uiElementPath);
- SaveUILayout(ExtractFileName(eventData, true));
- }
- void HandleLoadChildUIElementFile(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiElementFilter, uiElementPath);
- LoadChildUIElement(ExtractFileName(eventData));
- }
- void HandleSaveChildUIElementFile(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiElementFilter, uiElementPath);
- SaveChildUIElement(ExtractFileName(eventData, true));
- }
- void HandleUIElementDefaultStyle(StringHash eventType, VariantMap& eventData)
- {
- CloseFileSelector(uiElementFilter, uiElementPath);
- SetUIElementDefaultStyle(ExtractFileName(eventData));
- }
- void HandleHotKeysBlender( VariantMap& eventData)
- {
- int key = eventData["Key"].GetInt();
- int viewDirection = eventData["Qualifiers"].GetInt() == QUAL_CTRL ? -1 : 1;
- if (key == KEY_ESCAPE)
- {
- if (uiHidden)
- UnhideUI();
- else if (console.visible)
- console.visible = false;
- else if (contextMenu.visible)
- CloseContextMenu();
- else if (quickMenu.visible)
- {
- quickMenu.visible = false;
- quickMenu.enabled = false;
- }
- else
- {
- UIElement@ front = ui.frontElement;
- if (front is settingsDialog || front is preferencesDialog)
- {
- ui.focusElement = null;
- front.visible = false;
- }
- }
- }
- // Ignore other keys when UI has a modal element
- else if (ui.HasModalElement())
- return;
- else if (key == KEY_F1)
- console.Toggle();
- else if (key == KEY_F2)
- ToggleRenderingDebug();
- else if (key == KEY_F3)
- TogglePhysicsDebug();
- else if (key == KEY_F4)
- ToggleOctreeDebug();
- else if (key == KEY_F5)
- ToggleNavigationDebug();
- else if (key == KEY_F11)
- {
- Image@ screenshot = Image();
- graphics.TakeScreenShot(screenshot);
- if (!fileSystem.DirExists(screenshotDir))
- fileSystem.CreateDir(screenshotDir);
- screenshot.SavePNG(screenshotDir + "/Screenshot_" +
- time.timeStamp.Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
- }
- // In Blender, HOME key is for locating the selected objects by pan and
- // the PERIOD key of keypad is for moving the camera to focus the selected.
- // Here we ignore the difference.
- else if ((key == KEY_HOME || key == KEY_KP_PERIOD) && ui.focusElement is null)
- {
- if (selectedNodes.length > 0 || selectedComponents.length > 0)
- {
- LocateNodesAndComponents(selectedNodes, selectedComponents);
- }
- }
- else if (key == KEY_KP_1 && ui.focusElement is null) // Front view
- {
- cameraSmoothInterpolate.Finish();
- Vector3 pos = -Vector3(0.0, 0.0, cameraNode.position.length * viewDirection);
- Quaternion rot = Quaternion(Vector3::FORWARD, Vector3(0, 0, viewDirection));
- cameraSmoothInterpolate.SetCameraNodePosition(cameraNode.position, pos);
- cameraSmoothInterpolate.SetCameraNodeRotation(cameraNode.rotation, rot);
- cameraSmoothInterpolate.Start(0.5f);
- }
- else if ((key == KEY_KP_3 || key == KEY_KP_9) && ui.focusElement is null) // Side view
- {
- cameraSmoothInterpolate.Finish();
- Vector3 pos = -Vector3(cameraNode.position.length * -viewDirection, 0.0, 0.0);
- Quaternion rot = Quaternion(Vector3::FORWARD, Vector3(-viewDirection, 0, 0));
- cameraSmoothInterpolate.SetCameraNodePosition(cameraNode.position, pos);
- cameraSmoothInterpolate.SetCameraNodeRotation(cameraNode.rotation, rot);
- cameraSmoothInterpolate.Start(0.5f);
- }
- else if (key == KEY_KP_7 && ui.focusElement is null) // Top view
- {
- cameraSmoothInterpolate.Finish();
- Vector3 pos = -Vector3(0.0, cameraNode.position.length * -viewDirection, 0.0);
- Quaternion rot = Quaternion(Vector3::FORWARD, Vector3(0, -viewDirection, 0));
- cameraSmoothInterpolate.SetCameraNodePosition(cameraNode.position, pos);
- cameraSmoothInterpolate.SetCameraNodeRotation(cameraNode.rotation, rot);
- cameraSmoothInterpolate.Start(0.5f);
- }
- else if (key == KEY_KP_5 && ui.focusElement is null)
- {
- activeViewport.ToggleOrthographic();
- }
- else if (key == '4' && ui.focusElement is null)
- editMode = EDIT_SELECT;
- else if (key == '5' && ui.focusElement is null)
- axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
- else if (key == '6' && ui.focusElement is null)
- {
- --pickMode;
- if (pickMode < PICK_GEOMETRIES)
- pickMode = MAX_PICK_MODES - 1;
- }
- else if (key == '7' && ui.focusElement is null)
- {
- ++pickMode;
- if (pickMode >= MAX_PICK_MODES)
- pickMode = PICK_GEOMETRIES;
- }
- else if (key == KEY_Z && eventData["Qualifiers"].GetInt() != QUAL_CTRL)
- {
- if (ui.focusElement is null)
- {
- fillMode = FillMode(fillMode + 1);
- if (fillMode > FILL_POINT)
- fillMode = FILL_SOLID;
- // Update camera fill mode
- SetFillMode(fillMode);
- }
- }
- else if (key == KEY_SPACE)
- {
- if (ui.cursor.visible && ui.focusElement is null)
- ToggleQuickMenu();
- }
- else
- {
- SteppedObjectManipulation(key);
- }
- if ((ui.focusElement is null) && (selectedNodes.length > 0) && !cameraFlyMode)
- {
- if (eventData["Qualifiers"].GetInt() == QUAL_ALT) // reset transformations
- {
- if (key == KEY_G)
- SceneResetPosition();
- else if (key == KEY_R)
- SceneResetRotation();
- else if (key == KEY_S)
- SceneResetScale();
- }
- else if (eventData["Qualifiers"].GetInt() != QUAL_CTRL) // set transformations
- {
- if (key == KEY_G)
- {
- editMode = EDIT_MOVE;
- axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
- }
- else if (key == KEY_R)
- {
- editMode = EDIT_ROTATE;
- axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
- }
- else if (key == KEY_S)
- {
- editMode = EDIT_SCALE;
- axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
- }
- }
- }
- toolBarDirty = true;
- }
- void HandleHotKeysStandard(VariantMap& eventData)
- {
- int key = eventData["Key"].GetInt();
- int viewDirection = eventData["Qualifiers"].GetInt() == QUAL_CTRL ? -1 : 1;
- if (key == KEY_ESCAPE)
- {
- if (uiHidden)
- UnhideUI();
- else if (console.visible)
- console.visible = false;
- else if (contextMenu.visible)
- CloseContextMenu();
- else if (quickMenu.visible)
- {
- quickMenu.visible = false;
- quickMenu.enabled = false;
- }
- else
- {
- UIElement@ front = ui.frontElement;
- if (front is settingsDialog || front is preferencesDialog)
- {
- ui.focusElement = null;
- front.visible = false;
- }
- }
- }
- // Ignore other keys when UI has a modal element
- else if (ui.HasModalElement())
- return;
- else if (key == KEY_F1)
- console.Toggle();
- else if (key == KEY_F2)
- ToggleRenderingDebug();
- else if (key == KEY_F3)
- TogglePhysicsDebug();
- else if (key == KEY_F4)
- ToggleOctreeDebug();
- else if (key == KEY_F5)
- ToggleNavigationDebug();
- else if (key == KEY_F11)
- {
- Image@ screenshot = Image();
- graphics.TakeScreenShot(screenshot);
- if (!fileSystem.DirExists(screenshotDir))
- fileSystem.CreateDir(screenshotDir);
- screenshot.SavePNG(screenshotDir + "/Screenshot_" +
- time.timeStamp.Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
- }
- else if ((key == KEY_HOME || key == KEY_F) && ui.focusElement is null)
- {
- if (selectedNodes.length > 0 || selectedComponents.length > 0)
- {
- LocateNodesAndComponents(selectedNodes, selectedComponents);
- }
- }
- else if (key == KEY_KP_1 && ui.focusElement is null) // Front view
- {
- cameraSmoothInterpolate.Finish();
- Vector3 pos = -Vector3(0.0, 0.0, cameraNode.position.length * viewDirection);
- Quaternion rot = Quaternion(Vector3::FORWARD, Vector3(0, 0, viewDirection));
- cameraSmoothInterpolate.SetCameraNodePosition(cameraNode.position, pos);
- cameraSmoothInterpolate.SetCameraNodeRotation(cameraNode.rotation, rot);
- cameraSmoothInterpolate.Start(0.5f);
- }
- else if ((key == KEY_KP_3 || key == KEY_KP_9) && ui.focusElement is null) // Side view
- {
- cameraSmoothInterpolate.Finish();
- Vector3 pos = -Vector3(cameraNode.position.length * -viewDirection, 0.0, 0.0);
- Quaternion rot = Quaternion(Vector3::FORWARD, Vector3(-viewDirection, 0, 0));
- cameraSmoothInterpolate.SetCameraNodePosition(cameraNode.position, pos);
- cameraSmoothInterpolate.SetCameraNodeRotation(cameraNode.rotation, rot);
- cameraSmoothInterpolate.Start(0.5f);
- }
- else if (key == KEY_KP_7 && ui.focusElement is null) // Top view
- {
- cameraSmoothInterpolate.Finish();
- Vector3 pos = -Vector3(0.0, cameraNode.position.length * -viewDirection, 0.0);
- Quaternion rot = Quaternion(Vector3::FORWARD, Vector3(0, -viewDirection, 0));
- cameraSmoothInterpolate.SetCameraNodePosition(cameraNode.position, pos);
- cameraSmoothInterpolate.SetCameraNodeRotation(cameraNode.rotation, rot);
- cameraSmoothInterpolate.Start(0.5f);
- }
- else if (key == KEY_KP_5 && ui.focusElement is null)
- {
- activeViewport.ToggleOrthographic();
- }
- else if (eventData["Qualifiers"].GetInt() == QUAL_CTRL)
- {
- if (key == '1')
- editMode = EDIT_MOVE;
- else if (key == '2')
- editMode = EDIT_ROTATE;
- else if (key == '3')
- editMode = EDIT_SCALE;
- else if (key == '4')
- editMode = EDIT_SELECT;
- else if (key == '5')
- axisMode = AxisMode(axisMode ^ AXIS_LOCAL);
- else if (key == '6')
- {
- --pickMode;
- if (pickMode < PICK_GEOMETRIES)
- pickMode = MAX_PICK_MODES - 1;
- }
- else if (key == '7')
- {
- ++pickMode;
- if (pickMode >= MAX_PICK_MODES)
- pickMode = PICK_GEOMETRIES;
- }
- else if (key == KEY_W)
- {
- fillMode = FillMode(fillMode + 1);
- if (fillMode > FILL_POINT)
- fillMode = FILL_SOLID;
- // Update camera fill mode
- SetFillMode(fillMode);
- }
- else if (key == KEY_SPACE)
- {
- if (ui.cursor.visible)
- ToggleQuickMenu();
- }
- else
- SteppedObjectManipulation(key);
- toolBarDirty = true;
- }
- }
- void HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- if (hotKeyMode == HOTKEYS_MODE_STANDARD)
- {
- HandleHotKeysStandard(eventData);
- }
- else if( hotKeyMode == HOTKEYS_MODE_BLENDER)
- {
- HandleHotKeysBlender(eventData);
- }
- }
- void UnfadeUI()
- {
- FadeUI(false);
- }
- void FadeUI(bool fade = true)
- {
- if (uiHidden || uiFaded == fade)
- return;
- float opacity = (uiFaded = fade) ? uiMinOpacity : uiMaxOpacity;
- Array<UIElement@> children = ui.root.GetChildren();
- for (uint i = 0; i < children.length; ++i)
- {
- // Texts, popup&modal windows (which are anyway only in ui.modalRoot), and editorUIElement are excluded
- if (children[i].type != TEXT_TYPE && children[i] !is editorUIElement)
- children[i].opacity = opacity;
- }
- }
- bool ToggleUI()
- {
- HideUI(!uiHidden);
- return true;
- }
- void UnhideUI()
- {
- HideUI(false);
- }
- void HideUI(bool hide = true)
- {
- if (uiHidden == hide)
- return;
- // Note: we could set ui.root.visible = false and it would hide the whole hierarchy.
- // However in this case we need the editorUIElement to stay visible
- bool visible = !(uiHidden = hide);
- Array<UIElement@> children = ui.root.GetChildren();
- for (uint i = 0; i < children.length; ++i)
- {
- // Cursor and editorUIElement are excluded
- if (children[i].type != CURSOR_TYPE && children[i] !is editorUIElement)
- {
- if (visible)
- {
- if (!children[i].visible)
- children[i].visible = children[i].vars["HideUI"].GetBool();
- }
- else
- {
- children[i].vars["HideUI"] = children[i].visible;
- children[i].visible = false;
- }
- }
- }
- }
- void IconizeUIElement(UIElement@ element, const String&in iconType)
- {
- // Check if the icon has been created before
- BorderImage@ icon = element.GetChild("Icon");
- // If iconType is empty, it is a request to remove the existing icon
- if (iconType.empty)
- {
- // Remove the icon if it exists
- if (icon !is null)
- icon.Remove();
- // Revert back the indent but only if it is indented by this function
- if (element.vars[INDENT_MODIFIED_BY_ICON_VAR].GetBool())
- element.indent = 0;
- return;
- }
- // The UI element must itself has been indented to reserve the space for the icon
- if (element.indent == 0)
- {
- element.indent = 1;
- element.vars[INDENT_MODIFIED_BY_ICON_VAR] = true;
- }
- // If no icon yet then create one with the correct indent and size in respect to the UI element
- if (icon is null)
- {
- // The icon is placed at one indent level less than the UI element
- icon = BorderImage("Icon");
- icon.indent = element.indent - 1;
- icon.SetFixedSize(element.indentWidth - 2, 14);
- element.InsertChild(0, icon); // Ensure icon is added as the first child
- }
- // Set the icon type
- if (!icon.SetStyle(iconType, iconStyle))
- icon.SetStyle("Unknown", iconStyle); // If fails then use an 'unknown' icon type
- icon.color = Color(1,1,1,1); // Reset to enabled color
- }
- void SetIconEnabledColor(UIElement@ element, bool enabled, bool partial = false)
- {
- BorderImage@ icon = element.GetChild("Icon");
- if (icon !is null)
- {
- if (partial)
- {
- icon.colors[C_TOPLEFT] = Color(1,1,1,1);
- icon.colors[C_BOTTOMLEFT] = Color(1,1,1,1);
- icon.colors[C_TOPRIGHT] = Color(1,0,0,1);
- icon.colors[C_BOTTOMRIGHT] = Color(1,0,0,1);
- }
- else
- icon.color = enabled ? Color(1,1,1,1) : Color(1,0,0,1);
- }
- }
- void UpdateDirtyUI()
- {
- UpdateDirtyToolBar();
- terrainEditor.UpdateDirty();
- // Perform hierarchy selection latently after the new selections are finalized (used in undo/redo action)
- if (!hierarchyUpdateSelections.empty)
- {
- hierarchyList.SetSelections(hierarchyUpdateSelections);
- hierarchyUpdateSelections.Clear();
- }
- // Perform some event-triggered updates latently in case a large hierarchy was changed
- if (attributesFullDirty || attributesDirty)
- UpdateAttributeInspector(attributesFullDirty);
- }
- void HandleMessageAcknowledgement(StringHash eventType, VariantMap& eventData)
- {
- if (eventData["OK"].GetBool())
- messageBoxCallback();
- else
- messageBoxCallback = null;
- }
- void PopulateMruScenes()
- {
- mruScenesPopup.RemoveAllChildren();
- if (uiRecentScenes.length > 0)
- {
- recentSceneMenu.enabled = true;
- for (uint i=0; i < uiRecentScenes.length; ++i)
- mruScenesPopup.AddChild(CreateMenuItem(uiRecentScenes[i], @LoadMostRecentScene, 0, 0, false, "", false));
- }
- else
- recentSceneMenu.enabled = false;
- }
- bool LoadMostRecentScene()
- {
- Menu@ menu = GetEventSender();
- if (menu is null)
- return false;
- Text@ text = menu.GetChildren()[0];
- if (text is null)
- return false;
- return LoadScene(text.text);
- }
- // Set from click to false if opening menu procedurally.
- void OpenContextMenu(bool fromClick=true)
- {
- if (contextMenu is null)
- return;
- contextMenu.enabled = true;
- contextMenu.visible = true;
- contextMenu.BringToFront();
- if (fromClick)
- contextMenuActionWaitFrame=true;
- }
- void CloseContextMenu()
- {
- if (contextMenu is null)
- return;
- contextMenu.enabled = false;
- contextMenu.visible = false;
- }
- void ActivateContextMenu(Array<UIElement@> actions)
- {
- contextMenu.RemoveAllChildren();
- for (uint i=0; i< actions.length; ++i)
- {
- contextMenu.AddChild(actions[i]);
- }
- contextMenu.SetFixedHeight(24*actions.length+6);
- contextMenu.position = ui.cursor.screenPosition + IntVector2(10,-10);
- OpenContextMenu();
- }
- Menu@ CreateContextMenuItem(String text, String handler, String menuName = "", bool autoLocalize = true)
- {
- Menu@ menu = Menu();
- menu.defaultStyle = uiStyle;
- menu.style = AUTO_STYLE;
- menu.name = menuName;
- menu.SetLayout(LM_HORIZONTAL, 0, IntRect(8, 2, 8, 2));
- Text@ menuText = Text();
- menuText.style = "EditorMenuText";
- menu.AddChild(menuText);
- menuText.text = text;
- menuText.autoLocalizable = autoLocalize;
- menu.vars[VAR_CONTEXT_MENU_HANDLER] = handler;
- SubscribeToEvent(menu, "Released", "ContextMenuEventWrapper");
- return menu;
- }
- void ContextMenuEventWrapper(StringHash eventType, VariantMap& eventData)
- {
- UIElement@ uiElement = eventData["Element"].GetPtr();
- if (uiElement is null)
- return;
- String handler = uiElement.vars[VAR_CONTEXT_MENU_HANDLER].GetString();
- if (!handler.empty)
- {
- SubscribeToEvent(uiElement, "Released", handler);
- uiElement.SendEvent("Released", eventData);
- }
- CloseContextMenu();
- }
- /// Load a UI XML file used by the editor
- XMLFile@ GetEditorUIXMLFile(const String&in fileName)
- {
- // Prefer the executable path to avoid using the user's resource path, which may point
- // to an outdated Urho installation
- String fullFileName = fileSystem.programDir + "Data/" + fileName;
- if (fileSystem.FileExists(fullFileName))
- {
- File@ file = File(fullFileName, FILE_READ);
- XMLFile@ xml = XMLFile();
- xml.name = fileName;
- if (xml.Load(file))
- return xml;
- }
- // Fallback to resource system
- return cache.GetResource("XMLFile", fileName);
- }
- /// Load an UI layout used by the editor
- UIElement@ LoadEditorUI(const String&in fileName)
- {
- return ui.LoadLayout(GetEditorUIXMLFile(fileName));
- }
- /// Set node children as a spline path, either cyclic or non-cyclic
- bool SetSplinePath()
- {
- Menu@ menu = GetEventSender();
- if (menu is null)
- return false;
- return SceneSetChildrenSplinePath(menu.name == "Cyclic");
- }
- bool ColorWheelBuildMenuSelectTypeColor()
- {
- if (selectedNodes.empty && selectedComponents.empty) return false;
- editMode = EDIT_SELECT;
- // do coloring only for single selected object
- // start with trying to find single component
- if (selectedComponents.length == 1)
- {
- coloringComponent = selectedComponents[0];
- }
- // else try to get first component from selected node
- else if (selectedNodes.length == 1)
- {
- Array<Component@> components = selectedNodes[0].GetComponents();
- if (components.length > 0)
- {
- coloringComponent = components[0];
- }
- }
- else
- return false;
- if (coloringComponent is null) return false;
- Array<UIElement@> actions;
- if (coloringComponent.typeName == "Light")
- {
- actions.Push(CreateContextMenuItem("Light color", "HandleColorWheelMenu", "menuLightColor"));
- actions.Push(CreateContextMenuItem("Specular intensity", "HandleColorWheelMenu", "menuSpecularIntensity"));
- actions.Push(CreateContextMenuItem("Brightness multiplier", "HandleColorWheelMenu", "menuBrightnessMultiplier"));
- actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
- }
- else if (coloringComponent.typeName == "StaticModel")
- {
- actions.Push(CreateContextMenuItem("Diffuse color", "HandleColorWheelMenu", "menuDiffuseColor"));
- actions.Push(CreateContextMenuItem("Specular color", "HandleColorWheelMenu", "menuSpecularColor"));
- actions.Push(CreateContextMenuItem("Emissive color", "HandleColorWheelMenu", "menuEmissiveColor"));
- actions.Push(CreateContextMenuItem("Environment map color", "HandleColorWheelMenu", "menuEnvironmentMapColor"));
- actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
- }
- else if (coloringComponent.typeName == "Zone")
- {
- actions.Push(CreateContextMenuItem("Ambient color", "HandleColorWheelMenu", "menuAmbientColor"));
- actions.Push(CreateContextMenuItem("Fog color", "HandleColorWheelMenu", "menuFogColor"));
- actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
- }
- else if (coloringComponent.typeName == "Text3D")
- {
- actions.Push(CreateContextMenuItem("Color", "HandleColorWheelMenu", "c"));
- actions.Push(CreateContextMenuItem("Top left color", "HandleColorWheelMenu", "tl"));
- actions.Push(CreateContextMenuItem("Top right color", "HandleColorWheelMenu", "tr"));
- actions.Push(CreateContextMenuItem("Bottom left color", "HandleColorWheelMenu", "bl"));
- actions.Push(CreateContextMenuItem("Bottom right color", "HandleColorWheelMenu", "br"));
- actions.Push(CreateContextMenuItem("Cancel", "HandleColorWheelMenu", "menuCancel"));
- }
- if (actions.length > 0) {
- ActivateContextMenu(actions);
- return true;
- }
- return false;
- }
- void HandleColorWheelMenu()
- {
- ColorWheelSetupBehaviorForColoring();
- }
- // color was changed, update color of all colorGroup for immediate preview;
- void HandleWheelChangeColor(StringHash eventType, VariantMap& eventData)
- {
- if (timeToNextColoringGroupUpdate > time.systemTime) return;
- if (coloringComponent !is null)
- {
- Color c = eventData["Color"].GetColor(); // current ColorWheel
- // preview new color
- if (coloringComponent.typeName == "Light")
- {
- Light@ light = cast<Light>(coloringComponent);
- if (light !is null)
- {
- if (coloringPropertyName == "menuLightColor")
- {
- light.color = c;
- }
- else if (coloringPropertyName == "menuSpecularIntensity")
- {
- // multiply out
- light.specularIntensity = c.Value() * 10.0f;
- }
- else if (coloringPropertyName == "menuBrightnessMultiplier")
- {
- light.brightness = c.Value() * 10.0f;
- }
- attributesDirty = true;
- }
- }
- else if (coloringComponent.typeName == "StaticModel")
- {
- StaticModel@ model = cast<StaticModel>(coloringComponent);
- if (model !is null)
- {
- Material@ mat = model.materials[0];
- if (mat !is null)
- {
- if (coloringPropertyName == "menuDiffuseColor")
- {
- Variant oldValue = mat.shaderParameters["MatDiffColor"];
- Variant newValue;
- String valueString;
- valueString += String(c.r).Substring(0,5);
- valueString += " ";
- valueString += String(c.g).Substring(0,5);
- valueString += " ";
- valueString += String(c.b).Substring(0,5);
- valueString += " ";
- valueString += String(c.a).Substring(0,5);
- newValue.FromString(oldValue.type, valueString);
- mat.shaderParameters["MatDiffColor"] = newValue;
- }
- else if (coloringPropertyName == "menuSpecularColor")
- {
- Variant oldValue = mat.shaderParameters["MatSpecColor"];
- Variant newValue;
- String valueString;
- valueString += String(c.r).Substring(0,5);
- valueString += " ";
- valueString += String(c.g).Substring(0,5);
- valueString += " ";
- valueString += String(c.b).Substring(0,5);
- valueString += " ";
- valueString += String(c.a * 128).Substring(0,5);
- newValue.FromString(oldValue.type, valueString);
- mat.shaderParameters["MatSpecColor"] = newValue;
- }
- else if (coloringPropertyName == "menuEmissiveColor")
- {
- Variant oldValue = mat.shaderParameters["MatEmissiveColor"];
- Variant newValue;
- String valueString;
- valueString += String(c.r).Substring(0,5);
- valueString += " ";
- valueString += String(c.g).Substring(0,5);
- valueString += " ";
- valueString += String(c.b).Substring(0,5);
- valueString += " ";
- valueString += String(c.a).Substring(0,5);
- newValue.FromString(oldValue.type, valueString);
- mat.shaderParameters["MatEmissiveColor"] = newValue;
- }
- else if (coloringPropertyName == "menuEnvironmentMapColor")
- {
- Variant oldValue = mat.shaderParameters["MatEnvMapColor"];
- Variant newValue;
- String valueString;
- valueString += String(c.r).Substring(0,5);
- valueString += " ";
- valueString += String(c.g).Substring(0,5);
- valueString += " ";
- valueString += String(c.b).Substring(0,5);
- valueString += " ";
- valueString += String(c.a).Substring(0,5);
- newValue.FromString(oldValue.type, valueString);
- mat.shaderParameters["MatEnvMapColor"] = newValue;
- }
- }
- }
- }
- else if (coloringComponent.typeName == "Zone")
- {
- Zone@ zone = cast<Zone>(coloringComponent);
- if (zone !is null)
- {
- if (coloringPropertyName == "menuAmbientColor")
- {
- zone.ambientColor = c;
- }
- else if (coloringPropertyName == "menuFogColor")
- {
- zone.fogColor = c;
- }
- attributesDirty = true;
- }
- }
- else if (coloringComponent.typeName == "Text3D")
- {
- Text3D@ txt = cast<Text3D>(coloringComponent);
- if (txt !is null)
- {
- if (coloringPropertyName == "c")
- txt.color = c;
- else if (coloringPropertyName == "tl")
- txt.colors[C_TOPLEFT] = c;
- else if (coloringPropertyName == "tr")
- txt.colors[C_TOPRIGHT] = c;
- else if (coloringPropertyName == "bl")
- txt.colors[C_BOTTOMLEFT] = c;
- else if (coloringPropertyName == "br")
- txt.colors[C_BOTTOMRIGHT] = c;
- attributesDirty = true;
- }
- }
- }
- timeToNextColoringGroupUpdate = time.systemTime + stepColoringGroupUpdate;
- }
- // Return old colors, wheel was closed or color discarded
- void HandleWheelDiscardColor(StringHash eventType, VariantMap& eventData)
- {
- if (coloringComponent !is null)
- {
- //Color oldColor = eventData["Color"].GetColor(); //Old color from ColorWheel from ShowColorWheelWithColor(old)
- Color oldColor = coloringOldColor;
- // preview new color
- if (coloringComponent.typeName == "Light")
- {
- Light@ light = cast<Light>(coloringComponent);
- if (light !is null)
- {
- if (coloringPropertyName == "menuLightColor")
- {
- light.color = oldColor;
- }
- else if (coloringPropertyName == "menuSpecularIntensity")
- {
- light.specularIntensity = coloringOldScalar * 10.0f;
- }
- else if (coloringPropertyName == "menuBrightnessMultiplier")
- {
- light.brightness = coloringOldScalar * 10.0f;
- }
- attributesDirty = true;
- }
- }
- else if (coloringComponent.typeName == "StaticModel")
- {
- StaticModel@ model = cast<StaticModel>(coloringComponent);
- if (model !is null)
- {
- Material@ mat = model.materials[0];
- if (mat !is null)
- {
- if (coloringPropertyName == "menuDiffuseColor")
- {
- Variant oldValue = mat.shaderParameters["MatDiffColor"];
- Variant newValue;
- String valueString;
- valueString += String(oldColor.r).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.g).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.b).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.a).Substring(0,5);
- newValue.FromString(oldValue.type, valueString);
- mat.shaderParameters["MatDiffColor"] = newValue;
- }
- else if (coloringPropertyName == "menuSpecularColor")
- {
- Variant oldValue = mat.shaderParameters["MatSpecColor"];
- Variant newValue;
- String valueString;
- valueString += String(oldColor.r).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.g).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.b).Substring(0,5);
- valueString += " ";
- valueString += String(coloringOldScalar).Substring(0,5);
- newValue.FromString(oldValue.type, valueString);
- mat.shaderParameters["MatSpecColor"] = newValue;
- }
- else if (coloringPropertyName == "menuEmissiveColor")
- {
- Variant oldValue = mat.shaderParameters["MatEmissiveColor"];
- Variant newValue;
- String valueString;
- valueString += String(oldColor.r).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.g).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.b).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.a).Substring(0,5);
- newValue.FromString(oldValue.type, valueString);
- mat.shaderParameters["MatEmissiveColor"] = newValue;
- }
- else if (coloringPropertyName == "menuEnvironmentMapColor")
- {
- Variant oldValue = mat.shaderParameters["MatEnvMapColor"];
- Variant newValue;
- String valueString;
- valueString += String(oldColor.r).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.g).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.b).Substring(0,5);
- valueString += " ";
- valueString += String(oldColor.a).Substring(0,5);
- newValue.FromString(oldValue.type, valueString);
- mat.shaderParameters["MatEnvMapColor"] = newValue;
- }
- }
- }
- }
- else if (coloringComponent.typeName == "Zone")
- {
- Zone@ zone = cast<Zone>(coloringComponent);
- if (zone !is null)
- {
- if (coloringPropertyName == "menuAmbientColor")
- {
- zone.ambientColor = oldColor;
- }
- else if (coloringPropertyName == "menuFogColor")
- {
- zone.fogColor = oldColor;
- }
- attributesDirty = true;
- }
- }
- }
- }
- // Applying color wheel changes to material
- void HandleWheelSelectColor(StringHash eventType, VariantMap& eventData)
- {
- if (coloringComponent !is null)
- if (coloringComponent.typeName == "StaticModel")
- {
- Color c = eventData["Color"].GetColor(); //Selected color from ColorWheel
- StaticModel@ model = cast<StaticModel>(coloringComponent);
- if (model !is null)
- {
- Material@ mat = model.materials[0];
- if (mat !is null)
- {
- editMaterial = mat;
- SaveMaterial();
- }
- }
- }
- }
- bool ViewDebugIcons()
- {
- debugIconsShow = !debugIconsShow;
- return true;
- }
|