/// <summary>
        /// Based on the MPH/KMPH settings round the current speed to the nearest STEP and
        /// then increase by STEP.
        /// </summary>
        /// <param name="speed">Ingame speed</param>
        /// <returns>Ingame speed increased by the increment for MPH or KMPH</returns>
        public static SpeedValue GetNext(SpeedValue speed)
        {
            if (speed.GameUnits < 0f)
            {
                return(new SpeedValue(-1f));
            }

            if (GlobalConfig.Instance.Main.DisplaySpeedLimitsMph)
            {
                MphValue rounded = speed.ToMphRounded(MPH_STEP);
                rounded += MPH_STEP;

                if (rounded.Mph > UPPER_MPH)
                {
                    rounded = new MphValue(0);
                }

                return(SpeedValue.FromMph(rounded));
            }
            else
            {
                KmphValue rounded = speed.ToKmphRounded(KMPH_STEP);
                rounded += KMPH_STEP;

                if (rounded.Kmph > UPPER_KMPH)
                {
                    rounded = new KmphValue(0);
                }

                return(SpeedValue.FromKmph(rounded));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Given the float speed, style and MPH option return a texture to render.
        /// </summary>
        /// <param name="spd">float speed</param>
        /// <param name="mphStyle">Signs theme</param>
        /// <param name="unit">Mph or km/h</param>
        /// <returns></returns>
        public static Texture2D GetSpeedLimitTexture(SpeedValue spd, MphSignStyle mphStyle, SpeedUnit unit)
        {
            // Select the source for the textures based on unit and the theme
            bool mph = unit == SpeedUnit.Mph;
            IDictionary <int, Texture2D> textures = TexturesKmph;

            if (mph)
            {
                switch (mphStyle)
                {
                case MphSignStyle.SquareUS:
                    textures = TexturesMphUS;
                    break;

                case MphSignStyle.RoundUK:
                    textures = TexturesMphUK;
                    break;

                case MphSignStyle.RoundGerman:
                    // Do nothing, this is the default above
                    break;
                }
            }

            // Round to nearest 5 MPH or nearest 10 km/h
            ushort index = mph ? spd.ToMphRounded(SpeedLimitsTool.MPH_STEP).Mph
                               : spd.ToKmphRounded(SpeedLimitsTool.KMPH_STEP).Kmph;

            // Trim the index since 140 km/h / 90 MPH is the max sign we have
            ushort upper = mph ? SpeedLimitsTool.UPPER_MPH
                               : SpeedLimitsTool.UPPER_KMPH;

            // Show unlimited if the speed cannot be represented by the available sign textures
            if (index == 0 || index > upper)
            {
                // Log._Debug($"Trimming speed={speedLimit} index={index} to {upper}");
                return(textures[0]);
            }

            // Trim from below to not go below index 5 (5 kmph or 5 mph)
            ushort trimIndex = Math.Max((ushort)5, index);

            return(textures[trimIndex]);
        }
        /// <summary>
        /// Based on the MPH/KMPH settings round the current speed to the nearest STEP and
        /// then decrease by STEP.
        /// </summary>
        /// <param name="speed">Ingame speed</param>
        /// <returns>Ingame speed decreased by the increment for MPH or KMPH</returns>
        public static SpeedValue GetPrevious(SpeedValue speed)
        {
            if (speed.GameUnits < 0f)
            {
                return(new SpeedValue(-1f));
            }

            if (GlobalConfig.Instance.Main.DisplaySpeedLimitsMph)
            {
                MphValue rounded = speed.ToMphRounded(MPH_STEP);
                if (rounded.Mph == LOWER_MPH)
                {
                    return(new SpeedValue(0));
                }

                if (rounded.Mph == 0)
                {
                    return(SpeedValue.FromMph(UPPER_MPH));
                }

                return(SpeedValue.FromMph(rounded.Mph > LOWER_MPH
                                              ? (ushort)(rounded.Mph - MPH_STEP)
                                              : LOWER_MPH));
            }
            else
            {
                KmphValue rounded = speed.ToKmphRounded(KMPH_STEP);
                if (rounded.Kmph == LOWER_KMPH)
                {
                    return(new SpeedValue(0));
                }

                if (rounded.Kmph == 0)
                {
                    return(SpeedValue.FromKmph(UPPER_KMPH));
                }

                return(SpeedValue.FromKmph(rounded.Kmph > LOWER_KMPH
                                               ? (ushort)(rounded.Kmph - KMPH_STEP)
                                               : LOWER_KMPH));
            }
        }
Beispiel #4
0
            SetupControls_SpeedPalette_Button(UBuilder builder,
                                              UIComponent parent,
                                              bool showMph,
                                              SetSpeedLimitAction actionOnClick,
                                              SpeedLimitsTool parentTool)
            {
                SpeedValue speedValue =
                    actionOnClick.Type == SetSpeedLimitAction.ActionType.ResetToDefault
                        ? default
                        : actionOnClick.GuardedValue.Override;

                int speedInteger = showMph
                                       ? speedValue.ToMphRounded(RoadSignThemes.MPH_STEP).Mph
                                       : speedValue.ToKmphRounded(RoadSignThemes.KMPH_STEP).Kmph;

                //--------------------------------
                // Create vertical combo:
                // |[  100   ]|
                // | "65 mph" |
                //--------------------------------
                // Create a small panel which stacks together with other button panels horizontally
                var buttonPanel = builder.Panel_(parent: parent);

                buttonPanel.name = $"{GAMEOBJECT_NAME}_Button_{speedInteger}";
                buttonPanel.ResizeFunction(
                    resizeFn: (UResizer r) => {
                    r.Stack(UStackMode.ToTheRight, spacing: 2f);
                    r.FitToChildren();
                });

                SpeedLimitPaletteButton button = this.CreatePaletteButton(
                    builder,
                    actionOnClick,
                    parentTool,
                    buttonPanel,
                    speedInteger,
                    speedValue);

                this.CreatePaletteButtonHintLabel(builder, showMph, speedValue, button, buttonPanel);
                return(button);
            }