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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dims::{Dim, Padding2d, Rect, SimplePadding2d, SimpleRect};
use crate::graphics::{DrawContext, Graphic};
use crate::selectable::{Selectable, SelectionState, SelectionStateV2};

use crate::platform::opengl;
use opengl::context::bindings::{FALSE, FLOAT, TRIANGLES, UNSIGNED_BYTE};
use opengl::{DualVertexBufferIndexed, OpenGlRenderPlatform, Texture};

#[rustfmt::skip]
static SLICED_INDICES: [u8; 18 * 3] = [
    0, 4, 11,
    4, 12, 11,
    4, 5, 12,
    5, 13, 12,
    5, 1, 13,
    1, 6, 13,
    11, 12, 10,
    12, 15, 10,
    12, 13, 15,
    13, 14, 15,
    13, 6, 14,
    6, 7, 14,
    10, 15, 3,
    15, 9, 3,
    15, 14, 9,
    14, 8, 9,
    14, 7, 8,
    7, 2, 8,
];

/// A common graphic used for user interfaces, a sliced image is defined by
/// fixed-sized corners and an inner area which stretches to fill the
/// graphic area.
///
/// See the [Wikipedia article](https://en.wikipedia.org/wiki/9-slice_scaling)
/// on 9-slice scaling for more information.
pub struct SlicedImage {
    rect: SimpleRect,
    padding: SimplePadding2d,
    texture: Texture,
    buffers: DualVertexBufferIndexed<f32>,
}

impl Default for SlicedImage {
    fn default() -> Self {
        Self {
            rect: SimpleRect::default(),
            padding: SimplePadding2d::default(),
            texture: Texture::default(),
            buffers: DualVertexBufferIndexed::new(true, false, false),
        }
    }
}

impl SlicedImage {
    /// Create a new SlicedImage.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the texture used by this graphic.  The given padding describes
    /// the sliced area.
    pub fn set_image<P>(&mut self, texture: Texture, padding: &P)
    where
        P: Padding2d,
    {
        self.texture = texture;
        self.padding = padding.into();
        self.update_image();
    }

    fn update_image(&mut self) {
        let mut uvs = [0f32; 32];
        let Self {
            buffers,
            texture,
            padding,
            ..
        } = self;
        buffers.set_data_1(|_gl| {
            texture.size().and_then(|(tex_width, tex_height)| {
                let uvs = &mut uvs;
                texture.transform_uvs(move || {
                    let left = padding.left() / tex_width;
                    let right = 1.0 - (padding.right() / tex_width);
                    let bottom = padding.bottom() / tex_height;
                    let top = 1.0 - (padding.top() / tex_height);
                    #[rustfmt::skip]
                    let data = [
                        0.0, 0.0,
                        1.0, 0.0,
                        1.0, 1.0,
                        0.0, 1.0,
                        left, 0.0,
                        right, 0.0,
                        1.0, bottom,
                        1.0, top,
                        right, 1.0,
                        left, 1.0,
                        0.0, top,
                        0.0, bottom,
                        left, bottom,
                        right, bottom,
                        right, top,
                        left, top,
                    ];
                    *uvs = data;
                    &mut uvs[..]
                })
            })
        });
    }

    fn update(&mut self) {
        let mut inner = SimpleRect::default();
        inner.set_fill(&self.rect, &self.padding);
        let rect = &self.rect;
        let mut vertices = [0f32; 32];
        self.buffers.set_data_0(|_gl| {
            #[rustfmt::skip]
            let data = [
                // outer corners
                rect.left(), rect.bottom(),
                rect.right(), rect.bottom(),
                rect.right(), rect.top(),
                rect.left(), rect.top(),
                // bottom edge
                inner.left(), rect.bottom(),
                inner.right(), rect.bottom(),
                // right edge
                rect.right(), inner.bottom(),
                rect.right(), inner.top(),
                // top edge
                inner.right(), rect.top(),
                inner.left(), rect.top(),
                // left edge
                rect.left(), inner.top(),
                rect.left(), inner.bottom(),
                // inner corners
                inner.left(), inner.bottom(),
                inner.right(), inner.bottom(),
                inner.right(), inner.top(),
                inner.left(), inner.top(),
            ];
            vertices = data;
            &vertices[..]
        });
    }
}

impl Rect for SlicedImage {
    fn x(&self) -> Dim {
        self.rect.x()
    }
    fn y(&self) -> Dim {
        self.rect.y()
    }

    fn x_mut<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut Dim) -> R,
    {
        let res = self.rect.x_mut(f);
        self.update();
        res
    }

    fn y_mut<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut Dim) -> R,
    {
        let res = self.rect.y_mut(f);
        self.update();
        res
    }
}

impl Graphic<OpenGlRenderPlatform> for SlicedImage {
    fn draw(&mut self, ctx: &mut DrawContext<OpenGlRenderPlatform>) {
        ctx.push(|ctx| {
            ctx.params().standard_mode();
            ctx.params().use_texture(self.texture.clone());
            if let Some(ready) = self.buffers.check_ready(ctx) {
                let gl = ready.gl;
                ready.bind_0();
                unsafe {
                    gl.VertexAttribPointer(
                        0,
                        2,
                        FLOAT,
                        FALSE,
                        0,
                        std::ptr::null(),
                    );
                }
                ready.bind_1();
                unsafe {
                    gl.VertexAttribPointer(
                        1,
                        2,
                        FLOAT,
                        FALSE,
                        0,
                        std::ptr::null(),
                    );
                }
                ready.bind_indices();
                unsafe {
                    gl.DrawElements(
                        TRIANGLES,
                        SLICED_INDICES.len() as _,
                        UNSIGNED_BYTE,
                        std::ptr::null(),
                    );
                }
            } else {
                self.update();
                self.texture.bind(ctx.render_ctx_mut());
                self.update_image();
                self.buffers.set_indices(|_gl| &SLICED_INDICES[..]);
            }
        });
    }
}

/// A version of SliceImage which assumes multiple images are layed out in
/// the same texture corosponding to different "selection states".
///
/// See the selectable module for more information on selection states.
#[derive(Default)]
pub struct SelectableSlicedImage {
    inner: SlicedImage,
    states: std::borrow::Cow<'static, [SelectionState]>,
    current_state: SelectionState,
}

impl Rect for SelectableSlicedImage {
    fn x(&self) -> Dim {
        self.inner.x()
    }
    fn y(&self) -> Dim {
        self.inner.y()
    }

    fn x_mut<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut Dim) -> R,
    {
        self.inner.x_mut(f)
    }

    fn y_mut<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut Dim) -> R,
    {
        self.inner.y_mut(f)
    }
}

impl Selectable for SelectableSlicedImage {
    fn selection_changed(&mut self, state: SelectionState) {
        self.current_state = state;
    }
}

impl SelectableSlicedImage {
    /// Create a new SelectableSlicedImage
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the texture used by this graphic.
    ///
    /// The given slice of states defines the number and varients of the
    /// sub-images in the texture.  Note: the states must contain
    /// `SelectionState::normal()` as a possible fallback for other,
    /// unincluded states.
    ///
    /// The given padding describes the sliced area of each sub-image.
    pub fn set_image<P, S>(&mut self, texture: Texture, padding: &P, states: S)
    where
        P: Padding2d,
        S: Into<std::borrow::Cow<'static, [SelectionState]>>,
    {
        self.inner.texture = texture;
        self.inner.padding = padding.into();
        let states = states.into();
        assert!(
            states.contains(&SelectionState::normal()),
            "SelectableSlicedImage must have a variant for Normal",
        );
        self.states = states;
        self.update_image();
    }

    fn update_image(&mut self) {
        const NUM_STATES: usize = 5;
        const COORDS_PER_STATE: usize = 32;
        let all_states: [_; NUM_STATES] = [
            SelectionState::normal(),
            SelectionState::hover(),
            SelectionState::focus(),
            SelectionState::pressed(),
            SelectionState::active(),
        ];
        let mut uvs = [0f32; NUM_STATES * COORDS_PER_STATE];
        let states = &self.states;
        let SlicedImage {
            buffers,
            texture,
            padding,
            ..
        } = &mut self.inner;
        buffers.set_data_1(|_gl| {
            texture.size().and_then(|(tex_width, tex_height)| {
                let uvs = &mut uvs;
                texture.transform_uvs(move || {
                    let state_frac = 1.0 / (states.len() as f32);
                    let left = padding.left() / tex_width;
                    let bottom = padding.bottom() / tex_height;
                    let top = 1.0 - (padding.top() / tex_height);
                    for (i, state) in all_states.iter().enumerate() {
                        let data_start = i * COORDS_PER_STATE;
                        let data_end = data_start + COORDS_PER_STATE;
                        let mut find_state = *state;
                        let state_index: usize = loop {
                            if let Some(idx) = states
                                .iter()
                                .position(|item| *item == find_state)
                            {
                                break idx;
                            }
                            find_state = find_state.reduce();
                        };
                        let offset = (state_index as f32) * state_frac;
                        let left = offset + left;
                        let end = offset + state_frac;
                        let right = end - (padding.right() / tex_width);
                        #[rustfmt::skip]
                        uvs[data_start..data_end].copy_from_slice(&[
                            offset, 0.0,
                            end, 0.0,
                            end, 1.0,
                            offset, 1.0,
                            left, 0.0,
                            right, 0.0,
                            end, bottom,
                            end, top,
                            right, 1.0,
                            left, 1.0,
                            offset, top,
                            offset, bottom,
                            left, bottom,
                            right, bottom,
                            right, top,
                            left, top,
                        ]);
                    }
                    &mut uvs[..]
                })
            })
        });
    }
}

impl Graphic<OpenGlRenderPlatform> for SelectableSlicedImage {
    fn draw(&mut self, ctx: &mut DrawContext<OpenGlRenderPlatform>) {
        const UV_STATE_SIZE: usize = 32 * std::mem::size_of::<f32>();
        let uv_offset = match self.current_state.v2() {
            SelectionStateV2::Normal => 0,
            SelectionStateV2::Hover => UV_STATE_SIZE,
            SelectionStateV2::Focus => UV_STATE_SIZE * 2,
            SelectionStateV2::Pressed => UV_STATE_SIZE * 3,
            SelectionStateV2::Active => UV_STATE_SIZE * 4,
        };
        ctx.push(|ctx| {
            ctx.params().standard_mode();
            ctx.params().use_texture(self.inner.texture.clone());
            if let Some(ready) = self.inner.buffers.check_ready(ctx) {
                let gl = ready.gl;
                ready.bind_0();
                unsafe {
                    gl.VertexAttribPointer(
                        0,
                        2,
                        FLOAT,
                        FALSE,
                        0,
                        std::ptr::null(),
                    );
                }
                ready.bind_1();
                unsafe {
                    gl.VertexAttribPointer(
                        1,
                        2,
                        FLOAT,
                        FALSE,
                        0,
                        uv_offset as _,
                    );
                }
                ready.bind_indices();
                unsafe {
                    gl.DrawElements(
                        TRIANGLES,
                        SLICED_INDICES.len() as _,
                        UNSIGNED_BYTE,
                        std::ptr::null(),
                    );
                }
            } else {
                self.inner.update();
                self.inner.texture.bind(ctx.render_ctx_mut());
                self.update_image();
                self.inner.buffers.set_indices(|_gl| &SLICED_INDICES[..]);
            }
        });
    }
}