mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-29 17:34:58 +03:00
unify the two JustDraw things
This commit is contained in:
parent
13b1e86fb7
commit
b47a186373
@ -540,3 +540,50 @@ impl MultiText {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// I'm tempted to fold this into GeomBatch and Drawable, but since this represents a screen-space
|
||||
// thing, it'd be weird to do that.
|
||||
pub struct DrawBoth {
|
||||
geom: Drawable,
|
||||
txt: Vec<(Text, ScreenPt)>,
|
||||
// Covers both geometry and text
|
||||
dims: ScreenDims,
|
||||
}
|
||||
|
||||
impl DrawBoth {
|
||||
pub fn new(ctx: &EventCtx, batch: GeomBatch, txt: Vec<(Text, ScreenPt)>) -> DrawBoth {
|
||||
let mut total_dims = batch.get_dims();
|
||||
for (t, pt) in &txt {
|
||||
let dims = ctx.canvas.text_dims(t);
|
||||
let w = dims.width + pt.x;
|
||||
let h = dims.height + pt.y;
|
||||
if w > total_dims.width {
|
||||
total_dims.width = w;
|
||||
}
|
||||
if h > total_dims.height {
|
||||
total_dims.height = h;
|
||||
}
|
||||
}
|
||||
DrawBoth {
|
||||
geom: batch.upload(ctx),
|
||||
txt,
|
||||
dims: total_dims,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&self, top_left: ScreenPt, g: &mut GfxCtx) {
|
||||
g.fork(Pt2D::new(0.0, 0.0), top_left, 1.0);
|
||||
g.redraw(&self.geom);
|
||||
g.unfork();
|
||||
for (txt, pt) in &self.txt {
|
||||
g.draw_text_at_screenspace_topleft(
|
||||
txt,
|
||||
ScreenPt::new(top_left.x + pt.x, top_left.y + pt.y),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_dims(&self) -> ScreenDims {
|
||||
self.dims
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ mod widgets;
|
||||
|
||||
pub use crate::canvas::{Canvas, HorizontalAlignment, VerticalAlignment};
|
||||
pub use crate::color::Color;
|
||||
pub use crate::drawing::{Drawable, GeomBatch, GfxCtx, MultiText, Prerender};
|
||||
pub use crate::drawing::{DrawBoth, Drawable, GeomBatch, GfxCtx, MultiText, Prerender};
|
||||
pub use crate::event::{hotkey, lctrl, Event, Key, MultiKey};
|
||||
pub use crate::event_ctx::{EventCtx, TextureType};
|
||||
pub use crate::input::UserInput;
|
||||
@ -20,9 +20,8 @@ pub use crate::runner::{run, EventLoopMode, Settings, GUI};
|
||||
pub use crate::screen_geom::{ScreenDims, ScreenPt, ScreenRectangle};
|
||||
pub use crate::text::{Line, Text, TextSpan, HOTKEY_COLOR};
|
||||
pub use crate::widgets::{
|
||||
Autocomplete, Button, Choice, ItemSlider, JustDraw, JustDrawText, MenuUnderButton, ModalMenu,
|
||||
NewScroller, Scroller, Slider, SliderWithTextBox, Warper, WarpingItemSlider, Wizard,
|
||||
WrappedWizard,
|
||||
Autocomplete, Button, Choice, ItemSlider, JustDraw, MenuUnderButton, ModalMenu, NewScroller,
|
||||
Scroller, Slider, SliderWithTextBox, Warper, WarpingItemSlider, Wizard, WrappedWizard,
|
||||
};
|
||||
|
||||
pub enum InputResult<T: Clone> {
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::layout::Widget;
|
||||
use crate::{
|
||||
hotkey, text, Color, Drawable, EventCtx, GeomBatch, GfxCtx, Key, Line, MultiKey, ScreenDims,
|
||||
hotkey, text, Color, DrawBoth, EventCtx, GeomBatch, GfxCtx, Key, Line, MultiKey, ScreenDims,
|
||||
ScreenPt, Text,
|
||||
};
|
||||
use geom::{Circle, Distance, Polygon, Pt2D};
|
||||
@ -128,50 +128,6 @@ impl Widget for Button {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Probably going to fold this into Drawable or do something else...
|
||||
struct DrawBoth {
|
||||
geom: Drawable,
|
||||
txt: Vec<(Text, ScreenPt)>,
|
||||
// Covers both geometry and text
|
||||
dims: ScreenDims,
|
||||
}
|
||||
|
||||
impl DrawBoth {
|
||||
fn new(ctx: &EventCtx, batch: GeomBatch, txt: Vec<(Text, ScreenPt)>) -> DrawBoth {
|
||||
let mut total_dims = batch.get_dims();
|
||||
for (t, pt) in &txt {
|
||||
let dims = ctx.canvas.text_dims(t);
|
||||
let w = dims.width + pt.x;
|
||||
let h = dims.height + pt.y;
|
||||
if w > total_dims.width {
|
||||
total_dims.width = w;
|
||||
}
|
||||
if h > total_dims.height {
|
||||
total_dims.height = h;
|
||||
}
|
||||
}
|
||||
DrawBoth {
|
||||
geom: batch.upload(ctx),
|
||||
txt,
|
||||
dims: total_dims,
|
||||
}
|
||||
}
|
||||
|
||||
fn draw(&self, top_left: ScreenPt, g: &mut GfxCtx) {
|
||||
g.redraw(&self.geom);
|
||||
for (txt, pt) in &self.txt {
|
||||
g.draw_text_at_screenspace_topleft(
|
||||
txt,
|
||||
ScreenPt::new(top_left.x + pt.x, top_left.y + pt.y),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_dims(&self) -> ScreenDims {
|
||||
self.dims
|
||||
}
|
||||
}
|
||||
|
||||
// Stuff to construct different types of buttons
|
||||
|
||||
const CIRCULAR_ICON_BACKGROUND: Color = Color::grey(0.5);
|
||||
|
@ -18,7 +18,7 @@ pub use self::button::Button;
|
||||
pub(crate) use self::context_menu::ContextMenu;
|
||||
pub use self::menu_under_button::MenuUnderButton;
|
||||
pub use self::modal_menu::ModalMenu;
|
||||
pub use self::no_op::{JustDraw, JustDrawText};
|
||||
pub use self::no_op::JustDraw;
|
||||
pub(crate) use self::popup_menu::PopupMenu;
|
||||
pub(crate) use self::screenshot::{screenshot_current, screenshot_everything};
|
||||
pub use self::scroller::{NewScroller, Scroller};
|
||||
|
@ -1,12 +1,11 @@
|
||||
use crate::layout::Widget;
|
||||
use crate::{Drawable, EventCtx, GeomBatch, GfxCtx, ScreenDims, ScreenPt, Text};
|
||||
use crate::{DrawBoth, EventCtx, GeomBatch, GfxCtx, ScreenDims, ScreenPt, Text};
|
||||
use geom::{Distance, Polygon, Pt2D};
|
||||
|
||||
// Just draw something. A widget just so layouting works.
|
||||
pub struct JustDraw {
|
||||
draw: Drawable,
|
||||
draw: DrawBoth,
|
||||
|
||||
dims: ScreenDims,
|
||||
top_left: ScreenPt,
|
||||
}
|
||||
|
||||
@ -14,63 +13,35 @@ impl JustDraw {
|
||||
pub fn image(filename: &str, ctx: &EventCtx) -> JustDraw {
|
||||
let color = ctx.canvas.texture(filename);
|
||||
let dims = color.texture_dims();
|
||||
let draw = GeomBatch::from(vec![(
|
||||
let batch = GeomBatch::from(vec![(
|
||||
color,
|
||||
Polygon::rectangle_topleft(
|
||||
Pt2D::new(0.0, 0.0),
|
||||
Distance::meters(dims.width),
|
||||
Distance::meters(dims.height),
|
||||
),
|
||||
)])
|
||||
.upload(ctx);
|
||||
)]);
|
||||
JustDraw {
|
||||
draw,
|
||||
dims,
|
||||
draw: DrawBoth::new(ctx, batch, vec![]),
|
||||
top_left: ScreenPt::new(0.0, 0.0),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO I wish this wasn't a separate type...
|
||||
pub fn text(text: Text, ctx: &EventCtx) -> JustDrawText {
|
||||
JustDrawText {
|
||||
dims: ctx.canvas.text_dims(&text),
|
||||
text,
|
||||
pub fn text(text: Text, ctx: &EventCtx) -> JustDraw {
|
||||
JustDraw {
|
||||
draw: DrawBoth::new(ctx, GeomBatch::new(), vec![(text, ScreenPt::new(0.0, 0.0))]),
|
||||
top_left: ScreenPt::new(0.0, 0.0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&self, g: &mut GfxCtx) {
|
||||
g.fork(Pt2D::new(0.0, 0.0), self.top_left, 1.0);
|
||||
g.redraw(&self.draw);
|
||||
self.draw.draw(self.top_left, g);
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for JustDraw {
|
||||
fn get_dims(&self) -> ScreenDims {
|
||||
self.dims
|
||||
}
|
||||
|
||||
fn set_pos(&mut self, top_left: ScreenPt) {
|
||||
self.top_left = top_left;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct JustDrawText {
|
||||
text: Text,
|
||||
|
||||
dims: ScreenDims,
|
||||
top_left: ScreenPt,
|
||||
}
|
||||
|
||||
impl JustDrawText {
|
||||
pub fn draw(&self, g: &mut GfxCtx) {
|
||||
g.draw_text_at_screenspace_topleft(&self.text, self.top_left);
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for JustDrawText {
|
||||
fn get_dims(&self) -> ScreenDims {
|
||||
self.dims
|
||||
self.draw.get_dims()
|
||||
}
|
||||
|
||||
fn set_pos(&mut self, top_left: ScreenPt) {
|
||||
|
@ -1,8 +1,6 @@
|
||||
use crate::game::{State, Transition};
|
||||
use crate::ui::UI;
|
||||
use ezgui::{
|
||||
layout, Button, Color, EventCtx, GfxCtx, JustDraw, JustDrawText, Line, MultiKey, Text,
|
||||
};
|
||||
use ezgui::{layout, Button, Color, EventCtx, GfxCtx, JustDraw, Line, MultiKey, Text};
|
||||
|
||||
type Callback = Box<dyn Fn(&mut EventCtx, &mut UI) -> Option<Transition>>;
|
||||
|
||||
@ -13,7 +11,7 @@ pub struct ManagedGUIStateBuilder<'a> {
|
||||
|
||||
impl<'a> ManagedGUIStateBuilder<'a> {
|
||||
pub fn draw_text(&mut self, txt: Text) {
|
||||
self.state.draw_text.push(JustDraw::text(txt, &self.ctx));
|
||||
self.state.just_draw.push(JustDraw::text(txt, &self.ctx));
|
||||
}
|
||||
|
||||
pub fn img_button(&mut self, filename: &str, hotkey: Option<MultiKey>, onclick: Callback) {
|
||||
@ -47,7 +45,7 @@ impl<'a> ManagedGUIStateBuilder<'a> {
|
||||
}
|
||||
|
||||
pub struct ManagedGUIState {
|
||||
draw_text: Vec<JustDrawText>,
|
||||
just_draw: Vec<JustDraw>,
|
||||
buttons: Vec<(Button, Callback)>,
|
||||
}
|
||||
|
||||
@ -56,7 +54,7 @@ impl ManagedGUIState {
|
||||
ManagedGUIStateBuilder {
|
||||
ctx,
|
||||
state: ManagedGUIState {
|
||||
draw_text: Vec::new(),
|
||||
just_draw: Vec::new(),
|
||||
buttons: Vec::new(),
|
||||
},
|
||||
}
|
||||
@ -68,7 +66,7 @@ impl State for ManagedGUIState {
|
||||
// TODO If this ever gets slow, only run if window size has changed.
|
||||
layout::flexbox(
|
||||
ctx,
|
||||
self.draw_text
|
||||
self.just_draw
|
||||
.iter_mut()
|
||||
.map(|t| t as &mut dyn layout::Widget)
|
||||
.chain(
|
||||
@ -96,7 +94,7 @@ impl State for ManagedGUIState {
|
||||
fn draw(&self, g: &mut GfxCtx, ui: &UI) {
|
||||
// Happens to be a nice background color too ;)
|
||||
g.clear(ui.cs.get("grass"));
|
||||
for t in &self.draw_text {
|
||||
for t in &self.just_draw {
|
||||
t.draw(g);
|
||||
}
|
||||
for (btn, _) in &self.buttons {
|
||||
|
Loading…
Reference in New Issue
Block a user