Author Topic: Widget Bounds and OnHover  (Read 3949 times)

Whippets

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Widget Bounds and OnHover
« on: July 25, 2013, 08:01:52 AM »
Hi,

I've got a context menu that is full of options (labels with colliders). I'd like the context menu to disappear when the mouse moves out of it for which I'm using OnHover(false).

My problem is, when the mouse moves over one of the options, the OnHover(false) event is fired on the context menu holder. My idea is to also check if the mouse is within the bounds of the context menu holder object before closing it.

I'm having trouble finding the mouse position and context menu holder bounds, to tie it all together.
« Last Edit: July 26, 2013, 09:51:38 AM by Whippets »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Widget Bounds and OnHover
« Reply #1 on: July 26, 2013, 03:46:19 AM »
For this I would recommend using the Generic Event Handler (UICamera.genericEventHandler = yourCustomFunction). You will get a copy of all the events inside, letting you properly handle this easily.

Whippets

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Widget Bounds and OnHover
« Reply #2 on: July 26, 2013, 08:40:20 AM »
I found a much easier method, than redirecting all events. Here's a simple method to get the bounds of an object in the same scale as the mouse position given by UICamera.lastTouchPosition

  1. public Vector4 GetBounds()
  2. {
  3.     Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(UICamera.currentCamera.transform, transform);
  4.     float x = bounds.center.x - (bounds.size.x / 2) + (Screen.width / 2);
  5.     float y = bounds.center.y - (bounds.size.y / 2) + (Screen.height / 2);
  6.     float w = bounds.size.x;
  7.     float h = bounds.size.y;   
  8.     return new Vector4(x, y, w, h);
  9. }


Though I've now discovered a new issue with the OnHover event. If the mouse is moved fast, OnHover(false) is never raised. Still working out how to solve this one.
« Last Edit: July 26, 2013, 09:53:01 AM by Whippets »

Lohoris

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Widget Bounds and OnHover
« Reply #3 on: July 14, 2014, 04:00:46 AM »
Though I've now discovered a new issue with the OnHover event. If the mouse is moved fast, OnHover(false) is never raised. Still working out how to solve this one.
FYI: OnHover(false) is also not called if the mouse button is pressed while you are moving the cursor out of the object.