summaryrefslogtreecommitdiff
path: root/src/dialogbox.rs
blob: 4bdce0466d9802297e5405f52decd3631eb1675d (plain)
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
use ggez::conf::Conf;
use ggez::graphics::{
    self, DrawMode, DrawParam, Font, Mesh, MeshBuilder, Rect, Scale, Text, TextFragment,
};
use ggez::nalgebra::Point2;
use ggez::{filesystem, Context, GameResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::constants;
use crate::npc::Character;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Dialog {
    text: String,
    responses: Vec<(usize, String)>,
}

#[derive(Clone, Debug, Default)]
pub struct DialogTree {
    dialogs: HashMap<usize, Dialog>,
}

impl DialogTree {
    pub fn new(context: &mut Context, character: Character) -> DialogTree {
        DialogTree {
            dialogs: serde_json::from_reader(
                filesystem::open(context, "/dialogtrees/".to_string() + character.to_str())
                    .unwrap(),
            )
            .unwrap(),
        }
    }
}

#[derive(Clone)]
pub struct DialogBox {
    dialogtree: Option<DialogTree>,
    font: Font,
    text: Option<Text>,
    mesh: Mesh,
    conf: Conf,
}

impl DialogBox {
    pub fn new(context: &mut Context) -> DialogBox {
        let conf = Conf::new();

        DialogBox {
            dialogtree: None,
            font: Font::new(context, "/fonts/SONORM__.ttf").unwrap(),
            text: None,
            mesh: MeshBuilder::new()
                .rectangle(
                    DrawMode::fill(),
                    Rect::new(
                        conf.window_mode.width * 0.10,
                        2.5 * conf.window_mode.height / 4.0,
                        conf.window_mode.width * 0.80,
                        conf.window_mode.height / 4.0,
                    ),
                    constants::PURPLE,
                )
                .build(context)
                .unwrap(),
            conf,
        }
    }

    pub fn update(&mut self) {
        if let Some(dialogtree) = &self.dialogtree {
            if self.text.is_none() {
                self.text = Some(Text::new(
                    TextFragment::new(dialogtree.dialogs.get(&0).unwrap().text.as_str())
                        .font(self.font)
                        .scale(Scale::uniform(40.0)),
                ));
            }
        } else {
            self.text = None;
        }
    }

    pub fn draw(&self, context: &mut Context) -> GameResult {
        if let Some(text) = &self.text {
            graphics::draw(context, &self.mesh, DrawParam::default())?;
            graphics::draw(
                context,
                text,
                DrawParam::default().dest(Point2::new(
                    self.conf.window_mode.width * 0.11,
                    2.6 * self.conf.window_mode.height / 4.0,
                )),
            )?;
        }

        Ok(())
    }

    pub fn give_dialogtree(&mut self, dialogtree: Option<DialogTree>) {
        self.dialogtree = dialogtree;
    }
}