sevenx_engine 0.2.11

Engine de jogos 2D/3D completa com suporte Android, física, áudio, partículas, tilemap, UI, eventos e sistema 3D avançado com PBR.
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Sistema avançado de UI
use crate::primitives2d::Primitives2D;
use crate::input::InputHandler;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WidgetState {
    Normal,
    Hovered,
    Pressed,
    Disabled,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UITheme {
    pub primary_color: [u8; 4],
    pub secondary_color: [u8; 4],
    pub background_color: [u8; 4],
    pub text_color: [u8; 4],
    pub border_color: [u8; 4],
    pub hover_color: [u8; 4],
    pub pressed_color: [u8; 4],
    pub disabled_color: [u8; 4],
    pub border_width: u32,
    pub padding: u32,
    pub corner_radius: u32,
}

impl UITheme {
    pub fn dark() -> Self {
        Self {
            primary_color: [70, 130, 180, 255],
            secondary_color: [100, 149, 237, 255],
            background_color: [30, 30, 40, 255],
            text_color: [255, 255, 255, 255],
            border_color: [100, 100, 120, 255],
            hover_color: [90, 150, 200, 255],
            pressed_color: [50, 100, 150, 255],
            disabled_color: [80, 80, 90, 255],
            border_width: 2,
            padding: 8,
            corner_radius: 4,
        }
    }

    pub fn light() -> Self {
        Self {
            primary_color: [100, 149, 237, 255],
            secondary_color: [135, 206, 250, 255],
            background_color: [240, 240, 245, 255],
            text_color: [30, 30, 30, 255],
            border_color: [180, 180, 190, 255],
            hover_color: [120, 169, 255, 255],
            pressed_color: [80, 129, 217, 255],
            disabled_color: [200, 200, 210, 255],
            border_width: 2,
            padding: 8,
            corner_radius: 4,
        }
    }

    pub fn neon() -> Self {
        Self {
            primary_color: [255, 0, 255, 255],
            secondary_color: [0, 255, 255, 255],
            background_color: [10, 10, 20, 255],
            text_color: [255, 255, 255, 255],
            border_color: [255, 0, 255, 255],
            hover_color: [255, 100, 255, 255],
            pressed_color: [200, 0, 200, 255],
            disabled_color: [50, 50, 60, 255],
            border_width: 3,
            padding: 10,
            corner_radius: 8,
        }
    }
}

pub struct Button {
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
    pub text: String,
    pub state: WidgetState,
    pub enabled: bool,
    pub visible: bool,
}

impl Button {
    pub fn new(x: i32, y: i32, width: u32, height: u32, text: &str) -> Self {
        Self {
            x,
            y,
            width,
            height,
            text: text.to_string(),
            state: WidgetState::Normal,
            enabled: true,
            visible: true,
        }
    }

    pub fn update(&mut self, input: &InputHandler) -> bool {
        if !self.enabled || !self.visible {
            self.state = WidgetState::Disabled;
            return false;
        }

        let (mx, my) = input.get_mouse_position();
        let mx = mx as i32;
        let my = my as i32;

        let hovered = mx >= self.x
            && mx <= self.x + self.width as i32
            && my >= self.y
            && my <= self.y + self.height as i32;

        if hovered {
            if input.is_mouse_button_pressed(crate::input::MouseBtn::Left) {
                self.state = WidgetState::Pressed;
            } else {
                self.state = WidgetState::Hovered;
            }

            if input.is_mouse_button_just_released(crate::input::MouseBtn::Left) {
                return true; // Clicked!
            }
        } else {
            self.state = WidgetState::Normal;
        }

        false
    }

    pub fn draw(&self, prims: &mut Primitives2D, theme: &UITheme) {
        if !self.visible {
            return;
        }

        let color = match self.state {
            WidgetState::Normal => theme.primary_color,
            WidgetState::Hovered => theme.hover_color,
            WidgetState::Pressed => theme.pressed_color,
            WidgetState::Disabled => theme.disabled_color,
        };

        // Fundo
        prims.draw_rect_filled(self.x, self.y, self.width, self.height, color);

        // Borda
        prims.draw_rect(self.x, self.y, self.width, self.height, theme.border_color);

        // Texto seria renderizado aqui
    }
}

pub struct Slider {
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
    pub value: f32,
    pub min: f32,
    pub max: f32,
    pub dragging: bool,
    pub enabled: bool,
    pub visible: bool,
}

impl Slider {
    pub fn new(x: i32, y: i32, width: u32, min: f32, max: f32) -> Self {
        Self {
            x,
            y,
            width,
            height: 20,
            value: (min + max) / 2.0,
            min,
            max,
            dragging: false,
            enabled: true,
            visible: true,
        }
    }

    pub fn update(&mut self, input: &InputHandler) {
        if !self.enabled || !self.visible {
            return;
        }

        let (mx, my) = input.get_mouse_position();
        let mx = mx as i32;
        let my = my as i32;

        let hovered = mx >= self.x
            && mx <= self.x + self.width as i32
            && my >= self.y
            && my <= self.y + self.height as i32;

        if input.is_mouse_button_pressed(crate::input::MouseBtn::Left) && hovered {
            self.dragging = true;
        }

        if !input.is_mouse_button_pressed(crate::input::MouseBtn::Left) {
            self.dragging = false;
        }

        if self.dragging {
            let relative_x = (mx - self.x).max(0).min(self.width as i32);
            let t = relative_x as f32 / self.width as f32;
            self.value = self.min + t * (self.max - self.min);
        }
    }

    pub fn draw(&self, prims: &mut Primitives2D, theme: &UITheme) {
        if !self.visible {
            return;
        }

        // Track
        prims.draw_rect_filled(
            self.x,
            self.y + self.height as i32 / 4,
            self.width,
            self.height / 2,
            theme.background_color,
        );

        // Handle
        let t = (self.value - self.min) / (self.max - self.min);
        let handle_x = self.x + (t * self.width as f32) as i32;
        let handle_size = self.height;

        let color = if self.dragging {
            theme.pressed_color
        } else {
            theme.primary_color
        };

        prims.draw_circle_filled(handle_x, self.y + self.height as i32 / 2, handle_size as i32 / 2, color);
    }

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

    pub fn set_value(&mut self, value: f32) {
        self.value = value.clamp(self.min, self.max);
    }
}

pub struct Checkbox {
    pub x: i32,
    pub y: i32,
    pub size: u32,
    pub checked: bool,
    pub enabled: bool,
    pub visible: bool,
}

impl Checkbox {
    pub fn new(x: i32, y: i32, size: u32) -> Self {
        Self {
            x,
            y,
            size,
            checked: false,
            enabled: true,
            visible: true,
        }
    }

    pub fn update(&mut self, input: &InputHandler) -> bool {
        if !self.enabled || !self.visible {
            return false;
        }

        let (mx, my) = input.get_mouse_position();
        let mx = mx as i32;
        let my = my as i32;

        let hovered = mx >= self.x
            && mx <= self.x + self.size as i32
            && my >= self.y
            && my <= self.y + self.size as i32;

        if hovered && input.is_mouse_button_just_pressed(crate::input::MouseBtn::Left) {
            self.checked = !self.checked;
            return true;
        }

        false
    }

    pub fn draw(&self, prims: &mut Primitives2D, theme: &UITheme) {
        if !self.visible {
            return;
        }

        // Box
        prims.draw_rect_filled(self.x, self.y, self.size, self.size, theme.background_color);
        prims.draw_rect(self.x, self.y, self.size, self.size, theme.border_color);

        // Check mark
        if self.checked {
            let padding = self.size / 4;
            prims.draw_rect_filled(
                self.x + padding as i32,
                self.y + padding as i32,
                self.size - padding * 2,
                self.size - padding * 2,
                theme.primary_color,
            );
        }
    }
}

pub struct Panel {
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
    pub title: String,
    pub visible: bool,
    pub draggable: bool,
    pub dragging: bool,
    pub drag_offset: (i32, i32),
}

impl Panel {
    pub fn new(x: i32, y: i32, width: u32, height: u32, title: &str) -> Self {
        Self {
            x,
            y,
            width,
            height,
            title: title.to_string(),
            visible: true,
            draggable: true,
            dragging: false,
            drag_offset: (0, 0),
        }
    }

    pub fn update(&mut self, input: &InputHandler) {
        if !self.visible || !self.draggable {
            return;
        }

        let (mx, my) = input.get_mouse_position();
        let mx = mx as i32;
        let my = my as i32;

        let title_bar_height = 30;
        let title_bar_hovered = mx >= self.x
            && mx <= self.x + self.width as i32
            && my >= self.y
            && my <= self.y + title_bar_height;

        if title_bar_hovered && input.is_mouse_button_just_pressed(crate::input::MouseBtn::Left) {
            self.dragging = true;
            self.drag_offset = (mx - self.x, my - self.y);
        }

        if !input.is_mouse_button_pressed(crate::input::MouseBtn::Left) {
            self.dragging = false;
        }

        if self.dragging {
            self.x = mx - self.drag_offset.0;
            self.y = my - self.drag_offset.1;
        }
    }

    pub fn draw(&self, prims: &mut Primitives2D, theme: &UITheme) {
        if !self.visible {
            return;
        }

        let title_bar_height = 30;

        // Title bar
        prims.draw_rect_filled(self.x, self.y, self.width, title_bar_height, theme.primary_color);

        // Body
        prims.draw_rect_filled(
            self.x,
            self.y + title_bar_height as i32,
            self.width,
            self.height - title_bar_height,
            theme.background_color,
        );

        // Border
        prims.draw_rect(self.x, self.y, self.width, self.height, theme.border_color);
    }
}

pub struct ProgressBar {
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
    pub value: f32,
    pub max: f32,
    pub visible: bool,
}

impl ProgressBar {
    pub fn new(x: i32, y: i32, width: u32, height: u32, max: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
            value: 0.0,
            max,
            visible: true,
        }
    }

    pub fn set_value(&mut self, value: f32) {
        self.value = value.clamp(0.0, self.max);
    }

    pub fn draw(&self, prims: &mut Primitives2D, theme: &UITheme) {
        if !self.visible {
            return;
        }

        // Background
        prims.draw_rect_filled(self.x, self.y, self.width, self.height, theme.background_color);

        // Progress
        let progress_width = (self.width as f32 * (self.value / self.max)) as u32;
        prims.draw_rect_filled(self.x, self.y, progress_width, self.height, theme.primary_color);

        // Border
        prims.draw_rect(self.x, self.y, self.width, self.height, theme.border_color);
    }
}

pub struct Label {
    pub x: i32,
    pub y: i32,
    pub text: String,
    pub color: [u8; 4],
    pub visible: bool,
}

impl Label {
    pub fn new(x: i32, y: i32, text: &str) -> Self {
        Self {
            x,
            y,
            text: text.to_string(),
            color: [255, 255, 255, 255],
            visible: true,
        }
    }

    pub fn draw(&self, _prims: &mut Primitives2D, _theme: &UITheme) {
        if !self.visible {
            return;
        }
        // Texto seria renderizado aqui
    }
}

// Gerenciador de UI
pub struct UIManager {
    pub theme: UITheme,
    pub buttons: Vec<Button>,
    pub sliders: Vec<Slider>,
    pub checkboxes: Vec<Checkbox>,
    pub panels: Vec<Panel>,
    pub progress_bars: Vec<ProgressBar>,
    pub labels: Vec<Label>,
}

impl UIManager {
    pub fn new(theme: UITheme) -> Self {
        Self {
            theme,
            buttons: Vec::new(),
            sliders: Vec::new(),
            checkboxes: Vec::new(),
            panels: Vec::new(),
            progress_bars: Vec::new(),
            labels: Vec::new(),
        }
    }

    pub fn add_button(&mut self, button: Button) -> usize {
        self.buttons.push(button);
        self.buttons.len() - 1
    }

    pub fn add_slider(&mut self, slider: Slider) -> usize {
        self.sliders.push(slider);
        self.sliders.len() - 1
    }

    pub fn add_checkbox(&mut self, checkbox: Checkbox) -> usize {
        self.checkboxes.push(checkbox);
        self.checkboxes.len() - 1
    }

    pub fn add_panel(&mut self, panel: Panel) -> usize {
        self.panels.push(panel);
        self.panels.len() - 1
    }

    pub fn add_progress_bar(&mut self, bar: ProgressBar) -> usize {
        self.progress_bars.push(bar);
        self.progress_bars.len() - 1
    }

    pub fn add_label(&mut self, label: Label) -> usize {
        self.labels.push(label);
        self.labels.len() - 1
    }

    pub fn update(&mut self, input: &InputHandler) -> Vec<UIEvent> {
        let mut events = Vec::new();

        // Update panels first (for dragging)
        for panel in self.panels.iter_mut() {
            panel.update(input);
        }

        // Update buttons
        for (i, button) in self.buttons.iter_mut().enumerate() {
            if button.update(input) {
                events.push(UIEvent::ButtonClicked(i));
            }
        }

        // Update sliders
        for (i, slider) in self.sliders.iter_mut().enumerate() {
            let old_value = slider.value;
            slider.update(input);
            if (slider.value - old_value).abs() > 0.001 {
                events.push(UIEvent::SliderChanged(i, slider.value));
            }
        }

        // Update checkboxes
        for (i, checkbox) in self.checkboxes.iter_mut().enumerate() {
            if checkbox.update(input) {
                events.push(UIEvent::CheckboxToggled(i, checkbox.checked));
            }
        }

        events
    }

    pub fn draw(&self, prims: &mut Primitives2D) {
        // Draw panels
        for panel in &self.panels {
            panel.draw(prims, &self.theme);
        }

        // Draw progress bars
        for bar in &self.progress_bars {
            bar.draw(prims, &self.theme);
        }

        // Draw buttons
        for button in &self.buttons {
            button.draw(prims, &self.theme);
        }

        // Draw sliders
        for slider in &self.sliders {
            slider.draw(prims, &self.theme);
        }

        // Draw checkboxes
        for checkbox in &self.checkboxes {
            checkbox.draw(prims, &self.theme);
        }

        // Draw labels
        for label in &self.labels {
            label.draw(prims, &self.theme);
        }
    }

    pub fn set_theme(&mut self, theme: UITheme) {
        self.theme = theme;
    }
}

#[derive(Debug, Clone)]
pub enum UIEvent {
    ButtonClicked(usize),
    SliderChanged(usize, f32),
    CheckboxToggled(usize, bool),
}