Part II of status_gauge_gui.py:
class GraphWindow(wxWindow):
def __init__(self, parent, labes):
wxWindow.__init__(self, parent, -1)
self.labels = []
self.values = [struct.MaxBarWidth, struct.MaxBarWidth]
self.healthChange = 0
self.manaChange = 0
for label in labes:
self.labels.append(label)
font = wxFont(8, wxSWISS, wxNORMAL, wxBOLD)
self.SetFont(font)
EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
EVT_PAINT(self, self.OnPaint)
def SetValue(self, type, value):
if type:
if struct.Health != value:
self.healthChange = value - struct.Health
struct.Health = value
if struct.Health > struct.MaxHealth:
struct.MaxHealth = struct.Health
self.values[0] = int((float(struct.Health)/(float(struct.MaxHealth)/100.0))*float(struct.MaxBarWidth)/100.0)
else:
if struct.Mana != value:
self.manaChange = value - struct.Mana
struct.Mana = value
if struct.Mana > struct.MaxMana:
struct.MaxMana = struct.Health
self.values[1] = int((float(struct.Mana)/(float(struct.MaxMana)/100.0))*float(struct.MaxBarWidth)/100.0)
def OnPaint(self, evt):
size = self.GetSize()
bmp = wxEmptyBitmap(size.width, size.height)
dc = wxMemoryDC()
dc.SelectObject(bmp)
self.DrawBars(dc, size)
wdc = wxPaintDC(self)
wdc.BeginDrawing()
wdc.Blit(0,0, size.width, size.height, dc, 0,0)
wdc.EndDrawing()
dc.SelectObject(wxNullBitmap)
dc.Clear()
def DrawBars(self, dc, size):
dc.SetFont(self.GetFont())
dc.SetBackground(wxBrush(wxBLACK))
dc.Clear()
self.DrawHealthBar(dc, size)
self.DrawManaBar(dc, size)
def DrawHealthBar(self, dc, size):
ypos = int(float(size.height)/4.0) - int(float(struct.barHeight)/2.0)
label = self.labels[0]
dc.SetTextForeground(wxWHITE)
dc.DrawText(label, 2, ypos)
val = self.values[0]
if val:
if val < struct.MaxBarWidth/3:
color = wxRED
elif struct.MaxBarWidth/3 <= val <= (struct.MaxBarWidth/3)*2:
color = 'Yellow'
elif val > (struct.MaxBarWidth/3)*2:
color = wxGREEN
dc.SetPen(wxPen(color))
dc.SetBrush(wxBrush(color))
dc.DrawRectangle(20, ypos, val, struct.barHeight)
if self.healthChange < 0:
dc.SetTextForeground(wxRED)
string = str(self.healthChange)
dc.DrawText(string, val + 25, ypos)
elif self.healthChange > 0:
dc.SetTextForeground(wxGREEN)
string = "+" + str(self.healthChange)
dc.DrawText(string, val + 25, ypos)
def DrawManaBar(self, dc, size):
ypos = int((float(size.height)/4.0) - int(float(struct.barHeight)/2.0))*4
label = self.labels[1]
dc.SetTextForeground(wxWHITE)
dc.DrawText(label, 2, ypos)
val = self.values[1]
if val:
if val < struct.MaxBarWidth/3:
color = wxRED
elif struct.MaxBarWidth/3 <= val <= (struct.MaxBarWidth/3)*2:
color = 'Yellow'
elif val > (struct.MaxBarWidth/3)*2:
color = wxGREEN
dc.SetPen(wxPen(color))
dc.SetBrush(wxBrush(color))
dc.DrawRectangle(20, ypos, val, struct.barHeight)
if self.manaChange < 0:
dc.SetTextForeground(wxRED)
string = str(self.manaChange)
dc.DrawText(string, val + 25, ypos)
elif self.manaChange > 0:
dc.SetTextForeground(wxGREEN)
string = "+" + str(self.manaChange)
dc.DrawText(string, val + 25, ypos)
def OnEraseBackground(self, evt):
pass
class GUI(wxPySimpleApp):
def __init__(self, callingworld, pluginid, title, pos, size, maxHealth, maxMana):
wxPySimpleApp.__init__(self)
self.frame = MainWindow(callingworld, pluginid, NULL, -1, title, pos, size, maxHealth, maxMana)
def WakeUp(self):
if self.inQueue != None:
try:
value = self.inQueue.get_nowait()
except Queue.Empty:
pass
self.barNum = value[0]
self.val = value[1]
evt = UpdateBarEvent(self.barNum, self.val)
wxPostEvent(self.frame, evt)
|