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
// Timer Widget
// Timer-based widget that fires off a callback every time a certain time period is reached.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use opengl_graphics::GlGraphics;
use piston_window::*;
//use std::time::{SystemTime, UNIX_EPOCH};

use crate::core::point::*;
use crate::widget::config::*;
use crate::widget::widget::*;

/// This is the `TimerWidget`.  It contains no base widget, it only contains a start and end
/// time,
pub struct TimerWidget {
    config: Configurable,
    enabled: bool,
    _initiated: u128,
    timeout: u128,
}

/// Implementation of the constructor for the `TimerWidget`.  Timer widgets are not accessible
/// on the screen, so they have an origin of 0x0 and width of 0x0.
impl TimerWidget {
    pub fn new() -> Self {
        Self {
            config: Configurable::new(),
            enabled: true,
            _initiated: 0,
            timeout: 0,
        }
    }

    pub fn tick(&mut self) {
        if self.enabled {
            //            let cur_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().subsec_millis();
            //            let elapsed = cur_time - self.initiated;
            //
            //            eprintln!("Elapsed: {}", elapsed);
            //
            //            if elapsed > self.timeout {
            //                eprintln!("Timeout!");
            //                self.initiated = cur_time;
            //            }
            //
            //            eprintln!("Tick!");
        }
    }

    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    pub fn set_timeout(&mut self, timeout: u128) {
        self.timeout = timeout;
    }
}

/// Implementation of the `TimerWidget` object with the `Widget` traits implemented.
///
/// Example usage:
/// ```no_run
/// # use piston_window::*;
/// # use pushrod::core::point::*;
/// # use pushrod::core::window::*;
/// # use pushrod::widget::widget::*;
/// # use pushrod::widget::timer_widget::*;
/// # fn main() {
/// #   let opengl = OpenGL::V3_2;
/// #   let mut pushrod_window: PushrodWindow = PushrodWindow::new(
/// #       WindowSettings::new("Pushrod Window", [640, 480])
/// #           .opengl(opengl)
/// #           .build()
/// #           .unwrap_or_else(|error| panic!("Failed to build PistonWindow: {}", error)),
/// #   );
/// #
///    let mut timer_widget = TimerWidget::new();
///
///    // (OR)
///
/// # }
/// ```
impl Widget for TimerWidget {
    fn config(&mut self) -> &mut Configurable {
        &mut self.config
    }

    fn is_invalidated(&mut self) -> bool {
        true
    }

    fn get_origin(&mut self) -> Point {
        make_origin_point()
    }

    fn get_size(&mut self) -> crate::core::point::Size {
        make_unsized()
    }

    fn mouse_entered(&mut self, _widget_id: i32) {}

    fn mouse_exited(&mut self, _widget_id: i32) {}

    fn mouse_scrolled(&mut self, _widget_id: i32, _point: Point) {}

    /// Draws the contents of the widget in this order:
    ///
    /// - Base widget first
    /// - Box graphic for the specified width
    fn draw(&mut self, _context: Context, _graphics: &mut GlGraphics) {
        self.tick();
    }
}