/
label.py
61 lines (46 loc) · 1.61 KB
/
label.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
#!/usr/bin/env python
from guichan import *
from graphics import Graphics
from font import Font
from widget import Widget
class Label(Widget):
def __init__(self,caption=""):
Widget.__init__(self)
self.mCaption=caption
self.mAlignment=Graphics.LEFT
self.adjustSize()
def __del__(self):
Widget.__del__(self)
def setCaption(self,caption,adjust=True):
self.mCaption=caption
if adjust == True:
self.adjustSize()
else:
self.addDirtyRect()
def getCaption(self):
return self.mCaption
def setAlignment(self,align):
if self.mAlignment != align:
self.mAlignment=align
self.addDirtyRect()
def getAlignment(self):
return self.mAlignment
def adjustSize(self):
self.setHeight( self.getFont().getHeight() )
self.setWidth( self.getFont().getWidth(self.mCaption) )
self.addDirtyRect()
def draw(self,graphics):
textX=0
textY=self.getHeight()/2 - self.getFont().getHeight()/2
align=self.mAlignment
if align == Graphics.LEFT:
textX=0
elif align == Graphics.CENTER:
textX=self.getWidth()/2
elif align == Graphics.RIGHT:
textX=self.getWidth()
else:
raise GCN_EXCEPTION("Unknown alignment.")
graphics.setFont(self.getFont())
graphics.setColor(self.getForegroundColor())
graphics.drawText(self.mCaption, textX, textY, self.getAlignment() )