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
// Progress Widget // Handles the display of a progress bar // // 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 piston_window::*; use crate::core::callbacks::*; use crate::core::point::*; use crate::widget::box_widget::*; use crate::widget::config::*; use crate::widget::widget::*; /// This is the `ProgressWidget`, which is used for showing a progress bar. /// /// Example usage: /// ```no_run /// # use piston_window::*; /// # use pushrod::core::main::*; /// # use pushrod::core::point::*; /// # use pushrod::widget::widget::*; /// # use pushrod::widget::progress_widget::*; /// # fn main() { /// # let window: PistonWindow = WindowSettings::new("Pushrod Window", [800, 600]) /// # .opengl(OpenGL::V3_2) /// # .resizable(true) /// # .build() /// # .unwrap_or_else(|error| panic!("Failed to build PistonWindow: {}", error)); /// # let mut prod: Pushrod = Pushrod::new(window); /// let mut progress_widget = ProgressWidget::new(); /// /// progress_widget.set_color([1.0; 4]); /// progress_widget.set_secondary_color([0.25, 0.75, 0.50, 1.0]); /// progress_widget.set_origin(100, 100); /// progress_widget.set_size(200, 32); /// progress_widget.set_progress(50); /// # } /// ``` pub struct ProgressWidget { config: Configurable, callbacks: CallbackStore, base_widget: BoxWidget, progress: u16, } /// Implementation of the constructor for the `ProgressWidget`. impl ProgressWidget { pub fn new() -> Self { let mut base = BoxWidget::new(); base.set_border_thickness(1); base.set_border_color([0.0, 0.0, 0.0, 1.0]); Self { config: Configurable::new(), callbacks: CallbackStore::new(), base_widget: base, progress: 0, } } /// Sets the progress to be indicated. This is a number between 0 and 100. (Anything over 100 /// will just fill the box.) pub fn set_progress(&mut self, progress: u16) { self.progress = progress; self.invalidate(); } /// Returns the current progress shown. pub fn get_progress(&mut self) -> u16 { self.progress } } /// Implementation of the `ProgressWidget` object with the `Widget` traits implemented. /// The base widget is a `BoxWidget`, which overlays a `TextWidget` over the top. This `Widget` /// responds to the button down/up callbacks internally, and generates an `on_clicked` callback /// when appropriate. impl Widget for ProgressWidget { fn config(&mut self) -> &mut Configurable { &mut self.config } fn callbacks(&mut self) -> &mut CallbackStore { &mut self.callbacks } /// Sets the `Point` of origin for this widget and the base widget, given the X and Y /// coordinates. Invalidates the widget afterward. fn set_origin(&mut self, x: i32, y: i32) { self.config().set(Origin(Point { x, y })); self.base_widget.set_origin(x, y); self.invalidate(); } /// Sets the `Size` for this widget and the base widget, given width and height. Invalidates the widget afterward. fn set_size(&mut self, w: i32, h: i32) { self.config() .set(BodySize(crate::core::point::Size { w, h })); self.base_widget.set_size(w, h); self.invalidate(); } /// Sets the color for this widget. Invalidates the widget afterward. fn set_color(&mut self, color: types::Color) { self.base_widget.set_color(color); self.invalidate(); } /// Draws the widget. The progress bar is the secondary color. fn draw(&mut self, c: Context, g: &mut G2d, clip: &DrawState) { let size: crate::core::point::Size = self.get_size(); self.base_widget.draw(c, g, clip); let draw_width = (size.w as f64 * (self.progress as f64 / 100.0)) as f64; // Paint the secondary color to display the progress color. Rectangle::new(self.get_secondary_color()).draw( [1.0 as f64, 1.0 as f64, draw_width, (size.h - 2) as f64], clip, c.transform, g, ); // Then clear invalidation. self.clear_invalidate(); } }