Skip to content

Commit

Permalink
add links for the word tree
Browse files Browse the repository at this point in the history
  • Loading branch information
LupaDevStudio committed Feb 1, 2024
1 parent 3a84f81 commit b3119b4
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.xar

# Ignore the reports folder
reports
report.html

# Ignore screenshots
screenshot*
Expand Down
2 changes: 1 addition & 1 deletion data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pylint
pylint-report
2 changes: 1 addition & 1 deletion screens/custom_widgets/tree.kv
Original file line number Diff line number Diff line change
Expand Up @@ -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"

<WordButton>:
Expand Down
108 changes: 83 additions & 25 deletions screens/custom_widgets/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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):
Expand All @@ -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 = (
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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] = (
Expand Down
9 changes: 5 additions & 4 deletions screens/game.kv
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions screens/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b3119b4

Please sign in to comment.