-
Notifications
You must be signed in to change notification settings - Fork 455
Storybook: Adding language bookmarks #2712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
953db09
aa79b6d
214502d
5802000
6b2ecec
fd9e327
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,12 @@ var _current_spread_index: int = -1 | |||||||||
| var _navigation_locked: bool = false | ||||||||||
| var _current_list_page: int = 0 | ||||||||||
|
|
||||||||||
| ## A copy of all quests loaded on ready (before filtering) | ||||||||||
| var _all_quests: Array[Quest] = [] | ||||||||||
| var _filtered_quests: Array[Quest] = [] | ||||||||||
|
|
||||||||||
| var _current_tab_index: int = 0 # 0: TOC, 1: EN, 2: ES | ||||||||||
|
|
||||||||||
| @onready var left_quest_list: VBoxContainer = %LeftQuestList | ||||||||||
| @onready var right_quest_list: VBoxContainer = %RightQuestList | ||||||||||
|
|
||||||||||
|
|
@@ -35,6 +41,11 @@ var _current_list_page: int = 0 | |||||||||
| @onready var animated_book: AnimatedSprite2D = %AnimatedSprite2D | ||||||||||
| @onready var ui_container: Control = %StoryBookContent | ||||||||||
|
|
||||||||||
| # Bookmark button references | ||||||||||
| @onready var toc_bookmark_button: Button = %TOCBookmark | ||||||||||
| @onready var en_bookmark_button: Button = %ENBookmark | ||||||||||
| @onready var es_bookmark_button: Button = %ESBookmark | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _fade_out_ui() -> void: | ||||||||||
| var tween := create_tween() | ||||||||||
|
|
@@ -51,6 +62,20 @@ func _fade_in_ui() -> void: | |||||||||
|
|
||||||||||
| func _ready() -> void: | ||||||||||
| animated_book.animation_finished.connect(_on_animation_finished) | ||||||||||
|
|
||||||||||
| # Saving a copy of all quests | ||||||||||
| _all_quests = quests | ||||||||||
|
Comment on lines
+66
to
+67
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| _filtered_quests = quests | ||||||||||
|
|
||||||||||
| # Connecting the bookmark buttons | ||||||||||
| if toc_bookmark_button: | ||||||||||
| toc_bookmark_button.pressed.connect(_on_toc_bookmark_pressed) | ||||||||||
| if en_bookmark_button: | ||||||||||
| en_bookmark_button.pressed.connect(_on_en_bookmark_pressed) | ||||||||||
| if es_bookmark_button: | ||||||||||
| es_bookmark_button.pressed.connect(_on_es_bookmark_pressed) | ||||||||||
|
|
||||||||||
| _update_bookmark_visibility() | ||||||||||
| _populate_quest_lists() | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
@@ -74,11 +99,11 @@ func _populate_quest_lists() -> void: | |||||||||
| var previous_button: Button = null | ||||||||||
|
|
||||||||||
| # Building the left page | ||||||||||
| for i in range(left_start, min(left_end, quests.size())): | ||||||||||
| for i in range(left_start, min(left_end, _filtered_quests.size())): | ||||||||||
| previous_button = _create_quest_button(i, left_quest_list, previous_button) | ||||||||||
|
|
||||||||||
| #Building the right page | ||||||||||
| for i in range(right_start, min(right_end, quests.size())): | ||||||||||
| for i in range(right_start, min(right_end, _filtered_quests.size())): | ||||||||||
| previous_button = _create_quest_button(i, right_quest_list, previous_button) | ||||||||||
| # If the right page is empty, add a blank Control spacer so it maintains its width | ||||||||||
| if right_quest_list.get_child_count() == 0: | ||||||||||
|
|
@@ -98,7 +123,7 @@ func _populate_quest_lists() -> void: | |||||||||
| func _create_quest_button( | ||||||||||
| quest_index: int, parent_container: VBoxContainer, prev_btn: Button | ||||||||||
| ) -> Button: | ||||||||||
| var quest: Quest = quests[quest_index] | ||||||||||
| var quest: Quest = _filtered_quests[quest_index] | ||||||||||
| var button := Button.new() | ||||||||||
| button.text = quest.get_title() | ||||||||||
| button.theme_type_variation = "FlatButton" | ||||||||||
|
|
@@ -132,8 +157,8 @@ func _update_page_visibility() -> void: | |||||||||
| storybook_page.visible = true | ||||||||||
|
|
||||||||||
| var quest_index: int = _current_spread_index - 1 | ||||||||||
| if quest_index >= 0 and quest_index < quests.size(): | ||||||||||
| var quest: Quest = quests[quest_index] | ||||||||||
| if quest_index >= 0 and quest_index < _filtered_quests.size(): | ||||||||||
| var quest: Quest = _filtered_quests[quest_index] | ||||||||||
| storybook_page.quest = quest | ||||||||||
|
|
||||||||||
| if storybook_page.play_button and is_instance_valid(storybook_page.play_button): | ||||||||||
|
|
@@ -168,7 +193,7 @@ func _switch_to_page(spread_index: int) -> void: | |||||||||
| if _navigation_locked: | ||||||||||
| return | ||||||||||
|
|
||||||||||
| var total_spreads: int = quests.size() + 1 | ||||||||||
| var total_spreads: int = _filtered_quests.size() + 1 | ||||||||||
| if total_spreads <= 1: | ||||||||||
| return | ||||||||||
|
|
||||||||||
|
|
@@ -206,16 +231,21 @@ func _on_left_button_pressed() -> void: | |||||||||
| return | ||||||||||
|
|
||||||||||
| # If we are on the main index, turn pages back inside the list | ||||||||||
| if _current_spread_index == 0 and _current_list_page > 0: | ||||||||||
| _navigation_locked = true | ||||||||||
| _current_list_page -= 1 | ||||||||||
| await _fade_out_ui() | ||||||||||
| animated_book.play("book_left") | ||||||||||
| await animated_book.animation_finished | ||||||||||
| _populate_quest_lists() | ||||||||||
| _update_page_visibility() | ||||||||||
| _fade_in_ui() | ||||||||||
| _navigation_locked = false | ||||||||||
| if _current_spread_index == 0: | ||||||||||
| if _current_list_page > 0: | ||||||||||
| _navigation_locked = true | ||||||||||
| _current_list_page -= 1 | ||||||||||
| await _fade_out_ui() | ||||||||||
| animated_book.play("book_left") | ||||||||||
| await animated_book.animation_finished | ||||||||||
| _populate_quest_lists() | ||||||||||
| _update_page_visibility() | ||||||||||
| _fade_in_ui() | ||||||||||
| _navigation_locked = false | ||||||||||
| return | ||||||||||
|
|
||||||||||
| # Flipping back from the first page returns to full table of contents | ||||||||||
| _switch_bookmark_tab(0, "") | ||||||||||
| return | ||||||||||
|
|
||||||||||
| _switch_to_page(_current_spread_index - 1) | ||||||||||
|
|
@@ -228,7 +258,7 @@ func _on_right_button_pressed() -> void: | |||||||||
| # If we are on the main index, check if there are more quests to reveal on a new page | ||||||||||
| if _current_spread_index == 0: | ||||||||||
| var max_visible_so_far: int = (_current_list_page + 1) * quests_per_page * 2 | ||||||||||
| if quests.size() > max_visible_so_far: | ||||||||||
| if _filtered_quests.size() > max_visible_so_far: | ||||||||||
| _navigation_locked = true | ||||||||||
| _current_list_page += 1 | ||||||||||
| await _fade_out_ui() | ||||||||||
|
|
@@ -271,3 +301,92 @@ func _on_back_button_pressed() -> void: | |||||||||
|
|
||||||||||
| func reset_focus() -> void: | ||||||||||
| _switch_to_page(0) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _on_toc_bookmark_pressed() -> void: | ||||||||||
| _switch_bookmark_tab(0, "") | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _on_en_bookmark_pressed() -> void: | ||||||||||
| _switch_bookmark_tab(1, "en") | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _on_es_bookmark_pressed() -> void: | ||||||||||
| _switch_bookmark_tab(2, "es") | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _show_table_of_contents() -> void: | ||||||||||
| _filtered_quests = _all_quests | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| _reset_storybook_list_view() | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _filter_quests_by_language(lang_code: String) -> void: | ||||||||||
| _filtered_quests = _all_quests.filter( | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| func(quest: Quest) -> bool: return quest.language == lang_code | ||||||||||
| ) | ||||||||||
| _reset_storybook_list_view() | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _reset_storybook_list_view() -> void: | ||||||||||
| _current_list_page = 0 | ||||||||||
| _current_spread_index = 0 | ||||||||||
| _populate_quest_lists() | ||||||||||
| _update_page_visibility() | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _switch_bookmark_tab(target_tab_index: int, lang_code: String) -> void: | ||||||||||
| if _navigation_locked or target_tab_index == _current_tab_index: | ||||||||||
| return | ||||||||||
|
|
||||||||||
| _navigation_locked = true | ||||||||||
| var old_tab_index: int = _current_tab_index | ||||||||||
| _current_tab_index = target_tab_index | ||||||||||
|
|
||||||||||
| # Fade out UI | ||||||||||
| await _fade_out_ui() | ||||||||||
|
|
||||||||||
| # Flip forward if target > current, otherwise flip backward | ||||||||||
| if target_tab_index > old_tab_index: | ||||||||||
| animated_book.play("book_right") | ||||||||||
| else: | ||||||||||
| animated_book.play("book_left") | ||||||||||
|
|
||||||||||
| # Wait for animation to finish before rebuilding list | ||||||||||
| await animated_book.animation_finished | ||||||||||
|
|
||||||||||
| # Filter quests array | ||||||||||
| if lang_code == "": | ||||||||||
| _filtered_quests = _all_quests | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| else: | ||||||||||
| _filtered_quests = _all_quests.filter( | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| func(quest: Quest) -> bool: return quest.language == lang_code | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Reset page counters and reload UI | ||||||||||
| _current_list_page = 0 | ||||||||||
| _current_spread_index = 0 | ||||||||||
| _populate_quest_lists() | ||||||||||
| _update_page_visibility() | ||||||||||
|
|
||||||||||
| # Fade back in | ||||||||||
| _fade_in_ui() | ||||||||||
| _navigation_locked = false | ||||||||||
|
|
||||||||||
|
|
||||||||||
| func _update_bookmark_visibility() -> void: | ||||||||||
| var unique_languages := [] | ||||||||||
| for q in _all_quests: | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| if q.language != "" and q.language not in unique_languages: | ||||||||||
| unique_languages.append(q.language) | ||||||||||
|
|
||||||||||
| # Only show language bookmarks if there is more than 1 language | ||||||||||
| var show_language_tabs: bool = unique_languages.size() > 1 | ||||||||||
|
|
||||||||||
| # Check if at least one quest exists for each language | ||||||||||
| var has_en: bool = _all_quests.any(func(q: Quest) -> bool: return q.language == "en") | ||||||||||
| var has_es: bool = _all_quests.any(func(q: Quest) -> bool: return q.language == "es") | ||||||||||
|
Comment on lines
+386
to
+387
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| if en_bookmark_button: | ||||||||||
| en_bookmark_button.visible = show_language_tabs and has_en | ||||||||||
| if es_bookmark_button: | ||||||||||
| es_bookmark_button.visible = show_language_tabs and has_es | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,13 +7,23 @@ const STORYBOOK_SCENE := preload("uid://bhm7fdjvppt8b") | |
| @export_range(0, 100, 1, "or_greater") var quests_amount: int = 30 | ||
|
|
||
| var titles := [ | ||
| "Lord of the Needles", | ||
| "El Señor de las Agujas", | ||
| "The Secret of Crochet", | ||
| "The Dark Knit", | ||
| "El tejido oscurot", | ||
| "The Needle and the Sorcerer", | ||
| "Conan the Weaver", | ||
| "Return to Fray's End", | ||
| "The Little HushRoom", | ||
| "Le Seigneur des Aiguilles", | ||
| ] | ||
|
|
||
| var spanish_titles := [ | ||
| "El Señor de las Agujas", | ||
| "El tejido oscurot", | ||
| ] | ||
|
|
||
| var french_titles := [ | ||
| "Le Seigneur des Aiguilles", | ||
| ] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works. But there is a better approach to not duplicate the actual titles in the arrays: |
||
|
|
||
|
|
||
|
|
@@ -22,6 +32,14 @@ func _ready() -> void: | |
| for i in range(quests_amount): | ||
| var q := Quest.new() | ||
| q.title = titles.pick_random() | ||
|
|
||
| if q.title in spanish_titles: | ||
| q.language = "es" | ||
| elif q.title in french_titles: | ||
| q.language = "fr" | ||
| else: | ||
| q.language = "en" | ||
|
|
||
| quests.append(q) | ||
| var storybook := STORYBOOK_SCENE.instantiate() | ||
| storybook.quests = quests | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The copy of
questsis not needed anymore! You can remove this variable and replace all instances of it withquestsand it should still work the same way.