chatapp.rs 15 KB

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