-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormModel.cs
More file actions
150 lines (132 loc) · 4.69 KB
/
FormModel.cs
File metadata and controls
150 lines (132 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using SmartPixel.Extensions;
using SmartPixel.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Media;
using Color = System.Drawing.Color;
namespace SmartPixel
{
public class FormModel : INotifyPropertyChanged
{
private string _path;
private readonly FormService _formService;
private int _colors = 20;
private string _elapsedSeconds = "N/A";
private List<Color> _colorPalette = new List<Color>();
private List<SolidColorBrush> _visualColorPalette = new List<SolidColorBrush>();
public Relays StartSmartPixelCommand { get; private set; }
public Relays ConvertCommand { get; private set; }
public Relays GeneratePaletteCommand { get; private set; }
public Relays GenerateGrayPaletteCommand { get; private set; }
public FormModel()
{
_formService = new FormService();
InitializeRelayCommands();
}
private void InitializeRelayCommands()
{
StartSmartPixelCommand = new Relays(StartSp, CanFireCommand);
ConvertCommand = new Relays(ConvertSp, CanFireCommand);
GeneratePaletteCommand = new Relays(GeneratePalette, CanFireCommand);
GenerateGrayPaletteCommand = new Relays(GenerateGrayPalette, CanFireCommand);
}
public string Path
{
get => _path;
set
{
if (_path == value) return;
_path = value;
RaisePropertyChanged("Path");
}
}
public List<Color> ColorPalette
{
get => _colorPalette;
set
{
if (_colorPalette == value) return;
_colorPalette = value;
RaisePropertyChanged("ColorPalette");
}
}
public string Colors
{
get => $"{_colors}";
set
{
if (_colors.ToString() == value) return;
_colors = int.Parse(value);
RaisePropertyChanged("Colors");
}
}
public List<SolidColorBrush> VisualColorPalette
{
get => _visualColorPalette;
set
{
if (_visualColorPalette == value) return;
_visualColorPalette = value;
RaisePropertyChanged("VisualColorPalette");
}
}
public string ElapsedSeconds
{
get => _elapsedSeconds;
set
{
if (_elapsedSeconds == value) return;
_elapsedSeconds = value;
RaisePropertyChanged("ElapsedSeconds");
}
}
private void StartSp(object input)
{
var path = _formService.StartSmartPixel();
Path = path;
GC.Collect();
GC.WaitForPendingFinalizers();
}
private void ConvertSp(object input)
{
var data = _formService.SmartPixelConvert(Path, _colorPalette);
var dataSplit = data.Split('@');
Path = dataSplit[0];
ElapsedSeconds = dataSplit[1] + "s";
}
private void GeneratePalette(object input)
{
var palette = _formService.GenerateColorPalette(_colors).ToList();
ColorPalette = palette;
GenerateVisualColorPalette();
}
private void GenerateGrayPalette(object input)
{
var palette = _formService.GenerateGrayColorPalette(_colors).ToList();
ColorPalette = palette;
GenerateVisualColorPalette();
}
private void GenerateVisualColorPalette()
{
var newVisualColorPalette = ColorPalette.Select(color => color.ConvertToMedia()).Select(mediaColor => new SolidColorBrush(mediaColor)).ToList();
VisualColorPalette = newVisualColorPalette;
}
private bool CanFireCommand(object input)
{
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string property = null)
{
if (property is null) return;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
private void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
}