def update_mouse_hovering(self): mouse_x = bl.state(bl.TK_MOUSE_X) mouse_y = bl.state(bl.TK_MOUSE_Y) if mouse_x >= self.x and \ mouse_x <= self.x2 and \ mouse_y >= self.y and \ mouse_y <= self.y2: self.mouse_hovering = True else: self.mouse_hovering = False
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate total height for the header (after auto-wrap) and one line per option #header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)!!! header_height = len(textwrap.wrap(header, width)) height = len(options) + header_height #set co-ords of menu menu_x = SCREEN_WIDTH/2 - width/2 menu_y = SCREEN_HEIGHT/2 - height/2 #paint menu background terminal.layer(5) terminal.color(MENU_BACKGROUND_COLOR) #menu for y_bg in range(height): for x_bg in range(width): terminal.put(menu_x + x_bg, menu_y + y_bg, 20) #print the header, with auto-wrap terminal.layer(6) terminal.color('#FFFFFF') y = 0 for line in textwrap.wrap(header, width): terminal.print_(menu_x, menu_y + y, line) y += 1 #position of options, below header (y) y = menu_y + header_height letter_index = ord('a') #print all the options for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text #libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) terminal.print_(menu_x, y, text) y += 1 letter_index += 1 #present the root console to the player and wait for a key-press terminal.refresh() key = terminal.read() #temporary halt of operation, wait for keypress/click #clear the menu from screen terminal.layer(5) terminal.clear_area(menu_x, menu_y, width, height) if terminal.state(terminal.TK_CHAR): #!! maybe no if statement here? #convert the ASCII code to an index; if it corresponds to an option, return it index = terminal.state(terminal.TK_CHAR) - ord('a') if index >= 0 and index < len(options): return index return None
def get_names_under_mouse(): #global mouse #mouse = terminal.read() #if mouse == terminal.TK_MOUSE_MOVE: # print terminal.state #return a string with the names of all objects under the mouse (x, y) = (terminal.state(terminal.TK_MOUSE_X), terminal.state(terminal.TK_MOUSE_Y)) #!! #create a list with the names of all objects at the mouse's coordinates and in FOV names = [obj.name for obj in objects if obj.x == x and obj.y == y and libtcod.map_is_in_fov(fov_map, obj.x, obj.y)] names = ', '.join(names) #join the names, separated by commas return names.capitalize()
def target_tile(max_range=None): #return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked. global key, mouse while True: #render the screen. this erases the inventory and shows the names of objects under the mouse. render_all() terminal.refresh() key = terminal.read() #temporary halt of operation, wait for keypress/click #render_all() (x, y) = (terminal.state(terminal.TK_MOUSE_X), terminal.state(terminal.TK_MOUSE_Y)) #if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: if key == terminal.TK_ESCAPE or key == terminal.TK_MOUSE_RIGHT: return (None, None) #cancel if the player right-clicked or pressed Escape #accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range #if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and if (key == terminal.TK_MOUSE_LEFT and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y)
class AtMan(MapChild): def __init__(self, x, y, parent_map): MapChild.__init__(self, x, y, '@', parent_map) eventhandler.register(self, *ARROW_EVENTS) def receive_event(self, event): if event == bl.TK_RIGHT: new_pos = (self.x + 1, self.y) elif event == bl.TK_LEFT: new_pos = (self.x - 1, self.y) elif event == bl.TK_UP: new_pos = (self.x, self.y - 1) elif event == bl.TK_DOWN: new_pos = (self.x, self.y + 1) self.parent_map.move(self, new_pos) bl.open() bl.refresh() SCREEN_WIDTH = bl.state(bl.TK_WIDTH) SCREEN_HEIGHT = bl.state(bl.TK_HEIGHT) the_map = Map(SCREEN_WIDTH, SCREEN_HEIGHT, 3, 3, 9, 9) at = AtMan(5, 5, the_map) left_apostrophe = MapChild(0, 1, "'", the_map) right_apostrophe = MapChild(2, 1, "'", the_map) dud = MapChild(4, 4, "\u2193", the_map) the_map.draw_view() eventhandler.register(the_map, *ARROW_EVENTS + WASD_EVENTS) menu = ui.Menu(60, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1) paint(gridtools.hollow_rectangle(2, 2, 10, 10)) paint(gridtools.rectangle(60, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1))