def get_text_index(text: str) -> int: """ 计算文本最终显示的真实长度 Keyword arguments: text -- 要进行长度计算的文本 """ text_style_list = rich_text.set_rich_text_print(text, "standard") text_index = 0 style_width = 0 bar_list = list( text_loading.get_game_data(constant.FilePath.BAR_CONFIG_PATH).keys()) style_name_list = game_config.get_font_data_list() + bar_list for i in range(0, len(style_name_list)): style_text_head = "<" + style_name_list[i] + ">" style_text_tail = "</" + style_name_list[i] + ">" if style_text_head in text: if style_name_list[i] in bar_list: text = text.replace(style_text_head, "") text = text.replace(style_text_tail, "") else: text = text.replace(style_text_head, "") text = text.replace(style_text_tail, "") for i in range(len(text)): if text_style_list[i] in bar_list: text_width = text_loading.get_text_data( constant.FilePath.BAR_CONFIG_PATH, text_style_list[i])["width"] text_index = text_index + int(text_width) else: text_index += wcswidth(text[i]) return text_index + style_width
def one_by_one_print(sleep_time: float, string: str, style="standard"): """ 逐字绘制文本 Keyword arguments: sleep_time -- 逐字绘制时,绘制间隔时间 string -- 需要逐字绘制的文本 style -- 绘制文本的默认样式 (default 'standard') """ text_wait = cache_contorl.text_wait if text_wait != 0: time.sleep(text_wait) cache_contorl.wframe_mouse["w_frame_up"] = 0 style_list = rich_text.set_rich_text_print(string, style) style_name_list = game_config.get_font_data_list() for i in range(0, len(style_name_list)): style_text_head = "<" + style_name_list[i] + ">" style_text_tail = "</" + style_name_list[i] + ">" if style_text_head in string: string = string.replace(style_text_head, "") string = string.replace(style_text_tail, "") index = len(string) for i in range(0, index): normal_print(string[i], style_list[i]) time.sleep(sleep_time) if cache_contorl.wframe_mouse["w_frame_up"] == 1: index_i = i + 1 cache_contorl.wframe_mouse["w_frame_up"] = 2 for index_i in range(index_i, index): normal_print(string[index_i], style_list[index_i]) if cache_contorl.wframe_mouse["w_frame_line_state"] == 2: cache_contorl.wframe_mouse["w_frame_lines_up"] = 2 break
def set_rich_text_print(text_message: str, default_style: str) -> list: """ 获取文本的富文本样式列表 Keyword arguments: text_message -- 原始文本 default_style -- 无富文本样式时的默认样式 """ style_name_list = { key: 0 for key in game_config.get_font_data_list() + list( text_loading.get_game_data( constant.FilePath.BAR_CONFIG_PATH).keys()) } style_index = 0 style_last_index = None style_max_index = None style_list = [] for key in style_name_list: style_text_head = "<" + key + ">" if style_text_head in text_message: style_index = 1 if style_index == 0: style_list = [default_style] * len(text_message) else: for i in range(0, len(text_message)): input_text_style_size = text_message.find(">", i) + 1 input_text_style = text_message[i + 1:input_text_style_size - 1] if text_message[i] == "<" and ( (input_text_style in style_name_list) or (input_text_style[1:] in style_name_list)): style_last_index = i style_max_index = input_text_style_size if input_text_style[0] == "/": if cache_contorl.text_style_position["position"] == 1: cache_contorl.output_text_style = "standard" cache_contorl.text_style_position["position"] = 0 cache_contorl.text_style_cache = ["standard"] else: cache_contorl.text_style_position["position"] = ( cache_contorl.text_style_position["position"] - 1) cache_contorl.output_text_style = cache_contorl.text_style_cache[ cache_contorl.text_style_position["position"]] else: cache_contorl.text_style_position["position"] = len( cache_contorl.text_style_cache) cache_contorl.text_style_cache.append(input_text_style) cache_contorl.output_text_style = cache_contorl.text_style_cache[ cache_contorl.text_style_position["position"]] else: if style_last_index is not None: if i == len(text_message): cache_contorl.text_style_position["position"] = 0 cache_contorl.output_text_style = "standard" cache_contorl.text_style_cache = ["standard"] if i not in range(style_last_index, style_max_index): style_list.append(cache_contorl.output_text_style) else: style_list.append(cache_contorl.output_text_style) return style_list
def lines_center_print(sleep_time: float, string="", style="standard"): """ 将多行文本以居中的对齐方式进行逐字绘制 Keyword arguments: sleep_time -- 逐字的间隔时间 string -- 需要逐字绘制的文本 style -- 文本的默认样式 """ text_wait = cache_contorl.text_wait if text_wait != 0: time.sleep(text_wait) cache_contorl.wframe_mouse["w_frame_line_state"] = 1 string = str(string) string_list = string.split("\n") width = game_config.text_width style_name_list = game_config.get_font_data_list() string_center_list = "" for i in range(0, len(style_name_list)): style_text_head = "<" + style_name_list[i] + ">" style_text_tail = "</" + style_name_list[i] + ">" if style_text_head in string: string_center = string.replace(style_text_head, "") string_center = string_center.replace(style_text_tail, "") string_center_list = string_center.split("\n") else: string_center_list = string_list for i in range(0, len(string_list)): width_i = int(width) / 2 count_index = text_handle.get_text_index(string_center_list[i]) count_i = int(count_index) / 2 if cache_contorl.wframe_mouse["w_frame_re_print"] == 1: normal_print("\n") normal_print(" " * int((width_i - count_i))) normal_print(string_list[i]) else: normal_print(" " * int((width_i - count_i))) one_by_one_print(sleep_time, string_list[i]) normal_print("\n") if cache_contorl.wframe_mouse["w_frame_lines_up"] == 1: index_i_up = i + 1 cache_contorl.wframe_mouse["w_frame_lines_up"] = 2 for index_i_up in range(index_i_up, len(string_list)): restart_line_print( text_handle.align(string_list[index_i_up], "center"), style, ) cache_contorl.wframe_mouse["w_frame_line_state"] = 2 break cache_contorl.wframe_mouse["w_frame_re_print"] = 0
def remove_rich_cache(string: str) -> str: """ 移除文本中的富文本标签 Keyword arguments: string -- 原始文本 """ string = dictionaries.handle_text(string) bar_list = list( text_loading.get_game_data(constant.FilePath.BAR_CONFIG_PATH).keys()) style_name_list = game_config.get_font_data_list() + bar_list for i in range(0, len(style_name_list)): style_text_head = "<" + style_name_list[i] + ">" style_text_tail = "</" + style_name_list[i] + ">" if style_text_head in string: string = string.replace(style_text_head, "") string = string.replace(style_text_tail, "") return string
def init_style(): """ 富文本样式初始化 """ global style_def def new_style_def( style_name, foreground, background, font, fontsize, bold, underline, italic, ): frame_style_def( style_name, foreground, background, font, fontsize, bold, underline, italic, ) style_def = new_style_def style_list = game_config.get_font_data_list() standard_data = game_config.get_font_data("standard") style_data_list = [ "foreground", "background", "font", "fontSize", "bold", "underline", "italic", ] def_style_list = {} for i in range(0, len(style_list)): style_name = style_list[i] style_data = game_config.get_font_data(style_name) for index in range(0, len(style_data_list)): if style_data_list[index] in style_data: style_data_value = style_data[style_data_list[index]] else: style_data_value = standard_data[style_data_list[index]] def_style_list[style_data_list[index]] = style_data_value style_foreground = def_style_list["foreground"] style_background = def_style_list["background"] style_font = def_style_list["font"] style_font_size = def_style_list["fontSize"] style_bold = def_style_list["bold"] style_under_line = def_style_list["underline"] style_italic = def_style_list["italic"] style_def( style_name, style_foreground, style_background, style_font, style_font_size, style_bold, style_under_line, style_italic, )