Wish Rect class should have Width and Height properties

Widget

Well-Known Member
Licensed User
Longtime User
The current Rect class has the properties:
Bottom, CenterX, CenterY, Left, Right, Top

I'd like to see Width and Height properties added so if you set Width, it will automatically adjust the Right property wrt Left property. If you read the Width it will return Right-Left. Similar for Height. So it does not have to store Width or Height, but when writing to Width/Height it will adjust the current Right/Bottom properties. When it reads from Width/Height it will just calculate the value from Right-Left or Bottom-Top.

Example:
B4X:
Rect1.Left   = 100dip
Rect1.Width = 200dip
Rect1.Top   = 10dip
Rect1.Height = 100dip

This makes it easier to use Rect to store view positions (which use Width & Height).
 

Attachments

  • TRectDim.bas
    3.1 KB · Views: 211
Last edited:

ilan

Expert
Licensed User
Longtime User
you could use custom type

B4X:
Sub Process_Globals
    Type my_rect(left As Float, top As Float, width As Float, height As Float)
    Dim rect1 As my_rect
End Sub

Sub Globals
    Private c As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
    rect1.Left   = 100dip
    rect1.Width = 200dip
    rect1.Top   = 10dip
    rect1.Height = 100dip
  
    c.Initialize(Activity)
    c.DrawRect(set_rect(rect1),Colors.Yellow,True,0)
End Sub

Sub set_rect(r As my_rect) As Rect
    Dim newrect As Rect
    newrect.Initialize(r.left,r.top,r.left+r.width,r.top+r.height)
    Return newrect  
End Sub
 

Widget

Well-Known Member
Licensed User
Longtime User
Great minds think alike. :)

I have updated the original message (above) with an attachment TRectDim.bas which is a simple class module. It adds Width and Height properties to Rect and the class is called TRectDim. This simplifies my code and allows me to pass a TRectDim parameter to a sub so I can more easily control the height and width of views. Now both the TRectDim and Views can use Width and Height directly instead of using clumsy arithmetic like Rect1.Right-Rect2.Left) or (Rect1.Bottom-Rect1.Top) which could lead to typos. (My eyes aren't what they used to be.)

B4X:
'This is the old way
View1.Left = Rect1.Left
View1.Width = Rect1.Right-Rect1.Left
View1.Top = Rect1.Top
View1.Height = Rect1.Bottom-Rect1.Top

Rect1.Right = View1.Left + View1.Width
Rect1.Bottom = View1.Top + View1.Height

'This is the new way without any nasty arithmetic that could lead to errors
View1.Left = RectDim1.Left
View1.Width = RectDim1.Width
View1.Top  = RectDim1.Top
View1.Height = RectDim1.Height

RectDim1.Width = View1.Width
RectDim1.Height = View1.Height

The Width and Height properties are of course both Read/Write and will update the Right and Bottom properties accordingly.

I wrote it yesterday after I posted this message and I think it works ok although I haven't thoroughly tested it. Let me know if anyone has any problems with it. Enjoy. ;)
 

Widget

Well-Known Member
Licensed User
Longtime User
I updated this class so when TRectDim.Left and TRectDim.Top is changed, it will automatically keep the same Width and Height like it does with a View.
Enjoy!
 
Top