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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! The OpenGL window is (-1.0, -1.0) in the bottom left to (1.0, 1.0) in the top right.

// EXTERNAL
use glium::{
    glutin::{
        dpi::{LogicalSize, PhysicalPosition},
        event::{
            ElementState as GLElementState, Event as GLEvent, MouseButton as GLMouseButton,
            VirtualKeyCode as GLVirtualKeyCode, WindowEvent as GLWindowEvent,
        },
        event_loop::{ControlFlow, EventLoop},
        platform::desktop::EventLoopExtDesktop,
        window::WindowBuilder,
        ContextBuilder,
    },
    implement_vertex,
    index::{NoIndices, PrimitiveType},
    program::ProgramCreationInput,
    texture::{CompressedTexture2d, RawImage2d},
    uniform, Blend, Display, DrawParameters, Frame, IndexBuffer, PolygonMode, Program, Smooth,
    Surface, VertexBuffer,
};
use std::f64::consts::PI;

// RUSTY
use crate::color::Color;
use crate::event::*;
use rusty_core::glm::{self, Vec2};
use rusty_core::prelude::Transform;

// EXPORT
pub mod color;
pub mod event;
pub mod util;
pub mod prelude {
    pub use crate::color::*;
    pub use crate::event::*;
    pub use crate::util::*;
    pub use crate::{Sprite, Window};
}

pub enum ShapeStyle {
    Fill,
    Line,
}

pub enum ThingToDraw<'a> {
    Img(
        &'a VertexBuffer<ImgVertex>,
        &'a IndexBuffer<u16>,
        &'a CompressedTexture2d,
    ),
    Shape(&'a VertexBuffer<ShapeVertex>, &'a NoIndices, PolygonMode),
    ShaderShape(&'a VertexBuffer<ShaderShapeVertex>, &'a NoIndices, &'a f32),
}

pub trait Drawable {
    fn get_shader_items(&self) -> ThingToDraw;
}

pub struct Sprite {
    pub transform: Transform,
    drawable: Box<dyn Drawable>,
}

impl Sprite {
    pub fn smooth_circle(
        window: &Window,
        pos: Vec2,
        direction: f32,
        scale: f32,
        radius: f32,
        color: Color,
    ) -> Self {
        Self::with_drawable_at(
            ShaderShape::new_circle(window, radius, color),
            Transform::at(pos, direction, scale),
        )
    }
    pub fn new_rectangle(
        window: &Window,
        pos: Vec2,
        direction: f32,
        scale: f32,
        width: f32,
        height: f32,
        color: Color,
        shape_style: ShapeStyle,
    ) -> Self {
        Self::with_drawable_at(
            Shape::new_rectangle(&window, width, height, color, shape_style),
            Transform::at(pos, direction, scale),
        )
    }
    pub fn new_image(
        window: &Window,
        pos: Vec2,
        direction: f32,
        scale: f32,
        tint: Option<Color>,
        filename: &str,
    ) -> Self {
        Self::with_drawable_at(
            Img::new(window, tint, filename),
            Transform::at(pos, direction, scale),
        )
    }
    pub fn new_circle(
        window: &Window,
        pos: Vec2,
        direction: f32,
        scale: f32,
        radius: f32,
        color: Color,
        shape_style: ShapeStyle,
    ) -> Self {
        Self::with_drawable_at(
            Shape::new_circle(&window, radius, color, shape_style),
            Transform::at(pos, direction, scale),
        )
    }
    pub fn with_drawable<T: 'static + Drawable>(drawable: T) -> Self {
        Self {
            transform: Transform::new(),
            drawable: Box::new(drawable),
        }
    }
    pub fn with_drawable_at<T: 'static + Drawable>(drawable: T, transform: Transform) -> Self {
        Self {
            transform,
            drawable: Box::new(drawable),
        }
    }
    /// You must call a `Window`'s `.drawstart()` before calling this method.  `draw()` will draw your
    /// image to the current off-screen framebuffer.  After the first time a given image value is
    /// drawn it stays on the GPU and during subsequent calls it only sends updated
    /// position/rotation, which is super efficient, so don't destroy and recreate images every
    /// frame! Draw calls draw to the framebuffer in the order that they occur, so the last image
    /// is on top.
    pub fn draw(&mut self, window: &mut Window) {
        if let Some(ref mut target) = window.target {
            let affine = self.transform.get_affine();

            match self.drawable.get_shader_items() {
                ThingToDraw::Img(vertex_buffer, index_buffer, texture) => {
                    let uniforms = uniform! {
                        matrix: affine,
                        tex: texture,
                    };

                    let draw_parameters = DrawParameters {
                        blend: Blend::alpha_blending(),
                        ..Default::default()
                    };

                    target
                        .draw(
                            vertex_buffer,
                            index_buffer,
                            &window.img_program,
                            &uniforms,
                            &draw_parameters,
                        )
                        .unwrap();
                }
                ThingToDraw::Shape(vertex_buffer, indices, polygon_mode) => {
                    let uniforms = uniform! {
                        matrix: affine,
                    };

                    let draw_parameters = DrawParameters {
                        blend: Blend::alpha_blending(),
                        smooth: Some(Smooth::Nicest),
                        polygon_mode,
                        ..Default::default()
                    };
                    target
                        .draw(
                            vertex_buffer,
                            indices,
                            &window.shape_program,
                            &uniforms,
                            &draw_parameters,
                        )
                        .unwrap();
                }
                ThingToDraw::ShaderShape(vertex_buffer, indices, local_scale) => {
                    let uniforms = uniform! {
                        matrix: affine,
                        local_scale: *local_scale,
                    };

                    let draw_parameters = DrawParameters {
                        blend: Blend::alpha_blending(),
                        smooth: Some(Smooth::Nicest),
                        ..Default::default()
                    };
                    target
                        .draw(
                            vertex_buffer,
                            indices,
                            &window.shader_shape_program,
                            &uniforms,
                            &draw_parameters,
                        )
                        .unwrap();
                }
            }
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub struct ShaderShapeVertex {
    position: [f32; 2],
    color: [f32; 3],
}
implement_vertex!(ShaderShapeVertex, position, color);

#[derive(Debug)]
pub struct ShaderShape {
    vertex_buffer: VertexBuffer<ShaderShapeVertex>,
    indices: NoIndices,
    local_scale: f32,
}

impl Drawable for ShaderShape {
    fn get_shader_items(&self) -> ThingToDraw {
        ThingToDraw::ShaderShape(&self.vertex_buffer, &self.indices, &self.local_scale)
    }
}

impl ShaderShape {
    pub fn new_circle(window: &Window, radius: f32, color: Color) -> Self {
        let corner_a = Vec2::new(-6. / (2. * 3.0_f32.sqrt()), -1.0).scale(radius);
        let corner_b = Vec2::new(-0., 2.).scale(radius);
        let corner_c = Vec2::new(6. / (2. * 3.0_f32.sqrt()), -1.0).scale(radius);
        let v = vec![
            ShaderShapeVertex {
                position: corner_a.into(),
                color: color.into(),
            },
            ShaderShapeVertex {
                position: corner_b.into(),
                color: color.into(),
            },
            ShaderShapeVertex {
                position: corner_c.into(),
                color: color.into(),
            },
        ];
        let vertex_buffer = VertexBuffer::new(&window.display, &v).unwrap();
        Self {
            vertex_buffer,
            indices: NoIndices(PrimitiveType::TriangleStrip),
            local_scale: radius,
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub struct ShapeVertex {
    position: [f32; 2],
    color: [f32; 3],
}
implement_vertex!(ShapeVertex, position, color);

/// A `Shape` can be drawn to a `Window` using its `draw_shape()` method. Use the provided `new_*`
/// methods to make a `Shape`.
#[derive(Debug)]
pub struct Shape {
    vertex_buffer: VertexBuffer<ShapeVertex>,
    indices: NoIndices,
    polygon_mode: PolygonMode,
}

impl Drawable for Shape {
    fn get_shader_items(&self) -> ThingToDraw {
        ThingToDraw::Shape(&self.vertex_buffer, &self.indices, self.polygon_mode)
    }
}

impl Shape {
    // todo: document
    pub fn new_rectangle(
        window: &Window,
        width: f32,
        height: f32,
        color: Color,
        shape_style: ShapeStyle,
    ) -> Self {
        let vertex_buffer = VertexBuffer::new(
            &window.display,
            &vec![
                ShapeVertex {
                    position: (glm::vec2(width * 0.5, height * 0.5)).into(),
                    color: color.into(),
                },
                ShapeVertex {
                    position: (glm::vec2(width * 0.5, -height * 0.5)).into(),
                    color: color.into(),
                },
                ShapeVertex {
                    position: (glm::vec2(-width * 0.5, -height * 0.5)).into(),
                    color: color.into(),
                },
                ShapeVertex {
                    position: (glm::vec2(-width * 0.5, height * 0.5)).into(),
                    color: color.into(),
                },
            ],
        )
        .unwrap();
        let (primitive_type, polygon_mode) = match shape_style {
            ShapeStyle::Fill => (PrimitiveType::TriangleFan, PolygonMode::Fill),
            ShapeStyle::Line => (PrimitiveType::LineLoop, PolygonMode::Line),
        };
        Self {
            vertex_buffer,
            indices: NoIndices(primitive_type),
            polygon_mode,
        }
    }

    /// Create a solid circle with a stripe that always faces `direction`.
    pub fn new_circle(window: &Window, radius: f32, color: Color, shape_style: ShapeStyle) -> Self {
        let num_vertices = 63;
        let mut v = Vec::<ShapeVertex>::with_capacity(num_vertices + 1);
        for x in 0..=num_vertices {
            let inner: f64 = 2.0 * PI / num_vertices as f64 * x as f64;
            v.push(ShapeVertex {
                position: [inner.cos() as f32 * radius, inner.sin() as f32 * radius],
                color: color.into(),
            });
        }
        let vertex_buffer = VertexBuffer::new(&window.display, &v).unwrap();
        let (primitive_type, polygon_mode) = match shape_style {
            ShapeStyle::Fill => (PrimitiveType::TriangleFan, PolygonMode::Fill),
            ShapeStyle::Line => (PrimitiveType::LineLoop, PolygonMode::Line),
        };
        Self {
            vertex_buffer,
            indices: NoIndices(primitive_type),
            polygon_mode,
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub struct ImgVertex {
    position: [f32; 2],
    tex_coords: [f32; 2],
    tint: [f32; 3],
    should_tint: u8,
}

implement_vertex!(ImgVertex, position, tex_coords, tint, should_tint);

/// An image that can be drawn using the `Window.draw()` method.  Currently only PNG format is
/// supported.
///
/// If you are looking at an image in Photoshop, the "right" direction is the "front" of the
/// image.  `direction` is the angle in radians that the image will be rotated.
///
/// If you want your image to have transparency without getting white borders, export as a PNG-8
/// with Transparency checked, and Matte set to None.  See `media/png-settings-screenshot.png` in
/// the repository for a screenshot of the Photoshop "Export > Save for Web" settings that are known
/// to work.  Or just exporting as a 24-bit PNG might work.
#[derive(Debug)]
pub struct Img {
    vertex_buffer: VertexBuffer<ImgVertex>,
    index_buffer: IndexBuffer<u16>,
    texture: CompressedTexture2d,
}

impl Drawable for Img {
    fn get_shader_items(&self) -> ThingToDraw {
        ThingToDraw::Img(&self.vertex_buffer, &self.index_buffer, &self.texture)
    }
}

impl Img {
    /// Create a new image.  `filename` is relative to the root of the project you are running from.
    /// For example, if you created a `media` subdirectory in the root of your project and then put
    /// `soldier.png` in it, then your filename would be `media/soldier.png`.
    pub fn new(window: &Window, tint: Option<Color>, filename: &str) -> Self {
        let file = std::fs::File::open(filename).unwrap();
        let reader = std::io::BufReader::new(file);
        let image = image::load(reader, image::PNG).unwrap().to_rgba();
        let image_dimensions = image.dimensions();
        let image = RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
        let texture = CompressedTexture2d::new(&window.display, image).unwrap();

        let should_tint = if tint.is_some() { 1 } else { 0 };
        let tint = tint.unwrap_or_else(|| Color::new(1.0, 1.0, 1.0));

        // Size the image relative to the window - calculations assume a square window
        // Assuming 16 game units fit in the (1.0, 1.0) to (-1.0, -1.0) viewspace
        // So: 8 game units per OpenGL unit, a window is 2 OpenGL units high and wide
        let pixels_per_game_unit = window.dimension as f32 * 0.0625;
        let scale_x = image_dimensions.0 as f32 / pixels_per_game_unit / 16.;
        let scale_y = image_dimensions.1 as f32 / pixels_per_game_unit / 16.;

        let vertex_buffer = VertexBuffer::new(
            &window.display,
            &[
                ImgVertex {
                    position: [-scale_x, -scale_y],
                    tex_coords: [0.0, 0.0],
                    tint: tint.into(),
                    should_tint,
                },
                ImgVertex {
                    position: [-scale_x, scale_y],
                    tex_coords: [0.0, 1.0],
                    tint: tint.into(),
                    should_tint,
                },
                ImgVertex {
                    position: [scale_x, scale_y],
                    tex_coords: [1.0, 1.0],
                    tint: tint.into(),
                    should_tint,
                },
                ImgVertex {
                    position: [scale_x, -scale_y],
                    tex_coords: [1.0, 0.0],
                    tint: tint.into(),
                    should_tint,
                },
            ],
        )
        .unwrap();
        let index_buffer = IndexBuffer::new(
            &window.display,
            PrimitiveType::TriangleStrip,
            &[1 as u16, 2, 0, 3],
        )
        .unwrap();
        Self {
            vertex_buffer,
            index_buffer,
            texture,
        }
    }
}

/// An OpenGL window for displaying graphics. Also the object through which you'll receive input
/// events (mouse, keyboard, etc.)
pub struct Window {
    event_loop: EventLoop<()>,
    display: Display,
    shape_program: Program,
    shader_shape_program: Program,
    img_program: Program,
    screen_to_opengl: Box<dyn Fn(PhysicalPosition<f64>) -> Vec2>,
    dimension: u32,
    target: Option<Frame>,
}

impl Window {
    /// By default, this will be a square window with a dimension of `1024` logical pixels.  You can
    /// override the dimension by providing a value for override_dimension, for example: `Some(1200)`.
    ///
    /// `window_title` is for the OS to use on the bar above your window.
    pub fn new(override_dimension: Option<u32>, window_title: &str) -> Self {
        let event_loop = EventLoop::<()>::new();
        let dimension = override_dimension.unwrap_or(1024);
        let logical_size = LogicalSize::new(dimension, dimension);
        let window = WindowBuilder::new()
            .with_inner_size(logical_size)
            .with_title(window_title);
        let context = ContextBuilder::new(); // This is the place to enable multisampling and vsync if we want to
        let display = Display::new(window, context, &event_loop).unwrap();

        let current_monitor = display.gl_window().window().current_monitor();
        let scale_factor: f64 = current_monitor.scale_factor();

        // Create a closure that captures current screen information to use to
        // do local screen coordinate conversion for us.
        let inverse_half_dimension = 1.0 / (dimension as f32 * 0.5);
        let screen_to_opengl = Box::new(move |pos: PhysicalPosition<f64>| -> Vec2 {
            let logical_pos: (f64, f64) = pos.to_logical::<f64>(scale_factor).into();
            let x = (logical_pos.0 as f32 * inverse_half_dimension) - 1.0;
            let y = 1.0 - (logical_pos.1 as f32 * inverse_half_dimension);
            Vec2::new(x, y)
        });

        // For drawing shapes
        let shape_vertex_shader = r#"
        #version 140

        in vec2 position;
        in vec3 color;
        out vec3 v_color;

        uniform mat4 matrix;

        void main() {
            v_color = color;
            gl_Position = matrix * vec4(position, 0.0, 1.0);
        }
        "#;

        let shape_fragment_shader = r#"
            #version 140

            in vec3 v_color;
            out vec4 color;

            void main() {
                color = vec4(v_color, 1.0);
            }
        "#;

        let shape_program = Program::new(
            &display,
            ProgramCreationInput::SourceCode {
                vertex_shader: shape_vertex_shader,
                tessellation_control_shader: None,
                tessellation_evaluation_shader: None,
                geometry_shader: None,
                fragment_shader: shape_fragment_shader,
                transform_feedback_varyings: None,
                outputs_srgb: true,
                uses_point_size: true,
            },
        )
        .unwrap();

        let shader_circle_program = Program::new(
            &display,
            ProgramCreationInput::SourceCode {
                vertex_shader: include_str!("shader/circle_solid_vertex.glsl"),
                tessellation_control_shader: None,
                tessellation_evaluation_shader: None,
                geometry_shader: None,
                fragment_shader: include_str!("shader/circle_solid_frag.glsl"),
                transform_feedback_varyings: None,
                outputs_srgb: true,
                uses_point_size: true,
            },
        )
        .unwrap();

        // For drawing images
        let vertex_shader_img = r#"
            #version 140
            uniform mat4 matrix;
            in vec2 position;
            in vec2 tex_coords;
            in vec3 tint;
            in uint should_tint;
            out vec3 v_tint;
            out vec2 v_tex_coords;
            flat out uint v_should_tint;
            void main() {
                v_should_tint = should_tint;
                v_tint = tint;
                gl_Position = matrix * vec4(position, 0.0, 1.0);
                v_tex_coords = tex_coords;
            }
        "#;

        let fragment_shader_img = r#"
            #version 140
            uniform sampler2D tex;
            in vec2 v_tex_coords;
            in vec3 v_tint;
            flat in uint v_should_tint;
            out vec4 f_color;
            void main() {
                if ((texture(tex, v_tex_coords).a < 0.9) || (v_should_tint == 0u)) {
                    f_color = texture(tex, v_tex_coords);
                } else {
                    f_color = mix(texture(tex, v_tex_coords), vec4(v_tint, 1.0), 0.5);
                }
            }
        "#;

        let img_program = Program::new(
            &display,
            ProgramCreationInput::SourceCode {
                vertex_shader: vertex_shader_img,
                tessellation_control_shader: None,
                tessellation_evaluation_shader: None,
                geometry_shader: None,
                fragment_shader: fragment_shader_img,
                transform_feedback_varyings: None,
                outputs_srgb: true,
                uses_point_size: true,
            },
        )
        .unwrap();

        Self {
            event_loop,
            display,
            shape_program,
            shader_shape_program: shader_circle_program,
            img_program,
            screen_to_opengl,
            dimension,
            target: None,
        }
    }

    /// Call `drawstart()` when you are ready to draw a new frame. It will initialize the next
    /// off-screen framebuffer and clear it to black.
    pub fn drawstart(&mut self) {
        self.target = Some(self.display.draw());
        if let Some(ref mut target) = self.target {
            target.clear_color(0.0, 0.0, 0.0, 1.0);
        }
    }

    /// Call `drawfinish()` when you are ready to finalize the frame and show it.  You will need to
    /// call `drawstart()` again before you can `draw()` any shapes in a new frame.  I _think_ this
    /// method blocks until the hardware is ready for a frame (vsync), so an unconstrained loop
    /// (that runs fast enough) should run at 60fps on most displays.
    pub fn drawfinish(&mut self) {
        self.target.take().unwrap().finish().unwrap();
    }

    /// For convenience this method abstracts all the possible mouse and keyboard events to
    /// `GameEvent`s, which are the events we care about for the game.
    /// The WASD and arrow keys map to directions, mouse clicks and space bar map to attacks, and
    /// the Escape key maps to quitting.  Any number of events could have occurred since we last
    /// looked, so a `Vec<GameEvent>` is returned.
    pub fn poll_game_events(&mut self) -> Vec<GameEvent> {
        let screen_to_opengl = &mut (self.screen_to_opengl);
        let mut events = Vec::<GameEvent>::new();
        self.event_loop.run_return(|ev, _, control_flow| {
            *control_flow = ControlFlow::Exit;
            if let GLEvent::WindowEvent { event, .. } = ev {
                match event {
                    // Time to close the app?
                    GLWindowEvent::CloseRequested => events.push(GameEvent::Quit),
                    // Mouse moved
                    GLWindowEvent::CursorMoved { position, .. } => {
                        let mouse_pos = screen_to_opengl(position);
                        events.push(GameEvent::MouseMoved {
                            position: mouse_pos,
                        });
                    }
                    // Keyboard button
                    GLWindowEvent::KeyboardInput { input, .. } => {
                        let button_state = match input.state {
                            GLElementState::Pressed => ButtonState::Pressed,
                            GLElementState::Released => ButtonState::Released,
                        };
                        use GLVirtualKeyCode::*;
                        if let Some(vkey) = input.virtual_keycode {
                            match vkey {
                                W | Up | Comma => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Up,
                                }),
                                S | Down | O => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Down,
                                }),
                                A | Left => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Left,
                                }),
                                D | Right | E => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Right,
                                }),
                                Escape => events.push(GameEvent::Quit),
                                Space | Delete => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Action1,
                                }),
                                NumpadEnter | Return => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Action2,
                                }),
                                Tab => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Action3,
                                }),
                                // Equals covers the +/= key.
                                Equals => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Increase,
                                }),
                                // Minus covers the -/_ key.
                                Minus => events.push(GameEvent::Button {
                                    button_state,
                                    button_value: ButtonValue::Decrease,
                                }),
                                _ => (),
                            }
                        }
                    }
                    GLWindowEvent::MouseInput { state, button, .. } => {
                        let button_state = match state {
                            GLElementState::Pressed => ButtonState::Pressed,
                            GLElementState::Released => ButtonState::Released,
                        };
                        events.push(GameEvent::Button {
                            button_state,
                            button_value: {
                                match button {
                                    GLMouseButton::Left => ButtonValue::Action1,
                                    GLMouseButton::Right => ButtonValue::Action2,
                                    GLMouseButton::Middle | GLMouseButton::Other(_) => {
                                        ButtonValue::Action3
                                    }
                                }
                            },
                        });
                    }
                    _ => (),
                }
            }
        });
        events
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}