Skip to content

Commit 078d3ad

Browse files
committed
less cpu usage, fixed treeview recursive call
1 parent f15dc09 commit 078d3ad

File tree

15 files changed

+208
-156
lines changed

15 files changed

+208
-156
lines changed

System/Drawing/Graphics.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,17 @@ internal string uwfDrawTextField(string s, Font font, SolidBrush brush, float x,
415415
{
416416
return uwfDrawTextField(s, font, brush.Color, x, y, width, height, alignment);
417417
}
418-
internal void uwfDrawTexture(Texture texture, float x, float y, float width, float height, Material mat)
418+
internal void uwfDrawTexture(Texture texture, float x, float y, float width, float height, Material mat = null)
419419
{
420420
uwfDrawTexture(texture, x, y, width, height, Color.White, mat);
421421
}
422-
internal void uwfDrawTexture(Texture texture, float x, float y, float width, float height, Color color, Material mat)
422+
internal void uwfDrawTexture(Texture texture, float x, float y, float width, float height, Color color, Material mat = null)
423423
{
424424
if (texture == null) return;
425425
if (mat != null)
426426
mat.color = color.ToUnityColor();
427+
else
428+
GUI.color = color.ToUnityColor();
427429
UnityEngine.Graphics.DrawTexture(new Rect(x, y, width, height), texture, mat);
428430
}
429431
internal void uwfFillPolygonConvex(SolidBrush brush, PointF[] points)

System/Drawing/Image.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public interface ITexture
2424
int Width { get; }
2525

2626
void Apply();
27+
void Clear(Color color);
2728
Color GetPixel(int x, int y);
2829
Color[] GetPixels();
2930
Color[] GetPixels(int x, int y, int width, int height);

System/Drawing/Point.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public override int GetHashCode()
8787
}
8888
public void Offset(int dx, int dy)
8989
{
90-
X += dx;
91-
Y += dy;
90+
x += dx;
91+
y += dy;
9292
}
9393
public void Offset(Point point)
9494
{

System/Drawing/Rectangle.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,7 @@ public Rectangle(Point location, Size size)
7373
this.width = size.Width;
7474
this.height = size.Height;
7575
}
76-
77-
public Rectangle AddjustX(int x)
78-
{
79-
this.x += x;
80-
return this;
81-
}
82-
public Rectangle AddjustY(int y)
83-
{
84-
this.y += y;
85-
return this;
86-
}
76+
8777
public bool Contains(int x, int y)
8878
{
8979
return this.X <= x && x < this.X + this.Width && this.Y <= y && y < this.Y + this.Height;

System/Windows/Forms/Application.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ public class Application
3737
internal Control hoveredControl;
3838
internal readonly List<Form> ModalForms = new List<Form>();
3939
internal AppGdiImages Resources;
40-
41-
public static bool Debug { get; set; }
42-
public float FillRate { get; set; }
43-
public static bool IsDraging { get { return _dragndrop; } }
44-
public static bool IsStandalone
40+
41+
internal static bool IsDraging { get { return _dragndrop; } }
42+
internal static bool IsStandalone
4543
{
4644
get
4745
{
@@ -52,7 +50,7 @@ public static bool IsStandalone
5250
#endif
5351
}
5452
}
55-
public static float ScaleX
53+
internal static float ScaleX
5654
{
5755
get { return _scaleX; }
5856
set
@@ -62,7 +60,7 @@ public static float ScaleX
6260
_scaleX = 1f;
6361
}
6462
}
65-
public static float ScaleY
63+
internal static float ScaleY
6664
{
6765
get { return _scaleY; }
6866
set
@@ -72,8 +70,8 @@ public static float ScaleY
7270
_scaleY = 1f;
7371
}
7472
}
75-
public static Action<Control> ShowCallback { get; set; }
76-
public bool TabSwitching { get; set; }
73+
internal static Action<Control> ShowCallback { get; set; }
74+
internal bool TabSwitching { get; set; }
7775

7876
private static bool Contains(Control parent, Control child)
7977
{
@@ -110,12 +108,22 @@ private static Control FindControlAt(Control currentControl, System.Drawing.Poin
110108
internal static bool ControlIsVisible(Control control)
111109
{
112110
if (control.Visible == false) return false;
113-
if (control.Location.X + control.uwfOffset.X + control.Width < 0) return false;
114-
if (control.Location.Y + control.uwfOffset.Y + control.Height < 0) return false;
115-
if (control.Parent != null)
111+
112+
var controlLocationX = control.Location.X;
113+
var controlLocationY = control.Location.Y;
114+
var controluwfOffsetX = control.uwfOffset.X;
115+
var controluwfOffsetY = control.uwfOffset.Y;
116+
var controlWidth = control.Width;
117+
var controlHeight = control.Height;
118+
119+
if (controlLocationX + controluwfOffsetX + controlWidth < 0) return false;
120+
if (controlLocationY + controluwfOffsetY + controlHeight < 0) return false;
121+
122+
var controlParent = control.Parent;
123+
if (controlParent != null)
116124
{
117-
if (control.Location.X + control.uwfOffset.X > control.Parent.Width) return false;
118-
if (control.Location.Y + control.uwfOffset.Y > control.Parent.Height) return false;
125+
if (controlLocationX + controluwfOffsetX > controlParent.Width) return false;
126+
if (controlLocationY + controluwfOffsetY > controlParent.Height) return false;
119127
}
120128
return true;
121129
}
@@ -454,9 +462,9 @@ public void ProccessMouse(float mouseX, float mouseY)
454462
// Dispose context first.
455463
for (int i = 0; i < Contexts.Count; i++)
456464
{
457-
if (!Contexts[i].uwfContext) continue;
458-
459465
var contextControl = Contexts[i];
466+
if (!contextControl.uwfContext) continue;
467+
460468
if (Contains(contextControl, hoveredControl)) continue;
461469
if (_mouseEvent != MouseEvents.Down) continue;
462470

@@ -531,12 +539,13 @@ public void Redraw()
531539
ToolTip.OnPaint(_paintEventArgs);
532540

533541
var cursor = Cursor.CurrentSystem ?? Cursor.Current;
542+
var cursorSize = cursor.Size;
534543
cursor.Draw(_paintEventArgs.Graphics,
535544
new Drawing.Rectangle(
536545
Control.MousePosition.X,
537546
Control.MousePosition.Y,
538-
(int)(cursor.Size.Width / ScaleX),
539-
(int)(cursor.Size.Height / ScaleY)));
547+
(int)(cursorSize.Width / ScaleX),
548+
(int)(cursorSize.Height / ScaleY)));
540549
}
541550
public void Run(Control control)
542551
{

System/Windows/Forms/Button.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ protected override void OnPaint(PaintEventArgs e)
9494
{
9595
Graphics g = e.Graphics;
9696

97+
var enabled = Enabled;
98+
var height = Height;
99+
var width = Width;
100+
97101
// Back.
98-
if (Enabled)
102+
if (enabled)
99103
{
100104
if (Hovered == false)
101105
MathHelper.ColorLerp(BackColor, 5, ref cBackColorA, ref cBackColorR, ref cBackColorG, ref cBackColorB);
@@ -107,10 +111,10 @@ protected override void OnPaint(PaintEventArgs e)
107111

108112
currentBackColor = Color.FromArgb((int)cBackColorA, (int)cBackColorR, (int)cBackColorG, (int)cBackColorB);
109113

110-
g.uwfFillRectangle(currentBackColor, 0, 0, Width, Height);
114+
g.uwfFillRectangle(currentBackColor, 0, 0, width, height);
111115

112116
// Border.
113-
if (Enabled == false)
117+
if (enabled == false)
114118
borderPen.Color = BorderDisableColor;
115119
else if (Hovered)
116120
borderPen.Color = BorderHoverColor;
@@ -119,7 +123,7 @@ protected override void OnPaint(PaintEventArgs e)
119123
else
120124
borderPen.Color = BorderColor;
121125

122-
g.DrawRectangle(borderPen, 0, 0, Width, Height);
126+
g.DrawRectangle(borderPen, 0, 0, width, height);
123127

124128
if (Image != null && Image.uTexture != null)
125129
{
@@ -139,26 +143,27 @@ protected override void OnPaint(PaintEventArgs e)
139143
break;
140144
case ImageLayout.Center:
141145
g.uwfDrawImage(imageToPaint, imageColorToPaint,
142-
Width / 2 - imageToPaint.Width / 2,
143-
Height / 2 - imageToPaint.Height / 2,
146+
width / 2 - imageToPaint.Width / 2,
147+
height / 2 - imageToPaint.Height / 2,
144148
imageToPaint.Width,
145149
imageToPaint.Height);
146150
break;
147151
case ImageLayout.Stretch:
148-
g.uwfDrawImage(imageToPaint, imageColorToPaint, 0, 0, Width, Height);
152+
g.uwfDrawImage(imageToPaint, imageColorToPaint, 0, 0, width, height);
149153
break;
150154
case ImageLayout.Zoom:
151155
// TODO: not working.
152156
break;
153157
}
154158
}
155159
var textColor = ForeColor;
156-
if (Enabled == false) textColor = ForeColor + Color.FromArgb(0, 128, 128, 128);
160+
if (enabled == false) textColor = ForeColor + Color.FromArgb(0, 128, 128, 128);
161+
var padding = Padding;
157162
g.uwfDrawString(Text, Font, textColor,
158-
Padding.Left,
159-
Padding.Top,
160-
Width - Padding.Left - Padding.Right,
161-
Height - Padding.Top - Padding.Bottom, TextAlign);
163+
padding.Left,
164+
padding.Top,
165+
width - padding.Horizontal,
166+
height - padding.Vertical, TextAlign);
162167
}
163168
}
164169
}

System/Windows/Forms/ColorPicker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ protected override void OnPaint(PaintEventArgs e)
397397
{
398398
base.OnPaint(e);
399399

400-
e.Graphics.DrawLine(borderPen, 1, HeaderHeight - 1, Width - 1, HeaderHeight - 1);
400+
e.Graphics.DrawLine(borderPen, 1, uwfHeaderHeight - 1, Width - 1, uwfHeaderHeight - 1);
401401
}
402402

403403
public event EventHandler ColorChanged = delegate { };

System/Windows/Forms/Control.cs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static Point MousePosition
3636
internal bool mouseEntered;
3737
internal bool shouldFocus;
3838
private bool _uwfContext;
39-
private bool _visible;
39+
private bool visible;
4040
private int width;
4141
private int x, y;
4242

@@ -59,7 +59,6 @@ public Rectangle Bounds
5959
Size = new Size(value.Width, value.Height);
6060
}
6161
}
62-
6362
public bool CanSelect
6463
{
6564
get { return CanSelectCore(); }
@@ -119,11 +118,11 @@ public int Top
119118
}
120119
public bool Visible
121120
{
122-
get { return _visible; }
121+
get { return visible; }
123122
set
124123
{
125-
bool changed = _visible != value;
126-
_visible = value;
124+
bool changed = visible != value;
125+
visible = value;
127126
if (changed)
128127
VisibleChanged(this, new EventArgs());
129128
}
@@ -172,7 +171,7 @@ public Control()
172171
TabIndex = -1;
173172
TabStop = true;
174173
uwfAutoGroup = true;
175-
_visible = true;
174+
visible = true;
176175

177176
this.SetStyle(ControlStyles.UserPaint | ControlStyles.StandardClick | ControlStyles.Selectable | ControlStyles.StandardDoubleClick | ControlStyles.AllPaintingInWmPaint | ControlStyles.UseTextForAccessibility, true);
178177

@@ -282,20 +281,26 @@ public void PerformLayout()
282281
}
283282
public Point PointToClient(Point p)
284283
{
285-
if (Parent != null)
286-
p = Parent.PointToClient(p);
284+
var parent = Parent;
285+
if (parent != null)
286+
p = parent.PointToClient(p);
287287

288-
p.X -= Location.X + uwfOffset.X;
289-
p.Y -= Location.Y + uwfOffset.Y;
288+
var location = Location;
289+
var localuwfOffset = uwfOffset;
290+
291+
p.Offset(-location.X - localuwfOffset.X, -location.Y - localuwfOffset.Y);
290292
return p;
291293
}
292294
public Point PointToScreen(Point p)
293295
{
294-
if (Parent != null)
295-
p = Parent.PointToScreen(p);
296+
var parent = Parent;
297+
if (parent != null)
298+
p = parent.PointToScreen(p);
296299

297-
p.X += Location.X + uwfOffset.X;
298-
p.Y += Location.Y + uwfOffset.Y;
300+
var location = Location;
301+
var localuwfOffset = uwfOffset;
302+
303+
p.Offset(location.X + localuwfOffset.X, location.Y + localuwfOffset.Y);
299304
return p;
300305
}
301306
public virtual void Refresh()
@@ -352,7 +357,7 @@ protected override void Dispose(bool release_all)
352357

353358
Disposing = true;
354359
OnDisposing(this, EventArgs.Empty);
355-
360+
356361
if (release_all)
357362
{
358363
for (; Controls.Count > 0;)
@@ -688,11 +693,13 @@ internal void RaiseOnPaint(PaintEventArgs e)
688693
int shX = psLoc.X + 6;
689694
int shY = psLoc.Y + 6;
690695
var shadowColor = defaultShadowColor;
691-
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 6, shY + 6, Width - 12, Height - 12);
692-
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 5, shY + 5, Width - 10, Height - 10);
693-
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 4, shY + 4, Width - 8, Height - 8);
694-
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 3, shY + 3, Width - 6, Height - 6);
695-
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 2, shY + 2, Width - 4, Height - 4);
696+
var localWidth = Width;
697+
var localHeight = Height;
698+
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 6, shY + 6, localWidth - 12, localHeight - 12);
699+
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 5, shY + 5, localWidth - 10, localHeight - 10);
700+
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 4, shY + 4, localWidth - 8, localHeight - 8);
701+
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 3, shY + 3, localWidth - 6, localHeight - 6);
702+
pArgs.Graphics.uwfFillRectangle(shadowColor, shX + 2, shY + 2, localWidth - 4, localHeight - 4);
696703
};
697704
}
698705

@@ -701,33 +708,30 @@ internal void RaiseOnPaint(PaintEventArgs e)
701708

702709
if (uwfAutoGroup)
703710
e.Graphics.GroupBegin(this);
704-
711+
705712
OnPaintBackground(e);
706713
OnPaint(e);
707-
if (Controls != null)
708-
for (int i = 0; i < Controls.Count; i++)
714+
715+
var controls = Controls;
716+
if (controls != null)
717+
{
718+
var screenRect = Screen.PrimaryScreen.WorkingArea;
719+
for (int i = 0; i < controls.Count; i++)
709720
{
710-
var childControl = Controls[i];
721+
var childControl = controls[i];
711722
if (Application.ControlIsVisible(childControl) == false) continue;
712723

713724
var currentAbspos = childControl.PointToScreen(Point.Empty);
714-
if (currentAbspos.X + childControl.Width < 0 || currentAbspos.X > Screen.PrimaryScreen.WorkingArea.Width ||
715-
currentAbspos.Y + childControl.Height < 0 || currentAbspos.Y > Screen.PrimaryScreen.WorkingArea.Height)
725+
if (currentAbspos.X + childControl.Width < 0 ||
726+
currentAbspos.X > screenRect.Width ||
727+
currentAbspos.Y + childControl.Height < 0 ||
728+
currentAbspos.Y > screenRect.Height)
716729
continue;
717-
730+
718731
childControl.RaiseOnPaint(e);
719732
}
720-
721-
if (Application.Debug)
722-
{
723-
e.Graphics.uwfDrawString(GetType().Name, Font, Brushes.White, 3, 1, 256, 32);
724-
e.Graphics.uwfDrawString(GetType().Name, Font, Brushes.White, 5, 3, 256, 32);
725-
e.Graphics.uwfDrawString(GetType().Name, Font, Brushes.DarkRed, 4, 2, 256, 32);
726-
727-
var outlinePen = Pens.DarkRed;
728-
if (Focused) outlinePen = new Pen(SystemColors.Highlight);
729-
e.Graphics.DrawRectangle(outlinePen, 0, 0, Width, Height);
730733
}
734+
731735
OnLatePaint(e);
732736

733737
if (uwfAutoGroup)

0 commit comments

Comments
 (0)