use super::{Editor, Selection};
use crate::model::{ItemId, Light, Step};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LightTool {
pub item: ItemId,
pub light: Light,
}
impl Editor {
pub(super) fn start_light(&mut self, item: ItemId) -> bool {
let Some(current) = self.project.state().item(item) else {
return false;
};
let light = current.light.unwrap_or_else(|| self.neutral_light(item));
self.leave_tools();
self.light = Some(LightTool { item, light });
self.selection = Selection::Item(item);
true
}
pub fn light_tool(&self) -> Option<LightTool> {
self.light
}
pub fn set_light(&mut self, light: Light) {
let Some(tool) = &mut self.light else { return };
match light.check() {
Ok(()) => tool.light = light,
Err(e) => self.message = Some(e.to_string()),
}
}
fn neutral_light(&self, item: ItemId) -> Light {
let radius = self.project.state().item(item).map_or(1.0, |i| {
Light::default_radius(i.source_width, i.source_height)
});
Light::neutral(radius)
}
pub(super) fn reset_light(&mut self) -> bool {
let Some(tool) = self.light else { return false };
let neutral = self.neutral_light(tool.item);
self.light = Some(LightTool {
light: neutral,
..tool
});
true
}
pub(super) fn apply_light(&mut self) -> bool {
let Some(tool) = self.light.take() else {
return false;
};
let current = self.project.state().item(tool.item).and_then(|i| i.light);
let unchanged = match current {
Some(current) => current == tool.light,
None => tool.light.is_neutral(),
};
if unchanged {
return true;
}
self.record(Step::SetLight {
item: tool.item,
light: tool.light,
})
}
}