1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2025-06-20 18:01:03 +00:00

colored text and some shameless advertisting

This commit is contained in:
Alula 2020-09-22 21:31:47 +02:00
parent a59c150413
commit 1327fe9a27
No known key found for this signature in database
GPG key ID: 3E00485503A1D8BA
5 changed files with 49 additions and 16 deletions

View file

@ -52,4 +52,5 @@ toml = "0.5"
varint = "0.9.0"
# remove and replace when drain_filter is in stable
vec_mut_scan = "0.3.0"
webbrowser = "0.5.5"
winit = { version = "0.19.3" }

View file

@ -61,19 +61,25 @@ impl BMFontRenderer {
offset_x
}
pub fn draw_text<I: Iterator<Item=char>>(&self, iter: I, x: f32, y: f32, constants: &EngineConstants, texture_set: &mut TextureSet, ctx: &mut Context) -> GameResult {
self.draw_colored_text(iter, x, y, (255, 255, 255), constants, texture_set, ctx)
}
pub fn draw_colored_text<I: Iterator<Item=char>>(&self, iter: I, x: f32, y: f32, color: (u8, u8, u8),
constants: &EngineConstants, texture_set: &mut TextureSet, ctx: &mut Context) -> GameResult {
if self.pages.len() == 1 {
let batch = texture_set.get_or_load_batch(ctx, constants, self.pages.get(0).unwrap())?;
let mut offset_x = x;
for chr in iter {
if let Some(glyph) = self.font.chars.get(&chr) {
batch.add_rect_scaled(offset_x, y + (glyph.yoffset as f32 * constants.font_scale).floor(),
constants.font_scale, constants.font_scale,
&Rect::<usize>::new_size(
glyph.x as usize, glyph.y as usize,
glyph.width as usize, glyph.height as usize,
));
batch.add_rect_scaled_tinted(offset_x, y + (glyph.yoffset as f32 * constants.font_scale).floor(), color,
constants.font_scale, constants.font_scale,
&Rect::<usize>::new_size(
glyph.x as usize, glyph.y as usize,
glyph.width as usize, glyph.height as usize,
));
offset_x += ((glyph.width as f32 + glyph.xoffset as f32) * constants.font_scale).floor() + if chr != ' ' { 1.0 } else { constants.font_space_offset };
}
@ -103,12 +109,12 @@ impl BMFontRenderer {
for (chr, glyph) in chars.iter() {
if glyph.page == page {
batch.add_rect_scaled(offset_x, y + (glyph.yoffset as f32 * constants.font_scale).floor(),
constants.font_scale, constants.font_scale,
&Rect::<usize>::new_size(
glyph.x as usize, glyph.y as usize,
glyph.width as usize, glyph.height as usize,
));
batch.add_rect_scaled_tinted(offset_x, y + (glyph.yoffset as f32 * constants.font_scale).floor(), color,
constants.font_scale, constants.font_scale,
&Rect::<usize>::new_size(
glyph.x as usize, glyph.y as usize,
glyph.width as usize, glyph.height as usize,
));
}
offset_x += ((glyph.width as f32 + glyph.xoffset as f32) * constants.font_scale).floor() + if *chr != ' ' { 1.0 } else { constants.font_space_offset };

View file

@ -159,7 +159,7 @@ impl Menu {
state.font.draw_text(name.chars(), self.x as f32 + 20.0, y, &state.constants, &mut state.texture_set, ctx)?;
}
MenuEntry::Disabled(name) => {
state.font.draw_text(name.chars(), self.x as f32 + 20.0, y, &state.constants, &mut state.texture_set, ctx)?;
state.font.draw_colored_text(name.chars(), self.x as f32 + 20.0, y, (0xa0, 0xa0, 0xff), &state.constants, &mut state.texture_set, ctx)?;
}
MenuEntry::Toggle(name, value) => {
let value_text = if *value { "ON" } else { "OFF" };

View file

@ -27,7 +27,7 @@ impl TitleScene {
tick: 0,
current_menu: CurrentMenu::MainMenu,
main_menu: Menu::new(0, 0, 100, 5 * 14 + 6),
option_menu: Menu::new(0, 0, 140, 3 * 14 + 6),
option_menu: Menu::new(0, 0, 180, 5 * 14 + 6),
}
}
@ -78,6 +78,8 @@ static COPYRIGHT_PIXEL: &str = "2004.12 Studio Pixel";
static COPYRIGHT_NICALIS: &str = "@2011 NICALIS INC.";
static COPYRIGHT_NICALIS_SWITCH: &str = "@2017 NICALIS INC."; // untested?
static DISCORD_LINK: &str = "https://discord.gg/fbRsNNB";
impl Scene for TitleScene {
fn tick(&mut self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
if self.tick == 0 {
@ -90,6 +92,8 @@ impl Scene for TitleScene {
self.option_menu.push_entry(MenuEntry::Toggle("Test toggle".to_string(), false));
self.option_menu.push_entry(MenuEntry::Toggle("2x Speed hack".to_string(), false));
self.option_menu.push_entry(MenuEntry::Active("Join our Discord".to_string()));
self.option_menu.push_entry(MenuEntry::Disabled(DISCORD_LINK.to_owned()));
self.option_menu.push_entry(MenuEntry::Active("Back".to_string()));
}
@ -135,7 +139,12 @@ impl Scene for TitleScene {
state.set_speed_hack(*value);
}
}
MenuSelectionResult::Selected(2, _) | MenuSelectionResult::Canceled => {
MenuSelectionResult::Selected(2, _) => {
if let Err(e) = webbrowser::open(DISCORD_LINK) {
log::warn!("Error opening web browser: {}", e);
}
}
MenuSelectionResult::Selected(4, _) | MenuSelectionResult::Canceled => {
self.current_menu = CurrentMenu::MainMenu;
}
_ => {}

View file

@ -10,7 +10,7 @@ use crate::common::FILE_TYPES;
use crate::engine_constants::EngineConstants;
use crate::ggez::{Context, GameError, GameResult, graphics};
use crate::ggez::filesystem;
use crate::ggez::graphics::{Drawable, DrawMode, DrawParam, FilterMode, Image, Mesh, Rect};
use crate::ggez::graphics::{Drawable, DrawMode, DrawParam, FilterMode, Image, Mesh, Rect, Color};
use crate::ggez::graphics::spritebatch::SpriteBatch;
use crate::ggez::nalgebra::{Point2, Vector2};
use crate::str;
@ -78,6 +78,23 @@ impl SizedBatch {
self.batch.add(param);
}
pub fn add_rect_scaled_tinted(&mut self, x: f32, y: f32, color: (u8,u8,u8), scale_x: f32, scale_y: f32, rect: &common::Rect<usize>) {
if (rect.right - rect.left) == 0 || (rect.bottom - rect.top) == 0 {
return;
}
let param = DrawParam::new()
.color(color.into())
.src(Rect::new(rect.left as f32 / self.width as f32,
rect.top as f32 / self.height as f32,
(rect.right - rect.left) as f32 / self.width as f32,
(rect.bottom - rect.top) as f32 / self.height as f32))
.dest(mint::Point2 { x, y })
.scale(Vector2::new(scale_x, scale_y));
self.batch.add(param);
}
pub fn draw(&mut self, ctx: &mut Context) -> GameResult {
self.batch.set_filter(FilterMode::Nearest);
self.batch.draw(ctx, DrawParam::new())?;