/
frame.py
108 lines (75 loc) · 2.67 KB
/
frame.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
'''
Frame
'''
import libtcodpy as libtcod
class Frame:
x = y = 0
w = h = 0
ch = '#'
colFrame = libtcod.white
colText = libtcod.white
def __init__(self, console, x, y, w, h, name):
self.console = console
#x, y coordinates, on screen
self.x = x
self.y = y
#total width and height, including border
self.w = w
self.h = h
#number of inner rows
self.innerHeight = h - 2
#number of inner columns
self.innerWidth = w - 2
self.name = name
self.setTextColor(b'white');
self.setFrameColor(b'light_blue');
self.messages = [None for y in range(self.h)]
def putChar(self, x, y, char, col):
x = x + self.x + 1
y = y + self.y + 1
libtcod.console_put_char(self.console, x, y, char)
libtcod.console_set_char_foreground(self.console, x, y, getattr(libtcod, col))
def setBgColor(self, x, y, col, flag):
screenX = x + self.x + 1
screenY = y + self.y + 1
if isinstance(col, type(b'')):
col = getattr(libtcod, col)
libtcod.console_set_char_background(self.console, screenX, screenY, col, flag)
def addMessage(self, str, pos) :
self.messages[pos] = str
def printMessages(self) :
for pos, msg in enumerate(self.messages) :
if msg != None:
self.printMsg(pos, msg)
def printMsg(self, y, msg) :
for k, c in enumerate(msg) :
if k >= self.w :
break
x = 1 + self.x + k
_y = self.y + y
libtcod.console_put_char(self.console, x, _y, c)
libtcod.console_set_char_foreground(self.console, x, _y, self.colText)
def setTextColor(self, col) :
self.colText = getattr(libtcod, col)
def setFrameColor(self, col) :
self.colFrame = getattr(libtcod, col)
def renderFrame(self) :
for i in range (self.w + 1) :
x = self.x + i
y1 = self.y
y2 = self.y + self.h - 1
libtcod.console_put_char(self.console, x, y1, self.ch)
libtcod.console_put_char(self.console, x, y2, self.ch)
libtcod.console_set_char_foreground(self.console, x, y1, self.colFrame)
libtcod.console_set_char_foreground(self.console, x, y2, self.colFrame)
#we've already drawn the corners
for j in range (1, self.h) :
y = self.y + j
x1 = self.x
x2 = self.x + self.w
libtcod.console_put_char(self.console, x1, y, self.ch)
libtcod.console_put_char(self.console, x2, y, self.ch)
libtcod.console_set_char_foreground(self.console, x1, y, self.colFrame)
libtcod.console_set_char_foreground(self.console, x2, y, self.colFrame)
def renderTitle(self) :
self.printMsg(0, self.name)