|
| 1 | +extends ScrollContainer |
| 2 | + |
| 3 | +const image_container_scene := preload("res://components/PreviewContainer.tscn") |
| 4 | + |
| 5 | +@onready |
| 6 | +var container := %ImagePreviews |
| 7 | + |
| 8 | +var preview_list : Array[PreviewContainer] = [] |
| 9 | +var old_visible_items_start = -1 |
| 10 | +var old_visible_items_end = -1 |
| 11 | + |
| 12 | +var visible_items: int = -1 |
| 13 | +var item_width: int = -1 |
| 14 | +@export |
| 15 | +var loading_buffer: int = 32 |
| 16 | +var old_scroll_position: int = -1 |
| 17 | + |
| 18 | +var image_update_timer: Timer |
| 19 | + |
| 20 | +var filling_images : bool = false |
| 21 | + |
| 22 | + |
| 23 | +# Called when the node enters the scene tree for the first time. |
| 24 | +func _ready() -> void: |
| 25 | + |
| 26 | + Application.pre_clear.connect(clear_images) |
| 27 | + Application.new_files_loaded.connect(create_images) |
| 28 | + |
| 29 | + resized.connect(func(): old_scroll_position = -1) |
| 30 | + |
| 31 | + image_update_timer = Timer.new() |
| 32 | + image_update_timer.wait_time = 0.25 |
| 33 | + add_child(image_update_timer) |
| 34 | + image_update_timer.timeout.connect(load_and_unload_images) |
| 35 | + image_update_timer.start() |
| 36 | + |
| 37 | +func load_and_unload_images(): |
| 38 | + |
| 39 | + if preview_list.size() == 0: |
| 40 | + return |
| 41 | + |
| 42 | + item_width = preview_list[0].size.x |
| 43 | + visible_items = ceil(size.x / item_width) |
| 44 | + |
| 45 | + # We need to check every frame if the shown items changed |
| 46 | + # This is because we cant passively listen to changes |
| 47 | + var current_scroll_item: int = floor(scroll_horizontal / item_width) |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + # Check if our item index would change considering shown items |
| 52 | + if current_scroll_item != old_scroll_position: |
| 53 | + |
| 54 | + # Load and unload all images which we need |
| 55 | + |
| 56 | + # First determine which indexes should be visible |
| 57 | + var start_visible := current_scroll_item - loading_buffer |
| 58 | + |
| 59 | + if start_visible < 0: |
| 60 | + start_visible = 0 |
| 61 | + |
| 62 | + var end_visible := current_scroll_item + visible_items + loading_buffer |
| 63 | + if end_visible >= preview_list.size(): |
| 64 | + end_visible = preview_list.size() - 1 |
| 65 | + |
| 66 | + # Check that we actually have items |
| 67 | + if end_visible - start_visible <= 0: |
| 68 | + return |
| 69 | + |
| 70 | + # Unload the items which were visible previously |
| 71 | + for i in range(old_visible_items_start, old_visible_items_end + 1): |
| 72 | + |
| 73 | + |
| 74 | + # Check that we are not in the region of the new visibility |
| 75 | + if i <= end_visible and i >= start_visible: |
| 76 | + continue |
| 77 | + |
| 78 | + # print("Unloading Image - " + str(i)) |
| 79 | + |
| 80 | + # Unload the image at this offset |
| 81 | + preview_list[i].unload_image() |
| 82 | + |
| 83 | + # Now start loading the images in the visible section |
| 84 | + |
| 85 | + for i in range(start_visible, end_visible + 1): |
| 86 | + |
| 87 | + # Check that we are not in the region of the old visibility |
| 88 | + if i <= old_visible_items_end and i >= old_visible_items_start: |
| 89 | + continue |
| 90 | + |
| 91 | + # print("Loading Image - " + str(i)) |
| 92 | + preview_list[i].load_image() |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + old_visible_items_start = start_visible |
| 97 | + old_visible_items_end = end_visible |
| 98 | + |
| 99 | + old_scroll_position = current_scroll_item |
| 100 | + |
| 101 | + |
| 102 | +func clear_images() -> void: |
| 103 | + filling_images = false |
| 104 | + for child in container.get_children(): |
| 105 | + child.queue_free() |
| 106 | + preview_list.clear() |
| 107 | + |
| 108 | +# Creating all objects in a single frame can be too much, depending on the amount of data loaded |
| 109 | +# So we move this to process actually |
| 110 | +func create_images() -> void: |
| 111 | + filling_images = true |
| 112 | + |
| 113 | +func _process(delta: float) -> void: |
| 114 | + if filling_images: |
| 115 | + |
| 116 | + # Iterate the next 500 images we want to add |
| 117 | + |
| 118 | + for i in range(preview_list.size(), min(Application.currently_loaded_files.size(), preview_list.size() + 500)): |
| 119 | + var image : ProcessedImage = Application.currently_loaded_files[i] |
| 120 | + |
| 121 | + var child : PreviewContainer = image_container_scene.instantiate() |
| 122 | + |
| 123 | + child.image_reference = image |
| 124 | + |
| 125 | + container.add_child(child) |
| 126 | + |
| 127 | + preview_list.append(child) |
| 128 | + |
| 129 | + if preview_list.size() == Application.currently_loaded_files.size(): |
| 130 | + filling_images = false |
0 commit comments