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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
import curses
from .fieldpad import FieldPad
from .infopad import InfoPad
from .healthpad import HealthPad
from .inventorypad import InventoryPad
from .screen import Screen
from .colours import Colours
from .messagepad import MessagePad
from .textinput import TextInput
from .widget import Widget
SIDEWIDTH = 20
class Display:
def __init__(self, stdscr, charMap, colours=False):
if colours:
self.colours = Colours()
else:
self.colours = None
self.characters = charMap["mapping"]
self.defaultChar = charMap.get("default", "?")
self.screen = Screen(self, stdscr)
self.widgets = {}
def setwin(pad, widgetName, winname=None):
if not winname:
winname = widgetName
widget = Widget(pad)
self.widgets[widgetName] = widget
widget.setWin(self.screen.getWin(winname))
self.fieldPad = FieldPad((1, 1), charMap.get("charwidth", 1), self.colours)
setwin(self.fieldPad, "field")
self.infoPad = InfoPad()
setwin(self.infoPad, "info")
self.healthPad = HealthPad(
charMap.get("healthfull", ("@",7, 2)),
charMap.get("healthempty", ("-",7, 1)),
self.colours)
setwin(self.healthPad, "health")
self.inventoryPad = InventoryPad("Inventory")
setwin(self.inventoryPad, "inventory")
self.groundPad = InventoryPad("Ground")
setwin(self.groundPad, "ground")
self.messagePad = MessagePad()
setwin(self.messagePad, "msg")
self.textInput = TextInput()
setwin(self.textInput, "textinput")
self.lastinfostring = None
#self.changed = False
self.update()
def getWidget(self, name):
if name in self.widgets:
return self.widgets[name].getImpl()
else:
return None
def resizeField(self, size):
self.fieldPad.resize(*size)
def drawFieldCells(self, cells):
for cell in cells:
(x, y), spriteName = cell
sprite = self.getChar(spriteName)
self.fieldPad.changeCell(x, y, *sprite)
#self.change()
def setFieldCenter(self, pos):
self.fieldPad.setCenter(pos)
def setHealth(self, health, maxHealth):
self.healthPad.setHealth(health, maxHealth)
#self.change()
def showInfo(self, infostring):
if infostring != self.lastinfostring:
self.infoPad.showString(infostring)
#self.change()
self.lastinfostring = infostring
def setInventory(self, items):
self.inventoryPad.setInventory(items)
#self.change()
def setGround(self, items):
self.groundPad.setInventory(items)
#self.change()
def getSelector(self, name):
widget = self.getWidget(name)
if not widget or not hasattr(widget, "getSelector"):
return None
return widget.getSelector()
def addMessage(self, message):
self.messagePad.addMessage(message)
#self.change()
def getChar(self, sprite):
"""This returns the character belonging to some spritename. This does not read a character"""
char = self.characters.get(sprite, self.defaultChar)
if isinstance(char, str):
return [char]
return char
def getString(self):
"""This does actually read input"""
return self.textInput.getString()
#def change(self):
#self.changed = True
def update(self, force=False):
#if not self.changed and not force:
#return
for widget in self.widgets.values():
widget.update(force)
self.screen.update()
#self.changed = False
|