203 lines
5.7 KiB
GDScript
203 lines
5.7 KiB
GDScript
extends VBoxContainer
|
|
|
|
const TimeSlot = preload("res://scenes/board/time_slot.tscn")
|
|
const ReservationScene = preload("res://scenes/board/reservation.tscn")
|
|
|
|
const WorkingDayStart = 8
|
|
const WorkingDayEnd = 20
|
|
|
|
const MinimalMinutesToShowTitle = 30
|
|
const MinimalMinutesForBigSize = 60
|
|
|
|
const DateUpdateInterval := 60.0
|
|
const ScheduleUpdateInterval := 1.0 # 5.0
|
|
|
|
@onready var _main: Main = get_tree().get_current_scene()
|
|
@onready var _timeline = $Panel/Timeline
|
|
@onready var _reservations = $Panel/Reservations
|
|
@onready var _date = $TopBar/DateButton
|
|
@onready var _room = $TopBar/RoomButton
|
|
|
|
@onready var _rooms_menu = $RoomsMenu
|
|
|
|
var _date_update_timer := 0.0
|
|
var _schedule_update_timer := 0.0
|
|
var _block_reservation_creation := false
|
|
|
|
func _process(delta):
|
|
_process_hour_size()
|
|
_process_date(delta)
|
|
_process_fonts()
|
|
|
|
func _process_hour_size():
|
|
var hour_size = get_viewport_rect().size.y/15
|
|
|
|
for time_slot in _timeline.get_children():
|
|
time_slot.set_height(hour_size)
|
|
|
|
func _process_date(delta):
|
|
_date_update_timer += delta
|
|
if _date_update_timer >= DateUpdateInterval:
|
|
_date.text = _main.get_date()
|
|
_date_update_timer = 0.0
|
|
|
|
var date = Time.get_date_dict_from_system()
|
|
_date.text = "%02d.%02d.%04d" % [date.day, date.month, date.year]
|
|
|
|
func _process_fonts():
|
|
_process_font_size(_date, 35)
|
|
_process_font_size(_room, 35)
|
|
|
|
func _process_font_size(obj, k):
|
|
var font_size = obj.get_theme_default_font_size()
|
|
var new_font_size = get_viewport_rect().size.y/k
|
|
if font_size != new_font_size:
|
|
obj.add_theme_font_size_override("font_size", new_font_size)
|
|
|
|
func _process_schedule(delta):
|
|
if _schedule_update_timer >= ScheduleUpdateInterval:
|
|
_schedule_update_timer = 0.0
|
|
_update_schedule()
|
|
|
|
_schedule_update_timer += delta
|
|
|
|
func _ready():
|
|
_connect_signals()
|
|
|
|
_remove_time_slots()
|
|
_remove_reservations()
|
|
|
|
_fill_with_slots()
|
|
|
|
func _connect_signals():
|
|
await _main.ready
|
|
var event_handler = _main.get_event_handler()
|
|
event_handler.reservations_updated.connect(_on_reservations_updated)
|
|
|
|
_room.pressed.connect(_on_room_button_pressed)
|
|
_rooms_menu.item_clicked.connect(_on_room_selected)
|
|
|
|
func _remove_time_slots():
|
|
for time_slot in _timeline.get_children():
|
|
time_slot.queue_free()
|
|
|
|
func _fill_with_slots():
|
|
for hour in range(WorkingDayStart, WorkingDayEnd):
|
|
var slot = TimeSlot.instantiate()
|
|
_timeline.add_child(slot)
|
|
slot.set_time(hour)
|
|
|
|
func _remove_reservations():
|
|
for reservation in _reservations.get_children():
|
|
reservation.queue_free()
|
|
|
|
func _update_schedule(reservations=null):
|
|
if not _main.is_node_ready():
|
|
await _main.ready
|
|
|
|
var repo = _main.get_reservation_repo()
|
|
|
|
if reservations == null:
|
|
reservations = await repo.list_reservations({"date": _main.get_date()})
|
|
|
|
for reservation in reservations:
|
|
var start_time_hours = reservation.start_time.hours
|
|
var start_time_minutes = reservation.start_time.minutes
|
|
var start_time = (start_time_hours - WorkingDayStart)*60 + start_time_minutes
|
|
|
|
var finish_time_hours = reservation.finish_time.hours
|
|
var finish_time_minutes = reservation.finish_time.minutes
|
|
var finish_time = (finish_time_hours - WorkingDayStart)*60 + finish_time_minutes
|
|
|
|
var reservation_time = finish_time - start_time
|
|
|
|
_compose_reservation(start_time, reservation_time, \
|
|
reservation.title, reservation.color, reservation.id)
|
|
|
|
func _compose_reservation(start_time, duration_time, title, color, id):
|
|
if duration_time < MinimalMinutesToShowTitle:
|
|
title = ""
|
|
|
|
var reservation = ReservationScene.instantiate()
|
|
_reservations.add_child(reservation)
|
|
|
|
reservation.set_start_time(start_time)
|
|
reservation.set_duration_time(duration_time)
|
|
reservation.set_title(title)
|
|
reservation.set_color(color)
|
|
reservation.set_id(id)
|
|
|
|
if duration_time < MinimalMinutesForBigSize:
|
|
reservation.set_font(reservation.Fonts.MEDIUM)
|
|
|
|
func _input(event):
|
|
if not _main.get_current_page() == Main.Pages.Board:
|
|
return
|
|
|
|
if event is InputEventScreenTouch or (event is InputEventMouseButton and event.pressed):
|
|
if _clicked_on_timeline(event.position):
|
|
await get_tree().create_timer(0.01, false).timeout
|
|
|
|
if _block_reservation_creation:
|
|
_block_reservation_creation = false
|
|
return
|
|
|
|
if _main.get_current_page() == Main.Pages.Board:
|
|
var time = _get_time_by_position(event.position)
|
|
|
|
if _time_is_before_current_time(time):
|
|
return
|
|
|
|
var creation_page = _main.load_page(Main.Pages.ReservationCreation)
|
|
creation_page.set_start_time(time)
|
|
|
|
func _get_time_by_position(position):
|
|
var hour_size = get_viewport_rect().size.y / 15
|
|
var hours_with_minutes_in_float = \
|
|
(position.y - _timeline.global_position.y) / hour_size + WorkingDayStart
|
|
var hours = floor(hours_with_minutes_in_float)
|
|
var minutes = (hours_with_minutes_in_float - hours) * 60
|
|
var minutes_normalized = floor(minutes/30)*30
|
|
|
|
return {"hours": hours, "minutes": minutes_normalized}
|
|
|
|
func _clicked_on_timeline(pos: Vector2):
|
|
var timeline_position = _timeline.global_position
|
|
return (
|
|
pos.x > timeline_position.x and
|
|
pos.x < _timeline.size.x + timeline_position.x
|
|
) and (
|
|
pos.y > timeline_position.y and
|
|
pos.y < _timeline.size.y + timeline_position.y
|
|
)
|
|
|
|
func _time_is_before_current_time(time):
|
|
var current_time = Time.get_time_dict_from_system()
|
|
var time_in_minutes = time.hours*60 + time.minutes
|
|
var current_time_in_minutes = current_time.hour*60 + current_time.minute
|
|
|
|
return time_in_minutes < current_time_in_minutes
|
|
|
|
func _on_room_button_pressed():
|
|
if _rooms_menu.visible:
|
|
_rooms_menu.hide()
|
|
else:
|
|
_rooms_menu.position = _date.position
|
|
_rooms_menu.show()
|
|
|
|
func _on_room_selected(idx, text):
|
|
_block_reservation_creation = true
|
|
_room.text = text
|
|
_rooms_menu.hide()
|
|
|
|
func _on_date_button_pressed():
|
|
print("emit change date signal")
|
|
|
|
func _on_reservations_updated(reservations):
|
|
await get_tree().create_timer(0.1, false).timeout
|
|
update()
|
|
|
|
func update():
|
|
_ready()
|
|
_update_schedule()
|