mod.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 sled_overlay::sled;
  19. use crate::{
  20. app::{
  21. node::{
  22. create_button, create_chatedit, create_chatview, create_editbox, create_image,
  23. create_layer, create_text, create_vector_art,
  24. },
  25. populate_tree, App,
  26. },
  27. error::Error,
  28. expr::{self, Compiler, Op},
  29. gfx::{GraphicsEventPublisherPtr, Rectangle, RenderApi, Vertex},
  30. mesh::{Color, MeshBuilder},
  31. prop::{
  32. Property, PropertyBool, PropertyFloat32, PropertyStr, PropertySubType, PropertyType, Role,
  33. },
  34. scene::{SceneNodePtr, Slot},
  35. shape,
  36. text::TextShaperPtr,
  37. ui::{
  38. emoji_picker, Button, ChatEdit, ChatView, EditBox, Image, Layer, ShapeVertex, Text,
  39. VectorArt, VectorShape, Window,
  40. },
  41. ExecutorPtr,
  42. };
  43. mod chat;
  44. mod menu;
  45. //mod test;
  46. #[cfg(target_os = "android")]
  47. mod ui_consts {
  48. pub const CHATDB_PATH: &str = "/data/data/darkfi.darkwallet/chatdb/";
  49. pub const BG_PATH: &str = "bg.png";
  50. pub const EMOJI_PICKER_ICON_SIZE: f32 = 100.;
  51. }
  52. #[cfg(all(
  53. any(target_os = "linux", target_os = "macos", target_os = "windows"),
  54. not(feature = "emulate-android")
  55. ))]
  56. mod ui_consts {
  57. pub const CHATDB_PATH: &str = "chatdb";
  58. pub const BG_PATH: &str = "assets/bg.png";
  59. pub const EMOJI_PICKER_ICON_SIZE: f32 = 40.;
  60. }
  61. use ui_consts::*;
  62. pub static CHANNELS: &'static [&str] =
  63. &["dev", "media", "hackers", "memes", "philosophy", "markets", "math", "random"];
  64. pub async fn make(app: &App, window: SceneNodePtr) {
  65. let mut cc = Compiler::new();
  66. // Bg layer
  67. let layer_node = create_layer("bg_layer");
  68. let prop = layer_node.get_property("rect").unwrap();
  69. prop.set_f32(Role::App, 0, 0.).unwrap();
  70. prop.set_f32(Role::App, 1, 0.).unwrap();
  71. prop.set_expr(Role::App, 2, expr::load_var("w")).unwrap();
  72. prop.set_expr(Role::App, 3, expr::load_var("h")).unwrap();
  73. layer_node.set_property_bool(Role::App, "is_visible", true).unwrap();
  74. layer_node.set_property_u32(Role::App, "z_index", 0).unwrap();
  75. let layer_node =
  76. layer_node.setup(|me| Layer::new(me, app.render_api.clone(), app.ex.clone())).await;
  77. window.clone().link(layer_node.clone());
  78. // Create a bg image
  79. let node = create_image("bg_image");
  80. let prop = node.get_property("rect").unwrap();
  81. prop.set_f32(Role::App, 0, 0.).unwrap();
  82. prop.set_f32(Role::App, 1, 0.).unwrap();
  83. prop.set_expr(Role::App, 2, expr::load_var("w")).unwrap();
  84. prop.set_expr(Role::App, 3, expr::load_var("h")).unwrap();
  85. // Image aspect ratio
  86. //let R = 1.78;
  87. let R = 1.555;
  88. cc.add_const_f32("R", R);
  89. let prop = node.get_property("uv").unwrap();
  90. prop.set_f32(Role::App, 0, 0.).unwrap();
  91. prop.set_f32(Role::App, 1, 0.).unwrap();
  92. #[rustfmt::skip]
  93. let code = cc.compile("
  94. r = w / h;
  95. if r < R {
  96. r / R
  97. } else {
  98. 1
  99. }
  100. ").unwrap();
  101. prop.set_expr(Role::App, 2, code).unwrap();
  102. #[rustfmt::skip]
  103. let code = cc.compile("
  104. r = w / h;
  105. if r < R {
  106. 1
  107. } else {
  108. R / r
  109. }
  110. ").unwrap();
  111. prop.set_expr(Role::App, 3, code).unwrap();
  112. node.set_property_str(Role::App, "path", BG_PATH).unwrap();
  113. node.set_property_u32(Role::App, "z_index", 0).unwrap();
  114. let node = node.setup(|me| Image::new(me, app.render_api.clone(), app.ex.clone())).await;
  115. layer_node.clone().link(node);
  116. // Create a bg mesh on top to fade the bg image
  117. let node = create_vector_art("bg");
  118. let prop = node.get_property("rect").unwrap();
  119. prop.set_f32(Role::App, 0, 0.).unwrap();
  120. prop.set_f32(Role::App, 1, 0.).unwrap();
  121. prop.set_expr(Role::App, 2, expr::load_var("w")).unwrap();
  122. prop.set_expr(Role::App, 3, expr::load_var("h")).unwrap();
  123. node.set_property_u32(Role::App, "z_index", 1).unwrap();
  124. //let c = if LIGHTMODE { 1. } else { 0. };
  125. let c = 0.;
  126. // Setup the pimpl
  127. let node_id = node.id;
  128. let mut shape = VectorShape::new();
  129. shape.add_filled_box(
  130. expr::const_f32(0.),
  131. expr::const_f32(0.),
  132. expr::load_var("w"),
  133. expr::load_var("h"),
  134. [c, c, c, 0.3],
  135. );
  136. let node =
  137. node.setup(|me| VectorArt::new(me, shape, app.render_api.clone(), app.ex.clone())).await;
  138. layer_node.clone().link(node);
  139. let emoji_meshes = emoji_picker::EmojiMeshes::new(
  140. app.render_api.clone(),
  141. app.text_shaper.clone(),
  142. EMOJI_PICKER_ICON_SIZE,
  143. );
  144. let db = sled::open(CHATDB_PATH).expect("cannot open sleddb");
  145. for channel in CHANNELS {
  146. chat::make(app, window.clone(), channel, &db, emoji_meshes.clone()).await;
  147. }
  148. menu::make(app, window.clone()).await;
  149. // @@@ Debug stuff @@@
  150. let chatview_node = app.sg_root.clone().lookup_node("/window/dev_chat_layer").unwrap();
  151. chatview_node.set_property_bool(Role::App, "is_visible", true).unwrap();
  152. let menu_node = app.sg_root.clone().lookup_node("/window/menu_layer").unwrap();
  153. menu_node.set_property_bool(Role::App, "is_visible", false).unwrap();
  154. }