Skip to content

Commit

Permalink
Fix invalidated font crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Aug 16, 2024
1 parent 0f70ca4 commit de83907
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 46 deletions.
72 changes: 41 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reffect"
version = "0.3.1"
version = "0.3.2"
authors = ["Zerthox"]
edition = "2021"
description = "Customizable effect & resource displays"
Expand Down
8 changes: 8 additions & 0 deletions src/addon/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ impl Addon {
});
}

#[allow(unused)] // TODO: call when font atlas rebuilt
pub fn reload_fonts() {
let mut addon = Self::lock();
for pack in &mut addon.packs {
pack.reload_fonts();
}
}

pub fn open_addon_folder(&self) {
if let Err(err) = open::that_detached(Self::addon_dir()) {
log::error!("Failed to open addon folder: {err}");
Expand Down
6 changes: 1 addition & 5 deletions src/elements/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ impl Element {
kind,
..Self::default()
};
element.load();
Loader.visit_element(&mut element);
element
}

pub fn load(&mut self) {
Loader.visit_element(self);
}

/// Renders the element.
pub fn render(&mut self, ui: &Ui, ctx: &Context, state: &RenderState) {
self.common.render_child(ui, ctx, state, |state| {
Expand Down
8 changes: 6 additions & 2 deletions src/elements/pack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
},
schema::Schema,
traits::{RenderDebug, RenderOptions},
tree::{Loader, TreeNode, VisitMut},
tree::{FontReloader, Loader, TreeNode, VisitMut},
};
use nexus::imgui::{ComboBoxFlags, MenuItem, Ui};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -41,7 +41,11 @@ impl Pack {
}

pub fn load(&mut self) {
Loader.visit_elements(&mut self.elements);
Loader.visit_pack(self);
}

pub fn reload_fonts(&mut self) {
FontReloader.visit_pack(self);
}

pub fn load_from_file(path: impl Into<PathBuf>) -> Option<Self> {
Expand Down
6 changes: 5 additions & 1 deletion src/elements/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ impl Text {
}

pub fn load(&mut self) {
self.load_font();
}

pub fn load_font(&mut self) {
self.loaded_font = self.font_name.as_ref().and_then(Font::from_name_or_warn);
}
}
Expand Down Expand Up @@ -177,7 +181,7 @@ impl RenderOptions for Text {
}
if self.font_name.is_some() && self.loaded_font.is_none() {
match self.loaded_font {
Some(font) if !font.is_loaded() => helper_warn(ui, || ui.text("Font not loaded")),
Some(font) if !font.is_valid() => helper_warn(ui, || ui.text("Font invalidated")),
Some(_) => {}
None => helper_warn(ui, || ui.text("Failed to find font")),
}
Expand Down
6 changes: 5 additions & 1 deletion src/render_util/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl Font {
unsafe { sys::ImFont_IsLoaded(self.as_ptr()) }
}

pub fn is_valid(&self) -> bool {
unsafe { Self::get_all() }.any(|font| font == *self)
}

pub unsafe fn name_raw<'a>(&self) -> &'a CStr {
CStr::from_ptr(sys::ImFont_GetDebugName(self.as_ptr()))
}
Expand All @@ -55,7 +59,7 @@ impl Font {
}

pub fn push(&self) -> Option<FontToken> {
self.is_loaded().then(|| {
self.is_valid().then(|| {
unsafe { sys::igPushFont(self.as_ptr()) };
FontToken
})
Expand Down
11 changes: 11 additions & 0 deletions src/tree/font_reload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::VisitMut;
use crate::elements::Text;

#[derive(Debug, Default, Clone, Copy)]
pub struct FontReloader;

impl VisitMut for FontReloader {
fn visit_text(&mut self, text: &mut Text) {
text.load_font();
}
}
5 changes: 3 additions & 2 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
mod font_reload;
mod load;
mod visit;

use crate::elements::Element;
pub use self::{font_reload::*, load::*, visit::*};

pub use self::{load::*, visit::*};
use crate::elements::Element;

/// [`Element`] tree node.
pub trait TreeNode {
Expand Down
6 changes: 5 additions & 1 deletion src/tree/visit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::{
elements::{Bar, Element, ElementType, Group, IconElement, IconList, Text},
elements::{Bar, Element, ElementType, Group, IconElement, IconList, Pack, Text},
trigger::FilterTrigger,
};

pub trait VisitMut {
fn visit_pack(&mut self, pack: &mut Pack) {
self.visit_elements(&mut pack.elements);
}

fn visit_elements(&mut self, elements: &mut [Element]) {
for element in elements {
self.visit_element(element);
Expand Down
8 changes: 6 additions & 2 deletions src/trigger/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@ impl FilterTrigger {
pub fn load(&mut self) {
self.player.load();
}

fn is_active_no_update(&mut self, ctx: &Context) -> bool {
self.player.is_active(ctx) && self.map.is_active(ctx)
}
}

impl Trigger for FilterTrigger {
fn is_active(&mut self, ctx: &Context) -> bool {
if ctx.has_update(ContextUpdate::Map) {
self.map.update(ctx);
}
self.player.is_active(ctx) && self.map.is_active(ctx)
self.is_active_no_update(ctx)
}

fn is_active_or_edit(&mut self, ctx: &Context, state: &RenderState) -> bool {
if state.is_edit(ctx) {
self.map.update(ctx);
true
} else {
!ctx.edit.is_editing() && self.is_active(ctx)
!ctx.edit.is_editing() && self.is_active_no_update(ctx)
}
}
}
Expand Down

0 comments on commit de83907

Please sign in to comment.