Skip to content

Instantly share code, notes, and snippets.

@AliveDevil
Created October 7, 2012 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliveDevil/8772192bd0e63c6e65cd to your computer and use it in GitHub Desktop.
Save AliveDevil/8772192bd0e63c6e65cd to your computer and use it in GitHub Desktop.
Relative Calculation
public struct Margin {
public int? Left;
public int? Right;
public int? Top;
public int? Bottom;
public Margin ( int? side ) {
Left = side;
Right = side;
Top = side;
Bottom = side;
}
public Margin ( int? height, int? width ) {
Left = width;
Right = width;
Top = height;
Bottom = height;
}
public Margin ( int? left, int? right, int? top, int? bottom ) {
Left = left;
Right = right;
Top = top;
Bottom = bottom;
}
}
Private Function getRelativePosition(ByVal left As Integer, ByVal right As Integer, ByVal top As Integer, ByVal bottom As Integer, ByVal height As Integer, ByVal width As Integer) As Point
Dim ret As Point
If left <> -1 Then
ret.X = left
Else
If right <> -1 Then
ret.X = clipRectangle.Width - (right + width)
Else
ret.X = 0
End If
End If
If top <> -1 Then
ret.Y = top
Else
If bottom <> -1 Then
ret.Y = clipRectangle.Height - (bottom + height)
Else
ret.Y = 0
End If
End If
Return ret
End Function
Private Function getRelativeSize(ByVal left As Integer, ByVal right As Integer, ByVal top As Integer, ByVal bottom As Integer, ByVal height As Integer, ByVal width As Integer) As Size
Dim ret As Size
If right <> -1 Then
If left <> -1 Then
ret.Width = clipRectangle.Width - (left + right)
Else
ret.Width = width
End If
Else
ret.Width = width
End If
If bottom <> -1 Then
If top <> -1 Then
ret.Height = clipRectangle.Height - (top + bottom)
Else
ret.Height = height
End If
Else
ret.Height = height
End If
Return ret
End Function
Private Function getRelativeRectangle(ByVal left As Integer, ByVal right As Integer, ByVal top As Integer, ByVal bottom As Integer, ByVal height As Integer, ByVal width As Integer) As Rectangle
Return New Rectangle(getRelativePosition(left, right, top, bottom, height, width), getRelativeSize(left, right, top, bottom, height, width))
End Function
public static class UiConversion {
public static Vector2 RelativePosition ( int? width, int? height, Margin margin, Viewport viewport ) {
Vector2 retPos = new Vector2();
if ( margin.Left.HasValue )
retPos.X = margin.Left.Value;
else
if ( margin.Right.HasValue && width.HasValue )
retPos.X = viewport.Width - ( margin.Right.Value + width.Value );
else
retPos.X = 0;
if ( margin.Top.HasValue )
retPos.Y = margin.Top.Value;
else
if ( margin.Bottom.HasValue && height.HasValue )
retPos.Y = viewport.Height - ( margin.Bottom.Value + height.Value );
else
retPos.Y = 0;
return retPos;
}
public static Vector2 RelativeSize ( int? width, int? height, Margin margin, Viewport viewport ) {
Vector2 retVect = new Vector2();
if ( margin.Right.HasValue ) {
if ( margin.Left.HasValue ) {
retVect.X = viewport.Width - ( margin.Right.Value + margin.Left.Value );
} else {
if ( width.HasValue ) {
retVect.X = width.Value;
} else {
retVect.X = 0;
}
}
} else {
if ( width.HasValue ) {
retVect.X = width.Value;
} else {
retVect.X = 0;
}
}
if ( margin.Bottom.HasValue ) {
if ( margin.Top.HasValue ) {
retVect.Y = viewport.Height - ( margin.Bottom.Value + margin.Top.Value );
} else {
if ( height.HasValue ) {
retVect.Y = height.Value;
} else {
retVect.Y = 0;
}
}
} else {
if ( height.HasValue ) {
retVect.Y = height.Value;
} else {
retVect.Y = 0;
}
}
return retVect;
}
public static Rectangle RelativeRectangle ( int? width, int? height, Margin margin, Viewport viewport ) {
Vector2 pos = RelativePosition( width, height, margin, viewport );
Vector2 size = RelativeSize( width, height, margin, viewport );
return new Rectangle( (int) pos.X, (int) pos.Y, (int) size.X, (int) size.Y );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment