ui

source of hud

1// What `Hud` runs (F15.md): a Ui made, given events and drawn as an2// app's is, with the engine's window and device lent to each call.34import std::string;5import std::vector;6import std::option;7import std::time;8import std::sync;9import std::languages as system;10import pkg::alloy::window;11import pkg::alloy::gpu;12import pkg::alloy::draw;1314import ui;15import store;16import painting;17import events;18import timers;19import motion;20import running;21import languages;2223pub fn open_hud(24    device: &var Device,25    format: TextureFormat,26    window: &Window,27    view: |=| (&var Ui) -> Element28) -> Hud {29    var ui = Ui::create();30    ui.choose_language(&[], &system::preferred_languages().view());31    ui.time = window.now();32    ui.wall = wall_clock();33    ui.scale = window.scale();34    ui.start_path = String::from(&window.start_path());35    ui.app = String::from(&window.app_name());36    var canvas = Canvas::create(&var device, format);37    canvas.set_background(Color::rgba(0.0, 0.0, 0.0, 0.0));38    ui.use_scene(Scene {39        .own = ::None,40        .lent = ::Some(&var device),41        .surface = ::None,42        .canvas = move canvas,43        .target = ::None,44        .over = true45    });46    const root = ui.untracked(&view);47    const size = window.size();48    ui.mount(root, size.width to f32 / ui.scale, size.height to f32 / ui.scale);49    // laid out at once, so the first events find its elements50    ui.update_all();51    ui.lend(::None);52    return Hud { .ui = move ui };53}5455pub fn take_event(self hud: &var Hud, window: &var Window, event: &Event) -> bool {56    const ui = &var hud.ui;57    ui.time = if (event is ::Redraw |t|) t else window.now();58    ui.wall = wall_clock();59    if (!hud.waking && ui.workers is ::Some |&workers|) {60        hud.waking = true;61        var waker = workers.waker.lock();62        waker.value() = window.waker();63        waker.dispose();64    }65    const taken = ui.takes(&event);66    ui.begin_event(&window, &event);67    match (event) {68        ::Redraw, ::CloseRequested {}69        ::Woken {70            ui.take_done();71        }72        else {73            ui.input_event(&var window, &event);74        }75    }76    ui.end_event(&var window);77    ui.send_jobs();78    if (ui.quitting) {79        ui.close_window(&var window);80    }81    return taken;82}8384pub fn draw_over(85    self hud: &var Hud,86    device: &var Device,87    encoder: &var Encoder,88    frame: Texture,89    window: &var Window90) {91    const ui = &var hud.ui;92    ui.time = window.now();93    ui.wall = wall_clock();94    ui.lend(::Some(&var device));95    ui.run_timers();96    ui.frame();97    if (ui.scene is ::Some |&var scene|) {98        // pictures freed while no device was lent99        for (scene.unreleased.view()) |p| {100            scene.canvas.release_picture(&var device, p);101        }102        scene.unreleased.clear();103        scene.canvas.render_over(&var device, &var encoder, frame);104    }105    ui.lend(::None);106    ui.handled();107    ui.show_in(&var window);108    ui.send_jobs();109    if (ui.quitting) {110        ui.close_window(&var window);111    }112}113114// Lends the scene the engine's device while the HUD draws, or takes it115// back.116fn lend(self ui: &var Ui, device: Option<&var Device>) {117    if (ui.scene is ::Some |&var scene|) {118        scene.lent = device;119    }120}121122// Whether the HUD takes the event from the game, judged before it is123// handled.124fn takes(self ui: &Ui, event: &Event) -> bool {125    match (event) {126        ::PointerMoved |p| {127            return ui.pressed is ::Some || ui.interactive_at(logical(p, ui.scale));128        }129        ::Button |&b| {130            return ui.pressed is ::Some || ui.interactive_at(logical(b.position, ui.scale));131        }132        ::Scroll {133            if (ui.pointer is ::Some |at|) {134                return ui.interactive_at(at);135            }136            return false;137        }138        ::Key, ::Text, ::Preedit {139            return ui.input_area() is ::Some;140        }141        else {142            return false;143        }144    }145}146147fn interactive_at(self ui: &Ui, at: Point) -> bool => ui.hit_at(at, Hit::Interactive) is ::Some;