From 4c8e5078e03824597daa797786cdaf9913987277 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 12 Sep 2016 15:44:48 +1000 Subject: [PATCH] Better comments for launcher widgets. --- ClassicalSharp/2D/Widgets/Widget.cs | 22 ++++---- .../Gui/TableWidget/LauncherTableWidget.cs | 3 +- Launcher2/Gui/Widgets/LauncherBoolWidget.cs | 5 +- Launcher2/Gui/Widgets/LauncherButtonWidget.cs | 3 +- Launcher2/Gui/Widgets/LauncherInputWidget.cs | 3 +- Launcher2/Gui/Widgets/LauncherLabelWidget.cs | 3 +- Launcher2/Gui/Widgets/LauncherWidget.cs | 56 ++++++++++++++++--- 7 files changed, 69 insertions(+), 26 deletions(-) diff --git a/ClassicalSharp/2D/Widgets/Widget.cs b/ClassicalSharp/2D/Widgets/Widget.cs index ab61d0539..5abc37dbc 100644 --- a/ClassicalSharp/2D/Widgets/Widget.cs +++ b/ClassicalSharp/2D/Widgets/Widget.cs @@ -22,16 +22,16 @@ namespace ClassicalSharp.Gui { /// Invoked when this widget is clicked on. Can be null. public ClickHandler OnClick; - /// Horizontal coordinate of top left corner in window space. + /// Horizontal coordinate of top left corner in pixels. public int X; - /// Vertical coordinate of top left corner in window space. + /// Vertical coordinate of top left corner in pixels. public int Y; - /// Horizontal length of widget's bounds in window space. + /// Horizontal length of widget's bounds in pixels. public int Width; - /// Vertical length of widget's bounds in window space. + /// Vertical length of widget's bounds in pixels. public int Height; /// Specifies the horizontal reference point for when the widget is resized. @@ -40,25 +40,25 @@ namespace ClassicalSharp.Gui { /// Specifies the vertical reference point for when the widget is resized. public Anchor VerticalAnchor; - /// Horizontal offset from the reference point in window space. + /// Horizontal offset from the reference point in pixels. public int XOffset = 0; - /// Vertical offset from the reference point in window space. + /// Vertical offset from the reference point in pixels. public int YOffset = 0; - /// Width and height of widget in window space. + /// Width and height of widget in pixels. public Size Size { get { return new Size( Width, Height ); } } - /// Coordinate of top left corner of widget's bounds in window space. + /// Coordinate of top left corner of widget's bounds in pixels. public Point TopLeft { get { return new Point( X, Y ); } } - /// Coordinate of bottom right corner of widget's bounds in window space. + /// Coordinate of bottom right corner of widget's bounds in pixels. public Point BottomRight { get { return new Point( X + Width, Y + Height ); } } - /// Specifies the boundaries of the widget in window space. + /// Specifies the boundaries of the widget in pixels. public Rectangle Bounds { get { return new Rectangle( X, Y, Width, Height ); } } - /// Moves the widget to the specified window space coordinates. + /// Moves the widget to the specified pixel coordinates. public virtual void MoveTo( int newX, int newY ) { X = newX; Y = newY; } diff --git a/Launcher2/Gui/TableWidget/LauncherTableWidget.cs b/Launcher2/Gui/TableWidget/LauncherTableWidget.cs index 41d72ca7b..584ccd395 100644 --- a/Launcher2/Gui/TableWidget/LauncherTableWidget.cs +++ b/Launcher2/Gui/TableWidget/LauncherTableWidget.cs @@ -122,7 +122,8 @@ namespace Launcher.Gui.Widgets { public void SetDrawData( IDrawer2D drawer, Font font, Font titleFont, Anchor horAnchor, Anchor verAnchor, int x, int y ) { - CalculateOffset( x, y, horAnchor, verAnchor ); + SetAnchors( horAnchor, verAnchor ).SetOffsets( x, y ) + .CalculatePosition(); view.SetDrawData( drawer, font, titleFont ); } diff --git a/Launcher2/Gui/Widgets/LauncherBoolWidget.cs b/Launcher2/Gui/Widgets/LauncherBoolWidget.cs index d9cc60e70..1ce8a2e7f 100644 --- a/Launcher2/Gui/Widgets/LauncherBoolWidget.cs +++ b/Launcher2/Gui/Widgets/LauncherBoolWidget.cs @@ -18,8 +18,9 @@ namespace Launcher.Gui.Widgets { this.font = font; } - public void SetDrawData( IDrawer2D drawer, Anchor horAnchor, Anchor verAnchor, int x, int y ) { - CalculateOffset( x, y, horAnchor, verAnchor ); + public void SetDrawData( IDrawer2D drawer, Anchor horAnchor, Anchor verAnchor, int x, int y ) { + SetAnchors( horAnchor, verAnchor ).SetOffsets( x, y ) + .CalculatePosition(); } public override void Redraw( IDrawer2D drawer ) { diff --git a/Launcher2/Gui/Widgets/LauncherButtonWidget.cs b/Launcher2/Gui/Widgets/LauncherButtonWidget.cs index 5a56d28e1..e3b0202b4 100644 --- a/Launcher2/Gui/Widgets/LauncherButtonWidget.cs +++ b/Launcher2/Gui/Widgets/LauncherButtonWidget.cs @@ -19,7 +19,8 @@ namespace Launcher.Gui.Widgets { public void SetDrawData( IDrawer2D drawer, string text, Font font, Anchor horAnchor, Anchor verAnchor, int width, int height, int x, int y ) { Width = width; Height = height; - CalculateOffset( x, y, horAnchor, verAnchor ); + SetAnchors( horAnchor, verAnchor ).SetOffsets( x, y ) + .CalculatePosition(); this.font = font; Text = text; diff --git a/Launcher2/Gui/Widgets/LauncherInputWidget.cs b/Launcher2/Gui/Widgets/LauncherInputWidget.cs index 0f991ddf3..b3bc872df 100644 --- a/Launcher2/Gui/Widgets/LauncherInputWidget.cs +++ b/Launcher2/Gui/Widgets/LauncherInputWidget.cs @@ -43,7 +43,8 @@ namespace Launcher.Gui.Widgets { Anchor horAnchor, Anchor verAnchor, int width, int height, int x, int y ) { ButtonWidth = width; ButtonHeight = height; Width = width; Height = height; - CalculateOffset( x, y, horAnchor, verAnchor ); + SetAnchors( horAnchor, verAnchor ).SetOffsets( x, y ) + .CalculatePosition(); Text = text; if( Password ) text = new String( '*', text.Length ); diff --git a/Launcher2/Gui/Widgets/LauncherLabelWidget.cs b/Launcher2/Gui/Widgets/LauncherLabelWidget.cs index 7586804d8..038daa25f 100644 --- a/Launcher2/Gui/Widgets/LauncherLabelWidget.cs +++ b/Launcher2/Gui/Widgets/LauncherLabelWidget.cs @@ -19,7 +19,8 @@ namespace Launcher.Gui.Widgets { Size size = drawer.MeasureSize( ref args ); Width = size.Width; Height = size.Height; - CalculateOffset( x, y, horAnchor, verAnchor ); + SetAnchors( horAnchor, verAnchor ).SetOffsets( x, y ) + .CalculatePosition(); Text = text; this.font = font; } diff --git a/Launcher2/Gui/Widgets/LauncherWidget.cs b/Launcher2/Gui/Widgets/LauncherWidget.cs index 9cd0a7d0d..ac0cdcebe 100644 --- a/Launcher2/Gui/Widgets/LauncherWidget.cs +++ b/Launcher2/Gui/Widgets/LauncherWidget.cs @@ -4,28 +4,66 @@ using ClassicalSharp; namespace Launcher.Gui.Widgets { + /// Represents a graphical element/control. public abstract class LauncherWidget { public int X, Y, Width, Height; public LauncherWindow Window; public Action OnClick; + /// The text associated with this widget. public string Text; + /// Specifies the horizontal reference point for when the widget is resized. + public Anchor HorizontalAnchor; + + /// Specifies the vertical reference point for when the widget is resized. + public Anchor VerticalAnchor; + + /// Horizontal offset from the reference point in pixels. + public int XOffset = 0; + + /// Vertical offset from the reference point in pixels. + public int YOffset = 0; + + public LauncherWidget( LauncherWindow window ) { Window = window; } - protected void CalculateOffset( int x, int y, Anchor horAnchor, Anchor verAnchor ) { - if( horAnchor == Anchor.LeftOrTop ) X = x; - else if( horAnchor == Anchor.Centre ) X = x + Window.Width / 2 - Width / 2; - else if( horAnchor == Anchor.BottomOrRight ) X = x + Window.Width - Width; - - if( verAnchor == Anchor.LeftOrTop ) Y = y; - else if( verAnchor == Anchor.Centre ) Y = y + Window.Height / 2 - Height / 2; - else if( verAnchor == Anchor.BottomOrRight ) Y = y + Window.Height - Height; + /// Redraws the contents of this widget. + public abstract void Redraw( IDrawer2D drawer ); + + /// Sets the reference points for when this widget is resized. + public LauncherWidget SetAnchors( Anchor horAnchor, Anchor verAnchor ) { + HorizontalAnchor = horAnchor; + VerticalAnchor = verAnchor; + return this; } - public abstract void Redraw( IDrawer2D drawer ); + /// Sets the offsets from the reference points (anchors) in pixels. + public LauncherWidget SetOffsets( int xOffset, int yOffset ) { + XOffset = xOffset; + YOffset = yOffset; + return this; + } + + /// Calculates the position of this widget in the window, + /// based on its anchor points and offset from the anchor points. + public LauncherWidget CalculatePosition() { + X = CalcPos( HorizontalAnchor, XOffset, Width, Window.Width ); + Y = CalcPos( VerticalAnchor, YOffset, Height, Window.Height ); + return this; + } + + static int CalcPos( Anchor anchor, int offset, int size, int axisLen ) { + if( anchor == Anchor.LeftOrTop ) + return offset; + if( anchor == Anchor.Centre ) + return offset + axisLen / 2 - size / 2; + if( anchor == Anchor.BottomOrRight ) + return offset + axisLen - size; + return 0; + } } }