Skip to content

Commit 36cfcf9

Browse files
Made repeatad button code into an inheritable
1 parent f2576c8 commit 36cfcf9

File tree

2 files changed

+22
-93
lines changed

2 files changed

+22
-93
lines changed

UI/UI.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def display_main_menu(window, slide_in, screen_group, options_screen_group):
4242
screen_group.add(Wrapper.TextButton(Wrapper.DefaultText.text("Graphs", Wrapper.FontSizes.BUTTON_SIZE), ((window.window.get_size()[0] / 2), GRAPH_Y), pressed_graph, window, slide_in))
4343

4444
# Options
45-
OptionActions.display_options_button(window, screen_group, options_screen_group)
45+
OptionActions.display_options_button(window, screen_group, options_screen_group, slide_in)
4646

4747
# Input code, specifically here for button presses
4848
def pressed_sorting(window):
@@ -52,14 +52,14 @@ def pressed_graph(window):
5252

5353
class OptionActions():
5454

55-
def display_options_button(window, screen_group, options_screen_group):
55+
def display_options_button(window, screen_group, options_screen_group, slide_in):
5656
options_button_1 = pygame.image.load('Button/OptionButtons/options.png').convert_alpha()
5757
options_button_1 = pygame.transform.rotozoom(options_button_1, 0, 0.1)
5858
options_button_2 = pygame.image.load('Button/OptionButtons/options_hover.png').convert_alpha()
5959
options_button_2 = pygame.transform.rotozoom(options_button_2, 0, 0.1)
6060
display_options = Wrapper.add_args_to_func(OptionActions.display_options, window, options_screen_group)
6161
# TODO: Crop image properly using Figma
62-
screen_group.add(Wrapper.Button((options_button_1, options_button_2), ((window.window.get_size()[0])-40, 40), display_options, window, True))
62+
screen_group.add(Wrapper.Button((options_button_1, options_button_2), ((window.window.get_size()[0])-40, 40), display_options, window, slide_in))
6363

6464
# Input (specifically buttons)
6565

@@ -208,7 +208,7 @@ def update(self):
208208
def on_reset(self):
209209
self.window.event.set()
210210

211-
def display_sorting(self, screen_group, options_screen_group):
211+
def display_sorting(self, screen_group, options_screen_group, slide_in = True):
212212
self.scroll_bar = []
213213
if (self.window.screen == Wrapper.Screen.SORTING_SCREEN and (not self.window.screen_change and not self.window.window_size_change)):
214214
return
@@ -274,7 +274,7 @@ def func():
274274
self.scroll_bar.append(Wrapper.ScrollBar(resets, ((5*self.window.window.get_size()[0]/6), button_col_top + button_margin), (200, 50), self.scroll_group[0], self.window, "Reset", 124.9))
275275
self.scroll_bar.append(Wrapper.ScrollBar(buttons, ((5*self.window.window.get_size()[0]/6), button_col_top + 2 * button_margin), (200, 50), self.scroll_group[1], self.window, "Choose Sorted"))
276276
# Options button
277-
OptionActions.display_options_button(self.window, screen_group, options_screen_group)
277+
OptionActions.display_options_button(self.window, screen_group, options_screen_group, slide_in)
278278
return self.scroll_bar
279279

280280
def toggle_aux_array(self):

UI/Wrapper.py

Lines changed: 17 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
'''Contains code for various objects that are used frequently as sprites'''
99
def add_args_to_func(func, *args, **kwargs):
10-
#@func.wraps(inner) # This is optional TODO: Fix and understand wut this is
1110
def inner():
1211
return func(*args, **kwargs)
1312
return inner
@@ -104,7 +103,7 @@ class Slide(pygame.sprite.Sprite):
104103
dist = the distance it will be offset from the edge of screen
105104
'''
106105
def __init__(self, image, coords, dim, window, dist = 1200, will_slide = True, screen = None):
107-
super().__init__()
106+
pygame.sprite.Sprite.__init__(self)
108107
self.image = image
109108
self.coords = coords
110109
self.will_slide = will_slide
@@ -149,7 +148,7 @@ class ButtonConfig(pygame.sprite.Sprite):
149148
Function = function that will run when button is pressed
150149
'''
151150
def __init__(self, images, coords, function, window):
152-
super().__init__()
151+
pygame.sprite.Sprite.__init__(self)
153152
self.images = images
154153
self.image = images[0]
155154
self.rect = images[0].get_rect(center = coords)
@@ -205,57 +204,22 @@ def __init__(self, text_arg, coords, window, will_slide = True, screen = None):
205204
super().__init__(image, coords, (0, 0), window, 800, will_slide, screen)
206205

207206
# TODO: There is a double where a click registers as a double click
208-
class Button(Slide):
207+
class Button(ButtonConfig, Slide):
209208
'''This is an icon button class
210209
@images = image background of the button
211210
@coords = the icon will be centered at those coords
212211
@function = the function that will be called when the button is pressed
213212
@window = the Window class object
214213
'''
215214
def __init__(self, images, coords, function, window, will_slide = True, screen = None):
216-
self.images = images
217-
self.function = function
218-
super().__init__(images[0], coords, (0, 0), window, 400, will_slide, screen)
219-
220-
self.handled = False
221-
222-
self.can_press = True # if in options and you don't want buttons behind the options to be pressed
223-
self.can_press_timer = 0
224-
self.timer_is_handled = True # False means its currently handling it
225-
226-
def player_input(self):
227-
if self.can_press and self.rect.collidepoint(pygame.mouse.get_pos()):
228-
if not pygame.mouse.get_pressed()[0]:
229-
self.handled = False
230-
if pygame.mouse.get_pressed()[0] and not self.handled:
231-
self.function()
232-
self.handled = True
233-
else:
234-
self.image = self.images[1]
235-
else:
236-
self.image = self.images[0]
237-
238-
def set_pressable(self):
239-
if (not self.can_press and self.timer_is_handled):
240-
self.can_press_timer = 0.0001
241-
self.timer_is_handled = False
242-
243-
def set_unpressable(self):
244-
self.can_press = False
215+
ButtonConfig.__init__(self, images, coords, function, window)
216+
Slide.__init__(self, images[0], coords, (0, 0), window, 400, will_slide, screen)
245217

246218
def update(self):
247-
super().update()
248-
#print(self.rect.x, self.coords[0])
249-
self.player_input()
250-
#print(self.can_press_timer)
251-
if (self.can_press_timer != 0):
252-
self.can_press_timer += 0.1
253-
if (self.can_press_timer > 2):
254-
self.can_press = True
255-
self.timer_is_handled = True
256-
self.can_press_timer = 0
219+
Slide.update(self)
220+
ButtonConfig.update(self)
257221

258-
class TextButton(Slide):
222+
class TextButton(ButtonConfig, Slide):
259223
# https://www.clickminded.com/button-generator/
260224
'''Button Creation
261225
Images list should have at least two images, one for hover and one for default
@@ -282,34 +246,16 @@ def __init__(self, text_arg, coords, function, window, will_slide = True, screen
282246
# default dimensions, cannot change now
283247
self.width = 200
284248
self.height = 50
285-
image = pygame.image.load("Button/button.png").convert_alpha()
286-
super().__init__(image, coords, (self.width, self.height), window, 200, will_slide, screen)
249+
image1 = pygame.image.load("Button/button.png").convert_alpha()
250+
image2 = pygame.image.load("Button/button_hovering.png").convert_alpha()
251+
ButtonConfig.__init__(self, [image1, image2], coords, function, window)
252+
Slide.__init__(self, image1, coords, (self.width, self.height), window, 200, will_slide, screen)
287253

288254
if will_slide:
289255
self.textRect = self.text.get_rect(center = (window.window.get_size()[0] + 200, coords[1]))
290256
else:
291257
self.textRect = self.text.get_rect(center = coords)
292258

293-
self.function = function
294-
295-
self.handled = False
296-
297-
self.can_press = True
298-
self.can_press_timer = 0
299-
self.timer_is_handled = True
300-
301-
def player_input(self):
302-
if self.can_press and self.rect.collidepoint(pygame.mouse.get_pos()):
303-
if not pygame.mouse.get_pressed()[0]:
304-
self.handled = False
305-
if pygame.mouse.get_pressed()[0] and not self.handled:
306-
self.function()
307-
self.handled = True
308-
else:
309-
self.image = pygame.image.load("Button/button_hovering.png").convert_alpha()
310-
else:
311-
self.image = pygame.image.load("Button/button.png").convert_alpha()
312-
313259
def slide(self):
314260
if ((self.screen != self.window.screen) or self.rect.x + self.width/2 > self.coords[0]):
315261
self.textRect.x += self.accel
@@ -318,29 +264,13 @@ def slide(self):
318264

319265
def change_func(self, function):
320266
self.function = function
321-
322-
def set_pressable(self):
323-
if (not self.can_press and self.timer_is_handled):
324-
self.can_press_timer = 0.0001
325-
self.timer_is_handled = False
326-
327-
def set_unpressable(self):
328-
self.can_press = False
329267

330268
def update(self):
331-
super().update()
269+
ButtonConfig.update(self)
270+
Slide.update(self)
332271
if self.will_slide:
333272
super().slide()
334273
self.window.window.blit(self.text, self.textRect)
335-
#print(self.rect.x, self.coords[0])
336-
337-
self.player_input()
338-
if (self.can_press_timer != 0):
339-
self.can_press_timer += 0.1
340-
if (self.can_press_timer > 2):
341-
self.can_press = True
342-
self.timer_is_handled = True
343-
self.can_press_timer = 0
344274

345275
# TODO : Fixed scuffed position for scrollbar when scrolling thru the first two elements using arrow keys
346276
class ScrollBar():
@@ -492,14 +422,14 @@ def toggle(self):
492422
self.scroll_group.empty()
493423
self.scroll_group.add(self.scroll_button)
494424
self.scroll_group.add(self.selected)
495-
self.scroll_button.set_unpressable()
496-
self.selected.set_unpressable
425+
self.scroll_button.set_pressable()
426+
self.selected.set_pressable()
497427

498428
# If its in options, advanced tools, etc where a window would pop up, the button shouldn't be pressed
499429
def check_overlap(self):
500430
if (self.window.screen == Screen.NONE and self.screen != Screen.NONE):
501431
self.scroll_button.set_unpressable()
502-
self.selected.set_unpressable
432+
self.selected.set_unpressable()
503433
else:
504434
self.scroll_button.set_pressable()
505435
self.selected.set_pressable()
@@ -604,7 +534,6 @@ def update(self):
604534
self.can_press_timer = 0
605535

606536
class Background(Slide):
607-
# TODO: Make background slide
608537
'''Creates a background (also used as an array element)
609538
@color = the color that the background will be in
610539
@coords = the coordinates where the rectangle will be centered at

0 commit comments

Comments
 (0)