Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared examples #1280

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions backends/conrod_example_shared/src/button_xy_pad_toggle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

use conrod_core::{
Colorable,
Widget,
Labelable,
Positionable,
Rect,
Sizeable,
Ui,
UiCell,
widget,
};

use layout::*;

widget_ids! {
pub struct Ids {
button_title,
button,
xy_pad,
toggle,
ball,
}
}

pub struct GuiState {
ball_xy: conrod_core::Point,
ball_color: conrod_core::Color,
}

impl GuiState {
pub fn new() -> Self {
Self {
ball_xy: [0.0, 0.0],
ball_color: conrod_core::color::WHITE,
}
}
}

pub struct Gui {
ids: Ids,
}

impl Gui {
pub fn new(ui: &mut Ui) -> Self {
Self {
ids: Ids::new(ui.widget_id_generator()),
}
}

/// Returns id of widget that the next Gui should be down_from
pub fn update(&self, ui: &mut UiCell, state: &mut GuiState, canvas: widget::Id, last: widget::Id, rect: &Rect, side: f64) -> widget::Id {
let ids = &self.ids;

widget::Text::new("Button, XYPad and Toggle")
.down_from(last, 60.0)
.align_middle_x_of(canvas)
.font_size(SUBTITLE_SIZE)
.set(ids.button_title, ui);

for _press in widget::Button::new()
.label("PRESS ME")
.mid_left_with_margin_on(canvas, MARGIN)
.down_from(ids.button_title, 60.0)
.w_h(side, side)
.set(ids.button, ui)
{
let x = rand::random::<conrod_core::Scalar>() * (rect.x.end - rect.x.start) - rect.x.end;
let y = rand::random::<conrod_core::Scalar>() * (rect.y.end - rect.y.start) - rect.y.end;
state.ball_xy = [x, y];
}

for (x, y) in widget::XYPad::new(state.ball_xy[0], rect.x.start, rect.x.end,
state.ball_xy[1], rect.y.start, rect.y.end)
.label("BALL XY")
.wh_of(ids.button)
.align_middle_y_of(ids.button)
.align_middle_x_of(canvas)
.parent(canvas)
.set(ids.xy_pad, ui)
{
state.ball_xy = [x, y];
}

let is_white = state.ball_color == conrod_core::color::WHITE;
let label = if is_white { "WHITE" } else { "BLACK" };
for is_white in widget::Toggle::new(is_white)
.label(label)
.label_color(if is_white { conrod_core::color::WHITE } else { conrod_core::color::LIGHT_CHARCOAL })
.mid_right_with_margin_on(canvas, MARGIN)
.align_middle_y_of(ids.button)
.set(ids.toggle, ui)
{
state.ball_color = if is_white { conrod_core::color::WHITE } else { conrod_core::color::BLACK };
}

let ball_x = state.ball_xy[0];
let ball_y = state.ball_xy[1] - rect.y.end - side * 0.5 - MARGIN;
widget::Circle::fill(20.0)
.color(state.ball_color)
.x_y_relative_to(ids.xy_pad, ball_x, ball_y)
.set(ids.ball, ui);

ids.xy_pad // Return id of widget that the next Gui should be down_from
}
}
49 changes: 49 additions & 0 deletions backends/conrod_example_shared/src/image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use conrod_core::{
Positionable,
Sizeable,
Ui,
UiCell,
Widget,
widget,
};

use layout::*;

widget_ids! {
pub struct Ids {
image_title,
rust_logo,
}
}

pub struct Gui {
ids: Ids,
}

impl Gui {
pub fn new(ui: &mut Ui) -> Self {
Self {
ids: Ids::new(ui.widget_id_generator()),
}
}

/// Returns id of widget that the next Gui should be down_from
pub fn update(&self, ui: &mut UiCell, rust_logo: conrod_core::image::Id, canvas: widget::Id, last: widget::Id) -> widget::Id {
let ids = &self.ids;

widget::Text::new("Image")
.down_from(last, MARGIN)
.align_middle_x_of(canvas)
.font_size(SUBTITLE_SIZE)
.set(ids.image_title, ui);

const LOGO_SIDE: conrod_core::Scalar = 144.0;
widget::Image::new(rust_logo)
.w_h(LOGO_SIDE, LOGO_SIDE)
.down(60.0)
.align_middle_x_of(canvas)
.set(ids.rust_logo, ui);

ids.rust_logo // Return id of widget that the next Gui should be down_from
}
}
6 changes: 6 additions & 0 deletions backends/conrod_example_shared/src/layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use conrod_core::Scalar;

pub const MARGIN: Scalar = 30.0;
pub const SHAPE_GAP: Scalar = 50.0;
pub const TITLE_SIZE: conrod_core::FontSize = 42;
pub const SUBTITLE_SIZE: conrod_core::FontSize = 32;
Loading