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

Author Topic: [VB.NET] Mouse events for custom drawn gfx controls.  (Read 9167 times)

0 Members and 1 Guest are viewing this topic.

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
[VB.NET] Mouse events for custom drawn gfx controls.
« on: May 12, 2016, 03:02:35 pm »
Hey guys!  I ran into too many issues regarding lack of documentation for all of the SFML control libraries I found, so I set out to create my own that may work a bit easier for what I am doing.  I have created several generic controls that I would like to enhance and be able to throw in a dll and reuse with other projects. 

I am using a picturebox as my RenderWindow.  Currently I check the mouse events for that picturebox, and send them through to Rectangles that I have setup for the bounds of my drawn objects...this may sound simple, but it is often a very tedious way to get custom drawn controls from scratch to function properly, especially when I can have a dozen controls on the RenderWindow.  It would be so much easier if I could just set all the methods for the mouse events in the drawn object class itself, but I have tried adding handlers to these drawn controls with no effect when I try to use them.

Recently I found additional options for getting input in my application loop.

This can get mouse events specifically for my form:
        If WinForm.MouseButtons = (MouseButtons.Right) Then
            MsgBox("Right button pressed.")
        End If
 

This can get mouse events while the application is running, on my form or outside of it:
        If Mouse.IsButtonPressed(Mouse.Button.Right) Then
            MsgBox("Right button pressed.")
        End If
 

There is pretty much no way to determine if the mouse wheel is/has been scrolled though.  Regardless, these still are not proper ways to get my controls to function properly from the classes themselves.  Over the last few days I have been able to piece together a basic understanding of making the events work from my control classes, but with the SFML documentation in cpp, and very few examples in C# that will not port to VB.Net, I am stuck on making any progress.

I feel like I am very close to making these work as intended, so I am hoping fresh eyes may be able to point me in the right direction.

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #1 on: May 12, 2016, 04:08:13 pm »
For anyone else who has ran across this post in their extensive SFML/VB.NET searches...

Public WithEvents MyWindow As RenderWindow
 

Public Sub Main_KeyPressed(ByVal sender As Object, ByVal e As SFML.Window.KeyEventArgs) Handles MyWindow.KeyPressed
       
    MsgBox("KeyPressed " & e.Code & " on Main...")

End Sub

Public Sub Main_MouseWheel(ByVal sender As Object, ByVal e As MouseWheelEventArgs) Handles MyWindow.MouseWheelMoved

    MsgBox("MouseWheelMoved " & e.Delta & " on Main...")

End Sub
 

Private Sub MyPicturebox_MouseHover(sender As Object, e As EventArgs) Handles MyPicturebox.MouseHover

    MyPicturebox.Focus()

End Sub
 

However, this is still not a solution for custom drawn control events.  But is getting me closer  ;D

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #2 on: May 13, 2016, 02:44:02 pm »
Maybe I should add additional code and clarify what I am trying to do?

Public WithEvents MyWindow As RenderWindow
 

Here are the 3 primary subs I am using for my mouse events.
Private Sub MyWindow_MouseButtonPressed(sender As Object, e As MouseButtonEventArgs) Handles MyWindow.MouseButtonPressed

    MyControlMouseDown(sender, e)

End Sub

Private Sub MyWindow_MouseButtonReleased(sender As Object, e As MouseButtonEventArgs) Handles MyWindow.MouseButtonReleased

    MyControlMouseUp(sender, e)

End Sub

Private Sub MyWindow_MouseMoved(sender As Object, e As MouseMoveEventArgs) Handles MyWindow.MouseMoved

    MyControlMouseMove(sender, e)

End Sub
 

When any of that ^ is done, I am sending the event through to my controls, then running the following:
Public Sub MyControlMouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)

    If MyButton.ControlRectangle.Contains(e.X, e.Y) Then

        MyButton.MouseUp(sender, e)

        ' Fire off events here that happen when the mouse button is up on this control.

    End If

End Sub
 

The above is like the middle man, and sends events through to other control events.  When a control, like my button, is sent a mouse up event, it stops drawing the button down gfx I have to show that the button is being hit.

Regardless if my buttons (drawn controls) are declared with events or not, I have to use this middleman procedure to pass events off to them, unless I want to try and fire everything off in the main mouse event subs...and with the amount of custom drawn controls I have, would be way too many to shove in there.

So I am looking for a better way to go about this, particularly being able to have the drawn objects (controls) be able to handle their own events.

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #3 on: June 10, 2016, 03:39:05 am »
Coming back to this, I have been looking up how to apply a callback function to my custom control, but I am unable to figure this out.  Maybe I rambled on too much in the above posts?  I have sought assistance on other forums, but keep being referred here because no one knows what SFML is, or how to assist.  I'd like to start fresh and see if I can get a pointer, either C# or VB.net, on how to do this.

I'm making a custom control class library, essentially drawn primitives on a render window, to mimic a control such as a button.

Traditional event handlers do not work, so I am having to push the location of the mouse through the render window's event to the location of the button, check if the mouse is over or clicked(down), then set the image over/down for the button.

Moving the mouse with any speed will not toggle the mouse up/leave events, which causes the button to appear to still be hovered or clicked down on the button.  Note the horizontal scroll bars left button:



Every example of a callback that I can find refer to calling back to a .dll file.  I am really lost.  I have noticed other control libraries that allow the functionality for the mouse events to happen without going through what I am, plus there are no issues with the mouse leave events not firing.

Hopefully that can explain my issue better.  I will give someone an awesome hi-five if they can explain what I need to do, or if there is something else that I did not know to look up ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #4 on: June 10, 2016, 08:46:02 am »
It's not clear. You're talking about callbacks (?) then about the mouse released event not triggered. Maybe it's just me, but I have no idea what you're asking ;D
Laurent Gomila - SFML developer

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #5 on: June 10, 2016, 04:10:40 pm »
Thanks.  I guess I am explaining the problem right in my mind, or I am probably using the wrong terminology LOL!

Here are a few articles that explain what I am talking about, with examples in C++/C#/VB:
MSDN: Implement Callback Functions
MSDN: EventHandler Delegate

To be honest, I am not even sure those are what I am looking for because I have not been able to get them to work for this purpose.

When I make a new instance of my control in code I want to figure out how to have it perform like a traditional control - mouse hover or mouse clicked automatically works on the control, so it will change it's image.

Other controls that use a button, such as the scrollbar, will be able to click and fire the event to move the thumb button.

When I move the mouse over the render window, and I hover over a button, it will change a boolean MouseIsOver so when an application call the buttons draw method of the control it displays the proper image.

Because this is a predominant C++ forum I have not uploaded a test project with my control library project because I did not believe it would be of any use because it is in VB.  If it will help I will upload it.


Note: I am working on a few beginner tutorials in VB.NET to post at a programming forum so new programmers will get to know more about SFML and how to use it.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #6 on: June 10, 2016, 04:23:34 pm »
Ok, I see. And where exactly is your problem in implementing this stuff for your controls?
Laurent Gomila - SFML developer

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #7 on: June 10, 2016, 04:48:08 pm »
Ok, I see. And where exactly is your problem in implementing this stuff for your controls?

I have tried implementing event handlers and they would never work.  I have never worked with callback functions, but tried to implement those for this purpose, and they would not work either.

So first, I want to make sure one of those is the appropriate method I must do to make this work, and I am not blindly looking over the "proper" way to implement this functionality.  I have had this issue for about a month now, and am afraid I am going to waste more time when it will not work at all the ways I have been trying.

Once I am certain I am trying to go about this the proper way I can reevaluate what I am currently trying to do and see how it is wrong.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #8 on: June 10, 2016, 05:41:35 pm »
Events should be the correct thing for implementing events ;)

If you can't make them work, show your code so that we can help you to fix it.
Laurent Gomila - SFML developer

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #9 on: June 10, 2016, 06:16:55 pm »
Here is the general idea of how my button class is setup with the handlers:
Public Class MyButton

    Public Event MouseMove(sender As Object, e As MouseMoveEventArgs)

    Public MouseHover As Boolean

    Public MouseDown As Boolean


    Public Sub New()

        AddHandler MouseMove, AddressOf ButtonMouseMove

    End Sub


    Public Sub ButtonMouseMove(sender As Object, e As MouseMoveEventArgs) Handles Me.MouseMove

        RaiseEvent MouseMove(sender, e)

        If Not Visible And Not Enabled Then Exit Sub

        If MouseDown = False Then

            MouseHover = True

        Else

            MouseHover = False

        End If

    End Sub


    Public Sub DrawButton(ByVal target As RenderWindow)

        ' Draw base button image here...

        Dim _rectHighlight As New RectangleShape

        If MouseHover = True Then

            ' Set the hover image

        ElseIf MouseDown = True Then

            ' Set the down image

        End If

        target.Draw(_rectHighlight)

    End Sub
 

Currently the way I have to handle this:

RenderWindow mouse move event: Check if the location of the mouse is in the bounds of the button, then set the Button.MouseHover = True

Something tells me my logic for this is wrong.  One button instance doesn't take that much code.  However, going this way and I want to use a scrollbar, I have to send the mouse location through to that control which is then checking the location of the mouse in relation to it's controls and if it is on one of it's buttons then turn on/off the highlight image there.

The more complex the control, the more checks I have to go through, and the more tedious it is to setup.  I believe this is why it keeps "missing" the mouse leave events, which cause the button to stay highlighted.

« Last Edit: June 10, 2016, 06:40:20 pm by Recoil »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #10 on: June 10, 2016, 06:52:55 pm »
I'm not a VB expert, but I already see two mistakes in your code.

1. "AddHandler" is for adding an event handler dynamically. "Handles" is for adding an event handler statically. So using both to register the same handler is obviously not needed.

2. ButtonMouseMove handles the MouseMove event. The MouseMove event is triggered by ButtonMouseMove. Seems like an endless recursion.

MouseMove belongs to MyButton, it is triggered by it. So you don't have to write a handler for it, this event is made for users of your class, not the class itself.

What you should do is write a HandleMouseMove function, which will be called by the GUI (or parent control, or whatever). This function will change the internal state of the button, and finally raise the MouseMove event for anyone interested in it.
Laurent Gomila - SFML developer

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #11 on: June 10, 2016, 07:26:35 pm »
Thanks Laurent, that does clarify things quite a bit.  I did not think I needed to "AddHandler" in the class itself.  So I removed that from my Sub New() in my button class.

What you should do is write a HandleMouseMove function, which will be called by the GUI (or parent control, or whatever). This function will change the internal state of the button, and finally raise the MouseMove event for anyone interested in it.

Are you referring to writing a function in the button's class itself, or in the GUI?  If it is in the GUI I believe that was what I am doing now, checking the mouse position in the RenderWindow.MouseMove event then changing the state of the button that way.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #12 on: June 10, 2016, 09:57:20 pm »
Quote
Are you referring to writing a function in the button's class itself
That.

The function in GUI is the entry point, then the GUI finds the control under the mouse cursor and forwards the event to it.
Laurent Gomila - SFML developer

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #13 on: June 10, 2016, 10:18:22 pm »
I'm either really confused, or already doing that I believe:

    Private Sub _mRenderWindow_MouseMoved(sender As Object, e As MouseMoveEventArgs) Handles _mRenderWindow.MouseMoved

        If TestButton.ButtonControlBounds.Contains(e.X, e.Y) Then

            TestButton.ButtonMouseMove(sender, e)

        End If

    End Sub
 

That will already call to set the boolean value here in the actual control:
    Public Sub ButtonMouseMove(sender As Object, e As MouseMoveEventArgs)
       
        If Not Visible And Not Enabled Then Exit Sub

        If MouseDown = False Then

            MouseHover = True

        Else

            MouseHover = False

        End If

    End Sub
 

Then when it draws the button, if True, will draw the highlight as well.

Is this what you are talking about?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [VB.NET] Mouse events for custom drawn gfx controls.
« Reply #14 on: June 10, 2016, 10:34:03 pm »
Yes. But now where is the MouseMove event raised in your button class?

I think you should first clarify the whole process in your head, from main loop with SFML event handling to your button class. Write some pseudo-code or diagrams, and then only proceed with the implementation.

Handling mouse events in a complete GUI with many different controls that may fight for mouse input, can be really complicated. And we're not talking about keyboard focus yet... I've written 2 or 3 GUIs in my developer life, seen the source code of other GUI libraries (like Qt) and clearly, input handling has always been the most difficult part to do right. So take the time to understand what you do, and then design how you want to do it before writing code.
Laurent Gomila - SFML developer