Adding pins for each element

This commit is contained in:
Yann Kervran 2024-11-28 18:06:02 +01:00
parent a737212f51
commit 3f817ae4b5
8 changed files with 211 additions and 37 deletions

View file

@ -72,7 +72,7 @@ func lets_roll(chosen_seed: int, noise: bool = 1) -> void:
# Randomize trait
if not keep_traits.button_pressed:
traits.populate()
traits.build_traits()
func update_language(new_language: String) -> void:
# Change the localisation on Translation Server

View file

@ -250,6 +250,9 @@ offset_left = -16.512
offset_top = 128.088
offset_right = -15.616
offset_bottom = 112.232
traits = ["Courage", "Passion", "Creativity", "Temperance", "Sensuality", "Energy", "Responsibility", "Empathy", "Self-esteem", "Flexibility", "Hope", "Will"]
seed = 0
amount = 3
[node name="Traits_label" type="Label" parent="Traits"]
layout_mode = 1

BIN
textures/arrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cafw5ytvxgep4"
path="res://.godot/imported/arrowDown.png-ab79ddb99d5bc29b618e06ce2bcf2e97.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://textures/arrowDown.png"
dest_files=["res://.godot/imported/arrowDown.png-ab79ddb99d5bc29b618e06ce2bcf2e97.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

BIN
textures/arrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cfrj22ncm1tiq"
path="res://.godot/imported/arrowUp.png-b09ed087648058278d5b46b9c01afa74.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://textures/arrowUp.png"
dest_files=["res://.godot/imported/arrowUp.png-b09ed087648058278d5b46b9c01afa74.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

136
traits.gd
View file

@ -3,29 +3,117 @@ extends Control
@export var traits:Array = [
"Courage",
"Passion",
"Créativité",
"Tempérance",
"Sensualité",
"Énergie",
"Responsabilité",
"Empathie",
"Estime de soi",
"Flexibilité",
"Espoir",
"Volonté"
"Creativity",
"Temperance",
"Sensuality",
"Energy",
"Responsibility",
"Empathy",
"Self-esteem",
"Flexibility",
"Hope",
"Will"
]
@export var amount: int = 3
@export var seed: int = 0
@export var amount: int = 3
var pin_items: Array = []
# variables for nodes
@onready var traits_list: ItemList = $Trait
@onready var traits_list: VBoxContainer = $Traits_list
# Variable for icons
@onready var icon_up: Texture2D = load("res://textures/arrowUp.png")
@onready var icon_down: Texture2D = load("res://textures/arrowDown.png")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
populate()
build_traits()
func build_traits() -> void:
# Build the traits list with checkboxes to pin any
var list_amount: int = amount
var pinned: Array = []
# First, clear the box for non-pinned traits
for child in traits_list.get_children():
for sub_child in child.get_children():
if sub_child is CheckBox:
if sub_child.button_pressed:
list_amount -= 1
pinned.append([child.get_index(), child])
for child in traits_list.get_children():
for sub_child in child.get_children():
if sub_child is CheckBox:
if sub_child.button_pressed:
traits_list.remove_child(child)
else:
child.queue_free()
# generate the amount of traits needed
var char_traits: Dictionary = generate_traits(traits, seed, list_amount)
var char_traits_list: Array = []
for key in char_traits.keys():
char_traits_list.append([key, char_traits[key][0], char_traits[key][1]])
for pin in pinned:
char_traits_list.insert(pin[0], pin[1])
# Generate a line for each trait
for traitline in range(amount):
print("At index: ", traitline, " found: ", char_traits_list[traitline])
if char_traits_list[traitline] is HBoxContainer:
print("Je confirme")
print("Parent: ", char_traits_list[traitline].get_parent())
traits_list.add_child(char_traits_list[traitline])
#char_traits_list[traitline].reparent(traits_list)
else:
# Create horizontal container for elements
var container = HBoxContainer.new()
# Checkbox to pin or not
var checkbox: CheckBox = CheckBox.new()
var checkbox_index: int = pin_items.size()
checkbox.text = "" # No text
checkbox.tooltip_text = "PIN"
checkbox.toggled.connect(Callable(self, "_on_checkbox_toggled").bind(checkbox_index))
# Label for trait name
var label: Label = Label.new()
label.text = char_traits_list[traitline][0]
# Icon for up or down
var icon: Texture2D = icon_down
var icon_rect: TextureRect = TextureRect.new()
icon_rect.texture = icon_down
var value: bool = char_traits_list[traitline][1]
if value:
icon_rect.texture = icon_up
# Add the elements to the horizontal container
container.add_child(checkbox)
container.add_child(label)
container.add_child(icon_rect)
# Add the container
traits_list.add_child(container)
# Stock checkbox reference
pin_items.append(checkbox)
func _on_checkbox_toggled(checked: bool, checkbox: int) -> void:
# Manage checkbox signal
pass
func generate_traits(traits_list: Array = traits, chosen_seed: int = 0, chosen_amount: int = 3) -> Dictionary:
# Return a dictionary, with trait as key and boolean as value
# Return a dictionary,
# with trait as key and
# an array with index as first value and boolean as second value
# Inputs:
# chosen_seed for random seed
# chosen_amount for the amount of traits to return
var picked_traits: Dictionary = {}
@ -43,26 +131,10 @@ func generate_traits(traits_list: Array = traits, chosen_seed: int = 0, chosen_a
random_value.randomize()
var value: bool = random_value.randi_range(0, 1)
# Add the key and value to the array
picked_traits[traits_duplicate[result]] = value
# Add the key and array value to the array
picked_traits[traits_duplicate[result]] = [select, value]
# Delete the chosen trait from the possible list
traits_duplicate.pop_at(result)
return picked_traits
func populate(traits: Array = traits, seed: int = 0, amount: int = 3) -> void:
# To create an itemlist with traits and values
traits_list.clear()
var char_traits: Dictionary = generate_traits(traits, seed, amount)
for char_trait in char_traits:
var value: String = ""
if char_traits[char_trait]:
value = ""
else:
value = ""
var full_line: String = char_trait + " " + value
traits_list.add_item(full_line)

View file

@ -1,6 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://cl5wswke7jxpi"]
[gd_scene load_steps=3 format=3 uid="uid://cl5wswke7jxpi"]
[ext_resource type="Script" path="res://traits.gd" id="1_6h04s"]
[ext_resource type="Texture2D" uid="uid://cafw5ytvxgep4" path="res://textures/arrowDown.png" id="2_iexdr"]
[node name="Traits" type="Control"]
layout_mode = 3
@ -13,7 +14,37 @@ grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_6h04s")
[node name="Trait" type="ItemList" parent="."]
[node name="Traits_list" type="VBoxContainer" parent="."]
layout_mode = 0
offset_right = 224.0
offset_bottom = 96.0
offset_right = 232.0
offset_bottom = 119.0
[node name="HBoxContainer" type="HBoxContainer" parent="Traits_list"]
layout_mode = 2
[node name="CheckBox" type="CheckBox" parent="Traits_list/HBoxContainer"]
layout_mode = 2
[node name="Label" type="Label" parent="Traits_list/HBoxContainer"]
layout_mode = 2
text = "My text"
[node name="TextureRect" type="TextureRect" parent="Traits_list/HBoxContainer"]
layout_mode = 2
texture = ExtResource("2_iexdr")
expand_mode = 3
[node name="HBoxContainer2" type="HBoxContainer" parent="Traits_list"]
layout_mode = 2
[node name="CheckBox" type="CheckBox" parent="Traits_list/HBoxContainer2"]
layout_mode = 2
[node name="Label" type="Label" parent="Traits_list/HBoxContainer2"]
layout_mode = 2
text = "My text"
[node name="TextureRect" type="TextureRect" parent="Traits_list/HBoxContainer2"]
layout_mode = 2
texture = ExtResource("2_iexdr")
expand_mode = 3