Namespace UI.GameControls ''' ''' The base class for all UI controls. ''' Public MustInherit Class Control Protected Const TESTFORHEIGHTCHARS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;:_-.,*~+'#1234567890?ß\/!""§$%&/()}][{" Private _text As String = String.Empty Private _isFocused As Boolean = False Private _focusOnClick As Boolean = True 'Visuals: Private _font As SpriteFont Private _fontSize As Single = 1.0F Private _fontColor As Color = Color.Black Private _backColor As Color = Color.White Private _borderColor As Color = Color.Black Private _width As Integer = 120 Private _height As Integer = -1 Private _borderWidth As Integer = 1 Private _position As Drawing.Point = New Drawing.Point(100, 100) Private _visible As Boolean = True Private _createdScreenInstance As Screen ''' ''' Returns the font renderer applicable to the situation. ''' ''' Protected ReadOnly Property FontRenderer() As SpriteBatch Get If _createdScreenInstance.Equals(CurrentScreen) Then Return Core.FontRenderer Else Return Core.SpriteBatch End If End Get End Property Protected Sub New(ByVal createdScreenInstance As Screen) _createdScreenInstance = createdScreenInstance End Sub ''' ''' The text property of this control. ''' ''' Public Property Text() As String Get Return _text End Get Set(value As String) _text = value End Set End Property ''' ''' If the control is focused. ''' ''' Public Property IsFocused() As Boolean Get Return _isFocused End Get Set(value As Boolean) If value Then OnFocused(New EventArgs()) Else OnDeFocused(New EventArgs()) End If End Set End Property ''' ''' The font that renders on this control. ''' ''' Public Property Font() As SpriteFont Get Return _font End Get Set(value As SpriteFont) _font = value End Set End Property ''' ''' The width of this control. ''' ''' Public Property Width() As Integer Get Return _width End Get Set(value As Integer) _width = value End Set End Property ''' ''' The height of this control. ''' ''' Public Property Height As Integer Get Return _height End Get Set(value As Integer) _height = value End Set End Property ''' ''' The position of this control. ''' ''' Public Property Position As Drawing.Point Get Return _position End Get Set(value As Drawing.Point) _position = value End Set End Property ''' ''' The font size of the font of this control. ''' ''' Public Property FontSize As Single Get Return _fontSize End Get Set(value As Single) _fontSize = value End Set End Property ''' ''' The color of the font of this control. ''' ''' Public Property FontColor() As Color Get Return _fontColor End Get Set(value As Color) _fontColor = value End Set End Property ''' ''' The border color of this control. ''' ''' Public Property BorderColor() As Color Get Return _borderColor End Get Set(value As Color) _borderColor = value End Set End Property ''' ''' The background color of this control. ''' ''' Public Property BackColor() As Color Get Return _backColor End Get Set(value As Color) _backColor = value End Set End Property ''' ''' The border width of this control. ''' ''' Public Property BorderWidth() As Integer Get Return _borderWidth End Get Set(value As Integer) _borderWidth = value End Set End Property ''' ''' If this control gets focused when clicked on. ''' ''' Public Property FocusOnClick() As Boolean Get Return _focusOnClick End Get Set(value As Boolean) _focusOnClick = value End Set End Property ''' ''' If the control is visible or not. ''' ''' Public Property Visible() As Boolean Get Return _visible End Get Set(value As Boolean) _visible = value End Set End Property ''' ''' Draws the control. ''' Public Sub Draw() If _visible = True Then DrawClient() End If End Sub Protected Overridable Sub DrawClient() 'Draw in client control. End Sub ''' ''' Updates the control. ''' Public Sub Update() If Controls.Accept(True, False, False) = True Then If MouseInClientArea() Then If _focusOnClick Then IsFocused = True End If If IsFocused Then OnClick(New OnClickEventArgs(ActivationMethod.Click)) End If End If End If If IsFocused Then If Controls.Accept(False, True, False) Then OnClick(New OnClickEventArgs(ActivationMethod.Keyboard)) ElseIf Controls.Accept(False, False, True) Then OnClick(New OnClickEventArgs(ActivationMethod.Controller)) End If End If UpdateClient() End Sub Protected Overridable Sub UpdateClient() 'Update in client control. End Sub Protected Overridable Function GetClientRectangle() As Rectangle Return New Rectangle(Position.X, 'X Position.Y, 'Y Width + (BorderWidth * 2), 'Width Height + (BorderWidth * 2) 'Height ) End Function Protected Function MouseInClientArea() As Boolean Return GetClientRectangle().Contains(MouseHandler.MousePosition) End Function #Region "Events" ''' ''' The event that gets fired when the control gets focused. ''' ''' ''' Public Event Focused(ByVal sender As Object, ByVal e As EventArgs) ''' ''' The event that gets fired when the control loses focus. ''' ''' ''' Public Event DeFocused(ByVal sender As Object, ByVal e As EventArgs) ''' ''' The event that occurrs when the control gets clicked. ''' ''' ''' Public Event Click(ByVal sender As Object, ByVal e As EventArgs) ''' ''' Raises the Focused event. ''' ''' Protected Sub OnFocused(e As EventArgs) _isFocused = True RaiseEvent Focused(Me, e) End Sub ''' ''' Raises the DeFocused event. ''' ''' Protected Sub OnDeFocused(e As EventArgs) _isFocused = False RaiseEvent DeFocused(Me, e) End Sub Protected Sub OnClick(e As OnClickEventArgs) RaiseEvent Click(Me, e) End Sub #End Region End Class ''' ''' The activation method for a click or similar event. ''' Public Enum ActivationMethod Click Keyboard Controller End Enum ''' ''' Event arguments for a click event. ''' Public Class OnClickEventArgs Inherits EventArgs Private _method As ActivationMethod ''' ''' Creates an instance of the OnClickEventArgs class. ''' ''' Public Sub New(ByVal method As ActivationMethod) _method = method End Sub ''' ''' The method used to initiate this click event. ''' ''' Public ReadOnly Property ActivationMethod() As ActivationMethod Get Return _method End Get End Property End Class End Namespace