godot-flashcards/scenes/ui/quiz_mode/multiple_choice_display.gd
2025-12-27 00:20:33 -05:00

67 lines
2 KiB
GDScript

extends PanelContainer
signal answer_submitted(is_correct: bool)
@onready var question_label = $MarginContainer/VBoxContainer/QuestionPanel/MarginContainer/QuestionLabel
@onready var choices_container = $MarginContainer/VBoxContainer/ChoicesContainer
@onready var feedback_panel = $MarginContainer/VBoxContainer/FeedbackPanel
@onready var feedback_label = $MarginContainer/VBoxContainer/FeedbackPanel/MarginContainer/FeedbackLabel
var current_card: Flashcard
var choice_buttons: Array[Button] = []
var answered: bool = false
func _ready() -> void:
feedback_panel.hide()
func set_card(card: Flashcard) -> void:
current_card = card
answered = false
question_label.text = card.question_text
question_label.modulate = card.question_color
for button in choice_buttons:
button.queue_free()
choice_buttons.clear()
for i in range(card.choices.size()):
var choice = card.choices[i]
var button = Button.new()
button.text = choice.text
button.custom_minimum_size = Vector2(0, 50)
button.add_theme_font_size_override("font_size", 18)
button.pressed.connect(_on_choice_selected.bind(i))
choices_container.add_child(button)
choice_buttons.append(button)
feedback_panel.hide()
func _on_choice_selected(choice_index: int) -> void:
if answered:
return
answered = true
var is_correct = current_card.choices[choice_index].is_correct
for button in choice_buttons:
button.disabled = true
if is_correct:
choice_buttons[choice_index].modulate = Color.GREEN
feedback_label.text = "✓ Correct! " + current_card.answer_text
feedback_panel.self_modulate = Color(0.2, 0.6, 0.2, 1.0)
else:
choice_buttons[choice_index].modulate = Color.RED
var correct_index = current_card.get_correct_choice_index()
if correct_index >= 0:
choice_buttons[correct_index].modulate = Color.GREEN
feedback_label.text = "❌ Incorrect. The answer is: " + current_card.answer_text
feedback_panel.self_modulate = Color(0.6, 0.2, 0.2, 1.0)
feedback_panel.show()
await get_tree().create_timer(2.0).timeout
answer_submitted.emit(is_correct)