editbox.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. use miniquad::{BufferId, KeyCode, KeyMods, TextureId};
  2. use rand::{rngs::OsRng, Rng};
  3. use std::{
  4. collections::HashMap,
  5. sync::{Arc, Mutex as SyncMutex, Weak},
  6. time::Instant,
  7. };
  8. use crate::{
  9. gfx2::{
  10. DrawCall, DrawInstruction, DrawMesh, GraphicsEventPublisherPtr, Rectangle, RenderApi,
  11. RenderApiPtr, Vertex,
  12. },
  13. mesh::{Color, MeshBuilder, MeshInfo, COLOR_BLUE, COLOR_WHITE},
  14. prop::{
  15. PropertyBool, PropertyColor, PropertyFloat32, PropertyPtr, PropertyStr, PropertyUint32,
  16. },
  17. scene::{Pimpl, SceneGraph, SceneGraphPtr2, SceneNodeId},
  18. text2::{self, Glyph, GlyphPositionIter, RenderedAtlas, SpritePtr, TextShaper, TextShaperPtr},
  19. };
  20. use super::{eval_rect, get_parent_rect, read_rect, DrawUpdate, OnModify, Stoppable};
  21. // First refactor the event system
  22. // Each event should have its own unique pipe
  23. // Advantages:
  24. // - less overhead when publishing msgs to ppl who dont need them
  25. // - more advanced locking of streams when widgets capture input
  26. // also add capturing and make use of it with editbox.
  27. const CURSOR_WIDTH: f32 = 4.;
  28. struct PressedKeysSmoothRepeat {
  29. /// When holding keys, we track from start and last sent time.
  30. /// This is useful for initial delay and smooth scrolling.
  31. pressed_keys: HashMap<KeyCode, RepeatingKeyTimer>,
  32. /// Initial delay before allowing keys
  33. start_delay: u32,
  34. /// Minimum time between repeated keys
  35. step_time: u32,
  36. }
  37. impl PressedKeysSmoothRepeat {
  38. fn new(start_delay: u32, step_time: u32) -> Self {
  39. Self { pressed_keys: HashMap::new(), start_delay, step_time }
  40. }
  41. fn key_down(&mut self, key: KeyCode, repeat: bool) -> u32 {
  42. if !repeat {
  43. return 1;
  44. }
  45. // Insert key if not exists
  46. if !self.pressed_keys.contains_key(&key) {
  47. self.pressed_keys.insert(key, RepeatingKeyTimer::new());
  48. }
  49. let repeater = self.pressed_keys.get_mut(&key).expect("repeat map");
  50. repeater.update(self.start_delay, self.step_time)
  51. }
  52. fn key_up(&mut self, key: &KeyCode) {
  53. self.pressed_keys.remove(key);
  54. }
  55. }
  56. struct RepeatingKeyTimer {
  57. start: Instant,
  58. actions: u32,
  59. }
  60. impl RepeatingKeyTimer {
  61. fn new() -> Self {
  62. Self { start: Instant::now(), actions: 0 }
  63. }
  64. fn update(&mut self, start_delay: u32, step_time: u32) -> u32 {
  65. let elapsed = self.start.elapsed().as_millis();
  66. if elapsed < start_delay as u128 {
  67. return 0
  68. }
  69. let total_actions = ((elapsed - start_delay as u128) / step_time as u128) as u32;
  70. let remaining_actions = total_actions - self.actions;
  71. self.actions = total_actions;
  72. remaining_actions
  73. }
  74. }
  75. #[derive(Clone)]
  76. struct TextRenderInfo {
  77. glyphs: Vec<Glyph>,
  78. mesh: MeshInfo,
  79. texture_id: TextureId,
  80. }
  81. pub type EditBoxPtr = Arc<EditBox>;
  82. pub struct EditBox {
  83. node_id: SceneNodeId,
  84. tasks: Vec<smol::Task<()>>,
  85. sg: SceneGraphPtr2,
  86. render_api: RenderApiPtr,
  87. // So we can lock the event stream when we gain focus
  88. event_pub: GraphicsEventPublisherPtr,
  89. text_shaper: TextShaperPtr,
  90. key_repeat: SyncMutex<PressedKeysSmoothRepeat>,
  91. render_info: SyncMutex<TextRenderInfo>,
  92. dc_key: u64,
  93. is_active: PropertyBool,
  94. rect: PropertyPtr,
  95. baseline: PropertyFloat32,
  96. scroll: PropertyFloat32,
  97. cursor_pos: PropertyUint32,
  98. font_size: PropertyFloat32,
  99. text: PropertyStr,
  100. text_color: PropertyColor,
  101. cursor_color: PropertyColor,
  102. hi_bg_color: PropertyColor,
  103. selected: PropertyPtr,
  104. z_index: PropertyUint32,
  105. debug: PropertyBool,
  106. }
  107. impl EditBox {
  108. pub async fn new(
  109. ex: Arc<smol::Executor<'static>>,
  110. sg: SceneGraphPtr2,
  111. node_id: SceneNodeId,
  112. render_api: RenderApiPtr,
  113. event_pub: GraphicsEventPublisherPtr,
  114. text_shaper: TextShaperPtr,
  115. ) -> Pimpl {
  116. let scene_graph = sg.lock().await;
  117. let node = scene_graph.get_node(node_id).unwrap();
  118. let is_active = PropertyBool::wrap(node, "is_active", 0).unwrap();
  119. let rect = node.get_property("rect").expect("EditBox::rect");
  120. let baseline = PropertyFloat32::wrap(node, "baseline", 0).unwrap();
  121. let scroll = PropertyFloat32::wrap(node, "scroll", 0).unwrap();
  122. let cursor_pos = PropertyUint32::wrap(node, "cursor_pos", 0).unwrap();
  123. let font_size = PropertyFloat32::wrap(node, "font_size", 0).unwrap();
  124. let text = PropertyStr::wrap(node, "text", 0).unwrap();
  125. let text_color = PropertyColor::wrap(node, "text_color").unwrap();
  126. let cursor_color = PropertyColor::wrap(node, "cursor_color").unwrap();
  127. let hi_bg_color = PropertyColor::wrap(node, "hi_bg_color").unwrap();
  128. let selected = node.get_property("selected").unwrap();
  129. let z_index = PropertyUint32::wrap(node, "z_index", 0).unwrap();
  130. let debug = PropertyBool::wrap(node, "debug", 0).unwrap();
  131. drop(scene_graph);
  132. let render_info = Self::regen_mesh(
  133. &render_api,
  134. &text_shaper,
  135. text.get(),
  136. font_size.get(),
  137. text_color.get(),
  138. baseline.get(),
  139. debug.get(),
  140. )
  141. .await;
  142. let self_ = Arc::new_cyclic(|me: &Weak<Self>| {
  143. // Start a task monitoring for key down events
  144. let ev_sub = event_pub.subscribe_key_down();
  145. let me2 = me.clone();
  146. let key_down_task = ex.spawn(async move {
  147. loop {
  148. let Ok((key, mods, repeat)) = ev_sub.receive().await else {
  149. debug!(target: "ui::editbox", "Event relayer closed");
  150. break
  151. };
  152. let Some(self_) = me2.upgrade() else {
  153. // Should not happen
  154. panic!("self destroyed before key_down_task was stopped!");
  155. };
  156. let actions = {
  157. let mut repeater = self_.key_repeat.lock().unwrap();
  158. repeater.key_down(key, repeat)
  159. };
  160. for _ in 0..actions {
  161. self_.do_key_action(&key, &mods).await;
  162. }
  163. }
  164. });
  165. let ev_sub = event_pub.subscribe_key_up();
  166. let me2 = me.clone();
  167. let key_up_task = ex.spawn(async move {
  168. loop {
  169. let Ok((key, mods)) = ev_sub.receive().await else {
  170. debug!(target: "ui::editbox", "Event relayer closed");
  171. break
  172. };
  173. let Some(self_) = me2.upgrade() else {
  174. // Should not happen
  175. panic!("self destroyed before key_up_task was stopped!");
  176. };
  177. let actions = {
  178. let mut repeater = self_.key_repeat.lock().unwrap();
  179. repeater.key_up(&key)
  180. };
  181. }
  182. });
  183. // on modify tasks too
  184. let tasks = vec![key_down_task, key_up_task];
  185. Self {
  186. node_id,
  187. tasks,
  188. sg,
  189. render_api,
  190. event_pub,
  191. text_shaper,
  192. key_repeat: SyncMutex::new(PressedKeysSmoothRepeat::new(400, 50)),
  193. render_info: SyncMutex::new(render_info),
  194. dc_key: OsRng.gen(),
  195. is_active,
  196. rect,
  197. baseline,
  198. scroll,
  199. cursor_pos,
  200. font_size,
  201. text,
  202. text_color,
  203. cursor_color,
  204. hi_bg_color,
  205. selected,
  206. z_index,
  207. debug,
  208. }
  209. });
  210. Pimpl::EditBox(self_)
  211. }
  212. /// Called whenever the text or any text property changes.
  213. /// Not related to cursor, text highlighting or bounding (clip) rects.
  214. async fn regen_mesh(
  215. render_api: &RenderApi,
  216. text_shaper: &TextShaper,
  217. text: String,
  218. font_size: f32,
  219. text_color: Color,
  220. baseline: f32,
  221. debug: bool,
  222. ) -> TextRenderInfo {
  223. debug!(target: "ui::editbox", "Rendering text '{}'", text);
  224. let glyphs = text_shaper.shape(text, font_size).await;
  225. let atlas = text2::make_texture_atlas(render_api, font_size, &glyphs).await.unwrap();
  226. let mut mesh = MeshBuilder::new();
  227. let mut glyph_pos_iter = GlyphPositionIter::new(font_size, &glyphs, baseline);
  228. for ((uv_rect, glyph_rect), glyph) in
  229. atlas.uv_rects.into_iter().zip(glyph_pos_iter).zip(glyphs.iter())
  230. {
  231. //mesh.draw_outline(&glyph_rect, COLOR_BLUE, 2.);
  232. let mut color = text_color.clone();
  233. if glyph.sprite.has_color {
  234. color = COLOR_WHITE;
  235. }
  236. mesh.draw_box(&glyph_rect, color, &uv_rect);
  237. }
  238. let mesh = mesh.alloc(&render_api).await.unwrap();
  239. TextRenderInfo { glyphs, mesh, texture_id: atlas.texture_id }
  240. }
  241. async fn do_key_action(&self, key: &KeyCode, mods: &KeyMods) {
  242. match key {
  243. KeyCode::Left => {}
  244. _ => self.insert_char(key, mods).await,
  245. }
  246. }
  247. async fn insert_char(&self, key: &KeyCode, mods: &KeyMods) {
  248. // we could use a char but fuck the law
  249. let Some(key) = keycode_to_string(key) else {
  250. return;
  251. };
  252. // First filter for only single digit keys
  253. let allowed_keys = [
  254. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
  255. "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", ":", ";", "'", "-", ".", "/", "=",
  256. "(", "\\", ")", "`", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  257. ];
  258. if !allowed_keys.contains(&key.as_str()) {
  259. return
  260. }
  261. // If we want to only allow specific chars in a String here
  262. //let ch = key.chars().next().unwrap();
  263. // if !self.allowed_chars.chars().any(|c| c == ch) { return }
  264. let key = if mods.shift { key.to_string() } else { key.to_lowercase() };
  265. self.insert_text(key).await;
  266. }
  267. async fn insert_text(&self, key: String) {
  268. let mut text = String::new();
  269. let cursor_pos = self.cursor_pos.get();
  270. let glyphs = self.render_info.lock().unwrap().glyphs.clone();
  271. // We rebuild the string but insert our substr at cursor_pos.
  272. // The substr is inserted before cursor_pos, and appending to the end
  273. // of the string is when cursor_pos = len(str).
  274. // We can't use String::insert() because sometimes multiple chars are combined
  275. // into a single glyph. We treat the cursor pos as acting on the substrs
  276. // themselves.
  277. for (i, glyph) in glyphs.iter().enumerate() {
  278. if cursor_pos == i as u32 {
  279. text.push_str(&key);
  280. }
  281. text.push_str(&glyph.substr);
  282. }
  283. // Append to the end
  284. if cursor_pos == glyphs.len() as u32 {
  285. text.push_str(&key);
  286. }
  287. self.text.set(text);
  288. // Not always true lol
  289. // If glyphs are recombined, this could get messed up
  290. // meh lets pretend it doesn't exist for now.
  291. self.cursor_pos.set(cursor_pos + 1);
  292. self.redraw().await;
  293. }
  294. async fn redraw(&self) {
  295. let old = self.render_info.lock().unwrap().clone();
  296. let render_info = Self::regen_mesh(
  297. &self.render_api,
  298. &self.text_shaper,
  299. self.text.get(),
  300. self.font_size.get(),
  301. self.text_color.get(),
  302. self.baseline.get(),
  303. self.debug.get(),
  304. )
  305. .await;
  306. *self.render_info.lock().unwrap() = render_info;
  307. let sg = self.sg.lock().await;
  308. let node = sg.get_node(self.node_id).unwrap();
  309. let Some(parent_rect) = get_parent_rect(&sg, node) else {
  310. return;
  311. };
  312. let Some(draw_update) = self.draw(&sg, &parent_rect).await else {
  313. error!(target: "ui::editbox", "Text {:?} failed to draw", node);
  314. return;
  315. };
  316. self.render_api.replace_draw_calls(draw_update.draw_calls).await;
  317. debug!(target: "ui::editbox", "replace draw calls done");
  318. // We're finished with these so clean up.
  319. self.render_api.delete_buffer(old.mesh.vertex_buffer);
  320. self.render_api.delete_buffer(old.mesh.index_buffer);
  321. self.render_api.delete_texture(old.texture_id);
  322. }
  323. pub async fn draw(&self, sg: &SceneGraph, parent_rect: &Rectangle) -> Option<DrawUpdate> {
  324. debug!(target: "ui::editbox", "EditBox::draw()");
  325. // Only used for debug messages
  326. let node = sg.get_node(self.node_id).unwrap();
  327. let render_info = self.render_info.lock().unwrap().clone();
  328. let mesh = DrawMesh {
  329. vertex_buffer: render_info.mesh.vertex_buffer,
  330. index_buffer: render_info.mesh.index_buffer,
  331. texture: Some(render_info.texture_id),
  332. num_elements: render_info.mesh.num_elements,
  333. };
  334. if let Err(err) = eval_rect(self.rect.clone(), parent_rect) {
  335. panic!("Node {:?} bad rect property: {}", node, err);
  336. }
  337. let Ok(mut rect) = read_rect(self.rect.clone()) else {
  338. panic!("Node {:?} bad rect property", node);
  339. };
  340. rect.x += parent_rect.x;
  341. rect.y += parent_rect.x;
  342. let off_x = rect.x / parent_rect.w;
  343. let off_y = rect.y / parent_rect.h;
  344. let scale_x = 1. / parent_rect.w;
  345. let scale_y = 1. / parent_rect.h;
  346. let model = glam::Mat4::from_translation(glam::Vec3::new(off_x, off_y, 0.)) *
  347. glam::Mat4::from_scale(glam::Vec3::new(scale_x, scale_y, 1.));
  348. Some(DrawUpdate {
  349. key: self.dc_key,
  350. draw_calls: vec![(
  351. self.dc_key,
  352. DrawCall {
  353. instrs: vec![DrawInstruction::ApplyMatrix(model), DrawInstruction::Draw(mesh)],
  354. dcs: vec![],
  355. z_index: self.z_index.get(),
  356. },
  357. )],
  358. })
  359. }
  360. }
  361. fn keycode_to_string(key: &KeyCode) -> Option<String> {
  362. match key {
  363. KeyCode::Space => Some(" ".to_string()),
  364. KeyCode::Apostrophe => Some("'".to_string()),
  365. KeyCode::Comma => Some(",".to_string()),
  366. KeyCode::Minus => Some("-".to_string()),
  367. KeyCode::Period => Some(".".to_string()),
  368. KeyCode::Slash => Some("/".to_string()),
  369. KeyCode::Key0 => Some("0".to_string()),
  370. KeyCode::Key1 => Some("1".to_string()),
  371. KeyCode::Key2 => Some("2".to_string()),
  372. KeyCode::Key3 => Some("3".to_string()),
  373. KeyCode::Key4 => Some("4".to_string()),
  374. KeyCode::Key5 => Some("5".to_string()),
  375. KeyCode::Key6 => Some("6".to_string()),
  376. KeyCode::Key7 => Some("7".to_string()),
  377. KeyCode::Key8 => Some("8".to_string()),
  378. KeyCode::Key9 => Some("9".to_string()),
  379. KeyCode::Semicolon => Some(";".to_string()),
  380. KeyCode::Equal => Some("=".to_string()),
  381. KeyCode::A => Some("A".to_string()),
  382. KeyCode::B => Some("B".to_string()),
  383. KeyCode::C => Some("C".to_string()),
  384. KeyCode::D => Some("D".to_string()),
  385. KeyCode::E => Some("E".to_string()),
  386. KeyCode::F => Some("F".to_string()),
  387. KeyCode::G => Some("G".to_string()),
  388. KeyCode::H => Some("H".to_string()),
  389. KeyCode::I => Some("I".to_string()),
  390. KeyCode::J => Some("J".to_string()),
  391. KeyCode::K => Some("K".to_string()),
  392. KeyCode::L => Some("L".to_string()),
  393. KeyCode::M => Some("M".to_string()),
  394. KeyCode::N => Some("N".to_string()),
  395. KeyCode::O => Some("O".to_string()),
  396. KeyCode::P => Some("P".to_string()),
  397. KeyCode::Q => Some("Q".to_string()),
  398. KeyCode::S => Some("S".to_string()),
  399. KeyCode::T => Some("T".to_string()),
  400. KeyCode::U => Some("U".to_string()),
  401. KeyCode::V => Some("V".to_string()),
  402. KeyCode::W => Some("W".to_string()),
  403. KeyCode::X => Some("X".to_string()),
  404. KeyCode::Y => Some("Y".to_string()),
  405. KeyCode::Z => Some("Z".to_string()),
  406. KeyCode::LeftBracket => Some("(".to_string()),
  407. KeyCode::Backslash => Some("\\".to_string()),
  408. KeyCode::RightBracket => Some(")".to_string()),
  409. KeyCode::Kp0 => Some("0".to_string()),
  410. KeyCode::Kp1 => Some("1".to_string()),
  411. KeyCode::Kp2 => Some("2".to_string()),
  412. KeyCode::Kp3 => Some("3".to_string()),
  413. KeyCode::Kp4 => Some("4".to_string()),
  414. KeyCode::Kp5 => Some("5".to_string()),
  415. KeyCode::Kp6 => Some("6".to_string()),
  416. KeyCode::Kp7 => Some("7".to_string()),
  417. KeyCode::Kp8 => Some("8".to_string()),
  418. KeyCode::Kp9 => Some("9".to_string()),
  419. KeyCode::KpDecimal => Some(".".to_string()),
  420. KeyCode::KpDivide => Some("/".to_string()),
  421. KeyCode::KpMultiply => Some("*".to_string()),
  422. KeyCode::KpSubtract => Some("-".to_string()),
  423. _ => None,
  424. }
  425. }
  426. impl Stoppable for EditBox {
  427. async fn stop(&self) {
  428. // TODO: Delete own draw call
  429. // Free buffers
  430. // Should this be in drop?
  431. //self.render_api.delete_buffer(self.vertex_buffer);
  432. //self.render_api.delete_buffer(self.index_buffer);
  433. }
  434. }