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
// Widget Base Definition
//
// 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 graphics::*;
use opengl_graphics::GlGraphics;

use crate::core::callbacks::*;
use crate::core::point::{Point, Size};
use crate::widget::config::*;

/// Master level trait object for describing a `Widget`.  A `Widget` is a GUI element that can
/// be interacted with and can receive and generate events.
pub trait Widget {
    /// Retrieves the `Configurable` object for this `Widget`.  All `Widget` implementations
    /// must provide this.  (See the `CanvasWidget` implementation.)
    fn config(&mut self) -> &mut Configurable;

    /// Indicates that a `Widget` object needs to be repainted.
    fn invalidate(&mut self) {
        if !self.is_invalidated() {
            self.config().set_toggle(CONFIG_INVALIDATE, true);
        }
    }

    /// Clears the invalidation flag.  Set this when the `draw` function completes.  Otherwise,
    /// this `Widget` object may be continuously repainted.
    fn clear_invalidate(&mut self) {
        self.config().remove(CONFIG_INVALIDATE);
    }

    /// Indicates whether or not a `Widget` needs to be repainted.
    fn is_invalidated(&mut self) -> bool {
        self.config().contains(CONFIG_INVALIDATE)
    }

    /// Master config setter - use convenience methods.
    fn set_config(&mut self, config: u8, config_value: Config) {
        self.config().set(config, config_value.clone());
        self.invalidate();
    }

    /// Master config getter - use convenience methods.
    fn get_config(&mut self, config: u8) -> Option<&Config> {
        self.config().get(config)
    }

    /// Sets a point value for a configuration key.
    fn set_point(&mut self, config: u8, x: i32, y: i32) {
        self.set_config(config, Config::Point(Point { x, y }));
    }

    /// Sets a size value for a configuration key.
    fn set_size(&mut self, config: u8, w: i32, h: i32) {
        self.set_config(config, Config::Size(Size { w, h }));
    }

    /// Sets a color for a configuration key.
    fn set_color(&mut self, config: u8, color: types::Color) {
        self.set_config(config, Config::Color(color));
    }

    /// Sets a numeric value for a configuration key.
    fn set_numeric(&mut self, config: u8, value: u64) {
        self.set_config(config, Config::Numeric(value));
    }

    /// Sets a text value for a configuration key.
    fn set_text(&mut self, config: u8, text: String) {
        self.set_config(config, Config::Text(text.clone()));
    }

    /// Sets a toggle value for a configuration key.
    fn set_toggle(&mut self, config: u8, flag: bool) {
        self.set_config(config, Config::Toggle(flag));
    }

    /// Custom handler to receive an event.  Any `Widget` that implements this does so to handle
    /// top-level GUI events, such as a mouse entering or exiting the bounds of this `Widget`.
    /// If the `injected` flag is set, it indicates that the event supplied was generate by
    /// a `Widget`, and not by the run loop.
    fn handle_event(&mut self, _injected: bool, _event: CallbackEvent) -> Option<CallbackEvent> {
        None
    }

    /// Injects an event into the run loop.  This can be a timer event, a refresh event, or
    /// whatever the `Widget` wants to inject.  These should be custom events, not system
    /// events.  This method only gets called if `injects_events` returns `true`.
    fn inject_event(&mut self, _widget_id: i32) -> Option<CallbackEvent> {
        None
    }

    /// If this `Widget` provides custom injected events that are generated outside of the
    /// `handle_event` loop, indicate `true`.  Only override if necessary.  (See `TimerWidget`
    /// for reference.)
    fn injects_events(&mut self) -> bool {
        false
    }

    /// Draws the `Widget`'s contents.  Only gets called if the `Widget` is in invalidated
    /// state.  Provides a modified `Context` object that has an origin of `0x0` in drawing
    /// space for the draw routine.  Also provides a `mut G2d` object against which to draw,
    /// and a `clip`, which is automatically set to provide a clipping area for the `Widget`.  If
    /// the `Widget` draws outside of the clipped bounds, that will not be drawn on the
    /// screen.
    fn draw(&mut self, c: Context, g: &mut GlGraphics, clip: &DrawState) {
        let size: crate::core::point::Size = self.config().get_size(CONFIG_BODY_SIZE);

        g.rectangle(
            &Rectangle::new(self.config().get_color(CONFIG_MAIN_COLOR)),
            [0.0f64, 0.0f64, size.w as f64, size.h as f64],
            clip,
            c.transform,
        );

        self.clear_invalidate();
    }

    /// Internal method that is used to draw a box around the `Widget` when in disabled state.
    /// You can override this method, should you choose to, so that the disabled state appears
    /// differently in your application.  It is safe to leave this alone.
    fn draw_disabled(&mut self, c: Context, g: &mut GlGraphics, clip: &DrawState) {
        let size: crate::core::point::Size = self.config().get_size(CONFIG_BODY_SIZE);

        g.rectangle(
            &Rectangle::new([0.0, 0.0, 0.0, 0.8]),
            [0.0f64, 0.0f64, size.w as f64, size.h as f64],
            clip,
            c.transform,
        );
    }

    /// Draws an object at an offset on the screen.  This is a convenience method that is used
    /// by other `Widget`s that contain multiple widgets.  (See `CheckboxWidget` and
    /// `ImageButtonWidget` for good examples of this use.)
    fn draw_with_offset(
        &mut self,
        c: Context,
        g: &mut GlGraphics,
        clip: &DrawState,
        point_offset: Point,
    ) {
        self.draw(
            c.trans(point_offset.x as f64, point_offset.y as f64),
            g,
            clip,
        );
    }
}

/// Base `Widget` object.  Displays a blank canvas, with the color set by the `CONFIG_MAIN_COLOR`
/// configuration option.  Defaults to white.
pub struct CanvasWidget {
    config: Configurable,
}

impl CanvasWidget {
    pub fn new() -> Self {
        Self {
            config: Configurable::new(),
        }
    }
}

impl Widget for CanvasWidget {
    fn config(&mut self) -> &mut Configurable {
        &mut self.config
    }
}