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
use wasm4_sys::{text, SCREEN_SIZE};

const CHARS_PER_LINE: usize = 20;
const CHAR_WIDTH: usize = 8;
const CHAR_HEIGHT: usize = 8;

/// Horizontal alignment.
#[derive(Clone, Copy)]
pub enum TextHorizontalAlignment {
    /// Align left.
    Left,
    /// Align center.
    Center,
    /// Align right.
    Right,
}

/// Vertical alignment.
#[derive(Clone, Copy)]
pub enum TextVerticalAligment {
    /// Align top.
    Top,
    /// Align middle.
    Middle,
    /// Align bottom.
    Bottom,
}

impl TextHorizontalAlignment {
    fn get_padding_x<T: AsRef<[u8]>>(&self, text: T) -> i32 {
        let len = text.as_ref().len() as i32;

        match *self {
            Self::Left => 0,
            Self::Center => {
                (((CHARS_PER_LINE as f32 - len as f32) / 2.0) * CHAR_WIDTH as f32) as i32
            }
            Self::Right => (CHARS_PER_LINE as i32 - len) * CHAR_WIDTH as i32,
        }
    }
}

impl TextVerticalAligment {
    fn get_padding_y(&self, line_count: usize, line_separation: i32) -> i32 {
        match *self {
            Self::Top => 0,
            Self::Middle => {
                ((SCREEN_SIZE as f32) / 2.0 - (CHAR_HEIGHT as f32 * line_count as f32) / 2.0) as i32
            }
            Self::Bottom => {
                SCREEN_SIZE as i32
                    - (CHAR_HEIGHT as i32 * line_count as i32)
                    - line_separation * (line_count - 1) as i32
            }
        }
    }
}

/// A text drawing helper.
///
/// # Example
///
/// ```no_run
/// use wasm4_sx::*;
///
/// #[no_mangle]
/// fn update() {
///     Text::new("Top left")
///         .with_horizontal_alignment(TextHorizontalAlignment::Left)
///         .with_vertical_alignment(TextVerticalAligment::Top)
///         .draw();
///
///     Text::new("Middle center")
///         .with_horizontal_alignment(TextHorizontalAlignment::Center)
///         .with_vertical_alignment(TextVerticalAligment::Middle)
///         .draw();
///
///     Text::new("Bottom right")
///         .with_horizontal_alignment(TextHorizontalAlignment::Right)
///         .with_vertical_alignment(TextVerticalAligment::Bottom)
///         .draw();
/// }
/// ```
#[must_use]
pub struct Text<T: AsRef<[u8]>> {
    value: T,
    x: i32,
    y: i32,
    horizontal_alignment: Option<TextHorizontalAlignment>,
    vertical_alignment: Option<TextVerticalAligment>,
    padding_x: i32,
    padding_y: i32,
    line_separation: i32,
}

impl<T: AsRef<[u8]>> Text<T> {
    /// Build a new text drawing helper.
    pub const fn new(value: T) -> Self {
        Self {
            value,
            horizontal_alignment: None,
            vertical_alignment: None,
            x: 0,
            y: 0,
            padding_x: 0,
            padding_y: 0,
            line_separation: 0,
        }
    }

    /// Set the X coordinate.
    pub fn with_x(mut self, x: i32) -> Self {
        self.x = x;
        self
    }

    /// Set the Y coordinate.
    pub fn with_y(mut self, y: i32) -> Self {
        self.y = y;
        self
    }

    /// Set the X padding.
    pub fn with_padding_x(mut self, padding_x: i32) -> Self {
        self.padding_x = padding_x;
        self
    }

    /// Set the Y padding.
    pub fn with_padding_y(mut self, padding_y: i32) -> Self {
        self.padding_y = padding_y;
        self
    }

    /// Set the horizontal alignment.
    pub fn with_horizontal_alignment(mut self, alignment: TextHorizontalAlignment) -> Self {
        self.horizontal_alignment = Some(alignment);
        self
    }

    /// Set the vertical alignment.
    pub fn with_vertical_alignment(mut self, alignment: TextVerticalAligment) -> Self {
        self.vertical_alignment = Some(alignment);
        self
    }

    /// Set the line separation value.
    pub fn with_line_separation(mut self, line_separation: i32) -> Self {
        self.line_separation = line_separation;
        self
    }

    /// Draw the text.
    pub fn draw(self) {
        let line_count = self.value.as_ref().split(|&v| v == b'\n').count();
        for (line_index, line) in self.value.as_ref().split(|&v| v == b'\n').enumerate() {
            self.draw_line(line_index, line_count, line);
        }
    }

    fn horizontal_padding<U: AsRef<[u8]>>(&self, text: U) -> i32 {
        match self.horizontal_alignment.as_ref() {
            Some(s) => s.get_padding_x(text) + self.padding_x,
            None => self.padding_x,
        }
    }

    fn vertical_padding(&self, line_count: usize) -> i32 {
        match self.vertical_alignment.as_ref() {
            Some(s) => s.get_padding_y(line_count, self.line_separation) + self.padding_y,
            None => self.padding_y,
        }
    }

    fn compute_draw_coordinates<U: AsRef<[u8]>>(
        &self,
        line_index: usize,
        line_count: usize,
        line: U,
    ) -> (i32, i32) {
        let x = self.horizontal_padding(line.as_ref()) + self.x;
        let y = self.vertical_padding(line_count)
            + self.y
            + (CHAR_WIDTH as i32 + self.line_separation) * line_index as i32;

        (x, y)
    }

    fn draw_line<U: AsRef<[u8]>>(&self, line_index: usize, line_count: usize, line: U) {
        let (x, y) = self.compute_draw_coordinates(line_index, line_count, line.as_ref());
        text(line.as_ref(), x, y);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn horizontal_aligment() {
        let (x, y) = Text::new("Hello")
            .with_horizontal_alignment(TextHorizontalAlignment::Left)
            .compute_draw_coordinates(0, 1, "Hello");

        assert_eq!((x, y), (0, 0));

        let (x, y) = Text::new("Hello")
            .with_horizontal_alignment(TextHorizontalAlignment::Center)
            .compute_draw_coordinates(0, 1, "Hello");

        // (160 - 5 * 8) / 2 = 60
        assert_eq!((x, y), (60, 0));

        let (x, y) = Text::new("Hello")
            .with_horizontal_alignment(TextHorizontalAlignment::Center)
            .with_padding_x(1)
            .compute_draw_coordinates(0, 1, "Hello");

        // (160 - 5 * 8) / 2 + 1 = 61
        assert_eq!((x, y), (61, 0));

        let (x, y) = Text::new("Hello")
            .with_horizontal_alignment(TextHorizontalAlignment::Center)
            .with_padding_x(-1)
            .compute_draw_coordinates(0, 1, "Hello");

        // (160 - 5 * 8) / 2 - 1 = 59
        assert_eq!((x, y), (59, 0));

        let (x, y) = Text::new("Hello")
            .with_horizontal_alignment(TextHorizontalAlignment::Right)
            .compute_draw_coordinates(0, 1, "Hello");

        // (160 - 5 * 8) = 120
        assert_eq!((x, y), (120, 0));

        let (x, y) = Text::new("Hello")
            .with_horizontal_alignment(TextHorizontalAlignment::Right)
            .with_padding_x(1)
            .compute_draw_coordinates(0, 1, "Hello");

        // (160 - 5 * 8) + 1 = 121
        assert_eq!((x, y), (121, 0));
    }

    #[test]
    fn vertical_alignment() {
        let (x, y) = Text::new("Hello")
            .with_vertical_alignment(TextVerticalAligment::Top)
            .compute_draw_coordinates(0, 1, "Hello");

        assert_eq!((x, y), (0, 0));

        let (x, y) = Text::new("Hello")
            .with_vertical_alignment(TextVerticalAligment::Middle)
            .compute_draw_coordinates(0, 1, "Hello");

        // (160 - 8) / 2 = 76
        assert_eq!((x, y), (0, 76));

        let (x, y) = Text::new("Hello")
            .with_vertical_alignment(TextVerticalAligment::Middle)
            .with_padding_y(1)
            .compute_draw_coordinates(0, 1, "Hello");

        // (160 - 8) / 2 + 1 = 77
        assert_eq!((x, y), (0, 77));

        let (x, y) = Text::new("Hello")
            .with_vertical_alignment(TextVerticalAligment::Middle)
            .with_padding_y(-1)
            .compute_draw_coordinates(0, 1, "Hello");

        // (160 - 8) / 2 - 1 = 75
        assert_eq!((x, y), (0, 75));

        let (x, y) = Text::new("Hello")
            .with_vertical_alignment(TextVerticalAligment::Bottom)
            .compute_draw_coordinates(0, 1, "Hello");

        // 160 - 8 = 152
        assert_eq!((x, y), (0, 152));

        let (x, y) = Text::new("Hello")
            .with_vertical_alignment(TextVerticalAligment::Bottom)
            .with_padding_y(1)
            .compute_draw_coordinates(0, 1, "Hello");

        // 160 - 8 + 1 = 153
        assert_eq!((x, y), (0, 153));

        let (x, y) = Text::new("Hello")
            .with_vertical_alignment(TextVerticalAligment::Bottom)
            .with_padding_y(-1)
            .compute_draw_coordinates(0, 1, "Hello");

        // 160 - 8 - 1 = 151
        assert_eq!((x, y), (0, 151));
    }
}