Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing buttons  (Read 1385 times)

0 Members and 1 Guest are viewing this topic.

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Drawing buttons
« on: June 10, 2015, 04:08:52 pm »
I have been drawing text with rectangles for buttons as a temporary solution just to get something up and running.  The rectangles would be used to check the location of the mouse on mouse up, mouse down, and mouse over subs.  This worked but looked horrid...

I started working on creating buttons to replace that.  On the mouse events it checks if the location contains the mouse, then turns on a global variable. (Mouse Move Sub)
        If InBank Then

            If BankLeaveButtonLocation.Contains(e.Location) Then
                If BankLeaveMouseDownButton = False Then
                    BankLeaveMouseOverButton = True
                End If
            Else
                BankLeaveMouseOverButton = False
            End If
 

Then I have to declare the image for the button.
    ' Bank Leave Button
    Public BankLeaveButtonGfx As Texture
    Public BankLeaveButtonGfxInfo As GraphicInfo
    Public TempBankLeaveButtonBitmap As Bitmap
    Public BankLeaveButtonLocation As Rectangle
 

Then I have to initialize the button graphics with the rest of the graphics.
        ' Bank Leave Button
        BankLeaveButtonGfxInfo = New GraphicInfo
        If FileExist(Application.StartupPath & GFX_PATH & BANKWINDOW_PATH & "BankLeaveButton" & GFX_EXT) Then
            TempBankLeaveButtonBitmap = New Bitmap(Application.StartupPath & GFX_PATH & BANKWINDOW_PATH & "BankLeaveButton" & GFX_EXT)
            BankLeaveButtonGfxInfo.Width = TempBankLeaveButtonBitmap.Width
            BankLeaveButtonGfxInfo.Height = TempBankLeaveButtonBitmap.Height
            _transcolor = TempBankLeaveButtonBitmap.GetPixel(0, 0)

            _memStream = New MemoryStream()
            TempBankLeaveButtonBitmap.Save(_memStream, ImageFormat.Png)
            BankLeaveButtonGfx = New Texture(_memStream)
            _memStream.Dispose()
        End If
 

When the panel is drawing, then drawing the button, I have to check what the global variable is first and set the Y position.
        Dim BankLeaveButtonOffset As Integer = 0
        If BankLeaveMouseOverButton = True Then
            BankLeaveButtonOffset = MouseOverOffsetY
        ElseIf BankLeaveMouseDownButton = True Then
            BankLeaveButtonOffset = MouseDownOffsetY
        End If
 

Then draw the button in my render loop.
        ' Bank Leave Button Background
        Dim windowLeaveButtonSprite As Sprite = New Sprite(BankLeaveButtonGfx)
        windowLeaveButtonSprite.TextureRect = New IntRect(0, BankLeaveButtonOffset, TempBankLeaveButtonBitmap.Width, TempBankLeaveButtonBitmap.Height / 3)
        windowLeaveButtonSprite.Position = New Vector2f((windowSprite.Position.X + TempBankWindowBitmap.Width / 2) - (TempBankLeaveButtonBitmap.Width / 2), ((windowSprite.Position.Y + TempBankWindowBitmap.Height) - TempBankLeaveButtonBitmap.Height / 3) - 3)
        BankLeaveButtonLocation = New Rectangle(windowLeaveButtonSprite.Position.X, windowLeaveButtonSprite.Position.Y, windowLeaveButtonSprite.GetLocalBounds().Width, windowLeaveButtonSprite.GetLocalBounds().Height)
        GameWindow.Draw(windowLeaveButtonSprite)
 

Then I dispose of the TempBitmap along with the rest of the TempBitmaps...All that is just drawing 1 button, and only list 1 event that it changes.

However, I have to have several buttons drawn on the screen at various times.  Ideally I should create a class, but I am lost on what I will need to do to accomplish this with what I am doing to draw them now.  Can someone help me figure out what to do?

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Drawing buttons
« Reply #1 on: June 11, 2015, 03:12:46 pm »
I figured out how to make a custom class for this.  Because I need the images to change depending on if the mouse is over, down, or up I inherited a picturebox and tried to use those mouse events, but they will not work.  However, I can set the same boolean values that control the image outside of the class and they work.

Does anyone have an idea why the mouse events of the custom picturebox class will not work when it is drawn to the screen?

 

anything