|
| 1 | +extern crate iui; |
| 2 | +extern crate ui_sys; |
| 3 | + |
| 4 | +use iui::controls::{ |
| 5 | + Area, AreaDrawParams, AreaHandler, AreaMouseEvent, HorizontalBox, LayoutStrategy, |
| 6 | +}; |
| 7 | +use iui::draw::{Brush, Path, SolidBrush}; |
| 8 | +use iui::prelude::*; |
| 9 | +use std::f64::consts::PI; |
| 10 | +use ui_sys::uiDrawFillModeWinding; |
| 11 | + |
| 12 | +struct HandleCanvas {} |
| 13 | +impl AreaHandler for HandleCanvas { |
| 14 | + fn draw(&mut self, _area: &Area, draw_params: &AreaDrawParams) { |
| 15 | + let ctx = &draw_params.context; |
| 16 | + |
| 17 | + let path = Path::new(ctx, uiDrawFillModeWinding); |
| 18 | + path.add_rectangle(ctx, 0., 0., draw_params.area_width, draw_params.area_height); |
| 19 | + path.end(ctx); |
| 20 | + |
| 21 | + let brush = Brush::Solid(SolidBrush { |
| 22 | + r: 0.2, |
| 23 | + g: 0.6, |
| 24 | + b: 0.8, |
| 25 | + a: 1., |
| 26 | + }); |
| 27 | + |
| 28 | + draw_params.context.fill(&path, &brush); |
| 29 | + |
| 30 | + let path = Path::new(ctx, uiDrawFillModeWinding); |
| 31 | + for i in 0..100 { |
| 32 | + let x = i as f64 / 100.; |
| 33 | + let y = ((x * PI * 2.).sin() + 1.) / 2.; |
| 34 | + path.add_rectangle( |
| 35 | + ctx, |
| 36 | + x * draw_params.area_width, |
| 37 | + 0., |
| 38 | + draw_params.area_width / 100., |
| 39 | + y * draw_params.area_height, |
| 40 | + ); |
| 41 | + } |
| 42 | + path.end(ctx); |
| 43 | + |
| 44 | + let brush = Brush::Solid(SolidBrush { |
| 45 | + r: 0.2, |
| 46 | + g: 0., |
| 47 | + b: 0.3, |
| 48 | + a: 1., |
| 49 | + }); |
| 50 | + |
| 51 | + draw_params.context.fill(&path, &brush); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +fn main() { |
| 56 | + let ui = UI::init().expect("Couldn't initialize UI library"); |
| 57 | + let mut win = Window::new(&ui, "Area Canvas Example", 200, 200, WindowType::NoMenubar); |
| 58 | + |
| 59 | + let mut hbox = HorizontalBox::new(&ui); |
| 60 | + let area = Area::new(&ui, Box::new(HandleCanvas {})); |
| 61 | + hbox.append(&ui, area, LayoutStrategy::Stretchy); |
| 62 | + |
| 63 | + win.set_child(&ui, hbox); |
| 64 | + win.show(&ui); |
| 65 | + ui.main(); |
| 66 | +} |
0 commit comments