mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
wallet: add EditBox
This commit is contained in:
@@ -297,7 +297,7 @@ impl App {
|
||||
node.set_property_f32("font_size", 60.).unwrap();
|
||||
node.set_property_str("text", "anon1🍆").unwrap();
|
||||
//node.set_property_str("text", "anon1").unwrap();
|
||||
let prop = node.get_property("color").unwrap();
|
||||
let prop = node.get_property("text_color").unwrap();
|
||||
prop.set_f32(0, 0.).unwrap();
|
||||
prop.set_f32(1, 1.).unwrap();
|
||||
prop.set_f32(2, 0.).unwrap();
|
||||
@@ -324,11 +324,15 @@ impl App {
|
||||
node.set_property_bool("is_active", true).unwrap();
|
||||
let prop = node.get_property("rect").unwrap();
|
||||
prop.set_f32(0, 150.).unwrap();
|
||||
let code =
|
||||
vec![Op::Sub((Box::new(Op::LoadVar("lh".to_string())), Box::new(Op::ConstFloat32(60.))))];
|
||||
let code = vec![Op::Sub((
|
||||
Box::new(Op::LoadVar("h".to_string())),
|
||||
Box::new(Op::ConstFloat32(60.)),
|
||||
))];
|
||||
prop.set_expr(1, code).unwrap();
|
||||
let code =
|
||||
vec![Op::Sub((Box::new(Op::LoadVar("lw".to_string())), Box::new(Op::ConstFloat32(120.))))];
|
||||
let code = vec![Op::Sub((
|
||||
Box::new(Op::LoadVar("w".to_string())),
|
||||
Box::new(Op::ConstFloat32(120.)),
|
||||
))];
|
||||
prop.set_expr(2, code).unwrap();
|
||||
prop.set_f32(3, 60.).unwrap();
|
||||
node.set_property_f32("baseline", 40.).unwrap();
|
||||
@@ -356,11 +360,12 @@ impl App {
|
||||
|
||||
drop(sg);
|
||||
let pimpl = EditBox::new(
|
||||
//self.ex.clone(),
|
||||
//self.sg.clone(),
|
||||
//node_id,
|
||||
//self.render_api.clone(),
|
||||
//self.text_shaper.clone(),
|
||||
self.ex.clone(),
|
||||
self.sg.clone(),
|
||||
node_id,
|
||||
self.render_api.clone(),
|
||||
self.event_pub.clone(),
|
||||
self.text_shaper.clone(),
|
||||
)
|
||||
.await;
|
||||
let mut sg = self.sg.lock().await;
|
||||
@@ -424,7 +429,7 @@ fn create_text(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
|
||||
let prop = Property::new("text", PropertyType::Str, PropertySubType::Null);
|
||||
node.add_property(prop).unwrap();
|
||||
|
||||
let mut prop = Property::new("color", PropertyType::Float32, PropertySubType::Color);
|
||||
let mut prop = Property::new("text_color", PropertyType::Float32, PropertySubType::Color);
|
||||
prop.set_array_len(4);
|
||||
prop.set_range_f32(0., 1.);
|
||||
node.add_property(prop).unwrap();
|
||||
|
||||
@@ -304,6 +304,10 @@ pub struct GraphicsEventPublisher {
|
||||
lock_key_down: SyncMutex<Option<SubscriptionId>>,
|
||||
key_down: PublisherPtr<(KeyCode, KeyMods, bool)>,
|
||||
|
||||
lock_key_up: SyncMutex<Option<SubscriptionId>>,
|
||||
key_up: PublisherPtr<(KeyCode, KeyMods)>,
|
||||
|
||||
lock_resize: SyncMutex<Option<SubscriptionId>>,
|
||||
resize: PublisherPtr<(f32, f32)>,
|
||||
}
|
||||
|
||||
@@ -312,7 +316,11 @@ impl GraphicsEventPublisher {
|
||||
Arc::new(Self {
|
||||
lock_key_down: SyncMutex::new(None),
|
||||
key_down: Publisher::new(),
|
||||
resize: Publisher::new() })
|
||||
lock_key_up: SyncMutex::new(None),
|
||||
key_up: Publisher::new(),
|
||||
lock_resize: SyncMutex::new(None),
|
||||
resize: Publisher::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn lock_key_down(&self, sub_id: SubscriptionId) {
|
||||
@@ -322,6 +330,20 @@ impl GraphicsEventPublisher {
|
||||
*self.lock_key_down.lock().unwrap() = None;
|
||||
}
|
||||
|
||||
fn lock_key_up(&self, sub_id: SubscriptionId) {
|
||||
*self.lock_key_up.lock().unwrap() = Some(sub_id);
|
||||
}
|
||||
fn unlock_key_up(&self) {
|
||||
*self.lock_key_up.lock().unwrap() = None;
|
||||
}
|
||||
|
||||
fn lock_resize(&self, sub_id: SubscriptionId) {
|
||||
*self.lock_resize.lock().unwrap() = Some(sub_id);
|
||||
}
|
||||
fn unlock_resize(&self) {
|
||||
*self.lock_resize.lock().unwrap() = None;
|
||||
}
|
||||
|
||||
fn notify_key_down(&self, key: KeyCode, mods: KeyMods, repeat: bool) {
|
||||
let ev = (key, mods, repeat);
|
||||
|
||||
@@ -332,13 +354,33 @@ impl GraphicsEventPublisher {
|
||||
self.key_down.notify(ev);
|
||||
}
|
||||
}
|
||||
fn notify_key_up(&self, key: KeyCode, mods: KeyMods) {
|
||||
let ev = (key, mods);
|
||||
|
||||
let locked = self.lock_key_up.lock().unwrap().clone();
|
||||
if let Some(locked) = locked {
|
||||
self.key_up.notify_with_include(ev, &[locked]);
|
||||
} else {
|
||||
self.key_up.notify(ev);
|
||||
}
|
||||
}
|
||||
fn notify_resize(&self, w: f32, h: f32) {
|
||||
self.resize.notify((w, h));
|
||||
let ev = (w, h);
|
||||
|
||||
let locked = self.lock_resize.lock().unwrap().clone();
|
||||
if let Some(locked) = locked {
|
||||
self.resize.notify_with_include(ev, &[locked]);
|
||||
} else {
|
||||
self.resize.notify(ev);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscribe_key_down(&self) -> Subscription<(KeyCode, KeyMods, bool)> {
|
||||
self.key_down.clone().subscribe()
|
||||
}
|
||||
pub fn subscribe_key_up(&self) -> Subscription<(KeyCode, KeyMods)> {
|
||||
self.key_up.clone().subscribe()
|
||||
}
|
||||
pub fn subscribe_resize(&self) -> Subscription<(f32, f32)> {
|
||||
self.resize.clone().subscribe()
|
||||
}
|
||||
@@ -432,15 +474,15 @@ impl Stage {
|
||||
sendr: async_channel::Sender<TextureId>,
|
||||
) {
|
||||
let texture = self.ctx.new_texture_from_rgba8(width, height, &data);
|
||||
debug!(target: "gfx2", "Invoked method: new_texture({}, {}, ...) -> {:?}",
|
||||
width, height, texture);
|
||||
//debug!(target: "gfx2", "Invoked method: new_texture({}, {}, ...) -> {:?}",
|
||||
// width, height, texture);
|
||||
//debug!(target: "gfx2", "Invoked method: new_texture({}, {}, ...) -> {:?}\n{}",
|
||||
// width, height, texture,
|
||||
// ansi_texture(width as usize, height as usize, &data));
|
||||
sendr.try_send(texture).unwrap();
|
||||
}
|
||||
fn method_delete_texture(&mut self, texture: TextureId) {
|
||||
debug!(target: "gfx2", "Invoked method: delete_texture({:?})", texture);
|
||||
//debug!(target: "gfx2", "Invoked method: delete_texture({:?})", texture);
|
||||
self.ctx.delete_texture(texture);
|
||||
}
|
||||
fn method_new_vertex_buffer(
|
||||
@@ -453,7 +495,7 @@ impl Stage {
|
||||
BufferUsage::Immutable,
|
||||
BufferSource::slice(&verts),
|
||||
);
|
||||
debug!(target: "gfx2", "Invoked method: new_vertex_buffer({:?}) -> {:?}", verts, buffer);
|
||||
//debug!(target: "gfx2", "Invoked method: new_vertex_buffer({:?}) -> {:?}", verts, buffer);
|
||||
sendr.try_send(buffer).unwrap();
|
||||
}
|
||||
fn method_new_index_buffer(
|
||||
@@ -466,15 +508,15 @@ impl Stage {
|
||||
BufferUsage::Immutable,
|
||||
BufferSource::slice(&indices),
|
||||
);
|
||||
debug!(target: "gfx2", "Invoked method: new_index_buffer({:?}) -> {:?}", indices, buffer);
|
||||
//debug!(target: "gfx2", "Invoked method: new_index_buffer({:?}) -> {:?}", indices, buffer);
|
||||
sendr.try_send(buffer).unwrap();
|
||||
}
|
||||
fn method_delete_buffer(&mut self, buffer: BufferId) {
|
||||
debug!(target: "gfx2", "Invoked method: delete_buffer({:?})", buffer);
|
||||
//debug!(target: "gfx2", "Invoked method: delete_buffer({:?})", buffer);
|
||||
self.ctx.delete_buffer(buffer);
|
||||
}
|
||||
fn method_replace_draw_calls(&mut self, dcs: Vec<(u64, DrawCall)>) {
|
||||
debug!(target: "gfx2", "Invoked method: replace_draw_calls({:?})", dcs);
|
||||
//debug!(target: "gfx2", "Invoked method: replace_draw_calls({:?})", dcs);
|
||||
for (key, val) in dcs {
|
||||
self.draw_calls.insert(key, val);
|
||||
}
|
||||
@@ -550,6 +592,9 @@ impl EventHandler for Stage {
|
||||
fn key_down_event(&mut self, keycode: KeyCode, mods: KeyMods, repeat: bool) {
|
||||
self.event_pub.notify_key_down(keycode, mods, repeat);
|
||||
}
|
||||
fn key_up_event(&mut self, keycode: KeyCode, mods: KeyMods) {
|
||||
self.event_pub.notify_key_up(keycode, mods);
|
||||
}
|
||||
fn resize_event(&mut self, width: f32, height: f32) {
|
||||
self.event_pub.notify_resize(width, height);
|
||||
}
|
||||
@@ -557,6 +602,10 @@ impl EventHandler for Stage {
|
||||
fn quit_requested_event(&mut self) {
|
||||
self.async_runtime.stop();
|
||||
}
|
||||
|
||||
fn char_event(&mut self, chr: char, mods: KeyMods, repeat: bool) {
|
||||
//debug!("chr: {chr}, mods: {:?} repeat: {repeat}", mods);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_gui(
|
||||
|
||||
@@ -114,6 +114,23 @@ fn main() {
|
||||
}
|
||||
});
|
||||
async_runtime.push_task(ev_relay_task);
|
||||
let ev_sub = event_pub.subscribe_key_up();
|
||||
let ev_relay_task = ex.spawn(async move {
|
||||
debug!(target: "main", "event relayer started");
|
||||
loop {
|
||||
let Ok((key, mods)) = ev_sub.receive().await else {
|
||||
debug!(target: "main", "Event relayer closed");
|
||||
break
|
||||
};
|
||||
// Ignore keys which get stuck repeating when switching windows
|
||||
match key {
|
||||
miniquad::KeyCode::LeftShift | miniquad::KeyCode::LeftSuper => continue,
|
||||
_ => {}
|
||||
}
|
||||
debug!(target: "main", "key_up event: {:?} {:?}", key, mods);
|
||||
}
|
||||
});
|
||||
async_runtime.push_task(ev_relay_task);
|
||||
|
||||
//let stage = gfx2::Stage::new(method_rep, event_pub);
|
||||
gfx2::run_gui(async_runtime, method_rep, event_pub);
|
||||
|
||||
@@ -516,6 +516,7 @@ pub struct Sprite {
|
||||
pub has_color: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Glyph {
|
||||
pub glyph_id: u32,
|
||||
// Substring this glyph corresponds to
|
||||
|
||||
@@ -1,6 +1,25 @@
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
use miniquad::{BufferId, KeyCode, KeyMods, TextureId};
|
||||
use rand::{rngs::OsRng, Rng};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex as SyncMutex, Weak},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
use crate::scene::Pimpl;
|
||||
use crate::{
|
||||
gfx2::{
|
||||
DrawCall, DrawInstruction, DrawMesh, GraphicsEventPublisherPtr, Rectangle, RenderApi,
|
||||
RenderApiPtr, Vertex,
|
||||
},
|
||||
mesh::{Color, MeshBuilder, MeshInfo, COLOR_BLUE, COLOR_WHITE},
|
||||
prop::{
|
||||
PropertyBool, PropertyColor, PropertyFloat32, PropertyPtr, PropertyStr, PropertyUint32,
|
||||
},
|
||||
scene::{Pimpl, SceneGraph, SceneGraphPtr2, SceneNodeId},
|
||||
text2::{self, Glyph, GlyphPositionIter, RenderedAtlas, SpritePtr, TextShaper, TextShaperPtr},
|
||||
};
|
||||
|
||||
use super::{eval_rect, get_parent_rect, read_rect, DrawUpdate, OnModify, Stoppable};
|
||||
|
||||
// First refactor the event system
|
||||
// Each event should have its own unique pipe
|
||||
@@ -9,17 +28,471 @@ use crate::scene::Pimpl;
|
||||
// - more advanced locking of streams when widgets capture input
|
||||
// also add capturing and make use of it with editbox.
|
||||
|
||||
const CURSOR_WIDTH: f32 = 4.;
|
||||
|
||||
struct PressedKeysSmoothRepeat {
|
||||
/// When holding keys, we track from start and last sent time.
|
||||
/// This is useful for initial delay and smooth scrolling.
|
||||
pressed_keys: HashMap<KeyCode, RepeatingKeyTimer>,
|
||||
/// Initial delay before allowing keys
|
||||
start_delay: u32,
|
||||
/// Minimum time between repeated keys
|
||||
step_time: u32,
|
||||
}
|
||||
|
||||
impl PressedKeysSmoothRepeat {
|
||||
fn new(start_delay: u32, step_time: u32) -> Self {
|
||||
Self { pressed_keys: HashMap::new(), start_delay, step_time }
|
||||
}
|
||||
|
||||
fn key_down(&mut self, key: KeyCode, repeat: bool) -> u32 {
|
||||
if !repeat {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Insert key if not exists
|
||||
if !self.pressed_keys.contains_key(&key) {
|
||||
self.pressed_keys.insert(key, RepeatingKeyTimer::new());
|
||||
}
|
||||
|
||||
let repeater = self.pressed_keys.get_mut(&key).expect("repeat map");
|
||||
repeater.update(self.start_delay, self.step_time)
|
||||
}
|
||||
|
||||
fn key_up(&mut self, key: &KeyCode) {
|
||||
self.pressed_keys.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
struct RepeatingKeyTimer {
|
||||
start: Instant,
|
||||
actions: u32,
|
||||
}
|
||||
|
||||
impl RepeatingKeyTimer {
|
||||
fn new() -> Self {
|
||||
Self { start: Instant::now(), actions: 0 }
|
||||
}
|
||||
|
||||
fn update(&mut self, start_delay: u32, step_time: u32) -> u32 {
|
||||
let elapsed = self.start.elapsed().as_millis();
|
||||
if elapsed < start_delay as u128 {
|
||||
return 0
|
||||
}
|
||||
let total_actions = ((elapsed - start_delay as u128) / step_time as u128) as u32;
|
||||
let remaining_actions = total_actions - self.actions;
|
||||
self.actions = total_actions;
|
||||
remaining_actions
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct TextRenderInfo {
|
||||
glyphs: Vec<Glyph>,
|
||||
mesh: MeshInfo,
|
||||
texture_id: TextureId,
|
||||
}
|
||||
|
||||
pub type EditBoxPtr = Arc<EditBox>;
|
||||
|
||||
pub struct EditBox {
|
||||
node_id: SceneNodeId,
|
||||
tasks: Vec<smol::Task<()>>,
|
||||
sg: SceneGraphPtr2,
|
||||
render_api: RenderApiPtr,
|
||||
// So we can lock the event stream when we gain focus
|
||||
event_pub: GraphicsEventPublisherPtr,
|
||||
text_shaper: TextShaperPtr,
|
||||
key_repeat: SyncMutex<PressedKeysSmoothRepeat>,
|
||||
|
||||
render_info: SyncMutex<TextRenderInfo>,
|
||||
dc_key: u64,
|
||||
|
||||
is_active: PropertyBool,
|
||||
rect: PropertyPtr,
|
||||
baseline: PropertyFloat32,
|
||||
scroll: PropertyFloat32,
|
||||
cursor_pos: PropertyUint32,
|
||||
font_size: PropertyFloat32,
|
||||
text: PropertyStr,
|
||||
text_color: PropertyColor,
|
||||
cursor_color: PropertyColor,
|
||||
hi_bg_color: PropertyColor,
|
||||
selected: PropertyPtr,
|
||||
z_index: PropertyUint32,
|
||||
debug: PropertyBool,
|
||||
}
|
||||
|
||||
impl EditBox {
|
||||
pub async fn new(
|
||||
ex: Arc<smol::Executor<'static>>,
|
||||
sg: SceneGraphPtr2,
|
||||
node_id: SceneNodeId,
|
||||
render_api: RenderApiPtr,
|
||||
event_pub: GraphicsEventPublisherPtr,
|
||||
text_shaper: TextShaperPtr,
|
||||
) -> Pimpl {
|
||||
let self_ = Arc::new(Self {});
|
||||
let scene_graph = sg.lock().await;
|
||||
let node = scene_graph.get_node(node_id).unwrap();
|
||||
let is_active = PropertyBool::wrap(node, "is_active", 0).unwrap();
|
||||
let rect = node.get_property("rect").expect("EditBox::rect");
|
||||
let baseline = PropertyFloat32::wrap(node, "baseline", 0).unwrap();
|
||||
let scroll = PropertyFloat32::wrap(node, "scroll", 0).unwrap();
|
||||
let cursor_pos = PropertyUint32::wrap(node, "cursor_pos", 0).unwrap();
|
||||
let font_size = PropertyFloat32::wrap(node, "font_size", 0).unwrap();
|
||||
let text = PropertyStr::wrap(node, "text", 0).unwrap();
|
||||
let text_color = PropertyColor::wrap(node, "text_color").unwrap();
|
||||
let cursor_color = PropertyColor::wrap(node, "cursor_color").unwrap();
|
||||
let hi_bg_color = PropertyColor::wrap(node, "hi_bg_color").unwrap();
|
||||
let selected = node.get_property("selected").unwrap();
|
||||
let z_index = PropertyUint32::wrap(node, "z_index", 0).unwrap();
|
||||
let debug = PropertyBool::wrap(node, "debug", 0).unwrap();
|
||||
drop(scene_graph);
|
||||
|
||||
let render_info = Self::regen_mesh(
|
||||
&render_api,
|
||||
&text_shaper,
|
||||
text.get(),
|
||||
font_size.get(),
|
||||
text_color.get(),
|
||||
baseline.get(),
|
||||
debug.get(),
|
||||
)
|
||||
.await;
|
||||
|
||||
let self_ = Arc::new_cyclic(|me: &Weak<Self>| {
|
||||
// Start a task monitoring for key down events
|
||||
let ev_sub = event_pub.subscribe_key_down();
|
||||
let me2 = me.clone();
|
||||
let key_down_task = ex.spawn(async move {
|
||||
loop {
|
||||
let Ok((key, mods, repeat)) = ev_sub.receive().await else {
|
||||
debug!(target: "ui::editbox", "Event relayer closed");
|
||||
break
|
||||
};
|
||||
|
||||
let Some(self_) = me2.upgrade() else {
|
||||
// Should not happen
|
||||
panic!("self destroyed before key_down_task was stopped!");
|
||||
};
|
||||
|
||||
let actions = {
|
||||
let mut repeater = self_.key_repeat.lock().unwrap();
|
||||
repeater.key_down(key, repeat)
|
||||
};
|
||||
for _ in 0..actions {
|
||||
self_.do_key_action(&key, &mods).await;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let ev_sub = event_pub.subscribe_key_up();
|
||||
let me2 = me.clone();
|
||||
let key_up_task = ex.spawn(async move {
|
||||
loop {
|
||||
let Ok((key, mods)) = ev_sub.receive().await else {
|
||||
debug!(target: "ui::editbox", "Event relayer closed");
|
||||
break
|
||||
};
|
||||
|
||||
let Some(self_) = me2.upgrade() else {
|
||||
// Should not happen
|
||||
panic!("self destroyed before key_up_task was stopped!");
|
||||
};
|
||||
|
||||
let actions = {
|
||||
let mut repeater = self_.key_repeat.lock().unwrap();
|
||||
repeater.key_up(&key)
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// on modify tasks too
|
||||
let tasks = vec![key_down_task, key_up_task];
|
||||
|
||||
Self {
|
||||
node_id,
|
||||
tasks,
|
||||
sg,
|
||||
render_api,
|
||||
event_pub,
|
||||
text_shaper,
|
||||
key_repeat: SyncMutex::new(PressedKeysSmoothRepeat::new(400, 50)),
|
||||
|
||||
render_info: SyncMutex::new(render_info),
|
||||
dc_key: OsRng.gen(),
|
||||
|
||||
is_active,
|
||||
rect,
|
||||
baseline,
|
||||
scroll,
|
||||
cursor_pos,
|
||||
font_size,
|
||||
text,
|
||||
text_color,
|
||||
cursor_color,
|
||||
hi_bg_color,
|
||||
selected,
|
||||
z_index,
|
||||
debug,
|
||||
}
|
||||
});
|
||||
|
||||
Pimpl::EditBox(self_)
|
||||
}
|
||||
|
||||
/// Called whenever the text or any text property changes.
|
||||
/// Not related to cursor, text highlighting or bounding (clip) rects.
|
||||
async fn regen_mesh(
|
||||
render_api: &RenderApi,
|
||||
text_shaper: &TextShaper,
|
||||
text: String,
|
||||
font_size: f32,
|
||||
text_color: Color,
|
||||
baseline: f32,
|
||||
debug: bool,
|
||||
) -> TextRenderInfo {
|
||||
debug!(target: "ui::editbox", "Rendering text '{}'", text);
|
||||
let glyphs = text_shaper.shape(text, font_size).await;
|
||||
let atlas = text2::make_texture_atlas(render_api, font_size, &glyphs).await.unwrap();
|
||||
|
||||
let mut mesh = MeshBuilder::new();
|
||||
let mut glyph_pos_iter = GlyphPositionIter::new(font_size, &glyphs, baseline);
|
||||
for ((uv_rect, glyph_rect), glyph) in
|
||||
atlas.uv_rects.into_iter().zip(glyph_pos_iter).zip(glyphs.iter())
|
||||
{
|
||||
//mesh.draw_outline(&glyph_rect, COLOR_BLUE, 2.);
|
||||
let mut color = text_color.clone();
|
||||
if glyph.sprite.has_color {
|
||||
color = COLOR_WHITE;
|
||||
}
|
||||
mesh.draw_box(&glyph_rect, color, &uv_rect);
|
||||
}
|
||||
|
||||
let mesh = mesh.alloc(&render_api).await.unwrap();
|
||||
|
||||
TextRenderInfo { glyphs, mesh, texture_id: atlas.texture_id }
|
||||
}
|
||||
|
||||
async fn do_key_action(&self, key: &KeyCode, mods: &KeyMods) {
|
||||
match key {
|
||||
KeyCode::Left => {}
|
||||
_ => self.insert_char(key, mods).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn insert_char(&self, key: &KeyCode, mods: &KeyMods) {
|
||||
// we could use a char but fuck the law
|
||||
let Some(key) = keycode_to_string(key) else {
|
||||
return;
|
||||
};
|
||||
|
||||
// First filter for only single digit keys
|
||||
let allowed_keys = [
|
||||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
|
||||
"R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", ":", ";", "'", "-", ".", "/", "=",
|
||||
"(", "\\", ")", "`", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
||||
];
|
||||
if !allowed_keys.contains(&key.as_str()) {
|
||||
return
|
||||
}
|
||||
|
||||
// If we want to only allow specific chars in a String here
|
||||
//let ch = key.chars().next().unwrap();
|
||||
// if !self.allowed_chars.chars().any(|c| c == ch) { return }
|
||||
|
||||
let key = if mods.shift { key.to_string() } else { key.to_lowercase() };
|
||||
|
||||
self.insert_text(key).await;
|
||||
}
|
||||
|
||||
async fn insert_text(&self, key: String) {
|
||||
let mut text = String::new();
|
||||
|
||||
let cursor_pos = self.cursor_pos.get();
|
||||
|
||||
let glyphs = self.render_info.lock().unwrap().glyphs.clone();
|
||||
|
||||
// We rebuild the string but insert our substr at cursor_pos.
|
||||
// The substr is inserted before cursor_pos, and appending to the end
|
||||
// of the string is when cursor_pos = len(str).
|
||||
// We can't use String::insert() because sometimes multiple chars are combined
|
||||
// into a single glyph. We treat the cursor pos as acting on the substrs
|
||||
// themselves.
|
||||
for (i, glyph) in glyphs.iter().enumerate() {
|
||||
if cursor_pos == i as u32 {
|
||||
text.push_str(&key);
|
||||
}
|
||||
text.push_str(&glyph.substr);
|
||||
}
|
||||
// Append to the end
|
||||
if cursor_pos == glyphs.len() as u32 {
|
||||
text.push_str(&key);
|
||||
}
|
||||
|
||||
self.text.set(text);
|
||||
// Not always true lol
|
||||
// If glyphs are recombined, this could get messed up
|
||||
// meh lets pretend it doesn't exist for now.
|
||||
self.cursor_pos.set(cursor_pos + 1);
|
||||
|
||||
self.redraw().await;
|
||||
}
|
||||
|
||||
async fn redraw(&self) {
|
||||
let old = self.render_info.lock().unwrap().clone();
|
||||
|
||||
let render_info = Self::regen_mesh(
|
||||
&self.render_api,
|
||||
&self.text_shaper,
|
||||
self.text.get(),
|
||||
self.font_size.get(),
|
||||
self.text_color.get(),
|
||||
self.baseline.get(),
|
||||
self.debug.get(),
|
||||
)
|
||||
.await;
|
||||
*self.render_info.lock().unwrap() = render_info;
|
||||
|
||||
let sg = self.sg.lock().await;
|
||||
let node = sg.get_node(self.node_id).unwrap();
|
||||
|
||||
let Some(parent_rect) = get_parent_rect(&sg, node) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(draw_update) = self.draw(&sg, &parent_rect).await else {
|
||||
error!(target: "ui::editbox", "Text {:?} failed to draw", node);
|
||||
return;
|
||||
};
|
||||
self.render_api.replace_draw_calls(draw_update.draw_calls).await;
|
||||
debug!(target: "ui::editbox", "replace draw calls done");
|
||||
|
||||
// We're finished with these so clean up.
|
||||
self.render_api.delete_buffer(old.mesh.vertex_buffer);
|
||||
self.render_api.delete_buffer(old.mesh.index_buffer);
|
||||
self.render_api.delete_texture(old.texture_id);
|
||||
}
|
||||
|
||||
pub async fn draw(&self, sg: &SceneGraph, parent_rect: &Rectangle) -> Option<DrawUpdate> {
|
||||
debug!(target: "ui::editbox", "EditBox::draw()");
|
||||
// Only used for debug messages
|
||||
let node = sg.get_node(self.node_id).unwrap();
|
||||
|
||||
let render_info = self.render_info.lock().unwrap().clone();
|
||||
|
||||
let mesh = DrawMesh {
|
||||
vertex_buffer: render_info.mesh.vertex_buffer,
|
||||
index_buffer: render_info.mesh.index_buffer,
|
||||
texture: Some(render_info.texture_id),
|
||||
num_elements: render_info.mesh.num_elements,
|
||||
};
|
||||
|
||||
if let Err(err) = eval_rect(self.rect.clone(), parent_rect) {
|
||||
panic!("Node {:?} bad rect property: {}", node, err);
|
||||
}
|
||||
|
||||
let Ok(mut rect) = read_rect(self.rect.clone()) else {
|
||||
panic!("Node {:?} bad rect property", node);
|
||||
};
|
||||
|
||||
rect.x += parent_rect.x;
|
||||
rect.y += parent_rect.x;
|
||||
|
||||
let off_x = rect.x / parent_rect.w;
|
||||
let off_y = rect.y / parent_rect.h;
|
||||
let scale_x = 1. / parent_rect.w;
|
||||
let scale_y = 1. / parent_rect.h;
|
||||
let model = glam::Mat4::from_translation(glam::Vec3::new(off_x, off_y, 0.)) *
|
||||
glam::Mat4::from_scale(glam::Vec3::new(scale_x, scale_y, 1.));
|
||||
|
||||
Some(DrawUpdate {
|
||||
key: self.dc_key,
|
||||
draw_calls: vec![(
|
||||
self.dc_key,
|
||||
DrawCall {
|
||||
instrs: vec![DrawInstruction::ApplyMatrix(model), DrawInstruction::Draw(mesh)],
|
||||
dcs: vec![],
|
||||
z_index: self.z_index.get(),
|
||||
},
|
||||
)],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn keycode_to_string(key: &KeyCode) -> Option<String> {
|
||||
match key {
|
||||
KeyCode::Space => Some(" ".to_string()),
|
||||
KeyCode::Apostrophe => Some("'".to_string()),
|
||||
KeyCode::Comma => Some(",".to_string()),
|
||||
KeyCode::Minus => Some("-".to_string()),
|
||||
KeyCode::Period => Some(".".to_string()),
|
||||
KeyCode::Slash => Some("/".to_string()),
|
||||
KeyCode::Key0 => Some("0".to_string()),
|
||||
KeyCode::Key1 => Some("1".to_string()),
|
||||
KeyCode::Key2 => Some("2".to_string()),
|
||||
KeyCode::Key3 => Some("3".to_string()),
|
||||
KeyCode::Key4 => Some("4".to_string()),
|
||||
KeyCode::Key5 => Some("5".to_string()),
|
||||
KeyCode::Key6 => Some("6".to_string()),
|
||||
KeyCode::Key7 => Some("7".to_string()),
|
||||
KeyCode::Key8 => Some("8".to_string()),
|
||||
KeyCode::Key9 => Some("9".to_string()),
|
||||
KeyCode::Semicolon => Some(";".to_string()),
|
||||
KeyCode::Equal => Some("=".to_string()),
|
||||
KeyCode::A => Some("A".to_string()),
|
||||
KeyCode::B => Some("B".to_string()),
|
||||
KeyCode::C => Some("C".to_string()),
|
||||
KeyCode::D => Some("D".to_string()),
|
||||
KeyCode::E => Some("E".to_string()),
|
||||
KeyCode::F => Some("F".to_string()),
|
||||
KeyCode::G => Some("G".to_string()),
|
||||
KeyCode::H => Some("H".to_string()),
|
||||
KeyCode::I => Some("I".to_string()),
|
||||
KeyCode::J => Some("J".to_string()),
|
||||
KeyCode::K => Some("K".to_string()),
|
||||
KeyCode::L => Some("L".to_string()),
|
||||
KeyCode::M => Some("M".to_string()),
|
||||
KeyCode::N => Some("N".to_string()),
|
||||
KeyCode::O => Some("O".to_string()),
|
||||
KeyCode::P => Some("P".to_string()),
|
||||
KeyCode::Q => Some("Q".to_string()),
|
||||
KeyCode::S => Some("S".to_string()),
|
||||
KeyCode::T => Some("T".to_string()),
|
||||
KeyCode::U => Some("U".to_string()),
|
||||
KeyCode::V => Some("V".to_string()),
|
||||
KeyCode::W => Some("W".to_string()),
|
||||
KeyCode::X => Some("X".to_string()),
|
||||
KeyCode::Y => Some("Y".to_string()),
|
||||
KeyCode::Z => Some("Z".to_string()),
|
||||
KeyCode::LeftBracket => Some("(".to_string()),
|
||||
KeyCode::Backslash => Some("\\".to_string()),
|
||||
KeyCode::RightBracket => Some(")".to_string()),
|
||||
KeyCode::Kp0 => Some("0".to_string()),
|
||||
KeyCode::Kp1 => Some("1".to_string()),
|
||||
KeyCode::Kp2 => Some("2".to_string()),
|
||||
KeyCode::Kp3 => Some("3".to_string()),
|
||||
KeyCode::Kp4 => Some("4".to_string()),
|
||||
KeyCode::Kp5 => Some("5".to_string()),
|
||||
KeyCode::Kp6 => Some("6".to_string()),
|
||||
KeyCode::Kp7 => Some("7".to_string()),
|
||||
KeyCode::Kp8 => Some("8".to_string()),
|
||||
KeyCode::Kp9 => Some("9".to_string()),
|
||||
KeyCode::KpDecimal => Some(".".to_string()),
|
||||
KeyCode::KpDivide => Some("/".to_string()),
|
||||
KeyCode::KpMultiply => Some("*".to_string()),
|
||||
KeyCode::KpSubtract => Some("-".to_string()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl Stoppable for EditBox {
|
||||
async fn stop(&self) {
|
||||
// TODO: Delete own draw call
|
||||
|
||||
// Free buffers
|
||||
// Should this be in drop?
|
||||
//self.render_api.delete_buffer(self.vertex_buffer);
|
||||
//self.render_api.delete_buffer(self.index_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,7 @@ impl RenderLayer {
|
||||
Pimpl::RenderLayer(layer) => layer.draw(&sg, &rect).await,
|
||||
Pimpl::Mesh(mesh) => mesh.draw(&sg, &rect),
|
||||
Pimpl::Text(txt) => txt.draw(&sg, &rect).await,
|
||||
Pimpl::EditBox(editb) => editb.draw(&sg, &rect).await,
|
||||
_ => {
|
||||
error!(target: "ui::layer", "unhandled pimpl type");
|
||||
continue
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//use async_lock::Mutex;
|
||||
use miniquad::{BufferId, TextureId};
|
||||
use rand::{rngs::OsRng, Rng};
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
use std::sync::{Arc, Mutex as SyncMutex, Weak};
|
||||
|
||||
use crate::{
|
||||
gfx2::{DrawCall, DrawInstruction, DrawMesh, Rectangle, RenderApi, RenderApiPtr, Vertex},
|
||||
@@ -30,7 +30,7 @@ pub struct Text {
|
||||
text_shaper: TextShaperPtr,
|
||||
tasks: Vec<smol::Task<()>>,
|
||||
|
||||
render_info: Mutex<TextRenderInfo>,
|
||||
render_info: SyncMutex<TextRenderInfo>,
|
||||
dc_key: u64,
|
||||
|
||||
node_id: SceneNodeId,
|
||||
@@ -38,7 +38,7 @@ pub struct Text {
|
||||
z_index: PropertyUint32,
|
||||
text: PropertyStr,
|
||||
font_size: PropertyFloat32,
|
||||
color: PropertyColor,
|
||||
text_color: PropertyColor,
|
||||
baseline: PropertyFloat32,
|
||||
debug: PropertyBool,
|
||||
}
|
||||
@@ -58,7 +58,7 @@ impl Text {
|
||||
let z_index = PropertyUint32::wrap(node, "z_index", 0).unwrap();
|
||||
let text = PropertyStr::wrap(node, "text", 0).unwrap();
|
||||
let font_size = PropertyFloat32::wrap(node, "font_size", 0).unwrap();
|
||||
let color = PropertyColor::wrap(node, "color").unwrap();
|
||||
let text_color = PropertyColor::wrap(node, "text_color").unwrap();
|
||||
let baseline = PropertyFloat32::wrap(node, "baseline", 0).unwrap();
|
||||
let debug = PropertyBool::wrap(node, "debug", 0).unwrap();
|
||||
drop(scene_graph);
|
||||
@@ -68,7 +68,7 @@ impl Text {
|
||||
&text_shaper,
|
||||
text.get(),
|
||||
font_size.get(),
|
||||
color.get(),
|
||||
text_color.get(),
|
||||
baseline.get(),
|
||||
debug.get(),
|
||||
)
|
||||
@@ -80,7 +80,7 @@ impl Text {
|
||||
on_modify.when_change(z_index.prop(), Self::redraw);
|
||||
on_modify.when_change(text.prop(), Self::redraw);
|
||||
on_modify.when_change(font_size.prop(), Self::redraw);
|
||||
on_modify.when_change(color.prop(), Self::redraw);
|
||||
on_modify.when_change(text_color.prop(), Self::redraw);
|
||||
on_modify.when_change(debug.prop(), Self::redraw);
|
||||
on_modify.when_change(baseline.prop(), Self::redraw);
|
||||
|
||||
@@ -89,14 +89,14 @@ impl Text {
|
||||
render_api,
|
||||
text_shaper,
|
||||
tasks: on_modify.tasks,
|
||||
render_info: Mutex::new(render_info),
|
||||
render_info: SyncMutex::new(render_info),
|
||||
dc_key: OsRng.gen(),
|
||||
node_id,
|
||||
rect,
|
||||
z_index,
|
||||
text,
|
||||
font_size,
|
||||
color,
|
||||
text_color,
|
||||
baseline,
|
||||
debug,
|
||||
}
|
||||
@@ -146,17 +146,13 @@ impl Text {
|
||||
&self.text_shaper,
|
||||
self.text.get(),
|
||||
self.font_size.get(),
|
||||
self.color.get(),
|
||||
self.text_color.get(),
|
||||
self.baseline.get(),
|
||||
self.debug.get(),
|
||||
)
|
||||
.await;
|
||||
*self.render_info.lock().unwrap() = render_info;
|
||||
|
||||
self.render_api.delete_buffer(old.mesh.vertex_buffer);
|
||||
self.render_api.delete_buffer(old.mesh.index_buffer);
|
||||
self.render_api.delete_texture(old.texture_id);
|
||||
|
||||
let sg = self.sg.lock().await;
|
||||
let node = sg.get_node(self.node_id).unwrap();
|
||||
|
||||
@@ -170,6 +166,11 @@ impl Text {
|
||||
};
|
||||
self.render_api.replace_draw_calls(draw_update.draw_calls).await;
|
||||
debug!(target: "ui::text", "replace draw calls done");
|
||||
|
||||
// We're finished with these so clean up.
|
||||
self.render_api.delete_buffer(old.mesh.vertex_buffer);
|
||||
self.render_api.delete_buffer(old.mesh.index_buffer);
|
||||
self.render_api.delete_texture(old.texture_id);
|
||||
}
|
||||
|
||||
pub async fn draw(&self, sg: &SceneGraph, parent_rect: &Rectangle) -> Option<DrawUpdate> {
|
||||
@@ -224,7 +225,12 @@ impl Stoppable for Text {
|
||||
|
||||
// Free buffers
|
||||
// Should this be in drop?
|
||||
//self.render_api.delete_buffer(self.vertex_buffer);
|
||||
//self.render_api.delete_buffer(self.index_buffer);
|
||||
let render_info = self.render_info.lock().unwrap().clone();
|
||||
let vertex_buffer = render_info.mesh.vertex_buffer;
|
||||
let index_buffer = render_info.mesh.index_buffer;
|
||||
let texture_id = render_info.texture_id;
|
||||
self.render_api.delete_buffer(vertex_buffer);
|
||||
self.render_api.delete_buffer(index_buffer);
|
||||
self.render_api.delete_texture(texture_id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user