chatapp.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_serial::Encodable;
  19. use crate::{
  20. expr::Op,
  21. gfx::Rectangle,
  22. prop::{Property, PropertySubType, PropertyType},
  23. scene::{
  24. SceneGraph, SceneNodeId,
  25. SceneNodeType,
  26. },
  27. };
  28. struct Buffer {
  29. verts: Vec<u8>,
  30. faces: Vec<u8>,
  31. verts_len: u32,
  32. }
  33. impl Buffer {
  34. pub fn new() -> Self {
  35. Self { verts: vec![], faces: vec![], verts_len: 0 }
  36. }
  37. fn vertex(&mut self, x: f32, y: f32, r: f32, g: f32, b: f32, a: f32, u: f32, v: f32) {
  38. // xy
  39. x.encode(&mut self.verts).unwrap();
  40. y.encode(&mut self.verts).unwrap();
  41. // rgba
  42. r.encode(&mut self.verts).unwrap();
  43. g.encode(&mut self.verts).unwrap();
  44. b.encode(&mut self.verts).unwrap();
  45. a.encode(&mut self.verts).unwrap();
  46. // uv
  47. u.encode(&mut self.verts).unwrap();
  48. v.encode(&mut self.verts).unwrap();
  49. self.verts_len += 1
  50. }
  51. fn face(&mut self, i1: u32, i2: u32, i3: u32) {
  52. i1.encode(&mut self.faces).unwrap();
  53. i2.encode(&mut self.faces).unwrap();
  54. i3.encode(&mut self.faces).unwrap();
  55. }
  56. pub fn draw_box(&mut self, rect: Rectangle<f32>, color: [f32; 4]) {
  57. let k = self.verts_len;
  58. let x = rect.x;
  59. let y = rect.y;
  60. let w = rect.w;
  61. let h = rect.h;
  62. let r = color[0];
  63. let g = color[1];
  64. let b = color[2];
  65. let a = color[3];
  66. self.vertex(x, y, r, g, b, a, 0., 0.);
  67. self.vertex(x + w, y, r, g, b, a, 1., 0.);
  68. self.vertex(x, y + h, r, g, b, a, 0., 1.);
  69. self.vertex(x + w, y + h, r, g, b, a, 1., 1.);
  70. self.face(k, k + 2, k + 1);
  71. self.face(k + 1, k + 2, k + 3);
  72. }
  73. pub fn draw_outline(
  74. &mut self,
  75. rect: Rectangle<f32>,
  76. color: [f32; 4],
  77. pad: f32,
  78. layer_w: f32,
  79. layer_h: f32,
  80. ) {
  81. let pad_x = pad / layer_w;
  82. let pad_y = pad / layer_h;
  83. let x = rect.x;
  84. let y = rect.y;
  85. let w = rect.w;
  86. let h = rect.h;
  87. // left
  88. self.draw_box(Rectangle { x, y, w: pad_x, h }, color);
  89. // top
  90. self.draw_box(Rectangle { x, y, w, h: pad_y }, color);
  91. // right
  92. let rhs = x + w;
  93. self.draw_box(Rectangle { x: rhs - pad_x, y, w: pad_x, h }, color);
  94. // bottom
  95. let bhs = y + h;
  96. self.draw_box(Rectangle { x, y: bhs - pad_y, w, h: pad_y }, color);
  97. }
  98. }
  99. pub fn create_layer(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
  100. let win_id = sg.lookup_node("/window").unwrap().id;
  101. let node = sg.add_node(name, SceneNodeType::RenderLayer);
  102. let mut prop = Property::new("is_visible", PropertyType::Bool, PropertySubType::Null);
  103. node.add_property(prop).unwrap();
  104. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  105. prop.set_array_len(4);
  106. prop.allow_exprs();
  107. node.add_property(prop).unwrap();
  108. node.id
  109. }
  110. pub fn create_mesh(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
  111. let node = sg.add_node(name, SceneNodeType::RenderMesh);
  112. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  113. prop.set_array_len(4);
  114. prop.allow_exprs();
  115. node.add_property(prop).unwrap();
  116. let mut prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  117. node.add_property(prop).unwrap();
  118. node.id
  119. }
  120. fn create_text(sg: &mut SceneGraph, name: &str, layer_node_id: SceneNodeId) -> SceneNodeId {
  121. let node = sg.add_node(name, SceneNodeType::RenderText);
  122. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  123. prop.set_array_len(4);
  124. prop.allow_exprs();
  125. node.add_property(prop).unwrap();
  126. let mut prop = Property::new("baseline", PropertyType::Float32, PropertySubType::Pixel);
  127. node.add_property(prop).unwrap();
  128. let mut prop = Property::new("font_size", PropertyType::Float32, PropertySubType::Pixel);
  129. node.add_property(prop).unwrap();
  130. let mut prop = Property::new("text", PropertyType::Str, PropertySubType::Null);
  131. node.add_property(prop).unwrap();
  132. let mut prop = Property::new("color", PropertyType::Float32, PropertySubType::Color);
  133. prop.set_array_len(4);
  134. prop.set_range_f32(0., 1.);
  135. node.add_property(prop).unwrap();
  136. let mut prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  137. node.add_property(prop).unwrap();
  138. let mut prop = Property::new("debug", PropertyType::Bool, PropertySubType::Null);
  139. node.add_property(prop).unwrap();
  140. let node_id = node.id;
  141. sg.link(node_id, layer_node_id).unwrap();
  142. node_id
  143. }
  144. fn create_editbox(sg: &mut SceneGraph, name: &str, layer_node_id: SceneNodeId) -> SceneNodeId {
  145. let node = sg.add_node(name, SceneNodeType::EditBox);
  146. let mut prop = Property::new("is_active", PropertyType::Bool, PropertySubType::Null);
  147. node.add_property(prop).unwrap();
  148. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  149. prop.set_array_len(4);
  150. prop.allow_exprs();
  151. node.add_property(prop).unwrap();
  152. let mut prop = Property::new("baseline", PropertyType::Float32, PropertySubType::Pixel);
  153. node.add_property(prop).unwrap();
  154. let mut prop = Property::new("scroll", PropertyType::Float32, PropertySubType::Pixel);
  155. node.add_property(prop).unwrap();
  156. let mut prop = Property::new("cursor_pos", PropertyType::Uint32, PropertySubType::Pixel);
  157. node.add_property(prop).unwrap();
  158. let mut prop = Property::new("font_size", PropertyType::Float32, PropertySubType::Pixel);
  159. node.add_property(prop).unwrap();
  160. let mut prop = Property::new("text", PropertyType::Str, PropertySubType::Null);
  161. node.add_property(prop).unwrap();
  162. let mut prop = Property::new("text_color", PropertyType::Float32, PropertySubType::Color);
  163. prop.set_array_len(4);
  164. prop.set_range_f32(0., 1.);
  165. node.add_property(prop).unwrap();
  166. let mut prop = Property::new("cursor_color", PropertyType::Float32, PropertySubType::Color);
  167. prop.set_array_len(4);
  168. prop.set_range_f32(0., 1.);
  169. node.add_property(prop).unwrap();
  170. let mut prop = Property::new("hi_bg_color", PropertyType::Float32, PropertySubType::Color);
  171. prop.set_array_len(4);
  172. prop.set_range_f32(0., 1.);
  173. node.add_property(prop).unwrap();
  174. let mut prop = Property::new("selected", PropertyType::Uint32, PropertySubType::Color);
  175. prop.set_array_len(2);
  176. prop.allow_null_values();
  177. node.add_property(prop).unwrap();
  178. let mut prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  179. node.add_property(prop).unwrap();
  180. let mut prop = Property::new("debug", PropertyType::Bool, PropertySubType::Null);
  181. node.add_property(prop).unwrap();
  182. let node_id = node.id;
  183. sg.link(node_id, layer_node_id).unwrap();
  184. node_id
  185. }
  186. fn create_chatview(sg: &mut SceneGraph, name: &str, layer_node_id: SceneNodeId) -> SceneNodeId {
  187. let node = sg.add_node(name, SceneNodeType::ChatView);
  188. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  189. prop.set_array_len(4);
  190. prop.allow_exprs();
  191. node.add_property(prop).unwrap();
  192. let mut prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  193. node.add_property(prop).unwrap();
  194. let mut prop = Property::new("debug", PropertyType::Bool, PropertySubType::Null);
  195. node.add_property(prop).unwrap();
  196. let node_id = node.id;
  197. let mut arg_data = vec![];
  198. node_id.encode(&mut arg_data).unwrap();
  199. //let (tx, rx) = mpsc::sync_channel::<Result<Vec<u8>>>(0);
  200. let response_fn = Box::new(move |_result| {
  201. //tx.send(result).unwrap();
  202. });
  203. let win_node = sg.lookup_node_mut("/window").unwrap();
  204. win_node.call_method("create_chat_view", arg_data, response_fn).unwrap();
  205. //let _result = rx.recv().unwrap();
  206. sg.link(node_id, layer_node_id).unwrap();
  207. node_id
  208. }
  209. pub fn setup(sg: &mut SceneGraph) {
  210. let layer_node_id = create_layer(sg, "view");
  211. let node = sg.get_node(layer_node_id).unwrap();
  212. let prop = node.get_property("rect").unwrap();
  213. prop.set_u32(0, 0).unwrap();
  214. prop.set_u32(1, 0).unwrap();
  215. let code = vec![Op::Float32ToUint32((Box::new(Op::LoadVar("sw".to_string()))))];
  216. prop.set_expr(2, code).unwrap();
  217. let code = vec![Op::Float32ToUint32((Box::new(Op::LoadVar("sh".to_string()))))];
  218. prop.set_expr(3, code).unwrap();
  219. // Make the black background
  220. // Maybe we should use a RenderPass for this instead
  221. let node_id = create_mesh(sg, "bg");
  222. let node = sg.get_node(node_id).unwrap();
  223. let prop = node.get_property("rect").unwrap();
  224. prop.set_f32(0, 0.).unwrap();
  225. prop.set_f32(1, 0.).unwrap();
  226. let code = vec![Op::LoadVar("lw".to_string())];
  227. prop.set_expr(2, code).unwrap();
  228. let code = vec![Op::LoadVar("lh".to_string())];
  229. prop.set_expr(3, code).unwrap();
  230. let prop = node.get_property("data").unwrap();
  231. let mut buff = Buffer::new();
  232. buff.draw_box(Rectangle { x: 0., y: 0., w: 1., h: 1. }, [0.05, 0.05, 0.05, 1.]);
  233. prop.set_buf(0, buff.verts).unwrap();
  234. prop.set_buf(1, buff.faces).unwrap();
  235. // Make the chatedit bg
  236. let node_id = create_mesh(sg, "chateditbg");
  237. let node = sg.get_node(node_id).unwrap();
  238. let prop = node.get_property("rect").unwrap();
  239. let prop = node.get_property("rect").unwrap();
  240. prop.set_f32(0, 140.).unwrap();
  241. let code =
  242. vec![Op::Sub((Box::new(Op::LoadVar("lh".to_string())), Box::new(Op::ConstFloat32(60.))))];
  243. prop.set_expr(1, code).unwrap();
  244. let code =
  245. vec![Op::Sub((Box::new(Op::LoadVar("lw".to_string())), Box::new(Op::ConstFloat32(140.))))];
  246. prop.set_expr(2, code).unwrap();
  247. prop.set_f32(3, 60.).unwrap();
  248. let prop = node.get_property("data").unwrap();
  249. let mut buff = Buffer::new();
  250. buff.draw_box(Rectangle { x: 0., y: 0., w: 1., h: 1. }, [0., 0.13, 0.08, 1.]);
  251. // FIXME: layer dim is passed here manually!
  252. // we should just use separate objs
  253. buff.draw_outline(
  254. Rectangle { x: 0., y: 0., w: 1., h: 1. },
  255. [0.22, 0.22, 0.22, 1.],
  256. 1.,
  257. 1000.,
  258. 50.,
  259. );
  260. prop.set_buf(0, buff.verts).unwrap();
  261. prop.set_buf(1, buff.faces).unwrap();
  262. // Make the nicktext border
  263. let node_id = create_mesh(sg, "nickbg");
  264. let node = sg.get_node(node_id).unwrap();
  265. let prop = node.get_property("rect").unwrap();
  266. prop.set_f32(0, 0.).unwrap();
  267. let code =
  268. vec![Op::Sub((Box::new(Op::LoadVar("lh".to_string())), Box::new(Op::ConstFloat32(60.))))];
  269. prop.set_expr(1, code).unwrap();
  270. prop.set_f32(2, 130.).unwrap();
  271. prop.set_f32(3, 60.).unwrap();
  272. let prop = node.get_property("data").unwrap();
  273. let mut buff = Buffer::new();
  274. // FIXME: layer dim is passed here manually!
  275. // we should just use separate objs
  276. buff.draw_outline(
  277. Rectangle { x: 0., y: 0., w: 1., h: 1. },
  278. [0., 0.13, 0.08, 1.],
  279. 1.,
  280. 1000.,
  281. 50.,
  282. );
  283. prop.set_buf(0, buff.verts).unwrap();
  284. prop.set_buf(1, buff.faces).unwrap();
  285. // Nickname
  286. let node_id = create_text(sg, "nick", layer_node_id);
  287. let node = sg.get_node(node_id).unwrap();
  288. let prop = node.get_property("rect").unwrap();
  289. prop.set_f32(0, 20.).unwrap();
  290. let code =
  291. vec![Op::Sub((Box::new(Op::LoadVar("lh".to_string())), Box::new(Op::ConstFloat32(60.))))];
  292. prop.set_expr(1, code).unwrap();
  293. prop.set_f32(2, 120.).unwrap();
  294. prop.set_f32(3, 60.).unwrap();
  295. node.set_property_f32("baseline", 40.).unwrap();
  296. node.set_property_f32("font_size", 20.).unwrap();
  297. node.set_property_str("text", "anon1").unwrap();
  298. let prop = node.get_property("color").unwrap();
  299. prop.set_f32(0, 0.).unwrap();
  300. prop.set_f32(1, 1.).unwrap();
  301. prop.set_f32(2, 0.).unwrap();
  302. prop.set_f32(3, 1.).unwrap();
  303. node.set_property_u32("z_index", 1).unwrap();
  304. // Text edit
  305. let node_id = create_editbox(sg, "editz", layer_node_id);
  306. let node = sg.get_node(node_id).unwrap();
  307. node.set_property_bool("is_active", true).unwrap();
  308. let prop = node.get_property("rect").unwrap();
  309. prop.set_f32(0, 150.).unwrap();
  310. let code =
  311. vec![Op::Sub((Box::new(Op::LoadVar("lh".to_string())), Box::new(Op::ConstFloat32(60.))))];
  312. prop.set_expr(1, code).unwrap();
  313. let code =
  314. vec![Op::Sub((Box::new(Op::LoadVar("lw".to_string())), Box::new(Op::ConstFloat32(120.))))];
  315. prop.set_expr(2, code).unwrap();
  316. prop.set_f32(3, 60.).unwrap();
  317. node.set_property_f32("baseline", 40.).unwrap();
  318. node.set_property_f32("font_size", 20.).unwrap();
  319. //node.set_property_str("text", "hello king!😁🍆jelly 🍆1234").unwrap();
  320. node.set_property_str("text", "hello king! jelly 1234").unwrap();
  321. let prop = node.get_property("text_color").unwrap();
  322. prop.set_f32(0, 1.).unwrap();
  323. prop.set_f32(1, 1.).unwrap();
  324. prop.set_f32(2, 1.).unwrap();
  325. prop.set_f32(3, 1.).unwrap();
  326. let prop = node.get_property("cursor_color").unwrap();
  327. prop.set_f32(0, 1.).unwrap();
  328. prop.set_f32(1, 0.5).unwrap();
  329. prop.set_f32(2, 0.5).unwrap();
  330. prop.set_f32(3, 1.).unwrap();
  331. let prop = node.get_property("hi_bg_color").unwrap();
  332. prop.set_f32(0, 1.).unwrap();
  333. prop.set_f32(1, 1.).unwrap();
  334. prop.set_f32(2, 1.).unwrap();
  335. prop.set_f32(3, 0.5).unwrap();
  336. let prop = node.get_property("selected").unwrap();
  337. prop.set_null(0).unwrap();
  338. prop.set_null(1).unwrap();
  339. node.set_property_u32("z_index", 1).unwrap();
  340. let node_id = node.id;
  341. let mut arg_data = vec![];
  342. node_id.encode(&mut arg_data).unwrap();
  343. //let (tx, rx) = mpsc::sync_channel::<Result<Vec<u8>>>(0);
  344. let response_fn = Box::new(move |result| {
  345. //tx.send(result).unwrap();
  346. });
  347. let win_node = sg.lookup_node_mut("/window").unwrap();
  348. win_node.call_method("create_edit_box", arg_data, response_fn).unwrap();
  349. //let _result = rx.recv().unwrap();
  350. // ChatView
  351. let node_id = create_chatview(sg, "chatty", layer_node_id);
  352. let node = sg.get_node(node_id).unwrap();
  353. let prop = node.get_property("rect").unwrap();
  354. prop.set_f32(0, 0.).unwrap();
  355. prop.set_f32(1, 0.).unwrap();
  356. let code = vec![Op::LoadVar("lw".to_string())];
  357. prop.set_expr(2, code).unwrap();
  358. let code =
  359. vec![Op::Sub((Box::new(Op::LoadVar("lh".to_string())), Box::new(Op::ConstFloat32(50.))))];
  360. prop.set_expr(3, code).unwrap();
  361. node.set_property_u32("z_index", 1).unwrap();
  362. // On android lets scale the UI up
  363. let win_node = sg.lookup_node_mut("/window").unwrap();
  364. win_node.set_property_f32("scale", 1.6).unwrap();
  365. let layer_node = sg.get_node(layer_node_id).unwrap();
  366. layer_node.set_property_bool("is_visible", true).unwrap();
  367. }