shared/domain/module/body/
find_answer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use crate::domain::module::{
    body::{
        Body, BodyConvert, BodyExt, ModeExt, StepExt, ThemeId,
        _groups::design::{BaseContent, Trace},
    },
    ModuleKind,
};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

mod play_settings;
pub use play_settings::*;

use super::{
    Audio, Transform,
    _groups::design::{Sticker, Text},
};

/// The body for [`FindAnswer`](crate::domain::module::ModuleKind::FindAnswer) modules.
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct ModuleData {
    /// The content
    pub content: Option<Content>,
}

impl BodyExt<Mode, Step> for ModuleData {
    fn as_body(&self) -> Body {
        Body::FindAnswer(self.clone())
    }

    fn is_complete(&self) -> bool {
        self.content
            .as_ref()
            .map_or(false, |content| content.is_valid())
    }

    fn kind() -> ModuleKind {
        ModuleKind::FindAnswer
    }

    fn new_with_mode_and_theme(mode: Mode, theme: ThemeId) -> Self {
        let mut base = BaseContent::default();
        base.theme = theme;

        let mut sticker_content = Text::from_value(format!(
            r#"{{"version":"0.1.0","content":[{{"children":[{{"text":"{}","element":"P2"}}]}}]}}"#,
            "Questions appear here / שאלות מופיעות כאן"
        ));
        sticker_content.transform.translation.0[1] = -0.35; // Move Y coord up 35%
        base.stickers.push(Sticker::Text(sticker_content));
        let question_field_index = base.stickers.len() - 1;

        Self {
            content: Some(Content {
                mode,
                base,
                question_field: QuestionField::Text(question_field_index),
                ..Default::default()
            }),
        }
    }

    fn mode(&self) -> Option<Mode> {
        self.content.as_ref().map(|c| c.mode.clone())
    }

    fn requires_choose_mode(&self) -> bool {
        self.content.is_none()
    }

    fn set_editor_state_step(&mut self, step: Step) {
        if let Some(content) = self.content.as_mut() {
            content.editor_state.step = step;
        }
    }
    fn set_editor_state_steps_completed(&mut self, steps_completed: HashSet<Step>) {
        if let Some(content) = self.content.as_mut() {
            content.editor_state.steps_completed = steps_completed;
        }
    }

    fn get_editor_state_step(&self) -> Option<Step> {
        self.content
            .as_ref()
            .map(|content| content.editor_state.step)
    }

    fn get_editor_state_steps_completed(&self) -> Option<HashSet<Step>> {
        self.content
            .as_ref()
            .map(|content| content.editor_state.steps_completed.clone())
    }

    fn set_theme(&mut self, theme_id: ThemeId) {
        if let Some(content) = self.content.as_mut() {
            content.base.theme = theme_id;
        }
    }

    fn get_theme(&self) -> Option<ThemeId> {
        self.content.as_ref().map(|content| content.base.theme)
    }
}

impl BodyConvert for ModuleData {}

impl TryFrom<Body> for ModuleData {
    type Error = &'static str;

    fn try_from(body: Body) -> Result<Self, Self::Error> {
        match body {
            Body::FindAnswer(data) => Ok(data),
            _ => Err("cannot convert body to Answer This!"),
        }
    }
}

/// The body for [`FindAnswer`](crate::domain::module::ModuleKind::FindAnswer) modules.
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct Content {
    /// The base content for all design modules
    pub base: BaseContent,

    /// The editor state
    pub editor_state: EditorState,

    /// The mode
    pub mode: Mode,

    /// Questions
    pub questions: Vec<Question>,

    /// Sticker index of the related question sticker
    pub question_field: QuestionField,

    /// play settings
    pub play_settings: PlaySettings,
}

impl Content {
    /// Convenience method to determine whether questions have configured correctly
    pub fn is_valid(&self) -> bool {
        !self.questions.is_empty()
            && self
                .questions
                .iter()
                .find(|question| !question.is_valid())
                .is_none()
    }
}

/// The type of field to be used for displaying question text.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum QuestionField {
    /// Index of the text sticker to be used as the question field.
    Text(usize),
    /// When the teacher hasn't added or selected a text sticker, a dynamic label will be added to
    /// display the question. The teacher can move this around.
    ///
    /// Note (Ty): We won't make use of the scale field right now, but at some point we should add
    /// the ability to scale the label text
    Dynamic(Option<Transform>),
}

impl QuestionField {
    /// Whether the current variant is `Dynamic`
    pub fn is_dynamic(&self) -> bool {
        matches!(self, Self::Dynamic(_))
    }

    /// Whether the current variant is `Text`
    pub fn is_text(&self) -> bool {
        matches!(self, Self::Text(_))
    }
}

impl Default for QuestionField {
    fn default() -> Self {
        QuestionField::Dynamic(None)
    }
}

/// Represents a single question
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct Question {
    /// Title of the question
    pub title: String,

    /// The question text
    pub question_text: String,

    /// Optional audio for the question
    pub question_audio: Option<Audio>,

    /// Optional audio for incorrect choices
    pub incorrect_audio: Option<Audio>,

    #[serde(default)]
    /// Optional audio for correct choices
    pub correct_audio: Option<Audio>,

    /// Traces
    pub traces: Vec<Trace>,
}

impl Question {
    /// Convenience method to determine whether a question has been configured correctly
    pub fn is_valid(&self) -> bool {
        !self.traces.is_empty()
    }
}

/// Editor state
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct EditorState {
    /// the current step
    pub step: Step,

    /// the completed steps
    pub steps_completed: HashSet<Step>,
}

#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
/// The mode
pub enum Mode {
    /// Family mode
    Family,
    /// Map mode
    Map,
    /// Multiple mode
    MultipleChoice,
    /// Scene mode
    Scene,
    /// Text mode
    Text,
    /// Find the differences mode
    Differences,
}

impl Default for Mode {
    fn default() -> Self {
        Self::MultipleChoice
    }
}

impl ModeExt for Mode {
    fn get_list() -> Vec<Self> {
        vec![
            Self::Scene,
            Self::MultipleChoice,
            Self::Text,
            Self::Differences,
            Self::Map,
            Self::Family,
        ]
    }

    fn as_str_id(&self) -> &'static str {
        match self {
            Self::Family => "family",
            Self::Map => "map",
            Self::MultipleChoice => "multiple-choice",
            Self::Scene => "scene",
            Self::Text => "text",
            Self::Differences => "differences",
        }
    }

    fn label(&self) -> &'static str {
        const STR_FAMILY_LABEL: &'static str = "Family Tree";
        const STR_MAP_LABEL: &'static str = "Where on the Map?";
        const STR_MULTIPLE_CHOICE_LABEL: &'static str = "Find the Answers";
        const STR_SCENE_LABEL: &'static str = "Spot It";
        const STR_TEXT_LABEL: &'static str = "Find the Text";
        const STR_DIFFERENCES_LABEL: &'static str = "Find the Differences";

        match self {
            Self::Family => STR_FAMILY_LABEL,
            Self::Map => STR_MAP_LABEL,
            Self::MultipleChoice => STR_MULTIPLE_CHOICE_LABEL,
            Self::Scene => STR_SCENE_LABEL,
            Self::Text => STR_TEXT_LABEL,
            Self::Differences => STR_DIFFERENCES_LABEL,
        }
    }
}

/// The Steps
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Step {
    /// Step 1
    One,
    /// Step 2
    Two,
    /// Step 3
    Three,
    /// Step 4
    Four,
    /// Step 5
    Five,
}

impl Default for Step {
    fn default() -> Self {
        Self::One
    }
}

impl StepExt for Step {
    fn next(&self) -> Option<Self> {
        match self {
            Self::One => Some(Self::Two),
            Self::Two => Some(Self::Three),
            Self::Three => Some(Self::Four),
            Self::Four => Some(Self::Five),
            Self::Five => None,
        }
    }

    fn as_number(&self) -> usize {
        match self {
            Self::One => 1,
            Self::Two => 2,
            Self::Three => 3,
            Self::Four => 4,
            Self::Five => 5,
        }
    }

    fn label(&self) -> &'static str {
        const STR_BACKGROUND: &'static str = "Design";
        const STR_CONTENT: &'static str = "Content";
        const STR_INTERACTION: &'static str = "Interaction";
        const STR_SETTINGS: &'static str = "Settings";
        const STR_PREVIEW: &'static str = "Preview";
        match self {
            Self::One => STR_BACKGROUND,
            Self::Two => STR_CONTENT,
            Self::Three => STR_INTERACTION,
            Self::Four => STR_SETTINGS,
            Self::Five => STR_PREVIEW,
        }
    }

    fn get_list() -> Vec<Self> {
        vec![Self::One, Self::Two, Self::Three, Self::Four, Self::Five]
    }
    fn get_preview() -> Self {
        Self::Five
    }
}