diff --git a/data/client.kv b/data/client.kv index 3b48d216..bf98fa15 100644 --- a/data/client.kv +++ b/data/client.kv @@ -61,36 +61,42 @@ found_text: "Found?" TooltipLabel: id: receiving + sort_key: 'receiving' text: root.receiving_text halign: 'center' valign: 'center' pos_hint: {"center_y": 0.5} TooltipLabel: id: item + sort_key: 'item' text: root.item_text halign: 'center' valign: 'center' pos_hint: {"center_y": 0.5} TooltipLabel: id: finding + sort_key: 'finding' text: root.finding_text halign: 'center' valign: 'center' pos_hint: {"center_y": 0.5} TooltipLabel: id: location + sort_key: 'location' text: root.location_text halign: 'center' valign: 'center' pos_hint: {"center_y": 0.5} TooltipLabel: id: entrance + sort_key: 'entrance' text: root.entrance_text halign: 'center' valign: 'center' pos_hint: {"center_y": 0.5} TooltipLabel: id: found + sort_key: 'found' text: root.found_text halign: 'center' valign: 'center' diff --git a/kvui.py b/kvui.py index 5e1b0fc0..1cd1c998 100644 --- a/kvui.py +++ b/kvui.py @@ -2,6 +2,7 @@ import os import logging import sys import typing +import re if sys.platform == "win32": import ctypes @@ -72,6 +73,8 @@ if typing.TYPE_CHECKING: else: context_type = object +remove_between_brackets = re.compile(r"\[.*?]") + # I was surprised to find this didn't already exist in kivy :( class HoverBehavior(object): @@ -303,7 +306,6 @@ class HintLabel(RecycleDataViewBehavior, BoxLayout): selected = BooleanProperty(False) striped = BooleanProperty(False) index = None - no_select = [] def __init__(self): super(HintLabel, self).__init__() @@ -321,9 +323,7 @@ class HintLabel(RecycleDataViewBehavior, BoxLayout): def refresh_view_attrs(self, rv, index, data): self.index = index - if "select" in data and not data["select"] and index not in self.no_select: - self.no_select.append(index) - self.striped = data["striped"] + self.striped = data.get("striped", False) self.receiving_text = data["receiving"]["text"] self.item_text = data["item"]["text"] self.finding_text = data["finding"]["text"] @@ -337,24 +337,44 @@ class HintLabel(RecycleDataViewBehavior, BoxLayout): """ Add selection on touch down """ if super(HintLabel, self).on_touch_down(touch): return True - if self.index not in self.no_select: + if self.index: # skip header if self.collide_point(*touch.pos): if self.selected: self.parent.clear_selection() else: - text = "".join([self.receiving_text, "\'s ", self.item_text, " is at ", self.location_text, " in ", + text = "".join((self.receiving_text, "\'s ", self.item_text, " is at ", self.location_text, " in ", self.finding_text, "\'s World", (" at " + self.entrance_text) if self.entrance_text != "Vanilla" - else "", ". (", self.found_text.lower(), ")"]) + else "", ". (", self.found_text.lower(), ")")) temp = MarkupLabel(text).markup text = "".join( part for part in temp if not part.startswith(("[color", "[/color]", "[ref=", "[/ref]"))) Clipboard.copy(escape_markup(text).replace("&", "&").replace("&bl;", "[").replace("&br;", "]")) return self.parent.select_with_touch(self.index, touch) + else: + parent = self.parent + parent.clear_selection() + parent: HintLog = parent.parent + # find correct column + for child in self.children: + if child.collide_point(*touch.pos): + key = child.sort_key + parent.hint_sorter = lambda element: remove_between_brackets.sub("", element[key]["text"]).lower() + if key == parent.sort_key: + # second click reverses order + parent.reversed = not parent.reversed + else: + parent.sort_key = key + parent.reversed = False + break + else: + logging.warning("Did not find clicked header for sorting.") + + App.get_running_app().update_hints() def apply_selection(self, rv, index, is_selected): """ Respond to the selection of items in the view. """ - if self.index not in self.no_select: + if self.index: self.selected = is_selected @@ -646,20 +666,20 @@ class HintLog(RecycleView): "entrance": {"text": "[u]Entrance[/u]"}, "found": {"text": "[u]Status[/u]"}, "striped": True, - "select": False, } + sort_key: str = "" + reversed: bool = False + def __init__(self, parser): super(HintLog, self).__init__() self.data = [self.header] self.parser = parser def refresh_hints(self, hints): - self.data = [self.header] - striped = False + data = [] for hint in hints: - self.data.append({ - "striped": striped, + data.append({ "receiving": {"text": self.parser.handle_node({"type": "player_id", "text": hint["receiving_player"]})}, "item": {"text": self.parser.handle_node( {"type": "item_id", "text": hint["item"], "flags": hint["item_flags"]})}, @@ -672,7 +692,16 @@ class HintLog(RecycleView): "text": self.parser.handle_node({"type": "color", "color": "green" if hint["found"] else "red", "text": "Found" if hint["found"] else "Not Found"})}, }) - striped = not striped + + data.sort(key=self.hint_sorter, reverse=self.reversed) + for i in range(0, len(data), 2): + data[i]["striped"] = True + data.insert(0, self.header) + self.data = data + + @staticmethod + def hint_sorter(element: dict) -> str: + return "" class E(ExceptionHandler):