Add handy utils to TickFactors/DisplaySettings
* Add static TickFactors presets * Add TickDisplaySettings default * Make TickFactors implement ICloneable<TickFactors> * Add 'with' ext fn TODO: * Replace instantiations of TickDisplaySettings that rely on inlined default values with the TickDisplaySettings.Default prop
This commit is contained in:
1 parent
3cd0c4073e
commit
4b27bbe490
3 files changed
+68
-9
No files matched your search
@@ -1,18 +1,58 @@
|
||||
using System;
|
||||
|
||||
namespace mbts.user.settings {
|
||||
public class TickDisplaySettings {
|
||||
using bts;
|
||||
|
||||
using ProtoBuf;
|
||||
|
||||
using System;
|
||||
|
||||
[ProtoContract]
|
||||
public class TickDisplaySettings : ICloneable<TickDisplaySettings>, IEquatable<TickDisplaySettings> {
|
||||
public static TickDisplaySettings Default => new() {
|
||||
pad_with_leading_zeros = true,
|
||||
display_multiplier = double.NaN,
|
||||
show_commas = false,
|
||||
nan_text = "-",
|
||||
show_decimal_part = true,
|
||||
show_zero_integer = true
|
||||
};
|
||||
|
||||
public bool pad_with_leading_zeros { get; set; } = true;
|
||||
public double display_multiplier { get; set; } = double.NaN;
|
||||
public bool show_commas { get; set; } = false;
|
||||
public string nan_text { get; set; } = "-";
|
||||
public bool show_decimal_part { get; set; } = true;
|
||||
public bool show_zero_integer { get; set; } = true;
|
||||
|
||||
public static TickDisplaySettings Default =>
|
||||
new TickDisplaySettings();
|
||||
|
||||
public TickDisplaySettings Clone() =>
|
||||
new() {
|
||||
pad_with_leading_zeros = pad_with_leading_zeros,
|
||||
display_multiplier = display_multiplier,
|
||||
show_commas = show_commas,
|
||||
nan_text = nan_text,
|
||||
show_decimal_part = show_decimal_part,
|
||||
show_zero_integer = show_zero_integer
|
||||
};
|
||||
|
||||
public bool Equals(TickDisplaySettings other) =>
|
||||
other is not null &&
|
||||
pad_with_leading_zeros == other.pad_with_leading_zeros &&
|
||||
display_multiplier.Equals(other.display_multiplier) &&
|
||||
show_commas == other.show_commas &&
|
||||
nan_text == other.nan_text &&
|
||||
show_decimal_part == other.show_decimal_part &&
|
||||
show_zero_integer == other.show_zero_integer;
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
Equals(obj as TickDisplaySettings);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
// All props are mutable, and thus can't be incorporated into the hash code
|
||||
GetType().GetHashCode();
|
||||
|
||||
object ICloneable.Clone() =>
|
||||
Clone();
|
||||
}
|
||||
|
||||
|
||||
public static class TickDisplaySettingsExtensions {
|
||||
public static TickDisplaySettings with(
|
||||
this TickDisplaySettings settings,
|
||||
@@ -22,7 +62,7 @@ namespace mbts.user.settings {
|
||||
string nan_text = null,
|
||||
bool? show_decimal_part = null,
|
||||
bool? show_zero_integer = null) =>
|
||||
new TickDisplaySettings {
|
||||
new() {
|
||||
pad_with_leading_zeros = pad_with_leading_zeros ?? settings.pad_with_leading_zeros,
|
||||
display_multiplier = display_multiplier ?? settings.display_multiplier,
|
||||
show_commas = show_commas ?? settings.show_commas,
|
||||
|
||||
@@ -246,6 +246,9 @@ namespace mbts {
|
||||
return Math.Max(1, dec > 0 ? ++floor : floor);
|
||||
}
|
||||
|
||||
public static TickFactors with(this TickFactors tick_factors, double? tick_base = null, double? tick_unit = null) =>
|
||||
new(tick_base ?? tick_factors.tick_base, tick_unit ?? tick_factors.tick_unit);
|
||||
|
||||
#region Private Members
|
||||
private static char separator(this TickFactors tick) {
|
||||
double tb_log10 = Math.Log10(tick.tick_base);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
namespace mbts.user.settings {
|
||||
using bts;
|
||||
using bts.utils;
|
||||
|
||||
using ProtoBuf;
|
||||
@@ -7,7 +8,7 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
[ProtoContract]
|
||||
public class TickFactors : IEquatable<TickFactors> {
|
||||
public class TickFactors : IEquatable<TickFactors>, ICloneable<TickFactors> {
|
||||
[ProtoMember(1), DefaultValue(100.0)]
|
||||
readonly double _tick_base;
|
||||
[ProtoMember(2), DefaultValue(1.0)]
|
||||
@@ -21,9 +22,21 @@
|
||||
_tick_unit = tick_unit;
|
||||
}
|
||||
|
||||
public static TickFactors pennies => new(100, 1);
|
||||
public static TickFactors nickels => new(100, 5);
|
||||
public static TickFactors dimes => new(100, 10);
|
||||
public static TickFactors quarters => new(100, 25);
|
||||
public static TickFactors fractional_8 => new(8, 1);
|
||||
public static TickFactors fractional_16 => new(16, 1);
|
||||
public static TickFactors fractional_32 => new(32, 1);
|
||||
public static TickFactors fractional_64 => new(64, 1);
|
||||
|
||||
public double tick_base => _tick_base;
|
||||
public double tick_unit => _tick_unit;
|
||||
|
||||
public TickFactors Clone() =>
|
||||
new(tick_base, tick_unit);
|
||||
|
||||
public bool Equals(TickFactors other) =>
|
||||
other is not null &&
|
||||
tick_base.Equals(other.tick_base) &&
|
||||
@@ -39,6 +52,9 @@
|
||||
public override string ToString() =>
|
||||
"{0}/{1}".fmt(tick_unit, tick_base);
|
||||
|
||||
object ICloneable.Clone() =>
|
||||
Clone();
|
||||
|
||||
public static bool operator ==(TickFactors x, TickFactors y) =>
|
||||
x.safe_equals(y);
|
||||
|
||||
|
||||
Reference in new issue
Block a user