From b3119b41d7d35cb5f7139918c4e4f18ee768618e Mon Sep 17 00:00:00 2001 From: LupaDevStudio Date: Thu, 1 Feb 2024 13:26:16 +0100 Subject: [PATCH] add links for the word tree --- .gitignore | 2 +- data.json | 2 +- pyproject.toml | 2 +- requirements_dev.txt | 2 + screens/custom_widgets/tree.kv | 2 +- screens/custom_widgets/tree.py | 108 +++++++++++++++++++++++++-------- screens/game.kv | 9 +-- screens/game.py | 2 + 8 files changed, 96 insertions(+), 33 deletions(-) create mode 100644 requirements_dev.txt diff --git a/.gitignore b/.gitignore index dbdc0e4..905929e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ *.xar # Ignore the reports folder -reports +report.html # Ignore screenshots screenshot* diff --git a/data.json b/data.json index 45a2c0a..4ce3148 100644 --- a/data.json +++ b/data.json @@ -20,7 +20,7 @@ "sound_volume": 0.46621621621621623, "current_theme_image": "japanese_1", "current_music": "kids_party", - "current_theme_colors": "japanese_2" + "current_theme_colors": "japanese_1" }, "unlocked_themes": { "japanese_1": { diff --git a/pyproject.toml b/pyproject.toml index efb7d8b..d58f108 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,5 +34,5 @@ reports = "y" # [tool.pytest.ini_options] # addopts = "--ignore=.buildozer --cov-report html:reports/coverage --cov-report xml --cov" -# To generate html report with pytlint : python -m pylint . | pylint_report > reports/linting/report.html +# To generate html report with pytlint : python -m pylint . | pylint_report > report.html # To generate html reports with pytest : pytest . --ignore=.buildozer --cov-report html:reports/coverage --cov-report xml --cov diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..03663db --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,2 @@ +pylint +pylint-report \ No newline at end of file diff --git a/screens/custom_widgets/tree.kv b/screens/custom_widgets/tree.kv index 9e5cd11..f81dbb1 100644 --- a/screens/custom_widgets/tree.kv +++ b/screens/custom_widgets/tree.kv @@ -13,7 +13,7 @@ rgba: self.color Line: width: 2 - bezier: [self.x, self.y,self.center_x, self.y,self.center_x, self.top] + bezier: [self.x, self.top, self.right, self.top,self.x, self.y, self.right, self.y] cap: "none" : diff --git a/screens/custom_widgets/tree.py b/screens/custom_widgets/tree.py index e411871..6cfece8 100644 --- a/screens/custom_widgets/tree.py +++ b/screens/custom_widgets/tree.py @@ -34,13 +34,13 @@ PATH_TEXT_FONT ) from tools.constants import ( - MAIN_BUTTON_FONT_SIZE, WORD_BUTTON_WIDTH_HINT, WORD_BUTTON_HEIGHT_HINT, WORD_BUTTON_HSPACING, WORD_BUTTON_VSPACING, WORD_BUTTON_SIDE_OFFSET ) +from tools.linconym import get_parent_position test_words_found = ["sea", "sale", "sell", "shell", "sail", "snail", "see", "bee", "tea", "pea", "peak", "keep", "tape", "pelt", "apes"] @@ -84,13 +84,6 @@ def build_sorted_positions_list(position_to_word_id: Dict[str, int]): """ positions_list = list(position_to_word_id.keys()) - - print(positions_list) - - def key(x: str): - print(x) - return (len(x[1].split(",")), convert_str_position_to_tuple_position(x[1])) - # sorted_positions_indices = argsort(positions_list, key=key) sorted_positions_indices = argsort(positions_list) sorted_positions_list = [positions_list[i] for i in sorted_positions_indices] @@ -151,7 +144,10 @@ class TreeScrollview(ScrollView): """ Class containing the scrollview to scroll over the tree. """ + font_ratio = NumericProperty(1) + primary_color = ListProperty([0.5, 0.5, 0.5, 1.]) + secondary_color = ListProperty([1., 1., 1., 1.]) class TreeLayout(RelativeLayout): @@ -173,7 +169,6 @@ def __init__( self.on_primary_color_change() self.bind(secondary_color=self.on_secondary_color_change) self.on_secondary_color_change() - self.build_layout() def on_primary_color_change(self, base=None, widget=None, value=None): self.transparent_primary_color = ( @@ -203,18 +198,63 @@ def compute_word_button_pos_hint(self, current_rank, current_vertical_offset): Pos hint for the word button. """ - top = 1 - (WORD_BUTTON_SIDE_OFFSET + - (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * - current_vertical_offset) / (WORD_BUTTON_SIDE_OFFSET + - (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * - self.max_vertical_offset) * (1 - WORD_BUTTON_HSPACING - WORD_BUTTON_HEIGHT_HINT / 2) - left = (WORD_BUTTON_SIDE_OFFSET + - (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * (current_rank - 1)) / (WORD_BUTTON_SIDE_OFFSET + - (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * self.max_rank) * (1 - WORD_BUTTON_SIDE_OFFSET - WORD_BUTTON_WIDTH_HINT / 2) + top = 1 - \ + (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * current_vertical_offset) /\ + (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * self.max_vertical_offset) *\ + (1 - WORD_BUTTON_HSPACING - WORD_BUTTON_HEIGHT_HINT / 2) + left = (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * (current_rank - 1)) /\ + (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * self.max_rank) *\ + (1 - WORD_BUTTON_SIDE_OFFSET - WORD_BUTTON_WIDTH_HINT / 2) pos_hint = {"top": top, "x": left} return pos_hint + def compute_word_link_pos_hint( + self, + current_rank: int, + current_vertical_offset: int, + parent_rank: int, + parent_vertical_offset: int): + """ + Compute the pos hint for a word link. + + Parameters + ---------- + current_rank : int + Rank of the current word. + current_vertical_offset : int + Vertical offset of the current word. + parent_rank : int + Rank of the parent word. + parent_vertical_offset : int + Vertical offset of the parent word. + + Returns + ------- + dict + Pos hint of the word link. + """ + + top = 1 - \ + (WORD_BUTTON_SIDE_OFFSET + WORD_BUTTON_HEIGHT_HINT / 2 + (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * parent_vertical_offset) /\ + (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * self.max_vertical_offset) *\ + (1 - WORD_BUTTON_HSPACING - WORD_BUTTON_HEIGHT_HINT / 2) + x = (WORD_BUTTON_SIDE_OFFSET + WORD_BUTTON_WIDTH_HINT + (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * (parent_rank - 1)) /\ + (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * self.max_rank) *\ + (1 - WORD_BUTTON_SIDE_OFFSET - WORD_BUTTON_WIDTH_HINT / 2) + y = top - (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * \ + (current_vertical_offset - parent_vertical_offset) /\ + (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * self.max_vertical_offset) *\ + (1 - WORD_BUTTON_HSPACING - WORD_BUTTON_HEIGHT_HINT / 2) + right = (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * (current_rank - 1)) /\ + (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * self.max_rank) *\ + (1 - WORD_BUTTON_SIDE_OFFSET - WORD_BUTTON_WIDTH_HINT / 2) + + pos_hint = {"top": top, "x": x} + size_hint = (right - x, top - y) + + return pos_hint, size_hint + def build_layout( self, position_to_word_id: Dict[str, int] = test_position_to_word_id, @@ -258,7 +298,6 @@ def build_layout( # Define the size of the layout self.size = (self.max_rank * 180 * self.font_ratio, self.max_vertical_offset * 60 * self.font_ratio) - print(self.size) # Define the initial vertical offset current_vertical_offset = 0 @@ -270,11 +309,14 @@ def build_layout( position_to_grid_position = {} # Compute the appropriate size - current_word_button_width_hint = WORD_BUTTON_WIDTH_HINT / (0.1 + - (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * self.max_rank) * (1 - WORD_BUTTON_HSPACING - WORD_BUTTON_WIDTH_HINT / 2) - current_word_button_height_hint = WORD_BUTTON_HEIGHT_HINT / (WORD_BUTTON_SIDE_OFFSET + - (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * - self.max_vertical_offset) * (1 - WORD_BUTTON_HSPACING - WORD_BUTTON_HEIGHT_HINT / 2) + current_word_button_width_hint =\ + WORD_BUTTON_WIDTH_HINT /\ + (0.1 + (WORD_BUTTON_WIDTH_HINT + WORD_BUTTON_HSPACING) * self.max_rank) \ + * (1 - WORD_BUTTON_HSPACING - WORD_BUTTON_WIDTH_HINT / 2) + current_word_button_height_hint =\ + WORD_BUTTON_HEIGHT_HINT /\ + (WORD_BUTTON_SIDE_OFFSET + (WORD_BUTTON_HEIGHT_HINT + WORD_BUTTON_VSPACING) * self.max_vertical_offset) *\ + (1 - WORD_BUTTON_HSPACING - WORD_BUTTON_HEIGHT_HINT / 2) # Iterate over the positions to display the widgets for position in sorted_positions_list: @@ -305,8 +347,24 @@ def build_layout( font_ratio=self.font_ratio) self.add_widget(word_widget) - # Add the link with the parent - ... + # Recover the parent position + parent_position = get_parent_position(position) + if parent_position is not None: + parent_rank, parent_vertical_offset = position_to_grid_position[parent_position] + + # Compute the pos hint of the link + word_link_pos_hint, word_link_size_hint = self.compute_word_link_pos_hint( + current_rank=current_rank, + current_vertical_offset=current_vertical_offset, + parent_rank=parent_rank, + parent_vertical_offset=parent_vertical_offset) + + # Add the link with the parent + word_link = WordLink( + color=self.primary_color, + pos_hint=word_link_pos_hint, + size_hint=word_link_size_hint) + self.add_widget(word_link) # Store the grid position position_to_grid_position[position] = ( diff --git a/screens/game.kv b/screens/game.kv index 1f228a8..700a4f5 100644 --- a/screens/game.kv +++ b/screens/game.kv @@ -98,12 +98,13 @@ pos_hint: {"center_x":0.5, "center_y":0.585} size_hint: (1,0.365) bar_color: root.primary_color - bar_inactive_color: root.secondary_color + bar_inactive_color: (0,0,0,0) font_ratio: root.font_ratio + primary_color: root.primary_color + secondary_color: root.secondary_color TreeLayout: id: tree_layout - # size_hint: (2,2) - # size: (800,800) - pos: (0,0) font_ratio: root.font_ratio + primary_color: root.primary_color + secondary_color: root.secondary_color diff --git a/screens/game.py b/screens/game.py index 944d58f..19d1053 100644 --- a/screens/game.py +++ b/screens/game.py @@ -83,6 +83,8 @@ def on_pre_enter(self, *args): self.ids.keyboard_layout.build_keyboard() self.nb_stars = USER_DATA.classic_mode[self.current_act_id][self.current_level_id]["nb_stars"] + + self.ids["tree_layout"].build_layout() return super().on_pre_enter(*args) def on_enter(self, *args):