diff options
| author | dam <dam@gudinoff> | 2022-03-22 10:05:05 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-03-22 10:05:05 +0000 |
| commit | 35533e154ea9cde0b49c492261268e94195c4fd6 (patch) | |
| tree | 3818ff8eb037f9fbf3bfdc6d7f164995bff2ae3f /option_set | |
| parent | ab8f7810af5bb24bf092b784dbbe79e432d2e766 (diff) | |
| download | surgery-log-35533e154ea9cde0b49c492261268e94195c4fd6.tar.zst surgery-log-35533e154ea9cde0b49c492261268e94195c4fd6.zip | |
WIP of prototype of option set list component: fix label vertical alignment and height.
Diffstat (limited to 'option_set')
| -rw-r--r-- | option_set/option_set_list.gd | 78 |
1 files changed, 28 insertions, 50 deletions
diff --git a/option_set/option_set_list.gd b/option_set/option_set_list.gd index c027aa7..d2179b1 100644 --- a/option_set/option_set_list.gd +++ b/option_set/option_set_list.gd @@ -1,8 +1,10 @@ extends Control -var vscrollbar: VScrollBar -var labels: Array -var positions: Array +var vscrollbar: VScrollBar +var labels: Array +var labels_positions: Array +var labels_sizes: Array +var sizes: Array var items: Array = [ "item 1", "item 22", @@ -58,7 +60,7 @@ func _ready(): label.autowrap = true label.anchor_left = 0.0 label.anchor_right = 1.0 - label.valign = Label.VALIGN_CENTER + label.valign = Label.VALIGN_TOP labels_pool.push_back(label) for idx in range(min(num_labels, items.size())): @@ -77,38 +79,22 @@ func _ready(): # labels.push_back(label) # add_child(label) - build_positions() - - -var pos: Array -func build_positions(): - pos.clear() - positions.clear() - var label = labels[0] -# var label := Label.new() -# label.autowrap = true -# label.anchor_left = 0.0 -# label.anchor_right = 1.0 -# label.valign = Label.VALIGN_CENTER -# add_child(label) + process_labels() + + +func process_labels(): + labels_positions.clear() + labels_sizes.clear() var position := 0.0 var limit := rect_size.x -# positions.append(0) +# labels_positions.append(0) for it in items: var size := font.get_wordwrap_string_size(it, limit).y var lines = size / font.get_height() var height = size + font.get_descent() * lines -# label.text = it -# label.rect_position = Vector2(0.0, 500.0) -# label.rect_size.y = 0.0 -# position += label.rect_size.y - pos.append(position) position += height - positions.append(position) -# if label.rect_size.y != 14: -# breakpoint -# positions.append(position) -# remove_child(label) + labels_positions.append(position) + labels_sizes.append(height) func _process(delta): @@ -128,38 +114,30 @@ func _process(delta): # @DAM Since the items may have different heights, we must look at the edge items # in order to update the offsets. - build_positions() + process_labels() vscrollbar.min_value = 0 vscrollbar.max_value = rect_size.y - var ratio := rect_size.y / float(positions[positions.size()-1]) + var ratio := rect_size.y / float(labels_positions[labels_positions.size()-1]) vscrollbar.visible = ratio < 1.0 vscrollbar.page = ratio * (vscrollbar.max_value - vscrollbar.min_value) var offset := vscrollbar.value var bs_value := offset / ratio - var offset_idx := positions.bsearch(bs_value) + var offset_idx := labels_positions.bsearch(bs_value) # offset_idx = 0 for idx in range(min(num_labels, items.size())): + var label = labels[idx] if idx + offset_idx >= items.size(): - labels[idx].text = "" - break - labels[idx].text = items[idx + offset_idx] -# labels[idx].text = items[min(idx + offset_idx, items.size()-1)] - - var it = labels[idx].text - var limit = rect_size.x - var size := font.get_wordwrap_string_size(it, limit).y - var lines = size / font.get_height() - var height = size + font.get_descent() * lines - labels[idx].rect_size.y = height -# labels[idx].rect_size.y = font.get_wordwrap_string_size(labels[idx].text, rect_size.x).y -# labels[idx].rect_size.y = 0.0 - -# labels[idx].rect_position.y = positions[idx + offset_idx] - bs_value - positions[0] - labels[idx].rect_position.y = pos[idx + offset_idx] - bs_value - + label.text = "" + break # @DAM Or should we use continue? + label.text = items[idx + offset_idx] + label.rect_position.y = labels_positions[idx + offset_idx] - labels_sizes[idx + offset_idx] - bs_value + # This was initially used to bring back down the height of the labels that grow due to text + # wrap because they were vertically aligned to CENTER without a good reason to do so. + # Since the labels are now aligned to TOP, we may just forget about their height. +# label.rect_size.y = 0.0 # if difference != 0: @@ -167,7 +145,7 @@ func _process(delta): # labels[idx].text = items[idx] # if Engine.get_idle_frames() % 30 == 1: -# print_debug("> %s | %5.3f > %s > %d | lastPos: %s" % [labels.size(), offset, bs_value, offset_idx, positions[-1]]) +# print_debug("> %s | %5.3f > %s > %d | lastPos: %s" % [labels.size(), offset, bs_value, offset_idx, labels_positions[-1]]) # Adapt scrollbar according to options and drawable size. |
