Skip to content

Commit

Permalink
Ensure decent performance of GoodFileDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur committed Apr 10, 2024
1 parent c5cb60b commit 28589ca
Show file tree
Hide file tree
Showing 21 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Indications.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## This singleton handles editor information like zoom level and selections.
extends Node
## This singleton handles editor information like zoom level and selections.

const ContextPopup = preload("res://src/ui_elements/context_popup.tscn")
const PathCommandPopup = preload("res://src/ui_elements/path_popup.tscn")
Expand Down
2 changes: 1 addition & 1 deletion src/SVG.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extends Node
## This singleton handles the two representations of the SVG:
## The SVG text, and the native [TagSVG] representation.
extends Node


signal parsing_finished(error_id: SVGParser.ParseError)
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/Attribute.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Abstract class for an attribute inside a [Tag], i.e. <tag attribute="value"/>
class_name Attribute extends RefCounted
## Abstract class for an attribute inside a [Tag], i.e. <tag attribute="value"/>

signal value_changed(new_value: String)
signal propagate_value_changed(undo_redo: bool)
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/AttributeColor.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## An attribute representing a color string, or an url to an ID.
class_name AttributeColor extends Attribute
## An attribute representing a color string, or an url to an ID.

# No direct color representation for this attribute type. There are too many quirks.

Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/AttributeEnum.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## An attribute with only a set of meaningful values.
class_name AttributeEnum extends Attribute
## An attribute with only a set of meaningful values.

var possible_values: Array[String]

Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/AttributeList.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## An attribute representing a list of numbers.
class_name AttributeList extends Attribute
## An attribute representing a list of numbers.

var _list: PackedFloat32Array

Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/AttributeNumeric.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## An attribute representing a number.
class_name AttributeNumeric extends Attribute
## An attribute representing a number.

var _number := NAN
enum Mode {FLOAT, UFLOAT, NFLOAT} # UFLOAT is positive-only, NFLOAT is in [0, 1].
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/AttributePath.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## The "d" attribute of [TagPath].
class_name AttributePath extends Attribute
## The "d" attribute of [TagPath].

var _commands: Array[PathCommand]

Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/AttributeTransform.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## An attribute representing a list of transforms.
class_name AttributeTransform extends Attribute
## An attribute representing a list of transforms.

class Transform extends RefCounted:
func compute_transform() -> Transform2D:
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/AttributeUnknown.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## An attribute not recognized by GodSVG.
class_name AttributeUnknown extends Attribute
## An attribute not recognized by GodSVG.

var name := ""

Expand Down
6 changes: 6 additions & 0 deletions src/data_classes/ColorPalette.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class_name ColorPalette extends Resource
## A resource for the color palettes that are listed in the color picker.

signal layout_changed

@export var title: String # Color palettes must be uniquely named.
@export var colors: Array[String] # Colors must be unique within a palette.
Expand All @@ -16,11 +19,13 @@ func add_color() -> void:
colors.append("none")
color_names.append("")
emit_changed()
layout_changed.emit()

func remove_color(idx: int) -> void:
colors.remove_at(idx)
color_names.remove_at(idx)
emit_changed()
layout_changed.emit()

func move_color(old_idx: int, new_idx: int) -> void:
if old_idx == new_idx:
Expand All @@ -32,6 +37,7 @@ func move_color(old_idx: int, new_idx: int) -> void:
colors.insert(new_idx, colors.pop_at(old_idx))
color_names.insert(new_idx, color_names.pop_at(old_idx))
emit_changed()
layout_changed.emit()

func modify_title(new_title: String) -> void:
title = new_title
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/Tag.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## A SVG tag, standalone ([code]<tag/>[/code]) or container ([code]<tag></tag>[/code]).
class_name Tag extends RefCounted
## A SVG tag, standalone ([code]<tag/>[/code]) or container ([code]<tag></tag>[/code]).

var child_tags: Array[Tag]

Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/TagEllipse.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## An <ellipse/> tag.
class_name TagEllipse extends Tag
## An <ellipse/> tag.

const name = "ellipse"
const possible_conversions = ["circle", "rect", "path"]
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/TagLine.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## A <line/> tag.
class_name TagLine extends Tag
## A <line/> tag.

const name = "line"
const possible_conversions = ["path"]
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/TagPath.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## A <path/> tag.
class_name TagPath extends Tag
## A <path/> tag.

const name = "path"
const possible_conversions = []
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/TagRect.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## A <rect/> tag.
class_name TagRect extends Tag
## A <rect/> tag.

const name = "rect"
const possible_conversions = ["circle", "ellipse", "path"]
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/TagSVG.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## A <svg></svg> tag.
class_name TagSVG extends Tag
## A <svg></svg> tag.

var width: float
var height: float
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/TagStop.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## A <stop/> tag.
class_name TagStop extends Tag
## A <stop/> tag.

const name = "stop"
const possible_conversions = []
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/TagUnknown.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## A tag not recognized by GodSVG.
class_name TagUnknown extends Tag
## A tag not recognized by GodSVG.

var name: String
const possible_conversions = []
Expand Down
4 changes: 3 additions & 1 deletion src/ui_elements/palette_config.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var currently_edited_idx := -1
# Used to setup a palette for this element.
func assign_palette(palette: ColorPalette) -> void:
current_palette = palette
current_palette.changed.connect(rebuild_colors)
current_palette.layout_changed.connect(rebuild_colors)
rebuild_colors()

# Rebuilds the content of the colors container.
Expand Down Expand Up @@ -232,8 +232,10 @@ func _drop_data(_at_position: Vector2, data: Variant) -> void:
return

if data[0] == current_palette:
currently_edited_idx = -1
current_palette.move_color(data[1], proposed_drop_idx)
else:
currently_edited_idx = -1
current_palette.colors.insert(proposed_drop_idx, data[0].colors[data[1]])
current_palette.color_names.insert(proposed_drop_idx, data[0].color_names[data[1]])
current_palette.emit_changed()
Expand Down
47 changes: 29 additions & 18 deletions src/ui_parts/good_file_dialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ new_extension: String) -> void:
extension = new_extension

func _ready() -> void:
file_list.get_v_scroll_bar().value_changed.connect(_setup_file_images.unbind(1))
if mode == FileMode.SELECT:
file_container.hide()
if mode == FileMode.SAVE:
Expand Down Expand Up @@ -163,24 +164,11 @@ func set_dir(dir: String) -> void:
(not search_text.is_empty() and not search_text.is_subsequence_ofn(file)):
continue

match extension:
"svg":
# Setup a clean SVG graphic by using the scaling parameter.
var svg_text := FileAccess.open(current_dir.path_join(file),
FileAccess.READ).get_as_text()
var img := Image.new()
img.load_svg_from_string(svg_text)
img.load_svg_from_string(svg_text,
item_height / maxf(img.get_width(), img.get_height()))
var item_idx := file_list.add_item(file, ImageTexture.create_from_image(img))
file_list.set_item_metadata(item_idx,
Actions.new(select_file, focus_file.bind(file)))
"png":
var item_idx := file_list.add_item(file, ImageTexture.create_from_image(
Image.load_from_file(current_dir.path_join(file))))
file_list.set_item_metadata(item_idx,
Actions.new(select_file, focus_file.bind(file)))
file = DA.get_next()
var item_idx := file_list.add_item(file, null)
file_list.set_item_metadata(item_idx,
Actions.new(select_file, focus_file.bind(file)))
await get_tree().process_frame
_setup_file_images()

func set_file(file: String) -> void:
if mode == FileMode.SELECT:
Expand All @@ -193,9 +181,32 @@ func set_file(file: String) -> void:
if not file.is_empty():
if file.get_extension() != extension:
file += "." + extension
file_list.ensure_current_is_visible()
current_file = file
file_field.text = current_file

# For optimization, only generate the visible files' images.
func _setup_file_images() -> void:
for item_idx in file_list.item_count:
var visible_rect := file_list.get_rect()
visible_rect.position.y = file_list.get_v_scroll_bar().value
if file_list.get_item_icon(item_idx) == null and\
file_list.get_item_rect(item_idx).intersects(visible_rect):
var file := file_list.get_item_text(item_idx)
match file.get_extension():
"png":
file_list.set_item_icon(item_idx, ImageTexture.create_from_image(
Image.load_from_file(current_dir.path_join(file))))
"svg":
# Setup a clean SVG graphic by using the scaling parameter.
var svg_text := FileAccess.open(current_dir.path_join(file),
FileAccess.READ).get_as_text()
var img := Image.new()
img.load_svg_from_string(svg_text)
img.load_svg_from_string(svg_text,
item_height / maxf(img.get_width(), img.get_height()))
file_list.set_item_icon(item_idx, ImageTexture.create_from_image(img))


func select_file() -> void:
if mode == FileMode.SAVE and current_file in DirAccess.get_files_at(current_dir):
Expand Down

0 comments on commit 28589ca

Please sign in to comment.