iceberg 'show quantity' param uses display divisor
This commit is contained in:
1 parent
c0ea6943e1
commit
02e7fb1ee0
11 files changed
+190
-38
No files matched your search
@@ -15,7 +15,7 @@ using AlgoType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase;
|
||||
namespace mbts.spark {
|
||||
class AlgoProviderFactory {
|
||||
[ImportingConstructor]
|
||||
AlgoProviderFactory(IAlgoStudioModel studio_model, DynamicOrderHelper dynamic_helper) {
|
||||
AlgoProviderFactory(IAlgoStudioModel studio_model, DynamicOrderHelper dynamic_helper, ISecurityAuxiliaryDataModel security_auxiliary_model) {
|
||||
var from_conf = config.Configuration.get<bts.config.AlgoTypeRuleSet>();
|
||||
if (from_conf.rules.Count == 0) {
|
||||
// There seems to be a config race bug, where the returned config values are empty, despite having entries in spark.conf.
|
||||
@@ -97,8 +97,8 @@ namespace mbts.spark {
|
||||
"\n\n\tAttachable\n" +
|
||||
attachable_rules.fmt_diagnostic("\n\t\t"));
|
||||
|
||||
security_algo_provider = new AlgoProvider(rules, studio_model, dynamic_helper);
|
||||
attachable_algo_provider = new AttachableAlgoProvider(attachable_rules, studio_model, dynamic_helper);
|
||||
security_algo_provider = new AlgoProvider(rules, studio_model, dynamic_helper, security_auxiliary_model);
|
||||
attachable_algo_provider = new AttachableAlgoProvider(attachable_rules, studio_model, dynamic_helper, security_auxiliary_model);
|
||||
} else {
|
||||
var algo_type_data =
|
||||
EnumHelper.get_values_except(AlgoType.Dynamic, AlgoType.Composite, AlgoType.None)
|
||||
@@ -106,8 +106,8 @@ namespace mbts.spark {
|
||||
.Concat(dynamic_helper.dynamic_types.Select(dt => _get_type_data(AlgoType.Dynamic, dt)))
|
||||
.ToListLazy();
|
||||
var conf_sets = from_conf.rules.Select(r => _to_security_algo_set(r, dynamic_helper, algo_type_data)).ToList();
|
||||
security_algo_provider = new AlgoProvider(conf_sets.Select(tup => tup.standard).ToList(), studio_model, dynamic_helper);
|
||||
attachable_algo_provider = new AttachableAlgoProvider(conf_sets.Select(tup => tup.attachable).ToList(), studio_model, dynamic_helper);
|
||||
security_algo_provider = new AlgoProvider(conf_sets.Select(tup => tup.standard).ToList(), studio_model, dynamic_helper, security_auxiliary_model);
|
||||
attachable_algo_provider = new AttachableAlgoProvider(conf_sets.Select(tup => tup.attachable).ToList(), studio_model, dynamic_helper, security_auxiliary_model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,13 +78,24 @@ namespace mbts.spark {
|
||||
public static IEnumerable<AlgoData> define_algos(
|
||||
this AlgoTypeSetImmutable algo_set,
|
||||
DynamicOrderHelper dynamic_helper,
|
||||
Func<CustomOrderDefinitionImmutable, CustomOrderDefinitionImmutable> fit_to_type = null) =>
|
||||
Func<CustomOrderDefinitionImmutable, CustomOrderDefinitionImmutable> fit_to_type = null,
|
||||
uint display_qty_divisor = 1) =>
|
||||
//
|
||||
algo_set.factory_types
|
||||
.Select(algo_type => {
|
||||
var def = OrderDataHelper.Defaults.CustomOrderDefinitionImmutable(algo_type);
|
||||
if (fit_to_type != null)
|
||||
def = fit_to_type(def);
|
||||
// multiply quantity props by display qty divisor to get appropriate actual value
|
||||
if (display_qty_divisor > 1) {
|
||||
switch (def.CustomCase) {
|
||||
case AlgoType.Iceberg:
|
||||
var mdef = def.CloneToMutable();
|
||||
mdef.Iceberg.ShowQuantity *= display_qty_divisor;
|
||||
def = new CustomOrderDefinitionImmutable(mdef);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return def;
|
||||
})
|
||||
.Concat(algo_set.dynamic_types.Select(dynamic_helper.create_custom_defi))
|
||||
@@ -94,14 +105,16 @@ namespace mbts.spark {
|
||||
this IEnumerable<SecurityAlgoTypeSetImmutable> security_algo_sets,
|
||||
SecurityCriteriaClause clause,
|
||||
DynamicOrderHelper dynamic_helper,
|
||||
Func<CustomOrderDefinitionImmutable, CustomOrderDefinitionImmutable> fit_to_type = null) {
|
||||
Func<CustomOrderDefinitionImmutable, CustomOrderDefinitionImmutable> fit_to_type = null,
|
||||
uint display_qty_divisor = 1) {
|
||||
//
|
||||
ImmutableDictionary<AlgoKey, AlgoData> rv = null;
|
||||
foreach (var set in security_algo_sets) {
|
||||
if (!clause.is_subset_of(set.security_criteria)) {
|
||||
continue;
|
||||
} else if (rv == null) {
|
||||
rv = ImmutableDictionary.CreateRange(set.algo_type_set.define_algos(dynamic_helper, fit_to_type)
|
||||
rv = ImmutableDictionary.CreateRange(
|
||||
set.algo_type_set.define_algos(dynamic_helper, fit_to_type, display_qty_divisor)
|
||||
.Select(a => new KeyValuePair<AlgoKey, AlgoData>(a.key, a)));
|
||||
} else {
|
||||
foreach (var kv in rv) {
|
||||
|
||||
@@ -26,13 +26,15 @@ namespace mbts.spark {
|
||||
class AlgoProvider : IAlgoProvider {
|
||||
readonly IEnumerable<SecurityAlgoTypeSetImmutable> _security_algo_types;
|
||||
readonly IAlgoStudioModel _studio_model;
|
||||
readonly ISecurityAuxiliaryDataModel _security_auxiliary_model;
|
||||
readonly IObservable<TableUpdate<AlgoData, AlgoKey>> _studio_algos_source;
|
||||
readonly DynamicOrderHelper _dynamic_helper;
|
||||
|
||||
public AlgoProvider(IEnumerable<SecurityAlgoTypeSetImmutable> algo_sets, IAlgoStudioModel studio_model, DynamicOrderHelper dynamic_helper) {
|
||||
public AlgoProvider(IEnumerable<SecurityAlgoTypeSetImmutable> algo_sets, IAlgoStudioModel studio_model, DynamicOrderHelper dynamic_helper, ISecurityAuxiliaryDataModel security_auxiliary_model) {
|
||||
_security_algo_types = algo_sets;
|
||||
_studio_model = studio_model;
|
||||
_dynamic_helper = dynamic_helper;
|
||||
_security_auxiliary_model = security_auxiliary_model;
|
||||
/// initialize <see cref="_studio_algos_source"/> with an empty table, so the
|
||||
/// <see cref="UpdateExtensions.AppendTable{T, TKey}(IObservable{TableUpdate{T, TKey}}, IEnumerable{T})"/>
|
||||
/// calls made by the 'subscribe_algos' methods will fire at least once, even if the underlying
|
||||
@@ -83,7 +85,8 @@ namespace mbts.spark {
|
||||
var system_algos = _security_algo_types.match_algos(
|
||||
clause,
|
||||
_dynamic_helper,
|
||||
def => _fit_to_type(def, security, _security_algo_types));
|
||||
def => _fit_to_type(def, security, _security_algo_types),
|
||||
_security_auxiliary_model.get_display_quantity_divisor(security));
|
||||
|
||||
return _studio_algos_source.Select(
|
||||
a => (algo: a, fitted_def: _fit_to_type(a.value, security, _security_algo_types)),
|
||||
@@ -163,8 +166,8 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
class AttachableAlgoProvider : AlgoProvider, IAttachableAlgoProvider {
|
||||
public AttachableAlgoProvider(IEnumerable<SecurityAlgoTypeSetImmutable> algo_sets, IAlgoStudioModel studio_model, DynamicOrderHelper dynamic_helper)
|
||||
: base(algo_sets, studio_model, dynamic_helper) { }
|
||||
public AttachableAlgoProvider(IEnumerable<SecurityAlgoTypeSetImmutable> algo_sets, IAlgoStudioModel studio_model, DynamicOrderHelper dynamic_helper, ISecurityAuxiliaryDataModel security_auxiliary_model)
|
||||
: base(algo_sets, studio_model, dynamic_helper, security_auxiliary_model) { }
|
||||
|
||||
// arbitrarily assign LimitOrderDefinition (this is just a placeholder; the real child is the attach target algo).
|
||||
// Note: although 'None' might seem approrpriate, the server requires a valid definition (perhaps this will change in the future).
|
||||
|
||||
+2
-2
@@ -302,7 +302,7 @@
|
||||
<!--Double-->
|
||||
<usp:AlgoParamTemplateSelector.DoubleTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding param.value, Converter={StaticResource DoubleTextConverter}}"/>
|
||||
<TextBlock Text="{Binding param.display_value, Converter={StaticResource DoubleTextConverter}}"/>
|
||||
</DataTemplate>
|
||||
</usp:AlgoParamTemplateSelector.DoubleTemplate>
|
||||
<!--Enum-->
|
||||
@@ -406,7 +406,7 @@
|
||||
<!--Double-->
|
||||
<usp:AlgoParamTemplateSelector.DoubleTemplate>
|
||||
<DataTemplate>
|
||||
<c:TickUpDownDouble x:Name="tick_up_down" Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
|
||||
<c:TickUpDownDouble x:Name="tick_up_down" Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.display_value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
|
||||
<c:TickUpDownDouble.CommandBindings>
|
||||
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
|
||||
</c:TickUpDownDouble.CommandBindings>
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace bts.shell.manual_fills {
|
||||
readonly Property<bool> _is_dirty;
|
||||
readonly Property<EditableItemState> _editable_item_state;
|
||||
readonly DelegateProperty<bool> _is_valid;
|
||||
readonly DisplayQuantityMultiplier _size = new DisplayQuantityMultiplier();
|
||||
readonly DisplayQuantityMultiplier<uint> _size = DisplayQuantityMultiplier.create_uint();
|
||||
readonly Func<SecurityImmutable?, uint> _get_display_qty_divisor;
|
||||
SecurityImmutable? _security;
|
||||
AccountTuple? _account;
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace mbts.spark {
|
||||
int _depth_level;
|
||||
SecurityImmutable? _security => order_tuple.order.security;
|
||||
// for mini-ob
|
||||
DisplayQuantityMultiplier _prospective_qty_translator_backing;
|
||||
DisplayQuantityMultiplier<uint> _prospective_qty_translator_backing;
|
||||
uint _display_qty_divisor_backing;
|
||||
bool _display_qty_divisor_dirty = true;
|
||||
double _prospective_price;
|
||||
@@ -239,10 +239,9 @@ namespace mbts.spark {
|
||||
IDisplayTranslator<uint> _prospective_qty_translator {
|
||||
get {
|
||||
if (_prospective_qty_translator_backing == null) {
|
||||
_prospective_qty_translator_backing = new DisplayQuantityMultiplier {
|
||||
conversion_data = new QuantityConversionData(_display_qty_divisor, QuantityConversionData.OpType.DivideActual),
|
||||
actual_value = quantity
|
||||
};
|
||||
_prospective_qty_translator_backing = DisplayQuantityMultiplier.create_uint();
|
||||
_prospective_qty_translator_backing.conversion_data = new QuantityConversionData(_display_qty_divisor, QuantityConversionData.OpType.DivideActual);
|
||||
_prospective_qty_translator_backing.actual_value = quantity;
|
||||
_prospective_qty_translator.value_changed += () =>
|
||||
RaisePropertyChanged(nameof(is_qty_dirty));
|
||||
} else {
|
||||
|
||||
@@ -244,7 +244,22 @@ namespace mbts.spark {
|
||||
}),
|
||||
grid_settings_manager,
|
||||
order_mod_vm.algo_vm.connect_data(Observable.Return(model)),
|
||||
order_mod_vm.algo_vm.connect_data(_selected_params_mon.subscribe_spreader_security()),
|
||||
order_mod_vm.algo_vm.connect_data(
|
||||
/// This is not the same security source used to populate <see cref="OrderParamsVM.security_md"/>.
|
||||
/// The latter is always synced with the root of the selected row(s),
|
||||
/// while this source is synced with that row with one exception:
|
||||
/// If the root row's algo has a spreader algo (possibly nested),
|
||||
/// the synthetic security is taken instead of the root security.
|
||||
/// This has the affect of passing the synthetic security to the spreader algo viewmodel,
|
||||
/// which then uses the security to populate the synthetic params ui.
|
||||
/// TODO (maybe): clean this design up; the different security sources make it precarious.
|
||||
/// One idea is to pass a special data type to the algo vm (which contains the spreader
|
||||
/// security and execution strategy), which is only observed by the spreader algo vm.
|
||||
_selected_params_mon.subscribe_security().StartWith((SecurityImmutable?)null).CombineLatest(
|
||||
_selected_params_mon.subscribe_spreader_security().StartWith((SecurityImmutable?)null),
|
||||
(root_sec, spreader_sec) => (root_sec, spreader_sec))
|
||||
.Select(tup => tup.spreader_sec ?? tup.root_sec)
|
||||
.OnDispatcher()),
|
||||
order_mod_vm,
|
||||
attach_vm,
|
||||
_show_fills.observe_current(true)
|
||||
@@ -365,7 +380,7 @@ namespace mbts.spark {
|
||||
modify_selected_orders_async_cmd.RaiseCanExecuteChanged();
|
||||
revert_selected_orders_cmd.RaiseCanExecuteChanged();
|
||||
}),
|
||||
_selected_params_mon.subscribe_security().OnDispatcher().to_security_md(bus).OnDispatcher().Subscribe(newval => order_mod_vm.security_md = newval),
|
||||
_selected_params_mon.subscribe_security().to_security_md(bus).OnDispatcher().Subscribe(newval => order_mod_vm.security_md = newval),
|
||||
connect_common_selected_params(order_mod_vm));
|
||||
|
||||
IDisposable connect_common_selected_params(OrderParamsVM vm) =>
|
||||
@@ -878,6 +893,7 @@ namespace mbts.spark {
|
||||
public ReadOnlyObservableCollection<AccountTuple> accounts => _accounts.items;
|
||||
public AccountTuple? selected_account { get => _accounts.selected_item; set => _accounts.selected_item = value; }
|
||||
public SecurityImmutable? security => _owner._selected_params_mon.security;
|
||||
/// <see cref="security_md"/> should be synced with the root of the selected row(s),
|
||||
public SecurityMD? security_md {
|
||||
get => _security_md;
|
||||
set {
|
||||
|
||||
@@ -1,16 +1,82 @@
|
||||
using bts.api.orderman;
|
||||
using mbts.ui;
|
||||
using System;
|
||||
using System.Reactive;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
namespace mbts.spark {
|
||||
static partial class OrderTypeVMFactory {
|
||||
class IcebergOrderVM : OrderTypeVMBase<IcebergOrderDefinition, IcebergOrderDefinitionImmutable> {
|
||||
readonly Property<double> _show_quantity;
|
||||
readonly DisplayQuantityMultiplier<double> _display_qty_translator = DisplayQuantityMultiplier.create_double();
|
||||
new IDisposable _dispose;
|
||||
|
||||
public IcebergOrderVM(AlgoKey key, string display_name, AlgoEditingPermissions editing_permissions)
|
||||
: base(key, display_name, editing_permissions) {
|
||||
_show_quantity = add_property(nameof(show_quantity), IcebergOrderDefinition.ShowQuantityFieldNumber, double.NaN, double.NaN);
|
||||
_show_quantity = add_property(
|
||||
"show_quantity",
|
||||
IcebergOrderDefinition.ShowQuantityFieldNumber,
|
||||
double.NaN,
|
||||
double.NaN,
|
||||
to_display_value: v => _qty_conversion.to_display(v),
|
||||
from_display_value: v => _qty_conversion.from_display(v),
|
||||
refresh_display_value:
|
||||
Observable.Create<Unit>(o => {
|
||||
_request_refresh_display += refresh;
|
||||
return Disposable.Create(() => _request_refresh_display -= refresh);
|
||||
void refresh() =>
|
||||
o.OnNext(Unit.Default);
|
||||
}));
|
||||
_show_quantity.Changed += on_show_quantity_changed;
|
||||
_dispose = Disposable.Create(
|
||||
_display_qty_translator.connect_owner(this, nameof(display_show_quantity)),
|
||||
_display_qty_translator.subscribe_actual_value().Subscribe(v => _show_quantity.Value = v),
|
||||
Disposable.Create(() => _show_quantity.Changed -= on_show_quantity_changed));
|
||||
|
||||
void on_show_quantity_changed(double oldval, double newval) =>
|
||||
_display_qty_translator.actual_value = newval;
|
||||
}
|
||||
|
||||
public double show_quantity { get => _show_quantity.Value; set => _show_quantity.set(value); }
|
||||
event Action _request_refresh_display;
|
||||
|
||||
public double display_show_quantity {
|
||||
get => _display_qty_translator.display_value;
|
||||
set => _display_qty_translator.display_value = value;
|
||||
}
|
||||
|
||||
QuantityConversionData _qty_conversion {
|
||||
get => _display_qty_translator.conversion_data;
|
||||
set {
|
||||
if (_display_qty_translator.conversion_data != value) {
|
||||
_display_qty_translator.conversion_data = value;
|
||||
_request_refresh_display?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void on_model_internal() =>
|
||||
_refresh_display();
|
||||
|
||||
protected override void on_security_internal() =>
|
||||
_refresh_display();
|
||||
|
||||
void _refresh_display() {
|
||||
if (model == null) {
|
||||
_qty_conversion = default;
|
||||
} else {
|
||||
var divisor = model.security_auxiliary_model.get_display_quantity_divisor(security);
|
||||
if (divisor == 1 || divisor == 0) {
|
||||
_qty_conversion = default;
|
||||
} else {
|
||||
_qty_conversion = new QuantityConversionData(divisor, QuantityConversionData.OpType.DivideActual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing) {
|
||||
Disposable.dispose(ref _dispose);
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Reactive;
|
||||
using System.Reactive.Disposables;
|
||||
using System.Reactive.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
@@ -402,9 +403,19 @@ namespace mbts.spark {
|
||||
#endregion
|
||||
|
||||
#region Auto-Properties (non-child-algo params)
|
||||
protected Property<T> add_property<T>(string prop_name, int target_field_number, T init_value = default, T non_uniform_value = default, bool validate_on_changed = false, AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite) {
|
||||
protected Property<T> add_property<T>(
|
||||
string prop_name,
|
||||
int target_field_number,
|
||||
T init_value = default,
|
||||
T non_uniform_value = default,
|
||||
bool validate_on_changed = false,
|
||||
AlgoParamPermissions permissions = AlgoParamPermissions.ReadWrite,
|
||||
Func<T, T> to_display_value = null,
|
||||
Func<T, T> from_display_value = null,
|
||||
IObservable<Unit> refresh_display_value = null) {
|
||||
//
|
||||
var prop = backing(prop_name, init_value);
|
||||
_on_property_added(prop_name, target_field_number, prop, prop.set, non_uniform_value, validate_on_changed, permissions);
|
||||
_on_property_added(prop_name, target_field_number, prop, prop.set, non_uniform_value, validate_on_changed, permissions, to_display_value, from_display_value, refresh_display_value);
|
||||
return prop;
|
||||
}
|
||||
|
||||
@@ -579,17 +590,25 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
void _on_property_added<T>(string prop_name, int target_field_number, IProperty<T> prop, Action<T> assign_to_prop, T non_uniform_value, bool validate_on_changed, AlgoParamPermissions permissions) {
|
||||
void _on_property_added<T>(string prop_name, int target_field_number, IProperty<T> prop, Action<T> assign_to_prop, T non_uniform_value, bool validate_on_changed, AlgoParamPermissions permissions, Func<T, T> to_display_value = null, Func<T, T> from_display_value = null, IObservable<Unit> refresh_display_value = null) {
|
||||
var field_desc = new TMutable().Descriptor.FindFieldByNumber(target_field_number);
|
||||
var param = new AlgoParamVM<T>(prop_name, DisplayNameHelper.snake_to_title_case(prop_name), prop.Value) { permissions = permissions };
|
||||
var param = new AlgoParamVM<T>(prop_name, DisplayNameHelper.snake_to_title_case(prop_name), prop.Value, EqualityComparer<T>.Default, null, to_display_value, from_display_value) {
|
||||
permissions = permissions
|
||||
};
|
||||
_algo_params.Add(param);
|
||||
var prop_hook = _link_props(param, prop, assign_to_prop, non_uniform_value, validate_on_changed);
|
||||
if (refresh_display_value != null) {
|
||||
prop_hook = Disposable.Create(
|
||||
refresh_display_value.Subscribe(_ => param.refresh_display_value()),
|
||||
prop_hook);
|
||||
}
|
||||
_auto_props.Add(new AutoProp(prop_name, mval => {
|
||||
if (mval != null && param.has_value)
|
||||
field_desc.Accessor.SetValue(mval, prop.Value);
|
||||
},
|
||||
mval => assign_to_prop_and_commit(mval == null ? default : ConvertUtils.try_convert<T>(field_desc.Accessor.GetValue(mval))),
|
||||
mvals => assign_to_prop_and_commit(mvals.Select(v => ConvertUtils.try_convert<T>(field_desc.Accessor.GetValue(v))).Distinct().SafeSingleOrDefault(non_uniform_value)),
|
||||
_link_props(param, prop, assign_to_prop, non_uniform_value, validate_on_changed)));
|
||||
prop_hook));
|
||||
void assign_to_prop_and_commit(T value) {
|
||||
assign_to_prop(value);
|
||||
param.mark_committed();
|
||||
|
||||
@@ -947,7 +947,7 @@
|
||||
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center">Show Quantity</TextBlock>
|
||||
<c:HelpButton Margin="{StaticResource Margin.LP1}" ToolTip="Maximum size to show on exchange, this is the BTS provided iceberg"/>
|
||||
</StackPanel>
|
||||
<c:TickUpDownDouble x:Name="PART_ShowQuantity_TickUpDownDouble" Grid.Column="2" HorizontalAlignment="Stretch" Value="{Binding show_quantity}"/>
|
||||
<c:TickUpDownDouble x:Name="PART_ShowQuantity_TickUpDownDouble" Grid.Column="2" HorizontalAlignment="Stretch" Value="{Binding display_show_quantity}"/>
|
||||
<uag:AlgoPickerNodeView
|
||||
x:Name="PART_Child_AlgoPickerNodeView"
|
||||
Grid.Row="2"
|
||||
|
||||
@@ -40,25 +40,53 @@ namespace mbts.ui {
|
||||
public static bool operator !=(QuantityConversionData x, QuantityConversionData y) => !x.Equals(y);
|
||||
}
|
||||
|
||||
class DisplayQuantityMultiplier : IDisplayTranslator<uint> {
|
||||
uint _actual_value;
|
||||
static class DisplayQuantityMultiplier {
|
||||
public static DisplayQuantityMultiplier<uint> create_uint() =>
|
||||
new DisplayQuantityMultiplier<uint>(
|
||||
DisplayTranslatorExtensions.to_display,
|
||||
DisplayTranslatorExtensions.from_display);
|
||||
|
||||
public static DisplayQuantityMultiplier<double> create_double() =>
|
||||
new DisplayQuantityMultiplier<double>(
|
||||
DisplayTranslatorExtensions.to_display,
|
||||
DisplayTranslatorExtensions.from_display);
|
||||
}
|
||||
|
||||
class DisplayQuantityMultiplier<T> : IDisplayTranslator<T> {
|
||||
readonly Func<QuantityConversionData, T, T> _to_display, _from_display;
|
||||
readonly IEqualityComparer<T> _comparer;
|
||||
T _actual_value;
|
||||
QuantityConversionData _conversion_data;
|
||||
|
||||
public DisplayQuantityMultiplier(
|
||||
Func<QuantityConversionData, T, T> to_display,
|
||||
Func<QuantityConversionData, T, T> from_display,
|
||||
IEqualityComparer<T> comparer = null,
|
||||
T actual_value = default,
|
||||
QuantityConversionData conversion_data = default) {
|
||||
//
|
||||
_to_display = to_display ?? throw new ArgumentNullException(nameof(to_display));
|
||||
_from_display = from_display ?? throw new ArgumentNullException(nameof(from_display));
|
||||
_comparer = comparer ?? EqualityComparer<T>.Default;
|
||||
_actual_value = actual_value;
|
||||
_conversion_data = conversion_data;
|
||||
}
|
||||
|
||||
public event Action value_changed;
|
||||
|
||||
public uint actual_value {
|
||||
public T actual_value {
|
||||
get => _actual_value;
|
||||
set {
|
||||
if (_actual_value != value) {
|
||||
if (!_comparer.Equals(_actual_value, value)) {
|
||||
_actual_value = value;
|
||||
value_changed?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public uint display_value {
|
||||
get => _conversion_data.to_display(_actual_value);
|
||||
set => actual_value = _conversion_data.from_display(value);
|
||||
public T display_value {
|
||||
get => _to_display(_conversion_data, _actual_value);
|
||||
set => actual_value = _from_display(_conversion_data, value);
|
||||
}
|
||||
|
||||
public QuantityConversionData conversion_data {
|
||||
@@ -168,5 +196,13 @@ namespace mbts.ui {
|
||||
default: return display_value;
|
||||
}
|
||||
}
|
||||
|
||||
public static double from_display(this QuantityConversionData quantity_conversion_data, double display_value) {
|
||||
switch (quantity_conversion_data.op) {
|
||||
case QuantityConversionData.OpType.DivideActual: return display_value * quantity_conversion_data.value;
|
||||
case QuantityConversionData.OpType.MultiplyActual: return display_value.safe_divide(quantity_conversion_data.value);
|
||||
default: return display_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user