Skip to content

Commit

Permalink
GUI: Made focus info in journal tween left/right
Browse files Browse the repository at this point in the history
  • Loading branch information
db0 committed May 17, 2022
1 parent 38dd8cc commit b9c7cf3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
100 changes: 56 additions & 44 deletions src/core/CardViewer/CVPreviewPopup.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,65 @@
class_name CVPreviewPopup
extends Popup

signal placement_initialized
# The card currently being shown in a popup.
var preview_card: Card
var _tween_wait := 0
var _placement_initialized := false
var _visible = true
# The popup panel which contains the card.
onready var focus_info := $FocusInfo
onready var _tween := $Tween

func _ready() -> void:
# warning-ignore:return_value_discarded
get_viewport().connect("size_changed", self, '_on_viewport_resized')

connect("placement_initialized",self, "_on_placement_initialized")

func _process(_delta: float) -> void:
if visible and is_instance_valid(preview_card):
rect_position = get_preview_placement()
# This ensures the FocusInfoPanel is always on the bottom of the card
# for c in focus_info.get_children():
# # We use this to adjust the info panel labels depending on how the
# # PREVIEW_SCALE is
# c.get_node("Details").rect_min_size.x = preview_card.canonical_size.x * preview_card.preview_scale * cfc.curr_scale
# c.get_node("Details").rect_size.x = preview_card.canonical_size.x * preview_card.preview_scale * cfc.curr_scale
# c.rect_min_size.x = preview_card.canonical_size.x * preview_card.preview_scale * cfc.curr_scale
# c.rect_size.x = preview_card.canonical_size.x * preview_card.preview_scale * cfc.curr_scale
focus_info.rect_min_size.x = 0.0
focus_info.rect_size.x = 0.0
focus_info.rect_position.y = 0
focus_info.rect_position.x = preview_card.canonical_size.x * preview_card.preview_scale * cfc.curr_scale
if _placement_initialized and visible and is_instance_valid(preview_card):
_set_placement()

func _set_placement() -> void:
if _tween.is_active():
return
var new_position : Vector2 = get_preview_placement()
# We only want to tween, if the card position is changing dramatically
# such as when the info panels would exceed the width of the monitor, and therefore
# we need to move them to the other side of the mouse cursor.
if not _placement_initialized:
yield(get_tree().create_timer(0.1), "timeout")
rect_position = get_preview_placement()
elif new_position.distance_to(rect_position) > 200:
_tween_wait += 1
# This is needed because the focus_info is wiped of all panels every time it's refreshed
# so there's a small period of time where it's width is always 0
# This causes the tween to activate twice back and forth every time a card hover is switched to
# a new card.
# To avoid that, we put a small delay, to ensure the info panels have neen repopulated
if _tween_wait > 10:
_tween_wait = 0
_tween.interpolate_property(self, "rect_position", rect_position, new_position, 0.2, Tween.TRANS_EXPO, Tween.EASE_IN)
_tween.start()
# print_debug([preview_card, get_preview_placement()])
else:
rect_position = new_position
focus_info.rect_min_size.x = 0.0
focus_info.rect_size.x = 0.0
focus_info.rect_position.y = 0
focus_info.rect_position.x = preview_card.canonical_size.x * preview_card.preview_scale * cfc.curr_scale
if not _placement_initialized:
_placement_initialized = true
emit_signal("placement_initialized")


# Figures out where to place the card preview in respect to the player's mouse.
func get_preview_placement() -> Vector2:
# warning-ignore:unassigned_variable
var ret : Vector2
var focus_panel_offset = 0
var card_size : Vector2 = preview_card.canonical_size * preview_card.preview_scale * cfc.curr_scale
# If the card width is to small, we will place the info panels instead to the left of the card preview
if focus_info.visible:
focus_panel_offset = focus_info.rect_size.y
# We want the card to be on the right of the mouse always
# Unless that would make it hide outside the viewport.
# In which case we place it on the left of the mouse instead
# if get_global_mouse_position().x\
# + card_size.x\
# + 20\
# > get_viewport().size.x:
# ret.x = get_global_mouse_position().x - card_size.x - 20
# else:
# ret.x = get_global_mouse_position().x + 20
# if is_instance_valid(preview_card) and get_global_mouse_position().y\
# + card_size.y\
# + focus_panel_offset\
# > get_viewport().size.y:
# ret.y = get_viewport().size.y\
# - card_size.y\
# * preview_card.scale.y\
# - focus_panel_offset
# else:
# ret.y = get_global_mouse_position().y
if focus_info.visible:
focus_panel_offset = focus_info.rect_size.x
if get_global_mouse_position().x\
Expand All @@ -70,21 +73,20 @@ func get_preview_placement() -> Vector2:
else:
ret.x = get_global_mouse_position().x + 20
var card_offscreen_y = get_global_mouse_position().y\
+ card_size.y\
* preview_card.scale.y
+ card_size.y
var focus_offscreen_y = get_global_mouse_position().y + focus_info.rect_size.y
if card_offscreen_y > focus_offscreen_y\
and is_instance_valid(preview_card)\
and card_offscreen_y > get_viewport().size.y:
ret.y = get_viewport().size.y\
- card_size.y\
* preview_card.scale.y
+ 30
elif card_offscreen_y < focus_offscreen_y\
and focus_offscreen_y > get_viewport().size.y:
ret.y = get_viewport().size.y\
- focus_info.rect_size.y
- focus_info.rect_size.y + 30
else:
ret.y = get_global_mouse_position().y
ret.y = get_global_mouse_position().y + 30
# print_debug(ret)
return(ret)

Expand All @@ -107,18 +109,28 @@ func show_preview_card(card) -> void:
if CFConst.VIEWPORT_FOCUS_ZOOM_TYPE == "resize":
preview_card.resize_recursively(preview_card._control, preview_card.preview_scale * cfc.curr_scale)
preview_card.card_front.scale_to(preview_card.preview_scale * cfc.curr_scale)
rect_position = get_preview_placement()
cfc.ov_utils.populate_info_panels(preview_card,focus_info)
call_deferred("_set_placement")
mouse_filter = Control.MOUSE_FILTER_IGNORE
modulate.a = 1
visible = true
_visible = true
print(['show',preview_card])
if _placement_initialized:
modulate.a = 1



# Hides currently shown card.
# We're modulating to 0 instead of hide() because that messes with the rich text labels formatting
func hide_preview_card() -> void:
print(['hide',preview_card])
modulate.a = 0

_visible = false

func _on_placement_initialized() -> void:
if _visible:
modulate.a = 1

# We want to recreate the popup after the viewport size has changed
# As otherwise its elements may end up messed up
Expand Down
2 changes: 2 additions & 0 deletions src/core/CardViewer/CVPreviewPopup.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ margin_bottom = 40.0
custom_constants/vseparation = 10
custom_constants/hseparation = 10
script = ExtResource( 2 )

[node name="Tween" type="Tween" parent="."]

0 comments on commit b9c7cf3

Please sign in to comment.