class MessageBox(Window): def __init__(self, message, title = 'MessageBox', buttons = OK, width = 200, height = 150, bgcolor = COLOR_BG, bordercolor = COLOR, hoverbordercolor = COLOR_HOVER, borderwidth = 1, parent = None, gui = None): Window.__init__(self, title, width, height, bgcolor, bordercolor, hoverbordercolor, borderwidth, parent, gui) self.buttons = buttons self.message = message self.textMessage = TextArea(self.message,\ width=int(width*0.9),\ bgcolor=bgcolor,\ bordercolor=bordercolor,\ hoverbordercolor=bordercolor,\ borderwidth = 0,\ parent=self) self.yesButton = TextButton("Yes", color_bg=bgcolor,\ color=bordercolor,\ color_hover=bordercolor,\ borderwidth = borderwidth,\ parent=self) self.noButton = TextButton("No", color_bg=bgcolor,\ color=bordercolor,\ color_hover=bordercolor,\ borderwidth = borderwidth,\ parent=self) self.okButton = TextButton("Ok", color_bg=bgcolor,\ color=bordercolor,\ color_hover=bordercolor,\ borderwidth = borderwidth,\ parent=self) self.textMessage.setPadding(int(width*0.03)) self.lastclickedbutton = None self.yesButton.bind(self.setClickedButton, 'yes') self.noButton.bind(self.setClickedButton, 'no') self.okButton.bind(self.setClickedButton, 'ok') if buttons == YESNO: self.yesButton.show() self.noButton.show() self.okButton.hide() elif buttons == OK: self.yesButton.hide() self.noButton.hide() self.okButton.show() self.autolayout(offset=(0,30)) self.hide() def setMessageBox(self, message, buttons=OK): self.message = message self.buttons = buttons if buttons == YESNO: self.yesButton.show() self.noButton.show() self.okButton.hide() elif buttons == OK: self.yesButton.hide() self.noButton.hide() self.okButton.show() self.textMessage.setText(message) self.autolayout(offset=(0,30)) def setClickedButton(self,button): self.lastclickedbutton = button self.hide() def getClickedButton(self): clicked = self.lastclickedbutton self.lastclickedbutton = None return clicked
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()