-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from AustL/popups
Popups
- Loading branch information
Showing
5 changed files
with
205 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import pygame | ||
import tkinter as tk | ||
from tkinter import messagebox | ||
from enum import Enum | ||
|
||
import pygame_widgets | ||
from pygame_widgets.widget import WidgetBase | ||
|
||
tk.Tk().wm_withdraw() | ||
|
||
|
||
class PopupType(Enum): | ||
INFO = 0 | ||
ERROR = 1 | ||
WARNING = 2 | ||
QUESTION = 3 | ||
OK_CANCEL = 4 | ||
YES_NO = 5 | ||
YES_NO_CANCEL = 6 | ||
RETRY_CANCEL = 7 | ||
|
||
|
||
class Popup(WidgetBase): | ||
def __init__(self, win: pygame.Surface, x: int, y: int, width: int, height: int, popupType: PopupType, | ||
title: str, text: str, trigger=lambda *args: None, *buttons, **kwargs): | ||
super().__init__(win, x, y, width, height) | ||
self.popupType = popupType | ||
self.title = title | ||
self.text = text | ||
self.trigger = trigger | ||
self.buttons = buttons | ||
|
||
self.margin = kwargs.get('margin', 20) | ||
|
||
self.titleColour = kwargs.get('titleColour', (0, 0, 0)) | ||
self.titleSize = kwargs.get('titleSize', 40) | ||
self.titleFont = kwargs.get('titleFont', pygame.font.SysFont('calibri', self.titleSize, True)) | ||
self.titleRect = self.alignTitleRect() | ||
|
||
self.textColour = kwargs.get('textColour', (0, 0, 0)) | ||
self.textSize = kwargs.get('textSize', 18) | ||
self.textFont = kwargs.get('textFont', pygame.font.SysFont('calibri', self.textSize)) | ||
self.textRect = self.alignTextRect() | ||
|
||
self.radius = kwargs.get('radius', 0) | ||
|
||
self.colour = kwargs.get('colour', (150, 150, 150)) | ||
self.shadowDistance = kwargs.get('shadowDistance', 0) | ||
self.shadowColour = kwargs.get('shadowColour', (210, 210, 180)) | ||
|
||
self.result = None | ||
|
||
self.hide() | ||
|
||
def alignTitleRect(self): | ||
return pygame.Rect(self._x + self.margin, self._y + self.margin, | ||
self._width - self.margin * 2, self._height // 3 - self.margin * 2) | ||
|
||
def alignTextRect(self): | ||
return pygame.Rect(self._x + self.margin, self._y + self._height // 3, | ||
self._width - self.margin * 2, self._height // 2 - self.margin * 2) | ||
|
||
def listen(self, events): | ||
if self.trigger(): | ||
self.show() | ||
messagebox.showinfo(self.title, self.text) | ||
|
||
def draw(self): | ||
pass | ||
|
||
def show(self): | ||
super().show() | ||
match self.popupType: | ||
case PopupType.INFO: | ||
messagebox.showinfo(self.title, self.text) | ||
case PopupType.ERROR: | ||
messagebox.showerror(self.title, self.text) | ||
case PopupType.WARNING: | ||
messagebox.showwarning(self.title, self.text) | ||
case PopupType.QUESTION: | ||
self.result = messagebox.askquestion(self.title, self.text) | ||
case PopupType.OK_CANCEL: | ||
self.result = messagebox.askokcancel(self.title, self.text) | ||
case PopupType.YES_NO: | ||
self.result = messagebox.askyesno(self.title, self.text) | ||
case PopupType.YES_NO_CANCEL: | ||
self.result = messagebox.askyesnocancel(self.title, self.text) | ||
case PopupType.RETRY_CANCEL: | ||
self.result = messagebox.askretrycancel(self.title, self.text) | ||
|
||
def getResult(self): | ||
return self.result | ||
|
||
|
||
if __name__ == '__main__': | ||
from pygame_widgets.button import Button | ||
|
||
def setButtonColour(): | ||
if popup.getResult(): | ||
button.setInactiveColour('green') | ||
elif popup.getResult() == False: | ||
button.setInactiveColour('red') | ||
|
||
pygame.init() | ||
win = pygame.display.set_mode((600, 600)) | ||
|
||
popup = Popup(win, 100, 100, 400, 400, PopupType.YES_NO, 'Popup', | ||
'This is the text in the popup. Would you like to continue? The buttons below can be customised.', | ||
radius=20, textSize=20) | ||
|
||
button = Button(win, 100, 100, 400, 400, text='Popup', onClick=popup.show) | ||
|
||
run = True | ||
while run: | ||
events = pygame.event.get() | ||
for event in events: | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
run = False | ||
quit() | ||
|
||
win.fill((255, 255, 255)) | ||
|
||
pygame_widgets.update(events) | ||
pygame.display.update() | ||
setButtonColour() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pygame | ||
|
||
|
||
def drawText(win, text, colour, rect, font, align='centre'): | ||
rect = pygame.Rect(rect) | ||
y = rect.top | ||
lineSpacing = -2 | ||
|
||
fontHeight = font.size('Tg')[1] | ||
|
||
while text: | ||
i = 1 | ||
|
||
if y + fontHeight > rect.bottom: | ||
break | ||
|
||
while font.size(text[:i])[0] < rect.width and i < len(text): | ||
i += 1 | ||
|
||
if i < len(text): | ||
i = text.rfind(' ', 0, i) + 1 | ||
|
||
image: pygame.Surface = font.render(text[:i], 1, colour) | ||
|
||
imageRect: pygame.Rect = image.get_rect() | ||
|
||
imageRect.center = rect.center | ||
|
||
if align == 'left': | ||
imageRect.left = rect.left | ||
elif align == 'right': | ||
imageRect.right = rect.right | ||
|
||
win.blit(image, (imageRect.left, y)) | ||
y += fontHeight + lineSpacing | ||
|
||
text = text[i:] | ||
|
||
return text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters