Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update plugin to be used with 4.x #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.import
*.import
sketchfab
.*
5 changes: 2 additions & 3 deletions Main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[gd_scene format=2]

[node name="Spatial" type="Spatial" index="0"]
[gd_scene format=3 uid="uid://scfkut8juyym"]

[node name="Node3D" type="Node3D"]
26 changes: 13 additions & 13 deletions addons/sketchfab/Api.gd
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
tool
@tool
extends Object

const OAUTH_HOSTNAME = "sketchfab.com"
const API_HOSTNAME = "api.sketchfab.com"
const USE_SSL = true
const BASE_PATH = "/v3"
const CLIENT_ID = "a99JEwOhmIWHdRRDDgBsxbBf8ufC0ACoUcDAifSV"
const CLIENT_ID = "IUO8d5VVOIUCzWQArQ3VuXfbwx5QekZfLeDlpOmW"

enum SymbolicErrors {
NOT_AUTHORIZED,
Expand All @@ -23,14 +23,14 @@ static func set_token(token):
const Requestor = preload("res://addons/sketchfab/Requestor.gd")
var Result = Requestor.Result

var requestor = Requestor.new(API_HOSTNAME, USE_SSL)
var requestor = Requestor.new(API_HOSTNAME)
var busy = false

func term():
requestor.term()

func cancel():
yield(requestor.cancel(), "completed")
await requestor.cancel()

func login(username, password):
var query = {
Expand All @@ -39,13 +39,13 @@ func login(username, password):
}

busy = true
var requestor = Requestor.new(OAUTH_HOSTNAME, USE_SSL)
var requestor = Requestor.new(OAUTH_HOSTNAME)
requestor.request(
"/oauth2/token/?grant_type=password&client_id=%s" % CLIENT_ID, query,
{ "method": HTTPClient.METHOD_POST, "encoding": "form" }
)

var result = yield(requestor, "completed")
var result = await requestor.completed
requestor.term()
busy = false

Expand All @@ -61,7 +61,7 @@ func login(username, password):
func get_my_info():
busy = true
requestor.request("%s/me" % BASE_PATH, null, { "token": get_token() })
var result = yield(requestor, "completed")
var result = await requestor.completed
busy = false

return _handle_result(result)
Expand All @@ -70,7 +70,7 @@ func get_categories():
busy = true
requestor.request("%s/categories" % BASE_PATH)

var result = yield(requestor, "completed")
var result = await requestor.completed
busy = false

return _handle_result(result)
Expand All @@ -79,7 +79,7 @@ func get_model_detail(uid):
busy = true
requestor.request("%s/models/%s" % [BASE_PATH, uid])

var result = yield(requestor, "completed")
var result = await requestor.completed
busy = false

return _handle_result(result)
Expand All @@ -88,7 +88,7 @@ func request_download(uid):
busy = true
requestor.request("%s/models/%s/download" % [BASE_PATH, uid], null, { "token": get_token() })

var result = yield(requestor, "completed")
var result = await requestor.completed
busy = false

return _handle_result(result)
Expand Down Expand Up @@ -117,19 +117,19 @@ func search_models(q, categories, animated, staff_picked, min_face_count, max_fa
var search_domain = BASE_PATH + domain_suffix
requestor.request(search_domain, query, { "token": get_token() })

var result = yield(requestor, "completed")
var result = await requestor.completed
busy = false

return _handle_result(result)

func fetch_next_page(url):
# Strip protocol + domain
var uri = url.right(url.find(API_HOSTNAME) + API_HOSTNAME.length())
var uri = url.right(url.length() - url.find(API_HOSTNAME) - API_HOSTNAME.length())

busy = true
requestor.request(uri)

var result = yield(requestor, "completed")
var result = await requestor.completed
busy = false

return _handle_result(result)
Expand Down
101 changes: 42 additions & 59 deletions addons/sketchfab/HttpImage.gd
Original file line number Diff line number Diff line change
@@ -1,57 +1,38 @@
tool
extends Control
@tool
extends TextureRect

const MAX_COUNT = 4

export var max_size = 256
export var background = Color(0, 0, 0, 0)
export var immediate = false
@export var max_size = 256
@export var background = Color(0, 0, 0, 0)
@export var immediate = false

var url setget _set_url
var url : set = _set_url
var url_to_load

var http = HTTPRequest.new()
var http_request = null
var busy

var texture

func _enter_tree():
if !get_tree().has_meta("__http_image_count"):
get_tree().set_meta("__http_image_count", 0)

if !http.get_parent():
add_child(http)
if !http_request:
http_request = HTTPRequest.new()
add_child(http_request)
http_request.request_completed.connect(self._http_request_completed)
http_request.set_tls_options(TLSOptions.client())

busy = false
if url_to_load:
_start_load()

func _exit_tree():
if busy:
http.cancel_request()
http_request.cancel_request()
get_tree().set_meta("__http_image_count", get_tree().get_meta("__http_image_count") - 1)
busy = false

func _draw():
var rect = Rect2(0, 0, get_rect().size.x, get_rect().size.y)
draw_rect(rect, background)

if !texture:
return

var tw = texture.get_width()
var th = texture.get_height()

if float(tw) / th > rect.size.x / rect.size.y:
var old = rect.size.y
rect.size.y = rect.size.x * float(th) / tw
rect.position.y += 0.5 * (old - rect.size.y)
else:
var old = rect.size.x
rect.size.x = rect.size.y * float(tw) / th
rect.position.x += 0.5 * (old - rect.size.x)

draw_texture_rect(texture, rect, false)

func _set_url(url):
url_to_load = url
Expand All @@ -61,11 +42,12 @@ func _set_url(url):
_start_load()

func _start_load():
http.cancel_request()
http_request.cancel_request()
texture = null
update()
queue_redraw()

if !url_to_load:
print("there was no url to load from")
return

while true:
Expand All @@ -76,39 +58,40 @@ func _start_load():
get_tree().set_meta("__http_image_count", count + 1)
break
else:
yield(get_tree(), "idle_frame")
await get_tree().process_frame

_load(url_to_load)
url_to_load = null

func _load(url_to_load):
http.request(url_to_load, [], false)
func _load(url_to_load):# Create an HTTP request node and connect its completion signal.
# Perform the HTTP request. The URL below returns a PNG image as of writing.
var error = http_request.request(url_to_load)
if error != OK:
push_error("An error occurred in the HTTP request.")

busy = true
var data = yield(http, "request_completed")
# Called when the HTTP request is completed.
func _http_request_completed(result, response_code, headers, body):

busy = false
get_tree().set_meta("__http_image_count", get_tree().get_meta("__http_image_count") - 1)

var result = data[0]
var code = data[1]
var headers = data[2]
var body = data[3]

if result != HTTPRequest.RESULT_SUCCESS:
push_error("Image couldn't be downloaded. Try a different image.")

var image = Image.new()
var error = image.load_jpg_from_buffer(body)
if error != OK:
push_error("Couldn't load the image.")
return

var img = Image.new()
if img.load_jpg_from_buffer(body) == OK || img.load_png_from_buffer(body) == OK:
var w = img.get_width()
var h = img.get_height()
if w > h:
var new_w = min(w, max_size)
img.resize(new_w, (float(h) / w) * new_w)
else:
var new_h = min(h, max_size)
img.resize((float(w) / h) * new_h, new_h)
# Display the image in a TextureRect node.
var w = image.get_width()
var h = image.get_height()
if w > h:
var new_w = min(w, max_size)
image.resize(new_w, (float(h) / w) * new_w)
else:
var new_h = min(h, max_size)
image.resize((float(w) / h) * new_h, new_h)

texture = ImageTexture.create_from_image(image)


texture = ImageTexture.new()
texture.create_from_image(img)
update()
Loading