Skip to content

Commit

Permalink
kvui: allow sorting hints in the hint tab (ArchipelagoMW#2684)
Browse files Browse the repository at this point in the history
  • Loading branch information
Berserker66 authored and EmilyV99 committed Apr 15, 2024
1 parent e29c93c commit c004ba1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
6 changes: 6 additions & 0 deletions data/client.kv
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
57 changes: 43 additions & 14 deletions kvui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import sys
import typing
import re

if sys.platform == "win32":
import ctypes
Expand Down Expand Up @@ -72,6 +73,8 @@
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):
Expand Down Expand Up @@ -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__()
Expand All @@ -321,9 +323,7 @@ def set_height(self, instance, value):

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"]
Expand All @@ -337,24 +337,44 @@ def on_touch_down(self, touch):
""" 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


Expand Down Expand Up @@ -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"]})},
Expand All @@ -672,7 +692,16 @@ def refresh_hints(self, hints):
"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):
Expand Down

0 comments on commit c004ba1

Please sign in to comment.