2024-04-24 02:06:15 +03:00
|
|
|
@tool
|
|
|
|
extends FlexContainer3D
|
2024-04-25 01:22:27 +03:00
|
|
|
class_name Pagination3D
|
|
|
|
|
|
|
|
signal on_page_changed(page: int)
|
2024-04-24 02:06:15 +03:00
|
|
|
|
|
|
|
const ButtonScene = preload ("res://content/ui/components/button/button.tscn")
|
2024-04-24 15:00:08 +03:00
|
|
|
const LabelScene = preload ("res://content/ui/components/label_container/label_container.tscn")
|
|
|
|
|
2024-04-24 02:06:15 +03:00
|
|
|
@onready var prev_button = $Prev
|
|
|
|
@onready var next_button = $Next
|
|
|
|
|
|
|
|
@export var page: int = 0:
|
|
|
|
set(value):
|
2024-04-25 01:22:27 +03:00
|
|
|
if page == value: return
|
|
|
|
|
2024-04-24 02:06:15 +03:00
|
|
|
page = clamp(value, 0, pages - 1)
|
|
|
|
_update()
|
|
|
|
@export var pages: int = 5:
|
|
|
|
set(value):
|
2024-04-25 01:22:27 +03:00
|
|
|
if pages == value: return
|
|
|
|
|
2024-04-24 02:06:15 +03:00
|
|
|
pages = max(1, value)
|
|
|
|
_update()
|
|
|
|
@export var visible_pages: int = 5:
|
|
|
|
set(value):
|
2024-04-25 01:22:27 +03:00
|
|
|
if visible_pages == value: return
|
|
|
|
|
2024-04-24 15:00:08 +03:00
|
|
|
visible_pages = max(5, value)
|
2024-04-24 02:06:15 +03:00
|
|
|
_update()
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
_update()
|
|
|
|
|
2024-04-25 14:19:12 +03:00
|
|
|
prev_button.on_button_up.connect(func():
|
|
|
|
page -= 1
|
|
|
|
on_page_changed.emit(page)
|
|
|
|
)
|
|
|
|
|
|
|
|
next_button.on_button_up.connect(func():
|
|
|
|
page += 1
|
|
|
|
on_page_changed.emit(page)
|
|
|
|
)
|
|
|
|
|
2024-04-24 02:06:15 +03:00
|
|
|
func _update():
|
2024-04-25 01:22:27 +03:00
|
|
|
if !is_node_ready(): return
|
2024-04-24 15:00:08 +03:00
|
|
|
|
2024-04-24 02:06:15 +03:00
|
|
|
for child in get_children():
|
|
|
|
if child != prev_button&&child != next_button:
|
2024-04-25 01:22:27 +03:00
|
|
|
remove_child(child)
|
2024-04-25 14:19:12 +03:00
|
|
|
child.queue_free()
|
2024-04-24 02:06:15 +03:00
|
|
|
|
|
|
|
var display_pages = min(pages, visible_pages)
|
|
|
|
var center_pos = floor(display_pages / 2)
|
2024-04-24 15:00:08 +03:00
|
|
|
var start_dots = pages > visible_pages&&page > visible_pages - center_pos - 1
|
|
|
|
var end_dots = pages > visible_pages&&page < pages - visible_pages + floor((display_pages - 1) / 2)
|
|
|
|
|
|
|
|
var at_start = page == 0
|
|
|
|
prev_button.disabled = at_start
|
2024-04-25 14:19:12 +03:00
|
|
|
prev_button.visible = !at_start
|
2024-04-24 15:00:08 +03:00
|
|
|
|
|
|
|
var at_end = page == pages - 1
|
|
|
|
next_button.disabled = at_end
|
2024-04-25 14:19:12 +03:00
|
|
|
next_button.visible = !at_end
|
2024-04-24 02:06:15 +03:00
|
|
|
|
|
|
|
prev_button.size = Vector3(size.y, size.y, size.z)
|
2024-04-25 01:22:27 +03:00
|
|
|
next_button.size = Vector3(size.y, size.y, size.z)
|
|
|
|
|
2024-04-24 02:06:15 +03:00
|
|
|
for i in range(display_pages):
|
|
|
|
if (start_dots&&i == 1)||(end_dots&&i == display_pages - 2):
|
2024-04-24 15:00:08 +03:00
|
|
|
var container = Container3D.new()
|
|
|
|
container.size = Vector3(size.y, size.y, size.z)
|
|
|
|
add_child(container)
|
|
|
|
move_child(container, -2)
|
|
|
|
|
|
|
|
var dots = LabelScene.instantiate()
|
2024-04-24 02:06:15 +03:00
|
|
|
dots.text = "..."
|
2024-04-24 15:00:08 +03:00
|
|
|
container.add_child(dots)
|
2024-04-24 02:06:15 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
var button = ButtonScene.instantiate()
|
|
|
|
button.size = Vector3(size.y, size.y, size.z)
|
2024-04-26 14:30:11 +03:00
|
|
|
button.toggleable = true
|
2024-04-24 02:06:15 +03:00
|
|
|
|
|
|
|
if i == 0:
|
|
|
|
button.label = "1"
|
|
|
|
elif i == display_pages - 1:
|
|
|
|
button.label = str(pages)
|
2024-04-24 15:00:08 +03:00
|
|
|
elif pages <= visible_pages:
|
|
|
|
button.label = str(i + 1)
|
|
|
|
elif visible_pages % 2 == 1:
|
|
|
|
button.label = str(clamp(page, center_pos, pages - 1 - center_pos) - center_pos + i + 1)
|
2024-04-24 02:06:15 +03:00
|
|
|
else:
|
2024-04-24 15:00:08 +03:00
|
|
|
button.label = str(clamp(page + 1, 3, pages - 3) - center_pos + i + 1)
|
2024-04-24 02:06:15 +03:00
|
|
|
|
2024-04-26 14:30:11 +03:00
|
|
|
button.on_button_down.connect(func():
|
2024-04-24 02:06:15 +03:00
|
|
|
page=int(button.label) - 1
|
2024-04-25 01:22:27 +03:00
|
|
|
on_page_changed.emit(page)
|
2024-04-24 02:06:15 +03:00
|
|
|
)
|
|
|
|
|
2024-04-26 14:30:11 +03:00
|
|
|
if (int(button.label) - 1) == page:
|
|
|
|
button.initial_active = true
|
|
|
|
button.disabled = true
|
|
|
|
|
2024-04-24 02:06:15 +03:00
|
|
|
add_child(button)
|
|
|
|
move_child(button, -2)
|
|
|
|
|
|
|
|
super._update()
|
|
|
|
|