Mouse Drag Multiple Example

Mouse Drag Multiple Example preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

code example 

Tagged by Reuven M. Lerner almost 12 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 376 times • Downloaded 64 times • Run 1 time
Download the 'Mouse Drag Multiple Example' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

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.

Uploaded by When Description Download
Uri Wilensky over 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky about 12 years ago Updated version tag Download this version
Uri Wilensky almost 13 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky over 14 years ago Mouse Drag Multiple Example Download this version
Uri Wilensky over 14 years ago Mouse Drag Multiple Example Download this version

Attached files

File Type Description Last updated
Mouse Drag Multiple Example.png preview Preview for 'Mouse Drag Multiple Example' over 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.