mirror of
https://github.com/mr0xb/godot-flashcards.git
synced 2026-08-27 20:44:56 -04:00
91 lines
2.4 KiB
GDScript
91 lines
2.4 KiB
GDScript
class_name Flashcard
|
|
extends Resource
|
|
|
|
enum QuestionType {
|
|
BASIC,
|
|
MULTIPLE_CHOICE
|
|
}
|
|
|
|
@export var id: String = ""
|
|
@export var question_text: String = ""
|
|
@export var question_image: String = ""
|
|
@export var question_color: Color = Color.WHITE
|
|
@export var question_format: String = "plain"
|
|
|
|
@export var answer_text: String = ""
|
|
@export var answer_image: String = ""
|
|
@export var answer_color: Color = Color.WHITE
|
|
@export var answer_format: String = "plain"
|
|
|
|
@export var card_type: QuestionType = QuestionType.BASIC
|
|
|
|
@export var choices: Array[Dictionary] = []
|
|
|
|
@export var tags: Array[String] = []
|
|
@export var difficulty: int = 1
|
|
@export var hint: String = ""
|
|
|
|
func _init() -> void:
|
|
if id == "":
|
|
id = _generate_uuid()
|
|
|
|
func to_dict() -> Dictionary:
|
|
return {
|
|
"id": id,
|
|
"question": {
|
|
"text": question_text,
|
|
"image": question_image,
|
|
"color": question_color.to_html(),
|
|
"format": question_format
|
|
},
|
|
"answer": {
|
|
"text": answer_text,
|
|
"image": answer_image,
|
|
"color": answer_color.to_html(),
|
|
"format": answer_format
|
|
},
|
|
"type": "basic" if card_type == QuestionType.BASIC else "multiple_choice",
|
|
"choices": choices.duplicate(),
|
|
"metadata": {
|
|
"tags": tags.duplicate(),
|
|
"difficulty": difficulty,
|
|
"hint": hint
|
|
}
|
|
}
|
|
|
|
func from_dict(data: Dictionary) -> void:
|
|
id = data.get("id", _generate_uuid())
|
|
|
|
var question = data.get("question", {})
|
|
question_text = question.get("text", "")
|
|
question_image = question.get("image", "")
|
|
question_color = Color.from_string(question.get("color", "#FFFFFF"), Color.WHITE)
|
|
question_format = question.get("format", "plain")
|
|
|
|
var answer = data.get("answer", {})
|
|
answer_text = answer.get("text", "")
|
|
answer_image = answer.get("image", "")
|
|
answer_color = Color.from_string(answer.get("color", "#FFFFFF"), Color.WHITE)
|
|
answer_format = answer.get("format", "plain")
|
|
|
|
var type_str = data.get("type", "basic")
|
|
card_type = QuestionType.MULTIPLE_CHOICE if type_str == "multiple_choice" else QuestionType.BASIC
|
|
|
|
choices.assign(data.get("choices", []))
|
|
|
|
var metadata = data.get("metadata", {})
|
|
tags.assign(metadata.get("tags", []))
|
|
difficulty = metadata.get("difficulty", 1)
|
|
hint = metadata.get("hint", "")
|
|
|
|
func get_correct_choice_index() -> int:
|
|
for i in range(choices.size()):
|
|
if choices[i].get("is_correct", false):
|
|
return i
|
|
return -1
|
|
|
|
func _generate_uuid() -> String:
|
|
return "%x%x-%x-%x-%x-%x%x%x" % [
|
|
randi(), randi(), randi(), randi(),
|
|
randi(), randi(), randi(), randi()
|
|
]
|