-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMessageDialog.py
192 lines (158 loc) · 7.52 KB
/
MessageDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
Shows message dialog to user
"""
__version__ = "alpha"
__author__ = "Nard Janssens, Vinicius Silva, Robert Gowans, Ivan Antalec, Leonardo Marques - See Github PhotonFileUtils"
import os
import pygame
from pygame.locals import *
from GUI import *
########################################################################################################################
## Class MessageDialog
########################################################################################################################
class MessageDialog():
#Button constants
OK=0
YESNO=1
OKCANCEL=2
buttonChoice=OK
winrect=None
titlerect=None
margins=GRect(8,4,4,4)
waiting=False
dragDiff = None
bordercolor=(0,0,0)
titlebackcolor=defTitlebarBackground
titletextcolor=(0,0,0)
formcolor=defFormBackground
fontname = defFontName
fontsize = defFontSize
listbox=None
btnRight=None
titleheight=24
footerHeight=40
buttonHeight=28
buttonWidth=64
controls=[]
def reposControls(self): #called initially and after winrect is moved
""" Recalculates all positions after moving dialog box. """
self.winrect.height=self.titleheight+self.margins.y+self.label.rect.height+self.footerHeight+self.margins.height
self.titlerect = GRect(self.winrect.x, self.winrect.y, self.winrect.width, self.titleheight)
self.footerTop = self.winrect.y + self.winrect.height - self.margins.height - self.footerHeight
self.label.rect.x=self.winrect.x+self.margins.x
self.label.rect.y=self.winrect.y+self.titleheight+self.margins.y
self.btnRight.rect=GRect(self.winrect.x + self.winrect.width - self.margins.width - self.buttonWidth, self.footerTop + self.margins.x, self.buttonWidth, self.buttonHeight)
if not self.buttonChoice==self.OK: # two buttons
self.btnLeft.rect=GRect.copy(self.btnRight.rect)
self.btnLeft.rect.left=self.btnLeft.rect.left-self.margins.width - self.buttonWidth
def __init__(self, pyscreen,
pos, width=300,
center=True,
title="Message Dialog",message="Read this carefully... \n ...before entering Ok!",
dfontname=defFontName, dfontsize=defFontSize,
buttonChoice=OK,
handleOK=None,
handleCANCEL = None,
handleYES = None,
handleNO = None,
parentRedraw=None):
""" Saves all values to internal variables and calculates some extra internal vars. """
# Save variables
self.pyscreen = pyscreen
self.parentRedraw=parentRedraw
self.winrect=GRect(pos[0], pos[1], width, 160)
self.title=title
self.message=message
self.font = pygame.font.SysFont(dfontname, dfontsize)
self.buttonChoice=buttonChoice
self.handleOK=handleOK
self.handleCANCEL=handleCANCEL
self.handleNO = handleNO
self.handleYES = handleYES
# Calculate extra variables
dummy, textheight = self.font.size("MinimalText")
self.titleheight=textheight+self.margins.y+self.margins.height
self.footerTop = self.winrect.y + self.winrect.height - self.margins.height - self.footerHeight
# Add GUI.Label and GUI.Button(s)
rectLabel=GRect(0,0,self.winrect.width-self.margins.x-self.margins.width,0)
self.label=Label(pyscreen,text=message,fontname=dfontname,fontsize=dfontsize,rect=rectLabel,autoheight=True,center=center,backcolor=self.formcolor,autowrap=True)
self.controls.append(self.label)
self.btnRight=Button(pyscreen, text="OK", func_on_click=self.handleRight, rect=GRect())
self.controls.append(self.btnRight)
if buttonChoice==self.YESNO:
self.btnLeft = Button(pyscreen, text="YES", func_on_click=self.handleLeft, rect=GRect())
self.btnRight = Button(pyscreen, text="NO", func_on_click=self.handleRight, rect=GRect())
self.controls.append(self.btnLeft)
self.controls.append(self.btnRight)
if buttonChoice == self.OKCANCEL:
self.btnLeft = Button(pyscreen, text="OK", func_on_click=self.handleLeft, rect=GRect())
self.btnRight=Button(pyscreen, text="CANCEL", func_on_click=self.handleRight, rect=GRect())
self.controls.append(self.btnLeft)
self.controls.append(self.btnRight)
# (Re)calculate remaining variables
self.reposControls()
def show(self):
""" Returns selected button and closes dialog. """
self.waiting=True
self.waitforuser()
return self.lastaction
def redraw(self):
""" Redraws dialogbox. """
# First call parent / window to redraw itself
self.parentRedraw()
# Draw form background
pygame.draw.rect(self.pyscreen, self.formcolor, self.winrect.tuple(), 0)
# Draw title bar including title text
pygame.draw.rect(self.pyscreen, self.titlebackcolor,self.titlerect.tuple(), 0)
self.font.set_bold(True)
textsurface = self.font.render(self.title, True, self.titletextcolor)
self.pyscreen.blit(textsurface, (self.winrect.x + self.margins.x, self.winrect.y + self.margins.y))
self.font.set_bold(False)
# Draw form border
pygame.draw.rect(self.pyscreen, self.bordercolor, self.winrect.tuple(), 1)
# Call upon label and button to redraw themselves.
self.label.redraw()
self.btnRight.redraw()
if not self.buttonChoice==self.OK: self.btnLeft.redraw()
def waitforuser(self):
""" Blocks all events to Main window and wait for user to click OK. """
while self.waiting:
self.redraw()
pygame.display.flip()
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
gpos=GPoint().fromTuple(pos)
if event.type == pygame.MOUSEBUTTONUP:
if self.dragDiff==None:
for ctrl in self.controls:
ctrl.handleMouseUp(pos,event.button)
else: # handle window move
self.dragDiff=None
if event.type == pygame.MOUSEBUTTONDOWN:
if gpos.inGRect(self.titlerect):
self.dragDiff = gpos - self.winrect.p1
else:
for ctrl in self.controls:
ctrl.handleMouseDown(pos,event.button)
if event.type == pygame.MOUSEMOTION:
if not self.dragDiff==None:
self.winrect.p1=gpos-self.dragDiff
self.reposControls()
else:
for ctrl in self.controls:
ctrl.handleMouseMove(pos)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print("Escape key pressed down.")
self.waiting = False
def handleRight(self):
""" If OK we tell main loop we are ready waiting. """
if self.buttonChoice==self.OK: self.lastaction="OK"
if self.buttonChoice==self.OKCANCEL: self.lastaction="CANCEL"
if self.buttonChoice == self.YESNO: self.lastaction = "NO"
self.waiting=False
def handleLeft(self):
""" If OK we tell main loop we are ready waiting. """
if self.buttonChoice==self.OKCANCEL: self.lastaction="OK"
if self.buttonChoice == self.YESNO: self.lastaction = "YES"
self.waiting=False