Skip to content

Commit

Permalink
Add _total_results info & fix pc_info.visible on back button
Browse files Browse the repository at this point in the history
  • Loading branch information
Răzvan C. Rădulescu committed May 26, 2021
1 parent e33b065 commit af7f02b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
5 changes: 3 additions & 2 deletions project/PexelsQD/Main/SpinBoxTimeInput.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extends SpinBox

const MIN_VALUES := {true: 1, false: 5}
const PATTERN := "^(\\d{1,3})$"

var regex := RegEx.new()
Expand All @@ -17,13 +18,13 @@ func _ready() -> void:


func _on_CheckBoxTime_toggled(is_button_pressed: bool) -> void:
min_value = 1 if is_button_pressed else 5
min_value = MIN_VALUES[is_button_pressed]


func _on_LineEdit_text_changed(new_text: String) -> void:
var old_caret_position := le.caret_position
var regex_match := regex.search(new_text)

if regex_match == null:
le.text = old_text
le.caret_position = old_caret_position - 1
Expand Down
3 changes: 3 additions & 0 deletions project/PexelsQD/PanelContainerInfo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends PanelContainer

var html_color: String = "" setget , get_html_color

onready var l_trt: Label = $CenterContainer/VBoxContainer/LabelTotalResultsText
onready var rtl_author: RichTextLabel = $CenterContainer/VBoxContainer/RichTextLabelAuthor
onready var cr: ColorRect = $CenterContainer/VBoxContainer/ColorRect

Expand All @@ -14,9 +15,11 @@ func _ready() -> void:
func refresh(photo: Dictionary = {}) -> void:
if photo.empty():
rtl_author.bbcode_text = ""
l_trt.text = ""
cr.color = Color.white
cr.visible = false
else:
l_trt.text = "%d" % photo.total_results
var text := "[right][url={photographer_url}]{photographer}[/url][/right]"
rtl_author.bbcode_text = text.format(photo)
cr.color = photo.avg_color
Expand Down
13 changes: 7 additions & 6 deletions project/PexelsQD/PexelsQD.gd
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ func _ready() -> void:
_session = Session.new(config_file, http_request)
_tr_image_placeholder = tr_image.texture
NOTIFICATIONS.search = NOTIFICATIONS.search.format([Constants.MIN_SEARCH_LENGTH])

pc_intro.connect("notified", self, "_notify")
tb_next.connect("pressed", vbc_main, "set_visible", [true])
tb_back.connect("pressed", vbc_main, "set_visible", [false])
tb_back.connect("pressed", tb_help, "set_pressed", [false])
tb_back.connect("pressed", tb_info, "set_pressed", [false])
tb_back.connect("pressed", pc_intro, "set_visible", [true])
tb_back.connect("pressed", pc_help, "set_visible", [false])
tb_back.connect("pressed", tb_play_pause, "set_pressed", [false])
le_search.connect("text_entered", self, "_on_LineEditSearch_text_entered")
le_search.connect("text_validated", self, "_on_LineEditSearch_text_validated")
Expand All @@ -78,11 +79,11 @@ func _ready() -> void:
tb_pexels.connect("pressed", OS, "shell_open", [Constants.URLS.pexels])
pc_info.cr.connect("gui_input", self, "_on_PanelContainerInfoColorRect_gui_input")
tween.connect("tween_all_completed", self, "_search")

OS.min_window_size = Constants.MIN_WINDOW_SIZE
pc_intro.setup(config_file)
rtl_help.bbcode_text = rtl_help.bbcode_text.format([Constants.DELTA])

var api_key: String = config_file.get_value(
Constants.CONFIG_FILE.section, Constants.CONFIG_FILE.key, ""
)
Expand Down Expand Up @@ -127,7 +128,7 @@ func _on_TextureButtonStop_pressed() -> void:


func _on_PanelContainerInfoColorRect_gui_input(event: InputEvent) -> void:
if event.is_action_pressed("left_click"):
if pc_info.cr.visible and event.is_action_pressed("left_click"):
OS.clipboard = pc_info.html_color
_notify(NOTIFICATIONS.color.format([OS.clipboard]))

Expand All @@ -145,7 +146,7 @@ func _search() -> void:
_notify(NOTIFICATIONS.search)
tb_stop.emit_signal("pressed")
return

var photo = yield(_session.search(le_search.text), "completed")
match [photo, tb_play_pause.pressed]:
[{"texture": var texture, ..}, true]:
Expand Down
32 changes: 22 additions & 10 deletions project/PexelsQD/PexelsQD.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ anchor_right = 1.0
margin_left = -424.0
margin_top = 48.0
margin_right = -18.0
margin_bottom = 126.0
margin_bottom = 150.0
mouse_filter = 2
custom_styles/panel = SubResource( 21 )
script = ExtResource( 10 )
Expand All @@ -449,40 +449,52 @@ __meta__ = {
margin_left = 32.0
margin_top = 8.0
margin_right = 398.0
margin_bottom = 70.0
margin_bottom = 94.0

[node name="VBoxContainer" type="GridContainer" parent="PanelContainerInfo/CenterContainer"]
margin_top = 3.0
margin_right = 366.0
margin_bottom = 59.0
margin_bottom = 86.0
columns = 2

[node name="LabelAuthor" type="Label" parent="PanelContainerInfo/CenterContainer/VBoxContainer"]
[node name="LabelTotalResults" type="Label" parent="PanelContainerInfo/CenterContainer/VBoxContainer"]
margin_right = 148.0
margin_bottom = 26.0
text = "Total Results:"

[node name="LabelTotalResultsText" type="Label" parent="PanelContainerInfo/CenterContainer/VBoxContainer"]
margin_left = 152.0
margin_right = 366.0
margin_bottom = 26.0
align = 2

[node name="LabelAuthor" type="Label" parent="PanelContainerInfo/CenterContainer/VBoxContainer"]
margin_top = 30.0
margin_right = 148.0
margin_bottom = 56.0
text = "Author:"

[node name="RichTextLabelAuthor" type="RichTextLabel" parent="PanelContainerInfo/CenterContainer/VBoxContainer"]
margin_left = 152.0
margin_top = 30.0
margin_right = 366.0
margin_bottom = 26.0
margin_bottom = 56.0
rect_min_size = Vector2( 214, 0 )
size_flags_horizontal = 3
bbcode_enabled = true
scroll_active = false

[node name="LabelAverageColor" type="Label" parent="PanelContainerInfo/CenterContainer/VBoxContainer"]
margin_top = 30.0
margin_top = 60.0
margin_right = 148.0
margin_bottom = 56.0
margin_bottom = 86.0
text = "Average Color:"

[node name="ColorRect" type="ColorRect" parent="PanelContainerInfo/CenterContainer/VBoxContainer"]
visible = false
margin_left = 340.0
margin_top = 30.0
margin_top = 60.0
margin_right = 366.0
margin_bottom = 56.0
margin_bottom = 86.0
rect_min_size = Vector2( 26, 26 )
hint_tooltip = "Copy color to clipboard."
mouse_default_cursor_shape = 2
Expand Down
21 changes: 11 additions & 10 deletions project/PexelsQD/Session.gd
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,38 @@ func _init(config_file: ConfigFile, http_request: HTTPRequest) -> void:
_config_file = config_file
_http_request = http_request
_image_funcs.jpeg = _image_funcs.jpg

_rng.randomize()
_regex.compile(PATTERN)


func search(query: String) -> Dictionary:
var is_first := _previous_query != query
_previous_query = query

var params := {
"base_url": PHOTO.base_url,
"query": query,
"page": 1 if is_first else _rng.randi_range(1, _total_results)
}

var api_key: String = _config_file.get_value(
Constants.CONFIG_FILE.section, Constants.CONFIG_FILE.key, ""
)
PHOTO.headers[-1] = PHOTO.headers[-1].format({"api_key": api_key})
_http_request.request(PHOTO.search.format(params), PHOTO.headers)
var ret: Array = yield(_http_request, "request_completed")

if not _is_result_ok(ret[0], ret[1]):
return {"error": NOTIFICATIONS.http.format(ret)}

var body := JSON.parse(ret[3].get_string_from_utf8())
if body.error != OK:
return {"error": NOTIFICATIONS.json.format([body.error, body.error_line, body.error_string])}

if body.result.has("error"):
return {"error": NOTIFICATIONS.result.format([body.result.error])}

if is_first:
_total_results = body.result.total_results
return search(params.query)
Expand All @@ -81,20 +81,21 @@ func search(query: String) -> Dictionary:
var type := regex_result.get_string(1)
_http_request.request(src)
ret = yield(_http_request, "request_completed")

if not _is_result_ok(ret[0], ret[1]):
return {"error": NOTIFICATIONS.http.format(ret)}

var _image := Image.new()
_image.call(_image_funcs[type], ret[3])
photo.texture = ImageTexture.new()
photo.texture.create_from_image(_image)
photo.total_results = _total_results
return photo
else:
return {"error": NOTIFICATIONS.unsupported.format(src)}
else:
return {"error": NOTIFICATIONS.zero.format([query])}

# Function should never reach this code
return NOTIFICATIONS.unknown

Expand Down

0 comments on commit af7f02b

Please sign in to comment.