mod.rs 4.7 KB

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