aboutsummaryrefslogtreecommitdiff
path: root/Histacom2.Engine/UI/ClassicButton.cs
blob: 683b71b7792b5c324e8ee6cb1e84e36630d8a1a2 (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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Histacom2.Engine.UI
{
    public class ClassicButton : Control, IButtonControl
    {
        private Color _lightBack;
        private Color _darkBack;

        private bool _pressing = false;

        public DialogResult DialogResult { get; set; }

        public bool AdaptBackColorWithTheme { get; set; }
        public bool AdaptForeColorWithTheme { get; set; }
        public bool AdaptFontWithTheme { get; set; }

        public ClassicButton() : base()
        {
            AdaptBackColorWithTheme = true;
            AdaptForeColorWithTheme = true;
            AdaptFontWithTheme = true;
            if (SaveSystem.currentTheme != null) BackColor = SaveSystem.currentTheme.threeDObjectsColor;
            else BackColor = Color.Silver;
            _lightBack = ControlPaint.Light(BackColor, 50);
            _darkBack = ControlPaint.Dark(BackColor, 50);

            MouseDown += (s, e) => { _pressing = true; Invalidate(); };
            MouseUp += (s, e) => { _pressing = false; Invalidate(); };
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (SaveSystem.currentTheme != null && AdaptBackColorWithTheme) BackColor = SaveSystem.currentTheme.threeDObjectsColor;

            if (AdaptForeColorWithTheme)
            {
                if (SaveSystem.currentTheme != null) ForeColor = SaveSystem.currentTheme.threeDObjectsTextColor;
                else ForeColor = Color.Black;
            }

            if (AdaptFontWithTheme)
            {
                if (SaveSystem.currentTheme != null) Font = SaveSystem.currentTheme.buttonFont;
                else Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular);
            }

            _lightBack = Paintbrush.GetLightFromColor(BackColor);
            _darkBack = Paintbrush.GetDarkFromColor(BackColor);

            var g = e.Graphics;
            g.Clear(BackColor);

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
            
            if (_pressing && Enabled)
            {
                g.FillRectangle(new SolidBrush(_lightBack), new Rectangle(0, 0, Width, Height));
                g.FillRectangle(Brushes.Black, new Rectangle(0, 0, Width - 1, Height - 1));
                g.FillRectangle(new SolidBrush(_darkBack), new Rectangle(1, 1, Width - 2, Height - 2));
                g.FillRectangle(new SolidBrush(BackColor), new Rectangle(2, 2, Width - 3, Height - 3));

                g.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(2, 2, Width - 3, Height - 3), sf);
            }
            else
            {
                g.FillRectangle(Brushes.Black, new Rectangle(0, 0, Width, Height));
                g.FillRectangle(new SolidBrush(_lightBack), new Rectangle(0, 0, Width - 1, Height - 1));
                g.FillRectangle(new SolidBrush(_darkBack), new Rectangle(1, 1, Width - 2, Height - 2));
                g.FillRectangle(new SolidBrush(BackColor), new Rectangle(1, 1, Width - 3, Height - 3));

                if (Enabled) g.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(1, 1, Width - 3, Height - 3), sf);
                else g.DrawString(Text, Font, new SolidBrush(_darkBack), new Rectangle(1, 1, Width - 3, Height - 3), sf);
            }
        }

        public void NotifyDefault(bool value)
        {
            
        }

        public void PerformClick()
        {
            OnClick(EventArgs.Empty);
        }
    }
}