aboutsummaryrefslogtreecommitdiff
path: root/client/gui/menus/book/book.gd
blob: f6d95c8aa0d80db6b34a88c612f58a71679f5a08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Hurry Curry! - a game about cooking
# Copyright (C) 2025 Hurry Curry! contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
class_name BookMenu
extends Menu

const MARGIN: int = 32

var current_page := 0

@onready var first := $Margin/HBoxContainer/First/PanelContainer/MarginContainer
@onready var second := $Margin/HBoxContainer/Second/PanelContainer/MarginContainer

var pages: Array

class BookData:
	var game: Game
	var data: Dictionary

	func _init(game_, data_) -> void:
		game = game_
		data = data_

var book_data: BookData

func _ready():
	super()
	book_data = data
	pages = book_data.data["pages"]
	# Flip book for rtl languages
	$Margin/HBoxContainer/Previous.flip_h = not is_layout_rtl()
	$Margin/HBoxContainer/Next.flip_h = is_layout_rtl()
	build_page()

func build_page() -> void:
	for c: Node in first.get_children():
		c.queue_free()
	for c: Node in second.get_children():
		c.queue_free()

	var p: Dictionary = pages[current_page]
	match p.page_type:
		"contents":
			var title := build_title(MessageParser.new(p["title"]).parse(book_data.game))
			var vbox := VBoxContainer.new()
			vbox.add_child(title)
			for i: Array in p.table:
				var m := MessageParser.new(i[0])
				m.parse(book_data.game)
				vbox.add_child(build_contents_entry(m.result, i[1]))
			second.add_child(vbox)
		"recipe":
			var title := build_title(MessageParser.new(p["title"]).parse(book_data.game))
			var par := build_paragraph(MessageParser.new(p["description"]).parse(book_data.game))
			var vbox := VBoxContainer.new()
			vbox.add_child(title)
			vbox.add_child(par)
			var dia := Diagram.new(p["diagram"], book_data.game)
			first.add_child(vbox)
			second.add_child(dia)
		_: push_error("%s not known" % p.page_type)
	return Control.new()

func build_title(m: String) -> Label:
	var label := Label.new()
	label.add_theme_font_override("font", preload("res://gui/resources/fonts/font-sansita-swashed.woff2"))
	label.add_theme_font_size_override("font_size", 42)
	label.add_theme_color_override("font_color", Color.BLACK)
	label.text = m
	return label

func build_paragraph(m: String) -> Label:
	var label := Label.new()
	label.add_theme_font_override("font", preload("res://gui/resources/fonts/font-josefin-sans.woff2"))
	label.add_theme_font_size_override("font_size", 32)
	label.add_theme_color_override("font_color", Color.BLACK)
	label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	label.text = m
	return label

func build_contents_entry(m: String, ref: int) -> Control:
	var b := LinkButton.new()
	b.text = m
	b.pressed.connect(func():
		current_page = ref
		build_page()
	)
	b.add_theme_font_override("font", preload("res://gui/resources/fonts/font-josefin-sans.woff2"))
	b.add_theme_font_size_override("font_size", 32)
	b.add_theme_color_override("font_color", Color.BLACK)
	return b

func _on_previous_pressed() -> void:
	current_page = clampi(current_page - 1, 0, pages.size() - 1)
	build_page()

func _on_next_pressed() -> void:
	current_page = clampi(current_page + 1, 0, pages.size() - 1)
	build_page()