~starkingdoms/starkingdoms

ref: efb107d0e34ca6c555672d3ce690b4bc6a2b52c0 starkingdoms/crates/client/src/ui/mod.rs -rw-r--r-- 3.1 KiB
efb107d0 — ghostly_zsh merge rust-rendering-2 to master 8 months ago
                                                                                
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
mod colors;
mod widgets;

use bevy_ecs::prelude::With;
use bevy_ecs::world::World;
use egui::{Align, Align2, CornerRadius, CursorIcon, Layout, Order, ProgressBar, RichText, Shadow, Stroke, Visuals};
use crate::components::{Player, PlayerResources, Transform};
use crate::ui::widgets::{progress_bar, RichTextExt};

pub fn init_ui(ctx: egui::Context) {
    // set colors
    let mut style = egui::Style::default();

    let mut visuals = Visuals::default();
    visuals.dark_mode = true;


    // TODO(core): code_bg_colors

    visuals.warn_fg_color = colors::YELLOW; // used for warning text
    visuals.error_fg_color = colors::RED; // used for error text

    visuals.window_shadow = Shadow::NONE;
    visuals.window_fill = colors::MANTLE; // window background color
    visuals.extreme_bg_color = colors::CRUST; // must be darker than window_fill
    visuals.faint_bg_color = colors::BASE; // slightly different than window background color
    visuals.window_stroke.color = colors::SURFACE_0;
    visuals.window_highlight_topmost = false;

    visuals.text_cursor.stroke.color = colors::ROSEWATER;

    // TODO(core): button_frame, collapsing_header_frame
    visuals.striped = true;

    visuals.interact_cursor = Some(CursorIcon::PointingHand);

    visuals.selection.bg_fill = colors::OVERLAY_2.gamma_multiply(0.2); // selection background

    visuals.hyperlink_color = colors::BLUE;



    visuals.widgets.noninteractive.fg_stroke.color = colors::TEXT; // standard text color


    style.visuals = visuals;

    ctx.set_style(style);
}

pub fn draw_ui(ctx: &egui::Context, world: &mut World) {
    draw_hud(ctx, world);
}

pub fn draw_hud(ctx: &egui::Context, world: &mut World) {
    let mut player = world.query_filtered::<&Transform, With<Player>>();
    let player_position = player.single(&world);
    let player_resources = world.resource::<PlayerResources>();

    egui::Window::new("hud")
        .title_bar(false)
        .movable(false)
        .resizable(false)
        .order(Order::Foreground)
        .anchor(Align2::CENTER_BOTTOM, [0.0, -5.0])
        .show(ctx, |ui| {

            ui.horizontal(|ui| {
                ui.vertical(|ui| {
                    ui.label(RichText::new("Position:").stk_weak());
                    ui.label(RichText::new(format!("{:.0}, {:.0}", player_position.translation.x / 10.0, player_position.translation.y / 10.0)))
                });

                ui.add_space(8.0);

                ui.vertical(|ui| {
                    ui.horizontal(|ui| {
                        ui.label(RichText::new("Fuel:").stk_weak());
                        ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
                            ui.label(RichText::new(format!("{}/{} ({:.2}%)", player_resources.fuel_amount, player_resources.fuel_max, player_resources.fuel_amount as f32 / player_resources.fuel_max as f32 * 100.0)))
                        });

                    });

                    ui.add(
                        progress_bar(player_resources.fuel_amount as f32 / player_resources.fuel_max as f32)
                    );
                })
            });
        });
}