############################################################################# ## This file is part of TunnelHack II. ## ##-------------------------------------------------------------------------## ## Copyright 2007-2008 Bryce Schroeder ## ## bryce.schroeder@gmail.com ## ## http://www.ferazelhosting.net/~bryce/ ## ##-------------------------------------------------------------------------## ## This program is free software: you can redistribute it and/or modify ## ## it under the terms of the GNU General Public License as published by ## ## the Free Software Foundation, either version 3 of the License, or ## ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## ## along with this program. If not, see . ## ############################################################################# from .interface import IObj from .g import g import pygame class IButton(IObj): """ """ def render_self(self, theme, target, px, py): theme_blit = theme.blit if self.p.get('circular', False): bx, by = 2,6 else: bx, by = 1,6 width, height = self.p.w, self.p.h if width == 1 and height == 1: theme_blit((bx + 3 * self.state, by), target, px, py) elif width == 1: raise NotImplementedError('1xN buttons are not implemented') elif height == 1: theme_blit((1 + 3 * self.state, 7), target, px, py) for x in xrange(1, width - 1): theme_blit((2 + 3 * self.state, 7), target, x * 16 + px, py) theme_blit( (3 + 3 * self.state, 7), target, (width - 1) * 16 + px, py) else: # Draw the corners. self.draw_rect((1,10), target, px, py, width, height, True) return True def get_value(self): return self.state in [self.CLICKED, self.TOGGLED] def handle_click(self, lx, ly): #print "Button was clicked." pass def handle_unclick(self, lx, ly): # print "Button click was completed. pass class IGraphicButton(IButton): def render_self(self, theme, target, px, py): IButton.render_self(self, theme, target, px, py) im = self.p.image.surface mx, my = im.get_size() mx, my = (self.p.w * 16 - mx) // 2, (self.p.h * 16 - my) // 2 if self.state >= self.TOGGLED: target.blit(im, (px + 1 + mx, py + 1 + my)) else: target.blit(im, (px + mx, py + my)) return True class ITextButton(IButton): def init(self): font_res = g.resources.get('Font', self.p.get('font', 'Junicode')) width, height, size = self.p.w, self.p.h, self.p.get('size', 12) font = font_res.pygame_font(size) lines = font_res.wrap_text(size, self.p.text, width * 16 - 5) height_mul = font.get_height() + 2 font_height = len(lines) * height_mul - 2 self.text_surf = pygame.Surface((width * 16, font_height), pygame.SRCALPHA, 32) self.text_surf.fill((0, 0, 0, 0)) self.base_my = (height * 16 - font_height - 1) // 2 for e, line in enumerate(lines): surf = font.render(line, 1, self.p.color) mx = (width * 16 - surf.get_width()) // 2 self.text_surf.blit(surf, (mx, e * height_mul)) self.text_surf = self.text_surf.convert_alpha() def render_self(self, theme, target, px, py): IButton.render_self(self, theme, target, px, py) if self.state >= self.TOGGLED: target.blit(self.text_surf, (px + 1, py + 1 + self.base_my)) else: target.blit(self.text_surf, (px, py + self.base_my)) return True class ITextGraphicButton(ITextButton): def init(self): ITextButton.init(self) width, height, im = self.p.w, self.p.h, self.p.image.surface im_height = im.get_height() + self.text_surf.get_height() + 2 old_surf = self.text_surf self.text_surf = pygame.Surface((width * 16, im_height), pygame.SRCALPHA, 32) self.text_surf.fill((0, 0, 0, 0)) self.text_surf.blit(im, ((width * 16 - im.get_width()) // 2, 0)) self.text_surf.blit(old_surf, (0, im.get_height() + 2)) self.base_my = (height * 16 - self.text_surf.get_height()) // 2 self.text_surf = self.text_surf.convert_alpha()