Skip to content

Commit

Permalink
zgui: Remove Drawable::text's font_size arg
Browse files Browse the repository at this point in the history
And put a few TODOs
  • Loading branch information
ozkriff committed Jun 6, 2021
1 parent d603e6a commit 584da23
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/screen/agent_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn info_panel(
let mut layout = Box::new(ui::VLayout::new().stretchable(true));
layout.add(agent_image(typename)?);
let mut add = |w| layout.add(w);
let text_ = |s: &str| ui::Drawable::text(s, font, utils::font_size());
let text_ = |s: &str| ui::Drawable::text(s, font);
let label_ = |text: &str| -> ZResult<_> { Ok(ui::Label::new(text_(text), h)?) };
let label = |text: &str| -> ZResult<Box<_>> { Ok(Box::new(label_(text)?)) };
let label_s = |text: &str| -> ZResult<_> { Ok(Box::new(label_(text)?.stretchable(true))) };
Expand Down Expand Up @@ -181,7 +181,7 @@ fn info_panel(
fn button_back(gui: &mut ui::Gui<Message>, layout_width: f32) -> ZResult<Box<dyn ui::Widget>> {
let font = assets::get().font;
let h = utils::line_heights().normal;
let text = ui::Drawable::text("back", font, utils::font_size());
let text = ui::Drawable::text("back", font);
let msg = Message::Back;
let mut button = ui::Button::new(text, h, gui.sender(), msg)?.stretchable(true);
button.stretch(layout_width / 3.0);
Expand Down Expand Up @@ -226,7 +226,7 @@ impl AgentInfo {
let col = {
let mut col = Box::new(ui::VLayout::new());
col.add(Box::new(ui::Spacer::new_vertical(panel_from_height * 0.5)));
let text = ui::Drawable::text("=>", font, utils::font_size());
let text = ui::Drawable::text("=>", font);
col.add(Box::new(ui::Label::new(text, h)?));
col
};
Expand Down
13 changes: 5 additions & 8 deletions src/screen/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ use crate::{
mod view;
mod visualize;

const FONT_SIZE: u16 = utils::font_size();

#[derive(Clone, Debug)]
enum Message {
Exit,
Expand All @@ -65,11 +63,10 @@ fn line_with_info_button(
) -> ZResult<Box<dyn ui::Widget>> {
let h = line_heights().normal;
let font = assets::get().font;
let text = ui::Drawable::text(text, font, FONT_SIZE);
let icon = textures().icons.info;
let button = ui::Button::new(ui::Drawable::Texture(icon), h, gui.sender(), message)?;
let mut line = Box::new(ui::HLayout::new().stretchable(true));
line.add(Box::new(ui::Label::new(text, h)?));
line.add(Box::new(ui::Label::new(ui::Drawable::text(text, font), h)?));
line.add(Box::new(ui::Spacer::new_horizontal(0.0).stretchable(true)));
line.add(Box::new(button));
Ok(line)
Expand All @@ -86,7 +83,7 @@ fn build_panel_agent_info(gui: &mut Gui<Message>, state: &State, id: Id) -> ZRes
let h = line_heights().normal;
let space_between_buttons = h / 8.0;
let mut add = |w| layout.add(w);
let text_ = |s: &str| ui::Drawable::text(s, font, FONT_SIZE);
let text_ = |s: &str| ui::Drawable::text(s, font);
let label_ = |text: &str| -> ZResult<_> { Ok(ui::Label::new(text_(text), h)?) };
let label = |text: &str| -> ZResult<Box<dyn Widget>> { Ok(Box::new(label_(text)?)) };
let label_s = |text: &str| -> ZResult<_> { Ok(Box::new(label_(text)?.stretchable(true))) };
Expand Down Expand Up @@ -188,7 +185,7 @@ fn build_panel_agent_info(gui: &mut Gui<Message>, state: &State, id: Id) -> ZRes
effect::Duration::Rounds(n) => format!("{} ({}t)", s, n),
};
let message = Message::LastingEffectInfo(effect.effect);
let text = ui::Drawable::text(text, font, FONT_SIZE);
let text = ui::Drawable::text(text, font);
let tex_info = ui::Drawable::Texture(textures().icons.info);
let button_info = ui::Button::new(tex_info, h, gui.sender(), message)?;
let icon_effect = visualize::get_effect_icon(&effect.effect);
Expand Down Expand Up @@ -250,7 +247,7 @@ fn build_panel_agent_abilities(
if let ability::Status::Cooldown(n) = ability.status {
let mut layers = ui::LayersLayout::new();
layers.add(Box::new(button));
let text = ui::Drawable::text(format!(" ({})", n).as_str(), font, FONT_SIZE);
let text = ui::Drawable::text(format!(" ({})", n).as_str(), font);
let label = ui::Label::new(text, h / 2.0)?;
layers.add(Box::new(label));
layout.add(Box::new(layers));
Expand Down Expand Up @@ -283,7 +280,7 @@ fn build_panel_ability_description(
id: Id,
) -> ZResult<ui::RcWidget> {
let font = assets::get().font;
let text = |s: &str| ui::Drawable::text(s, font, FONT_SIZE);
let text = |s: &str| ui::Drawable::text(s, font);
let h = line_heights().normal;
let mut layout = Box::new(ui::VLayout::new().stretchable(true));
let text_title = text(&format!("~~~ {} ~~~", ability.title()));
Expand Down
10 changes: 4 additions & 6 deletions src/screen/campaign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ enum Message {
Action(Action),
}

const FONT_SIZE: u16 = utils::font_size();

// The main line height of this screen.
fn line_height() -> f32 {
utils::line_heights().normal
Expand Down Expand Up @@ -133,7 +131,7 @@ fn build_panel_actions(gui: &mut ui::Gui<Message>, state: &State) -> ZResult<Box
}
};
{
let text = ui::Drawable::text(text, font, FONT_SIZE);
let text = ui::Drawable::text(text, font);
let sender = gui.sender();
let message = Message::Action(action.clone());
let mut button = ui::Button::new(text, h, sender, message)?.stretchable(true);
Expand Down Expand Up @@ -162,7 +160,7 @@ fn build_panel_actions(gui: &mut ui::Gui<Message>, state: &State) -> ZResult<Box
state.current_scenario_index() + 1,
state.scenarios_count()
);
let text = ui::Drawable::text(text, font, FONT_SIZE);
let text = ui::Drawable::text(text, font);
let command = Message::StartBattle;
let button = ui::Button::new(text, h, gui.sender(), command)?.stretchable(true);
layout.add(Box::new(button));
Expand All @@ -173,7 +171,7 @@ fn build_panel_actions(gui: &mut ui::Gui<Message>, state: &State) -> ZResult<Box
}

fn label(font: Font, text: &str) -> ZResult<Box<dyn ui::Widget>> {
let text = ui::Drawable::text(text, font, FONT_SIZE);
let text = ui::Drawable::text(text, font);
Ok(Box::new(ui::Label::new(text, line_height())?))
}

Expand Down Expand Up @@ -256,7 +254,7 @@ impl Campaign {
fn add_label_central_message(&mut self, text: &str) -> ZResult {
let font = assets::get().font;
let h = utils::line_heights().large;
let text = ui::Drawable::text(text, font, FONT_SIZE);
let text = ui::Drawable::text(text, font);
let label = ui::pack(ui::Label::new_with_bg(text, h)?);
let anchor = ui::Anchor(ui::HAnchor::Middle, ui::VAnchor::Middle);
self.gui.add(&label, anchor);
Expand Down
6 changes: 2 additions & 4 deletions src/screen/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ impl Confirm {
pub fn from_lines(lines: &[impl AsRef<str>], sender: Sender<Message>) -> ZResult<Self> {
let font = assets::get().font;
let h = utils::line_heights().big;
let font_size = utils::font_size();
let mut layout = ui::VLayout::new();
for line in lines {
let text = ui::Drawable::text(line.as_ref(), font, font_size);
let text = ui::Drawable::text(line.as_ref(), font);
let label = Box::new(ui::Label::new(text, h)?);
layout.add(label);
}
Expand All @@ -51,11 +50,10 @@ impl Confirm {
let font = assets::get().font;
let mut gui = ui::Gui::new();
let h = utils::line_heights().big;
let font_size = utils::font_size();
let mut layout = Box::new(ui::VLayout::new());
let spacer = || Box::new(ui::Spacer::new_vertical(h * 0.5));
let button = |line, message| -> ZResult<_> {
let text = ui::Drawable::text(line, font, font_size);
let text = ui::Drawable::text(line, font);
let b = ui::Button::new(text, h, gui.sender(), message)?.stretchable(true);
Ok(b)
};
Expand Down
3 changes: 1 addition & 2 deletions src/screen/general_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ impl GeneralInfo {
let font = assets::get().font;
let mut gui = ui::Gui::new();
let h = utils::line_heights().normal;
let font_size = utils::font_size();
let mut layout = Box::new(ui::VLayout::new().stretchable(true));
let text_ = |s: &str| ui::Drawable::text(s, font, font_size);
let text_ = |s: &str| ui::Drawable::text(s, font);
let label_ = |text: &str| -> ZResult<_> { Ok(ui::Label::new(text_(text), h)?) };
let label = |text: &str| -> ZResult<_> { Ok(Box::new(label_(text)?)) };
let label_s = |text: &str| -> ZResult<_> { Ok(Box::new(label_(text)?.stretchable(true))) };
Expand Down
3 changes: 1 addition & 2 deletions src/screen/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ fn make_gui() -> ZResult<ui::Gui<Message>> {
let font = assets::get().font;
let mut gui = ui::Gui::new();
let h = utils::line_heights().large;
let font_size = utils::font_size();
let space = || Box::new(ui::Spacer::new_vertical(h / 8.0));
let button = &mut |text, message| -> ZResult<_> {
let text = ui::Drawable::text(text, font, font_size);
let text = ui::Drawable::text(text, font);
let b = ui::Button::new(text, h, gui.sender(), message)?.stretchable(true);
Ok(Box::new(b))
};
Expand Down
5 changes: 0 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ pub fn time_s(s: f32) -> Duration {
Duration::from_millis(ms as u64)
}

// TODO: Move to some config (https://github.com/ozkriff/zemeroth/issues/424)
pub const fn font_size() -> u16 {
20
}

pub struct LineHeights {
pub small: f32,
pub normal: f32,
Expand Down
3 changes: 1 addition & 2 deletions zgui/examples/absolute_coordinates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ enum Message {
}

fn make_gui(font: mq::text::Font) -> ui::Result<ui::Gui<Message>> {
let font_size = 64;
let mut gui = ui::Gui::new();
let anchor = ui::Anchor(ui::HAnchor::Right, ui::VAnchor::Bottom);
let text = ui::Drawable::text("Button", font, font_size);
let text = ui::Drawable::text("Button", font);
let button = ui::Button::new(text, 0.2, gui.sender(), Message::Command)?;
gui.add(&ui::pack(button), anchor);
Ok(gui)
Expand Down
3 changes: 1 addition & 2 deletions zgui/examples/layers_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ enum Message {
}

fn make_gui(assets: common::Assets) -> ui::Result<ui::Gui<Message>> {
let font_size = 64;
let mut gui = ui::Gui::new();
let text = ui::Drawable::text(" text", assets.font, font_size);
let text = ui::Drawable::text(" text", assets.font);
let texture = ui::Drawable::Texture(assets.texture);
let button = ui::Button::new(texture, 0.2, gui.sender(), Message::Command)?;
let label = ui::Label::new(text, 0.1)?;
Expand Down
3 changes: 1 addition & 2 deletions zgui/examples/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ enum Message {
}

fn make_gui(assets: common::Assets) -> ui::Result<ui::Gui<Message>> {
let font_size = 64;
let text = |s| ui::Drawable::text(s, assets.font, font_size);
let text = |s| ui::Drawable::text(s, assets.font);
let texture = || ui::Drawable::Texture(assets.texture);
let mut gui = ui::Gui::new();
{
Expand Down
3 changes: 1 addition & 2 deletions zgui/examples/pixel_coordinates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ pub fn make_and_set_camera(_aspect_ratio: f32) -> Camera2D {
}

fn make_gui(font: mq::text::Font) -> ui::Result<ui::Gui<Message>> {
let font_size = 64;
let mut gui = ui::Gui::new();
let anchor = ui::Anchor(ui::HAnchor::Right, ui::VAnchor::Bottom);
let text = ui::Drawable::text("Button", font, font_size);
let text = ui::Drawable::text("Button", font);
let button = ui::Button::new(text, 0.2, gui.sender(), Message::Command)?;
gui.add(&ui::pack(button), anchor);
Ok(gui)
Expand Down
3 changes: 1 addition & 2 deletions zgui/examples/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ enum Message {
}

fn make_gui(font: mq::text::Font) -> ui::Result<ui::Gui<Message>> {
let font_size = 64;
let mut gui = ui::Gui::new();
let anchor = ui::Anchor(ui::HAnchor::Right, ui::VAnchor::Bottom);
let text = ui::Drawable::text("Button", font, font_size);
let text = ui::Drawable::text("Button", font);
let button = ui::Button::new(text, 0.2, gui.sender(), Message::AddOrRemove)?;
gui.add(&ui::pack(button), anchor);
Ok(gui)
Expand Down
3 changes: 1 addition & 2 deletions zgui/examples/text_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ enum Message {
}

fn make_gui(font: mq::text::Font) -> ui::Result<ui::Gui<Message>> {
let font_size = 64;
let mut gui = ui::Gui::new();
let anchor = ui::Anchor(ui::HAnchor::Right, ui::VAnchor::Bottom);
let text = ui::Drawable::text("Button", font, font_size);
let text = ui::Drawable::text("Button", font);
let button = ui::Button::new(text, 0.2, gui.sender(), Message::Command)?;
gui.add(&ui::pack(button), anchor);
Ok(gui)
Expand Down
7 changes: 5 additions & 2 deletions zgui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ pub enum Drawable {
}

impl Drawable {
pub fn text(label: impl Into<String>, font: Font, font_size: u16) -> Drawable {
pub fn text(label: impl Into<String>, font: Font) -> Drawable {
Drawable::Text {
label: label.into(),
font,
font_size,
// TODO: this field doesn't mean much with new macroquad since
// we're using camera_font_scale vefore rendering it anyway.
// But it's somewhat tricky to remove this so I'm putting this constant here for now.
font_size: 128,
}
}

Expand Down
1 change: 1 addition & 0 deletions zscene/src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl Sprite {
}

pub fn from_text((label, font): (&str, Font), height: f32) -> Self {
// TODO: it'd be cool to move this to the drawing method (same as in zgui)
let (font_size, _, _) = mq::text::camera_font_scale(height);
Self::from_drawable(
Drawable::Text {
Expand Down

0 comments on commit 584da23

Please sign in to comment.