Skip to content

Commit

Permalink
Merge branch 'main' of lupa.github.com:LupaDevStudio/Linconym
Browse files Browse the repository at this point in the history
  • Loading branch information
LupaDevStudio committed Feb 1, 2024
2 parents 4948344 + 72948d8 commit 9ff8597
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 18 deletions.
8 changes: 8 additions & 0 deletions screens/custom_widgets/tree.kv
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
RoundedRectangle:
pos:(0, 0)
size:self.size
# Border
Color:
rgba: root.outline_color
Line:
dash_offset: 4
dash_length: 7
width: OUTLINE_RECTANGLE_COLORED_ROUNDED_BUTTON
rounded_rectangle: (-OUTLINE_RECTANGLE_COLORED_ROUNDED_BUTTON/2, -OUTLINE_RECTANGLE_COLORED_ROUNDED_BUTTON/2, self.size[0]+OUTLINE_RECTANGLE_COLORED_ROUNDED_BUTTON,self.size[1]+OUTLINE_RECTANGLE_COLORED_ROUNDED_BUTTON, 12, 12, 12, 12)

# Main text
Label:
Expand Down
110 changes: 92 additions & 18 deletions screens/custom_widgets/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
### Local imports ###

from tools.basic_tools import argsort
from tools.kivy_tools import change_color_opacity
from tools.path import (
PATH_TEXT_FONT
)
Expand All @@ -40,7 +41,11 @@
WORD_BUTTON_VSPACING,
WORD_BUTTON_SIDE_OFFSET
)
from tools.linconym import get_parent_position
from tools.linconym import (
get_parent_position,
is_parent_of,
get_word_position
)

test_words_found = ["sea", "sale", "sell", "shell", "sail", "snail",
"see", "bee", "tea", "pea", "peak", "keep", "tape", "pelt", "apes"]
Expand Down Expand Up @@ -118,13 +123,11 @@ def __init__(
self,
text_font_name=PATH_TEXT_FONT,
text_filling_ratio=0.8,
release_function=lambda: 1 + 1,
font_ratio=None,
**kwargs):
if font_ratio is not None:
self.font_ratio = font_ratio
super().__init__(**kwargs)
self.release_function = release_function
self.always_release = True
self.text_font_name = text_font_name
self.text_filling_ratio = text_filling_ratio
Expand All @@ -137,7 +140,7 @@ def on_press(self):
def on_release(self):
if not self.disable_button:
self.background_color = self.temp_color
self.release_function()
self.parent.change_to_word(self.text)


class TreeScrollview(ScrollView):
Expand Down Expand Up @@ -171,15 +174,50 @@ def __init__(
self.on_secondary_color_change()

def on_primary_color_change(self, base=None, widget=None, value=None):
self.transparent_primary_color = (
self.primary_color[0], self.primary_color[1], self.primary_color[2], 0.7)
self.transparent_primary_color = change_color_opacity(
self.primary_color, 0.7)

def on_secondary_color_change(self, base=None, widget=None, value=None):
self.transparent_secondary_color = (
self.secondary_color[0], self.secondary_color[1], self.secondary_color[2], 0.7)

def change_current_position():
pass
self.transparent_secondary_color = change_color_opacity(
self.secondary_color, 0.7)

def change_to_word(self, current_word):
current_position = get_word_position(
current_word, self.position_to_word_id, self.words_found)
print(current_position)
if current_position is not None:
self.change_current_position(current_position)

def change_current_position(self, current_position):
self.current_position = current_position
for position in self.position_to_word_id.keys():
# Determine if the word is in the main branch
is_main_branch = is_parent_of(
position, child_position=current_position)
is_selected = current_position == position

if is_main_branch:
main_color = self.primary_color
touch_color = self.transparent_primary_color
if is_selected:
outline_color = (1, 1, 1, 1)
else:
outline_color = self.primary_color
else:
main_color = self.secondary_color
outline_color = self.secondary_color
touch_color = self.transparent_secondary_color

# Apply the color changes to the word button
word_button = self.word_button_dict[position]
word_button.background_color = main_color
word_button.outline_color = outline_color
word_button.touch_color = touch_color

# Apply the color changes to the word link
if position in self.word_link_dict:
word_link = self.word_link_dict[position]
word_link.color = main_color

def compute_word_button_pos_hint(self, current_rank, current_vertical_offset):
"""
Expand Down Expand Up @@ -259,7 +297,7 @@ def build_layout(
self,
position_to_word_id: Dict[str, int] = test_position_to_word_id,
words_found: List[str] = test_words_found,
current_position: str = "0,0"):
current_position: str = "0,0,1,0"):
"""
Build the layout of the tree.
Expand All @@ -273,6 +311,17 @@ def build_layout(
_description_
"""

# Store the tree infos
self.position_to_word_id = position_to_word_id
self.words_found = words_found
self.current_position = current_position

# Init a word button pile
self.word_button_dict = {}

# Init a word link pile
self.word_link_dict = {}

# Reorder the positions
sorted_positions_list = build_sorted_positions_list(
position_to_word_id)
Expand Down Expand Up @@ -331,21 +380,38 @@ def build_layout(
word_id = position_to_word_id[position]
word = words_found[word_id]

# Determine if the word is in the main branch
is_main_branch = is_parent_of(
position, child_position=current_position)
is_selected = current_position == position

if is_main_branch:
main_color = self.primary_color
touch_color = self.transparent_primary_color
if is_selected:
outline_color = (1, 1, 1, 1)
else:
outline_color = self.primary_color
else:
main_color = self.secondary_color
outline_color = self.secondary_color
touch_color = self.transparent_secondary_color

# Compute the pos hint of the word button
word_button_pos_hint = self.compute_word_button_pos_hint(
current_rank=current_rank,
current_vertical_offset=current_vertical_offset)

# Add the word widget
word_widget = WordButton(
word_button = WordButton(
text=word,
background_color=self.primary_color,
touch_color=self.transparent_primary_color,
background_color=main_color,
outline_color=outline_color,
touch_color=touch_color,
size_hint=(current_word_button_width_hint,
current_word_button_height_hint),
pos_hint=word_button_pos_hint,
font_ratio=self.font_ratio)
self.add_widget(word_widget)

# Recover the parent position
parent_position = get_parent_position(position)
Expand All @@ -361,10 +427,12 @@ def build_layout(

# Add the link with the parent
word_link = WordLink(
color=self.primary_color,
color=main_color,
pos_hint=word_link_pos_hint,
size_hint=word_link_size_hint)
self.add_widget(word_link)
self.word_link_dict[position] = word_link

self.word_button_dict[position] = word_button

# Store the grid position
position_to_grid_position[position] = (
Expand All @@ -373,6 +441,12 @@ def build_layout(
# Update the previous rank
previous_rank = current_rank

for pos in self.word_link_dict:
self.add_widget(self.word_link_dict[pos])

for pos in self.word_button_dict:
self.add_widget(self.word_button_dict[pos])

###############
### Testing ###
###############
Expand Down
1 change: 1 addition & 0 deletions tools/kivy_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from tools.kivy_tools.image_with_text import ImageWithText
from tools.kivy_tools.image_with_text_button import ImageWithTextButton
from tools.kivy_tools.image_button import ImageButton
from tools.kivy_tools.colors import change_color_opacity

###############
### Process ###
Expand Down
12 changes: 12 additions & 0 deletions tools/kivy_tools/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Module to manage colors with kivy.
"""

#################
### Functions ###
#################


def change_color_opacity(color, opacity):
new_color = (color[0], color[1], color[2], opacity)
return new_color
58 changes: 58 additions & 0 deletions tools/linconym.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
### Imports ###
###############

### Python imports ###

from typing import (
Dict,
List
)

### Local imports ###

from tools.path import (
PATH_GAMEPLAY,
PATH_RESOURCES_FOLDER
Expand Down Expand Up @@ -52,6 +61,55 @@ def get_parent_position(position: str):
return parent_pos


def is_parent_of(position, child_position):
"""
Check if the given position is parent of the given child position.
Parameters
----------
position : _type_
Position to check.
child_position : _type_
Position of the potential child.
Returns
-------
bool
Boolean indication if the given position is parent or not.
"""

sliced_child_position = child_position[:len(position)]

return sliced_child_position == position


def get_word_position(input_word: str, position_to_word_id: Dict[str, int], words_found: List[str]):
"""
Get the position of the given word.
Parameters
----------
input_word : str
Input word.
position_to_word_id : Dict[str, int]
Dictionnary containing positions for each word index.
words_found : List[str]
List of all words founds.
Returns
-------
str or None
Position or None if the word is not included in the list.
"""
for i, word in enumerate(words_found):
if input_word == word:
word_index = i
for index, position in enumerate(position_to_word_id):
if index == word_index:
return position
return None


def is_in_english_words(word: str):
"""
Indicates whether a word belongs to the english words dictionnary or not.
Expand Down

0 comments on commit 9ff8597

Please sign in to comment.