From 2fdac8fc505a96a6099ac9d115f9ee322549de80 Mon Sep 17 00:00:00 2001 From: Hendrik Richter Date: Fri, 9 Aug 2019 10:23:36 +0200 Subject: [PATCH 1/3] add ColorButton --- iui/src/controls/basic.rs | 68 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/iui/src/controls/basic.rs b/iui/src/controls/basic.rs index 25f23497f..85e6d60a9 100644 --- a/iui/src/controls/basic.rs +++ b/iui/src/controls/basic.rs @@ -3,7 +3,15 @@ use std::os::raw::c_void; use std::ffi::{CStr, CString}; use std::mem; use ui::UI; -use ui_sys::{self, uiButton, uiControl, uiLabel}; +use ui_sys::{self, uiButton, uiColorButton, uiControl, uiLabel}; + +#[derive(Debug)] +pub struct Rgba { + pub r: f64, + pub g: f64, + pub b: f64, + pub a: f64, +} define_control!{ /// A non-interactable piece of text. @@ -17,6 +25,12 @@ define_control!{ sys_type: uiButton } +define_control!{ + /// A color chooser button which users can click on, causing a callback to run. + rust_type: ColorButton, + sys_type: uiColorButton +} + impl Button { /// Create a new button with the given text as its label. pub fn new(_ctx: &UI, text: &str) -> Button { @@ -69,6 +83,58 @@ impl Button { } } +impl ColorButton { + /// Create a new color button + pub fn new(_ctx: &UI) -> ColorButton { + unsafe { ColorButton::from_raw(ui_sys::uiNewColorButton()) } + } + + pub fn color(&self, _ctx: &UI) -> Rgba { + let mut r: f64 = 0.0; + let mut g: f64 = 0.0; + let mut b: f64 = 0.0; + let mut a: f64 = 0.0; + + unsafe { + ui_sys::uiColorButtonColor(self.uiColorButton, &mut r, &mut g, &mut b, &mut a); + Rgba { r, g, b, a } + } + } + + /// Set the text on the button. + pub fn set_color(&mut self, _ctx: &UI, color: Rgba) { + unsafe { + ui_sys::uiColorButtonSetColor(self.uiColorButton, color.r, color.g, color.b, color.a) + } + } + + /// Run the given callback when the color button is clicked. + pub fn on_changed<'ctx, F: FnMut(&mut ColorButton) + 'ctx>( + &mut self, + _ctx: &'ctx UI, + callback: F, + ) { + unsafe { + let mut data: Box> = Box::new(Box::new(callback)); + ui_sys::uiColorButtonOnChanged( + self.uiColorButton, + Some(c_callback), + &mut *data as *mut Box as *mut c_void, + ); + mem::forget(data); + } + + extern "C" fn c_callback(button: *mut uiColorButton, data: *mut c_void) { + unsafe { + let mut button = ColorButton { + uiColorButton: button, + }; + mem::transmute::<*mut c_void, &mut Box>(data)(&mut button) + } + } + } +} + impl Label { /// Create a new label with the given string as its text. /// Note that labels do not auto-wrap their text; they will expand as far as needed From 10ee6c3c013a3bba93d539e2b70346a9b385a15a Mon Sep 17 00:00:00 2001 From: Hendrik Richter Date: Fri, 9 Aug 2019 10:23:48 +0200 Subject: [PATCH 2/3] add ColorButton usage example --- iui/examples/basic.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/iui/examples/basic.rs b/iui/examples/basic.rs index 4edeb2e43..89afe62f7 100644 --- a/iui/examples/basic.rs +++ b/iui/examples/basic.rs @@ -1,13 +1,13 @@ extern crate iui; +use iui::controls::{Button, ColorButton, Group, Label, Rgba, VerticalBox}; use iui::prelude::*; -use iui::controls::{Label, Button, VerticalBox, Group}; fn main() { // Initialize the UI library let ui = UI::init().expect("Couldn't initialize UI library"); // Create a window into which controls can be placed let mut win = Window::new(&ui, "Test App", 200, 200, WindowType::NoMenubar); - + // Create a vertical layout to hold the controls let mut vbox = VerticalBox::new(&ui); vbox.set_padded(&ui, true); @@ -32,6 +32,23 @@ fn main() { } }); + let mut color_button = ColorButton::new(&ui); + color_button.set_color( + &ui, + Rgba { + r: 1.0, + g: 0.0, + b: 0.0, + a: 1.0, + }, + ); + color_button.on_changed(&ui, { + let ui = ui.clone(); + move |btn| { + dbg!(btn.color(&ui)); + } + }); + // Create a new label. Note that labels don't auto-wrap! let mut label_text = String::new(); label_text.push_str("There is a ton of text in this label.\n"); @@ -42,6 +59,7 @@ fn main() { vbox.append(&ui, label, LayoutStrategy::Stretchy); group_vbox.append(&ui, button, LayoutStrategy::Compact); group_vbox.append(&ui, quit_button, LayoutStrategy::Compact); + group_vbox.append(&ui, color_button, LayoutStrategy::Compact); group.set_child(&ui, group_vbox); vbox.append(&ui, group, LayoutStrategy::Compact); From de396bda5edfcbf659aa1ffa0a6bfde33d2683e3 Mon Sep 17 00:00:00 2001 From: Hendrik Richter Date: Fri, 9 Aug 2019 12:19:39 +0200 Subject: [PATCH 3/3] add derive(Clone) --- iui/src/controls/basic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iui/src/controls/basic.rs b/iui/src/controls/basic.rs index 85e6d60a9..8b4099bba 100644 --- a/iui/src/controls/basic.rs +++ b/iui/src/controls/basic.rs @@ -5,7 +5,7 @@ use std::mem; use ui::UI; use ui_sys::{self, uiButton, uiColorButton, uiControl, uiLabel}; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Rgba { pub r: f64, pub g: f64,