-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[icon_explorer] Update icon explorer
- Loading branch information
Showing
21 changed files
with
617 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
addons/icon_explorer/internal/scripts/collection_simpleicons.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
extends "res://addons/icon_explorer/internal/scripts/collection.gd" | ||
|
||
const IconSimpleIcons := preload("res://addons/icon_explorer/internal/scripts/icon_simpleicons.gd") | ||
|
||
const _DOWNLOAD_FILE: String = "https://github.com/simple-icons/simple-icons/archive/master.zip" | ||
|
||
var _unpack_thread: Thread | ||
|
||
func _init() -> void: | ||
self.id = Id.SIMPLE_ICONS | ||
self.name = "Simple Icons" | ||
self.version = "" | ||
self.author = "" | ||
self.license = "CC0 1.0 Universal / Others" | ||
self.web = "https://github.com/simple-icons/simple-icons" | ||
|
||
func _notification(what: int) -> void: | ||
if what == NOTIFICATION_PREDELETE: | ||
if self._unpack_thread != null: | ||
self._unpack_thread.wait_to_finish() | ||
|
||
# OVERRIDE | ||
func load() -> Array[Icon]: | ||
var parser_version: JSON = JSON.new() | ||
var res_version: int = parser_version.parse(FileAccess.get_file_as_string(self.directory().path_join("simple-icons-master/package.json"))) | ||
if res_version != OK: | ||
push_warning("could not parse simple icons package.json: '%s'", [parser_version.get_error_message()]) | ||
return [] | ||
self.version = parser_version.data["version"] | ||
|
||
var parser: JSON = JSON.new() | ||
var res: int = parser.parse(FileAccess.get_file_as_string(self.directory().path_join("simple-icons-master/_data/simple-icons.json"))) | ||
if res != OK: | ||
push_warning("could not parse simple icons simple-icons.json: '%s'", [parser.get_error_message()]) | ||
return [] | ||
|
||
var icon_path: String = self.icon_directory() | ||
var icons: Array[Icon] = [] | ||
for item: Dictionary in parser.data.get("icons", []): | ||
var icon: IconSimpleIcons = self._load_item(item, icon_path) | ||
if icon == null: | ||
continue | ||
icons.append(icon) | ||
for dup: Dictionary in item.get("aliases", {}).get("dup", []): | ||
var dup_item: Dictionary = item.duplicate(true) | ||
dup_item.merge(dup, true) | ||
icon = self._load_item(item, icon_path) | ||
if icon != null: | ||
icons.append(icon) | ||
|
||
return icons | ||
|
||
func _load_item(item: Dictionary, icon_path: String) -> IconSimpleIcons: | ||
var icon: IconSimpleIcons = IconSimpleIcons.new() | ||
icon.collection = self | ||
icon.name = item["title"] | ||
icon.icon_path = icon_path.path_join(icon.name.to_lower().replace(".", "dot").replace(" ", "") + ".svg") | ||
|
||
var hex: String = item.get("hex", "") | ||
if hex != "": | ||
icon.hex = Color.from_string(hex, Color()) | ||
icon.source = item.get("source", "") | ||
var aliases: Dictionary = item.get("aliases", {}) | ||
icon.aliases = aliases.get("aka", PackedStringArray()) | ||
icon.aliases.append_array(aliases.get("loc", {}).values()) | ||
icon.aliases.append_array(aliases.get("old", PackedStringArray())) | ||
icon.license = item.get("license", {}).get("type", "") | ||
icon.license_link = item.get("license", {}).get("url", "") | ||
icon.guidelines = item.get("guidelines", "") | ||
|
||
var buffer: PackedByteArray = FileAccess.get_file_as_bytes(icon.icon_path) | ||
if buffer.size() == 0: | ||
push_warning("could not load '" + icon.icon_path + "'") | ||
return null | ||
var img: Image = Image.new() | ||
var success: int = img.load_svg_from_buffer(buffer, 4.0) | ||
if success != OK: | ||
push_warning("could not load '" + icon.icon_path + "'") | ||
return null | ||
img.fix_alpha_edges() | ||
icon.texture = ImageTexture.create_from_image(img) | ||
return icon | ||
|
||
# OVERRIDE | ||
func install(http: HTTPRequest, version: String) -> void: | ||
if self._unpack_thread != null && self._unpack_thread.is_alive(): | ||
return | ||
if !http.request_completed.is_connected(self._on_request_completed): | ||
http.request_completed.connect(_on_request_completed) | ||
|
||
DirAccess.make_dir_recursive_absolute(self.directory()) | ||
http.download_file = self.directory().path_join("icons.zip") | ||
http.request(_DOWNLOAD_FILE) | ||
|
||
func _on_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void: | ||
if result != HTTPRequest.RESULT_SUCCESS: | ||
self.install_finished.emit(Error.FAILED) | ||
return | ||
|
||
if self._unpack_thread != null && self._unpack_thread.is_started(): | ||
self._unpack_thread.wait_to_finish() | ||
self._unpack_thread = Thread.new() | ||
self._unpack_thread.start(self._unpack_fn) | ||
|
||
# THREADED | ||
func _unpack_fn() -> void: | ||
Io.MtUnzip.new().unpack_zip(self.directory().path_join("icons.zip"), self.directory(), maxi(OS.get_processor_count() / 2, 1)) | ||
DirAccess.remove_absolute(self.directory().path_join("icons.zip")) | ||
self._install_finished.bind(Error.OK).call_deferred() | ||
|
||
func _install_finished(status: Error) -> void: | ||
self.install_finished.emit(status) | ||
|
||
# OVERRIDE | ||
func remove() -> void: | ||
super.remove() | ||
self.version = "" | ||
|
||
# OVERRIDE | ||
func icon_directory() -> String: | ||
return self.directory().path_join("simple-icons-master/icons/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
extends "res://addons/icon_explorer/internal/scripts/icon.gd" | ||
|
||
var hex: Color | ||
var source: String | ||
var aliases: PackedStringArray | ||
var license: String | ||
var license_link: String | ||
var guidelines: String | ||
|
||
|
||
func is_filtered(keyword: String) -> bool: | ||
for alias: String in self.aliases: | ||
if alias.to_lower().contains(keyword): | ||
return true | ||
return super.is_filtered(keyword) || self.hex.to_html().to_lower().contains(keyword) |
Oops, something went wrong.