def __init__(self, text= "OK", font=FONT, width=0, height=0, bgcolor=COLOR_BG, bordercolor=COLOR, hoverbordercolor=COLOR_HOVER, borderwidth = 1, parent=None): self.has_focus = False self.shift = False Label.__init__(self, text, font, width, height, bgcolor, bordercolor, hoverbordercolor, borderwidth, parent)
def __init__(self, name = "Window", width = 80, height = 60, bgcolor = COLOR_BG, bordercolor = COLOR, hoverbordercolor = COLOR_HOVER, borderwidth = 1, parent = None, gui = None): Frame.__init__(self, width, height, bgcolor, bordercolor, hoverbordercolor, borderwidth, parent) self.gui = gui self.name = Label(name, width = width-21, bgcolor = COLOR_BG, borderwidth = 1, parent = self) self.name.setPos(0, 0) self.close_button = ImgButton(self.w-20, 0, 20, 20, "graphics/gui/guibase.png", 1,49,1,70, parent=self) self.close_button.setPos(self.w-20, 0) self.close_button.bind(self.hide) self.drag_button = ImgButton(self.w-20, 0, 20, 20, "graphics/gui/guibase.png", 22,49,22,70, parent=self) self.drag_button.setPos(self.w-20, self.h-20) self.click = False self._resize = False
def __init__(self, text= "OK", font=FONT, width=0, height=0, bgcolor=(100,100,100), bordercolor=(255,255,255), hoverbordercolor=(255,255,255), borderwidth = 1): Label.__init__(self, text, font, width, height, bgcolor, bordercolor, hoverbordercolor, borderwidth) self.baseText = text self.padding = 2 self.font = font self.click = False self.setText(" " + self.baseText + " ")
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 __init__(self, text= "OK", textcolor=COLOR, font=FONT, width=80, height=20, bgcolor=COLOR_BG, bordercolor=COLOR, hoverbordercolor=COLOR_HOVER, borderwidth = 1, parent=None): self.i = 0 #Frame.__init__(self, width, height, bgcolor, bordercolor, hoverbordercolor, borderwidth, parent) self.lineStep = 14 self.baseText = text self.padding = 2 self.font = font self.textColor = textcolor Label.__init__(self, text, font, width, height, bgcolor, bordercolor, hoverbordercolor, borderwidth, parent) self.setText(self.baseText)
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()
class Window(Frame): def __init__(self, name = "Window", width = 80, height = 60, bgcolor = COLOR_BG, bordercolor = COLOR, hoverbordercolor = COLOR_HOVER, borderwidth = 1, parent = None, gui = None): Frame.__init__(self, width, height, bgcolor, bordercolor, hoverbordercolor, borderwidth, parent) self.gui = gui self.name = Label(name, width = width-21, bgcolor = COLOR_BG, borderwidth = 1, parent = self) self.name.setPos(0, 0) self.close_button = ImgButton(self.w-20, 0, 20, 20, "graphics/gui/guibase.png", 1,49,1,70, parent=self) self.close_button.setPos(self.w-20, 0) self.close_button.bind(self.hide) self.drag_button = ImgButton(self.w-20, 0, 20, 20, "graphics/gui/guibase.png", 22,49,22,70, parent=self) self.drag_button.setPos(self.w-20, self.h-20) self.click = False self._resize = False def setSize(self, w, h): if w == self.width and h == self.height: return if w<40:w=40 if h<40:h=40 self.width = int(w) self.height = int(h) self.makeSurface() self.close_button.setPos(self.width - 21, 1) self.drag_button.setPos(self.w-21, self.h-21) self.name.setWidth(self.width-21) self.name.makeSurface() def OnDrag(self, x, y): self.gui.game.addDirtyRect(self.copy()) self.setPos(self.x + x, self.y + y) self.updateSurface() def OnResize(self, x, y): self.gui.game.addDirtyRect(self.copy()) self.setSize(self.w+x, self.h+y) 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 handleEvents(self, events=[]): if not events: return False if not self.visible: return for event in events: if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 and self.hover: self.click = True if event.type == pygame.MOUSEBUTTONUP: if self.click and self.hover: self._resize = False self.click = False #self.OnClick() if event.type == pygame.MOUSEMOTION: if self.click: resize_rect = pygame.Rect(self.x + self.getWidth()-20, self.y + self.getHeight()-20, 20, 20) if self._resize: self.OnResize(*event.rel) elif resize_rect.collidepoint(*event.pos): self._resize = True self.OnResize(*event.rel) else: self.OnDrag(*event.rel) #self.name.updateSurface() #self.name.blit(self.surface) #self.close_button.blit(self.surface) #self.drag_button.blit(self.surface) self.updateSurface() for child in self._children: if hasattr(child, 'handleEvents'): child.handleEvents(events)