Skip to content

Commit

Permalink
Implement InspectorPropertyPaginator
Browse files Browse the repository at this point in the history
  • Loading branch information
4d49 committed Apr 22, 2024
1 parent a436be6 commit 6425134
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 1 deletion.
1 change: 1 addition & 0 deletions icons/page_first.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/page_last.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/page_next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/page_prev.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion inspector_theme.tres
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
[gd_resource type="Theme" load_steps=9 format=3 uid="uid://dyf67c0ud50ts"]
[gd_resource type="Theme" load_steps=13 format=3 uid="uid://dyf67c0ud50ts"]

[ext_resource type="Texture2D" uid="uid://cr6jrnuywr8g4" path="res://addons/object-inspector/icons/maximize.svg" id="1_hfaeu"]
[ext_resource type="Texture2D" uid="uid://cfw5fv6chuy5o" path="res://addons/object-inspector/icons/collapsed.svg" id="1_hpca1"]
[ext_resource type="Texture2D" uid="uid://dec8bwwwl2po1" path="res://addons/object-inspector/icons/expanded.svg" id="2_0jwgf"]
[ext_resource type="Texture2D" uid="uid://bq3g4y2emis6p" path="res://addons/object-inspector/icons/search.svg" id="2_vebvf"]
[ext_resource type="Texture2D" uid="uid://ce8g2ipkdytv4" path="res://addons/object-inspector/icons/page_first.svg" id="5_e0bdv"]
[ext_resource type="Texture2D" uid="uid://ccs8xtkn7x5wd" path="res://addons/object-inspector/icons/page_last.svg" id="6_wdbtw"]
[ext_resource type="Texture2D" uid="uid://bd0mwwwcv347l" path="res://addons/object-inspector/icons/page_next.svg" id="7_hjbvh"]
[ext_resource type="Texture2D" uid="uid://cysi7j6pgq3m" path="res://addons/object-inspector/icons/page_prev.svg" id="8_ybigx"]

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_h08j0"]

Expand Down Expand Up @@ -76,5 +80,10 @@ InspectorPropertyGroup/base_type = &"PanelContainer"
InspectorPropertyGroup/icons/collapsed = ExtResource("1_hpca1")
InspectorPropertyGroup/icons/expanded = ExtResource("2_0jwgf")
InspectorPropertyGroup/styles/panel = SubResource("StyleBoxFlat_idiuy")
InspectorPropertyPaginator/base_type = &"VBoxContainer"
InspectorPropertyPaginator/icons/page_first = ExtResource("5_e0bdv")
InspectorPropertyPaginator/icons/page_last = ExtResource("6_wdbtw")
InspectorPropertyPaginator/icons/page_next = ExtResource("7_hjbvh")
InspectorPropertyPaginator/icons/page_prev = ExtResource("8_ybigx")
InspectorPropertySubGroup/base_type = &"InspectorPropertyGroup"
InspectorPropertySubGroup/styles/panel = SubResource("StyleBoxFlat_cncyr")
178 changes: 178 additions & 0 deletions scripts/inspector_property_paginator.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Copyright (c) 2022-2024 Mansur Isaev and contributors - MIT License
# See `LICENSE.md` included in the source distribution for details.

class_name InspectorPropertyPaginator
extends VBoxContainer


const PAGE_SIZE: int = 20


var _hseparator: HSeparator = null
var _footer: HBoxContainer = null

var _page_first: Button = null
var _page_prev: Button = null

var _page_edit: LineEdit = null
var _page_label: Label = null

var _page_next: Button = null
var _page_last: Button = null


var _element_count: int = -1

var _current_page: int = 0
var _page_count: int = 0

var _element_constructor: Callable


func _init(element_constructor: Callable) -> void:
self.set_theme_type_variation(&"InspectorPropertyPaginator")

_element_constructor = element_constructor

_footer = HBoxContainer.new()
_footer.set_name("Footer")
_footer.set_h_size_flags(Control.SIZE_SHRINK_CENTER)
_footer.hide()

_page_first = Button.new()
_page_first.set_name("PageFirst")
_page_first.set_flat(true)
_page_first.set_disabled(true)
_page_first.pressed.connect(_on_page_first_pressed)
_footer.add_child(_page_first)

_page_prev = Button.new()
_page_prev.set_name("PagePrev")
_page_prev.set_flat(true)
_page_prev.set_disabled(true)
_page_prev.pressed.connect(_on_page_prev_pressed)
_footer.add_child(_page_prev)

_page_edit = LineEdit.new()
_page_edit.set_text("0")
_page_edit.text_submitted.connect(_on_page_edit_text_submitted)
_footer.add_child(_page_edit)

_page_label = Label.new()
_page_label.set_name("PageCount")
_page_label.set_text("/ 42")
_footer.add_child(_page_label)

_page_next = Button.new()
_page_next.set_name("PageNext")
_page_next.set_flat(true)
_page_next.pressed.connect(_on_page_next_pressed)
_footer.add_child(_page_next)

_page_last = Button.new()
_page_last.set_name("PageLast")
_page_last.set_flat(true)
_page_last.pressed.connect(_on_page_last_pressed)
_footer.add_child(_page_last)

_hseparator = HSeparator.new()
_hseparator.hide()
self.add_child(_hseparator, false, Node.INTERNAL_MODE_BACK)

self.add_child(_footer, false, Node.INTERNAL_MODE_BACK)


func _enter_tree() -> void:
_page_first.set_button_icon(get_theme_icon(&"page_first"))
_page_prev.set_button_icon(get_theme_icon(&"page_prev"))
_page_next.set_button_icon(get_theme_icon(&"page_next"))
_page_last.set_button_icon(get_theme_icon(&"page_last"))


func set_element_count(element_count: int) -> void:
element_count = maxi(element_count, 0)
if _element_count == element_count:
return

_hseparator.set_visible(element_count > PAGE_SIZE)
_footer.set_visible(_hseparator.is_visible())

_page_count = float(element_count - 1) / PAGE_SIZE
_page_label.set_text("/ %d" % _page_count)

_current_page = clampi(_current_page, 0, _page_count)
_page_edit.set_text(str(_current_page))

_element_count = element_count

update_elements()

func get_element_count() -> int:
return _element_count


func get_page_count() -> int:
return _page_count


func create_element(index: int) -> Control:
return _element_constructor.call(index)


func update_elements() -> void:
for i: int in get_child_count():
var child: Node = get_child(i)
if child is Control:
child.queue_free()

var begin: int = _current_page * PAGE_SIZE
var end: int = mini(begin + PAGE_SIZE, _element_count)

while begin < end:
var control: Control = create_element(begin)
if is_instance_valid(control):
add_child(control)

begin += 1


func set_current_page(page: int) -> void:
page = clampi(page, 0, get_page_count())
if _current_page == page:
return

_page_first.set_disabled(page == 0)
_page_prev.set_disabled(_page_first.is_disabled())
_page_next.set_disabled(page == get_page_count())
_page_last.set_disabled(_page_next.is_disabled())

_current_page = page
_page_edit.set_text(str(page))

update_elements()

func get_current_page() -> int:
return _current_page


func _on_page_first_pressed() -> void:
set_current_page(0)


func _on_page_prev_pressed() -> void:
set_current_page(get_current_page() - 1)


func _on_page_edit_text_submitted(text: String) -> void:
if not text.is_valid_int():
return

set_current_page(text.to_int())


func _on_page_next_pressed() -> void:
set_current_page(get_current_page() + 1)


func _on_page_last_pressed() -> void:
set_current_page(get_page_count())

0 comments on commit 6425134

Please sign in to comment.