├── .gitignore
├── App.config
├── App.xaml
├── App.xaml.cs
├── ControlValue.cs
├── Controls
├── CurveEditor.cs
└── CurveEditor.xaml
├── CurveEditor.csproj
├── CurveEditor.sln
├── CurveLibs
└── CatmullRomSpline.cs
├── CurveType.cs
├── Extensions
├── ArrayExtension.cs
└── VectorExtension.cs
├── LICENSE
├── MainWindow.xaml
├── MainWindow.xaml.cs
├── MainWindowViewModel.cs
├── Properties
├── AssemblyInfo.cs
├── Resources.Designer.cs
├── Resources.resx
├── Settings.Designer.cs
└── Settings.settings
├── README.md
├── Windows
├── CurveEditorWindow.xaml
└── CurveEditorWindow.xaml.cs
└── packages.config
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Ww][Ii][Nn]32/
27 | [Aa][Rr][Mm]/
28 | [Aa][Rr][Mm]64/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Ll]og/
33 | [Ll]ogs/
34 |
35 | # Visual Studio 2015/2017 cache/options directory
36 | .vs/
37 | # Uncomment if you have tasks that create the project's static files in wwwroot
38 | #wwwroot/
39 |
40 | # Visual Studio 2017 auto generated files
41 | Generated\ Files/
42 |
43 | # MSTest test Results
44 | [Tt]est[Rr]esult*/
45 | [Bb]uild[Ll]og.*
46 |
47 | # NUnit
48 | *.VisualState.xml
49 | TestResult.xml
50 | nunit-*.xml
51 |
52 | # Build Results of an ATL Project
53 | [Dd]ebugPS/
54 | [Rr]eleasePS/
55 | dlldata.c
56 |
57 | # Benchmark Results
58 | BenchmarkDotNet.Artifacts/
59 |
60 | # .NET Core
61 | project.lock.json
62 | project.fragment.lock.json
63 | artifacts/
64 |
65 | # ASP.NET Scaffolding
66 | ScaffoldingReadMe.txt
67 |
68 | # StyleCop
69 | StyleCopReport.xml
70 |
71 | # Files built by Visual Studio
72 | *_i.c
73 | *_p.c
74 | *_h.h
75 | *.ilk
76 | *.meta
77 | *.obj
78 | *.iobj
79 | *.pch
80 | *.pdb
81 | *.ipdb
82 | *.pgc
83 | *.pgd
84 | *.rsp
85 | *.sbr
86 | *.tlb
87 | *.tli
88 | *.tlh
89 | *.tmp
90 | *.tmp_proj
91 | *_wpftmp.csproj
92 | *.log
93 | *.vspscc
94 | *.vssscc
95 | .builds
96 | *.pidb
97 | *.svclog
98 | *.scc
99 |
100 | # Chutzpah Test files
101 | _Chutzpah*
102 |
103 | # Visual C++ cache files
104 | ipch/
105 | *.aps
106 | *.ncb
107 | *.opendb
108 | *.opensdf
109 | *.sdf
110 | *.cachefile
111 | *.VC.db
112 | *.VC.VC.opendb
113 |
114 | # Visual Studio profiler
115 | *.psess
116 | *.vsp
117 | *.vspx
118 | *.sap
119 |
120 | # Visual Studio Trace Files
121 | *.e2e
122 |
123 | # TFS 2012 Local Workspace
124 | $tf/
125 |
126 | # Guidance Automation Toolkit
127 | *.gpState
128 |
129 | # ReSharper is a .NET coding add-in
130 | _ReSharper*/
131 | *.[Rr]e[Ss]harper
132 | *.DotSettings.user
133 |
134 | # TeamCity is a build add-in
135 | _TeamCity*
136 |
137 | # DotCover is a Code Coverage Tool
138 | *.dotCover
139 |
140 | # AxoCover is a Code Coverage Tool
141 | .axoCover/*
142 | !.axoCover/settings.json
143 |
144 | # Coverlet is a free, cross platform Code Coverage Tool
145 | coverage*.json
146 | coverage*.xml
147 | coverage*.info
148 |
149 | # Visual Studio code coverage results
150 | *.coverage
151 | *.coveragexml
152 |
153 | # NCrunch
154 | _NCrunch_*
155 | .*crunch*.local.xml
156 | nCrunchTemp_*
157 |
158 | # MightyMoose
159 | *.mm.*
160 | AutoTest.Net/
161 |
162 | # Web workbench (sass)
163 | .sass-cache/
164 |
165 | # Installshield output folder
166 | [Ee]xpress/
167 |
168 | # DocProject is a documentation generator add-in
169 | DocProject/buildhelp/
170 | DocProject/Help/*.HxT
171 | DocProject/Help/*.HxC
172 | DocProject/Help/*.hhc
173 | DocProject/Help/*.hhk
174 | DocProject/Help/*.hhp
175 | DocProject/Help/Html2
176 | DocProject/Help/html
177 |
178 | # Click-Once directory
179 | publish/
180 |
181 | # Publish Web Output
182 | *.[Pp]ublish.xml
183 | *.azurePubxml
184 | # Note: Comment the next line if you want to checkin your web deploy settings,
185 | # but database connection strings (with potential passwords) will be unencrypted
186 | *.pubxml
187 | *.publishproj
188 |
189 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
190 | # checkin your Azure Web App publish settings, but sensitive information contained
191 | # in these scripts will be unencrypted
192 | PublishScripts/
193 |
194 | # NuGet Packages
195 | *.nupkg
196 | # NuGet Symbol Packages
197 | *.snupkg
198 | # The packages folder can be ignored because of Package Restore
199 | **/[Pp]ackages/*
200 | # except build/, which is used as an MSBuild target.
201 | !**/[Pp]ackages/build/
202 | # Uncomment if necessary however generally it will be regenerated when needed
203 | #!**/[Pp]ackages/repositories.config
204 | # NuGet v3's project.json files produces more ignorable files
205 | *.nuget.props
206 | *.nuget.targets
207 |
208 | # Microsoft Azure Build Output
209 | csx/
210 | *.build.csdef
211 |
212 | # Microsoft Azure Emulator
213 | ecf/
214 | rcf/
215 |
216 | # Windows Store app package directories and files
217 | AppPackages/
218 | BundleArtifacts/
219 | Package.StoreAssociation.xml
220 | _pkginfo.txt
221 | *.appx
222 | *.appxbundle
223 | *.appxupload
224 |
225 | # Visual Studio cache files
226 | # files ending in .cache can be ignored
227 | *.[Cc]ache
228 | # but keep track of directories ending in .cache
229 | !?*.[Cc]ache/
230 |
231 | # Others
232 | ClientBin/
233 | ~$*
234 | *~
235 | *.dbmdl
236 | *.dbproj.schemaview
237 | *.jfm
238 | *.pfx
239 | *.publishsettings
240 | orleans.codegen.cs
241 |
242 | # Including strong name files can present a security risk
243 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
244 | #*.snk
245 |
246 | # Since there are multiple workflows, uncomment next line to ignore bower_components
247 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
248 | #bower_components/
249 |
250 | # RIA/Silverlight projects
251 | Generated_Code/
252 |
253 | # Backup & report files from converting an old project file
254 | # to a newer Visual Studio version. Backup files are not needed,
255 | # because we have git ;-)
256 | _UpgradeReport_Files/
257 | Backup*/
258 | UpgradeLog*.XML
259 | UpgradeLog*.htm
260 | ServiceFabricBackup/
261 | *.rptproj.bak
262 |
263 | # SQL Server files
264 | *.mdf
265 | *.ldf
266 | *.ndf
267 |
268 | # Business Intelligence projects
269 | *.rdl.data
270 | *.bim.layout
271 | *.bim_*.settings
272 | *.rptproj.rsuser
273 | *- [Bb]ackup.rdl
274 | *- [Bb]ackup ([0-9]).rdl
275 | *- [Bb]ackup ([0-9][0-9]).rdl
276 |
277 | # Microsoft Fakes
278 | FakesAssemblies/
279 |
280 | # GhostDoc plugin setting file
281 | *.GhostDoc.xml
282 |
283 | # Node.js Tools for Visual Studio
284 | .ntvs_analysis.dat
285 | node_modules/
286 |
287 | # Visual Studio 6 build log
288 | *.plg
289 |
290 | # Visual Studio 6 workspace options file
291 | *.opt
292 |
293 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
294 | *.vbw
295 |
296 | # Visual Studio LightSwitch build output
297 | **/*.HTMLClient/GeneratedArtifacts
298 | **/*.DesktopClient/GeneratedArtifacts
299 | **/*.DesktopClient/ModelManifest.xml
300 | **/*.Server/GeneratedArtifacts
301 | **/*.Server/ModelManifest.xml
302 | _Pvt_Extensions
303 |
304 | # Paket dependency manager
305 | .paket/paket.exe
306 | paket-files/
307 |
308 | # FAKE - F# Make
309 | .fake/
310 |
311 | # CodeRush personal settings
312 | .cr/personal
313 |
314 | # Python Tools for Visual Studio (PTVS)
315 | __pycache__/
316 | *.pyc
317 |
318 | # Cake - Uncomment if you are using it
319 | # tools/**
320 | # !tools/packages.config
321 |
322 | # Tabs Studio
323 | *.tss
324 |
325 | # Telerik's JustMock configuration file
326 | *.jmconfig
327 |
328 | # BizTalk build output
329 | *.btp.cs
330 | *.btm.cs
331 | *.odx.cs
332 | *.xsd.cs
333 |
334 | # OpenCover UI analysis results
335 | OpenCover/
336 |
337 | # Azure Stream Analytics local run output
338 | ASALocalRun/
339 |
340 | # MSBuild Binary and Structured Log
341 | *.binlog
342 |
343 | # NVidia Nsight GPU debugger configuration file
344 | *.nvuser
345 |
346 | # MFractors (Xamarin productivity tool) working folder
347 | .mfractor/
348 |
349 | # Local History for Visual Studio
350 | .localhistory/
351 |
352 | # BeatPulse healthcheck temp database
353 | healthchecksdb
354 |
355 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
356 | MigrationBackup/
357 |
358 | # Ionide (cross platform F# VS Code tools) working folder
359 | .ionide/
360 |
361 | # Fody - auto-generated XML schema
362 | FodyWeavers.xsd
--------------------------------------------------------------------------------
/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace CurveEditor
10 | {
11 | ///
12 | /// App.xaml の相互作用ロジック
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/ControlValue.cs:
--------------------------------------------------------------------------------
1 | using Livet;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace CurveEditor
9 | {
10 | public class ControlValue : NotificationObject
11 | {
12 | public float Value
13 | {
14 | get => _Value;
15 | set => RaisePropertyChangedIfSet(ref _Value, value);
16 | }
17 | float _Value;
18 |
19 | public float RangeValue
20 | {
21 | get => _RangedValue;
22 | set => RaisePropertyChangedIfSet(ref _RangedValue, value);
23 | }
24 | float _RangedValue;
25 |
26 | public float NormalizedTime
27 | {
28 | get => _NormalizedTime;
29 | set => RaisePropertyChangedIfSet(ref _NormalizedTime, value);
30 | }
31 | float _NormalizedTime;
32 |
33 | public ControlValue(float value, float nTime)
34 | {
35 | Value = value;
36 | RangeValue = value;
37 | NormalizedTime = nTime;
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Controls/CurveEditor.cs:
--------------------------------------------------------------------------------
1 | using CurveEditor.CurveLibs;
2 | using CurveEditor.Extensions;
3 | using CurveEditor.Windows;
4 | using System;
5 | using System.Collections;
6 | using System.Collections.Generic;
7 | using System.Collections.Specialized;
8 | using System.Globalization;
9 | using System.Linq;
10 | using System.Text;
11 | using System.Threading.Tasks;
12 | using System.Windows;
13 | using System.Windows.Controls;
14 | using System.Windows.Controls.Primitives;
15 | using System.Windows.Data;
16 | using System.Windows.Documents;
17 | using System.Windows.Input;
18 | using System.Windows.Media;
19 | using System.Windows.Media.Imaging;
20 | using System.Windows.Navigation;
21 | using System.Windows.Shapes;
22 |
23 | namespace CurveEditor.Controls
24 | {
25 | public class CurveEditor : Selector
26 | {
27 | public CurveType Type
28 | {
29 | get => (CurveType)GetValue(CurveTypeProperty);
30 | set => SetValue(CurveTypeProperty, value);
31 | }
32 | public static readonly DependencyProperty CurveTypeProperty =
33 | DependencyProperty.Register(nameof(Type), typeof(CurveType), typeof(CurveEditor), new FrameworkPropertyMetadata(CurveType.Linear, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
34 |
35 | public bool IsReadOnlyType
36 | {
37 | get => (bool)GetValue(IsReadOnlyTypeProperty);
38 | set => SetValue(IsReadOnlyTypeProperty, value);
39 | }
40 | public static readonly DependencyProperty IsReadOnlyTypeProperty =
41 | DependencyProperty.Register(nameof(IsReadOnlyType), typeof(bool), typeof(CurveEditor), new FrameworkPropertyMetadata(false));
42 |
43 | public bool IsClampEnabled
44 | {
45 | get => (bool)GetValue(IsClampEnabledProperty);
46 | set => SetValue(IsClampEnabledProperty, value);
47 | }
48 | public static readonly DependencyProperty IsClampEnabledProperty =
49 | DependencyProperty.Register(nameof(IsClampEnabled), typeof(bool), typeof(CurveEditor), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
50 |
51 | public bool IsReadOnlyClampFlag
52 | {
53 | get => (bool)GetValue(IsReadOnlyClampFlagProperty);
54 | set => SetValue(IsReadOnlyClampFlagProperty, value);
55 | }
56 | public static readonly DependencyProperty IsReadOnlyClampFlagProperty =
57 | DependencyProperty.Register(nameof(IsReadOnlyClampFlag), typeof(bool), typeof(CurveEditor), new FrameworkPropertyMetadata(false));
58 |
59 | public bool IsRangeEnabled
60 | {
61 | get => (bool)GetValue(IsRangeEnabledProperty);
62 | set => SetValue(IsRangeEnabledProperty, value);
63 | }
64 | public static readonly DependencyProperty IsRangeEnabledProperty =
65 | DependencyProperty.Register(nameof(IsRangeEnabled), typeof(bool), typeof(CurveEditor), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
66 |
67 | public bool IsReadOnlyRangeFlag
68 | {
69 | get => (bool)GetValue(IsReadOnlyRangeFlagProperty);
70 | set => SetValue(IsReadOnlyRangeFlagProperty, value);
71 | }
72 | public static readonly DependencyProperty IsReadOnlyRangeFlagProperty =
73 | DependencyProperty.Register(nameof(IsReadOnlyRangeFlag), typeof(bool), typeof(CurveEditor), new FrameworkPropertyMetadata(false));
74 |
75 | public float MaxValue
76 | {
77 | get => (float)GetValue(MaxValueProperty);
78 | set => SetValue(MaxValueProperty, value);
79 | }
80 | public static readonly DependencyProperty MaxValueProperty =
81 | DependencyProperty.Register(nameof(MaxValue), typeof(float), typeof(CurveEditor), new PropertyMetadata(1.0f));
82 |
83 | public float MinValue
84 | {
85 | get => (float)GetValue(MinValueProperty);
86 | set => SetValue(MinValueProperty, value);
87 | }
88 | public static readonly DependencyProperty MinValueProperty =
89 | DependencyProperty.Register(nameof(MinValue), typeof(float), typeof(CurveEditor), new PropertyMetadata(0.0f));
90 |
91 | bool IsOpenedCurveEditor
92 | {
93 | get => (bool)GetValue(IsOpenedCurveEditorProperty);
94 | set => SetValue(IsOpenedCurveEditorProperty, value);
95 | }
96 | public static readonly DependencyProperty IsOpenedCurveEditorProperty = DependencyProperty.Register(
97 | nameof(IsOpenedCurveEditor), typeof(bool), typeof(CurveEditor), new FrameworkPropertyMetadata(false));
98 |
99 | public Brush Color
100 | {
101 | get => (Brush)GetValue(ColorProperty);
102 | set => SetValue(ColorProperty, value);
103 | }
104 | public static readonly DependencyProperty ColorProperty =
105 | DependencyProperty.Register(nameof(Color), typeof(Brush), typeof(CurveEditor), new FrameworkPropertyMetadata(Brushes.WhiteSmoke, ColorPropertyChanged));
106 |
107 | static void ColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
108 | {
109 | (d as CurveEditor).UpdateBrush();
110 | }
111 |
112 | Pen _UnitPen = null;
113 | Pen _BorderPen = null;
114 |
115 | CurveEditorWindow _CurveEditorWindow = null;
116 |
117 | static CurveEditor()
118 | {
119 | DefaultStyleKeyProperty.OverrideMetadata(typeof(CurveEditor), new FrameworkPropertyMetadata(typeof(CurveEditor)));
120 | }
121 |
122 | public CurveEditor()
123 | {
124 | Unloaded += (object sender, RoutedEventArgs e) =>
125 | {
126 | _CurveEditorWindow?.Close();
127 | _CurveEditorWindow = null;
128 |
129 | IsOpenedCurveEditor = false;
130 | };
131 | }
132 |
133 | void UpdateBrush()
134 | {
135 | if (_UnitPen == null)
136 | {
137 | _UnitPen = new Pen(Color, 1);
138 | _UnitPen.Freeze();
139 | }
140 |
141 | if (_BorderPen == null)
142 | {
143 | _BorderPen = new Pen(Background, 1);
144 | _BorderPen.Freeze();
145 | }
146 | }
147 |
148 | protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
149 | {
150 | base.OnMouseDoubleClick(e);
151 |
152 | if (_CurveEditorWindow != null)
153 | {
154 | return;
155 | }
156 |
157 | IsOpenedCurveEditor = true;
158 |
159 | var screenPosition = PointToScreen(e.GetPosition(this));
160 |
161 | _CurveEditorWindow = new CurveEditorWindow(this)
162 | {
163 | Background = Background,
164 | ResizeMode = ResizeMode.NoResize,
165 | Left = screenPosition.X,
166 | Top = screenPosition.Y,
167 | };
168 | _CurveEditorWindow.Closed += CurveEditorWindow_Closed;
169 | _CurveEditorWindow.Show();
170 |
171 | e.Handled = true;
172 | }
173 |
174 | protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
175 | {
176 | base.OnItemsSourceChanged(oldValue, newValue);
177 |
178 | _CurveEditorWindow?.ItemsSourceChanged(oldValue, newValue);
179 | }
180 |
181 | void CurveEditorWindow_Closed(object sender, EventArgs e)
182 | {
183 | _CurveEditorWindow.Closed -= CurveEditorWindow_Closed;
184 | _CurveEditorWindow = null;
185 | }
186 | }
187 | }
188 |
--------------------------------------------------------------------------------
/Controls/CurveEditor.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
19 |
20 |
--------------------------------------------------------------------------------
/CurveEditor.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {CEE7C706-974B-47B9-B136-FA21A01CDE21}
8 | WinExe
9 | CurveEditor
10 | CurveEditor
11 | v4.7.2
12 | 512
13 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
14 | 4
15 | true
16 | true
17 |
18 |
19 | AnyCPU
20 | true
21 | full
22 | false
23 | bin\Debug\
24 | DEBUG;TRACE
25 | prompt
26 | 4
27 |
28 |
29 | AnyCPU
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 | packages\LivetCask.3.2.3.1\lib\net452\Livet.dll
40 |
41 |
42 | packages\LivetCask.Behaviors.3.2.3.1\lib\net452\Livet.Behaviors.dll
43 |
44 |
45 | packages\LivetCask.Collections.3.2.3.1\lib\net452\Livet.Collections.dll
46 |
47 |
48 | packages\LivetCask.Converters.3.2.3.1\lib\net452\Livet.Converters.dll
49 |
50 |
51 | packages\LivetCask.Core.3.2.3.1\lib\net452\Livet.Core.dll
52 |
53 |
54 | packages\LivetCask.EventListeners.3.2.3.1\lib\net452\Livet.EventListeners.dll
55 |
56 |
57 | packages\LivetCask.Messaging.3.2.3.1\lib\net452\Livet.Messaging.dll
58 |
59 |
60 | packages\LivetCask.Mvvm.3.2.3.1\lib\net452\Livet.Mvvm.dll
61 |
62 |
63 | packages\Microsoft.Xaml.Behaviors.Wpf.1.1.19\lib\net45\Microsoft.Xaml.Behaviors.dll
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | 4.0
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 | MSBuild:Compile
84 | Designer
85 |
86 |
87 | Designer
88 | MSBuild:Compile
89 |
90 |
91 | MSBuild:Compile
92 | Designer
93 |
94 |
95 | App.xaml
96 | Code
97 |
98 |
99 |
100 | CurveEditorWindow.xaml
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 | MainWindow.xaml
109 | Code
110 |
111 |
112 | MSBuild:Compile
113 | Designer
114 |
115 |
116 |
117 |
118 |
119 | Code
120 |
121 |
122 | True
123 | True
124 | Resources.resx
125 |
126 |
127 | True
128 | Settings.settings
129 | True
130 |
131 |
132 | ResXFileCodeGenerator
133 | Resources.Designer.cs
134 |
135 |
136 |
137 | SettingsSingleFileGenerator
138 | Settings.Designer.cs
139 |
140 |
141 |
142 |
143 |
144 |
145 |
--------------------------------------------------------------------------------
/CurveEditor.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30717.126
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CurveEditor", "CurveEditor.csproj", "{CEE7C706-974B-47B9-B136-FA21A01CDE21}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {CEE7C706-974B-47B9-B136-FA21A01CDE21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {CEE7C706-974B-47B9-B136-FA21A01CDE21}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {CEE7C706-974B-47B9-B136-FA21A01CDE21}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {CEE7C706-974B-47B9-B136-FA21A01CDE21}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {76A89A72-D0E6-4DBF-AA4D-6AD12DA036D5}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/CurveLibs/CatmullRomSpline.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows;
7 |
8 | namespace CurveEditor.CurveLibs
9 | {
10 | class CatmullRomSpline
11 | {
12 | public static float[] Compute(int nSectionDivision, float[] values, float minValue, float maxValue)
13 | {
14 | if (nSectionDivision == 0)
15 | {
16 | throw new InvalidProgramException("nSectionDivision is Zero.");
17 | }
18 |
19 | float rcp = 1.0f / nSectionDivision;
20 |
21 | var results = new List();
22 |
23 | for (int i = 0; i < values.Length - 1; ++i)
24 | {
25 | for (int j = 0; j < nSectionDivision; ++j)
26 | {
27 | float t = j * rcp;
28 | var value = ComputeSingle(i, t, values);
29 | results.Add(Math.Min(Math.Max(value, minValue), maxValue));
30 | }
31 |
32 | results.Add(Math.Min(Math.Max(values[i + 1], minValue), maxValue));
33 | }
34 |
35 | return results.ToArray();
36 | }
37 |
38 | public static float[] Compute(int nSectionDivision, float[] values)
39 | {
40 | if(nSectionDivision == 0)
41 | {
42 | throw new InvalidProgramException("nSectionDivision is Zero.");
43 | }
44 |
45 | float rcp = 1.0f / nSectionDivision;
46 |
47 | var results = new List();
48 |
49 | for (int i = 0; i < values.Length - 1; ++i)
50 | {
51 | for (int j = 0; j < nSectionDivision; ++j)
52 | {
53 | float t = j * rcp;
54 | results.Add(ComputeSingle(i, t, values));
55 | }
56 |
57 | results.Add(values[i + 1]);
58 | }
59 |
60 | return results.ToArray();
61 | }
62 |
63 | public static float ComputeSingle(int index, float t, float[] values)
64 | {
65 | // You must specify 0 for next index if start index is 0.
66 | // So first index is decremented.
67 | float p0 = values[Clamp(index - 1, 0, values.Length - 1)];
68 | float p1 = values[Clamp(index + 0, 0, values.Length - 1)];
69 | float p2 = values[Clamp(index + 1, 0, values.Length - 1)];
70 | float p3 = values[Clamp(index + 2, 0, values.Length - 1)];
71 |
72 | float v0 = (p2 - p0) * 0.5f;
73 | float v1 = (p3 - p1) * 0.5f;
74 |
75 | return CalculateInternal(p1, p2, v0, v1, t);
76 | }
77 |
78 | public static Point Compute(int index, float t, Vector[] points)
79 | {
80 | // You must specify 0 for next index if start index is 0.
81 | // So first index is decremented.
82 | Vector p0 = points[Clamp(index - 1, 0, points.Length - 1)];
83 | Vector p1 = points[Clamp(index + 0, 0, points.Length - 1)];
84 | Vector p2 = points[Clamp(index + 1, 0, points.Length - 1)];
85 | Vector p3 = points[Clamp(index + 2, 0, points.Length - 1)];
86 |
87 | Vector v0 = (p2 - p0) * 0.5f;
88 | Vector v1 = (p3 - p1) * 0.5f;
89 |
90 | float x = CalculateInternal((float)p1.X, (float)p2.X, (float)v0.X, (float)v1.X, t);
91 | float y = CalculateInternal((float)p1.Y, (float)p2.Y, (float)v0.Y, (float)v1.Y, t);
92 |
93 | return new Point(x, y);
94 | }
95 |
96 | static int Clamp(int value, int min, int max)
97 | {
98 | return Math.Max(Math.Min(value, max), min);
99 | }
100 |
101 | static float CalculateInternal(float p0, float p1, float v0, float v1, float t)
102 | {
103 | float t2 = t * t;
104 | return (2.0f * p0 - 2.0f * p1 + v0 + v1) * t2 * t + (-3.0f * p0 + 3.0f * p1 - 2.0f * v0 - v1) * t2 + v0 * t + p0;
105 | }
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/CurveType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace CurveEditor
8 | {
9 | public enum CurveType
10 | {
11 | Linear,
12 | CatmullRom,
13 | B_Spline, // Not implemented yet
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Extensions/ArrayExtension.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace CurveEditor.Extensions
8 | {
9 | static class ArrayExtension
10 | {
11 | public static void Fill(this T[] array, T initialValue)
12 | {
13 | for (int i = 0; i < array.Length; ++i)
14 | {
15 | array.SetValue(initialValue, i);
16 | }
17 | }
18 |
19 | public static int IndexOf(this T[] array, T value)
20 | {
21 | for (int i = 0; i < array.Length; ++i)
22 | {
23 | if (array[i].Equals(value))
24 | {
25 | return i;
26 | }
27 | }
28 |
29 | return -1;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Extensions/VectorExtension.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows;
7 |
8 | namespace CurveEditor.Extensions
9 | {
10 | static class VectorExtension
11 | {
12 | public static Point ToPoint(this Vector me)
13 | {
14 | return new Point(me.X, me.Y);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Jinten
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.ComponentModel;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 | using System.Windows.Controls;
9 | using System.Windows.Data;
10 | using System.Windows.Documents;
11 | using System.Windows.Input;
12 | using System.Windows.Media;
13 | using System.Windows.Media.Imaging;
14 | using System.Windows.Navigation;
15 | using System.Windows.Shapes;
16 |
17 | namespace CurveEditor
18 | {
19 | ///
20 | /// MainWindow.xaml の相互作用ロジック
21 | ///
22 | public partial class MainWindow : Window
23 | {
24 | public MainWindow()
25 | {
26 | InitializeComponent();
27 | }
28 |
29 | protected override void OnClosed(EventArgs e)
30 | {
31 | base.OnClosed(e);
32 |
33 | if(DataContext is IDisposable disposableObj)
34 | {
35 | disposableObj.Dispose();
36 | }
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/MainWindowViewModel.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Collections.ObjectModel;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace CurveEditor
10 | {
11 | public class ControlValueViewModel : Livet.ViewModel
12 | {
13 | public float Value => Model.Value;
14 | public float RangeValue => Model.RangeValue;
15 | public float NormalizedTime => Model.NormalizedTime;
16 |
17 | ControlValue Model { get; }
18 |
19 | public ControlValueViewModel(ControlValue model)
20 | {
21 | Model = model;
22 | Model.PropertyChanged += Model_PropertyChanged;
23 |
24 | CompositeDisposable.Add(() =>
25 | {
26 | Model.PropertyChanged -= Model_PropertyChanged;
27 | });
28 | }
29 |
30 | private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
31 | {
32 | switch(e.PropertyName)
33 | {
34 | case nameof(ControlValue.Value):
35 | RaisePropertyChanged(nameof(Value));
36 | break;
37 | case nameof(ControlValue.RangeValue):
38 | RaisePropertyChanged(nameof(RangeValue));
39 | break;
40 | case nameof(ControlValue.NormalizedTime):
41 | RaisePropertyChanged(nameof(NormalizedTime));
42 | break;
43 | }
44 | }
45 | }
46 |
47 | class MainWindowViewModel : Livet.ViewModel
48 | {
49 | public bool IsClampEnabled
50 | {
51 | get => _IsClampEnabled;
52 | set => RaisePropertyChangedIfSet(ref _IsClampEnabled, value);
53 | }
54 | bool _IsClampEnabled = false;
55 |
56 | public bool IsRangeEnabled
57 | {
58 | get => _IsRangeEnabled;
59 | set => RaisePropertyChangedIfSet(ref _IsRangeEnabled, value);
60 | }
61 | bool _IsRangeEnabled = false;
62 |
63 | public bool IsReadOnlyCurveType
64 | {
65 | get => _IsReadOnlyCurveType;
66 | set => RaisePropertyChangedIfSet(ref _IsReadOnlyCurveType, value);
67 | }
68 | bool _IsReadOnlyCurveType = true;
69 |
70 | public bool IsReadOnlyClampFlag
71 | {
72 | get => _IsReadOnlyClampFlag;
73 | set => RaisePropertyChangedIfSet(ref _IsReadOnlyClampFlag, value);
74 | }
75 | bool _IsReadOnlyClampFlag = false;
76 |
77 | public bool IsReadOnlyRangeFlag
78 | {
79 | get => _IsReadOnlyRangeFlag;
80 | set => RaisePropertyChangedIfSet(ref _IsReadOnlyRangeFlag, value);
81 | }
82 | bool _IsReadOnlyRangeFlag = false;
83 |
84 | public CurveType CurveType
85 | {
86 | get => _CurveType;
87 | set => RaisePropertyChangedIfSet(ref _CurveType, value);
88 | }
89 | CurveType _CurveType = CurveType.CatmullRom;
90 |
91 | public float MinValue
92 | {
93 | get => _MinValue;
94 | set => RaisePropertyChangedIfSet(ref _MinValue, value);
95 | }
96 | float _MinValue = 0;
97 |
98 | public float MaxValue
99 | {
100 | get => _MaxValue;
101 | set => RaisePropertyChangedIfSet(ref _MaxValue, value);
102 | }
103 | float _MaxValue = 100;
104 |
105 | public IEnumerable ControlPoints => _ControlPoints;
106 | ObservableCollection _ControlPoints = new ObservableCollection();
107 |
108 | public IEnumerable ControlPointVMs => _ControlPointVMs;
109 | ObservableCollection _ControlPointVMs = new ObservableCollection();
110 |
111 | public MainWindowViewModel()
112 | {
113 | _ControlPoints.Add(new ControlValue(0.0f, 0.0f));
114 | _ControlPoints.Add(new ControlValue(100.0f, 0.5f));
115 | _ControlPoints.Add(new ControlValue(50.0f, 0.8f));
116 | _ControlPoints.Add(new ControlValue(60.0f, 1.0f));
117 |
118 | foreach (var m in _ControlPoints)
119 | {
120 | _ControlPointVMs.Add(new ControlValueViewModel(m));
121 | }
122 |
123 | CompositeDisposable.Add(() =>
124 | {
125 | foreach(var vm in _ControlPointVMs)
126 | {
127 | vm.Dispose();
128 | }
129 | });
130 | }
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // アセンブリに関する一般情報は以下の属性セットをとおして制御されます。
8 | // アセンブリに関連付けられている情報を変更するには、
9 | // これらの属性値を変更してください。
10 | [assembly: AssemblyTitle("CurveEditor")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("HP Inc.")]
14 | [assembly: AssemblyProduct("CurveEditor")]
15 | [assembly: AssemblyCopyright("Copyright © HP Inc. 2021")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // ComVisible を false に設定すると、このアセンブリ内の型は COM コンポーネントから
20 | // 参照できなくなります。COM からこのアセンブリ内の型にアクセスする必要がある場合は、
21 | // その型の ComVisible 属性を true に設定してください。
22 | [assembly: ComVisible(false)]
23 |
24 | //ローカライズ可能なアプリケーションのビルドを開始するには、
25 | //.csproj ファイルの CultureYouAreCodingWith を
26 | // 内部で設定します。たとえば、
27 | //ソース ファイルで英語を使用している場合、 を en-US に設定します。次に、
28 | //下の NeutralResourceLanguage 属性のコメントを解除します。下の行の "en-US" を
29 | //プロジェクト ファイルの UICulture 設定と一致するよう更新します。
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //テーマ固有のリソース ディクショナリが置かれている場所
36 | //(リソースがページ、
37 | // またはアプリケーション リソース ディクショナリに見つからない場合に使用されます)
38 | ResourceDictionaryLocation.SourceAssembly //汎用リソース ディクショナリが置かれている場所
39 | //(リソースがページ、
40 | //アプリケーション、またはいずれのテーマ固有のリソース ディクショナリにも見つからない場合に使用されます)
41 | )]
42 |
43 |
44 | // アセンブリのバージョン情報は次の 4 つの値で構成されています:
45 | //
46 | // メジャー バージョン
47 | // マイナー バージョン
48 | // ビルド番号
49 | // リビジョン
50 | //
51 | // すべての値を指定するか、次を使用してビルド番号とリビジョン番号を既定に設定できます
52 | // 既定値にすることができます:
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // このコードはツールによって生成されました。
4 | // ランタイム バージョン:4.0.30319.42000
5 | //
6 | // このファイルへの変更は、正しくない動作の原因になったり、
7 | // コードが再生成されるときに失われたりします。
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 |
12 | namespace CurveEditor.Properties
13 | {
14 | ///
15 | /// ローカライズされた文字列などを検索するための、厳密に型指定されたリソース クラスです。
16 | ///
17 | // このクラスは StronglyTypedResourceBuilder クラスによって ResGen
18 | // または Visual Studio のようなツールを使用して自動生成されました。
19 | // メンバーを追加または削除するには、.ResX ファイルを編集して、/str オプションと共に
20 | // ResGen を実行し直すか、または VS プロジェクトをリビルドします。
21 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
22 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
23 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
24 | internal class Resources
25 | {
26 |
27 | private static global::System.Resources.ResourceManager resourceMan;
28 |
29 | private static global::System.Globalization.CultureInfo resourceCulture;
30 |
31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
32 | internal Resources()
33 | {
34 | }
35 |
36 | ///
37 | /// このクラスで使用されるキャッシュされた ResourceManager インスタンスを返します。
38 | ///
39 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
40 | internal static global::System.Resources.ResourceManager ResourceManager
41 | {
42 | get
43 | {
44 | if ((resourceMan == null))
45 | {
46 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CurveEditor.Properties.Resources", typeof(Resources).Assembly);
47 | resourceMan = temp;
48 | }
49 | return resourceMan;
50 | }
51 | }
52 |
53 | ///
54 | /// すべてについて、現在のスレッドの CurrentUICulture プロパティをオーバーライドします
55 | /// 現在のスレッドの CurrentUICulture プロパティをオーバーライドします。
56 | ///
57 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
58 | internal static global::System.Globalization.CultureInfo Culture
59 | {
60 | get
61 | {
62 | return resourceCulture;
63 | }
64 | set
65 | {
66 | resourceCulture = value;
67 | }
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 |
12 | namespace CurveEditor.Properties
13 | {
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
17 | {
18 |
19 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
20 |
21 | public static Settings Default
22 | {
23 | get
24 | {
25 | return defaultInstance;
26 | }
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CurveEditor
2 | Curve editor in WPF.
3 |
4 | Clamped curve.
5 | 
6 |
7 | Range curve.
8 | 
9 |
10 | You can preview interpolated value during right mouse button pressing.
11 |
--------------------------------------------------------------------------------
/Windows/CurveEditorWindow.xaml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Windows/CurveEditorWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using CurveEditor.Controls;
2 | using CurveEditor.CurveLibs;
3 | using CurveEditor.Extensions;
4 | using System;
5 | using System.Collections;
6 | using System.Collections.Generic;
7 | using System.Collections.Specialized;
8 | using System.Globalization;
9 | using System.Linq;
10 | using System.Text;
11 | using System.Threading.Tasks;
12 | using System.Windows;
13 | using System.Windows.Controls;
14 | using System.Windows.Data;
15 | using System.Windows.Documents;
16 | using System.Windows.Input;
17 | using System.Windows.Media;
18 | using System.Windows.Media.Imaging;
19 | using System.Windows.Shapes;
20 |
21 | namespace CurveEditor.Windows
22 | {
23 | static class MathUtility
24 | {
25 | public static int Clamp(int value, int minValue, int maxValue)
26 | {
27 | return Math.Min(Math.Max(value, minValue), maxValue);
28 | }
29 |
30 | public static double Clamp(double value, double minValue, double maxValue)
31 | {
32 | return Math.Min(Math.Max(value, minValue), maxValue);
33 | }
34 | }
35 |
36 | class FontRenderer
37 | {
38 | internal static Typeface Typeface { get; } = new Typeface("Verdana");
39 |
40 | FormattedText _Text = null;
41 | Brush _Foreground = null;
42 | double _FontSize = 8.0;
43 |
44 | public FontRenderer(Brush foreground, double emSize = 8, FlowDirection flowDirection = FlowDirection.LeftToRight)
45 | {
46 | _FontSize = emSize;
47 |
48 | _Foreground = foreground.Clone();
49 | _Foreground.Freeze();
50 |
51 | _Text = new FormattedText("Default", CultureInfo.CurrentCulture, flowDirection, Typeface, emSize, _Foreground, 1.0);
52 | }
53 |
54 | public void Render(DrawingContext dc, in string text, in Point pos, double emSize = 8, FlowDirection flowDirection = FlowDirection.LeftToRight)
55 | {
56 | if (_Text.Text != text)
57 | {
58 | _FontSize = emSize;
59 | _Text = new FormattedText(text, CultureInfo.CurrentCulture, flowDirection, Typeface, _FontSize, _Foreground, 1.0);
60 | }
61 | else
62 | {
63 | if (_FontSize != emSize)
64 | {
65 | _FontSize = emSize;
66 | _Text.SetFontSize(_FontSize);
67 | }
68 | if (_Text.FlowDirection != flowDirection)
69 | {
70 | _Text.FlowDirection = flowDirection;
71 | }
72 | }
73 |
74 | dc.DrawText(_Text, pos);
75 | }
76 | }
77 |
78 | abstract class ControlPoint : UIElement
79 | {
80 | internal const float Size = 9;
81 | internal const float HalfSize = Size * 0.5f;
82 | internal static Size FullSize { get; } = new Size(Size, Size);
83 | internal static Vector RenderOffset { get; } = new Vector(-Math.Ceiling(HalfSize), -Math.Ceiling(HalfSize));
84 | internal static Vector ComputeOffset { get; } = new Vector(Math.Ceiling(HalfSize), Math.Ceiling(HalfSize));
85 |
86 | internal abstract double PositionX { get; }
87 | internal abstract double PositionY { get; }
88 |
89 | internal abstract float Value { get; }
90 | internal abstract float NormalizedTime { get; }
91 |
92 | internal abstract void Capture(Point capturedPosition);
93 | internal abstract void Release();
94 |
95 | internal abstract ControlValue GetControlValue();
96 | }
97 |
98 | class RangeControlPoint : ControlPoint
99 | {
100 | internal override double PositionX => Translate.X;
101 | internal override double PositionY => Translate.Y;
102 |
103 | internal override float Value => Owner.RangedValue;
104 | internal override float NormalizedTime => Owner.NormalizedTime;
105 |
106 | bool _IsCapturing = false;
107 |
108 | Pen _CapturingPen = null;
109 | Brush _CapturingBrush = null;
110 |
111 | Pen _DefaultPen = null;
112 | Brush _DefaultBrush = null;
113 |
114 | Vector _CapturedLocalPosition = new Vector(0, 0);
115 |
116 | DefaultControlPoint Owner { get; }
117 | FontRenderer ValueText { get; } = new FontRenderer(Brushes.Black);
118 | TranslateTransform Translate { get; } = new TranslateTransform(0, 0);
119 |
120 | internal RangeControlPoint(DefaultControlPoint owner)
121 | {
122 | Owner = owner;
123 |
124 | Translate.X = Owner.PositionX;
125 | Translate.Y = Owner.PositionY;
126 |
127 | var transformGroup = new TransformGroup();
128 | transformGroup.Children.Add(Translate);
129 |
130 | RenderTransform = transformGroup;
131 | RenderTransformOrigin = new Point(0.5, 0.5);
132 | }
133 |
134 | internal override ControlValue GetControlValue()
135 | {
136 | return Owner.GetControlValue();
137 | }
138 |
139 | internal override void Capture(Point capturedPosition)
140 | {
141 | _CapturedLocalPosition.X = capturedPosition.X;
142 | _CapturedLocalPosition.Y = capturedPosition.Y;
143 | _IsCapturing = true;
144 | }
145 |
146 | internal override void Release()
147 | {
148 | _CapturedLocalPosition.X = 0;
149 | _CapturedLocalPosition.Y = 0;
150 | _IsCapturing = false;
151 | }
152 |
153 | internal void UpdateTime()
154 | {
155 | Translate.X = Owner.PositionX;
156 |
157 | InvalidateVisual();
158 | }
159 |
160 | internal void UpdateValue(Point mousePosition)
161 | {
162 | Translate.Y = MathUtility.Clamp(mousePosition.Y - _CapturedLocalPosition.Y, ComputeOffset.Y, Owner.LimitedYPos);
163 |
164 | var v = DefaultControlPoint.GetControlValue(new Point(Translate.X, Translate.Y), Owner.ActualAreaSize, Owner.Delta);
165 | Owner.UpdateRangedValue(v.Value);
166 |
167 | InvalidateVisual();
168 | }
169 |
170 | internal void Placement()
171 | {
172 | var y = Owner.ControlAreaHeight - (Owner.RangedValue / Owner.Delta * Owner.ControlAreaHeight);
173 | Translate.X = Owner.PositionX;
174 | Translate.Y = MathUtility.Clamp(y, 0, Owner.ActualAreaSize.Height) + ComputeOffset.Y;
175 |
176 | InvalidateVisual();
177 | }
178 |
179 | Pen GetPen()
180 | {
181 | if (_CapturingPen == null)
182 | {
183 | _CapturingPen = new Pen(Brushes.Yellow, 1);
184 | _CapturingPen.DashStyle = new DashStyle(new double[] { 1 }, 0);
185 | _CapturingPen.Freeze();
186 | }
187 |
188 | if (_DefaultPen == null)
189 | {
190 | _DefaultPen = new Pen(Brushes.Black, 1);
191 | _DefaultPen.Freeze();
192 | }
193 |
194 | return _IsCapturing ? _CapturingPen : _DefaultPen;
195 | }
196 |
197 | Brush GetBrush()
198 | {
199 | if (_CapturingBrush == null)
200 | {
201 | _CapturingBrush = new SolidColorBrush(Color.FromRgb(128, 80, 160));
202 | _CapturingBrush.Freeze();
203 | }
204 |
205 | if (_DefaultBrush == null)
206 | {
207 | _DefaultBrush = new SolidColorBrush(Color.FromRgb(128, 80, 160));
208 | _DefaultBrush.Opacity = 0.3;
209 | _DefaultBrush.Freeze();
210 | }
211 |
212 | return _IsCapturing ? _CapturingBrush : _DefaultBrush;
213 | }
214 |
215 | protected override void OnRender(DrawingContext dc)
216 | {
217 | var text = string.Format("(v={0:F2},t={1:F2})", Value, NormalizedTime);
218 |
219 | dc.DrawRectangle(GetBrush(), GetPen(), new Rect(RenderOffset.ToPoint(), FullSize));
220 | ValueText.Render(dc, text, new Point(0, 4));
221 | }
222 | }
223 |
224 | class DefaultControlPoint : ControlPoint
225 | {
226 | internal Size ActualAreaSize { get; private set; } = new Size();
227 | internal float Delta { get; private set; } = 0.0f;
228 |
229 | internal double LimitedYPos => ActualAreaSize.Height - Math.Ceiling(HalfSize);
230 |
231 | internal double ControlAreaWidth => ActualAreaSize.Width - FullSize.Width - 1;
232 | internal double ControlAreaHeight => ActualAreaSize.Height - FullSize.Height - 1;
233 |
234 | internal override double PositionX => Translate.X;
235 | internal override double PositionY => Translate.Y;
236 | internal override float Value => ControlValue.Value;
237 | internal override float NormalizedTime => ControlValue.NormalizedTime;
238 |
239 | internal float RangedValue => ControlValue.RangeValue;
240 | internal RangeControlPoint RangeControlPoint { get; } = null;
241 |
242 | ControlValue ControlValue { get; } = null;
243 |
244 | bool _IsCapturing = false;
245 |
246 | Pen _CapturingPen = null;
247 | Brush _CapturingBrush = null;
248 |
249 | Pen _DefaultPen = null;
250 | Brush _DefaultBrush = null;
251 |
252 | Vector _CapturedLocalPosition = new Vector(0, 0);
253 |
254 | FontRenderer ValueText { get; } = new FontRenderer(Brushes.Black);
255 | TranslateTransform Translate { get; } = new TranslateTransform(0, 0);
256 |
257 | public DefaultControlPoint(ControlValue value, float delta, float maxWidth, float maxHeight)
258 | {
259 | ControlValue = value;
260 |
261 | Delta = delta;
262 | ActualAreaSize = new Size(maxWidth, maxHeight);
263 |
264 | var transformGroup = new TransformGroup();
265 | transformGroup.Children.Add(Translate);
266 |
267 | RenderTransform = transformGroup;
268 | RenderTransformOrigin = new Point(0.5, 0.5);
269 |
270 | RangeControlPoint = new RangeControlPoint(this);
271 |
272 | Placement();
273 | }
274 |
275 | internal override ControlValue GetControlValue()
276 | {
277 | return ControlValue;
278 | }
279 |
280 | internal void UpdateRangedValue(float value)
281 | {
282 | ControlValue.RangeValue = value;
283 | }
284 |
285 | internal override void Capture(Point capturedPosition)
286 | {
287 | _CapturedLocalPosition.X = capturedPosition.X;
288 | _CapturedLocalPosition.Y = capturedPosition.Y;
289 | _IsCapturing = true;
290 | }
291 |
292 | internal override void Release()
293 | {
294 | _CapturedLocalPosition.X = 0;
295 | _CapturedLocalPosition.Y = 0;
296 | _IsCapturing = false;
297 | }
298 |
299 | public void Update(float delta, Size actualSize)
300 | {
301 | Delta = delta;
302 | ActualAreaSize = actualSize;
303 |
304 | Placement();
305 | }
306 |
307 | public void UpdatePosition(Point mousePosition, double minX, double maxX)
308 | {
309 | Translate.X = MathUtility.Clamp(mousePosition.X - _CapturedLocalPosition.X, minX, maxX);
310 | Translate.Y = MathUtility.Clamp(mousePosition.Y - _CapturedLocalPosition.Y, ComputeOffset.Y, LimitedYPos);
311 |
312 | var v = GetControlValue(new Point(Translate.X, Translate.Y), ActualAreaSize, Delta);
313 | ControlValue.Value = v.Value;
314 | ControlValue.NormalizedTime = v.NormalizedTime;
315 |
316 | RangeControlPoint.UpdateTime();
317 |
318 | InvalidateVisual();
319 | }
320 |
321 | internal static ControlValue GetControlValue(in Point pos, Size actualSize, double valueDelta)
322 | {
323 | var xPos = MathUtility.Clamp(pos.X, ComputeOffset.X, actualSize.Width) - ComputeOffset.X;
324 | var yPos = MathUtility.Clamp(pos.Y, ComputeOffset.Y, actualSize.Height) - ComputeOffset.Y;
325 |
326 | var value = (float)((1.0 - yPos / (actualSize.Height - FullSize.Height - 1)) * valueDelta);
327 | var nTime = (float)(xPos / (actualSize.Width - FullSize.Width - 1));
328 |
329 | return new ControlValue(value, nTime);
330 | }
331 |
332 | internal static Size GetControlAreaSize(in Size actualSize)
333 | {
334 | double w = actualSize.Width - FullSize.Width - 1;
335 | double h = actualSize.Height - FullSize.Height - 1;
336 |
337 | return new Size(w, h);
338 | }
339 |
340 | protected override void OnRender(DrawingContext dc)
341 | {
342 | var text = string.Format("(v={0:F2},t={1:F2})", ControlValue.Value, ControlValue.NormalizedTime);
343 |
344 | dc.DrawRectangle(GetBrush(), GetPen(), new Rect(RenderOffset.ToPoint(), FullSize));
345 | ValueText.Render(dc, text, new Point(0, 4));
346 | }
347 |
348 | void Placement()
349 | {
350 | var x = ControlValue.NormalizedTime * ControlAreaWidth;
351 | var y = ControlAreaHeight - (ControlValue.Value / Delta * ControlAreaHeight);
352 | Translate.X = MathUtility.Clamp(x, 0, ActualAreaSize.Width) + ComputeOffset.X;
353 | Translate.Y = MathUtility.Clamp(y, 0, ActualAreaSize.Height) + ComputeOffset.Y;
354 |
355 | RangeControlPoint.Placement();
356 |
357 | InvalidateVisual();
358 | }
359 |
360 | Pen GetPen()
361 | {
362 | if (_CapturingPen == null)
363 | {
364 | _CapturingPen = new Pen(Brushes.Aqua, 1);
365 | _CapturingPen.DashStyle = new DashStyle(new double[] { 1 }, 0);
366 | _CapturingPen.Freeze();
367 | }
368 |
369 | if (_DefaultPen == null)
370 | {
371 | _DefaultPen = new Pen(Brushes.Black, 1);
372 | _DefaultPen.Freeze();
373 | }
374 |
375 | return _IsCapturing ? _CapturingPen : _DefaultPen;
376 | }
377 |
378 | Brush GetBrush()
379 | {
380 | if (_CapturingBrush == null)
381 | {
382 | _CapturingBrush = new SolidColorBrush(Color.FromRgb(255, 255, 255));
383 | _CapturingBrush.Freeze();
384 | }
385 |
386 | if (_DefaultBrush == null)
387 | {
388 | _DefaultBrush = new SolidColorBrush(Color.FromRgb(255, 255, 255));
389 | _DefaultBrush.Opacity = 0.3;
390 | _DefaultBrush.Freeze();
391 | }
392 |
393 | return _IsCapturing ? _CapturingBrush : _DefaultBrush;
394 | }
395 | }
396 |
397 | class CurveEditorCanvas : Canvas
398 | {
399 | public double ControlAreaStartX => -ControlPoint.RenderOffset.X;
400 | public double ControlAreaStartY => -ControlPoint.RenderOffset.Y;
401 | public double ControlAreaEndX => ActualWidth - ControlAreaStartX;
402 | public double ControlAreaEndY => ActualHeight - ControlAreaStartY;
403 |
404 | public bool IsClamped
405 | {
406 | get => _IsClamped;
407 | set => UpdateClampEnabled(value);
408 | }
409 | bool _IsClamped = false;
410 |
411 | public bool IsRanged
412 | {
413 | get => _IsRanged;
414 | set => UpdateRangeEnabled(value);
415 | }
416 | bool _IsRanged = false;
417 |
418 | Controls.CurveEditor _Editor = null;
419 |
420 | Pen _GridLinePen = null;
421 | Pen _OuterBorderLinePen = null;
422 | Pen _InnerBorderLinePen = null;
423 |
424 | bool _IsScanning = false;
425 | float _ScanningTime = 0.0f;
426 | float _ScanningValue = 0.0f;
427 | float _ScanningRangeValue = 0.0f;
428 | Point _ScanningPoint = new Point();
429 | ControlPoint _DraggingControlPoint = null;
430 |
431 | float DeltaValue => _Editor.MaxValue - _Editor.MinValue;
432 |
433 | FontRenderer MinValueFont = new FontRenderer(Brushes.Black);
434 | FontRenderer MaxValueFont = new FontRenderer(Brushes.Black);
435 | FontRenderer ScanningValueFont = new FontRenderer(Brushes.Red);
436 | FontRenderer ScanningRangeValueFont = new FontRenderer(Brushes.Yellow);
437 |
438 |
439 | internal void SetEditor(Controls.CurveEditor editor)
440 | {
441 | _Editor = editor;
442 | }
443 |
444 | protected override Size ArrangeOverride(Size arrangeSize)
445 | {
446 | foreach (var cp in Children.OfType())
447 | {
448 | cp.Update(DeltaValue, arrangeSize);
449 | }
450 |
451 | return base.ArrangeOverride(arrangeSize);
452 | }
453 |
454 | public void MoveDraggingControlPosition(MouseEventArgs e)
455 | {
456 | if (e.LeftButton == MouseButtonState.Released)
457 | {
458 | _DraggingControlPoint?.Release();
459 | _DraggingControlPoint = null;
460 | return;
461 | }
462 |
463 | UpdateDraggingControlPosition(e);
464 | }
465 |
466 | public void ReleaseDraggingControlPoint()
467 | {
468 | _DraggingControlPoint?.Release();
469 | _DraggingControlPoint = null;
470 | }
471 |
472 | public void BeginScanning(MouseButtonEventArgs e)
473 | {
474 | _IsScanning = true;
475 | _ScanningPoint = e.GetPosition(this);
476 |
477 | UpdateScanningValue(e);
478 |
479 | InvalidateVisual();
480 | }
481 |
482 | public void MoveScanning(MouseEventArgs e)
483 | {
484 | if (e.RightButton == MouseButtonState.Released)
485 | {
486 | _IsScanning = false;
487 | }
488 | else if (_IsScanning)
489 | {
490 | UpdateScanningValue(e);
491 | }
492 | InvalidateVisual();
493 | }
494 |
495 | public void EndScanning(MouseButtonEventArgs e)
496 | {
497 | _IsScanning = false;
498 |
499 | InvalidateVisual();
500 | }
501 |
502 | public void AddControlPoints(bool isRange, ControlValue[] values)
503 | {
504 | foreach (var value in values)
505 | {
506 | // EndX/Y is not still initialized when construct control in the first time.
507 | // So need to be not minus value.
508 | var w = (float)Math.Max(0, ControlAreaEndX);
509 | var h = (float)Math.Max(0, ControlAreaEndY);
510 | var cp = new DefaultControlPoint(value, DeltaValue, w, h);
511 | cp.MouseLeftButtonDown += ControlPoint_MouseLeftButtonDown;
512 | cp.RangeControlPoint.MouseLeftButtonDown += ControlPoint_MouseLeftButtonDown;
513 |
514 | cp.RangeControlPoint.Visibility = IsRanged ? Visibility.Visible : Visibility.Collapsed;
515 |
516 | Children.Add(cp);
517 | Children.Add(cp.RangeControlPoint);
518 | }
519 |
520 | InvalidateVisual();
521 | }
522 |
523 | public void InsertControlPoints(bool isRange, ControlValue[] cpTbl)
524 | {
525 | foreach (var item in cpTbl)
526 | {
527 | var cp = new DefaultControlPoint(item, DeltaValue, (float)ActualWidth, (float)ActualHeight);
528 | cp.MouseLeftButtonDown += ControlPoint_MouseLeftButtonDown;
529 | cp.RangeControlPoint.MouseLeftButtonDown += ControlPoint_MouseLeftButtonDown;
530 |
531 | cp.RangeControlPoint.Visibility = IsRanged ? Visibility.Visible : Visibility.Collapsed;
532 |
533 | Children.Add(cp);
534 | Children.Add(cp.RangeControlPoint);
535 | }
536 |
537 | InvalidateVisual();
538 | }
539 |
540 | public void RemoveControlPoints(ControlValue[] cpTbl)
541 | {
542 | var removeList = Children.OfType().Where(arg => cpTbl.Contains(arg.GetControlValue())).ToArray();
543 | for (int i = 0; i < removeList.Length; i++)
544 | {
545 | var cp = removeList[i];
546 | cp.MouseLeftButtonDown -= ControlPoint_MouseLeftButtonDown;
547 | cp.RangeControlPoint.MouseLeftButtonDown -= ControlPoint_MouseLeftButtonDown;
548 |
549 | Children.Remove(cp);
550 | Children.Remove(cp.RangeControlPoint);
551 | }
552 | }
553 |
554 | public void ClearControlPoints()
555 | {
556 | foreach (var child in Children.OfType())
557 | {
558 | child.MouseLeftButtonDown -= ControlPoint_MouseLeftButtonDown;
559 | }
560 | Children.Clear();
561 |
562 | InvalidateVisual();
563 | }
564 |
565 | protected override void OnRender(DrawingContext dc)
566 | {
567 | if (_GridLinePen == null)
568 | {
569 | _GridLinePen = new Pen(new SolidColorBrush(Color.FromRgb(60, 60, 60)), 1);
570 | _GridLinePen.DashStyle = new DashStyle(new double[] { 2 }, 0);
571 | _GridLinePen.Freeze();
572 | }
573 |
574 | if (_OuterBorderLinePen == null)
575 | {
576 | _OuterBorderLinePen = new Pen(Brushes.Black, 1);
577 | _OuterBorderLinePen.Freeze();
578 | }
579 | if (_InnerBorderLinePen == null)
580 | {
581 | _InnerBorderLinePen = new Pen(Brushes.Black, 1);
582 | _InnerBorderLinePen.DashStyle = new DashStyle(new double[] { 3.0 }, 0.0);
583 | }
584 |
585 | // Render for hit testing.
586 | dc.DrawRectangle(Brushes.Transparent, null, new Rect(0, 0, ActualWidth, ActualHeight));
587 |
588 | int nGrid = 5;
589 | double rcpGrid = 1.0 / nGrid;
590 |
591 | { // Actual size border line.
592 | var p0 = new Point(0, 0);
593 | var p1 = new Point(ActualWidth, ActualHeight);
594 | dc.DrawRectangle(null, _OuterBorderLinePen, new Rect(p0, p1));
595 | }
596 |
597 | { // Inner controllable area border line.
598 | var p0 = new Point(ControlAreaStartX, ControlAreaStartY);
599 | var p1 = new Point(ControlAreaEndX, ControlAreaEndY);
600 | dc.DrawRectangle(null, _InnerBorderLinePen, new Rect(p0, p1));
601 | }
602 |
603 | for (int i = 1; i < nGrid; ++i)
604 | {
605 | var y = i * rcpGrid * ControlAreaEndX;
606 | var p0 = new Point(ControlAreaStartX, y);
607 | var p1 = new Point(ControlAreaEndX, y);
608 | dc.DrawLine(_GridLinePen, p0, p1);
609 | }
610 |
611 | for (int i = 1; i < nGrid; ++i)
612 | {
613 | var x = i * rcpGrid * ControlAreaEndX;
614 | var p0 = new Point(x, ControlAreaStartY);
615 | var p1 = new Point(x, ControlAreaEndX - 1);
616 | dc.DrawLine(_GridLinePen, p0, p1);
617 | }
618 |
619 | var controls = Children.OfType().ToArray();
620 | var rangeControls = Children.OfType().ToArray();
621 |
622 | RenderCurve(dc, Brushes.Pink, controls);
623 | if (IsRanged)
624 | {
625 | RenderCurve(dc, Brushes.Purple, rangeControls);
626 | }
627 |
628 | if (_IsScanning)
629 | {
630 | var sz = DefaultControlPoint.GetControlAreaSize(new Size(ActualWidth, ActualHeight));
631 |
632 | // Scanning line.
633 | double line_x = _ScanningTime * sz.Width + ControlPoint.ComputeOffset.X;
634 | dc.DrawLine(new Pen(Brushes.Red, 1), new Point(line_x, 0), new Point(line_x, ActualHeight));
635 |
636 | // Subtract ControlPoint.HalfSize is for adjusting to render layout.
637 | // _ScanningValue / _Owner.MaxValue is for normalizing vertical value line.
638 | // Multiply sz.Height for stretching control area vertical and subtract from sz.Height for inverting Y axis.
639 |
640 | // Value
641 | if (controls.Length > 0)
642 | {
643 | var pos = new Point(line_x, sz.Height - (_ScanningValue / _Editor.MaxValue * sz.Height) + ControlPoint.ComputeOffset.Y);
644 |
645 | dc.DrawRectangle(Brushes.DarkRed, null, new Rect(pos + ControlPoint.RenderOffset, ControlPoint.FullSize));
646 |
647 | var text = string.Format("(v={0:F2},t={1:F2})", _ScanningValue, _ScanningTime);
648 | ScanningValueFont.Render(dc, text, new Point(_ScanningPoint.X + 2, pos.Y + ControlPoint.FullSize.Height));
649 | }
650 |
651 | // Range value
652 | if (IsRanged && rangeControls.Length > 0)
653 | {
654 | var pos = new Point(line_x, sz.Height - (_ScanningRangeValue / _Editor.MaxValue * sz.Height) + ControlPoint.ComputeOffset.Y);
655 |
656 | dc.DrawRectangle(Brushes.Purple, null, new Rect(pos + ControlPoint.RenderOffset, ControlPoint.FullSize));
657 |
658 | var text = string.Format("(v={0:F2},t={1:F2})", _ScanningRangeValue, _ScanningTime);
659 | ScanningRangeValueFont.Render(dc, text, new Point(_ScanningPoint.X + 2, pos.Y + ControlPoint.FullSize.Height));
660 | }
661 | }
662 |
663 | {
664 | var pos = new Point(ControlPoint.HalfSize + 3, ControlAreaEndY - ControlPoint.FullSize.Height - 3);
665 | MinValueFont.Render(dc, $"{_Editor.MinValue}", pos);
666 | }
667 |
668 | {
669 | var pos = new Point(ControlPoint.HalfSize + 3, ControlPoint.HalfSize + 3);
670 | MaxValueFont.Render(dc, $"{_Editor.MaxValue}", pos);
671 | }
672 | }
673 |
674 | void RenderCurve(DrawingContext dc, Brush curveBrush, ControlPoint[] controls)
675 | {
676 | if (controls.Length == 0)
677 | {
678 | return;
679 | }
680 |
681 | // render curve
682 | var geometry = new StreamGeometry();
683 |
684 | switch (_Editor.Type)
685 | {
686 | case CurveType.Linear:
687 | using (var ctx = geometry.Open())
688 | {
689 | var firstPoint = controls[0];
690 | ctx.BeginFigure(new Point(firstPoint.PositionX, firstPoint.PositionY), false, false);
691 |
692 | var points = new List();
693 | for (int i = 1; i < controls.Length; ++i)
694 | {
695 | var cp = controls[i];
696 | points.Add(new Point(cp.PositionX, cp.PositionY));
697 | }
698 |
699 | ctx.PolyLineTo(points, true, false);
700 | }
701 | break;
702 | case CurveType.CatmullRom:
703 | using (var ctx = geometry.Open())
704 | {
705 | var firstPoint = controls[0];
706 | ctx.BeginFigure(new Point(firstPoint.PositionX, firstPoint.PositionY), false, false);
707 |
708 | // convert position in area unit.
709 | var sz = DefaultControlPoint.GetControlAreaSize(new Size(ActualWidth, ActualHeight));
710 | var controlValues = controls.Select(arg => arg.Value).ToArray();
711 |
712 | int nSectionDivision = 256;
713 |
714 | float[] curveValues;
715 | if (IsClamped)
716 | {
717 | curveValues = CatmullRomSpline.Compute(nSectionDivision, controlValues, _Editor.MinValue, _Editor.MaxValue);
718 | }
719 | else
720 | {
721 | curveValues = CatmullRomSpline.Compute(nSectionDivision, controlValues);
722 | }
723 |
724 | var rcp = 1.0f / nSectionDivision;
725 |
726 | var points = new List();
727 |
728 | for (int i = 0; i < controlValues.Length - 1; ++i)
729 | {
730 | var t0 = controls[i + 0].NormalizedTime;
731 | var t1 = controls[i + 1].NormalizedTime;
732 | var dt = t1 - t0;
733 |
734 | int index = i * nSectionDivision;
735 | for (int j = 0; j < nSectionDivision; ++j)
736 | {
737 | var x = (dt * (j * rcp) + t0) * sz.Width + ControlPoint.ComputeOffset.X;
738 | var y = sz.Height - curveValues[index + j] / _Editor.MaxValue * sz.Height + ControlPoint.ComputeOffset.Y;
739 | points.Add(new Point(x, y));
740 | }
741 |
742 | }
743 | ctx.PolyLineTo(points, true, false);
744 | }
745 | break;
746 | case CurveType.B_Spline:
747 | throw new NotImplementedException();
748 | }
749 |
750 | dc.DrawGeometry(null, new Pen(curveBrush, 1), geometry);
751 | }
752 |
753 | void UpdateClampEnabled(bool enable)
754 | {
755 | _IsClamped = enable;
756 |
757 | InvalidateVisual();
758 | }
759 |
760 | void UpdateRangeEnabled(bool enable)
761 | {
762 | _IsRanged = enable;
763 |
764 | var rangeControlPoints = Children.OfType().ToArray();
765 | foreach (var rcp in rangeControlPoints)
766 | {
767 | rcp.Visibility = _IsRanged ? Visibility.Visible : Visibility.Collapsed;
768 | }
769 |
770 | InvalidateVisual();
771 | }
772 |
773 | void UpdateDraggingControlPosition(MouseEventArgs e)
774 | {
775 | if (_DraggingControlPoint == null)
776 | {
777 | return;
778 | }
779 |
780 | // depends on not ranged value position.
781 | var controls = Children.OfType().ToArray();
782 |
783 | int index = controls.IndexOf(_DraggingControlPoint);
784 | var min_x = index > 0 ? controls[index - 1].PositionX : ControlAreaStartX;
785 | var max_x = index < controls.Length - 1 ? controls[index + 1].PositionX : ControlAreaEndX;
786 |
787 | var pos = e.GetPosition(this);
788 | switch (_DraggingControlPoint)
789 | {
790 | case DefaultControlPoint dcp:
791 | dcp.UpdatePosition(pos, min_x, max_x);
792 | break;
793 | case RangeControlPoint rcp:
794 | rcp.UpdateValue(pos);
795 | break;
796 | }
797 |
798 | InvalidateVisual();
799 | }
800 |
801 | void UpdateScanningValue(MouseEventArgs e)
802 | {
803 | var controls = Children.OfType().ToArray();
804 | _ScanningValue = GetScanningValue(e, controls);
805 |
806 | var rangeControls = Children.OfType().ToArray();
807 | _ScanningRangeValue = GetScanningValue(e, rangeControls);
808 | }
809 |
810 | float GetScanningValue(MouseEventArgs e, ControlPoint[] controls)
811 | {
812 | if (controls.Length == 0)
813 | {
814 | return 0;
815 | }
816 |
817 | var p0 = controls.First();
818 | var p1 = controls.Last();
819 |
820 | _ScanningPoint = e.GetPosition(this);
821 | _ScanningPoint.X = MathUtility.Clamp(_ScanningPoint.X, p0.PositionX, p1.PositionX);
822 |
823 | _ScanningTime = (float)((_ScanningPoint.X - Math.Ceiling(ControlPoint.HalfSize)) / (ControlAreaEndX - ControlAreaStartX));
824 |
825 | switch (_Editor.Type)
826 | {
827 | case CurveType.Linear:
828 | return GetScanningLinearValue(_ScanningTime, controls);
829 | case CurveType.CatmullRom:
830 | return GetScanningCatmullRomValue(_ScanningTime, controls);
831 | case CurveType.B_Spline:
832 | throw new NotImplementedException();
833 | }
834 |
835 | throw new InvalidProgramException();
836 | }
837 |
838 | float GetScanningLinearValue(float t, ControlPoint[] controls)
839 | {
840 | var p0 = controls.Reverse().FirstOrDefault(arg => arg.NormalizedTime <= t);
841 | var p1 = controls.FirstOrDefault(arg => arg.NormalizedTime > t);
842 |
843 | float t0 = p0.NormalizedTime;
844 | float t1 = p1 != null ? p1.NormalizedTime : p0.NormalizedTime;
845 | float dt = t1 - t0;
846 | if (dt == 0)
847 | {
848 | return p0.Value;
849 | }
850 |
851 | float v0 = p0.Value;
852 | float v1 = p1.Value;
853 | float st = (t - t0) / dt;
854 |
855 | return (v1 - v0) * st + v0;
856 | }
857 |
858 | float GetScanningCatmullRomValue(float t, ControlPoint[] controls)
859 | {
860 | if (t == 0)
861 | {
862 | return controls.First().Value;
863 | }
864 | if (t == 1.0f)
865 | {
866 | return controls.Last().Value;
867 | }
868 |
869 | var p0 = controls.Reverse().FirstOrDefault(arg => arg.NormalizedTime <= t);
870 | var p1 = controls.FirstOrDefault(arg => arg.NormalizedTime > t);
871 |
872 | var t0 = p0.NormalizedTime;
873 | var t1 = p1 != null ? p1.NormalizedTime : p0.NormalizedTime;
874 | var dt = t1 - t0;
875 | if (dt == 0)
876 | {
877 | return p0.Value;
878 | }
879 | var seg_t = (t - t0) / dt;
880 |
881 | var values = controls.Select(arg => arg.Value).ToArray();
882 | var value = CatmullRomSpline.ComputeSingle(controls.IndexOf(p0), seg_t, values);
883 |
884 | if (IsClamped == false)
885 | {
886 | return value;
887 | }
888 |
889 | return Math.Min(Math.Max(value, _Editor.MinValue), _Editor.MaxValue);
890 | }
891 |
892 | void ControlPoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
893 | {
894 | var cp = sender as ControlPoint;
895 |
896 | _DraggingControlPoint = cp;
897 | _DraggingControlPoint.Capture(e.GetPosition(cp));
898 |
899 | UpdateDraggingControlPosition(e);
900 | }
901 | }
902 |
903 | ///
904 | /// CurveEditorWindow.xaml の相互作用ロジック
905 | ///
906 | public partial class CurveEditorWindow : Window
907 | {
908 | Controls.CurveEditor Editor { get; } = null;
909 |
910 | public CurveEditorWindow(Controls.CurveEditor editor)
911 | {
912 | Editor = editor;
913 |
914 | InitializeComponent();
915 |
916 | EditorCanvas.SetEditor(Editor);
917 | EditorCanvas.IsClamped = Editor.IsClampEnabled;
918 | EditorCanvas.IsRanged = Editor.IsRangeEnabled;
919 |
920 | switch (Editor.Type)
921 | {
922 | case CurveType.Linear:
923 | CurveTypeComboBox.SelectedIndex = 0;
924 | break;
925 | case CurveType.CatmullRom:
926 | CurveTypeComboBox.SelectedIndex = 1;
927 | break;
928 | }
929 |
930 | ClampCheckBox.IsChecked = Editor.IsClampEnabled;
931 | RangeCheckBox.IsChecked = Editor.IsRangeEnabled;
932 |
933 | ClampCheckBox.IsEnabled = Editor.IsReadOnlyClampFlag == false;
934 | RangeCheckBox.IsEnabled = Editor.IsReadOnlyRangeFlag == false;
935 | CurveTypeComboBox.IsEnabled = Editor.IsReadOnlyType == false;
936 |
937 | CurveTypeComboBox.SelectionChanged += CurveTypeComboBox_SelectionChanged;
938 |
939 | ClampCheckBox.Checked += ClampCheckBox_Checked;
940 | ClampCheckBox.Unchecked += ClampCheckBox_Unchecked;
941 |
942 | RangeCheckBox.Checked += RangeCheckBox_Checked;
943 | RangeCheckBox.Unchecked += RangeCheckBox_Unchecked;
944 |
945 | var cpTbl = Editor.ItemsSource?.OfType().ToArray();
946 |
947 | if (cpTbl != null)
948 | {
949 | EditorCanvas.AddControlPoints(false, cpTbl);
950 | }
951 | }
952 |
953 | public void ItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
954 | {
955 | if (oldValue is INotifyCollectionChanged oldCollection)
956 | {
957 | oldCollection.CollectionChanged -= CollectionChanged;
958 | }
959 |
960 | if (newValue is INotifyCollectionChanged newCollection)
961 | {
962 | newCollection.CollectionChanged += CollectionChanged;
963 | }
964 |
965 | EditorCanvas.ClearControlPoints();
966 |
967 | if (newValue != null)
968 | {
969 | EditorCanvas.AddControlPoints(false, newValue.OfType().ToArray());
970 | }
971 | }
972 |
973 | protected override void OnMouseMove(MouseEventArgs e)
974 | {
975 | EditorCanvas.MoveScanning(e);
976 |
977 | EditorCanvas.MoveDraggingControlPosition(e);
978 | }
979 |
980 | protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
981 | {
982 | EditorCanvas.ReleaseDraggingControlPoint();
983 | }
984 |
985 | protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
986 | {
987 | base.OnMouseRightButtonDown(e);
988 |
989 | EditorCanvas.BeginScanning(e);
990 | }
991 |
992 | protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
993 | {
994 | base.OnMouseRightButtonUp(e);
995 |
996 | EditorCanvas.EndScanning(e);
997 | }
998 |
999 | protected override void OnClosed(EventArgs e)
1000 | {
1001 | base.OnClosed(e);
1002 |
1003 | CurveTypeComboBox.SelectionChanged -= CurveTypeComboBox_SelectionChanged;
1004 |
1005 | RangeCheckBox.Unchecked -= RangeCheckBox_Unchecked;
1006 | RangeCheckBox.Checked -= RangeCheckBox_Checked;
1007 |
1008 | ClampCheckBox.Unchecked -= ClampCheckBox_Unchecked;
1009 | ClampCheckBox.Checked -= ClampCheckBox_Checked;
1010 | }
1011 |
1012 | void CurveTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
1013 | {
1014 | var selectedContent = (e.AddedItems[0] as ComboBoxItem).Content;
1015 |
1016 | switch (selectedContent)
1017 | {
1018 | case nameof(CurveType.Linear):
1019 | Editor.Type = CurveType.Linear;
1020 | break;
1021 | case nameof(CurveType.CatmullRom):
1022 | Editor.Type = CurveType.CatmullRom;
1023 | break;
1024 | }
1025 |
1026 | EditorCanvas.InvalidateVisual();
1027 | }
1028 |
1029 | void ClampCheckBox_Checked(object sender, RoutedEventArgs e)
1030 | {
1031 | EditorCanvas.IsClamped = true;
1032 | Editor.IsClampEnabled = true;
1033 | }
1034 |
1035 | void ClampCheckBox_Unchecked(object sender, RoutedEventArgs e)
1036 | {
1037 | EditorCanvas.IsClamped = false;
1038 | Editor.IsClampEnabled = false;
1039 | }
1040 |
1041 | void RangeCheckBox_Checked(object sender, RoutedEventArgs e)
1042 | {
1043 | EditorCanvas.IsRanged = true;
1044 | Editor.IsRangeEnabled = true;
1045 | }
1046 |
1047 | void RangeCheckBox_Unchecked(object sender, RoutedEventArgs e)
1048 | {
1049 | EditorCanvas.IsRanged = false;
1050 | Editor.IsRangeEnabled = false;
1051 | }
1052 |
1053 | void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
1054 | {
1055 | switch (e.Action)
1056 | {
1057 | case NotifyCollectionChangedAction.Reset:
1058 | EditorCanvas.ClearControlPoints();
1059 | break;
1060 | case NotifyCollectionChangedAction.Add:
1061 | EditorCanvas.InsertControlPoints(false, e.NewItems.OfType().ToArray());
1062 | break;
1063 | case NotifyCollectionChangedAction.Remove:
1064 | EditorCanvas.RemoveControlPoints(e.OldItems.OfType().ToArray());
1065 | break;
1066 | }
1067 | }
1068 | }
1069 | }
1070 |
--------------------------------------------------------------------------------
/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------