def showHelp(self): string = "Type a command key for contextual help: " unicurses.waddstr(self.scr.win, string) while True: try: key = chr(self.scr.getch(self.scr.win)) except ValueError: continue if key in self.keymap and self.keymap[key]['help'] != None: if self.keymap[key]['help'] == False: break unicurses.wclear(self.scr.win) err = self.keymap[key]['stat'] if isinstance(err, basestring): attr = self.scr.attr['err'] unicurses.wmove(self.scr.win, self.scr.h - 1, 0) unicurses.waddnstr(self.scr.win, err, self.scr.w, attr) unicurses.wmove(self.scr.win, 0, 0) title = self.keymap[key]['text'] title = ' ' * (self.scr.w - len(title) - 6) + title + " Help \n" unicurses.waddstr(self.scr.win, title, self.scr.attr['ttl']) unicurses.waddstr(self.scr.win, self.keymap[key]['help']) string = "\n\nPress 'q' to exit help.\n\n" unicurses.waddstr(self.scr.win, string) while chr(self.scr.getch(self.scr.win)) != 'q': pass break
def draw_boardareaarea(self): uc.werase(self.win_board) uc.box(self.win_boardarea, 0, 0) uc.wmove(self.win_boardarea, 1, 2) if self.on_menu: s = self.player.board.__str__() else: s = self.player.board.show_new_ship(self.new_ship_x, self.new_ship_y, self.menu[self.menu_hi].id, self.menu[self.menu_hi].length, self.new_ship_hor) for chr in s: if chr == '~': uc.wattron(self.win_board, uc.COLOR_PAIR(11)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(11)) elif chr == 'O': uc.wattron(self.win_board, uc.COLOR_PAIR(12)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(12)) elif chr == '#': uc.wattron(self.win_board, uc.COLOR_PAIR(13)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(13)) else: uc.wattron(self.win_board, uc.COLOR_PAIR(12)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(12)) uc.wrefresh(self.win_boardarea) uc.update_panels()
def draw_board(self): uc.wclear(self.win_board1) uc.wclear(self.win_baord2) uc.box(self.win_boardarea, 0, 0) uc.wmove(self.win_boardarea, 1, 2) s1 = self.player.board.__str__() s2 = self.player_enemy.board.__str__() for chr in s1: if chr == '~': uc.wattron(self.win_board, uc.COLOR_PAIR(11)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(11)) elif chr == 'O': uc.wattron(self.win_board, uc.COLOR_PAIR(12)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(12)) elif chr == '#': uc.wattron(self.win_board, uc.COLOR_PAIR(13)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(13)) else: uc.wattron(self.win_board, uc.COLOR_PAIR(12)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(12)) uc.wrefresh(self.win_boardarea) uc.update_panels()
def __init__(self): self.win = uc.newwin(10, 30, 0, 0) uc.box(self.win) uc.wmove(self.win, 1, 1) uc.waddstr(self.win, "Tile Info") panel = uc.new_panel(self.win) uc.move_panel(panel, glob.CAM_HEIGHT, 62)
def draw_title(self): offset_x = 2 offset_y = 1 uc.box(self.win_title, 0, 0) uc.wmove(self.win_title, offset_y, offset_x) uc.waddstr(self.win_title, self.title) uc.wrefresh(self.win_title)
def _resize_window(self, name, size): """Register a window Args: size (tuple): The size of the window. name (str): The name of the window. """ uc.wresize(self._windows[name].window, size[0], size[1]) uc.wmove(self._windows[name].window, size[2], size[3])
def write(self, strings): row = 2 for s in strings: s +=(" " * (28-len(s))) #fill extra with spaces uc.wmove(self.win, row, 1) uc.waddstr(self.win, s) row +=1 #clear the remaining rows filler_str = " "*28 for i in range(row, 10): uc.wmove(self.win, row, 1) uc.waddstr(self.win, filler_str)
def refresh(self): """Refresh the window.""" self.update() if self.enabled: curses.wmove(self.win, 0, 0) self.draw() # Explicitly check wether clearing to bottom is necessary # Otherwise the most bottom-right character will be blanked if curses.getyx(self.win) != (self.height - 1, self.width - 1): curses.wclrtobot(self.win) curses.wrefresh(self.win)
def incrementTurn(self): self.turn +=1 uc.wmove(self.status_win, 1, 6) #6 is pos after string "Turn " uc.waddstr(self.status_win, str(self.turn)) worker_old = self.world.entities[0].my_tile self.world.moveEntities() worker_new = self.world.entities[0].my_tile self.painter.updateTileCenter(worker_old) self.painter.updateTileEdges(worker_old) self.painter.updateTileCenter(worker_new) self.painter.drawWindow() showChanges()
def render(self): unicurses.wclear(self.scr.win) if self.scr.msg != None: unicurses.wmove(self.scr.win, self.scr.h - 1, 0) if isinstance(self.scr.msg, basestring): unicurses.waddnstr(self.scr.win, self.scr.msg, self.scr.w) elif isinstance(self.scr.msg, tuple): attr = self.scr.attr[self.scr.msg[0]] msg = self.scr.msg[1] unicurses.waddnstr(self.scr.win, msg, self.scr.w, attr) unicurses.wmove(self.scr.win, 0, 0) unicurses.waddstr(self.scr.win, self.titlebar, self.scr.attr['ttl']) for opt in self.curropts: if opt == None: unicurses.waddstr(self.scr.win, "\n") continue key = opt['key'] + ": " unicurses.waddstr(self.scr.win, key, self.scr.attr['key']) if not isinstance(opt['val'], basestring): pad = 2 if opt['stat'] != True else 0 if opt['wait'] == True or opt['val'] == True: pad += 2 self.dotstr(opt['text'], None, pad) else: unicurses.waddstr(self.scr.win, opt['text']) attr = self.scr.attr['val'] if opt['stat'] != True: attr = self.scr.attr['err'] unicurses.waddstr(self.scr.win, " !", attr) if opt['wait'] == True: unicurses.waddstr(self.scr.win, " ~", self.scr.attr['slp']) elif opt['val'] == True: unicurses.waddstr(self.scr.win, " +", attr) elif opt['val'] == (None, ): unicurses.waddstr(self.scr.win, " unchanged", self.scr.attr['slp']) elif isinstance(opt['val'], (basestring, list)): if len(opt['text']) > 0: unicurses.waddstr(self.scr.win, " ") value = opt['val'] if isinstance(value, list): value = ','.join(value) if '*' in opt['act']: value = '*' * len(value) self.dotstr(value, attr) if unicurses.getyx(self.scr.win)[1] > 0: unicurses.waddstr(self.scr.win, "\n") unicurses.waddstr(self.scr.win, "\n")
def draw_board(self): uc.wclear(self.win_board) offset_x = 2 offset_y = 1 uc.box(self.win_boardarea, 0, 0) uc.wmove(self.win_boardarea, offset_y, offset_x) if self.on_menu: s = self.player.board.__str__() else: s = self.player.board.show_new_ship(self.new_ship_x, self.new_ship_y, self.menu[self.menu_hi].id, self.menu[self.menu_hi].length, self.new_ship_hor) uc.init_pair(11, uc.COLOR_BLUE, uc.COLOR_BLACK) uc.init_pair(12, uc.COLOR_WHITE, uc.COLOR_BLACK) uc.init_pair(13, uc.COLOR_RED, uc.COLOR_BLACK) for chr in s: if chr == '~': uc.wattron(self.win_board, uc.COLOR_PAIR(11)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(11)) elif chr == 'O': uc.wattron(self.win_board, uc.COLOR_PAIR(12)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(12)) elif chr == '#': uc.wattron(self.win_board, uc.COLOR_PAIR(13)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(13)) else: uc.wattron(self.win_board, uc.COLOR_PAIR(12)) uc.waddstr(self.win_board, chr) uc.wattroff(self.win_board, uc.COLOR_PAIR(12)) uc.wbkgd(self.win_statusbar, uc.COLOR_PAIR(11)) uc.wrefresh(self.win_boardarea) uc.update_panels()
def render(self): unicurses.wclear(self.scr.win) if self.scr.msg != None: unicurses.wmove(self.scr.win, self.scr.h - 1, 0) if isinstance(self.scr.msg, basestring): unicurses.waddnstr(self.scr.win, self.scr.msg, self.scr.w) elif isinstance(self.scr.msg, tuple): attr = self.scr.attr[self.scr.msg[0]] msg = self.scr.msg[1] unicurses.waddnstr(self.scr.win, msg, self.scr.w, attr) unicurses.wmove(self.scr.win, 0, 0) unicurses.waddstr(self.scr.win, self.titlebar, self.scr.attr['ttl']) for opt in self.curropts: if opt == None: unicurses.waddstr(self.scr.win, "\n") continue key = opt['key'] + ": " unicurses.waddstr(self.scr.win, key, self.scr.attr['key']) if not isinstance(opt['val'], basestring): pad = 2 if opt['stat'] != True else 0 if opt['wait'] == True or opt['val'] == True: pad += 2 self.dotstr(opt['text'], None, pad) else: unicurses.waddstr(self.scr.win, opt['text']) attr = self.scr.attr['val'] if opt['stat'] != True: attr = self.scr.attr['err'] unicurses.waddstr(self.scr.win, " !", attr) if opt['wait'] == True: unicurses.waddstr(self.scr.win, " ~", self.scr.attr['slp']) elif opt['val'] == True: unicurses.waddstr(self.scr.win, " +", attr) elif opt['val'] == (None,): unicurses.waddstr(self.scr.win, " unchanged", self.scr.attr['slp']) elif isinstance(opt['val'], (basestring, list)): if len(opt['text']) > 0: unicurses.waddstr(self.scr.win, " ") value = opt['val'] if isinstance(value, list): value = ','.join(value) if '*' in opt['act']: value = '*'*len(value) self.dotstr(value, attr) if unicurses.getyx(self.scr.win)[1] > 0: unicurses.waddstr(self.scr.win, "\n") unicurses.waddstr(self.scr.win, "\n")
def draw_shipmenu(self): uc.wclear(self.win_shipmenu) offset_x = 2 offset_y = 1 uc.box(self.win_shipmenu, 0, 0) uc.wmove(self.win_shipmenu, offset_y, offset_x) uc.waddstr(self.win_shipmenu, 'ship menu:') offset_y += 2 for ele in self.menu: if (self.on_menu and self.menu_hi == self.menu.index(ele)): uc.wattron(self.win_shipmenu, uc.A_REVERSE) uc.mvwaddstr(self.win_shipmenu, offset_y, offset_x, ele) uc.wattroff(self.win_shipmenu, uc.A_REVERSE) offset_y += 2 else: uc.mvwaddstr(self.win_shipmenu, offset_y, offset_x, ele) offset_y += 2 uc.wrefresh(self.win_shipmenu)
def showHelp(self): string = "Type a command key for contextual help: " unicurses.waddstr(self.scr.win, string) while True: try: key = chr(self.scr.getch(self.scr.win)) except ValueError: continue if key in self.keymap and self.keymap[key]['help'] != None: if self.keymap[key]['help'] == False: break unicurses.wclear(self.scr.win) err = self.keymap[key]['stat'] if isinstance(err, basestring): attr = self.scr.attr['err'] unicurses.wmove(self.scr.win, self.scr.h - 1, 0) unicurses.waddnstr(self.scr.win, err, self.scr.w, attr) unicurses.wmove(self.scr.win, 0, 0) title = self.keymap[key]['text'] title = ' '*(self.scr.w - len(title) - 6) + title + " Help \n" unicurses.waddstr(self.scr.win, title, self.scr.attr['ttl']) unicurses.waddstr(self.scr.win, self.keymap[key]['help']) string = "\n\nPress 'q' to exit help.\n\n" unicurses.waddstr(self.scr.win, string) while chr(self.scr.getch(self.scr.win)) != 'q': pass break
def __init__(self): self.turn =1 #Create debugging display debug_win = uc.newwin(15, 30, 0, 0) uc.box(debug_win) uc.wmove(debug_win, 1, 1) uc.waddstr(debug_win, "Debug:") uc.wmove(debug_win, 2, 1) debug_pnl = uc.new_panel(debug_win) uc.move_panel(debug_pnl, glob.CAM_HEIGHT, 32) #Generate the world self.world = Map(glob.N_HEX_ROWS, glob.N_HEX_COLS, debug_win) #map_height: 3 + 2*(rows-1) + 2 for border #map_width: 5 + 4*cols + 2 for border map_win = uc.newwin(glob.CAM_HEIGHT, glob.CAM_WIDTH, 0, 0) self.painter = Painter(glob.N_HEX_ROWS, glob.N_HEX_COLS, map_win) self.painter.updateAllTiles(self.world) #Put world window into a panel map_pnl = uc.new_panel(map_win) uc.move_panel(map_pnl, 1, 1) uc.top_panel(debug_pnl) self.status_win = uc.newwin(10, 30, 0, 0) uc.box(self.status_win) uc.wmove(self.status_win, 1, 1) uc.waddstr(self.status_win, "Turn " + str(self.turn)) status_pnl = uc.new_panel(self.status_win) uc.move_panel(status_pnl, glob.CAM_HEIGHT, 2) self.tile_window = TileWindow() self.painter.drawWindow() showChanges() #input loop while True: sys.stdout.flush() ch = uc.getch() uc.waddstr(debug_win, str(ch)) #Exit Key if ch == Key.ESC: break #Movement Keys elif ch == Key.E: self.movePlayer(HexDir.UL) elif ch == Key.R: self.movePlayer(HexDir.UR) elif ch == Key.S: self.movePlayer(HexDir.L) elif ch == Key.G: self.movePlayer(HexDir.R) elif ch == Key.D: self.movePlayer(HexDir.DL) elif ch == Key.F: self.movePlayer(HexDir.DR) #End Turn Key elif ch == Key.SPACE: self.incrementTurn() #Camera Scrolling Keys #TBD: Remaining order checks elif ch == uc.KEY_UP: self.painter.moveCamera(0, -1*glob.CAM_SPEED) elif ch == uc.KEY_DOWN: self.painter.moveCamera(0, glob.CAM_SPEED) elif ch == uc.KEY_LEFT: self.painter.moveCamera(-1*glob.CAM_SPEED, 0) elif ch == uc.KEY_RIGHT: self.painter.moveCamera(glob.CAM_SPEED, 0) #Toggle drawing borders elif ch == Key.B: self.painter.draw_borders = not self.painter.draw_borders self.painter.updateAllTileBorders(self.world) self.painter.drawWindow()
def showLineEdit(self, sel, quiet): maxwidth = 4096 fw, hw = self.scr.w - 2, (self.scr.w - 2)/2 unicurses.waddstr(self.scr.win, "Edit " + sel['text'] + ":\n> ") unicurses.noutrefresh(self.scr.win) (yn, xn) = unicurses.getyx(self.scr.win) buf = unicurses.newpad(1, maxwidth + 1) unicurses.keypad(buf, 1) offs, length, submit = 0, 0, False if not quiet and sel['val'] != None: length = len(sel['val']) unicurses.waddstr(buf, sel['val']) offs = max(0, length/hw - 1) (yb, xb) = unicurses.getbegyx(self.scr.win) (yp, xp) = (yn + yb, xn + xb) unicurses.prefresh(buf, 0, offs*hw, yp, xp, yp, xp + fw - 1) unicurses.doupdate() while True: if quiet: ch = self.scr.getch(buf) else: etc = (buf, 0, offs*hw, yn, xn, yn, xn + fw - 1) ch = self.scr.getch(buf, etc) (y, x) = unicurses.getyx(buf) if ch in (ord('\n'), unicurses.KEY_ENTER): sel['val'] = unicurses.mvwinstr(buf, 0, 0, length) if sel['val'] != None and len(sel['val']) == 0: sel['val'] = None submit = True break elif ch == ord('\x1b'): break elif ch == unicurses.KEY_HOME and x > 0: unicurses.wmove(buf, y, 0) elif ch == unicurses.KEY_END and x < length: unicurses.wmove(buf, y, length) elif ch == unicurses.KEY_LEFT and x > 0: unicurses.wmove(buf, y, x - 1) elif ch == unicurses.KEY_RIGHT and x < length: unicurses.wmove(buf, y, x + 1) elif ch in (ord('\b'), ord('\x7f'), unicurses.KEY_BACKSPACE, unicurses.KEY_DC) and x > 0: unicurses.wmove(buf, y, x - 1) unicurses.wdelch(buf) length -= 1 elif ord(' ') <= ch <= ord('~') and length < maxwidth: unicurses.winsstr(buf, chr(ch)) length += 1 unicurses.wmove(buf, y, x + 1) else: continue if quiet: continue oldoffs, offs = offs, max(0, unicurses.getyx(buf)[1]/hw - 1) if oldoffs < offs and maxwidth - offs*hw < fw: unicurses.wclrtoeol(self.scr.win) unicurses.noutrefresh(self.scr.win) (yb, xb) = unicurses.getbegyx(self.scr.win) (yp, xp) = (yn + yb, xn + xb) unicurses.prefresh(buf, 0, offs*hw, yp, xp, yp, xp + fw - 1) unicurses.doupdate() return submit
def draw_title(self): uc.box(self.win_title, 0, 0) uc.wmove(self.win_title, 1, 2) uc.waddstr(self.win_title, self.str_title) uc.wrefresh(self.win_title)
def showLineEdit(self, sel, quiet): maxwidth = 4096 fw, hw = self.scr.w - 2, (self.scr.w - 2) / 2 unicurses.waddstr(self.scr.win, "Edit " + sel['text'] + ":\n> ") unicurses.noutrefresh(self.scr.win) (yn, xn) = unicurses.getyx(self.scr.win) buf = unicurses.newpad(1, maxwidth + 1) unicurses.keypad(buf, 1) offs, length, submit = 0, 0, False if not quiet and sel['val'] != None and sel['val'] != (None, ): length = len(sel['val']) unicurses.waddstr(buf, sel['val']) offs = max(0, length / hw - 1) (yb, xb) = unicurses.getbegyx(self.scr.win) (yp, xp) = (yn + yb, xn + xb) unicurses.prefresh(buf, 0, offs * hw, yp, xp, yp, xp + fw - 1) unicurses.doupdate() while True: if quiet: ch = self.scr.getch(buf, redact=True) else: etc = (buf, 0, offs * hw, yn, xn, yn, xn + fw - 1) ch = self.scr.getch(buf, etc) (y, x) = unicurses.getyx(buf) if ch in (ord('\n'), unicurses.KEY_ENTER): sel['val'] = unicurses.mvwinstr(buf, 0, 0, length) if sel['val'] != None and len(sel['val']) == 0: sel['val'] = None submit = True break elif ch == ord('\x1b'): break elif ch == unicurses.KEY_HOME and x > 0: unicurses.wmove(buf, y, 0) elif ch == unicurses.KEY_END and x < length: unicurses.wmove(buf, y, length) elif ch == unicurses.KEY_LEFT and x > 0: unicurses.wmove(buf, y, x - 1) elif ch == unicurses.KEY_RIGHT and x < length: unicurses.wmove(buf, y, x + 1) elif ch in (ord('\b'), ord('\x7f'), unicurses.KEY_BACKSPACE, unicurses.KEY_DC) and x > 0: unicurses.wmove(buf, y, x - 1) unicurses.wdelch(buf) length -= 1 elif ord(' ') <= ch <= ord('~') and length < maxwidth: unicurses.winsstr(buf, chr(ch)) length += 1 unicurses.wmove(buf, y, x + 1) else: continue if quiet: continue oldoffs, offs = offs, max(0, unicurses.getyx(buf)[1] / hw - 1) if oldoffs < offs and maxwidth - offs * hw < fw: unicurses.wclrtoeol(self.scr.win) unicurses.noutrefresh(self.scr.win) (yb, xb) = unicurses.getbegyx(self.scr.win) (yp, xp) = (yn + yb, xn + xb) unicurses.prefresh(buf, 0, offs * hw, yp, xp, yp, xp + fw - 1) unicurses.doupdate() return submit