-
Notifications
You must be signed in to change notification settings - Fork 0
/
meter.py
83 lines (69 loc) · 2.77 KB
/
meter.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
# Copyright 2009 Christopher Czyzewski
# This file is part of Project Mage.
#
# Project Mage is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Project Mage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Project Mage. If not, see <http://www.gnu.org/licenses/>.
import sys
import pygame
from pygame.locals import *
from math import sqrt
import random
class Meter:
def __init__ (self, loc, sizeX, sizeY, valMax, colorFull, colorEmpty, colorFlash=(255, 255, 255), colorSegment=(255, 255, 255)):
self.loc = [loc[0], loc[1]]
self.sizeX = sizeX
self.sizeY = sizeY
self.valMax = valMax
self.val = valMax
self.colorFull = colorFull
self.colorEmpty = colorEmpty
self.colorFlash = colorFlash
self.colorSegment = colorSegment
self.flash = False
self.flashCount = 0
self.flashTick = 3
self.segment = 0
self.bar = pygame.Surface((self.sizeX, self.sizeY))
if self.valMax == 0:
print "Error: Illegal zero value - Meter"
sys.exit()
def update(self, val):
if val > self.valMax:
val = self.valMax
self.val = val
if self.segment < 0:
self.segment = 0
def draw(self, screen):
mainPane = pygame.Surface((self.sizeX, self.sizeY))
fillNum = int((float(self.val) / float(self.valMax)) * self.sizeX)
if self.flash:
self.flashCount += 1
if self.flashCount > self.flashTick:
self.flashCount = 0
self.flash = False
self.bar.fill (self.colorFlash)
else:
self.bar.fill (self.colorEmpty)
self.bar.fill (self.colorFull, (0, 0, fillNum, self.sizeY))
mainPane.blit(self.bar, (0, 0))
if self.segment > 0:
tempSize = int((float(self.segment) / float(self.valMax)) * self.sizeX)
tempPane = pygame.Surface((tempSize, self.sizeY))
tempPane.fill (self.colorSegment)
mainPane.blit(tempPane, ((fillNum - tempSize), 0))
screen.blit(mainPane, self.loc)
def center(self, baseRect, doHoriz, doVert):
if doHoriz:
self.loc[0] = (baseRect.width / 2) - (self.sizeX / 2)
if doVert:
self.loc[1] = (baseRect.height / 2) - (self.sizeY / 2)