summaryrefslogtreecommitdiff
path: root/grapher/Models/Options/LUT/LUTPanelOptions.cs
blob: eedcfa8069bd48e4fe3b843fe60f3ad12114a350 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace grapher.Models.Options.LUT
{
    public class LUTPanelOptions : OptionBase
    {
        public const int PanelPadding = 5;
        public const int PanelHeight = 100;

        public LUTPanelOptions(RichTextBox pointsTextBox, RichTextBox activeValuesTextBox)
        {
            PointsTextBox = pointsTextBox;
            PointsTextBox.Height = PanelHeight;

            ActiveValuesTextBox = activeValuesTextBox;
            ActiveValuesTextBox.Height = PanelHeight;
            ActiveValuesTextBox.ReadOnly = true;
        }

        public RichTextBox PointsTextBox
        {
            get;
        }

        public RichTextBox ActiveValuesTextBox
        {
            get;
        }

        public override bool Visible
        {
            get
            {
                return PointsTextBox.Visible || ShouldShow;
            }
        }

        public override int Top
        {
            get
            {
                return PointsTextBox.Top;
            }
            set
            {
                PointsTextBox.Top = value;
                ActiveValuesTextBox.Top = value;
            }
        }

        public override int Height
        {
            get
            {
                return PointsTextBox.Height;
            }
        }

        public override int Left
        {
            get
            {
                return PointsTextBox.Left;
            }
            set
            {
                PointsTextBox.Left = value;
                AlignActivePanelToPointsTextBox();
            }
        }

        public override int Width
        {
            get
            {
                return PointsTextBox.Width;
            }
            set
            {
                var panelWidth = value / 2 - PanelPadding;
                PointsTextBox.Width = panelWidth;
                ActiveValuesTextBox.Width = panelWidth;
                AlignActivePanelToPointsTextBox();
            }
        }

        private bool ShouldShow { get; set; }

        public override void Hide()
        {
            PointsTextBox.Hide();
            ActiveValuesTextBox.Hide();
            ShouldShow = false;
        }

        public override void Show(string name)
        {
            PointsTextBox.Show();
            ActiveValuesTextBox.Show();
            ShouldShow = true;
        }

        public override void AlignActiveValues()
        {
            // Nothing to do here.
        }

        public void SetActiveValues(IEnumerable<float> rawData, int length, AccelMode mode)
        {
            if (mode == AccelMode.lut && length > 1 && rawData.First() != 0)
            {
                var pointsLen = length / 2;
                var points = new Vec2<float>[pointsLen];
                for (int i = 0; i < pointsLen; i++)
                {
                    var data_idx = i * 2;
                    points[i] = new Vec2<float>
                    {
                        x = rawData.ElementAt(data_idx),
                        y = rawData.ElementAt(data_idx + 1)
                    };
                }
                ActiveValuesTextBox.Text = PointsToActiveValuesText(points, pointsLen);

                if (string.IsNullOrWhiteSpace(PointsTextBox.Text))
                {
                    PointsTextBox.Text = PointsToEntryTextBoxText(points, pointsLen);
                }
            }
            else
            {
                ActiveValuesTextBox.Text = string.Empty;
            }
        }

        public (Vec2<float>[], int length) GetPoints()
        {
            return UserTextToPoints(PointsTextBox.Text);
        }

        private static (Vec2<float>[], int length) UserTextToPoints(string userText)
        {
            if (string.IsNullOrWhiteSpace(userText))
            {
                throw new ApplicationException("Text must be entered in text box to fill Look Up Table.");
            }

            Vec2<float>[] points = new Vec2<float>[AccelArgs.MaxLutPoints];

            var userTextSplit = userText.Trim().Trim(';').Split(';');
            int index = 0;
            float lastX = 0;

            int pointsCount = userTextSplit.Count();

            if (pointsCount < 2)
            {
                throw new ApplicationException("At least 2 points required");
            }

            if (pointsCount > AccelArgs.MaxLutPoints)
            {
                throw new ApplicationException($"Number of points exceeds max ({AccelArgs.MaxLutPoints})");
            }

            foreach(var pointEntry in userTextSplit)
            {
                var pointSplit = pointEntry.Trim().Split(',');

                if (pointSplit.Length != 2)
                {
                    throw new ApplicationException($"Point at index {index} is malformed. Expected format: x,y; Given: {pointEntry.Trim()}");
                }

                float x;
                float y;

                try
                {
                    x = float.Parse(pointSplit[0]);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException($"X-value for point at index {index} is malformed. Expected: float. Given: {pointSplit[0]}", ex);
                }

                if (x <= 0)
                {
                    throw new ApplicationException($"X-value for point at index {index} is less than or equal to 0. Point (0,0) is implied and should not be specified in points text.");
                }

                if (x <= lastX)
                {
                    throw new ApplicationException($"X-value for point at index {index} is less than or equal to previous x-value. Value: {x} Previous: {lastX}");
                }

                lastX = x;

                try
                {
                    y = float.Parse(pointSplit[1]);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException($"Y-value for point at index {index} is malformed. Expected: float. Given: {pointSplit[1]}", ex);
                }

                if (y <= 0)
                {
                    throw new ApplicationException($"Y-value for point at index {index} is less than or equal to 0. Value: {y}");
                }

                points[index] = new Vec2<float> { x = x, y = y };

                index++;
            }

            return (points, userTextSplit.Length);
        }

        private void AlignActivePanelToPointsTextBox()
        {
            ActiveValuesTextBox.Left = PointsTextBox.Right + PanelPadding;
        }

        private string PointsToActiveValuesText(IEnumerable<Vec2<float>> points, int length)
        {
            StringBuilder builder = new StringBuilder();

            for(int i = 0; i < length; i++)
            {
                var point = points.ElementAt(i);
                builder.AppendLine($"{point.x},{point.y};");
            }

            return builder.ToString();
        }

        private string PointsToEntryTextBoxText(IEnumerable<Vec2<float>> points, int length)
        {
            StringBuilder builder = new StringBuilder();

            for(int i = 0; i < length; i++)
            {
                var point = points.ElementAt(i);
                builder.Append($"{point.x},{point.y};");
            }

            return builder.ToString();
        }
    }
}