2017-10-15 21:30:31 +00:00
|
|
|
|
|
|
|
import curses
|
2018-01-01 19:09:23 +00:00
|
|
|
from asciifarm.common.utils import clamp
|
2018-01-03 16:07:15 +00:00
|
|
|
from .window import Window
|
2017-10-25 21:32:01 +00:00
|
|
|
|
|
|
|
import signal
|
|
|
|
|
2017-10-15 21:30:31 +00:00
|
|
|
class Screen:
|
|
|
|
|
2018-01-03 16:07:15 +00:00
|
|
|
def __init__(self, display, stdscr, colours):
|
2017-11-13 12:37:05 +00:00
|
|
|
self.display = display
|
2018-05-23 18:36:47 +00:00
|
|
|
try:
|
|
|
|
curses.curs_set(0)
|
|
|
|
self.cursorSet = False
|
|
|
|
except curses.error:
|
|
|
|
# Not all terminals support this functionality.
|
|
|
|
# When the error is ignored the screen will look a little uglier,
|
|
|
|
# A cursor will move around, but that's not terrible
|
|
|
|
# So in order to keep the game as accesible as possible to everyone, it should be safe to ignore the error.
|
|
|
|
self.cursorSet = True
|
|
|
|
# It is probably possible to make sure the cursor is only in a corner of the screen
|
|
|
|
# but I can't figure out how.
|
|
|
|
# it seems to ignore all my move commands unless I press a key
|
|
|
|
# I give up
|
2017-10-15 21:30:31 +00:00
|
|
|
self.stdscr = stdscr
|
2018-01-03 16:07:15 +00:00
|
|
|
self.colours = colours
|
2017-11-13 12:37:05 +00:00
|
|
|
self.setWins()
|
2017-10-25 21:32:01 +00:00
|
|
|
signal.signal(signal.SIGWINCH, self.updateSize)
|
2017-10-15 21:30:31 +00:00
|
|
|
|
2017-11-13 12:37:05 +00:00
|
|
|
def _limitHeight(self, h, y):
|
|
|
|
return min(h + y, self.height) - y
|
|
|
|
|
|
|
|
def setWins(self):
|
|
|
|
height, width = self.height, self.width = self.stdscr.getmaxyx()
|
|
|
|
|
|
|
|
sideW = 20
|
|
|
|
sideX = width-sideW
|
2018-09-05 10:52:06 +00:00
|
|
|
|
2018-01-01 19:09:23 +00:00
|
|
|
msgH = clamp(height // 5, 3, 5)
|
2017-12-28 21:47:36 +00:00
|
|
|
msgY = height - msgH-1
|
|
|
|
inputH = 1
|
|
|
|
inputY = msgY + msgH
|
2018-09-05 10:52:06 +00:00
|
|
|
|
2017-11-13 12:37:05 +00:00
|
|
|
healthY = 0
|
|
|
|
healthH = self._limitHeight(2, healthY)
|
2018-09-09 12:52:59 +00:00
|
|
|
indexY = healthY + healthH
|
|
|
|
indexH = self._limitHeight(4, indexY)
|
|
|
|
listY = indexY + indexH + 1
|
|
|
|
listH = self._limitHeight(12, listY)
|
|
|
|
infoY = listY + listH
|
2017-11-13 12:37:05 +00:00
|
|
|
infoH = self._limitHeight(20, infoY)
|
|
|
|
|
2018-09-09 15:20:40 +00:00
|
|
|
lists = self.makeWin(sideX, listY, sideW, listH)
|
|
|
|
|
2017-11-13 12:44:13 +00:00
|
|
|
self.windows = {
|
2017-11-13 13:29:54 +00:00
|
|
|
"field": self.makeWin(0, 0, sideX - 1, msgY),
|
|
|
|
"msg": self.makeWin(0, msgY, sideX - 1, msgH),
|
2017-12-28 21:47:36 +00:00
|
|
|
"textinput": self.makeWin(0, inputY, sideX - 1, inputH),
|
2018-09-05 10:52:06 +00:00
|
|
|
|
2017-11-13 13:29:54 +00:00
|
|
|
"health": self.makeWin(sideX, healthY, sideW, healthH),
|
2018-09-09 12:52:59 +00:00
|
|
|
"switch": self.makeWin(sideX, indexY, sideW, indexH),
|
2018-09-09 15:20:40 +00:00
|
|
|
"ground": lists,
|
|
|
|
"inventory": lists,
|
|
|
|
"equipment": lists,
|
2017-11-13 13:29:54 +00:00
|
|
|
"info": self.makeWin(sideX, infoY, sideW, infoH)
|
2017-11-13 12:44:13 +00:00
|
|
|
}
|
2018-09-06 16:51:15 +00:00
|
|
|
|
2017-11-13 12:37:05 +00:00
|
|
|
|
2017-11-13 13:09:16 +00:00
|
|
|
def makeWin(self, x, y, width, height):
|
|
|
|
if width < 1 or height < 1:
|
2018-01-03 16:07:15 +00:00
|
|
|
win = None
|
2018-05-22 18:31:45 +00:00
|
|
|
else:
|
|
|
|
win = curses.newwin(height, width, y, x)
|
2018-01-03 16:07:15 +00:00
|
|
|
return Window(win, self.colours)
|
2017-11-13 13:09:16 +00:00
|
|
|
|
2017-12-28 21:47:36 +00:00
|
|
|
def getWin(self, name):
|
2018-01-02 12:28:23 +00:00
|
|
|
return self.windows.get(name, None)
|
2017-12-28 21:47:36 +00:00
|
|
|
|
2017-10-26 10:24:58 +00:00
|
|
|
def updateSize(self, *args):
|
|
|
|
curses.endwin()
|
|
|
|
curses.initscr()
|
2017-11-13 12:37:05 +00:00
|
|
|
self.setWins()
|
2017-10-26 08:30:51 +00:00
|
|
|
self.stdscr.clear()
|
2018-01-02 12:28:23 +00:00
|
|
|
self.display.forceUpdate()
|
2017-11-13 12:37:05 +00:00
|
|
|
|
2017-12-28 21:47:36 +00:00
|
|
|
def update(self):
|
2017-11-13 13:29:54 +00:00
|
|
|
curses.doupdate()
|
2017-10-21 16:24:39 +00:00
|
|
|
|
2017-10-25 21:32:01 +00:00
|
|
|
def getWidth(self):
|
|
|
|
return self.width
|
|
|
|
|
|
|
|
def getHeight(self):
|
|
|
|
return self.height
|
2017-11-13 12:37:05 +00:00
|
|
|
|
2018-05-23 18:36:47 +00:00
|
|
|
|