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.
25 def __init__(self
, xy
, size
, text
, underline_char
, bg_color
, out_color
,
26 sel_color
, text_color
, font
, button_id
=""):
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
]
40 self
.button_id
= button_id
41 self
.stay_selected
= 0
43 self
.button_id
= self
.text
47 if self
.sel_color
== self
.bg_color
:
50 temp_size
= font
.size(text
)
51 if size
== -1: #Autofit
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
)
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
)
68 self
.button_surface
.fill(self
.out_color
)
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
)
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
)
78 self
.sel_button_surface
.fill(self
.out_color
)
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
)
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
97 g
.screen
.blit(self
.button_surface
, self
.xy
)
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]:
106 #Returns 1 if the event should activate this button. Checks for keypresses
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():
114 elif event
.type == pygame
.MOUSEBUTTONUP
and event
.button
== 1:
115 return self
.is_over(event
.pos
)
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
):
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
126 if sel_button
!= new_sel_button
:
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
)