Browse Source

rendering multiline dialogue boxes

master
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 {
self.push_filled(x1, y1, x2, y2, c.r, c.g, c.b, c.a);
}

// renders a rectangle over the TileFrame
// frame should be in screen co-ordinates
pub fn push_filled_block(&mut self, block: Block2D, c: Col) {
let w = self.block_width;
let h = self.block_height;

let x1 = (block.x as f32) * w;
let y1 = (block.y as f32) * h;
let x2 = ((block.x + 1) as f32) * w;
let y2 = ((block.y + 1) as f32) * h;
let x2 = x1 + w;
let y2 = y1 + h;

self.push_filled(x1, y1, x2, y2, c.r, c.g, c.b, c.a);
}

// block is position of lower left hand block
// width and height are in block coordinates
//
pub fn push_filled_dialogue_bg(&mut self, block: Block2D, width: usize, height: usize, c: Col) {
let w = self.block_width;
let h = self.block_height;

let x1 = (block.x as f32) * w;
let y1 = (block.y as f32) * h;
let x2 = x1 + (width as f32 * w);
let y2 = y1 + (height as f32 * h);

self.push_filled(x1, y1, x2, y2, c.r, c.g, c.b, c.a);

let no_offset = Vec2D { x: 0.0, y: 0.0};
for y in block.y..(block.y + height as i32) {
self.push_sprite(Sprite::BorderLeft, c, Block2D { x: block.x - 1, y }, no_offset);
self.push_sprite(Sprite::BorderRight, c, Block2D { x: block.x + width as i32, y }, no_offset);
}

for x in block.x..(block.x + width as i32) {
self.push_sprite(Sprite::BorderBottom, c, Block2D { x, y: block.y - 1}, no_offset);
self.push_sprite(Sprite::BorderTop, c, Block2D { x, y: block.y + height as i32}, no_offset);
}

self.push_sprite(Sprite::BorderBottomLeft, c, Block2D { x: block.x - 1, y: block.y - 1 }, no_offset);
self.push_sprite(Sprite::BorderBottomRight, c, Block2D { x: block.x + width as i32, y: block.y - 1 }, no_offset);

self.push_sprite(Sprite::BorderTopLeft, c, Block2D { x: block.x - 1, y: block.y + height as i32 }, no_offset);
self.push_sprite(Sprite::BorderTopRight, c, Block2D { x: block.x + width as i32, y: block.y + height as i32 }, no_offset);
}

pub fn push_outline_block(&mut self, block: Block2D, thickness: f32, c: Col) {
@@ -279,6 +309,31 @@ impl Geometry {
}
}

// render first line at pos
// remaining lines in pos.y - n
pub fn push_centred_boxed_multiline(&mut self, lines: &Vec<&str>, centre: Block2D, text_col: Col, background_colour: Col) {

let longest_length = lines.iter().fold(0, |longest, line|
if line.len() > longest {
line.len()
} else {
longest
});

let height = lines.len();
let width = longest_length / 2;
let pos = Block2D { x: centre.x - (width as i32 / 2), y: centre.y + (height as i32 / 2)};

self.push_filled_dialogue_bg(Block2D { x: pos.x, y: pos.y - (height as i32 - 1)},
width,
height,
background_colour);

for y in 0..height {
self.push_text(lines[y], Block2D { x: pos.x, y: pos.y - (y as i32) }, text_col);
}
}

fn push_char(&mut self, c: char, pos: Vec2D, col: Col, char_w: f32, char_h: f32) -> Vec2D {
let (t1, t2, t3, t4) = self.get_char_uv(c);


+ 20
- 0
src/sprite.rs View File

@@ -9,18 +9,38 @@ pub struct SpriteLocation {
#[derive(Debug, Clone, Copy)]
pub enum Sprite {
Block,

DebugChecker,
DebugBlank,
DebugFilled,
Debug4Corners,

BorderTop,
BorderTopRight,
BorderRight,
BorderBottomRight,
BorderBottom,
BorderLeft,
BorderBottomLeft,
BorderTopLeft,
}

pub fn get_sprite_location(sprite: Sprite) -> SpriteLocation {
match sprite {
Sprite::Block => SpriteLocation { row: 6, col: 9 },

Sprite::DebugChecker => SpriteLocation { row: 0, col: 13 },
Sprite::DebugBlank => SpriteLocation { row: 0, col: 14 },
Sprite::DebugFilled => SpriteLocation { row: 0, col: 15 },
Sprite::Debug4Corners => SpriteLocation { row: 0, col: 16 },

Sprite::BorderTop => SpriteLocation { row: 0, col: 17 },
Sprite::BorderTopRight => SpriteLocation { row: 0, col: 18 },
Sprite::BorderRight => SpriteLocation { row: 0, col: 19 },
Sprite::BorderBottomRight => SpriteLocation { row: 0, col: 20 },
Sprite::BorderBottom => SpriteLocation { row: 0, col: 21 },
Sprite::BorderLeft => SpriteLocation { row: 0, col: 22 },
Sprite::BorderBottomLeft => SpriteLocation { row: 0, col: 23 },
Sprite::BorderTopLeft => SpriteLocation { row: 0, col: 24 },
}
}

BIN
web/img/tileset.png View File

Before After
Width: 512  |  Height: 512  |  Size: 13KB Width: 512  |  Height: 512  |  Size: 13KB

Loading…
Cancel
Save