/// <summary> /// Create a toggle button at the specified position, assign it to a parent <c>Component</c> and return it. /// </summary> /// <remarks> /// Uses <c>STANDARD_UI_ELEMENT_WIDTH</c> and <c>STANDARD_UI_ELEMENT_HEIGHT</c> to determine the /// width and height of this object. Using other values might lead to weird results. /// </remarks> /// <param name="parent">The toggle buttons's supposed parent.</param> /// <param name="name">The Unity name of the toggle button.</param> /// <param name="x">The x-position of the toggle button, relative to the container's upper left corner.</param> /// <param name="y">The y-position of the toggle button, relative to the container's upper left corner.</param> /// <returns>The created toggle button.</returns> public static GameUISelectableToggle AddToggleToComponent(Component parent, string name, float x, float y) { GameUISelectableToggle toggle = GameObject.Instantiate(toggleTemplate, parent.transform); toggle.name = name; (toggle.transform as RectTransform).anchorMin = new Vector2(0, 1); (toggle.transform as RectTransform).anchorMax = new Vector2(0, 1); (toggle.transform as RectTransform).offsetMin = new Vector2(x, -y - STANDARD_UI_ELEMENT_HEIGHT); (toggle.transform as RectTransform).offsetMax = new Vector2(x + STANDARD_UI_TOGGLE_WIDTH, -y); return(toggle); }
/// <summary> /// Initalize a bunch of UI element templates: a dialog, a button, a dropdown menu, a slider, a toggle button and /// a label. /// </summary> /// <remarks> /// This method needs to be called before trying to instantiate any UI elements with <c>CreateCustomXXX</c> /// methods, otherwise those will throw unchecked NullPointerExceptions (I should probably handle those better). /// Currently, this is achieved by the <see cref="InitCustomUIElementsPatch"/>. Also, once it has been called, /// another call to this method has no effect. /// </remarks> /// <param name="settingsScreen">The settings screen. Using this to store the dialog template so that it doesn't /// get unloaded. There's probably a less hacky way to do this.</param> /// <param name="dialog">The dialog to be used as a template.</param> /// <param name="settingsDialog">The Settings Dialog, used to get a variety of UI elements.</param> public static void InitalizeTemplates(SettingsScreen settingsScreen, ScreenDialog dialog, SettingsDialog settingsDialog) { // This only needs to run once. if (!AreTemplatesInitalized()) { initalized = true; // Just store a bunch of UI elements from the settings screen as templates. They will always be loaded, // so we don't have to care about manipulating them in any way yet. buttonTemplate = Traverse.Create(settingsDialog).Field("keyMappingButton").GetValue <GameUISelectableButton>(); dropdownTemplate = Traverse.Create(settingsDialog).Field("gameSpeedDropdown").GetValue <GameUISelectableDropdown>();; toggleTemplate = Traverse.Create(settingsDialog).Field("googlyEyesToggle").GetValue <GameUISelectableToggle>(); sliderTemplate = Traverse.Create(Traverse.Create(settingsDialog).Field("scrollSensitivityControl").GetValue <ScrollSensitivityControl>()).Field("slider").GetValue <SelectableSliderHelper>(); // There don't seem to be any labels that are assigned to explicit fields, which kinda makes sense, so we'll have to search for one. // This unfortunately breaks pretty easily as shown by the latest update of the game, so I guess we need a long term solution. labelTemplate = settingsDialog.transform.Find("Content/Content/Audio Section/Global volume control").GetComponentInChildren <TextMeshProUGUI>(); // Print warnings if any initalization has been unsuccessful. if (buttonTemplate == null) { AdvancedRunHistory.Log("Button template is null.", LogLevel.Warning); } if (dropdownTemplate == null) { AdvancedRunHistory.Log("Dropdown template is null.", LogLevel.Warning); } if (toggleTemplate == null) { AdvancedRunHistory.Log("Toggle template is null.", LogLevel.Warning); } if (sliderTemplate == null) { AdvancedRunHistory.Log("Slider template is null.", LogLevel.Warning); } if (labelTemplate == null) { AdvancedRunHistory.Log("Label template is null.", LogLevel.Warning); } // We actually neeed to instantiate a copy of the dialog template right now, as the Patch Notes dialog will // unload as soon as you leave the Main Menu screen. Also, we can remove its contents while we're at it. dialogTemplate = GameObject.Instantiate(dialog, settingsScreen.transform); foreach (Transform child in dialogTemplate.transform.Find("Content")) { GameObject.Destroy(child.gameObject); } if (dialogTemplate == null) { AdvancedRunHistory.Log("Dialog template is null.", LogLevel.Warning); } } }