def draw(self, surface): rect = self._getRect() size = rect.size pos = rect.topleft # Fill the input area with the specified colour surface.fill(self.realBackColour, rect) # Put what's currently inputted into the input area if self.value is None: name = '' else: name = keyboard.shortcutName(self.value) inputText = self.font.render(self.app, name, True, self.fontColour, self.realBackColour) text_rect = inputText.get_rect() diff = (text_rect.height - rect.height) / 2 area = pygame.Rect(0, diff, size[0] - self.pxFromRight, rect.height) # Put the text on the screen text_rect.centery = rect.centery surface.blit(inputText, (pos[0] + self.pxFromLeft, rect.top), area)
def __repr__(self): if isinstance(self.key, int): key = self.key else: # Calculate the physical key. for key, value in self.mapping.iteritems(): if value == self.key: break else: return '??' return keyboard.shortcutName(key, 0)
def addMenuItem(self, item, x, y, width, itemHeight): stdImage = pygame.Surface((self.rect.width, itemHeight)) hvrImage = pygame.Surface((self.rect.width, itemHeight)) stdImage.set_colorkey((0, 0, 0)) hvrImage.set_colorkey((0, 0, 0)) if item.enabled: stdColour = self.stdColour hvrColour = self.hvrColour else: stdColour = hvrColour = self.disabledColour if item.name == '---': # Separator. pygame.draw.line(stdImage, stdColour, (0, itemHeight // 2), (self.rect.width, itemHeight // 2)) pygame.draw.line(hvrImage, hvrColour, (0, itemHeight // 2), (self.rect.width, itemHeight // 2)) else: # Put caption on standard canvas. img = self.font.render(self.app, item.name, True, stdColour) r = img.get_rect() r.midleft = (2, itemHeight // 2) stdImage.blit(img, r) # Put caption on hover canvas. img = self.font.render(self.app, item.name, True, hvrColour) hvrImage.blit(img, r) if item.action is not None and self.keymapping is not None: try: hkStr = keyboard.shortcutName( self.keymapping.getkey(item.action)) except KeyError: pass else: # Put hotkey on standard canvas. img = self.font.render(self.app, hkStr, True, stdColour) r = img.get_rect() r.midright = (width - 2, itemHeight // 2) stdImage.blit(img, r) # Put hotkey on hover canvas. img = self.font.render(self.app, hkStr, True, hvrColour) hvrImage.blit(img, r) btn = elements.HoverButton(self.app, Location((x, y), 'center'), stdImage, hvrImage) if item.enabled: btn.onClick.addListener(lambda btn: item.execute()) self.elements.append(btn)
def __str__(self): return keyboard.shortcutName(self.key, self.mods)