Skip to content

Commit

Permalink
Merge pull request #16 from sketchfab/store-purchases
Browse files Browse the repository at this point in the history
[#D3D-5748] Adds ability to download own models and store purchases
  • Loading branch information
cedricpinson authored Dec 21, 2020
2 parents 379ad4d + 7e0950c commit f88e23b
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 49 deletions.
12 changes: 6 additions & 6 deletions addons/sketchfab/Api.gd
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ func request_download(uid):

return _handle_result(result)

func search_models(q, categories, animated, staff_picked, min_face_count, max_face_count, sort_by):
var query = {
"type": "models",
"downloadable": "true",
}
func search_models(q, categories, animated, staff_picked, min_face_count, max_face_count, sort_by, domain_suffix):

var query = {}

if q:
query.q = q
Expand All @@ -115,7 +113,9 @@ func search_models(q, categories, animated, staff_picked, min_face_count, max_fa
query.sort_by = sort_by

busy = true
requestor.request("%s/search" % BASE_PATH, query)

var search_domain = BASE_PATH + domain_suffix
requestor.request(search_domain, query, { "token": get_token() })

var result = yield(requestor, "completed")
busy = false
Expand Down
38 changes: 36 additions & 2 deletions addons/sketchfab/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ const SORT_BY_OPTIONS = [
["Likes", "-likeCount"],
["Views", "-viewCount"],
]

const SEARCH_DOMAIN = [
["Whole site", "/search?type=models&downloadable=true"],
["Own models (PRO)", "/me/search?type=models&downloadable=true"],
["Purchased models", "/me/models/purchases?"],
]

const SORT_BY_DEFAULT_INDEX = 1
const DEFAULT_DOMAIN = 0

const SafeData = preload("res://addons/sketchfab/SafeData.gd")
const Utils = preload("res://addons/sketchfab/Utils.gd")
Expand All @@ -32,6 +40,9 @@ onready var search_animated = find_node("Search").find_node("Animated")
onready var search_staff_picked = find_node("Search").find_node("StaffPicked")
onready var search_face_count = find_node("Search").find_node("FaceCount")
onready var search_sort_by = find_node("Search").find_node("SortBy")
onready var search_domain = find_node("Search").find_node("SearchDomain")
onready var cta_button = find_node("CTA")
onready var trailer = find_node("Trailer")

onready var paginator = find_node("Paginator")

Expand Down Expand Up @@ -87,6 +98,12 @@ func _notification(what):
search_sort_by.add_item(item[0])
_commit_sort_by(SORT_BY_DEFAULT_INDEX)

for item in SEARCH_DOMAIN:
search_domain.add_item(item[0])
_commit_domain(DEFAULT_DOMAIN)
search_domain.hide()
cta_button.hide()

logged.visible = false
not_logged.visible = false
login_name.text = cfg.get_value("api", "user", "")
Expand Down Expand Up @@ -135,6 +152,10 @@ func _on_SortBy_item_selected(index):
_commit_sort_by(index)
_search()

func _on_SearchDomain_item_selected(index):
_commit_domain(index)
_search()

func _on_SearchButton_pressed():
_search()

Expand Down Expand Up @@ -164,6 +185,9 @@ func _login():
cfg.save(CONFIG_FILE_PATH)

func _populate_login():

search_domain.show()

_set_login_disabled(true)
var user = yield(api.get_my_info(), "completed")
_set_login_disabled(false)
Expand All @@ -182,7 +206,9 @@ func _populate_login():
logged_name.text = "User: %s" % user["username"]

var plan_name
if user["account"] == "pro":
if user["account"] == "plus":
plan_name = "PLUS"
elif user["account"] == "pro":
plan_name = "PRO"
elif user["account"] == "prem":
plan_name = "PREMIUM"
Expand All @@ -207,6 +233,10 @@ func _logout():
not_logged.visible = true
logged.visible = false
logged_avatar.url = null
search_domain.hide()
cta_button.hide()
trailer.modulate.a = 0.0
search_domain.set_meta("__suffix", SEARCH_DOMAIN[0][1])

func _load_categories():
var result = yield(api.get_categories(), "completed")
Expand All @@ -232,7 +262,8 @@ func _search():
search_staff_picked.pressed,
search_face_count.get_meta("__data")[1],
search_face_count.get_meta("__data")[2],
search_sort_by.get_meta("__key")
search_sort_by.get_meta("__key"),
search_domain.get_meta("__suffix")
)

##### Helpers
Expand Down Expand Up @@ -288,6 +319,9 @@ func _commit_face_count(index):
func _commit_sort_by(index):
search_sort_by.set_meta("__key", SORT_BY_OPTIONS[index][1])

func _commit_domain(index):
search_domain.set_meta("__suffix", SEARCH_DOMAIN[index][1])

func _set_login_disabled(disabled):
login_name.editable = !disabled
login_password.editable = !disabled
Expand Down
78 changes: 42 additions & 36 deletions addons/sketchfab/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,43 @@ lines_skipped = 0
max_lines_visible = -1
_sections_unfolded = [ "Anchor", "Focus", "Margin", "Rect", "Size Flags" ]

[node name="Text" type="LineEdit" parent="Search/_/_" index="1"]
[node name="SearchDomain" type="OptionButton" parent="Search/_/_" index="1"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = 56.0
margin_right = 878.0
margin_top = 5.0
margin_right = 146.0
margin_bottom = 19.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
size_flags_horizontal = 1
size_flags_vertical = 4
toggle_mode = false
action_mode = 0
enabled_focus_mode = 2
shortcut = null
group = null
flat = false
align = 0
items = [ ]
selected = -1
_sections_unfolded = [ "Rect", "Size Flags" ]
rect_max_size = Vector2( 120, 0 )


[node name="Text" type="LineEdit" parent="Search/_/_" index="2"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0

margin_left = -56.0
margin_right = -56
margin_bottom = 24.0

rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
focus_mode = 2
Expand All @@ -472,7 +500,7 @@ caret_blink_speed = 0.65
caret_position = 0
_sections_unfolded = [ "Caret", "Placeholder", "Size Flags" ]

[node name="Button" type="Button" parent="Search/_/_" index="2"]
[node name="Button" type="Button" parent="Search/_/_" index="3"]

anchor_left = 0.0
anchor_top = 0.0
Expand Down Expand Up @@ -868,46 +896,22 @@ custom_constants/hseparation = 16
columns = 6
_sections_unfolded = [ "Size Flags", "custom_constants" ]

[node name="Trailer" type="CenterContainer" parent="_2/Paginator/_" index="1"]
[node name="Trailer" type="VBoxContainer" parent="_2/Paginator/_" index="1"]

modulate = Color( 1, 1, 1, 0 )
anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_top = 4.0
margin_right = 1008.0
margin_bottom = 104.0
rect_min_size = Vector2( 0, 100 )
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 0
mouse_default_cursor_shape = 0
size_flags_horizontal = 3
size_flags_vertical = 1
use_top_left = false
_sections_unfolded = [ "Rect", "Size Flags" ]

[node name="Label" type="Label" parent="_2/Paginator/_/Trailer" index="0"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = 471.0
margin_top = 43.0
margin_right = 537.0
margin_bottom = 57.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
size_flags_horizontal = 4
text = "Fetching..."
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1

[node name="CTA" type="Button" parent="_2/Paginator/_/Trailer" index="1"]

disabled=false
size_flags_horizontal = 4
text = "Upgrade to PRO"

[connection signal="pressed" from="Header/_/Logged/_/MainBlock/Logout" to="." method="_on_Logout_pressed"]

Expand All @@ -931,6 +935,8 @@ max_lines_visible = -1

[connection signal="item_selected" from="Search/_/_3/_/_/SortBy" to="." method="_on_SortBy_item_selected"]

[connection signal="item_selected" from="Search/_/_/SearchDomain" to="." method="_on_SearchDomain_item_selected"]

[connection signal="item_selected" from="Search/_/_3/_/_2/FaceCount" to="." method="_on_FaceCount_item_selected"]


37 changes: 34 additions & 3 deletions addons/sketchfab/Paginator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var api = preload("res://addons/sketchfab/Api.gd").new()

onready var grid = find_node("ResultsGrid")
onready var trailer = find_node("Trailer")
onready var label = find_node("Label")
onready var cta_button = find_node("CTA")
onready var search_domain = find_node("SearchDomain")

var next_page_url

Expand All @@ -17,12 +20,14 @@ func _ready():
func _exit_tree():
api.term()

func search(query, categories, animated, staff_picked, min_face_count, max_face_count, sort_by):
func search(query, categories, animated, staff_picked, min_face_count, max_face_count, sort_by, domain_suffix):
for item in grid.get_children():
grid.remove_child(item)
queue_sort()

trailer.modulate.a = 1.0
label.text = "Fetching..."
cta_button.hide()
yield(api.cancel(), "completed")
var result = yield(api.search_models(
query,
Expand All @@ -31,16 +36,40 @@ func search(query, categories, animated, staff_picked, min_face_count, max_face_
staff_picked,
min_face_count,
max_face_count,
sort_by
sort_by,
domain_suffix
), "completed")
trailer.modulate.a = 0.0

_process_page(result)
var n_results = _process_page(result)

# Upgrade to pro and empty results
if domain_suffix == "/me":
var user = yield(api.get_my_info(), "completed")
if user["account"] == "plus" || user["account"] == "basic":
trailer.modulate.a = 1.0
label.text = "Access your personal library of 3D models"
cta_button.show()
cta_button.text = "Upgrade to PRO"
elif n_results == 0:
trailer.modulate.a = 1.0
label.text = "No results found"
elif n_results == 0:
trailer.modulate.a = 1.0
label.text = "No results found"
if domain_suffix == "/me/models/purchases":
cta_button.show()
cta_button.text = "Visit the Store"
else:
trailer.modulate.a = 0.0
cta_button.hide()

func _process(delta):
if !api.busy && next_page_url && trailer.get_global_rect().intersects(get_viewport_rect()):
# Fetch next page
trailer.modulate.a = 1.0
label.text = "Fetching..."
cta_button.hide()
var result = yield(api.fetch_next_page(next_page_url), "completed")
trailer.modulate.a = 0.0

Expand All @@ -66,3 +95,5 @@ func _process_page(result):

# Set next page now we know the current one succeeded
next_page_url = SafeData.string(result, "next")

return results.size()
2 changes: 1 addition & 1 deletion addons/sketchfab/Requestor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func request(path, payload = null, options = DEFAULT_OPTIONS):
if payload:
var encoding = _get_option(options, "encoding")
if encoding == "query":
uri += "?" + _dict_to_query_string(payload)
uri += "&" + _dict_to_query_string(payload)
elif encoding == "json":
headers.append("Content-Type: application/json")
encoded_payload = to_json(payload)
Expand Down
2 changes: 1 addition & 1 deletion addons/sketchfab/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="Sketchfab"
description="Sketchfab browsing & importing right inside Godot"
author="Sketchfab"
version="1.0.1"
version="1.1.0"
script="plugin.gd"

0 comments on commit f88e23b

Please sign in to comment.