Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CalibrateGrayscaleControl.axaml.cs « Calibrators « Controls « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 02a7a810eb8b6af75e085c0d6d64e43bd990f850 (plain)
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
using System.Timers;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using UVtools.Core.Operations;
using UVtools.WPF.Controls.Tools;
using UVtools.WPF.Extensions;
using UVtools.WPF.Windows;

namespace UVtools.WPF.Controls.Calibrators
{
    public class CalibrateGrayscaleControl : ToolControl
    {
        public OperationCalibrateGrayscale Operation => BaseOperation as OperationCalibrateGrayscale;

        private Bitmap _previewImage;
        public Bitmap PreviewImage
        {
            get => _previewImage;
            set => RaiseAndSetIfChanged(ref _previewImage, value);
        }

        private readonly Timer _timer;
        
        public CalibrateGrayscaleControl()
        {
            BaseOperation = new OperationCalibrateGrayscale(SlicerFile);
            if (!ValidateSpawn()) return;

            InitializeComponent();

            
            _timer = new Timer(20)
            {
                AutoReset = false
            };
            _timer.Elapsed += (sender, e) => Dispatcher.UIThread.InvokeAsync(UpdatePreview);
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public override void Callback(ToolWindow.Callbacks callback)
        {
            if (App.SlicerFile is null) return;
            switch (callback)
            {
                case ToolWindow.Callbacks.Init:
                case ToolWindow.Callbacks.Loaded:
                    Operation.PropertyChanged += (sender, e) =>
                    {
                        _timer.Stop();
                        _timer.Start();
                        if (e.PropertyName == nameof(Operation.Divisions))
                        {
                            ParentWindow.ButtonOkEnabled = Operation.Divisions > 0;
                            return;
                        }
                    };
                    ParentWindow.ButtonOkEnabled = Operation.Divisions > 0;
                    _timer.Stop();
                    _timer.Start();
                    break;
            }
        }
        public void UpdatePreview()
        {
            var layers = Operation.GetLayers();
            _previewImage?.Dispose();
            PreviewImage = layers[2].ToBitmap();
            foreach (var layer in layers)
            {
                layer.Dispose();
            }
        }
    }
}