Mouse Drag Multiple Example
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
This is an example of how to use the mouse to select and drag multiple turtles.
NETLOGO FEATURES
We use two breeds of turtle:
- one to represent the agents being selected and dragged
- one to represent the sides of the selection rectangle
You might think that the sides of the selection rectangle might better be represented by links. This would work, but it would have two drawbacks:
A third breed of turtles would be required in order to represent the corners of the box (because links must have turtles to connect).
It would work fine in a non-wrapping world, but in a wrapping world, a large selection rectangle would be drawn incorrectly because the sides would use the shortest possible path to connect the corners, and that path would sometimes wrap around the world boundaries.
RELATED MODELS
Mouse Drag One Example
Comments and Questions
breed [circles circle] ;; the turtles being selected breed [sides side] ;; the four sides of the selection rectangle globals [selected] ;; agentset of currently selected circles to setup clear-all set-default-shape circles "circle" set-default-shape sides "line" create-circles 200 [ set color blue setxy random-xcor random-ycor ] ;; initially, no turtles are selected set selected no-turtles end to go if mouse-down? [ ifelse selected? mouse-xcor mouse-ycor [ handle-drag deselect ] [ handle-select ] ] end to handle-select ;; remember where the mouse pointer was located when ;; the user pressed the mouse button let old-x mouse-xcor let old-y mouse-ycor while [mouse-down?] [ select old-x old-y mouse-xcor mouse-ycor ;; update the view, otherwise the user can't see ;; what's going on display ] ;; if no turtles are selected, kill off ;; the selection rectangle and start over if not any? selected [ deselect ] end to handle-drag ;; remember where the mouse pointer was located when ;; the user pressed the mouse button let old-x mouse-xcor let old-y mouse-ycor if selected? old-x old-y [ while [mouse-down?] [ let new-x mouse-xcor let new-y mouse-ycor ;; we need to move both the selected turtles and the sides ;; of the selection rectangle by the same amount that the ;; mouse has moved. we do this by subtracting the current ;; mouse coordinates from the previous mouse coordinates ;; and adding the results to the coordinates of the turtles ;; and sides. ask selected [ setxy xcor + new-x - old-x ycor + new-y - old-y ] ask sides [ setxy xcor + new-x - old-x ycor + new-y - old-y ] set old-x new-x set old-y new-y ;; update the view, otherwise the user can't see ;; what's going on display ] ] end to deselect ask sides [ die ] ask selected [ set color blue ] set selected no-turtles end to select [x1 y1 x2 y2] ;; x1 y1 is initial corner and x2 y2 is current corner deselect ;; kill old selection rectangle make-side x1 y1 x2 y1 make-side x1 y1 x1 y2 make-side x1 y2 x2 y2 make-side x2 y1 x2 y2 set selected circles with [selected? xcor ycor] ask selected [ set color red ] end to make-side [x1 y1 x2 y2] ;; for each side, one thin line shape is created at the mid point of each segment ;; of the bounding box and scaled to the proper length create-sides 1 [ set color gray setxy (x1 + x2) / 2 (y1 + y2) / 2 facexy x1 y1 set size 2 * distancexy x1 y1 ] end ;; helper procedure that determines whether a point is ;; inside the selection rectangle to-report selected? [x y] if not any? sides [ report false ] let y-max max [ycor] of sides ;; largest ycor is where the top is let y-min min [ycor] of sides ;; smallest ycor is where the bottom is let x-max max [xcor] of sides ;; largest xcor is where the right side is let x-min min [xcor] of sides ;; smallest xcor is where the left side is ;; report whether the input coordinates are within the rectangle report x >= x-min and x <= x-max and y >= y-min and y <= y-max end ; Public Domain: ; To the extent possible under law, Uri Wilensky has waived all ; copyright and related or neighboring rights to this model.
There are 10 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Mouse Drag Multiple Example.png | preview | Preview for 'Mouse Drag Multiple Example' | over 12 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.