/// <summary> /// Creates a button with speed value on it, and label under it, showing opposite units. /// Also can be zero (reset to default) and 1000 km/h (unlimited speed button). /// </summary> /// <param name="builder">UI builder.</param> /// <param name="actionOnClick">What happens if clicked.</param> /// <param name="parentTool">Parent speedlimits tool.</param> /// <param name="buttonPanel">Panel where buttons are added to.</param> /// <param name="speedInteger">Integer value of the speed in the selected units.</param> /// <param name="speedValue">Speed value of the button we're creating.</param> /// <returns>The new button.</returns> private SpeedLimitPaletteButton CreatePaletteButton(UBuilder builder, SetSpeedLimitAction actionOnClick, SpeedLimitsTool parentTool, UPanel buttonPanel, int speedInteger, SpeedValue speedValue) { // Helper function to choose text for the button string GetSpeedButtonText() { if (speedInteger == 0) { return("✖"); // Unicode symbol U+2716 Heavy Multiplication X } if (speedValue.GameUnits >= SpeedValue.UNLIMITED) { return("⊘"); // Unicode symbol U+2298 Circled Division Slash } return(speedInteger.ToString()); } var button = builder.Button <SpeedLimitPaletteButton>(parent: buttonPanel); button.text = GetSpeedButtonText(); button.textScale = UIScaler.UIScale; button.textHorizontalAlignment = UIHorizontalAlignment.Center; button.normalBgSprite = button.hoveredBgSprite = "GenericPanel"; button.color = new Color32(128, 128, 128, 240); // button must know what to do with its speed value button.AssignedAction = actionOnClick; // The click events will be routed via the parent tool OnPaletteButtonClicked button.ParentTool = parentTool; button.SetStacking(UStackMode.NewRowBelow); // Width will be overwritten in SpeedLimitPaletteButton.UpdateSpeedLimitButton button.SetFixedSize( new Vector2( SpeedLimitPaletteButton.DEFAULT_WIDTH, SpeedLimitPaletteButton.DEFAULT_HEIGHT)); return(button); }
private void CreatePaletteButtonHintLabel(UBuilder builder, bool showMph, SpeedValue speedValue, SpeedLimitPaletteButton button, UPanel buttonPanel) { // Other speed unit info label string otherUnit = showMph ? ToKmphPreciseString(speedValue) : ToMphPreciseString(speedValue); // Choose label text under the button string GetSpeedButtonHintText() { if (FloatUtil.NearlyEqual(speedValue.GameUnits, 0.0f)) { return(Translation.SpeedLimits.Get("Palette.Text:Default")); } if (speedValue.GameUnits >= SpeedValue.UNLIMITED) { return(Translation.SpeedLimits.Get("Palette.Text:Unlimited")); } return(otherUnit); } ULabel label = button.AltUnitsLabel = builder.Label_( parent: buttonPanel, t: GetSpeedButtonHintText(), stack: UStackMode.Below); label.width = SpeedLimitPaletteButton.SELECTED_WIDTH; label.textAlignment = UIHorizontalAlignment.Center; label.ContributeToBoundingBox(false); // parent ignore our width }
/// <summary> /// Create button triples for number of lanes. /// Buttons are linked to lanes later by LaneArrowTool class. /// </summary> /// <param name="builder">The UI Builder.</param> /// <param name="numLanes">How many lane groups.</param> public void SetupControls(UBuilder builder, int numLanes) { Buttons = new List <LaneArrowButton>(); var buttonRowPanel = builder.Panel_(parent: this, stack: UStackMode.NewRowBelow); buttonRowPanel.name = "TMPE_ButtonRow"; buttonRowPanel.SetPadding(UPadding.Default); buttonRowPanel.ResizeFunction((UResizer r) => { r.FitToChildren(); }); // ----------------------------------- // Create a row of button groups // [ Lane 1 ] [ Lane 2 ] [ Lane 3 ] ... // [ [←] [↑] [→] ] [... ] [ ... ] // ----------------------------------- for (var i = 0; i < numLanes; i++) { string buttonName = $"TMPE_LaneArrow_ButtonGroup{i + 1}"; UPanel buttonGroupPanel = builder.Panel_( parent: buttonRowPanel, stack: i == 0 ? UStackMode.Below : UStackMode.ToTheRight); buttonGroupPanel.name = buttonName; buttonGroupPanel.atlas = TextureUtil.Ingame; buttonGroupPanel.backgroundSprite = "GenericPanel"; int i1 = i; // copy of the loop variable, for the resizeFunction below buttonGroupPanel.ResizeFunction((UResizer r) => { r.FitToChildren(); }); buttonGroupPanel.SetPadding(UPadding.Default); // Create a label with "Lane #" title string labelText = Translation.LaneRouting.Get("Format.Label:Lane") + " " + (i + 1); ULabel laneLabel = builder.Label_( parent: buttonGroupPanel, t: labelText); // The label will be repositioned to the top of the parent laneLabel.ResizeFunction(r => { r.Stack(UStackMode.Below); }); // Create and populate the panel with buttons // 3 buttons are created [←] [↑] [→], // The click event is assigned outside in LaneArrowTool.cs foreach (string prefix in new[] { "LaneArrowLeft", "LaneArrowForward", "LaneArrowRight", }) { LaneArrowButton arrowButton = builder.Button <LaneArrowButton>( parent: buttonGroupPanel, text: string.Empty, tooltip: null, size: new Vector2(40f, 40f), stack: prefix == "LaneArrowLeft" ? UStackMode.Below : UStackMode.ToTheRight); arrowButton.atlas = GetAtlas(); arrowButton.Skin = CreateDefaultButtonSkin(); arrowButton.Skin.ForegroundPrefix = prefix; Buttons.Add(arrowButton); } // for each button } // end button loop, for each lane }