chatapp.rs 14 KB

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