Results 1 to 16 of 16

Thread: [RESOLVED] Semi-transparent menu over the top of XNA graphics

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Resolved [RESOLVED] Semi-transparent menu over the top of XNA graphics

    Hello friends. I am making a 2D sports game using XNA graphics. I would like to create a drop down menu from which the user can select various tactics. The menu will be a panel so that I can add in various sliders and check boxes, and it will drop down when the user clicks or hovers over a button.

    Since this drop down menu will overlap with the ground, I would like for the panel to be semi-transparent so the user can still see the game in the background.

    Does anyone here have any experience with this? If so, it would be greatly appreciated

  2. #2
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Semi-transparent menu over the top of XNA graphics

    I have dabbled with XNA a bit, but I'm not very sure about this but can't you just use images with transparency and/or add transparency to your colors that you are using to draw the menu ?

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Re: Semi-transparent menu over the top of XNA graphics

    Well the menu would just be a panel with various controls and stuff on it. I'm not sure if the XNA bit is really all that relevant, I just need to be able to create a semi-transparent panel that partially shows through whatever is underneath, even if it is XNA.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Semi-transparent menu over the top of XNA graphics

    Why not just create a normal form and alter the opacity?

  5. #5
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Semi-transparent menu over the top of XNA graphics

    To follow on, I guess the above was a fairly vague concept...

    Create a form which has no border, has reduced opacity, has several labels which act as the drop down menu buttons and the startposition is dictated by the other windows position.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Re: Semi-transparent menu over the top of XNA graphics

    I wasn't aware that you could alter the opacity of forms, I'll give that a shot.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Re: Semi-transparent menu over the top of XNA graphics

    Okay, that works very well. I'll just make sure I can do everything fully before marking this thread as resolved.

  8. #8
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Semi-transparent menu over the top of XNA graphics

    Glad I could help

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Re: Semi-transparent menu over the top of XNA graphics

    I'm trying to rep you but it won't let me! Silly forum.

    It looks nice but now I just need code that will close the form whenever I click elsewhere. I tried using the LostFocus routine from the form but it did not work.

  10. #10
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Semi-transparent menu over the top of XNA graphics

    How about for the translucent form:

    vbnet Code:
    1. Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    2.         Me.Focus()
    3.     End Sub
    4.  
    5.     Private Sub Form2_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
    6.         Me.Close()
    7.     End Sub

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Re: Semi-transparent menu over the top of XNA graphics

    Hmm I wrote this last night but must have forgotten to click Post:

    I have tried the code you suggested but it still does not work. When I click elsewhere on the main form the popup menu form disappears but it does not close, it simply goes behind the main form.

  12. #12

    Re: Semi-transparent menu over the top of XNA graphics

    Programmin: XNA has the ability, in the content pipeline, to allow you to have pixels of a certain color drawn transparent. You can look into that possibly?

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Re: Semi-transparent menu over the top of XNA graphics

    Visual basic has very poor support for xna, at least with what I have seen. Despite trying I haven't been able to get the content pipeline going. Also it is not xna drawing the menu, the menu just goes over the top.

  14. #14
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Semi-transparent menu over the top of XNA graphics

    Here's an idea: use a timer to close the form if the mouse stays outside it for a number of seconds. A Windows.Forms.Timer is no good for this purpose because it stops ticking when the form is minimized. So instead use a System.Timers.Timer with AutoReset set to True (by default it is a one-shot timer). It ticks on a separate thread, and continues ticking even when the form is hidden. To close or fade the form from the timer's Elapsed event, you have to invoke a delegate.

    Here's an example where the form (Form1) starts fading as soon as the mouse leaves it and closes after 10 seconds, regardless of whether the form is visible. It resets to its original opacity if the mouse reenters the form. I haven't tried it in combination with XNA.

    vb Code:
    1. Private WithEvents tim As New System.Timers.Timer With {.Interval = 100, .AutoReset = True, .Enabled = False}
    2.     Private millisecondsLeft As Integer
    3.  
    4.     Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    5.         tim.Enabled = False
    6.         Cursor.Position = Me.PointToScreen(New Point(50, 50))
    7.         millisecondsLeft = 10000
    8.         Me.Opacity = 0.8F
    9.     End Sub
    10.  
    11.     Private Sub Form1_MouseEnter(sender As Object, e As System.EventArgs) Handles Me.MouseEnter
    12.         tim.Enabled = False
    13.         millisecondsLeft = 10000
    14.         Me.Opacity = 0.8F
    15.     End Sub
    16.  
    17.     Private Sub Form1_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
    18.         tim.Enabled = True
    19.     End Sub
    20.  
    21.     Private Sub tim_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles tim.Elapsed
    22.         millisecondsLeft -= 100    
    23.         Dim fod As New FadeOutDelegate(AddressOf FadeOut)
    24.         Me.Invoke(fod)
    25.     End Sub
    26.  
    27.     Private Delegate Sub FadeOutDelegate()
    28.     Private Sub FadeOut()
    29.         Me.Opacity = Math.Max(Me.Opacity - 0.02F, 0.05F)
    30.         If millisecondsLeft <= 0 Then Me.Close()
    31.     End Sub

    BB

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Re: Semi-transparent menu over the top of XNA graphics

    BB,

    Thank you kindly for the suggestion. About to hit the hay but will test it out tomorrow and let you know how it goes! Cheers.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    117

    Re: Semi-transparent menu over the top of XNA graphics

    BB,

    I have actually changed my code so that the popup menu form stays visible until the button is clicked again. Things are working fine now. Thank you for your suggestion though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width