x


How to determine if a slider is present in a scrollview (GUILayout)

EDIT: attempting to state my question more clearly

In my game the mouse scrollwheel zooms the camera in and out.

I have a GUILayout.Label on the screen that is inside a scrollview.

The code looks something like this:

 GUILayout.BeginArea(someRectangle);
 scrollVector = GUILayout.BeginScrollView(scrollVector);
 GUILayout.Label(textOfArbitraryLength);
 GUILayout.EndScrollView();
 GUILayout.EndArea();

If textOfArbitraryLength is long enough (such that it is taller than someRectangle and requires a vertical slider) I would like for mouse scrollwheel input to control the scrolling of the text.

If textOfArbitraryLength is small enough (such that it fits inside of someRectangle without scrolling) I would like for mouse scrollwheel input to control the zooming of the camera.

Is it possible to determine if there is a slider present in the scrollview?

I want to be able to determine when the Camera Control script should ignore scrollwheel input.

Sorry if I did a bad job explaining what I was looking for... And thanks in advance for any help.

original post:

Is it possible to determine whether or not a slider is being used inside a scrollview when using GUILayout?

I have a label inside a scrollview and would like to ignore input from the mouse scrollwheel when the mouse is over the label AND the content is scrollable. In other words, I don't want undesired things to happen in the scene when the user is trying to scroll over the text in the label.

If the content of the label fits within the scrollview (ie there is no slider) I would like to process mouse input as usual. Most of the time this is the case...

I can almost accomplish this by checking to see if the scrollPosition vector's y-value is 0, but not quite. If the user scrolls up too much, there will be undesired effects once the slider hits the top and the scroll wheel keeps moving.

Thanks in advance for any help...

more ▼

asked Apr 20, 2011 at 11:01 PM

jahroy gravatar image

jahroy
3.8k 37 46 88

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Just don't assign the returned value to your scrollposition in that case...
I'll assume JS:

var scrollPosition : Vector2;

function OnGUI() { var newposition = GUILayout.BeginScrollView(scrollPosition); if ("It's allowed to be scrolled") { scrollPosition = newposition; } // [...] GUILayout.EndScrollView(); }

Just to complete the example. Here in C#:

Vector2 scrollPosition;

void OnGUI() { Vector2 newposition = GUILayout.BeginScrollView(scrollPosition); if ("It's allowed to be scrolled") { scrollPosition = newposition; } // [...] GUILayout.EndScrollView(); }

edit

As you said you want the opposite. You want to know when the users mouse cursor is over a scrollview. With GUILayoutUtility.GetLastRect you can query the last layouted rectangle. But that only returns the right Rect in the repaint event, not during the layouting step.

var mouseOverScrollView = false;
function OnGUI()
{
    // [...]
    GUILayout.EndScrollView();
    if (Event.current.type == EventType.Repaint)
    {
        mouseOverScrollView = GUILayoutUtility.GetLastRect().Contains(Input.mousePosition);
    }
}

Another way could be using the tooltip functionality, but unfortunately a scrollview doesn't have a tooltip...

edit
I guess you use wordwrap. I've not tested this, but i guess it should work.

var canScrollText = false;

function OnGUI() { GUILayout.BeginArea(someRectangle); scrollVector = GUILayout.BeginScrollView(scrollVector); GUILayout.Label(textOfArbitraryLength); GUILayout.EndScrollView(); GUILayout.EndArea(); if (Event.current.type == EventType.Repaint) { var labelHeight = GUI.skin.label.CalcHeight(GUIContent(textOfArbitraryLength),someRectangle.width); canScrollText = (labelHeight > someRectangle.height); } }

I think it should be possible to do that for arbitrary content by using GetLastRect on a grouping control inside the scrollview to determine its size. But again, not tested.

Good luck.

more ▼

answered Apr 20, 2011 at 11:08 PM

Bunny83 gravatar image

Bunny83
85.6k 31 169 437

Thanks, but this is not what I'm looking for.

This would conditionally ignore mouse input for the slider. My goal is NOT to effect the scene when the mouse scrollwheel input is intended for a scrollview.

In my game the mouse scrollwheel causes the camera to zoom. I would like to ignore this effect when the user's mouse is over a scrollview area.

Apr 20, 2011 at 11:32 PM jahroy

Ok, that's quite simple, i will add it to my answer ;)

Apr 21, 2011 at 03:48 AM Bunny83

Will that really tell me whether or not there is a slider present? It's very easy to tell if the mouse is over a given rectangle... I'm wondering if you can determine whether or not the contents of the scrollview are large enough to require a slider.

In other words if the contents of scrollview A are taller than scrollview A's rectangle, let scrollwheel input scroll the text. If they are not (if all of the text fits in the rect w/o scrolling) let scrollwheel input zoom the camera.

Is it possible to tell whether or not there's a slider in the scrollview?

Apr 21, 2011 at 04:33 AM jahroy

Thanks for your answers. I apologize if my question was not clear. I just tried to re-word my original post...

Apr 21, 2011 at 05:06 AM jahroy

I finally got around to trying this... Unfortunately it did not work. Part of the problem in my case is the fact that the size of the text in my label changes. It's not only scaled to the screen resolution, but it also gets changed when the user clicks certain buttons. My label also uses a custom GUIStyle, which is probably the real issue. I guess I could temporarily change GUI.skin.label to the actual style I use then change it back after calling CalcHeight. I'll try that next...

Apr 26, 2011 at 03:28 AM jahroy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x133

asked: Apr 20, 2011 at 11:01 PM

Seen: 2652 times

Last Updated: Apr 21, 2011 at 05:04 AM