/* This file is part of DarkFi (https://dark.fi) * * Copyright (C) 2020-2025 Dyne.org foundation * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ use async_trait::async_trait; use rand::{rngs::OsRng, Rng}; use std::sync::{Arc, Mutex as SyncMutex, OnceLock, Weak}; use crate::{ gfx::{ GfxDrawCall, GfxDrawInstruction, GfxDrawMesh, GfxTextureId, ManagedTexturePtr, Rectangle, RenderApi, }, mesh::{Color, MeshBuilder, MeshInfo, COLOR_BLUE, COLOR_RED, COLOR_WHITE}, prop::{ PropertyAtomicGuard, PropertyBool, PropertyColor, PropertyFloat32, PropertyPtr, PropertyRect, PropertyStr, PropertyUint32, Role, }, scene::{Pimpl, SceneNodePtr, SceneNodeWeak}, text::{self, GlyphPositionIter, TextShaper, TextShaperPtr}, text2, util::unixtime, ExecutorPtr, }; use super::{DrawUpdate, OnModify, UIObject}; macro_rules! d { ($($arg:tt)*) => { debug!(target: "ui::text", $($arg)*); } } macro_rules! t { ($($arg:tt)*) => { trace!(target: "ui::text", $($arg)*); } } pub type TextPtr = Arc; #[derive(Clone)] struct TextRenderInfo { mesh: MeshInfo, texture: ManagedTexturePtr, } pub struct Text { node: SceneNodeWeak, render_api: RenderApi, text_shaper: TextShaperPtr, tasks: OnceLock>>, dc_key: u64, rect: PropertyRect, z_index: PropertyUint32, priority: PropertyUint32, text: PropertyStr, font_size: PropertyFloat32, text_color: PropertyColor, baseline: PropertyFloat32, debug: PropertyBool, window_scale: PropertyFloat32, parent_rect: SyncMutex>, } impl Text { pub async fn new( node: SceneNodeWeak, window_scale: PropertyFloat32, render_api: RenderApi, text_shaper: TextShaperPtr, ex: ExecutorPtr, ) -> Pimpl { t!("Text::new()"); let node_ref = &node.upgrade().unwrap(); let rect = PropertyRect::wrap(node_ref, Role::Internal, "rect").unwrap(); let z_index = PropertyUint32::wrap(node_ref, Role::Internal, "z_index", 0).unwrap(); let priority = PropertyUint32::wrap(node_ref, Role::Internal, "priority", 0).unwrap(); let text = PropertyStr::wrap(node_ref, Role::Internal, "text", 0).unwrap(); let font_size = PropertyFloat32::wrap(node_ref, Role::Internal, "font_size", 0).unwrap(); let text_color = PropertyColor::wrap(node_ref, Role::Internal, "text_color").unwrap(); let baseline = PropertyFloat32::wrap(node_ref, Role::Internal, "baseline", 0).unwrap(); let debug = PropertyBool::wrap(node_ref, Role::Internal, "debug", 0).unwrap(); let node_name = node_ref.name.clone(); let node_id = node_ref.id; let self_ = Arc::new(Self { node, render_api, text_shaper, tasks: OnceLock::new(), dc_key: OsRng.gen(), rect, z_index, priority, text, font_size, text_color, baseline, debug, window_scale, parent_rect: SyncMutex::new(None), }); Pimpl::Text(self_) } async fn regen_mesh(&self) -> Vec { let text = self.text.get(); let font_size = self.font_size.get(); let text_color = self.text_color.get(); let window_scale = self.window_scale.get(); let layout = { let mut txt_ctx = text2::get_ctx().await; txt_ctx.make_layout(&text, text_color, font_size, 0., window_scale, None) }; text2::render_layout(&layout, &self.render_api) } async fn redraw(self: Arc) { let trace_id = rand::random(); let timest = unixtime(); t!("Text::redraw({:?}) [trace_id={trace_id}]", self.node.upgrade().unwrap()); let Some(parent_rect) = self.parent_rect.lock().unwrap().clone() else { return }; let Some(draw_update) = self.get_draw_calls(parent_rect, trace_id).await else { error!(target: "ui::text", "Text failed to draw [trace_id={trace_id}]"); return; }; self.render_api.replace_draw_calls(timest, draw_update.draw_calls); t!("Text::redraw() DONE [trace_id={trace_id}]"); } async fn get_draw_calls(&self, parent_rect: Rectangle, trace_id: u32) -> Option { self.rect.eval(&parent_rect).ok()?; let rect = self.rect.get(); let mut instrs = vec![GfxDrawInstruction::Move(rect.pos())]; instrs.append(&mut self.regen_mesh().await); Some(DrawUpdate { key: self.dc_key, draw_calls: vec![( self.dc_key, GfxDrawCall { instrs, dcs: vec![], z_index: self.z_index.get() }, )], }) } } #[async_trait] impl UIObject for Text { fn priority(&self) -> u32 { self.priority.get() } async fn start(self: Arc, ex: ExecutorPtr) { let me = Arc::downgrade(&self); let mut on_modify = OnModify::new(ex, self.node.clone(), me.clone()); on_modify.when_change(self.rect.prop(), Self::redraw); on_modify.when_change(self.z_index.prop(), Self::redraw); on_modify.when_change(self.text.prop(), Self::redraw); on_modify.when_change(self.font_size.prop(), Self::redraw); on_modify.when_change(self.text_color.prop(), Self::redraw); on_modify.when_change(self.debug.prop(), Self::redraw); on_modify.when_change(self.baseline.prop(), Self::redraw); self.tasks.set(on_modify.tasks); } async fn draw( &self, parent_rect: Rectangle, trace_id: u32, atom: &mut PropertyAtomicGuard, ) -> Option { t!("Text::draw({:?}) [trace_id={trace_id}]", self.node.upgrade().unwrap()); *self.parent_rect.lock().unwrap() = Some(parent_rect); self.get_draw_calls(parent_rect, trace_id).await } } impl Drop for Text { fn drop(&mut self) { self.render_api.replace_draw_calls(unixtime(), vec![(self.dc_key, Default::default())]); } }