- # use timer to move one of the objects around
- self.timer = wx.Timer()
- self.timer.SetOwner(self)
- self.Bind(wx.EVT_TIMER, self.OnTimer)
- self.timer.Start(50)
- self.movingids = random.sample(self.objids, MOVING_COUNT)
- self.velocitydict = {}
- for id in self.movingids:
- vx = random.randint(1,5) * random.choice([-1,1])
- vy = random.randint(1,5) * random.choice([-1,1])
- self.velocitydict[id] = (vx,vy)
-
- def OnTimer(self, event):
- # get the current position
- xv, yv = self.GetViewStart()
- dx, dy = self.GetScrollPixelsPerUnit()
- x, y = (xv * dx, yv * dy)
- w, h = self.GetClientSizeTuple()
- clip = wx.Rect(x,y,w,h)
- refreshed = False
- for id in self.movingids:
- r = self.pdc.GetIdBounds(id)
- # get new object position
- (vx,vy) = self.velocitydict[id]
- x = r.x + vx
- y = r.y + vy
- # check for bounce
- if x < 0:
- x = -x
- vx = -vx
- if x >= W:
- x = W - (x - W)
- vx = -vx
- if y < 0:
- y = -y
- vy = -vy
- if y >= H:
- y = H - (y - H)
- vy = -vy
- self.velocitydict[id] = (vx,vy)
- # get change
- dx = x - r.x
- dy = y - r.y
- # translate the object
- self.pdc.TranslateId(id, dx, dy)
- # redraw
- r.x -= 20
- if dx < 0:
- r.x = x
- r.y -= 20
- if dy < 0:
- r.y = y
- r.width += abs(dx) + 40
- r.height += abs(dy) + 40
- if r.Intersects(clip):
- r.x -= clip.x
- r.y -= clip.y
- refreshed = True
+ # vars for handling mouse clicks
+ self.dragid = -1
+ self.lastpos = (0,0)
+
+ def ConvertEventCoords(self, event):
+ xView, yView = self.GetViewStart()
+ xDelta, yDelta = self.GetScrollPixelsPerUnit()
+ return (event.GetX() + (xView * xDelta),
+ event.GetY() + (yView * yDelta))
+
+ def OffsetRect(self, r):
+ xView, yView = self.GetViewStart()
+ xDelta, yDelta = self.GetScrollPixelsPerUnit()
+ r.OffsetXY(-(xView*xDelta),-(yView*yDelta))
+
+ def OnMouse(self, event):
+ global hitradius
+ if event.LeftDown():
+ x,y = self.ConvertEventCoords(event)
+ #l = self.pdc.FindObjectsByBBox(x, y)
+ l = self.pdc.FindObjects(x, y, hitradius)
+ for id in l:
+ if not self.pdc.GetIdGreyedOut(id):
+ self.dragid = id
+ self.lastpos = (event.GetX(),event.GetY())
+ break
+ elif event.RightDown():
+ x,y = self.ConvertEventCoords(event)
+ #l = self.pdc.FindObjectsByBBox(x, y)
+ l = self.pdc.FindObjects(x, y, hitradius)
+ if l:
+ self.pdc.SetIdGreyedOut(l[0], not self.pdc.GetIdGreyedOut(l[0]))
+ r = self.pdc.GetIdBounds(l[0])
+ r.Inflate(4,4)
+ self.OffsetRect(r)