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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use super::{Anchor, Anchored, FontAsset, FontHandle, MouseReactive, Stretch, Stretched, TtfFormat,
            UiImage, UiText, UiTransform};
///! A clickable button.
use amethyst_assets::{AssetStorage, Loader};
use amethyst_core::Parent;
use amethyst_core::specs::prelude::{Entities, Entity, Read, ReadExpect, World, WriteStorage};
use amethyst_renderer::Texture;
use shred::SystemData;

const DEFAULT_Z: f32 = -1.0;
const DEFAULT_WIDTH: f32 = 128.0;
const DEFAULT_HEIGHT: f32 = 64.0;
const DEFAULT_TAB_ORDER: i32 = 9;
const DEFAULT_BKGD_COLOR: [f32; 4] = [0.82, 0.83, 0.83, 1.0];
const DEFAULT_TXT_COLOR: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
const DEFAULT_FONT_NAME: &'static str = "font/square.ttf";

/// Container that wraps the resources we need to initialize button defaults
#[derive(SystemData)]
pub struct UiButtonResources<'a> {
    font_asset: Read<'a, AssetStorage<FontAsset>>,
    loader: ReadExpect<'a, Loader>,
    texture_asset: Read<'a, AssetStorage<Texture>>,
}

/// Container for all the resources the builder needs to make a new UiButton.
#[derive(SystemData)]
struct UiButtonBuilderResources<'a> {
    anchored: WriteStorage<'a, Anchored>,
    entities: Entities<'a>,
    image: WriteStorage<'a, UiImage>,
    mouse_reactive: WriteStorage<'a, MouseReactive>,
    parent: WriteStorage<'a, Parent>,
    stretched: WriteStorage<'a, Stretched>,
    text: WriteStorage<'a, UiText>,
    transform: WriteStorage<'a, UiTransform>,
}

impl<'a> UiButtonResources<'a> {
    /// Grab the resources we need from the world.
    pub fn from_world(world: &'a World) -> Self {
        Self::fetch(&world.res)
    }
}

impl<'a> UiButtonBuilderResources<'a> {
    /// Grab the resources we need from the world.
    pub fn from_world(world: &'a World) -> Self {
        Self::fetch(&world.res)
    }
}

/// Builder for a `UiButton`.
pub struct UiButtonBuilder<'a> {
    name: &'a str,
    image: UiImage,
    text: UiText,
    anchored: Option<Anchored>,
    parent: Option<Parent>,
    stretched: Option<Stretched>,
    transform: Option<UiTransform>,
}

/// A clickable button.
pub struct UiButton {
    /// The actual text of the button.
    pub text: Entity,
    /// Represents the background of the image. Defaults to a grey rectangle.
    pub image: Entity,
}

impl<'a> UiButtonBuilder<'a> {
    /// Construct a new UiButtonBuilder.
    /// This allows easy use of default values for text and button appearance and allows the user
    /// to easily set other UI-related options.
    pub fn new<'b, S: ToString>(
        name: &'a str,
        text: S,
        resources: UiButtonResources<'b>,
    ) -> UiButtonBuilder<'a> {
        let (text, image) = {
            let loader = &resources.loader;

            let font = loader.load(
                DEFAULT_FONT_NAME,
                TtfFormat,
                Default::default(),
                (),
                &resources.font_asset,
            );
            let text = UiText::new(font, text.to_string(), DEFAULT_TXT_COLOR, 32.0);
            let grey =
                loader.load_from_data(DEFAULT_BKGD_COLOR.into(), (), &resources.texture_asset);
            let image = UiImage { texture: grey };
            (text, image)
        };

        UiButtonBuilder {
            name,
            image,
            text,
            anchored: None,
            parent: None,
            stretched: None,
            transform: None,
        }
    }

    /// Add an anchor to the button.
    pub fn with_anchored(mut self, anchored: Anchored) -> Self {
        self.anchored = Some(anchored);
        self
    }

    /// Add a parent to the button.
    pub fn with_parent(mut self, parent: Parent) -> Self {
        self.parent = Some(parent);
        self
    }

    /// Stretch the button.
    pub fn with_stretched(mut self, stretched: Stretched) -> Self {
        self.stretched = Some(stretched);
        self
    }

    /// Add a UiTransform to the image to offset it within the UI.
    pub fn with_transform(mut self, transform: UiTransform) -> Self {
        self.transform = Some(transform);
        self
    }

    /// This will completely replace the UiText object representing the button's text.
    /// Use this if you want to change more than just the characters, but the font size, color,
    /// etc. as well.
    /// Use [`with_text`](#with_text) to just change the underlying text.
    pub fn with_uitext(mut self, text: UiText) -> Self {
        self.text = text;
        self
    }

    /// This will set the rendered characters within the button. Use this to just change what
    /// characters will appear. If you need to change the font size, color, etc., then you should
    /// use
    /// [`with_uitext`](#with_uitext) and provide a new `UiText` object.
    pub fn with_text<S>(mut self, text: S) -> Self
    where
        S: ToString,
    {
        self.text.text = text.to_string();
        self
    }

    /// Replace the default UiImage with `image`.
    pub fn with_image(mut self, image: UiImage) -> Self {
        self.image = image;
        self
    }

    /// Use a different font for the button text.
    pub fn with_font(mut self, font: FontHandle) -> Self {
        self.text.font = font;
        self
    }

    /// Provide an X and Y position for the button.
    ///
    /// This will create a default UiTransform if one is not already attached.
    /// See `DEFAULT_Z`, `DEFAULT_WIDTH`, `DEFAULT_HEIGHT`, and `DEFAULT_TAB_ORDER` for
    /// the values that will be provided to the default UiTransform.
    pub fn with_position(mut self, x: f32, y: f32) -> Self {
        self.transform = if let Some(mut t) = self.transform.take() {
            t.local_x = x;
            t.global_x = x;
            t.local_y = y;
            t.global_y = y;
            Some(t)
        } else {
            let mut id = self.name.to_string();
            id.push_str("_new_transform");
            Some(UiTransform::new(
                id,
                x,
                y,
                DEFAULT_Z,
                DEFAULT_WIDTH,
                DEFAULT_HEIGHT,
                DEFAULT_TAB_ORDER,
            ))
        };
        self
    }

    // unwraps are safe because we create the entities inside
    fn build(mut self, mut res: UiButtonBuilderResources) -> UiButton {
        let image_entity = res.entities.create();
        res.image.insert(image_entity, self.image).unwrap();
        res.mouse_reactive
            .insert(image_entity, MouseReactive)
            .unwrap();
        if let Some(parent) = self.parent.take() {
            res.parent.insert(image_entity, parent).unwrap();
        }
        if let Some(transform) = self.transform.take() {
            res.transform.insert(image_entity, transform).unwrap();
        }
        if let Some(anchored) = self.anchored.take() {
            res.anchored.insert(image_entity, anchored).unwrap();
        }
        if let Some(stretched) = self.stretched.take() {
            res.stretched.insert(image_entity, stretched).unwrap();
        }

        let mut id = self.name.to_string();
        id.push_str("_btn_txt");
        let text_entity = res.entities.create();
        res.transform
            .insert(
                text_entity,
                UiTransform::new(id, 0., 0., -1., 0., 0., 10).as_transparent(),
            )
            .unwrap();
        res.anchored
            .insert(text_entity, Anchored::new(Anchor::Middle))
            .unwrap();
        res.stretched
            .insert(text_entity, Stretched::new(Stretch::XY, 0., 0.))
            .unwrap();
        res.text.insert(text_entity, self.text).unwrap();
        res.parent
            .insert(
                text_entity,
                Parent {
                    entity: image_entity.clone(),
                },
            )
            .unwrap();

        UiButton {
            text: text_entity,
            image: image_entity,
        }
    }
    /// Create the UiButton based on provided configuration parameters.
    pub fn build_from_world(self, world: &World) -> UiButton {
        self.build(UiButtonBuilderResources::from_world(world))
    }
}