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

ShadeLayer.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 166f1f4e5af5e6190ef295a4b20de6b758eb6a19 (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
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
using System;
using AppKit;
using CoreAnimation;
using CoreGraphics;
using ObjCRuntime;

namespace Xamarin.PropertyEditing.Mac
{
	class ShadeLayer : ColorEditorLayer
	{
		private const float GripRadius = 4;
		private const float BorderRadius = 3;
		private readonly ChannelEditor saturationEditor = new HsbSaturationChannelEditor ();
		private readonly ChannelEditor brightnessEditor = new HsbBrightnessChannelEditor ();

		public ShadeLayer ()
		{
			AddSublayer (saturationLayer);
			saturationLayer.AddSublayer (brightnessLayer);
			AddSublayer (grip);
			float innerRadius = GripRadius - 1;

			grip.AddSublayer (new CALayer {
				BorderWidth = 1,
				BorderColor = new CGColor (0, 0, 0),
				Frame = new CGRect (
					GripRadius - innerRadius,
					GripRadius - innerRadius,
					innerRadius * 2,
					innerRadius * 2),
				CornerRadius = innerRadius
			});
		}

		public ShadeLayer (NativeHandle handle) : base (handle)
		{
		}

		private readonly CALayer grip = new UnanimatedLayer {
			BorderColor = new CGColor (1, 1, 1),
			BorderWidth = 1,
			CornerRadius = GripRadius,
		};

		private readonly CAGradientLayer brightnessLayer = new UnanimatedGradientLayer {
			Colors = new [] {
					new CGColor (0f, 0f, 0f, 1f),
					new CGColor (0f, 0f, 0f, 0f)
				},
			CornerRadius = BorderRadius,
		};

		private readonly CAGradientLayer saturationLayer = new UnanimatedGradientLayer {
			Colors = new [] {
					new CGColor (1f, 1f, 1f),
					new CGColor (1f, .3f, 0f)
				},
			Actions = new Foundation.NSDictionary (),
			BackgroundColor = NSColor.Black.CGColor,
			BorderColor = new CGColor (.5f, .5f, .5f, .5f),
			BorderWidth = 1,
			CornerRadius = BorderRadius,
		};

		public override void LayoutSublayers ()
		{
			base.LayoutSublayers ();

			this.saturationLayer.Frame = Bounds;
			this.brightnessLayer.Frame = this.saturationLayer.Bounds;
			this.saturationLayer.StartPoint = new CGPoint (0, .5);
			this.saturationLayer.EndPoint = new CGPoint (1, .5);
		}

		public override void UpdateFromModel (EditorInteraction interaction)
		{
			LayoutIfNeeded ();
			var color = interaction.Color;

			var sat = this.saturationEditor.LocationFromColor (this.saturationLayer, color);
			var bright = this.brightnessEditor.LocationFromColor (this.brightnessLayer, color);

			var x = sat.X;
			var y = bright.Y + this.saturationLayer.Frame.Y;

			grip.Frame = new CGRect (x - GripRadius, y - GripRadius, GripRadius * 2, GripRadius * 2);

			var hueColor = interaction.Color.HueColor;
			this.saturationEditor.UpdateGradientLayer (this.saturationLayer, hueColor);
		}

		public override void UpdateFromLocation (EditorInteraction interaction, CGPoint location)
		{
			var loc = location;
			var frame = saturationLayer.Frame;

			if (interaction.ViewModel == null)
				return;
			
			var color = interaction.Color;
			var saturation = this.saturationEditor.ValueFromLocation (this.saturationLayer, loc);
			var brightness = this.saturationEditor.ValueFromLocation (
				this.brightnessLayer,
				new CGPoint (loc.X + brightnessLayer.Frame.X, loc.Y + brightnessLayer.Frame.Y));
			
			interaction.Color = interaction.Color.UpdateHSB (saturation: saturation, brightness: brightness);
		}

		public override void Commit (EditorInteraction interaction)
		{
			interaction.ViewModel.CommitLastColor ();
		}
	}
}