wgui: checkbox component
This commit is contained in:
@@ -25,10 +25,6 @@ pub struct Params {
|
||||
pub text_style: TextStyle,
|
||||
}
|
||||
|
||||
fn get_color2(color: &drawing::Color) -> drawing::Color {
|
||||
color.lerp(&Color::new(0.0, 0.0, 0.0, color.a), 0.2)
|
||||
}
|
||||
|
||||
impl Default for Params {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@@ -58,9 +54,9 @@ struct Data {
|
||||
initial_color: drawing::Color,
|
||||
initial_color2: drawing::Color,
|
||||
initial_border_color: drawing::Color,
|
||||
text_id: WidgetID, // Text
|
||||
rect_id: WidgetID, // Rectangle
|
||||
text_node: taffy::NodeId,
|
||||
id_label: WidgetID, // Label
|
||||
id_rect: WidgetID, // Rectangle
|
||||
node_label: taffy::NodeId,
|
||||
}
|
||||
|
||||
pub struct ComponentButton {
|
||||
@@ -83,12 +79,12 @@ impl ComponentButton {
|
||||
|
||||
state
|
||||
.widgets
|
||||
.call(self.data.text_id, |label: &mut WidgetLabel| {
|
||||
.call(self.data.id_label, |label: &mut WidgetLabel| {
|
||||
label.set_text(&mut globals.i18n(), text);
|
||||
});
|
||||
|
||||
alterables.mark_redraw();
|
||||
alterables.mark_dirty(self.data.text_node);
|
||||
alterables.mark_dirty(self.data.node_label);
|
||||
}
|
||||
|
||||
pub fn on_click(&self, func: ButtonClickCallback) {
|
||||
@@ -96,6 +92,10 @@ impl ComponentButton {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_color2(color: &drawing::Color) -> drawing::Color {
|
||||
color.lerp(&Color::new(0.0, 0.0, 0.0, color.a), 0.2)
|
||||
}
|
||||
|
||||
fn anim_hover(rect: &mut WidgetRectangle, data: &Data, pos: f32, pressed: bool) {
|
||||
let brightness = pos * if pressed { 0.75 } else { 0.5 };
|
||||
let border_brightness = pos;
|
||||
@@ -139,7 +139,7 @@ fn register_event_mouse_enter<U1, U2>(
|
||||
) {
|
||||
listeners.register(
|
||||
listener_handles,
|
||||
data.rect_id,
|
||||
data.id_rect,
|
||||
EventListenerKind::MouseEnter,
|
||||
Box::new(move |common, event_data, _, _| {
|
||||
common.alterables.trigger_haptics();
|
||||
@@ -162,7 +162,7 @@ fn register_event_mouse_leave<U1, U2>(
|
||||
) {
|
||||
listeners.register(
|
||||
listener_handles,
|
||||
data.rect_id,
|
||||
data.id_rect,
|
||||
EventListenerKind::MouseLeave,
|
||||
Box::new(move |common, event_data, _, _| {
|
||||
common.alterables.trigger_haptics();
|
||||
@@ -185,7 +185,7 @@ fn register_event_mouse_press<U1, U2>(
|
||||
) {
|
||||
listeners.register(
|
||||
listener_handles,
|
||||
data.rect_id,
|
||||
data.id_rect,
|
||||
EventListenerKind::MousePress,
|
||||
Box::new(move |common, event_data, _, _| {
|
||||
let mut state = state.borrow_mut();
|
||||
@@ -213,7 +213,7 @@ fn register_event_mouse_release<U1, U2>(
|
||||
) {
|
||||
listeners.register(
|
||||
listener_handles,
|
||||
data.rect_id,
|
||||
data.id_rect,
|
||||
EventListenerKind::MouseRelease,
|
||||
Box::new(move |common, event_data, _, _| {
|
||||
let rect = event_data.obj.get_as_mut::<WidgetRectangle>();
|
||||
@@ -256,7 +256,7 @@ pub fn construct<U1, U2>(
|
||||
|
||||
let globals = layout.state.globals.clone();
|
||||
|
||||
let (rect_id, _) = layout.add_child(
|
||||
let (id_rect, _) = layout.add_child(
|
||||
parent,
|
||||
WidgetRectangle::create(WidgetRectangleParams {
|
||||
color: params.color,
|
||||
@@ -271,8 +271,8 @@ pub fn construct<U1, U2>(
|
||||
|
||||
let light_text = (params.color.r + params.color.g + params.color.b) < 1.5;
|
||||
|
||||
let (text_id, text_node) = layout.add_child(
|
||||
rect_id,
|
||||
let (id_label, node_label) = layout.add_child(
|
||||
id_rect,
|
||||
WidgetLabel::create(
|
||||
&mut globals.i18n(),
|
||||
WidgetLabelParams {
|
||||
@@ -294,9 +294,9 @@ pub fn construct<U1, U2>(
|
||||
)?;
|
||||
|
||||
let data = Rc::new(Data {
|
||||
text_id,
|
||||
rect_id,
|
||||
text_node,
|
||||
id_label,
|
||||
id_rect,
|
||||
node_label,
|
||||
initial_color: params.color,
|
||||
initial_color2: get_color2(¶ms.color),
|
||||
initial_border_color: params.border_color,
|
||||
|
||||
Reference in New Issue
Block a user