mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Rearrange iced renderer modules, wire up events for the main menu iced ui
This commit is contained in:
parent
095345d8c6
commit
04fe308b9c
@ -158,7 +158,6 @@ rotation_image_ids! {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)] // TODO: why does iced require Clone?
|
||||
pub enum Event {
|
||||
LoginAttempt {
|
||||
username: String,
|
||||
@ -190,7 +189,12 @@ struct IcedState {
|
||||
imgs: IcedImgs,
|
||||
quit_button: iced::button::State,
|
||||
}
|
||||
pub type Message = Event;
|
||||
|
||||
#[derive(Clone)] // TODO: why does iced require Clone?
|
||||
enum Message {
|
||||
Quit,
|
||||
}
|
||||
|
||||
impl IcedState {
|
||||
pub fn new(imgs: IcedImgs) -> Self {
|
||||
Self {
|
||||
@ -351,9 +355,9 @@ impl IcedState {
|
||||
BackgroundContainer::new(Image::new(self.imgs.bg), content).into()
|
||||
}
|
||||
|
||||
pub fn update(message: Message) {
|
||||
pub fn update(&mut self, message: Message, events: &mut Vec<Event>) {
|
||||
match message {
|
||||
_ => unimplemented!(),
|
||||
Message::Quit => events.push(Event::Quit),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1121,12 +1125,16 @@ impl<'a> MainMenuUi {
|
||||
pub fn handle_iced_event(&mut self, event: ui::ice::Event) { self.ice_ui.handle_event(event); }
|
||||
|
||||
pub fn maintain(&mut self, global_state: &mut GlobalState, dt: Duration) -> Vec<Event> {
|
||||
let events = self.update_layout(global_state, dt);
|
||||
let mut events = self.update_layout(global_state, dt);
|
||||
self.ui.maintain(global_state.window.renderer_mut(), None);
|
||||
self.ice_ui.maintain(
|
||||
let (messages, _) = self.ice_ui.maintain(
|
||||
self.ice_state.view(&self.i18n),
|
||||
global_state.window.renderer_mut(),
|
||||
);
|
||||
messages
|
||||
.into_iter()
|
||||
.for_each(|message| self.ice_state.update(message, &mut events));
|
||||
|
||||
events
|
||||
}
|
||||
|
||||
|
12
voxygen/src/ui/ice/renderer/defaults.rs
Normal file
12
voxygen/src/ui/ice/renderer/defaults.rs
Normal file
@ -0,0 +1,12 @@
|
||||
// TODO: expose to user
|
||||
pub struct Defaults {
|
||||
pub text_color: iced::Color,
|
||||
}
|
||||
|
||||
impl Default for Defaults {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
text_color: iced::Color::WHITE,
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +1,9 @@
|
||||
// TODO: reorganize modules (e.g. put all these in a widget submodule)
|
||||
mod aspect_ratio_container;
|
||||
mod background_container;
|
||||
mod button;
|
||||
mod column;
|
||||
mod compound_graphic;
|
||||
mod container;
|
||||
mod image;
|
||||
mod row;
|
||||
mod space;
|
||||
mod text;
|
||||
mod defaults;
|
||||
mod style;
|
||||
mod widgets;
|
||||
|
||||
pub use button::Style as ButtonStyle;
|
||||
pub use defaults::Defaults;
|
||||
pub use style::ButtonStyle;
|
||||
|
||||
use super::{
|
||||
super::graphic::{self, Graphic, TexId},
|
||||
@ -24,7 +17,6 @@ use crate::{
|
||||
Error,
|
||||
};
|
||||
use common::util::srgba_to_linear;
|
||||
//use log::warn;
|
||||
use std::ops::Range;
|
||||
use vek::*;
|
||||
|
||||
@ -596,19 +588,6 @@ fn default_scissor(renderer: &Renderer) -> Aabr<u16> {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: expose to user
|
||||
pub struct Defaults {
|
||||
pub text_color: iced::Color,
|
||||
}
|
||||
|
||||
impl Default for Defaults {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
text_color: iced::Color::WHITE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl iced::Renderer for IcedRenderer {
|
||||
// Default styling
|
||||
type Defaults = Defaults;
|
||||
|
@ -1,6 +1,5 @@
|
||||
use super::{super::Rotation, widget::image, Defaults, IcedRenderer, Primitive};
|
||||
use iced::{button, mouse, Color, Element, Layout, Point, Rectangle};
|
||||
use vek::Rgba;
|
||||
use super::super::widget::image;
|
||||
use iced::Color;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Background {
|
||||
@ -69,22 +68,22 @@ impl Style {
|
||||
self
|
||||
}
|
||||
|
||||
fn disabled(&self) -> (Option<image::Handle>, Color) {
|
||||
pub fn disabled(&self) -> (Option<image::Handle>, Color) {
|
||||
(
|
||||
self.background.as_ref().map(|b| b.default),
|
||||
self.disabled_text,
|
||||
)
|
||||
}
|
||||
|
||||
fn pressed(&self) -> (Option<image::Handle>, Color) {
|
||||
pub fn pressed(&self) -> (Option<image::Handle>, Color) {
|
||||
(self.background.as_ref().map(|b| b.press), self.enabled_text)
|
||||
}
|
||||
|
||||
fn hovered(&self) -> (Option<image::Handle>, Color) {
|
||||
pub fn hovered(&self) -> (Option<image::Handle>, Color) {
|
||||
(self.background.as_ref().map(|b| b.hover), self.enabled_text)
|
||||
}
|
||||
|
||||
fn active(&self) -> (Option<image::Handle>, Color) {
|
||||
pub fn active(&self) -> (Option<image::Handle>, Color) {
|
||||
(
|
||||
self.background.as_ref().map(|b| b.default),
|
||||
self.enabled_text,
|
||||
@ -101,65 +100,3 @@ impl Default for Style {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl button::Renderer for IcedRenderer {
|
||||
// TODO: what if this gets large enough to not be copied around?
|
||||
type Style = Style;
|
||||
|
||||
const DEFAULT_PADDING: u16 = 0;
|
||||
|
||||
fn draw<M>(
|
||||
&mut self,
|
||||
defaults: &Self::Defaults,
|
||||
bounds: Rectangle,
|
||||
cursor_position: Point,
|
||||
is_disabled: bool,
|
||||
is_pressed: bool,
|
||||
style: &Self::Style,
|
||||
content: &Element<'_, M, Self>,
|
||||
content_layout: Layout<'_>,
|
||||
) -> Self::Output {
|
||||
let is_mouse_over = bounds.contains(cursor_position);
|
||||
|
||||
let (maybe_image, text_color) = if is_disabled {
|
||||
style.disabled()
|
||||
} else if is_mouse_over {
|
||||
if is_pressed {
|
||||
style.pressed()
|
||||
} else {
|
||||
style.hovered()
|
||||
}
|
||||
} else {
|
||||
style.active()
|
||||
};
|
||||
|
||||
let (content, _) = content.draw(
|
||||
self,
|
||||
&Defaults { text_color },
|
||||
content_layout,
|
||||
cursor_position,
|
||||
);
|
||||
|
||||
let primitive = if let Some(handle) = maybe_image {
|
||||
let background = Primitive::Image {
|
||||
handle: (handle, Rotation::None),
|
||||
bounds,
|
||||
color: Rgba::broadcast(255),
|
||||
};
|
||||
|
||||
Primitive::Group {
|
||||
primitives: vec![background, content],
|
||||
}
|
||||
} else {
|
||||
content
|
||||
};
|
||||
|
||||
let mouse_interaction = if is_mouse_over {
|
||||
mouse::Interaction::Pointer
|
||||
} else {
|
||||
mouse::Interaction::default()
|
||||
};
|
||||
|
||||
(primitive, mouse_interaction)
|
||||
}
|
||||
}
|
3
voxygen/src/ui/ice/renderer/style/mod.rs
Normal file
3
voxygen/src/ui/ice/renderer/style/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
mod button;
|
||||
|
||||
pub use button::Style as ButtonStyle;
|
@ -1,4 +1,4 @@
|
||||
use super::{
|
||||
use super::super::{
|
||||
super::widget::{aspect_ratio_container, image},
|
||||
IcedRenderer,
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
use super::{super::widget::background_container, IcedRenderer, Primitive};
|
||||
use super::super::{super::widget::background_container, IcedRenderer, Primitive};
|
||||
use iced::{Element, Layout, Point};
|
||||
|
||||
impl background_container::Renderer for IcedRenderer {
|
65
voxygen/src/ui/ice/renderer/widgets/button.rs
Normal file
65
voxygen/src/ui/ice/renderer/widgets/button.rs
Normal file
@ -0,0 +1,65 @@
|
||||
use super::super::{super::Rotation, Defaults, IcedRenderer, Primitive};
|
||||
use iced::{button, mouse, Element, Layout, Point, Rectangle};
|
||||
use vek::Rgba;
|
||||
|
||||
impl button::Renderer for IcedRenderer {
|
||||
// TODO: what if this gets large enough to not be copied around?
|
||||
type Style = super::super::style::ButtonStyle;
|
||||
|
||||
const DEFAULT_PADDING: u16 = 0;
|
||||
|
||||
fn draw<M>(
|
||||
&mut self,
|
||||
_defaults: &Self::Defaults,
|
||||
bounds: Rectangle,
|
||||
cursor_position: Point,
|
||||
is_disabled: bool,
|
||||
is_pressed: bool,
|
||||
style: &Self::Style,
|
||||
content: &Element<'_, M, Self>,
|
||||
content_layout: Layout<'_>,
|
||||
) -> Self::Output {
|
||||
let is_mouse_over = bounds.contains(cursor_position);
|
||||
|
||||
let (maybe_image, text_color) = if is_disabled {
|
||||
style.disabled()
|
||||
} else if is_mouse_over {
|
||||
if is_pressed {
|
||||
style.pressed()
|
||||
} else {
|
||||
style.hovered()
|
||||
}
|
||||
} else {
|
||||
style.active()
|
||||
};
|
||||
|
||||
let (content, _) = content.draw(
|
||||
self,
|
||||
&Defaults { text_color },
|
||||
content_layout,
|
||||
cursor_position,
|
||||
);
|
||||
|
||||
let primitive = if let Some(handle) = maybe_image {
|
||||
let background = Primitive::Image {
|
||||
handle: (handle, Rotation::None),
|
||||
bounds,
|
||||
color: Rgba::broadcast(255),
|
||||
};
|
||||
|
||||
Primitive::Group {
|
||||
primitives: vec![background, content],
|
||||
}
|
||||
} else {
|
||||
content
|
||||
};
|
||||
|
||||
let mouse_interaction = if is_mouse_over {
|
||||
mouse::Interaction::Pointer
|
||||
} else {
|
||||
mouse::Interaction::default()
|
||||
};
|
||||
|
||||
(primitive, mouse_interaction)
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
use super::{IcedRenderer, Primitive};
|
||||
use super::super::{IcedRenderer, Primitive};
|
||||
use iced::{column, mouse, Element, Layout, Point};
|
||||
|
||||
impl column::Renderer for IcedRenderer {
|
@ -1,4 +1,4 @@
|
||||
use super::{
|
||||
use super::super::{
|
||||
super::{widget::compound_graphic, Rotation},
|
||||
IcedRenderer, Primitive,
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
use super::IcedRenderer;
|
||||
use super::super::IcedRenderer;
|
||||
use iced::{container, Element, Layout, Point, Rectangle};
|
||||
|
||||
impl container::Renderer for IcedRenderer {
|
@ -1,4 +1,4 @@
|
||||
use super::{
|
||||
use super::super::{
|
||||
super::{widget::image, Rotation},
|
||||
IcedRenderer, Primitive,
|
||||
};
|
10
voxygen/src/ui/ice/renderer/widgets/mod.rs
Normal file
10
voxygen/src/ui/ice/renderer/widgets/mod.rs
Normal file
@ -0,0 +1,10 @@
|
||||
mod aspect_ratio_container;
|
||||
mod background_container;
|
||||
mod button;
|
||||
mod column;
|
||||
mod compound_graphic;
|
||||
mod container;
|
||||
mod image;
|
||||
mod row;
|
||||
mod space;
|
||||
mod text;
|
@ -1,4 +1,4 @@
|
||||
use super::{IcedRenderer, Primitive};
|
||||
use super::super::{IcedRenderer, Primitive};
|
||||
use iced::{mouse, row, Element, Layout, Point};
|
||||
|
||||
impl row::Renderer for IcedRenderer {
|
@ -1,4 +1,4 @@
|
||||
use super::{IcedRenderer, Primitive};
|
||||
use super::super::{IcedRenderer, Primitive};
|
||||
use iced::{mouse, space, Rectangle};
|
||||
|
||||
impl space::Renderer for IcedRenderer {
|
@ -1,4 +1,4 @@
|
||||
use super::{super::widget::stack, IcedRenderer, Primitive};
|
||||
use super::super::{super::widget::stack, IcedRenderer, Primitive};
|
||||
use iced::{mouse, Element, Layout, Point};
|
||||
|
||||
impl stack::Renderer for IcedRenderer {
|
@ -1,4 +1,4 @@
|
||||
use super::{super::FontId, IcedRenderer, Primitive};
|
||||
use super::super::{super::FontId, IcedRenderer, Primitive};
|
||||
use glyph_brush::GlyphCruncher;
|
||||
use iced::{mouse, text, Color, HorizontalAlignment, Rectangle, Size, VerticalAlignment};
|
||||
|
Loading…
Reference in New Issue
Block a user