forked from Torabi/GHCustomControls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiToggleSwitchItem.cs
180 lines (137 loc) · 6.44 KB
/
MultiToggleSwitchItem.cs
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
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
Original work Copyright (c) 2021 Ali Torabi ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
*/
namespace GHCustomControls
{
public class MultiToggleSwitchItem : GHControl
{
public readonly int Value;
public bool ShowTitle = false;
readonly Image _image;
readonly StringFormat _format;
public bool IsSelected = false;
public override RectangleF Bounds { get ; set ; }
public override int Offset => 6;
public MultiToggleSwitchItem(string name,string description,int value, Image image ):base(name,description)
{
Value = value;
_image = image;
_format = new StringFormat();
_format.Alignment = StringAlignment.Near;
_format.LineAlignment = StringAlignment.Near;
_format.Trimming = StringTrimming.EllipsisCharacter;
}
/// <summary>
/// Width of the item
/// </summary>
/// <returns></returns>
internal override int GetWidth()
{
int width =ShowTitle ? GH_FontServer.StringWidth(Name, SmallFont) : 0;
return (_image == null) ? Offset + width : Offset + _image.Width + width;
}
/// <summary>
/// Height of the item
/// </summary>
/// <returns></returns>
internal override int GetHeight()
{
return (_image == null)? 24 : 4 + _image.Height;
}
internal override void Render(Graphics graphics, PointF cursorCanvasPosition, bool selected, bool locked, bool hidden)
{
//RectangleF rec = new RectangleF(Bounds.X + Offset / 3f, Bounds.Y + Offset / 3f, Bounds.Width - 2 * Offset / 3f, Bounds.Height - 2 * Offset / 3f);
if (IsSelected)
{
GH_Capsule capsule = GH_Capsule.CreateCapsule(Bounds, GH_Palette.Transparent, 2, 0);
capsule.Render(graphics,
(Enabled && !locked)?
new GH_PaletteStyle(Color.AntiqueWhite, Color.SlateGray)
:
new GH_PaletteStyle(Color.Transparent, Color.SlateGray)
);
capsule.Dispose();
//graphics.DrawRectangle(new Pen(Brushes.Black,2.5f), Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);
}else if (Bounds.Contains(cursorCanvasPosition) && Enabled )
{
GH_Capsule capsule = GH_Capsule.CreateCapsule(Bounds, GH_Palette.Transparent, 2, 0);
capsule.Render(graphics, new GH_PaletteStyle(Color.Transparent));
capsule.Dispose();
}
//graphics.DrawRectangle(new Pen(Brushes.Black), Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);
if (ShowTitle)
{
if (_image != null)
{
graphics.DrawImage(_image, Bounds.X + Offset / 3.0f, Bounds.Y + Offset / 3.0f);
graphics.DrawString(Name, SmallFont, this.ActiveBrush(), _image.Width + Bounds.X + 2 * Offset / 3.0f, Bounds.Y + Offset / 3.0f, _format);
}
else
{
graphics.DrawString(Name, SmallFont, this.ActiveBrush(), Bounds.X + 2 * Offset / 3.0f, Bounds.Y + Offset / 3.0f, _format);
}
}
else
{
if (_image != null)
graphics.DrawImage(_image, Bounds.X+Bounds.Width/2f - _image.Width/2f, Bounds.Y + Offset / 3.0f);
else
graphics.DrawString(Name, SmallFont,this.ActiveBrush(), Bounds.X + 2 * Offset / 3.0f, Bounds.Y + Offset / 3.0f, _format);
}
}
#region mouse events
//internal override void MouseOver(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
//{
// //if (Highlighted==0)
// // return;
// //result = result | GHMouseEventResult.Invalidated ;
// //Highlighted = 0;
//}
//internal override void MouseLeave(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
//{
// //if (Highlighted!=0)
// // return;
// //Highlighted = -1;
// result = result | GHMouseEventResult.Invalidated;
//}
internal override void MouseLeftClick(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
//if (Bounds.Contains(e.CanvasLocation))
//{
// // clicked on this toggle
// if (IsSelected)
// return; // already selected nothing happens
// IsSelected = true;
// result = result | GHMouseEventResult.Handled | GHMouseEventResult.UpdateSolution;
//}
//else {
// // outside of this toggle
// if (!IsSelected)
// return; // nothing happens
// IsSelected = false;
// result = result | GHMouseEventResult.Invalidated;
//}
}
internal override void MouseRightClick(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
}
#endregion
}
}