Browse Source

rendering multiline dialogue boxes

Inderjit Gill 1 year ago
parent
commit
ebcb67c964
4 changed files with 79 additions and 4 deletions
  1. BIN
      research/tileset.pdn
  2. 59
    4
      src/geometry.rs
  3. 20
    0
      src/sprite.rs
  4. BIN
      web/img/tileset.png

BIN
research/tileset.pdn View File


+ 59
- 4
src/geometry.rs View File

@@ -215,18 +215,48 @@ impl Geometry {
215 215
         self.push_filled(x1, y1, x2, y2, c.r, c.g, c.b, c.a);
216 216
     }
217 217
 
218
-    // renders a rectangle over the TileFrame
219
-    // frame should be in screen co-ordinates
220 218
     pub fn push_filled_block(&mut self, block: Block2D, c: Col) {
221 219
         let w = self.block_width;
222 220
         let h = self.block_height;
223 221
 
224 222
         let x1 = (block.x as f32) * w;
225 223
         let y1 = (block.y as f32) * h;
226
-        let x2 = ((block.x + 1) as f32) * w;
227
-        let y2 = ((block.y + 1) as f32) * h;
224
+        let x2 = x1 + w;
225
+        let y2 = y1 + h;
226
+
227
+        self.push_filled(x1, y1, x2, y2, c.r, c.g, c.b, c.a);
228
+    }
229
+
230
+    // block is position of lower left hand block
231
+    // width and height are in block coordinates
232
+    //
233
+    pub fn push_filled_dialogue_bg(&mut self, block: Block2D, width: usize, height: usize, c: Col) {
234
+        let w = self.block_width;
235
+        let h = self.block_height;
236
+
237
+        let x1 = (block.x as f32) * w;
238
+        let y1 = (block.y as f32) * h;
239
+        let x2 = x1 + (width as f32 * w);
240
+        let y2 = y1 + (height as f32 * h);
228 241
 
229 242
         self.push_filled(x1, y1, x2, y2, c.r, c.g, c.b, c.a);
243
+
244
+        let no_offset = Vec2D { x: 0.0, y: 0.0};
245
+        for y in block.y..(block.y + height as i32) {
246
+            self.push_sprite(Sprite::BorderLeft, c, Block2D { x: block.x - 1, y }, no_offset);
247
+            self.push_sprite(Sprite::BorderRight, c, Block2D { x: block.x + width as i32, y }, no_offset);
248
+        }
249
+
250
+        for x in block.x..(block.x + width as i32) {
251
+            self.push_sprite(Sprite::BorderBottom, c, Block2D { x, y: block.y - 1}, no_offset);
252
+            self.push_sprite(Sprite::BorderTop, c, Block2D { x, y: block.y + height as i32}, no_offset);
253
+        }
254
+
255
+        self.push_sprite(Sprite::BorderBottomLeft, c, Block2D { x: block.x - 1, y: block.y - 1 }, no_offset);
256
+        self.push_sprite(Sprite::BorderBottomRight, c, Block2D { x: block.x + width as i32, y: block.y - 1 }, no_offset);
257
+
258
+        self.push_sprite(Sprite::BorderTopLeft, c, Block2D { x: block.x - 1, y: block.y + height as i32 }, no_offset);
259
+        self.push_sprite(Sprite::BorderTopRight, c, Block2D { x: block.x + width as i32, y: block.y + height as i32 }, no_offset);
230 260
     }
231 261
 
232 262
     pub fn push_outline_block(&mut self, block: Block2D, thickness: f32, c: Col) {
@@ -279,6 +309,31 @@ impl Geometry {
279 309
         }
280 310
     }
281 311
 
312
+    // render first line at pos
313
+    // remaining lines in pos.y - n
314
+    pub fn push_centred_boxed_multiline(&mut self, lines: &Vec<&str>, centre: Block2D, text_col: Col, background_colour: Col) {
315
+
316
+        let longest_length = lines.iter().fold(0, |longest, line|
317
+                                               if line.len() > longest {
318
+                                                   line.len()
319
+                                               } else {
320
+                                                   longest
321
+                                               });
322
+
323
+        let height = lines.len();
324
+        let width = longest_length / 2;
325
+        let pos = Block2D { x: centre.x - (width as i32 / 2), y: centre.y + (height as i32 / 2)};
326
+
327
+        self.push_filled_dialogue_bg(Block2D { x: pos.x, y: pos.y - (height as i32 - 1)},
328
+                                     width,
329
+                                     height,
330
+                                     background_colour);
331
+
332
+        for y in 0..height {
333
+            self.push_text(lines[y], Block2D { x: pos.x, y: pos.y - (y as i32) }, text_col);
334
+        }
335
+    }
336
+
282 337
     fn push_char(&mut self, c: char, pos: Vec2D, col: Col, char_w: f32, char_h: f32) -> Vec2D {
283 338
         let (t1, t2, t3, t4) = self.get_char_uv(c);
284 339
 

+ 20
- 0
src/sprite.rs View File

@@ -9,18 +9,38 @@ pub struct SpriteLocation {
9 9
 #[derive(Debug, Clone, Copy)]
10 10
 pub enum Sprite {
11 11
     Block,
12
+
12 13
     DebugChecker,
13 14
     DebugBlank,
14 15
     DebugFilled,
15 16
     Debug4Corners,
17
+
18
+    BorderTop,
19
+    BorderTopRight,
20
+    BorderRight,
21
+    BorderBottomRight,
22
+    BorderBottom,
23
+    BorderLeft,
24
+    BorderBottomLeft,
25
+    BorderTopLeft,
16 26
 }
17 27
 
18 28
 pub fn get_sprite_location(sprite: Sprite) -> SpriteLocation {
19 29
     match sprite {
20 30
         Sprite::Block => SpriteLocation { row: 6, col: 9 },
31
+
21 32
         Sprite::DebugChecker => SpriteLocation { row: 0, col: 13 },
22 33
         Sprite::DebugBlank => SpriteLocation { row: 0, col: 14 },
23 34
         Sprite::DebugFilled => SpriteLocation { row: 0, col: 15 },
24 35
         Sprite::Debug4Corners => SpriteLocation { row: 0, col: 16 },
36
+
37
+        Sprite::BorderTop => SpriteLocation { row: 0, col: 17 },
38
+        Sprite::BorderTopRight => SpriteLocation { row: 0, col: 18 },
39
+        Sprite::BorderRight => SpriteLocation { row: 0, col: 19 },
40
+        Sprite::BorderBottomRight => SpriteLocation { row: 0, col: 20 },
41
+        Sprite::BorderBottom => SpriteLocation { row: 0, col: 21 },
42
+        Sprite::BorderLeft => SpriteLocation { row: 0, col: 22 },
43
+        Sprite::BorderBottomLeft => SpriteLocation { row: 0, col: 23 },
44
+        Sprite::BorderTopLeft => SpriteLocation { row: 0, col: 24 },
25 45
     }
26 46
 }

BIN
web/img/tileset.png View File


Loading…
Cancel
Save