예제 #1
0
class ChatWindow(Widget):
	def __init__(self, x, y, w, h, parent=None, gui=None):
		Widget.__init__(self, x, y, w, h, parent)
		self.gui = gui
		self.makeSurface()
		self.entry = TextEntry("", width = w, parent=self)
		self.scrollTextWindow = ScrollTextWindow(0, 0, w, h-20, parent=self)
		
		#print "created chatwindow, after init, w = %s, h = %s, x = %s, y = %s" % (self.w, self.h, self.x, self.y)
		self.entry.setPos(0, self.h-20)
		
	def addText(self, text):
		self.scrollTextWindow.addText(text)
		
	def getFocus(self):
		self.entry.getFocus()
		
	def loseFocus(self):
		self.entry.loseFocus()
		
	def handleEvents(self, events=[]):
		self.scrollTextWindow.handleEvents(events)
		self.entry.handleEvents(events)
		if events:
			self.updateSurface()
		
		
	def hide(self):
		if not self.visible:
			return
		# do not send the widget itself to the map, it would get modified
		self.gui.game.addDirtyRect(self.copy())
		
		self.visible = False
		for child in self._children:
			child.hide()
	
	def setPos(self, x, y):
		"""Sets the widget position
		if the widget is a child of another widget the position
		is relative to the parent widget"""
		self.topleft = (x, y)
		for child in self._children:
			child.dx = x
			child.dy = y
예제 #2
0
class LoginGUI(Widget):
	def __init__(self, game):
		self.game = game
		self.screen = self.game.screen
		
		screen_w = self.screen.get_width()
		screen_h = self.screen.get_height()
		self.x = 220
		self.y = 180
		self.step1 = 140
		self.step2 = 40
		
		self.title = renderText("LOGIN", FONT_MANGA, True)
		
		self.loginLabel = Label("  Name  ", FONT, width=100)
		self.loginLabel.setPos(self.x,self.y)
		
		self.loginEntry = TextEntry("", width = 180)
		self.loginEntry.setPos(self.x + self.step1 ,self.y)
		self.loginEntry.getFocus()
		
		
		self.passwordLabel = Label("  Password  ", FONT, width=100)
		self.passwordLabel.setPos(self.x,self.y + 2*self.step2)
		
		self.passwordEntry = TextEntry("", width = 180)
		self.passwordEntry.setPos(self.x + self.step1, self.y +  + 2*self.step2)
		
		self.infoLabel = Label("Info", width = screen_w-40)
		#self.infoLabel.setPos(self.x,self.y + 5*self.step1)
		self.infoLabel.setPos(20,screen_h *0.9)
		
		self.loginButton = TextButton(text='  LOG IN  ')
		#self.loginButton.setPos(0, self.y + 4*self.step2)
		self.loginButton.setPos(screen_w*0.5, screen_h*0.8)
		#self.loginButton.centerH(self.screen)
		self.loginButton.bind(self.sendLoginRequest)
		
		self.registerButton = TextButton(text='  REGISTER  ')
		self.registerButton.setPos(screen_w*0.75, screen_h*0.8)
		self.registerButton.bind(self.sendRegisterRequest)
		
	def blit(self):
		self.screen.blit(self.title, (50,50))
		self.infoLabel.blit(self.screen)
		self.loginLabel.blit(self.screen)
		self.passwordLabel.blit(self.screen)
		self.loginEntry.blit(self.screen)
		self.passwordEntry.blit(self.screen)
		self.loginButton.blit(self.screen)
		self.registerButton.blit(self.screen)
		
	def handleEvents(self, events=[]):
		if not events:
			return False
			
		self.loginEntry.handleEvents(events)
		self.passwordEntry.handleEvents(events)
		self.loginButton.handleEvents(events)
		self.registerButton.handleEvents(events)
		
		for event in events:
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_TAB:
					if self.loginEntry.has_focus:
						self.loginEntry.loseFocus()
						self.passwordEntry.getFocus()
					elif self.passwordEntry.has_focus:
						self.loginEntry.getFocus()
						self.passwordEntry.loseFocus()
						
				elif event.key == pygame.K_RETURN:
					if self.loginEntry.has_focus:
						self.loginEntry.loseFocus()
						self.passwordEntry.getFocus()
					elif self.passwordEntry.has_focus:
						if len(self.passwordEntry.baseText):
							self.sendLoginRequest()
						#self.loginEntry.getFocus()
						#self.passwordEntry.loseFocus()
						
				
						
			elif event.type == pygame.MOUSEBUTTONDOWN:
				if event.button == 1:
					if self.loginEntry.hover and not self.loginEntry.has_focus:
						self.loginEntry.getFocus()
						self.passwordEntry.loseFocus()
					elif self.passwordEntry.hover and not self.passwordEntry.has_focus:
						self.loginEntry.loseFocus()
						self.passwordEntry.getFocus()
			
			elif event.type == pygame.QUIT:
				pygame.quit()
			
		return False
		
	def sendLoginRequest(self):
		self.name = self.loginEntry.getText()
		self.password = self.passwordEntry.getText()
		print "Sending login request with name = %s, pass = %s" % (self.name, self.password)
		self.game.SendLogin(self.name, self.password)
		
	def sendRegisterRequest(self):
		self.name = self.loginEntry.getText()
		self.password = self.passwordEntry.getText()
		print "Sending register request with name = %s, pass = %s" % (self.name, self.password)
		self.game.SendRegister(self.name, self.password)
		
	def launch(self):
		self.running = True
		while self.running:
			self.update()
		
	def update(self):
		self.screen.fill((100,140,160))
		events = pygame.event.get()
		self.handleEvents(events)
		self.blit()
		pygame.display.flip()