2022-05-14 20:51:48 +02:00
extends HBoxContainer
class_name MessageGroupContainer
onready var profile_image_rect = get_node ( " ProfileImageRect " )
onready var header = get_node ( " VBoxContainer/Header " )
onready var username = get_node ( " VBoxContainer/Header/Username " )
onready var date = get_node ( " VBoxContainer/Header/Date " )
onready var message_container = get_node ( " VBoxContainer/MessageContainer " )
2022-06-04 13:33:00 +02:00
var associated_message_group : WeakRef
2022-05-14 20:51:48 +02:00
func _ready ( ) :
# warning-ignore:return_value_discarded
Users . connect ( " switched " , self , " _on_user_switched " )
_setup ( )
func get_user ( ) - > User :
2022-06-04 13:33:00 +02:00
return associated_message_group . get_ref ( ) . associated_user
2022-05-14 20:51:48 +02:00
func add ( message ) :
message_container . add_child ( message )
func _on_user_switched ( ) :
_adjust_layout_to_user ( )
func _setup ( ) :
profile_image_rect . texture = get_user ( ) . profile_image
username . text = get_user ( ) . username
# Define date.
2022-06-04 13:33:00 +02:00
var time_stamp = associated_message_group . get_ref ( ) . time_stamp
2022-05-14 20:51:48 +02:00
var storaged_date = OS . get_datetime_from_unix_time ( time_stamp )
var os_time = OS . get_datetime ( )
if storaged_date . day == os_time . day :
2022-06-04 13:33:00 +02:00
date . text = " Today at " + _get_time_string ( storaged_date )
elif storaged_date . day == os_time . day - 1 :
date . text = " Yesterday at " + _get_time_string ( storaged_date )
else :
date . text = _get_date_string ( storaged_date )
2022-05-14 20:51:48 +02:00
_adjust_layout_to_user ( )
2022-06-04 13:33:00 +02:00
func _get_time_string ( datetime : Dictionary ) - > String :
var hour = datetime . hour
var minute = datetime . minute
var am_pm
var offset_wrapped_hour = wrapi ( hour - 1 , 0 , 24 )
var processed_hour = offset_wrapped_hour
if offset_wrapped_hour > = 12 :
am_pm = " PM "
processed_hour -= 12
else :
am_pm = " AM "
processed_hour += 1
var hour_string = str ( processed_hour )
var minute_string = " %02d " % minute
return hour_string + " : " + minute_string + am_pm
func _get_date_string ( datetime : Dictionary ) - > String :
var month_string = " %02d " % datetime . month
var day_string = " %02d " % datetime . day
var year_string = str ( datetime . year )
return month_string + " / " + day_string + " / " + year_string
2022-05-14 20:51:48 +02:00
func _adjust_layout_to_user ( ) :
if get_user ( ) . is_current ( ) :
move_child ( profile_image_rect , 1 )
header . alignment = BoxContainer . ALIGN_END
size_flags_horizontal = SIZE_SHRINK_END
else :
move_child ( profile_image_rect , 0 )
header . alignment = BoxContainer . ALIGN_BEGIN
size_flags_horizontal = 0