87 lines
2.3 KiB
GDScript
87 lines
2.3 KiB
GDScript
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")
|
|
|
|
var associated_message_group: WeakRef
|
|
|
|
|
|
func _ready():
|
|
# warning-ignore:return_value_discarded
|
|
Users.connect("switched", self, "_on_user_switched")
|
|
_setup()
|
|
|
|
|
|
func get_user() -> User:
|
|
return associated_message_group.get_ref().associated_user
|
|
|
|
|
|
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.
|
|
var time_stamp = associated_message_group.get_ref().time_stamp
|
|
var storaged_date = OS.get_datetime_from_unix_time(time_stamp)
|
|
var os_time = OS.get_datetime()
|
|
|
|
if storaged_date.day == os_time.day:
|
|
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)
|
|
|
|
_adjust_layout_to_user()
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|