def button_box(w, h, text, color, offset='', bg=''): mouse_over = False width = terminal.measure(text)[0] mouse = (terminal.state(terminal.TK_MOUSE_X), terminal.state(terminal.TK_MOUSE_Y)) if (mouse[1] <= h + 2 and mouse[1] >= h) and (mouse[0] <= w + width and mouse[0] >= w): color = '[color=' + color + ']' text = color + text mouse_over = True else: color = '' text = bg + text draw_rect(w, h, terminal.measure(text)[0] + 2, 3, color=color) terminal.printf(w + 1, h + 1, text) return mouse_over
def draw_messages(self): terminal.bkcolor(0xFF000000) terminal.color(0xFFFFFFFF) # Load new messages into a list messages = [] for entity, message in self.world.get_component(Message): messages.append(message) self.world.delete_entity(entity) # Sort by priority messages.sort(key=lambda m: m.priority, reverse=True) prefix = "[color=#FF666666]>[/color] " for index, message in enumerate(messages): self.buffer.append(prefix + message.text) prefix = " " self.buffer = self.buffer[-14:] s = "\n".join(self.buffer) w, h = terminal.measure(s, 50, 14) if h < 14: align = terminal.TK_ALIGN_TOP else: align = terminal.TK_ALIGN_BOTTOM terminal.puts(x=34, y=7, s=s, width=50, height=14, align=align)
def update_heights(self, width): self.width = width self.heights = [blt.measure(text, width)[1] for text in self.texts] # print('log height', self.heights) # recompute total height, including the blank lines between messages if len(self.texts) <= self.total_height: return self.total_height = len(self.texts) + sum(self.heights) - 1
def add_message(self, message, color='white'): """Add message to log. Wraps messages to width and deletes lines when buffer is full. """ _, num_lines = blt.measure(message, self.width, self.height) self.lines += num_lines while self.lines >= self.height: oldest_message = self.messages.popleft() self.lines -= oldest_message.lines self.messages.append( Message(text=message, color=color, lines=num_lines))
def calculate_select_box_dimension(ctrl): w, h = 3, 3 for item in ctrl.items: w = max(len(item.label), w) for item in ctrl.items: box = "[bbox={}]".format(w) (_, m) = terminal.measure(box + item.label) h = max(m, h) return w, h
def button_text(w, h, text, color, offset='', bg=''): mouse_over = False width = terminal.measure(text)[0] mouse = (terminal.state(terminal.TK_MOUSE_X), terminal.state(terminal.TK_MOUSE_Y)) if h == mouse[1] and (mouse[0] <= w + width and mouse[0] >= w): text = '[color=' + color + ']' + text mouse_over = True else: text = bg + text terminal.printf(w, h, text) return mouse_over
def draw_history(history: History): r = settings.status_rect x, y = r.x + 1, r.bottom - 2 color = "white" for msgtime, msg in reversed(history.messages): if y <= r.y: return s = "{} [color={}][bbox={}]{}".format(msgtime.strftime("%H:%M:%S"), color, r.width, msg) terminal.print_(x, y, s) (_, mh) = terminal.measure(s) y -= mh color = "dark gray"
def button_sliding(w, h, text, color, key=False): mouse_over = False width = terminal.measure(text)[0] mouse = (terminal.state(terminal.TK_MOUSE_X), terminal.state(terminal.TK_MOUSE_Y)) if key or (h == mouse[1] or h == mouse[1] - 1) and (mouse[0] <= w + width and mouse[0] >= w): text = text mouse_over = True else: text = '[color=' + color + '] ' + text terminal.printf(w, h, text) return mouse_over
def draw_inventory_state_items(items, selected_index): line_x = settings.gui_rect.x + 1 line_y = settings.gui_rect.y + 3 line_w = settings.gui_rect.width - 3 item_w = 2 item_h = 3 index = 0 for item in items: text_x = line_x + 4 text_y = line_y + 1 if index == selected_index: item_bg = colors.inventory_item_hover_bg item_fg = colors.inventory_item_hover_fg else: item_bg = colors.inventory_bk_color item_fg = colors.inventory_item_fg label = "[bbox={}][color=white] {}[/color]".format( line_w, item.description) _, mh = terminal.measure(label) cy = mh # draw icon terminal.bkcolor(colors.inventory_bk_color) terminal.color(colors.white) draw_corners(line_x, line_y, line_x + item_w, line_y + item_w) terminal.color(item.color) terminal.put(line_x + 1, line_y + 1, item.char) # draw highlight terminal.bkcolor(item_bg) terminal.clear_area(text_x, line_y, line_w - 4, item_h) # draw text terminal.print_(text_x, text_y, label) # restore background color terminal.bkcolor(colors.black) # calculations line_y += max(3, cy + 1) index += 1
def draw_select_box(control, x, y): padding_left = 2 w, h = calculate_select_box_dimension(control) w += padding_left index = 0 py = 0 for item in control.items: color = colors.white if item.active and control.item_focused_index == index: color = colors.yellow elif not item.active: color = colors.gray box = "[bbox={}]".format(w - padding_left) (_, height) = terminal.measure(box + item.label) terminal.color(color) terminal.print_(x + 2, y + py, box + item.label) if index == control.item_focused_index: terminal.color(color) terminal.put(x, y + py, ">") py += height index += 1
def update_heights(self, width): self.heights = [blt.measure(text, width)[1] for text in self.texts] # recompute total height, including the blank lines between messages self.total_height = sum(self.heights) + len(self.texts) - 1
def intrinsic_size(self): width, height = terminal.measure(self.text) return Size(width, height)
def center_text(self, string): offset = int(terminal.measure(string)[0] / 2) return (self.window_center - offset)
def write_center(y: int, text: str): screen_w = int(terminal.get("ini.game.screen_width", "100")) w, _ = terminal.measure(text) x = (screen_w - w) // 2 terminal.print_(x, y, text)