ABC_Game_Engine 0.1.2

A simple, fast, and flexible Game Engine written in Rust, with simplicity in mind.
Documentation
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use ABC_ECS::World;

use crate::input::*;
use crate::Component;
use crate::System;
use crate::Transform;
use crate::{EntitiesAndComponents, Entity};

#[derive(Clone)]
/// A slider is a UI element that can be dragged to change a value
pub struct Slider {
    min_value: f32,
    max_value: f32,
    min_position: f32,
    max_position: f32,
    // the width of the clickable area
    width: f32,
    value: f32,
    callback: fn(&mut EntitiesAndComponents, f32),
    value_changed: bool,
    knob_entity: Option<Entity>,
    mouse_was_held: bool,
}

impl Slider {
    pub fn new(min_value: f32, max_value: f32, min_position: f32, max_position: f32) -> Self {
        Slider {
            min_value,
            max_value,
            min_position,
            max_position,
            width: max_position - min_position,
            value: min_value,
            callback: |_, _| {},
            value_changed: false,
            knob_entity: None,
            mouse_was_held: false,
        }
    }

    pub fn get_value(&self) -> f32 {
        self.value
    }

    /// sets the value of the slider, clamped between min_value and max_value
    pub fn set_value(&mut self, value: f32) {
        self.value = value.clamp(self.min_value, self.max_value);

        self.value_changed = true;
    }

    pub fn with_value(mut self, value: f32) -> Self {
        self.value = value;
        self
    }

    pub fn set_callback(&mut self, callback: fn(&mut EntitiesAndComponents, f32)) {
        self.callback = callback;
    }

    pub fn with_callback(mut self, callback: fn(&mut EntitiesAndComponents, f32)) -> Self {
        self.callback = callback;
        self
    }

    pub fn get_callback(&self) -> fn(&mut EntitiesAndComponents, f32) {
        self.callback
    }

    pub fn get_min_value(&self) -> f32 {
        self.min_value
    }

    pub fn get_max_value(&self) -> f32 {
        self.max_value
    }

    pub fn get_min_position(&self) -> f32 {
        self.min_position
    }

    pub fn get_max_position(&self) -> f32 {
        self.max_position
    }

    pub fn get_width(&self) -> f32 {
        self.width
    }

    pub fn set_min_value(&mut self, min_value: f32) {
        self.min_value = min_value;
    }

    pub fn set_max_value(&mut self, max_value: f32) {
        self.max_value = max_value;
    }

    pub fn set_min_position(&mut self, min_position: f32) {
        self.min_position = min_position;
    }

    pub fn set_max_position(&mut self, max_position: f32) {
        self.max_position = max_position;
    }

    pub fn set_width(&mut self, width: f32) {
        self.width = width;
    }

    pub fn with_min_value(mut self, min_value: f32) -> Self {
        self.min_value = min_value;
        self
    }

    pub fn with_max_value(mut self, max_value: f32) -> Self {
        self.max_value = max_value;
        self
    }

    pub fn with_min_position(mut self, min_position: f32) -> Self {
        self.min_position = min_position;
        self
    }

    pub fn with_max_position(mut self, max_position: f32) -> Self {
        self.max_position = max_position;
        self
    }

    pub fn with_width(mut self, width: f32) -> Self {
        self.width = width;
        self
    }

    pub fn with_knob_entity(mut self, knob_entity: Entity) -> Self {
        self.knob_entity = Some(knob_entity);
        self
    }

    pub fn get_knob_entity(&self) -> Option<Entity> {
        self.knob_entity
    }

    pub fn set_knob_entity(&mut self, knob_entity: Entity) {
        self.knob_entity = Some(knob_entity);
    }
}

/// just a mockup of a slider system for now
struct SliderSystem;

impl System for SliderSystem {
    fn run(&mut self, entities_and_components: &mut EntitiesAndComponents) {
        let entities_with_slider = entities_and_components
            .get_entities_with_component::<Slider>()
            .cloned()
            .collect::<Vec<Entity>>();

        for entity in entities_with_slider {
            let transform = crate::get_transform(entity, entities_and_components);

            let input = entities_and_components
                .get_resource::<Input>()
                .expect("Failed to get input");

            let mouse_position = input.get_mouse_position();
            let is_held = input.get_mouse_state(MouseButton::Left) == KeyState::Held
                || input.get_mouse_state(MouseButton::Left) == KeyState::Pressed;

            let slider = entities_and_components
                .get_components_mut::<(Slider,)>(entity)
                .0;

            if is_held {
                let dist_from_center_y = (mouse_position[1] - transform.y as f32).abs();

                if (mouse_position[0] >= slider.min_position
                    && mouse_position[0] <= slider.max_position
                    && dist_from_center_y <= slider.width / 2.0)
                    || slider.mouse_was_held
                {
                    let percentage = (mouse_position[0] - slider.min_position)
                        / (slider.max_position - slider.min_position);

                    slider.set_value(
                        slider.min_value + (slider.max_value - slider.min_value) * percentage,
                    );

                    slider.mouse_was_held = true;
                }
            } else {
                slider.mouse_was_held = false;
            }

            if slider.value_changed {
                slider.value_changed = false;
                let callback = slider.callback.clone();
                let value = slider.value;
                (callback)(entities_and_components, value);
            }

            let slider = entities_and_components
                .get_components::<(Slider,)>(entity)
                .0
                .clone();

            let knob_entity = slider.knob_entity.clone();
            let min_position = slider.min_position;
            let max_position = slider.max_position;

            if let Some(knob_entity) = knob_entity {
                let knob_x = min_position
                    + (max_position - min_position)
                        * ((slider.value - slider.min_value)
                            / (slider.max_value - slider.min_value));

                entities_and_components
                    .try_get_components_mut::<(Transform,)>(knob_entity)
                    .0
                    .expect("Failed to get knob transform")
                    .x = knob_x as f64;
            }
        }
    }
}

pub struct Button {
    callback: fn(&mut EntitiesAndComponents, bool),
    was_held: bool,
    width: f32,
    height: f32,
}

impl Button {
    pub fn new() -> Self {
        Button {
            callback: |_, _| {},
            was_held: false,
            width: 10.0,
            height: 10.0,
        }
    }

    pub fn set_callback(&mut self, callback: fn(&mut EntitiesAndComponents, bool)) {
        self.callback = callback;
    }

    pub fn with_callback(mut self, callback: fn(&mut EntitiesAndComponents, bool)) -> Self {
        self.callback = callback;
        self
    }

    pub fn get_callback(&self) -> fn(&mut EntitiesAndComponents, bool) {
        self.callback
    }

    pub fn set_width(&mut self, width: f32) {
        self.width = width;
    }

    pub fn set_height(&mut self, height: f32) {
        self.height = height;
    }

    pub fn with_width(mut self, width: f32) -> Self {
        self.width = width;
        self
    }

    pub fn with_height(mut self, height: f32) -> Self {
        self.height = height;
        self
    }

    pub fn get_width(&self) -> f32 {
        self.width
    }

    pub fn get_height(&self) -> f32 {
        self.height
    }
}

struct ButtonSystem;

impl System for ButtonSystem {
    fn run(&mut self, entities_and_components: &mut EntitiesAndComponents) {
        let entities_with_button = entities_and_components
            .get_entities_with_component::<Button>()
            .cloned()
            .collect::<Vec<Entity>>();

        for entity in entities_with_button {
            let transform = crate::get_transform(entity, entities_and_components);

            let input = entities_and_components
                .get_resource::<Input>()
                .expect("Failed to get input");

            let mouse_position = input.get_mouse_position();
            let is_held = input.get_mouse_state(MouseButton::Left) == KeyState::Held
                || input.get_mouse_state(MouseButton::Left) == KeyState::Pressed;

            let button = entities_and_components
                .get_components::<(Button,)>(entity)
                .0;

            let dist_from_center_x = (transform.x as f32 - mouse_position[0]).abs();
            let dist_from_center_y = (transform.y as f32 - mouse_position[1]).abs();

            if dist_from_center_x <= button.width / 2.0 && dist_from_center_y <= button.height / 2.0
            {
                if is_held && !button.was_held {
                    (button.callback)(entities_and_components, true);
                } else if button.was_held && !is_held {
                    (button.callback)(entities_and_components, false);
                }
            }

            let button = entities_and_components
                .get_components_mut::<(Button,)>(entity)
                .0;
            button.was_held = is_held;
        }
    }
}

#[derive(Clone, Copy)]
pub struct ScrollBar {
    min_bar_position: f32,
    max_bar_position: f32,
    min_content_position: f32,
    max_content_position: f32,
    width: f32,
    value: f32,
    callback: fn(&mut EntitiesAndComponents, f32),
    value_changed: bool,
    knob_entity: Option<Entity>,
    content_entity: Option<Entity>,
    mouse_was_held: bool,
    scroll_speed: f32,
}

impl ScrollBar {
    fn set_value(&mut self, value: f32) {
        self.value = value.clamp(self.min_bar_position, self.max_bar_position);
        self.value_changed = true;
    }

    pub fn new(
        min_bar_position: f32,
        max_bar_position: f32,
        min_content_position: f32,
        max_content_position: f32,
    ) -> Self {
        ScrollBar {
            min_bar_position,
            max_bar_position,
            min_content_position,
            max_content_position,
            width: max_bar_position - min_bar_position,
            value: min_bar_position,
            callback: |_, _| {},
            value_changed: false,
            knob_entity: None,
            content_entity: None,
            mouse_was_held: false,
            scroll_speed: 1.0,
        }
    }

    pub fn with_callback(mut self, callback: fn(&mut EntitiesAndComponents, f32)) -> Self {
        self.callback = callback;
        self
    }

    pub fn with_width(mut self, width: f32) -> Self {
        self.width = width;
        self
    }

    pub fn with_knob_entity(mut self, knob_entity: Entity) -> Self {
        self.knob_entity = Some(knob_entity);
        self
    }

    pub fn with_content_entity(mut self, content_entity: Entity) -> Self {
        self.content_entity = Some(content_entity);
        self
    }

    pub fn with_scroll_speed(mut self, scroll_speed: f32) -> Self {
        self.scroll_speed = scroll_speed;
        self
    }
}

struct ScrollBarSystem;

impl System for ScrollBarSystem {
    fn run(&mut self, entities_and_components: &mut EntitiesAndComponents) {
        let entities_with_scroll_bar = entities_and_components
            .get_entities_with_component::<ScrollBar>()
            .cloned()
            .collect::<Vec<Entity>>();

        for entity in entities_with_scroll_bar {
            let transform = crate::get_transform(entity, entities_and_components);

            let input = entities_and_components
                .get_resource::<Input>()
                .expect("Failed to get input");

            let mouse_position = input.get_mouse_position();

            let delta_scroll = input.get_mouse_wheel();

            let is_held = input.get_mouse_state(MouseButton::Left) == KeyState::Held
                || input.get_mouse_state(MouseButton::Left) == KeyState::Pressed;

            let scroll_bar = entities_and_components
                .get_components_mut::<(ScrollBar,)>(entity)
                .0;

            if delta_scroll.abs() > 0.01 {
                scroll_bar
                    .set_value(scroll_bar.value + delta_scroll as f32 * scroll_bar.scroll_speed);
            }

            if is_held {
                let dist_from_center_x = (transform.x as f32 - mouse_position[0]).abs();

                let is_in_bounds = mouse_position[1] >= scroll_bar.min_bar_position
                    && mouse_position[1] <= scroll_bar.max_bar_position
                    && dist_from_center_x <= scroll_bar.width / 2.0;

                if is_in_bounds || scroll_bar.mouse_was_held {
                    let percentage = (mouse_position[1] - scroll_bar.min_bar_position)
                        / (scroll_bar.max_bar_position - scroll_bar.min_bar_position);

                    scroll_bar.set_value(
                        scroll_bar.min_bar_position
                            + (scroll_bar.max_bar_position - scroll_bar.min_bar_position)
                                * percentage,
                    );

                    scroll_bar.mouse_was_held = true;
                }
            } else {
                scroll_bar.mouse_was_held = false;
            }

            if scroll_bar.value_changed {
                scroll_bar.value_changed = false;
                let callback = scroll_bar.callback.clone();
                let value = scroll_bar.value;
                (callback)(entities_and_components, value);
            }

            let scroll_bar = *entities_and_components
                .get_components::<(ScrollBar,)>(entity)
                .0;

            let knob_entity = scroll_bar.knob_entity.clone();
            let min_position = scroll_bar.min_bar_position;
            let max_position = scroll_bar.max_bar_position;

            if let Some(knob_entity) = knob_entity {
                let knob_y = min_position
                    + (max_position - min_position)
                        * ((scroll_bar.value - scroll_bar.min_bar_position)
                            / (scroll_bar.max_bar_position - scroll_bar.min_bar_position));

                entities_and_components
                    .try_get_components_mut::<(Transform,)>(knob_entity)
                    .0
                    .expect("Failed to get knob transform")
                    .y = knob_y as f64;
            }

            if let Some(content_entity) = scroll_bar.content_entity.clone() {
                let content_transform = entities_and_components
                    .try_get_components_mut::<(Transform,)>(content_entity)
                    .0
                    .expect("Failed to get content transform");

                let content_y = scroll_bar.min_content_position
                    + (scroll_bar.max_content_position - scroll_bar.min_content_position)
                        * ((scroll_bar.value - scroll_bar.min_bar_position)
                            / (scroll_bar.max_bar_position - scroll_bar.min_bar_position));

                content_transform.y = content_y as f64;
            }
        }
    }
}

pub(crate) fn add_all_ui_systems(world: &mut World) {
    // remove all ui systems to prevent duplicates
    world.remove_all_systems_of_type::<SliderSystem>();
    world.remove_all_systems_of_type::<ButtonSystem>();
    world.remove_all_systems_of_type::<ScrollBarSystem>();

    world.add_system(SliderSystem {});
    world.add_system(ButtonSystem {});
    world.add_system(ScrollBarSystem {});
}