Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » List Item Detector (Detect what item of a list the mouse is hovering over)
- AATPicchu
- Scratcher
25 posts
List Item Detector (Detect what item of a list the mouse is hovering over)
Recently I've been trying to detect what item of a list the mouse is hovering over, since I am currently unaware ow a way to do so.
I am currently aware of the following information about lists:
The part that I need help with, however, would be with the scrolling mechanism of a list. I've been experimenting with the following script:
So, you might be wondering what I need help with. Most of those issues are with the programming, since making the detection system can be quite complex sometimes. Well, those reasons are as follows:
I am currently aware of the following information about lists:
- Each single-lined item is 20 units tall
<touching (item (Variable) of [list v] :: list) ?>
doesn't work, since the script will detect if the sprite is touching the mouse or whatever the list item is (such as typing mouse pointer)
The part that I need help with, however, would be with the scrolling mechanism of a list. I've been experimenting with the following script:
set [(variable) v] to ((280) / (length of [list v] :: list))What the script should do is calculate about what amount of the scroll bar equals one list item. The 280 is an example. Let's say that the list, from the top of the scroll bar to the bottom of it is 280 units. Dividing by the length of the list should, in theory, calculate about how much of the scroll bar is needed to display a specific item. So by setting a variable to that value, one should be able save it for later. Another variable could be used to detect the mouse y position if it is within a certain x-y range (over the scrollbar) until the mouse is down, where the data is sent to another variable (since the first will change as soon as the mouse is up). When clicked, a separate variable could be used to calculate the y value until the mouse it up. Using those, the distance, or units the list was scrolled can be determined, and if multiplied (or maybe divided, my head hurts x) ) with the variable containing the items per unit(s), it should, in theory, determine how many units the list has scrolled and send it to a variable. The variable determining what item the mouse is at (“Detector” in my project) can be added to the variable to get the new list number. Oh yeah, I should probably mention this method only works when the list items showing are complete (like, not having half an item showing at the bottom of the list)
So, you might be wondering what I need help with. Most of those issues are with the programming, since making the detection system can be quite complex sometimes. Well, those reasons are as follows:
- I don't know how to make the variable determining what is scrolled to reset and have the items go down if the list scrolls up (I know it would require negative increase detection, I only have a vague idea of what I need to program), rather than having an always-increasing scrolling variable.
- I have no clue how to actually program what I just explained to you in the above paragraph, just what should possibly happen XD I've tried, but without any success. What I've explained is what I've tried to program, but apparently messed up.
Favorite Quote (From the Infinity War trailer ):
“In time, you will know what it's like to lose, to feel so desperately that you're right, yet to fail, all the same. Dread it, run from it, Destiny still arrives.”
(trailer cuz I like the “all the same” more than “nonetheless” for that quote for some reason lol)
- Locomule
- Scratcher
1000+ posts
List Item Detector (Detect what item of a list the mouse is hovering over)
My solution is a lot simpler.
1. Using the
1. Using the
(item ( v) of [list v] :: list)block causes the displayed list to adjust and display the inquired list entry at the top. So use that to adjust the order of the displayed list, then just count Y down from the top list entry to see which one the mouse is over.
- -ShadowOfTheFuture-
- Scratcher
1000+ posts
List Item Detector (Detect what item of a list the mouse is hovering over)
I guess a *very* simple way of doing it is to create a ton of detection sprites and hide them, but that would be extremely inefficient.
You could also calculate the item by using mouse y, I think.
You could also calculate the item by using mouse y, I think.
Last edited by -ShadowOfTheFuture- (Nov. 23, 2017 14:35:05)
<Insert uncreative signature here>
██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
███ ███ ██ ████ ██ ███ ███
█████████ █████ █████ █████████
“Though the seasons come and go, and sunshine turns to snow, we will always have tomorrow up ahead.”
- deck26
- Scratcher
1000+ posts
List Item Detector (Detect what item of a list the mouse is hovering over)
Doesn't always do that though. If the item is already on screen I don't think it changes the view. I just created a list of 22 items with 14 on screen. setting a variable to item 1 did indeed set item 1 to the top of the view but doing the same for item 5 or 12 didn't change anything. My solution is a lot simpler.
1. Using the(item ( v) of [list v] :: list)block causes the displayed list to adjust and display the inquired list entry at the top. So use that to adjust the order of the displayed list, then just count Y down from the top list entry to see which one the mouse is over.
- Locomule
- Scratcher
1000+ posts
List Item Detector (Detect what item of a list the mouse is hovering over)
add extra (dummy, nulls, etc) list items to compensate for list length
to go to 5, go to a higher entry like 10 first
to go to 5, go to a higher entry like 10 first
Last edited by Locomule (Nov. 23, 2017 14:50:44)
- deck26
- Scratcher
1000+ posts
List Item Detector (Detect what item of a list the mouse is hovering over)
OK, it works if you do this add extra list items
add to end of list
delete last of list
set variable to desired item of list
will put the selected item at the top as long as there are enough items to fill the rest of the list on screen. So it all requires a bit of management but is doable.
You need to prevent the user scrolling the list by normal means though.
- Charles12310
- Scratcher
1000+ posts
List Item Detector (Detect what item of a list the mouse is hovering over)
The best way to detect what item of a list the mouse is hovering over is its position.
Let's say you need to detect hovering of item 1, and item 1's x is between 100 and - 100, and item 1's y is between 30 and 35. (This is just a guess) This script will help you detect the first item:
Let's say you need to detect hovering of item 1, and item 1's x is between 100 and - 100, and item 1's y is between 30 and 35. (This is just a guess) This script will help you detect the first item:
show list [list v]
if <<<(mouse x) > [-100]> and <(mouse x) < [100]>> and <<(mouse y) > [30]> and <(mouse y) < [50]>>> then
...
end
A few internet communication companies want to corrupt the internet by getting rid of net neutrality. Stop Them!
- asivi
- Scratcher
1000+ posts
List Item Detector (Detect what item of a list the mouse is hovering over)
- AATPicchu
- Scratcher
25 posts
List Item Detector (Detect what item of a list the mouse is hovering over)
https://scratch.mit.edu/projects/152320490/Precisely what I was looking for! Thanks so much!
Hi, perhaps something there could help(or not).
Greets.
But it could be time consuming to calculate each value's x,y range. I was doing that until I found a slightly more efficient way to find that out The best way to detect what item of a list the mouse is hovering over is its position.
Let's say you need to detect hovering of item 1, and item 1's x is between 100 and - 100, and item 1's y is between 30 and 35. (This is just a guess) This script will help you detect the first item:show list [list v]
if <<<(mouse x) > [-100]> and <(mouse x) < [100]>> and <<(mouse y) > [30]> and <(mouse y) < [50]>>> then
...
end
I've been putting all of the scripts in the backdrop section, so, as you said, many detection sprites could be inefficient. I guess a *very* simple way of doing it is to create a ton of detection sprites and hide them, but that would be extremely inefficient.
You could also calculate the item by using mouse y, I think.
You have a very good point there! I'll look into it. Thanks!OK, it works if you do this add extra list items
add to end of list
delete last of list
set variable to desired item of list
will put the selected item at the top as long as there are enough items to fill the rest of the list on screen. So it all requires a bit of management but is doable.
You need to prevent the user scrolling the list by normal means though.
Last edited by AATPicchu (Nov. 24, 2017 23:40:06)
Favorite Quote (From the Infinity War trailer ):
“In time, you will know what it's like to lose, to feel so desperately that you're right, yet to fail, all the same. Dread it, run from it, Destiny still arrives.”
(trailer cuz I like the “all the same” more than “nonetheless” for that quote for some reason lol)
- Discussion Forums
- » Help with Scripts
- » List Item Detector (Detect what item of a list the mouse is hovering over)