cu asta
Cod:
class TipBoard(ui.Bar):
SCROLL_WAIT_TIME = 5.0
TIP_DURATION = 10.0
FONT_WIDTH = 18
FONT_HEIGHT = 18
LINE_WIDTH = 500
LINE_HEIGHT = FONT_HEIGHT + 5
STEP_HEIGHT = LINE_HEIGHT * 2
LINE_CHANGE_LIMIT_WIDTH = 350
FRAME_IMAGE_FILE_NAME_LIST = [
"season1/interface/oxevent/frame_0.sub",
"season1/interface/oxevent/frame_1.sub",
"season1/interface/oxevent/frame_2.sub",
]
FRAME_IMAGE_STEP = 256
FRAME_BASE_X = -20
FRAME_BASE_Y = -12
def __init__(self):
ui.Bar.__init__(self)
self.AddFlag("not_pick")
self.tipList = []
self.curPos = 0
self.dstPos = 0
self.nextScrollTime = 0
self.SetPosition(0, 150)
self.SetSize(512, 55)
self.SetColor(grp.GenerateColor(0.0, 0.0, 0.0, 0.5))
self.SetWindowHorizontalAlignCenter()
self.__CreateTextBar()
self.__LoadFrameImages()
def __LoadFrameImages(self):
x = self.FRAME_BASE_X
y = self.FRAME_BASE_Y
self.imgList = []
for imgFileName in self.FRAME_IMAGE_FILE_NAME_LIST:
self.imgList.append(self.__LoadImage(x, y, imgFileName))
x += self.FRAME_IMAGE_STEP
def __LoadImage(self, x, y, fileName):
img = ui.ImageBox()
img.SetParent(self)
img.AddFlag("not_pick")
img.LoadImage(fileName)
img.SetPosition(x, y)
img.Show()
return img
def __del__(self):
ui.Bar.__del__(self)
def __CreateTextBar(self):
x, y = self.GetGlobalPosition()
self.textBar = BigTextBar(self.LINE_WIDTH, 300, self.FONT_HEIGHT)
self.textBar.SetParent(self)
self.textBar.SetPosition(6, 8)
self.textBar.SetTextColor(242, 231, 193)
self.textBar.SetClipRect(0, y+8, wndMgr.GetScreenWidth(), y+8+self.STEP_HEIGHT)
self.textBar.Show()
def __CleanOldTip(self):
curTime = app.GetTime()
leaveList = []
for madeTime, text in self.tipList:
if curTime + self.TIP_DURATION <= madeTime:
leaveList.append(text)
self.tipList = leaveList
if not leaveList:
self.textBar.ClearBar()
self.Hide()
return
self.__RefreshBoard()
def __RefreshBoard(self):
self.textBar.ClearBar()
if len(self.tipList) == 1:
checkTime, text = self.tipList[0]
(text_width, text_height) = self.textBar.GetTextExtent(text)
self.textBar.TextOut((500-text_width)/2, (self.STEP_HEIGHT-8-text_height)/2, text)
else:
index = 0
for checkTime, text in self.tipList:
(text_width, text_height) = self.textBar.GetTextExtent(text)
self.textBar.TextOut((500-text_width)/2, index*self.LINE_HEIGHT, text)
index += 1
def SetTip(self, text):
if not app.IsVisibleNotice():
return
curTime = app.GetTime()
self.__AppendText(curTime, text)
self.__RefreshBoard()
self.nextScrollTime = curTime + 1.0
if not self.IsShow():
self.curPos = -self.STEP_HEIGHT
self.dstPos = -self.STEP_HEIGHT
self.textBar.SetPosition(3, 8 - self.curPos)
self.Show()
def __AppendText(self, curTime, text):
import dbg
prevPos = 0
while 1:
curPos = text.find(" ", prevPos)
if curPos < 0:
break
(text_width, text_height) = self.textBar.GetTextExtent(text[:curPos])
if text_width > self.LINE_CHANGE_LIMIT_WIDTH:
self.tipList.append((curTime, text[:prevPos]))
self.tipList.append((curTime, text[prevPos:]))
return
prevPos = curPos + 1
self.tipList.append((curTime, text))
def OnUpdate(self):
if not self.tipList:
self.Hide()
return
if app.GetTime() > self.nextScrollTime:
self.nextScrollTime = app.GetTime() + self.SCROLL_WAIT_TIME
self.dstPos = self.curPos + self.STEP_HEIGHT
if self.dstPos > self.curPos:
self.curPos += 1
self.textBar.SetPosition(3, 8 - self.curPos)
if self.curPos > len(self.tipList)*self.LINE_HEIGHT:
self.curPos = -self.STEP_HEIGHT
self.dstPos = -self.STEP_HEIGHT
self.__CleanOldTip()