[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Question about mouse motion



This is my code for handling mouse in put. I also did this because my
mouse has 5 buttons and Pygame only handles 3 of them. The 4 and 5
buttons are the roller. This code also handles the keyboard in put. It
takes in EVERY event and acts on it, so you must have code that keeps
up or else you will blow the stack. Blowing the stack seems to make
odd things happen to kde even after the program is done. This code is
to a kids photo shop program I am making. Note the calls to the
foodfill peope are helping me with. I may put in code after the flood
fill to clear the event buffer because the fill is slow compared to
the buffer.

I can't say this is the best way because I am new to pygame and python
but it works.
Douglas

PS Watch out for the email induced line wraps.

    eventstates={}
    for a in range(0,320) : eventstates[a]=False #set all the keys
eventstates to false or else the dictionary will be empty and caues an
error
    # color, pattern, pen, background,
    while 1 :
	for e in pygame.event.get(): #set eventstates of keys and mouse to dictionary
#This section is for things that happen because of the change of state
of the button or key
#set eventstates of keys and mouse in dictionary eventstates and take actions
		if e.type == QUIT: return # keys that stop the program
		if e.type == KEYDOWN :#may draw if it is a function key
			eventstates[e.key]=True
			if eventstates[K_RSHIFT]==True : # if Right shift key pressed then
check parents commands
				if eventstates[K_ESCAPE]== True : return #quit program
				if eventstates[K_F1]== True : #help Don't panic in yellow on red
in center of screen
					dirty.append(pygame.draw.rect(screen,(190,190,250),(SCREENRECT.centerx-SCREENRECT.centerx/2,
SCREENRECT.centery-SCREENRECT.centery/2, SCREENRECT.centerx,
SCREENRECT.centery),0))
					dirty.append(pygame.draw.circle(screen,color.get(),SCREENRECT.center,SCREENRECT.centery/2.5,0))
					if pygame.font:
						font = pygame.font.Font(None, 66)
						text = font.render("Don't Panic!", 1, YELLOW)
						textpos = text.get_rect()
						textpos.centerx = screen.get_rect().centerx
						textpos.centery = screen.get_rect().centery
						dirty.append(screen.blit(text, textpos))
				if eventstates[K_F4]== True : # Clear to white
					dirty.append(pygame.draw.rect(screen,BLACK,SCREENRECT,0))
					dirty.append(pygame.draw.rect(background,BLACK,SCREENRECT,0))
				if eventstates[K_F5]== True : # LOAD
					dirty.append(pygame.draw.rect(screen,(250,200,20),(150,250,600,500),0))
				if eventstates[K_F11]== True : # SAVE
					dirty.append(pygame.draw.rect(screen,(200,20,20),(150,150,600,500),0))
		if e.type == KEYUP :#key up never draws
			eventstates[e.key]=False
		if e.type == MOUSEBUTTONDOWN : #only mouse buttons down need to
change mouse feedback and draws on screen
			eventstates[e.button]=True
			firstpos=e.pos
			gui.buttonFeedback(e.button,color)#show the button is pushed
			if e.button==MOUSE1 : #Left Mouse button
				if eventstates[K_LSHIFT] == True :# fill to color
					pattern=pygame.Surface((1, 1)) # make a pattern to draw from
					pattern.fill(color.get()) # make a pattern to draw from
					dirty.append(seed_fillA(screen, e.pos, pattern))
	   			elif eventstates[K_LALT] == True :#fill to pattern
					pattern=load_image('data','Patterns/wax.png')
					dirty.append(seed_fillA(screen, e.pos, pattern))
	   			else:
					dirty.append(pygame.draw.circle(screen,color.get(),e.pos,(gui.getThick()/2),0))#circle
at head of new line.
			if e.button==MOUSE2 : # mouse button center, change activeTool
				gui.IncDecActive(1)
				gui.showActive()
			if e.button==MOUSE3 : #right mouse button, erase with background color
				if eventstates[K_LSHIFT] == True :#fill to background
					dirty.append(seed_fillA(screen, e.pos, background))
				else :
					pygame.draw.rect(screen,color.getbg(),(150,0,100,50),0)#show erase color
					pygame.draw.rect(screen,GRAY,(150,0,100,50),1)#frame it.
					dirty.append(screen.blit(background,(e.pos[0]-erasethick/2,e.pos[1]-erasethick/2),((e.pos[0]-erasethick/2,e.pos[1]-erasethick/2),(erasethick,erasethick))))
			if e.button==MOUSE4 : # mouse button roll up, shrink pen
				if gui.getActive()==1:
					color.IncDec(1)# next color
					gui.colorPickerGui(color)
					gui.pen(color) #show size of brush in color
				if gui.getActive()==0:
					gui.IncDecThick(1)
					gui.pen(color) #show size of the brush and its' color
			if e.button==MOUSE5  :#mouse button roll down, grow pen
				if gui.getActive()==1 :
					color.IncDec(-1)# next color
					gui.colorPickerGui(color)
					gui.pen(color) #show size of brush in color
				if gui.getActive()==0 :
					gui.IncDecThick(-1)
					gui.pen(color) #show pen size
		if e.type == MOUSEBUTTONUP : #mouse buttons up only needs to change
mouse feedback and event states
			eventstates[e.button]=False
			if e.button==MOUSE1 or e.button==MOUSE2 or e.button==MOUSE3 :
gui.buttonFeedback(e.button,color,False) #show button up
			if e.button==MOUSE3 : gui.colorPickerGui(color)#show draw color
			if e.button==MOUSE4 : gui.pen(color)#show pen size
			if e.button==MOUSE5 : gui.pen(color)#show pen size
		if e.type == MOUSEMOTION : #this section is all about line drawing
			if eventstates[MOUSE1]==True and eventstates[K_LSHIFT]== False and
eventstates[K_LALT]== False: #draw a line
				temprect=(pygame.draw.line(screen,color.get(),firstpos,e.pos,gui.getThick()))
				if distance(firstpos,e.pos)>200 : print distance(firstpos,e.pos)#test line
				temprect.inflate_ip(gui.getThick()*2, gui.getThick()*2)#hack to
fix mistake in pygame.draw.line
				dirty.append(temprect)
				if gui.getThick()>4 :# thick lines need endcaps to make it look good
					dirty.append(pygame.draw.circle(screen,color.get(),e.pos,(gui.getThick()/2),0))
			if eventstates[MOUSE3]==True and eventstates[K_LSHIFT] == False and
eventstates[K_LALT]== False :#erase screen back to original
				dirty.append(screen.blit(background,(e.pos[0]-erasethick/2,e.pos[1]-erasethick/2),((e.pos[0]-erasethick/2,e.pos[1]-erasethick/2),(erasethick,erasethick))))
			firstpos=e.pos #set up firstpos for next mousemotion if no new
button 1 down happens
# this next section is for things that happen because of the state of
the button or key
# this section is for stuff that happens for every event
		dirty.append(gui.show(screen)) #  might be able to set this up to
happen less.!!!!
		if dirty != [] :
			pygame.display.update(dirty)
			dirty=[]