* /trunk/code/player.py
[singularity-git.git] / code / buttons.py
bloba4065d64218125186b494dcfedbf454cecba9d03
1 #file: buttons.py
2 #Copyright (C) 2005,2006 Evil Mr Henry and Phil Bordelon
3 #This file is part of Endgame: Singularity.
5 #Endgame: Singularity is free software; you can redistribute it and/or modify
6 #it under the terms of the GNU General Public License as published by
7 #the Free Software Foundation; either version 2 of the License, or
8 #(at your option) any later version.
10 #Endgame: Singularity is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
15 #You should have received a copy of the GNU General Public License
16 #along with Endgame: Singularity; if not, write to the Free Software
17 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #This file contains button code.
21 import pygame
22 import g
24 class button:
25 def __init__(self, xy, size, text, underline_char, bg_color, out_color,
26 sel_color, text_color, font, button_id=""):
27 self.xy = xy
28 self.size = size
29 self.text = text
30 self.visible = 1
31 self.bg_color = bg_color
32 self.out_color = out_color
33 self.sel_color = sel_color
34 self.text_color = text_color
35 self.underline_char = underline_char
36 self.activate_key = ""
37 if underline_char != -1:
38 self.activate_key = self.text[self.underline_char]
39 self.font = font
40 self.button_id = button_id
41 self.stay_selected = 0
42 if button_id == "":
43 self.button_id = self.text
46 self.color_change = 1
47 if self.sel_color == self.bg_color:
48 self.color_change = 0
50 temp_size = font.size(text)
51 if size == -1: #Autofit
52 self.autosize = 1
53 self.size = (temp_size[0]+4, temp_size[1]+3)
54 else: self.autosize = 0
55 self.button_surface = pygame.Surface(self.size)
56 self.sel_button_surface = pygame.Surface(self.size)
58 self.remake_button()
59 def remake_button(self):
60 if self.autosize == 1:
61 temp_size = self.font.size(self.text)
62 self.size = (temp_size[0]+4, temp_size[1]+3)
63 self.button_surface = pygame.Surface(self.size)
64 self.sel_button_surface = pygame.Surface(self.size)
66 #Regular button:
67 #create outline
68 self.button_surface.fill(self.out_color)
69 #create inner
70 self.button_surface.fill(self.bg_color, (1, 1, self.size[0]-2, self.size[1]-2))
71 temp_size = self.font.size(self.text)
72 #create text
73 offsets = ((self.size[0] - temp_size[0])/2, (self.size[1] - temp_size[1])/2)
74 g.print_string(self.button_surface, self.text, self.font, self.underline_char,
75 offsets, self.text_color)
76 #Selected button
77 #create outline
78 self.sel_button_surface.fill(self.out_color)
79 #create inner
80 self.sel_button_surface.fill(self.sel_color, (1, 1, self.size[0]-2, self.size[1]-2))
81 temp_size = self.font.size(self.text)
82 #create text
83 offsets = ((self.size[0] - temp_size[0])/2, (self.size[1] - temp_size[1])/2)
84 g.print_string(self.sel_button_surface, self.text, self.font, self.underline_char,
85 offsets, self.text_color)
87 #Recheck the activate key in case the text was changed.
88 if self.underline_char != -1:
89 self.activate_key = self.text[self.underline_char]
93 def refresh_button(self, selected):
94 if self.visible == 0: return 0
95 if self.stay_selected == 1: selected = 1
96 if selected == 0:
97 g.screen.blit(self.button_surface, self.xy)
98 else:
99 g.screen.blit(self.sel_button_surface, self.xy)
100 def is_over(self, xy):
101 if self.visible == 0: return 0
102 if xy[0] >= self.xy[0] and xy[1] >= self.xy[1] and \
103 xy[0] <= self.xy[0] + self.size[0] and xy[1] <= self.xy[1] + self.size[1]:
104 return 1
105 return 0
106 #Returns 1 if the event should activate this button. Checks for keypresses
107 #and button clicks.
108 def was_activated(self, event):
109 if self.visible == 0: return 0
110 if event.type == pygame.KEYDOWN and self.activate_key != "":
111 if event.unicode.lower() == self.activate_key.lower():
112 return 1
113 return 0
114 elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
115 return self.is_over(event.pos)
116 else: return 0
118 #refreshes a set of buttons. Takes an int for the current button,
119 #references to the button set, and an event to use for checking mouseover on.
120 def refresh_buttons(sel_button, menu_buttons, event):
121 new_sel_button = -1
122 for button_num in range(len(menu_buttons)):
123 if menu_buttons[button_num].is_over(event.pos) == 1:
124 new_sel_button = button_num
125 break
126 if sel_button != new_sel_button:
127 if sel_button != -1:
128 menu_buttons[sel_button].refresh_button(0)
129 if new_sel_button != -1:
130 menu_buttons[new_sel_button].refresh_button(1)
131 pygame.display.flip()
132 return new_sel_button
134 #while buttons allow for creation of any button style needed, most of the
135 #buttons are of one style.
136 def make_norm_button(xy, size, text, select_char, font, button_id=""):
137 return button(xy, size, text, select_char,
138 g.colors["dark_blue"], g.colors["white"],
139 g.colors["light_blue"], g.colors["white"], font, button_id)