/// <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); }
public void SetupControls(SpeedLimitsToolWindow window, UBuilder builder) { this.name = GAMEOBJECT_NAME + "_ModesPanel"; void ButtonpanelResizeFn(UResizer r) { r.Stack( mode: UStackMode.NewRowBelow, spacing: UConst.UIPADDING, stackRef: window.windowTitleLabel_); r.FitToChildren(); } this.ResizeFunction(ButtonpanelResizeFn); Vector2 buttonSize = new Vector2(40f, 40f); UITextureAtlas uiAtlas = window.GetUiAtlas(); LookupTable translation = Translation.SpeedLimits; //---------------- // Edit Segments/Lanes mode button //---------------- this.SegmentModeButton = builder.Button <UButton>( parent: this, text: string.Empty, tooltip: translation.Get("Tooltip:Edit segment speed limits"), size: buttonSize, stack: UStackMode.Below); this.SegmentModeButton.atlas = uiAtlas; // Note the atlas is loaded before this skin is created in window.GetUiAtlas() this.SegmentModeButton.Skin = ButtonSkin.CreateSimple( foregroundPrefix: "EditSegments", backgroundPrefix: UConst.MAINMENU_ROUND_BUTTON_BG) .CanActivate(background: false) .CanHover(foreground: false); this.SegmentModeButton.ApplyButtonSkin(); // the onclick handler is set by SpeedLimitsTool outside of this module //---------------- // Edit Lanes mode button //---------------- this.LaneModeButton = builder.Button <UButton>( parent: this, text: string.Empty, tooltip: translation.Get("Tooltip:Edit lane speed limits"), size: buttonSize, stack: UStackMode.ToTheRight); this.LaneModeButton.atlas = uiAtlas; // Note the atlas is loaded before this skin is created in window.GetUiAtlas() this.LaneModeButton.Skin = ButtonSkin .CreateSimple( foregroundPrefix: "EditLanes", backgroundPrefix: UConst.MAINMENU_ROUND_BUTTON_BG) .CanActivate(background: false) .CanHover(foreground: false); this.LaneModeButton.ApplyButtonSkin(); // the onclick handler is set by SpeedLimitsTool outside of this module //---------------- // Edit Defaults mode button //---------------- this.DefaultsModeButton = builder.Button <UButton>( parent: this, text: string.Empty, tooltip: translation.Get("Tooltip:Default speed limits per road type"), size: buttonSize, stack: UStackMode.NewRowBelow); this.DefaultsModeButton.atlas = uiAtlas; // Note the atlas is loaded before this skin is created in window.GetUiAtlas() this.DefaultsModeButton.Skin = ButtonSkin .CreateSimple( foregroundPrefix: "EditDefaults", backgroundPrefix: UConst.MAINMENU_ROUND_BUTTON_BG) .CanActivate(background: false) .CanHover(foreground: false); this.DefaultsModeButton.ApplyButtonSkin(); // the onclick handler is set by SpeedLimitsTool outside of this module //---------------- // MPH/Kmph switch //---------------- bool displayMph = GlobalConfig.Instance.Main.DisplaySpeedLimitsMph; this.ToggleMphButton = builder.Button <MphToggleButton>( parent: this, text: string.Empty, tooltip: displayMph ? translation.Get("Miles per hour") : translation.Get("Kilometers per hour"), size: buttonSize, stack: UStackMode.ToTheRight); this.ToggleMphButton.atlas = uiAtlas; // Note the atlas is loaded before this skin is created in window.GetUiAtlas() this.ToggleMphButton.Skin = ButtonSkin.CreateSimple( foregroundPrefix: "MphToggle", backgroundPrefix: UConst.MAINMENU_ROUND_BUTTON_BG) .CanActivate(background: false) .CanHover(foreground: false); this.ToggleMphButton.ApplyButtonSkin(); // the onclick handler is set by SpeedLimitsTool outside of this module }
/// <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 }