diff options
| author | dam <dam@gudinoff> | 2022-03-21 23:50:15 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-03-21 23:50:15 +0000 |
| commit | ab8f7810af5bb24bf092b784dbbe79e432d2e766 (patch) | |
| tree | 77a554cb2a6e690ca2ddf0991c30b343f6fbfb06 | |
| parent | 0c705c18900f8b12fd147937584e5228776e8469 (diff) | |
| download | surgery-log-ab8f7810af5bb24bf092b784dbbe79e432d2e766.tar.zst surgery-log-ab8f7810af5bb24bf092b784dbbe79e432d2e766.zip | |
WIP of prototype of option set list component: better label size and position.
| -rw-r--r-- | option_set/option_set_list.gd | 79 |
1 files changed, 64 insertions, 15 deletions
diff --git a/option_set/option_set_list.gd b/option_set/option_set_list.gd index 4114ae1..c027aa7 100644 --- a/option_set/option_set_list.gd +++ b/option_set/option_set_list.gd @@ -2,6 +2,7 @@ extends Control var vscrollbar: VScrollBar var labels: Array +var positions: Array var items: Array = [ "item 1", "item 22", @@ -75,7 +76,39 @@ func _ready(): # label.valign = Label.VALIGN_CENTER # 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) + var position := 0.0 + var limit := rect_size.x +# 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) func _process(delta): @@ -93,39 +126,55 @@ func _process(delta): labels_pool.append(label) remove_child(label) - WIP # @DAM Since the items may have different heights, we must look at the edge items # in order to update the offsets. + build_positions() + vscrollbar.min_value = 0 + vscrollbar.max_value = rect_size.y + var ratio := rect_size.y / float(positions[positions.size()-1]) + vscrollbar.visible = ratio < 1.0 + vscrollbar.page = ratio * (vscrollbar.max_value - vscrollbar.min_value) + var offset := vscrollbar.value - var offset_idx := int(floor(offset * items.size() / vscrollbar.max_value)) + var bs_value := offset / ratio + var offset_idx := positions.bsearch(bs_value) # offset_idx = 0 for idx in range(min(num_labels, items.size())): + 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 + + # if difference != 0: # for idx in range(min(num_labels, items.size())): # labels[idx].text = items[idx] - if Engine.get_idle_frames() % 30 == 1: - print_debug("> %s | %5.3f | %d " % [labels.size(), offset, offset_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]]) - var position := Vector2(0.0, -offset) - for it in labels: - it.rect_position = position - it.rect_size.y = 0 -# var rect_size = font.get_wordwrap_string_size((it as Label).text, it.rect_size.x) -# it.rect_size.y = rect_size.y - position.y += it.rect_size.y # Adapt scrollbar according to options and drawable size. # @DAM This does not take into consideration the items that are wrapped. - var ratio := rect_size.y / (items.size() * font.get_height()) - vscrollbar.visible = ratio < 1.0 - vscrollbar.page = ratio * (vscrollbar.max_value - vscrollbar.min_value) + + - vscrollbar.max_value = rect_size.y |
