forked from philseeley/boatinstrument
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauge_box.dart
403 lines (320 loc) · 12.9 KB
/
gauge_box.dart
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import 'dart:math';
import 'package:boatinstrument/boatinstrument_controller.dart';
import 'package:flutter/material.dart';
import 'double_value_box.dart';
abstract class DoubleValueGaugeBox extends DoubleValueBox {
final double step;
const DoubleValueGaugeBox(super.config, super.title, super.path, {super.minValue = 0, required super.maxValue, super.angle, this.step = 1, super.key});
@override
DoubleValueGaugeBoxState createState() => DoubleValueGaugeBoxState();
}
class DoubleValueGaugeBoxState<T extends DoubleValueGaugeBox> extends DoubleValueBoxState<T> {
int _minDisplay = 0;
int _maxDisplay = 0;
int _displayStep = 0;
@override
void initState() {
super.initState();
_minDisplay = widget.convert(widget.minValue!).ceil();
_maxDisplay = widget.convert(widget.maxValue!).floor();
double steps = (widget.maxValue! - widget.minValue!)/widget.step;
_displayStep = ((_maxDisplay - _minDisplay)/steps).round();
}
}
enum GaugeOrientation {
down(0, 0.0, -0.5, null, 0, 0, null, null, 0, null, 0),
left(pi/2, 0.0, 0.0, 0, null, 0, null, null, 0, 0, null),
up(pi, 0.0, 0.0, 0, null, 0, null, 0, null, null, 0),
right(pi/2+pi, -0.5, 0.0, 0, null, null, 0, null, 0, null, 0);
final double _rotation;
final double _xm;
final double _ym;
final double? _titleTop;
final double? _titleBottom;
final double? _titleLeft;
final double? _titleRight;
final double? _unitsTop;
final double? _unitsBottom;
final double? _unitsLeft;
final double? _unitsRight;
const GaugeOrientation(this._rotation, this._xm, this._ym,
this._titleTop, this._titleBottom, this._titleLeft, this._titleRight,
this._unitsTop, this._unitsBottom, this._unitsLeft, this._unitsRight);
}
class _SemiGaugePainter extends CustomPainter {
final BuildContext _context;
final GaugeOrientation _orientation;
final bool _mirror;
final int _minValue;
final int _maxValue;
_SemiGaugePainter(this._context, this._orientation, this._mirror, this._minValue, this._maxValue);
@override
void paint(Canvas canvas, Size canvasSize) {
ThemeData theme = Theme.of(_context);
double w = canvasSize.width;
double h = canvasSize.height;
Paint paint = Paint()
..style = PaintingStyle.stroke
..color = theme.colorScheme.onSurface
..strokeWidth = 2.0;
double base = w;
if(_orientation == GaugeOrientation.left ||
_orientation == GaugeOrientation.right) {
base = h;
}
canvas.save();
canvas.translate(base*_orientation._xm, base*_orientation._ym);
canvas.translate(base/2, base/2);
canvas.rotate(_orientation._rotation);
canvas.translate(-base/2, -base/2);
canvas.drawArc(Rect.fromLTWH(0, 0, base, base), 0.0, pi, true, paint);
canvas.restore();
TextPainter tp = TextPainter(textDirection: TextDirection.ltr);
try {
canvas.translate((base / 2) + base*_orientation._xm, (base / 2) + base*_orientation._ym);
double diff = (_maxValue - _minValue) / 4;
for (int i = 0; i <= 4; ++i) {
String label = (_minValue + (diff*i)).toInt().toString();
tp.text = TextSpan(
text: label,
style: theme.textTheme.bodyMedium?.copyWith(backgroundColor: theme.colorScheme.surface));
tp.layout();
double angle = (_mirror ? pi - pi/4*i : pi/4*i) + _orientation._rotation;
angle = i == 0 ? angle: angle;
angle = i == 4 ? angle: angle;
double x = cos(angle) * (base / 2 - 20.0);
double y = sin(angle) * (base / 2 - 20.0);
tp.paint(
canvas, Offset(x - tp.size.width / 2, y - tp.size.height / 2));
}
} finally {
tp.dispose();
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
class _SemiGaugeNeedlePainter extends CustomPainter {
final GaugeOrientation _orientation;
final double _angle;
_SemiGaugeNeedlePainter(this._orientation, this._angle);
@override
void paint(Canvas canvas, Size canvasSize) {
double w = canvasSize.width;
double h = canvasSize.height;
Paint paint = Paint()
..style = PaintingStyle.fill
..color = Colors.blue;
double base = w;
if(_orientation == GaugeOrientation.left ||
_orientation == GaugeOrientation.right) {
base = h;
}
Path needle = Path()
..moveTo(-10.0, 0.0)
..lineTo(0.0, base/2)
..lineTo(10.0, 0.0)
..moveTo(0.0, 0.0)
..addArc(const Offset(-10, -10.0) & const Size(20.0, 20.0), 0.0, -pi)
..close();
canvas.translate(base*_orientation._xm, base*_orientation._ym);
canvas.translate(base/2, base/2);
canvas.rotate(_orientation._rotation);
canvas.rotate(_angle);
canvas.drawPath(needle, paint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
abstract class DoubleValueSemiGaugeBox extends DoubleValueGaugeBox {
final GaugeOrientation orientation;
final bool mirror;
const DoubleValueSemiGaugeBox(super.config, super.title, this.orientation, super.path, {super.minValue = 0, required super.maxValue, super.angle, this.mirror = false, super.key});
@override
DoubleValueSemiGaugeBoxState createState() => DoubleValueSemiGaugeBoxState();
}
class DoubleValueSemiGaugeBoxState<T extends DoubleValueSemiGaugeBox> extends DoubleValueGaugeBoxState<T> {
@override
Widget build(BuildContext context) {
GaugeOrientation o = widget.orientation;
List<Widget> stack = [
Positioned(top: o._titleTop, bottom: o._titleBottom, left: o._titleLeft, right: o._titleRight, child: Text(widget.title)),
Positioned(top: o._unitsTop, bottom: o._unitsBottom, left: o._unitsLeft, right: o._unitsRight, child: Text(widget.units(displayValue??0.0))),
CustomPaint(
size: Size.infinite,
painter: _SemiGaugePainter(context, o, widget.mirror, _minDisplay, _maxDisplay)
)
];
if(value != null) {
double angle = ((pi/(widget.maxValue! - widget.minValue!)) * (value! - widget.minValue!)) - pi/2;
if(widget.mirror) {
angle = (pi*2)-angle;
}
stack.add(CustomPaint(
size: Size.infinite,
painter: _SemiGaugeNeedlePainter(o, angle)
));
}
return Container(padding: const EdgeInsets.all(15.0), child: RepaintBoundary(child: Stack(children: stack)));
}
}
// This is the number of degrees that the circular gauge starts at from dead south.
final double circularGaugeOffset = deg2Rad(20);
class _CircularGaugePainter extends CustomPainter {
final BuildContext _context;
final double _minValue;
final int _minDisplay;
final int _maxDisplay;
final int _displayStep;
_CircularGaugePainter(this._context, this._minValue, this._minDisplay, this._maxDisplay, this._displayStep);
@override
void paint(Canvas canvas, Size canvasSize) {
double size = min(canvasSize.width, canvasSize.height);
Paint paint = Paint()
..style = PaintingStyle.stroke
..color = Theme.of(_context).colorScheme.onSurface
..strokeWidth = 2.0;
canvas.drawArc(Rect.fromLTWH(0, 0, size, size), pi/2+circularGaugeOffset, 2*pi-(circularGaugeOffset*2), false, paint);
TextPainter tp = TextPainter(textDirection: TextDirection.ltr);
try {
paint.strokeWidth = 20.0;
const double width = 0.02;
double steps = (_maxDisplay - _minValue) / _displayStep;
double angleStep = (2*pi-(circularGaugeOffset*2))/steps;
double convertOffset = (_minDisplay - _minValue) * angleStep;
for (int i = 0; i <= steps; ++i) {
canvas.drawArc(
const Offset(10.0, 10.0) & Size(size - 20.0, size - 20.0),
(i*angleStep) + (pi / 2) + circularGaugeOffset + convertOffset - (width / 2), width, false, paint);
tp.text = TextSpan(
text: (i*_displayStep+_minDisplay).toString(),
style: Theme
.of(_context)
.textTheme
.bodyMedium);
tp.layout();
double x = cos((i*angleStep) + (pi / 2) + circularGaugeOffset + convertOffset) * (size / 2 - 40.0);
double y = sin((i*angleStep) + (pi / 2) + circularGaugeOffset + convertOffset) * (size / 2 - 40.0);
canvas.save();
canvas.translate(size / 2, size / 2);
tp.paint(
canvas, Offset(x - tp.size.width / 2, y - tp.size.height / 2));
canvas.restore();
}
} finally {
tp.dispose();
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
class _CircularGaugeNeedlePainter extends CustomPainter {
final double _angle;
_CircularGaugeNeedlePainter(this._angle);
@override
void paint(Canvas canvas, Size canvasSize) {
double size = min(canvasSize.width, canvasSize.height);
Paint paint = Paint()
..style = PaintingStyle.fill
..color = Colors.blue;
Path needle = Path()
..moveTo(-10.0, 0.0)
..lineTo(0.0, -size/2)
..lineTo(10.0, 0.0)
..moveTo(0.0, 0.0)
..addArc(const Offset(-10, -10.0) & const Size(20.0, 20.0), 0.0, pi)
..close();
canvas.translate(size/2, size/2);
canvas.rotate(pi+_angle+circularGaugeOffset);
canvas.drawPath(needle, paint);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
abstract class DoubleValueCircularGaugeBox extends DoubleValueGaugeBox {
const DoubleValueCircularGaugeBox(super.config, super.title, super.path, {super.minValue = 0, required super.maxValue, required super.step, super.key});
@override
DoubleValueCircularGaugeBoxState createState() => DoubleValueCircularGaugeBoxState();
}
class DoubleValueCircularGaugeBoxState<T extends DoubleValueCircularGaugeBox> extends DoubleValueGaugeBoxState<T> {
@override
Widget build(BuildContext context) {
List<Widget> stack = [
Positioned(top: 0, left: 0, child: Text(widget.title)),
Positioned(top: 0, right: 0, child: Text(widget.units(displayValue??0.0))),
CustomPaint(
size: Size.infinite,
painter: _CircularGaugePainter(context, widget.convert(widget.minValue!), _minDisplay, _maxDisplay, _displayStep)
)
];
if(displayValue != null) {
double steps = widget.maxValue! - widget.minValue!;
double angleStep = (2*pi-(circularGaugeOffset*2))/steps;
double angle = angleStep * (value! - widget.minValue!);
stack.add(CustomPaint(
size: Size.infinite,
painter: _CircularGaugeNeedlePainter(angle)
));
}
return Container(padding: const EdgeInsets.all(5.0), child: RepaintBoundary(child: Stack(children: stack)));
}
}
class _BarGaugePainter extends CustomPainter {
final BuildContext _context;
final int _minValue;
final int _maxValue;
final int _step;
final double? _value;
_BarGaugePainter(this._context, this._minValue, this._maxValue, this._step, this._value);
@override
void paint(Canvas canvas, Size canvasSize) {
ThemeData theme = Theme.of(_context);
double w = canvasSize.width;
double h = canvasSize.height;
Paint paint = Paint()
..style = PaintingStyle.fill
..color = Colors.blue;
if(_value != null) {
double step = h/(_maxValue - _minValue);
canvas.drawRect(Rect.fromLTRB(35, h-(step*(_value-_minValue)), w, h), paint);
}
paint.color = theme.colorScheme.onSurface;
TextPainter tp = TextPainter(textDirection: TextDirection.ltr);
try {
double steps = (_maxValue - _minValue) / _step;
double lineStep = h / steps;
for (int i = 0; i <= steps; ++i) {
canvas.drawRect(Rect.fromLTWH(0, h-(lineStep*i)-1, w, 2), paint);
tp.text = TextSpan(
text: (i*_step+_minValue).toInt().toString(),
style: theme.textTheme.bodyMedium?.copyWith(backgroundColor: theme.colorScheme.surface));
tp.layout();
tp.paint(canvas, Offset(5, h-(i*lineStep)-(tp.size.height/2)));
}
} finally {
tp.dispose();
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
abstract class DoubleValueBarGaugeBox extends DoubleValueGaugeBox {
const DoubleValueBarGaugeBox(super.config, super.title, super.path, {super.minValue = 0, required super.maxValue, required super.step, super.key});
@override
DoubleValueBarGaugeBoxState createState() => DoubleValueBarGaugeBoxState();
}
class DoubleValueBarGaugeBoxState<T extends DoubleValueBarGaugeBox> extends DoubleValueGaugeBoxState<T> {
@override
Widget build(BuildContext context) {
const double pad = 5.0;
return Column(mainAxisAlignment: MainAxisAlignment.start, children: [
Row(children: [Padding(padding: const EdgeInsets.all(pad), child: Text(widget.title, style: Theme.of(context).textTheme.titleMedium))]),
Expanded(child: Padding(padding: const EdgeInsets.all(pad),
child: RepaintBoundary(child: CustomPaint(
size: Size.infinite,
painter: _BarGaugePainter(context, _minDisplay, _maxDisplay, _displayStep, displayValue)
)))),
Row(children: [Padding(padding: const EdgeInsets.all(pad), child: Text(widget.units(value??0), style: Theme.of(context).textTheme.titleMedium))]),
]);
}
}