aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Frontend/GUI
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-07-03 08:55:21 -0400
committerMichael <[email protected]>2017-07-03 08:55:21 -0400
commit68362b4c6fe2e740ac02f7b4cb1ae77de3cfbd09 (patch)
treea346b092eee671ab9fae32cbca23d22b171c748b /ShiftOS.Frontend/GUI
parent6f3a5cba2ea08ea6f442e2336b74f32f4bbc0604 (diff)
downloadshiftos_thereturn-68362b4c6fe2e740ac02f7b4cb1ae77de3cfbd09.tar.gz
shiftos_thereturn-68362b4c6fe2e740ac02f7b4cb1ae77de3cfbd09.tar.bz2
shiftos_thereturn-68362b4c6fe2e740ac02f7b4cb1ae77de3cfbd09.zip
major optimizations, draggable windows
Diffstat (limited to 'ShiftOS.Frontend/GUI')
-rw-r--r--ShiftOS.Frontend/GUI/Button.cs9
-rw-r--r--ShiftOS.Frontend/GUI/Control.cs171
-rw-r--r--ShiftOS.Frontend/GUI/PictureBox.cs2
-rw-r--r--ShiftOS.Frontend/GUI/ProgressBar.cs2
-rw-r--r--ShiftOS.Frontend/GUI/TextControl.cs4
-rw-r--r--ShiftOS.Frontend/GUI/TextInput.cs2
6 files changed, 171 insertions, 19 deletions
diff --git a/ShiftOS.Frontend/GUI/Button.cs b/ShiftOS.Frontend/GUI/Button.cs
index c2e41a3..551d0d4 100644
--- a/ShiftOS.Frontend/GUI/Button.cs
+++ b/ShiftOS.Frontend/GUI/Button.cs
@@ -25,14 +25,13 @@ namespace ShiftOS.Frontend.GUI
using (var gfx = Graphics.FromImage(new Bitmap(1, 1)))
{
var measure = gfx.MeasureString(this.Text, this.Font);
- Width = borderwidth + (int)measure.Width + 4;
- Height = borderwidth + (int)measure.Height + 8;
+ Width = borderwidth + (int)measure.Width + 16;
+ Height = borderwidth + (int)measure.Height + 12;
}
}
- base.OnLayout();
}
- public override void Paint(Graphics gfx)
+ protected override void OnPaint(Graphics gfx)
{
Color bgCol = SkinEngine.LoadedSkin.ButtonBackgroundColor;
Color fgCol = SkinEngine.LoadedSkin.ControlTextColor;
@@ -43,7 +42,7 @@ namespace ShiftOS.Frontend.GUI
gfx.Clear(bgCol);
gfx.DrawRectangle(new Pen(new SolidBrush(fgCol), SkinEngine.LoadedSkin.ButtonBorderWidth), new Rectangle(0, 0, Width, Height));
- base.Paint(gfx);
+ base.OnPaint(gfx);
}
}
diff --git a/ShiftOS.Frontend/GUI/Control.cs b/ShiftOS.Frontend/GUI/Control.cs
index fcbd429..90e7ba5 100644
--- a/ShiftOS.Frontend/GUI/Control.cs
+++ b/ShiftOS.Frontend/GUI/Control.cs
@@ -30,6 +30,63 @@ namespace ShiftOS.Frontend.GUI
private bool _focused = false;
private bool _autoSize = false;
private double _opacity = 1.0;
+ private bool _invalidated = true;
+ private Bitmap _texCache = null;
+ private Anchor _anchor = null;
+ private int _mouseX = 0;
+ private int _mouseY = 0;
+ private bool _captureMouse = false;
+
+ public bool CaptureMouse
+ {
+ get
+ {
+ return _captureMouse;
+ }
+ set
+ {
+ _captureMouse = value;
+ }
+ }
+
+ public int MouseX
+ {
+ get
+ {
+ return _mouseX;
+ }
+ }
+
+ public int MouseY
+ {
+ get
+ {
+ return _mouseY;
+ }
+ }
+
+
+ public Anchor Anchor
+ {
+ get
+ {
+ return _anchor;
+ }
+ set
+ {
+ _anchor = value;
+ Invalidate();
+ }
+ }
+
+ public void Invalidate()
+ {
+ _invalidated = true;
+ foreach(var child in _children)
+ {
+ child.Invalidate();
+ }
+ }
public double Opacity
{
@@ -39,7 +96,8 @@ namespace ShiftOS.Frontend.GUI
}
set
{
- _opacity = (double)MathHelper.Clamp((float)value, 0, 1);
+ _opacity = value;
+ Invalidate();
}
}
@@ -107,6 +165,7 @@ namespace ShiftOS.Frontend.GUI
set
{
_visible = value;
+ Invalidate();
}
}
@@ -116,6 +175,7 @@ namespace ShiftOS.Frontend.GUI
{
ctrl._parent = this;
_children.Add(ctrl);
+ Invalidate();
}
}
@@ -154,6 +214,7 @@ namespace ShiftOS.Frontend.GUI
set
{
_x = value;
+ Invalidate();
}
}
@@ -166,6 +227,7 @@ namespace ShiftOS.Frontend.GUI
set
{
_y = value;
+ Invalidate();
}
}
@@ -178,6 +240,7 @@ namespace ShiftOS.Frontend.GUI
set
{
_w = value;
+ Invalidate();
}
}
@@ -190,6 +253,7 @@ namespace ShiftOS.Frontend.GUI
set
{
_h = value;
+ Invalidate();
}
}
@@ -229,6 +293,7 @@ namespace ShiftOS.Frontend.GUI
public void ClearControls()
{
_children.Clear();
+ Invalidate();
}
public Point PointToLocal(int x, int y)
@@ -238,23 +303,56 @@ namespace ShiftOS.Frontend.GUI
public virtual void MouseStateChanged() { }
- public virtual void Paint(System.Drawing.Graphics gfx)
+ protected virtual void OnPaint(Graphics gfx)
+ {
+
+ }
+
+ public void InvalidateTopLevel()
+ {
+ var parent = this;
+ while (parent.Parent != null)
+ parent = parent.Parent;
+ parent.Invalidate();
+ }
+
+ public void Paint(System.Drawing.Graphics gfx)
{
if (_visible == true)
{
- if (_children.Count > 0)
+ if (_invalidated)
+ {
+ _texCache = new Bitmap(Width, Height);
+ using (var cGfx = Graphics.FromImage(_texCache))
+ {
+ OnPaint(cGfx);
+ }
+ _invalidated = false;
+ }
+ gfx.DrawImage(_texCache, 0, 0);
+ foreach (var child in _children)
{
- foreach (var child in _children)
+ if (child.Visible)
{
- using (var cBmp = new System.Drawing.Bitmap(child.Width, child.Height))
+ if (child._invalidated)
{
+ var cBmp = new Bitmap(child.Width, child.Height);
child.Paint(System.Drawing.Graphics.FromImage(cBmp));
cBmp.SetOpacity((float)child.Opacity);
+ child._invalidated = false;
+ child._texCache = cBmp;
gfx.DrawImage(cBmp, new System.Drawing.Point(child.X, child.Y));
+
+
+ }
+ else
+ {
+ gfx.DrawImage(child._texCache, child.X, child.Y);
}
}
}
+
}
}
@@ -263,6 +361,11 @@ namespace ShiftOS.Frontend.GUI
//Dock style
if(_parent != null)
{
+ if(_anchor != null)
+ {
+
+ }
+
switch (_dock)
{
case DockStyle.Top:
@@ -342,8 +445,10 @@ namespace ShiftOS.Frontend.GUI
//Firstly, we get the mouse coordinates in the local space
var coords = PointToLocal(state.Position.X, state.Position.Y);
+ _mouseX = coords.X;
+ _mouseY = coords.Y;
//Now we check if the mouse is within the bounds of the control
- if(coords.X > 0 && coords.Y > 0 && coords.X <= _w && coords.Y <= _h)
+ if(coords.X >= 0 && coords.Y >= 0 && coords.X <= _w && coords.Y <= _h)
{
//We're in the local space. Let's fire the MouseMove event.
MouseMove?.Invoke(coords);
@@ -352,6 +457,7 @@ namespace ShiftOS.Frontend.GUI
{
_wasMouseInControl = true;
MouseEnter?.Invoke();
+ Invalidate();
}
//Things are going to get a bit complicated.
@@ -389,9 +495,17 @@ namespace ShiftOS.Frontend.GUI
fire = true;
}
if (_leftState == true && ld == false)
+ {
Click?.Invoke();
+ Invalidate();
+ }
if (_leftState == false && ld == true)
+ {
+ var focused = UIManager.FocusedControl;
UIManager.FocusedControl = this;
+ focused?.InvalidateTopLevel();
+ InvalidateTopLevel();
+ }
_leftState = ld;
_middleState = md;
_rightState = rd;
@@ -407,12 +521,36 @@ namespace ShiftOS.Frontend.GUI
_middleState = false;
MouseStateChanged();
//If the mouse was in local space before, fire MouseLeave
- if(_wasMouseInControl == true)
+ if (_wasMouseInControl == true)
{
- _wasMouseInControl = false;
- MouseLeave?.Invoke();
+ if (CaptureMouse == true)
+ {
+ _wasMouseInControl = true;
+ int newX = MathHelper.Clamp(state.X, X, X + Width);
+ int newY = MathHelper.Clamp(state.Y, Y, Y + Height);
+ Mouse.SetPosition(newX, newY);
+
+ }
+ else
+ {
+ _wasMouseInControl = false;
+ MouseLeave?.Invoke();
+ Invalidate();
+ }
}
}
+ if (CaptureMouse == true)
+ {
+ _mouseX = coords.X;
+ _mouseY = coords.Y;
+ Layout();
+ _wasMouseInControl = true;
+ int newX = MathHelper.Clamp(state.X, X, X + Width);
+ int newY = MathHelper.Clamp(state.Y, Y, Y + Height);
+ Mouse.SetPosition(newX, newY);
+ return true;
+ }
+
//Mouse is not in the local space, don't do anything.
return false;
}
@@ -486,4 +624,19 @@ namespace ShiftOS.Frontend.GUI
return output;
}
}
+
+ [Flags]
+ public enum AnchorStyle
+ {
+ Top,
+ Left,
+ Bottom,
+ Right
+ }
+
+ public class Anchor
+ {
+ public AnchorStyle Style { get; set; }
+ public int Distance { get; set; }
+ }
}
diff --git a/ShiftOS.Frontend/GUI/PictureBox.cs b/ShiftOS.Frontend/GUI/PictureBox.cs
index 2447416..9a234d4 100644
--- a/ShiftOS.Frontend/GUI/PictureBox.cs
+++ b/ShiftOS.Frontend/GUI/PictureBox.cs
@@ -50,7 +50,7 @@ namespace ShiftOS.Frontend.GUI
}
}
- public override void Paint(Graphics gfx)
+ protected override void OnPaint(Graphics gfx)
{
if(img != null)
switch (_layout)
diff --git a/ShiftOS.Frontend/GUI/ProgressBar.cs b/ShiftOS.Frontend/GUI/ProgressBar.cs
index f0dd626..e35dc27 100644
--- a/ShiftOS.Frontend/GUI/ProgressBar.cs
+++ b/ShiftOS.Frontend/GUI/ProgressBar.cs
@@ -37,7 +37,7 @@ namespace ShiftOS.Frontend.GUI
}
}
- public override void Paint(Graphics gfx)
+ protected override void OnPaint(Graphics gfx)
{
gfx.Clear(LoadedSkin.ProgressBarBackgroundColor);
int w = (int)linear(_value, 0, _maximum, 0, Width);
diff --git a/ShiftOS.Frontend/GUI/TextControl.cs b/ShiftOS.Frontend/GUI/TextControl.cs
index 79da213..37b9ca7 100644
--- a/ShiftOS.Frontend/GUI/TextControl.cs
+++ b/ShiftOS.Frontend/GUI/TextControl.cs
@@ -53,9 +53,9 @@ namespace ShiftOS.Frontend.GUI
set { _textAlign = value; }
}
- public override void Paint(Graphics gfx)
+ protected override void OnPaint(Graphics gfx)
{
- var sMeasure = gfx.MeasureString(_text, _font);
+ var sMeasure = gfx.MeasureString(_text, _font, Width);
PointF loc = new PointF(2, 2);
float centerH = (Width - sMeasure.Width) / 2;
float centerV = (Height - sMeasure.Height) / 2;
diff --git a/ShiftOS.Frontend/GUI/TextInput.cs b/ShiftOS.Frontend/GUI/TextInput.cs
index fc142a0..851f7d2 100644
--- a/ShiftOS.Frontend/GUI/TextInput.cs
+++ b/ShiftOS.Frontend/GUI/TextInput.cs
@@ -37,7 +37,7 @@ namespace ShiftOS.Frontend.GUI
private int _textOffset = 0;
- public override void Paint(Graphics gfx)
+ protected override void OnPaint(Graphics gfx)
{
gfx.Clear(LoadedSkin.ControlColor);