Browse Source

app/gfx: add new Overlay instruction to draw temp ui hints above everything else

jkds 7 months ago
parent
commit
4e997b2f27
2 changed files with 55 additions and 1 deletions
  1. 50 0
      bin/app/src/gfx/mod.rs
  2. 5 1
      bin/app/src/ui/edit/mod.rs

+ 50 - 0
bin/app/src/gfx/mod.rs

@@ -477,6 +477,7 @@ pub enum DrawInstruction {
     Animation(ManagedSeqAnimPtr),
     EnableDebug,
     SetPipeline(GraphicPipeline),
+    Overlay(Vec<DrawInstruction>),
 }
 
 impl DrawInstruction {
@@ -497,6 +498,11 @@ impl DrawInstruction {
             Self::Animation(anim) => GfxDrawInstruction::Animation(anim),
             Self::EnableDebug => GfxDrawInstruction::EnableDebug,
             Self::SetPipeline(pipeline) => GfxDrawInstruction::SetPipeline(pipeline),
+            Self::Overlay(instrs) => {
+                let compiled =
+                    instrs.into_iter().map(|i| i.compile(textures, buffers, debug_str)).collect();
+                GfxDrawInstruction::Overlay(compiled)
+            }
         }
     }
 }
@@ -558,6 +564,7 @@ enum GfxDrawInstruction {
     Animation(ManagedSeqAnimPtr),
     EnableDebug,
     SetPipeline(GraphicPipeline),
+    Overlay(Vec<GfxDrawInstruction>),
 }
 
 #[derive(Clone, Debug)]
@@ -568,6 +575,11 @@ struct GfxDrawCall {
     timest: Timestamp,
 }
 
+struct OverlayDefer {
+    pos: Point,
+    instrs: Vec<GfxDrawInstruction>,
+}
+
 struct RenderContext<'a> {
     ctx: &'a mut Box<dyn RenderingBackend>,
     draw_calls: &'a HashMap<DcId, GfxDrawCall>,
@@ -581,6 +593,7 @@ struct RenderContext<'a> {
     gfx_pipeline: GraphicPipeline,
 
     anims: &'a mut HashMap<AnimId, GfxSeqAnim>,
+    overlays: Vec<OverlayDefer>,
 }
 
 impl<'a> RenderContext<'a> {
@@ -596,6 +609,38 @@ impl<'a> RenderContext<'a> {
         if DEBUG_RENDER {
             d!("RenderContext::draw() [DONE]");
         }
+        // View should be reset now so draw overlays
+        self.draw_overlays();
+    }
+
+    fn draw_overlays(&mut self) {
+        let overlays = std::mem::take(&mut self.overlays);
+        for overlay in overlays {
+            self.cursor = overlay.pos;
+            self.apply_model();
+
+            for instr in overlay.instrs {
+                match instr {
+                    GfxDrawInstruction::Draw(mesh) => {
+                        if DEBUG_RENDER {
+                            d!("    draw_overlay({mesh:?})");
+                        }
+                        let images = match &mesh.textures {
+                            Some(texs) => texs.iter().map(|(_, tex_id)| *tex_id).collect(),
+                            None => vec![self.white_texture],
+                        };
+                        let bindings = Bindings {
+                            vertex_buffers: vec![mesh.vertex_buffer],
+                            index_buffer: mesh.index_buffer,
+                            images,
+                        };
+                        self.ctx.apply_bindings(&bindings);
+                        self.ctx.draw(0, mesh.num_elements, 1);
+                    }
+                    _ => unimplemented!(),
+                }
+            }
+        }
     }
 
     fn apply_view(&mut self) {
@@ -739,6 +784,10 @@ impl<'a> RenderContext<'a> {
                         d!("{ws}set_pipeline({pipeline:?})");
                     }
                 }
+                GfxDrawInstruction::Overlay(instrs) => {
+                    let pos = self.view.pos() + self.cursor;
+                    self.overlays.push(OverlayDefer { pos, instrs: instrs.clone() });
+                }
             }
         }
 
@@ -1771,6 +1820,7 @@ impl EventHandler for Stage {
             cursor: Point::from([0., 0.]),
             gfx_pipeline: GraphicPipeline::RGB,
             anims: &mut self.anims,
+            overlays: vec![],
         };
         render_ctx.draw();
 

+ 5 - 1
bin/app/src/ui/edit/mod.rs

@@ -193,6 +193,7 @@ pub struct BaseEdit {
     select_dc_key: u64,
     text_dc_key: u64,
     cursor_dc_key: u64,
+    overlay_dc_key: u64,
     cursor_mesh: SyncMutex<Option<DrawMesh>>,
 
     is_active: PropertyBool,
@@ -342,6 +343,7 @@ impl BaseEdit {
             select_dc_key: OsRng.gen(),
             text_dc_key: OsRng.gen(),
             cursor_dc_key: OsRng.gen(),
+            overlay_dc_key: OsRng.gen(),
             cursor_mesh: SyncMutex::new(None),
 
             is_active,
@@ -1003,6 +1005,7 @@ impl BaseEdit {
                     self.text_dc_key,
                     self.phone_select_handle_dc_key,
                     self.cursor_dc_key,
+                    self.overlay_dc_key,
                     self.select_dc_key,
                 ],
                 0,
@@ -1161,7 +1164,7 @@ impl BaseEdit {
                     self.root_dc_key,
                     DrawCall::new(
                         vec![DrawInstruction::Move(rect.pos())],
-                        vec![self.content_dc_key],
+                        vec![self.content_dc_key, self.overlay_dc_key],
                         self.z_index.get(),
                         "chatedit_root",
                     ),
@@ -1187,6 +1190,7 @@ impl BaseEdit {
                     self.phone_select_handle_dc_key,
                     DrawCall::new(phone_sel_instrs, vec![], 1, "chatedit_phone_sel"),
                 ),
+                (self.overlay_dc_key, DrawCall::new(vec![], vec![], 0, "chatedit_overlay")),
             ],
         }
     }