Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspector: Add new AdvancedSlider control #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.ComponentModel;
using Gemini.Modules.Inspector.Inspectors;

namespace Gemini.Modules.Inspector.Conventions
{
public class AdvancedSliderPropertyEditorBuilder : PropertyEditorBuilder
{
public override bool IsApplicable(PropertyDescriptor propertyDescriptor)
{
var isNumberType = propertyDescriptor.PropertyType == typeof(int)
|| propertyDescriptor.PropertyType == typeof(double)
|| propertyDescriptor.PropertyType == typeof(float);

return isNumberType;
}

public override IEditor BuildEditor(PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.PropertyType == typeof(int))
{
return new AdvancedSliderEditorViewModel<int>() {
Speed = 1
};
}

if (propertyDescriptor.PropertyType == typeof(double))
{
return new AdvancedSliderEditorViewModel<double>() {
Speed = 0.1
};
}

if (propertyDescriptor.PropertyType == typeof(float))
{
return new AdvancedSliderEditorViewModel<float>() {
Speed = 0.1f
};
}

throw new InvalidOperationException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Gemini.Modules.Inspector.Inspectors;

namespace Gemini.Modules.Inspector.Conventions
{
public class AdvancedSliderRangePropertyEditorBuilder : PropertyEditorBuilder
{
public override bool IsApplicable(PropertyDescriptor propertyDescriptor)
{
var isNumberType = propertyDescriptor.PropertyType == typeof(int)
|| propertyDescriptor.PropertyType == typeof(double)
|| propertyDescriptor.PropertyType == typeof(float);

if (!isNumberType)
return false;

return propertyDescriptor.Attributes.Cast<Attribute>().Any(x => x is RangeAttribute);
}

public override IEditor BuildEditor(PropertyDescriptor propertyDescriptor)
{
var rangeAttribute = propertyDescriptor.Attributes
.Cast<Attribute>().OfType<RangeAttribute>()
.First();

if (propertyDescriptor.PropertyType == typeof(int))
{
return new AdvancedSliderEditorViewModel<int>((int) rangeAttribute.Minimum, (int) rangeAttribute.Maximum) {
Speed = 1
};
}

if (propertyDescriptor.PropertyType == typeof(double))
{
return new AdvancedSliderEditorViewModel<double>((double) rangeAttribute.Minimum, (double) rangeAttribute.Maximum) {
Speed = 0.1
};
}

if (propertyDescriptor.PropertyType == typeof(float))
{
return new AdvancedSliderEditorViewModel<float>((float) rangeAttribute.Minimum, (float) rangeAttribute.Maximum) {
Speed = 0.1f
};
}

throw new InvalidOperationException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ static DefaultPropertyInspectors()
{
_inspectorBuilders = new List<PropertyEditorBuilder>
{
new RangePropertyEditorBuilder(),
new AdvancedSliderPropertyEditorBuilder(),
new AdvancedSliderRangePropertyEditorBuilder(),
new EnumPropertyEditorBuilder(),

new StandardPropertyEditorBuilder<bool, CheckBoxEditorViewModel>(),
new StandardPropertyEditorBuilder<Color, ColorEditorViewModel>(),
new StandardPropertyEditorBuilder<double, TextBoxEditorViewModel<double>>(),
new StandardPropertyEditorBuilder<float, TextBoxEditorViewModel<float>>(),
new StandardPropertyEditorBuilder<int, TextBoxEditorViewModel<int>>(),
new StandardPropertyEditorBuilder<double?, TextBoxEditorViewModel<double?>>(),
new StandardPropertyEditorBuilder<float?, TextBoxEditorViewModel<float?>>(),
new StandardPropertyEditorBuilder<int?, TextBoxEditorViewModel<int?>>(),
Expand Down
10 changes: 10 additions & 0 deletions src/Gemini.Modules.Inspector/Gemini.Modules.Inspector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,20 @@
<Compile Include="Controls\NumericTextBox.cs" />
<Compile Include="Controls\ScreenColorPicker.cs" />
<Compile Include="Controls\SimpleGridSplitter.cs" />
<Compile Include="Conventions\AdvancedSliderPropertyEditorBuilder.cs" />
<Compile Include="Conventions\DefaultPropertyInspectors.cs" />
<Compile Include="Conventions\EnumPropertyEditorBuilder.cs" />
<Compile Include="Conventions\PropertyEditorBuilder.cs" />
<Compile Include="Conventions\AdvancedSliderRangePropertyEditorBuilder.cs" />
<Compile Include="Conventions\RangePropertyEditorBuilder.cs" />
<Compile Include="Conventions\StandardPropertyEditorBuilder.cs" />
<Compile Include="InspectableObject.cs" />
<Compile Include="InspectableObjectBuilder.cs" />
<Compile Include="InspectorBuilder.cs" />
<Compile Include="Inspectors\AdvancedSliderEditorView.xaml.cs">
<DependentUpon>AdvancedSliderEditorView.xaml</DependentUpon>
</Compile>
<Compile Include="Inspectors\AdvancedSliderEditorViewModel.cs" />
<Compile Include="Inspectors\BitmapSourceEditorView.xaml.cs">
<DependentUpon>BitmapSourceEditorView.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -183,6 +189,10 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Page Include="Inspectors\AdvancedSliderEditorView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Inspectors\BitmapSourceEditorView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cal="http://www.caliburnproject.org"
xmlns:local="clr-namespace:Gemini.Modules.Inspector.Inspectors"
xmlns:Controls="clr-namespace:Gemini.Framework.Controls;assembly=Gemini"
x:Class="Gemini.Modules.Inspector.Inspectors.AdvancedSliderEditorView"
mc:Ignorable="d" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Controls:AdvancedSlider x:Name="slider" Grid.Column="0" Grid.RowSpan="2"
BorderThickness="1"
Value="{Binding Path=Value, Mode=TwoWay}"
Type="{Binding Path=Type}"
ValueType="{Binding Path=ValueType}"
ValueMax="{Binding Path=Maximum}"
ValueMin="{Binding Path=Minimum}"
Speed="{Binding Path=Speed}"
ValueFormat="{Binding Path=ValueFormat}"
MouseCaptured="{Binding Path=MouseCaptured, Mode=OneWayToSource}"
/>

<Controls:RepeatingButton Grid.Column="1" Grid.Row="0" cal:Message.Attach="Up" BorderBrush="Transparent" Padding="0">
<Path Fill="{DynamicResource EnvironmentToolWindowText}" Data="M 0 6 L 12 6 L 6 0 Z"/>
</Controls:RepeatingButton>
<Controls:RepeatingButton Grid.Column="1" Grid.Row="1" cal:Message.Attach="Down" BorderBrush="Transparent" Padding="0">
<Path Fill="{DynamicResource EnvironmentToolWindowText}" Data="M 0 0 L 6 6 L 12 0 Z"/>
</Controls:RepeatingButton>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Windows.Controls;

namespace Gemini.Modules.Inspector.Inspectors
{
/// <summary>
/// Interaction logic for AdvancedSliderEditorView.xaml
/// </summary>
public partial class AdvancedSliderEditorView : UserControl
{
public AdvancedSliderEditorView()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System;

using Caliburn.Micro;

using Gemini.Framework.Controls;
using Gemini.Framework.Util;

namespace Gemini.Modules.Inspector.Inspectors
{
public class AdvancedSliderEditorViewModel<TValue> : SelectiveUndoEditorBase<TValue>, ILabelledInspector, IViewAware where TValue : IComparable<TValue>
{
private static readonly string _defaultValueFormat = "{0:0.#####}";

public AdvancedSliderEditorViewModel()
{
_valueFormat = _defaultValueFormat;
_valueType = typeof(TValue);
_type = AdvancedSliderBase.DisplayType.Number;
}

public AdvancedSliderEditorViewModel(TValue min, TValue max)
{
_minimum = min;
_maximum = max;
_valueFormat = _defaultValueFormat;
_valueType = typeof(TValue);
_type = AdvancedSliderBase.DisplayType.Bar;
}

private TValue _minimum;

public TValue Minimum
{
get { return _minimum; }
set
{
_minimum = value;
NotifyOfPropertyChange(() => Minimum);
}
}

private TValue _maximum;

public TValue Maximum
{
get { return _maximum; }
set
{
_maximum = value;
NotifyOfPropertyChange(() => Maximum);
}
}

private TValue _speed;

public TValue Speed
{
get { return _speed; }
set
{
_speed = value;
NotifyOfPropertyChange(() => Speed);
}
}

private bool _mouseCaptured;

public bool MouseCaptured
{
get { return _mouseCaptured; }
set
{
if (_mouseCaptured == value)
return;

_mouseCaptured = value;

if (value)
OnBeginEdit();
else
OnEndEdit();
}
}

private string _valueFormat;

public string ValueFormat
{
get { return _valueFormat; }
set
{
_valueFormat = value;
NotifyOfPropertyChange(() => ValueFormat);
}
}

private Type _valueType;

public Type ValueType
{
get { return _valueType; }
set
{
_valueType = value;
NotifyOfPropertyChange(() => ValueType);
}
}

private AdvancedSliderBase.DisplayType _type;

public AdvancedSliderBase.DisplayType Type
{
get { return _type; }

set
{
if (Equals(_type, value))
return;

_type = value;

NotifyOfPropertyChange(() => Type);
}
}

public void Up()
{
_view.slider.ApplyValueChange(SpeedMultiplier.Get());
}

public void Down()
{
_view.slider.ApplyValueChange(-SpeedMultiplier.Get());
}

private AdvancedSliderEditorView _view;

public event EventHandler<ViewAttachedEventArgs> ViewAttached;

public void AttachView(object view, object context = null)
{
_view = (AdvancedSliderEditorView) view;
ViewAttached?.Invoke(this, new ViewAttachedEventArgs() { View = view, Context = context });
}

public object GetView(object context = null)
{
return _view;
}
}
}
Loading