Skip to content

Commit ca529fc

Browse files
committed
- Color.FromHsb added;
- controls' focusing replaced to graphics class, more cleaner code in input controls; - fixed controlPicker alpha button; - fixed(?) HideRowHeaders method in tableView control;
1 parent cfd5a3f commit ca529fc

File tree

8 files changed

+276
-378
lines changed

8 files changed

+276
-378
lines changed

System/Drawing/Color.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static implicit operator ColorF(Color self)
4545
}
4646

4747
// TODO: as null.
48-
public static readonly Color Empty = new Color() { _a = 0, _r = 0, _g = 0, _b = 0, _isEmpty = true };
48+
public static readonly Color Empty = new Color() { _a = 0, _r = 0, _g = 0, _b = 0, _isEmpty = true };
4949

5050
public bool IsEmpty { get { return _isEmpty; } }
5151

@@ -204,6 +204,32 @@ public static Color FromArgb(int a, int r, int g, int b)
204204
if (a < 0) a = 0;
205205
return new Color() { _a = (byte)a, _r = (byte)r, _g = (byte)g, _b = (byte)b, _isEmpty = false };
206206
}
207+
public static Color FromHsb(byte hue, byte saturation, byte brigthness)
208+
{
209+
double dh = (double)hue / 255;
210+
double ds = (double)saturation / 255;
211+
double db = (double)brigthness / 255;
212+
return FromHsb(dh, ds, db);
213+
}
214+
public static Color FromHsb(double hue, double saturation, double brigthness)
215+
{
216+
double r = 0, g = 0, b = 0;
217+
if (brigthness != 0)
218+
{
219+
if (saturation == 0)
220+
r = g = b = brigthness;
221+
else
222+
{
223+
double temp2 = _GetTemp2(hue, saturation, brigthness);
224+
double temp1 = 2.0f * brigthness - temp2;
225+
226+
r = _GetColorComponent(temp1, temp2, hue + 1.0f / 3.0f);
227+
g = _GetColorComponent(temp1, temp2, hue);
228+
b = _GetColorComponent(temp1, temp2, hue - 1.0f / 3.0f);
229+
}
230+
}
231+
return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
232+
}
207233
public static Color FromUColor(UnityEngine.Color color)
208234
{
209235
return Color.FromArgb((int)(color.a * 255), (int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255));
@@ -343,5 +369,35 @@ public UnityEngine.Color ToUColor()
343369
{
344370
return new UnityEngine.Color((float)_r / 255, (float)_g / 255, (float)_b / 255, (float)_a / 255);
345371
}
372+
373+
private static double _GetColorComponent(double temp1, double temp2, double temp3)
374+
{
375+
temp3 = _MoveIntoRange(temp3);
376+
if (temp3 < 1.0f / 6.0f)
377+
return temp1 + (temp2 - temp1) * 6.0f * temp3;
378+
else if (temp3 < 0.5f)
379+
return temp2;
380+
else if (temp3 < 2.0f / 3.0f)
381+
return temp1 + ((temp2 - temp1) * ((2.0f / 3.0f) - temp3) * 6.0f);
382+
else
383+
return temp1;
384+
}
385+
private static double _GetTemp2(double h, double s, double l)
386+
{
387+
double temp2;
388+
if (l < 0.5f)
389+
temp2 = l * (1.0f + s);
390+
else
391+
temp2 = l + s - (l * s);
392+
return temp2;
393+
}
394+
private static double _MoveIntoRange(double temp3)
395+
{
396+
if (temp3 < 0.0f)
397+
temp3 += 1.0f;
398+
else if (temp3 > 1.0f)
399+
temp3 -= 1.0f;
400+
return temp3;
401+
}
346402
}
347403
}

System/Drawing/Graphics.cs

Lines changed: 67 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static PointF GetBezierPoint(float t, PointF[] controlPoints, int index,
5353
var P1 = GetBezierPoint(t, controlPoints, index + 1, count - 1);
5454
return new PointF((1 - t) * P0.X + t * P1.X, (1 - t) * P0.Y + t * P1.Y);
5555
}
56-
private static int GUI_SetLabelFont(Font font)
56+
private static int GUI_SetFont(GUIStyle style, Font font)
5757
{
5858
int guiSkinFontSizeBuffer = GUI.skin.label.fontSize;
5959
if (font != null)
@@ -62,36 +62,36 @@ private static int GUI_SetLabelFont(Font font)
6262
font.UFont = System.Windows.Forms.ApplicationBehaviour.Resources.Fonts.Find(f => f.fontNames[0] == font.Name);
6363

6464
if (font.UFont != null)
65-
GUI.skin.label.font = font.UFont;
65+
style.font = font.UFont;
6666
else
6767
{
68-
GUI.skin.label.font = null;
68+
style.font = null;
6969
UnityEngine.Debug.LogError("Font not found: " + font.Name);
7070
}
7171

72-
GUI.skin.label.fontSize = (int)(font.Size);
72+
style.fontSize = (int)(font.Size);
7373
bool styleBold = (font.Style & FontStyle.Bold) == FontStyle.Bold;
7474
bool styleItalic = (font.Style & FontStyle.Italic) == FontStyle.Italic;
7575
if (styleBold)
7676
{
7777
if (styleItalic)
78-
GUI.skin.label.fontStyle = UnityEngine.FontStyle.BoldAndItalic;
78+
style.fontStyle = UnityEngine.FontStyle.BoldAndItalic;
7979
else
80-
GUI.skin.label.fontStyle = UnityEngine.FontStyle.Bold;
80+
style.fontStyle = UnityEngine.FontStyle.Bold;
8181
}
8282
else if (styleItalic)
83-
GUI.skin.label.fontStyle = UnityEngine.FontStyle.Italic;
84-
else GUI.skin.label.fontStyle = UnityEngine.FontStyle.Normal;
83+
style.fontStyle = UnityEngine.FontStyle.Italic;
84+
else style.fontStyle = UnityEngine.FontStyle.Normal;
8585
}
8686
else
8787
{
8888
if (ApplicationBehaviour.Resources.Fonts.Count > 0)
8989
{
9090
var _font = ApplicationBehaviour.Resources.Fonts[0];
9191
if (_font != null)
92-
GUI.skin.label.font = _font;
93-
GUI.skin.label.fontSize = (int)(12);
94-
GUI.skin.label.fontStyle = UnityEngine.FontStyle.Normal;
92+
style.font = _font;
93+
style.fontSize = (int)(12);
94+
style.fontStyle = UnityEngine.FontStyle.Normal;
9595
}
9696
}
9797
return guiSkinFontSizeBuffer;
@@ -339,6 +339,34 @@ public void DrawPolygon(Pen pen, Point[] points)
339339
GL.End();
340340
}
341341
}
342+
public void DrawRectangle(Color color, float x, float y, float width, float height)
343+
{
344+
if (NoRects) return;
345+
if (color.A <= 0) return;
346+
347+
GUI.color = color.ToUColor();
348+
349+
if (Control != null)
350+
Control.Batches += 2;
351+
352+
GUI.DrawTexture(new Rect(x, y, width, 1), System.Windows.Forms.ApplicationBehaviour.DefaultSprite);
353+
GUI.DrawTexture(new Rect(x + width - 1, y + 1, 1, height - 2), System.Windows.Forms.ApplicationBehaviour.DefaultSprite);
354+
FillRate += width + height - 2;
355+
if (height > 1)
356+
{
357+
if (Control != null)
358+
Control.Batches++;
359+
GUI.DrawTexture(new Rect(x, y + height - 1, width, 1), System.Windows.Forms.ApplicationBehaviour.DefaultSprite);
360+
FillRate += width * 1 + 1;
361+
}
362+
if (width > 1)
363+
{
364+
if (Control != null)
365+
Control.Batches++;
366+
GUI.DrawTexture(new Rect(x, y + 1, 1, height - 2), System.Windows.Forms.ApplicationBehaviour.DefaultSprite);
367+
FillRate += height - 2;
368+
}
369+
}
342370
public void DrawRectangle(Pen pen, Rectangle rect)
343371
{
344372
DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
@@ -475,7 +503,7 @@ public void DrawString(string s, Font font, Color color, float x, float y, float
475503
break;
476504
}
477505

478-
int guiSkinFontSizeBuffer = GUI_SetLabelFont(font);
506+
int guiSkinFontSizeBuffer = GUI_SetFont(GUI.skin.label, font);
479507
GUI.color = color.ToUColor();
480508
GUI.Label(new Rect(x, y, width, height), s);
481509

@@ -545,6 +573,10 @@ public void DrawString(string s, Font font, SolidBrush brush, RectangleF layoutR
545573
DrawString(s, font, brush, layoutRectangle.X, layoutRectangle.Y, layoutRectangle.Width, layoutRectangle.Height, format);
546574
}
547575
public string DrawTextArea(string s, Font font, SolidBrush brush, float x, float y, float width, float height)
576+
{
577+
return DrawTextArea(s, font, brush.Color, x, y, width, height);
578+
}
579+
public string DrawTextArea(string s, Font font, Color color, float x, float y, float width, float height)
548580
{
549581
if (Control == null) return s;
550582
if (s == null) s = "";
@@ -553,55 +585,28 @@ public string DrawTextArea(string s, Font font, SolidBrush brush, float x, float
553585

554586
GUI.skin.textArea.alignment = TextAnchor.UpperLeft;
555587

556-
GUI.color = brush.Color.ToUColor();
588+
GUI.color = color.ToUColor();
557589
//GUI.skin.textArea.hover.textColor = brush.Color.ToUColor();
558-
if (font != null)
559-
{
560-
var _font = System.Windows.Forms.ApplicationBehaviour.Resources.Fonts.Find(f => f.fontNames[0] == font.Name);
561-
if (_font != null)
562-
GUI.skin.textArea.font = _font;
563-
else
564-
GUI.skin.textArea.font = null;
565-
GUI.skin.textArea.fontSize = (int)font.Size;
566-
bool styleBold = (font.Style & FontStyle.Bold) == FontStyle.Bold;
567-
bool styleItalic = (font.Style & FontStyle.Italic) == FontStyle.Italic;
568-
if (styleBold)
569-
{
570-
if (styleItalic)
571-
GUI.skin.textArea.fontStyle = UnityEngine.FontStyle.BoldAndItalic;
572-
else
573-
GUI.skin.textArea.fontStyle = UnityEngine.FontStyle.Bold;
574-
}
575-
else if (styleItalic)
576-
GUI.skin.textArea.fontStyle = UnityEngine.FontStyle.Italic;
577-
else GUI.skin.textArea.fontStyle = UnityEngine.FontStyle.Normal;
578-
}
579-
else
580-
{
581-
var _font = System.Windows.Forms.ApplicationBehaviour.Resources.Fonts.Find(f => f.fontNames[0] == "Arial");
582-
if (_font != null)
583-
GUI.skin.textArea.font = _font;
584-
GUI.skin.textArea.fontSize = 12;
585-
}
590+
591+
GUI_SetFont(GUI.skin.textArea, font);
586592

587593
GUI.skin.textArea.hover.background = null;
588594
GUI.skin.textArea.active.background = null;
589595
GUI.skin.textArea.focused.background = null;
590596
GUI.skin.textArea.normal.background = null;
591597

592-
if (!_group)
598+
if (Control.shouldFocus)
599+
FocusNext();
600+
601+
var val = GUI.TextArea(new Rect(x, y, width, height), s);
602+
603+
if (Control.shouldFocus)
593604
{
594-
var c_position = Control.PointToScreen(Point.Zero);
595-
return GUI.TextArea(new Rect(c_position.X + x, c_position.Y + y, width, height), s);
605+
Focus();
606+
Control.shouldFocus = false;
596607
}
597-
else
598-
{
599-
var c_position = Control.PointToScreen(Point.Zero);
600-
var g_position = _groupControlLast.PointToScreen(Point.Zero);
601-
var position = c_position - g_position + new PointF(x, y);
602608

603-
return GUI.TextArea(new Rect(position.X, position.Y, width, height), s);
604-
}
609+
return val;
605610
}
606611
public string DrawTextField(string s, Font font, SolidBrush brush, RectangleF layoutRectangle, HorizontalAlignment alignment)
607612
{
@@ -628,54 +633,26 @@ public string DrawTextField(string s, Font font, Color color, float x, float y,
628633
break;
629634
}
630635

631-
if (font != null)
632-
{
633-
var _font = System.Windows.Forms.ApplicationBehaviour.Resources.Fonts.Find(f => f.fontNames[0] == font.Name);
634-
if (_font != null)
635-
GUI.skin.textField.font = _font;
636-
else
637-
GUI.skin.textField.font = null;
638-
GUI.skin.textField.fontSize = (int)font.Size;
639-
bool styleBold = (font.Style & FontStyle.Bold) == FontStyle.Bold;
640-
bool styleItalic = (font.Style & FontStyle.Italic) == FontStyle.Italic;
641-
if (styleBold)
642-
{
643-
if (styleItalic)
644-
GUI.skin.textField.fontStyle = UnityEngine.FontStyle.BoldAndItalic;
645-
else
646-
GUI.skin.textField.fontStyle = UnityEngine.FontStyle.Bold;
647-
}
648-
else if (styleItalic)
649-
GUI.skin.textField.fontStyle = UnityEngine.FontStyle.Italic;
650-
else GUI.skin.textField.fontStyle = UnityEngine.FontStyle.Normal;
651-
}
652-
else
653-
{
654-
var _font = System.Windows.Forms.ApplicationBehaviour.Resources.Fonts.Find(f => f.fontNames[0] == "Arial");
655-
if (_font != null)
656-
GUI.skin.textField.font = _font;
657-
GUI.skin.textField.fontSize = 12;
658-
}
636+
GUI_SetFont(GUI.skin.textField, font);
659637

660638
GUI.color = color.ToUColor();
661639
GUI.skin.textField.hover.background = null;
662640
GUI.skin.textField.active.background = null;
663641
GUI.skin.textField.focused.background = null;
664642
GUI.skin.textField.normal.background = null;
665643

666-
if (!_group)
644+
if (Control.shouldFocus)
645+
FocusNext();
646+
647+
var val = GUI.TextField(new Rect(x, y, width, height), s);
648+
649+
if (Control.shouldFocus)
667650
{
668-
var c_position = Control.PointToScreen(Point.Zero);
669-
return GUI.TextField(new Rect(c_position.X + x, c_position.Y + y, width, height), s);
651+
Focus();
652+
Control.shouldFocus = false;
670653
}
671-
else
672-
{
673-
var c_position = Control.PointToScreen(Point.Zero);
674-
var g_position = _groupControlLast.PointToScreen(Point.Zero);
675-
var position = c_position - g_position + new PointF(x, y);
676654

677-
return GUI.TextField(new Rect(position.X, position.Y, width, height), s);
678-
}
655+
return val;
679656
}
680657
public string DrawTextField(string s, Font font, SolidBrush brush, float x, float y, float width, float height, HorizontalAlignment alignment)
681658
{
@@ -835,7 +812,7 @@ public SizeF MeasureString(string text, Font font)
835812
}
836813
public static SizeF MeasureStringStatic(string text, Font font)
837814
{
838-
int guiSkinFontSizeBuffer = GUI_SetLabelFont(font);
815+
int guiSkinFontSizeBuffer = GUI_SetFont(GUI.skin.label, font);
839816

840817
var size = GUI.skin.label.CalcSize(new GUIContent(text));
841818

0 commit comments

Comments
 (0)