Skip to content

Commit

Permalink
migrate chat to newgui
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Feb 14, 2024
1 parent 22765d9 commit 76253ab
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 172 deletions.
4 changes: 2 additions & 2 deletions assets_gui/src/yakui_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use goryak::{
background, button_primary, checkbox_value, constrained_viewport, dragvalue, icon,
interact_box_radius, is_hovered, on_secondary_container, on_surface, outline_variant,
round_rect, secondary_container, set_theme, surface, surface_variant, textc, use_changed,
RoundRect, Theme, VertScroll,
RoundRect, Theme, VertScrollSize,
};
use prototypes::{prototypes_iter, GoodsCompanyID, GoodsCompanyPrototype};

Expand Down Expand Up @@ -80,7 +80,7 @@ impl State {
}
});
});
VertScroll::Percent(1.0).show(|| {
VertScrollSize::Percent(1.0).show(|| {
let mut l = List::column();
l.cross_axis_alignment = CrossAxisAlignment::Stretch;
l.main_axis_size = MainAxisSize::Min;
Expand Down
74 changes: 61 additions & 13 deletions goryak/src/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,35 @@ use yakui_core::Response;
use yakui_widgets::shapes::RoundedRectangle;

#[derive(Debug)]
pub enum VertScroll {
pub enum VertScrollSize {
/// The scroll size is a percentage of the parent's size.
Percent(f32),
/// The scroll size is exactly what is given (within constraints).
Exact(f32),
/// The scroll size is at most what is given (within constraints).
Fixed(f32),
/// The scroll size is equal to the parent's size.
Max,
}

impl VertScrollSize {
pub fn show<F: FnOnce()>(self, children: F) -> Response<VertScrollResponse> {
yakui_widgets::util::widget_children::<VertScrollWidget, F>(
children,
VertScroll {
size: self,
align_bot: false,
},
)
}
}

#[derive(Debug)]
pub struct VertScroll {
pub size: VertScrollSize,
pub align_bot: bool,
}

impl VertScroll {
pub fn show<F: FnOnce()>(self, children: F) -> Response<VertScrollResponse> {
yakui_widgets::util::widget_children::<VertScrollWidget, F>(children, self)
Expand All @@ -40,7 +63,10 @@ impl Widget for VertScrollWidget {

fn new() -> Self {
Self {
props: VertScroll::Max,
props: VertScroll {
size: VertScrollSize::Max,
align_bot: false,
},
scroll_position: Cell::new(0.0),
size: Cell::new(0.0),
canvas_size: Cell::new(0.0),
Expand All @@ -55,9 +81,10 @@ impl Widget for VertScrollWidget {
}

fn flex(&self) -> (u32, FlexFit) {
match self.props {
VertScroll::Max => (1, FlexFit::Tight),
VertScroll::Percent(_) => (1, FlexFit::Loose),
match self.props.size {
VertScrollSize::Max => (1, FlexFit::Tight),
VertScrollSize::Percent(_) => (1, FlexFit::Loose),
VertScrollSize::Exact(_) => (0, FlexFit::Tight),
_ => (0, FlexFit::Loose),
}
}
Expand All @@ -68,13 +95,17 @@ impl Widget for VertScrollWidget {
let node = ctx.dom.get_current();
let mut canvas_size = Vec2::ZERO;

let main_axis_size = match self.props {
VertScroll::Max => constraints.max.y,
VertScroll::Fixed(h) => {
let main_axis_size = match self.props.size {
VertScrollSize::Max => constraints.max.y,
VertScrollSize::Fixed(h) => {
constraints.max.y = constraints.max.y.min(h);
constraints.min.y
}
VertScroll::Percent(percent) => {
VertScrollSize::Exact(h) => {
constraints.max.y = constraints.max.y.min(h);
constraints.min.y
}
VertScrollSize::Percent(percent) => {
constraints.max.y = constraints.max.y * percent;
constraints.min.y
}
Expand All @@ -92,7 +123,10 @@ impl Widget for VertScrollWidget {
canvas_size = canvas_size.max(child_size);
}

let size = constraints.constrain(canvas_size);
let mut size = constraints.constrain(canvas_size);
if let VertScrollSize::Exact(_) = self.props.size {
size.y = size.y.max(constraints.max.y);
}

self.canvas_size.set(canvas_size.y);
self.size.set(size.y);
Expand All @@ -103,7 +137,12 @@ impl Widget for VertScrollWidget {
self.scroll_position.set(scroll_position);

for &child in &node.children {
ctx.layout.set_pos(child, Vec2::new(0.0, -scroll_position));
let mut off = Vec2::new(0.0, -scroll_position);
if self.props.align_bot {
off.y = size.y - canvas_size.y + scroll_position;
}

ctx.layout.set_pos(child, off);
}

size
Expand Down Expand Up @@ -134,10 +173,16 @@ impl Widget for VertScrollWidget {
drawn_rect.size().y * (drawn_rect.size().y / self.canvas_size.get());
let remaining_space = drawn_rect.size().y - scroll_bar_height - SCROLLBAR_PAD_Y;

let mut pos_y = remaining_space * scrollbar_progress + SCROLLBAR_PAD_Y * 0.5;

if self.props.align_bot {
pos_y = drawn_rect.size().y - scroll_bar_height - pos_y;
}

let scroll_bar_pos = drawn_rect.pos()
+ Vec2::new(
drawn_rect.size().x - scrollbar_width * 0.5 - SCROLLBAR_PAD_X,
remaining_space * scrollbar_progress + SCROLLBAR_PAD_Y * 0.5,
pos_y,
);
let scroll_bar_rect = Rect::from_pos_size(
scroll_bar_pos,
Expand Down Expand Up @@ -166,7 +211,10 @@ impl Widget for VertScrollWidget {

fn event(&mut self, _ctx: EventContext<'_>, event: &WidgetEvent) -> EventResponse {
match *event {
WidgetEvent::MouseScroll { delta } => {
WidgetEvent::MouseScroll { mut delta } => {
if self.props.align_bot {
delta.y = -delta.y;
}
*self.scroll_position.get_mut() += delta.y;
EventResponse::Sink
}
Expand Down
26 changes: 24 additions & 2 deletions goryak/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::DEFAULT_FONT_SIZE;
use std::borrow::Cow;
use yakui_core::geometry::Color;
use yakui_core::geometry::{Color, Constraints, Vec2};
use yakui_core::Response;
use yakui_widgets::constrained;
use yakui_widgets::font::FontName;
use yakui_widgets::widgets::{Text, TextResponse};
use yakui_widgets::widgets::{Text, TextBox, TextResponse};

pub fn text<S: Into<Cow<'static, str>>>(text: S) -> Response<TextResponse> {
Text::new(DEFAULT_FONT_SIZE, text.into()).show()
Expand All @@ -15,3 +16,24 @@ pub fn monospace<S: Into<Cow<'static, str>>>(col: Color, text: S) -> Response<Te
t.style.color = col;
t.show()
}

pub fn text_edit(width: f32, x: &mut String, placeholder: &str) -> bool {
let mut activated = false;
constrained(
Constraints {
min: Vec2::new(width, 20.0),
max: Vec2::new(f32::INFINITY, f32::INFINITY),
},
|| {
let mut text = TextBox::new(x.clone());
text.placeholder = placeholder.to_string();
text.fill = Some(Color::rgba(0, 0, 0, 50));
let resp = text.show().into_inner();
if let Some(changed) = resp.text {
*x = changed;
}
activated = resp.activated;
},
);
activated
}
123 changes: 0 additions & 123 deletions native_app/src/gui/chat.rs

This file was deleted.

3 changes: 0 additions & 3 deletions native_app/src/gui/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use prototypes::Money;
use simulation::economy::Government;
use simulation::Simulation;

use crate::gui::chat::chat;
use crate::gui::debug_inspect::debug_inspector;
use crate::gui::debug_window::debug_window;
use crate::newgui::{ErrorTooltip, GuiState, PotentialCommands};
Expand All @@ -19,8 +18,6 @@ pub fn render_oldgui(ui: &Context, uiworld: &UiWorld, sim: &Simulation) {

debug_inspector(ui, uiworld, sim);

chat(ui, uiworld, sim);

debug_window(ui, uiworld, sim);

tooltip(ui, uiworld, sim);
Expand Down
1 change: 0 additions & 1 deletion native_app/src/gui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod chat;
pub mod debug_inspect;
pub mod debug_window;
pub mod hud;
Expand Down
2 changes: 1 addition & 1 deletion native_app/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::game_loop::Timings;
use crate::gui::chat::GUIChatState;
use crate::gui::debug_window::{DebugObjs, DebugState, TestFieldProperties};
use crate::inputmap::{Bindings, InputMap};
use crate::network::NetworkState;
use crate::newgui::bulldozer::BulldozerState;
use crate::newgui::chat::GUIChatState;
use crate::newgui::follow::FollowEntity;
use crate::newgui::lotbrush::LotBrushResource;
use crate::newgui::roadbuild::RoadBuildResource;
Expand Down
2 changes: 2 additions & 0 deletions native_app/src/newgui/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::newgui::windows::settings::Settings;
use crate::newgui::GuiState;
use crate::uiworld::{SaveLoadState, UiWorld};

pub mod chat;
mod menu;
mod time_controls;
pub mod toolbox;
Expand All @@ -35,6 +36,7 @@ pub fn render_newgui(uiworld: &UiWorld, sim: &Simulation) {
power_errors(uiworld, sim);
new_toolbox(uiworld, sim);
menu_bar(uiworld, sim);
chat::chat(uiworld, sim);
new_inspector(uiworld, sim);
uiworld.write::<GuiState>().windows.render(uiworld, sim);
time_controls(uiworld, sim);
Expand Down
Loading

0 comments on commit 76253ab

Please sign in to comment.