Scrollable.gd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. extends WindowPanel
  2. class_name Scrollable
  3. #
  4. @export var jsonFile: Resource = null
  5. @onready var textContainer : VBoxContainer = $Scroll/Margin/VBox
  6. const categoryLabel : PackedScene = preload("res://presets/gui/labels/CategoryLabel.tscn")
  7. const titleLabel : PackedScene = preload("res://presets/gui/labels/TitleLabel.tscn")
  8. const contentLabel : PackedScene = preload("res://presets/gui/labels/ContentLabel.tscn")
  9. const contactLabel : PackedScene = preload("res://presets/gui/labels/ContactLabel.tscn")
  10. #
  11. func Clear():
  12. for child in textContainer.get_children():
  13. textContainer.remove_child(child)
  14. func AddCategories(dictionary : Dictionary):
  15. if "categories" in dictionary:
  16. for category in dictionary["categories"]:
  17. if "category" in category:
  18. var label : RichTextLabel = categoryLabel.instantiate()
  19. label.text = "[center][color=#" + UICommons.LightTextColor.to_html(false) + "]" + category["category"] + "[/color][/center]\n"
  20. textContainer.add_child.call_deferred(label)
  21. AddEntries(category)
  22. func AddEntries(entries : Dictionary):
  23. if "entries" in entries:
  24. for entry in entries["entries"]:
  25. AddTitle(entry)
  26. AddContent(entry)
  27. AddContacts(entry)
  28. func AddTitle(entry : Dictionary):
  29. if "title" in entry:
  30. var label : RichTextLabel = titleLabel.instantiate()
  31. label.text = "[color=#" + UICommons.LightTextColor.to_html(false) + "]" + entry["title"]
  32. if "date" in entry:
  33. label.text += " ~ " + entry["date"]
  34. label.text += "[/color]\n"
  35. textContainer.add_child.call_deferred(label)
  36. func AddContent(entry : Dictionary):
  37. if "content" in entry:
  38. var label : RichTextLabel = contentLabel.instantiate()
  39. label.text = "[color=#" + UICommons.TextColor.to_html(false) + "]" + entry["content"] + "[/color]\n"
  40. textContainer.add_child.call_deferred(label)
  41. label.meta_clicked.connect(_richtextlabel_on_meta_clicked)
  42. func AddContacts(entry : Dictionary):
  43. if "contacts" in entry:
  44. var label : RichTextLabel = contactLabel.instantiate()
  45. label.text += "[center][color=#" + UICommons.TextColor.to_html(false) + "]"
  46. for contact in entry["contacts"]:
  47. var contactName : String
  48. if "name" in contact and "nick" in contact:
  49. contactName = contact["name"] + " \"" + contact["nick"] + "\""
  50. elif "name" in contact:
  51. contactName = contact["name"]
  52. elif "nick" in contact:
  53. contactName = contact["nick"]
  54. else:
  55. assert(false, "No name for this contact information")
  56. continue
  57. if "mailid" in contact and "mailprovider" in contact:
  58. label.text += "[url=mailto:" + contact["mailid"] + "@" + contact["mailprovider"] + "]" + contactName + "[/url]\n"
  59. elif "website" in contact:
  60. label.text += "[url=" + contact["website"] + "]" + contactName + "[/url]\n"
  61. else:
  62. label.text += contactName + "\n"
  63. label.text += "[/color][/center]"
  64. textContainer.add_child.call_deferred(label)
  65. #
  66. func _ready():
  67. Clear()
  68. if jsonFile:
  69. AddCategories(jsonFile.get_data())
  70. func _richtextlabel_on_meta_clicked(meta):
  71. OS.shell_open(str(meta).uri_encode())