This commit is contained in:
2026-01-12 18:42:34 -05:00
parent efb21705c3
commit 040b70e1c5
9 changed files with 517 additions and 196 deletions

View File

@@ -1,28 +1,30 @@
use crate::Client;
use iris::prelude::*;
use crate::{Client, Rsc};
use openworm::net::NetServerMsg;
pub fn debug(_client: &mut Client, ui: &mut Ui, _state: &UiState) {
ui.debug_layers();
// let mut file = std::fs::OpenOptions::new()
// .write(true)
// .create(true)
// .truncate(true)
// .open("./old_msgs")
// .unwrap();
// bincode::encode_into_std_write(
// self.msgs.clone(),
// &mut file,
// openworm::net::BINCODE_CONFIG,
// )
// .unwrap();
// let mut file = std::fs::OpenOptions::new()
// .read(true)
// .open("./old_msgs")
// .unwrap();
// let msgs: Vec<NetMsg> =
// bincode::decode_from_std_read(&mut file, openworm::net::BINCODE_CONFIG).unwrap();
// for msg in msgs {
// println!("{msg:?}");
// }
// client.ui.debug_layers();
impl Client {
pub fn debug(&mut self, rsc: &mut Rsc) {
rsc.ui.debug_layers();
// let mut file = std::fs::OpenOptions::new()
// .write(true)
// .create(true)
// .truncate(true)
// .open("./old_msgs")
// .unwrap();
// bincode::encode_into_std_write(
// self.msgs.clone(),
// &mut file,
// openworm::net::BINCODE_CONFIG,
// )
// .unwrap();
let mut file = std::fs::OpenOptions::new()
.read(true)
.open("/home/bryan/.local/share/openworm/old_msgs")
.unwrap();
let msgs: Vec<NetServerMsg> =
bincode::decode_from_std_read(&mut file, openworm::net::BINCODE_CONFIG).unwrap();
for msg in msgs {
println!("{msg:?}");
}
// client.ui.debug_layers();
}
}

View File

@@ -11,10 +11,7 @@ use openworm::{
net::{ClientMsg, ServerMsg, install_crypto_provider},
rsc::DataDir,
};
use winit::{
event::{ElementState, MouseButton, WindowEvent},
window::WindowAttributes,
};
use winit::event::{ElementState, MouseButton, WindowEvent};
mod account;
mod debug;
@@ -28,7 +25,9 @@ fn main() {
DefaultApp::<Client>::run();
}
#[derive(DefaultUiState)]
pub struct Client {
ui_state: DefaultUiState,
dir: DataDir,
data: ClientData,
state: ClientState,
@@ -37,6 +36,8 @@ pub struct Client {
proxy: Proxy<ClientEvent>,
}
pub type Rsc = DefaultRsc<Client>;
pub enum ClientEvent {
Connect { send: NetSender },
ServerMsg(ServerMsg),
@@ -46,14 +47,14 @@ pub enum ClientEvent {
impl DefaultAppState for Client {
type Event = ClientEvent;
fn window_attrs() -> WindowAttributes {
WindowAttributes::default().with_title("OPENWORM")
}
fn new(ui: &mut Ui, _state: &UiState, proxy: Proxy<Self::Event>) -> Self {
fn new(
ui_state: DefaultUiState,
rsc: &mut DefaultRsc<Self>,
proxy: Proxy<Self::Event>,
) -> Self {
let dir = DataDir::default();
let notif = WidgetPtr::default().add(ui);
let main_ui = WidgetPtr::default().add(ui);
let notif = WidgetPtr::default().add(rsc);
let main_ui = WidgetPtr::default().add(rsc);
let bg = (
image(include_bytes!("./assets/fuit.jpg")),
rect(Color::BLACK.alpha((0.8 * 255.0) as u8)),
@@ -62,25 +63,26 @@ impl DefaultAppState for Client {
(
bg,
main_ui.clone(),
notif.clone().pad(Padding::top(10)).align(Align::TOP_CENTER),
main_ui,
notif.pad(Padding::top(10)).align(Align::TOP_CENTER),
)
.stack()
.set_root(ui);
.set_root(rsc);
let mut s = Self {
main_view(rsc).set_ptr(main_ui, rsc);
Self {
ui_state,
data: dir.load(CLIENT_DATA),
state: Default::default(),
dir,
main_ui: main_ui.clone(),
main_ui,
notif,
proxy,
};
start_ui(&mut s, ui).set_ptr(&s.main_ui, ui);
s
}
}
fn event(&mut self, event: ClientEvent, ui: &mut Ui, _state: &UiState) {
fn event(&mut self, event: Self::Event, rsc: &mut DefaultRsc<Self>) {
match event {
ClientEvent::Connect { send } => {
let ClientState::Connect(connect) = self.state.take() else {
@@ -98,20 +100,20 @@ impl DefaultAppState for Client {
ClientEvent::ServerMsg(msg) => match msg {
ServerMsg::SendMsg(msg) => {
if let ClientState::LoggedIn(state) = &mut self.state
&& let Some(msg_area) = &state.channel
&& let Some(msg_area) = state.channel
{
let msg = msg_widget(&msg.user, &msg.content).add(ui);
msg_area.get_mut().children.push(msg);
let msg = msg_widget(&msg.user, &msg.content).add_strong(rsc);
rsc.ui[msg_area].children.push(msg);
}
}
ServerMsg::LoadMsgs(msgs) => {
if let ClientState::LoggedIn(state) = &mut self.state
&& let Some(msg_area) = &state.channel
&& let Some(msg_area) = state.channel
{
for msg in msgs {
state.msgs.push(msg.clone());
let msg = msg_widget(&msg.user, &msg.content).add(ui);
msg_area.get_mut().children.push(msg);
let msg = msg_widget(&msg.user, &msg.content).add_strong(rsc);
rsc.ui[msg_area].children.push(msg);
}
}
}
@@ -126,32 +128,32 @@ impl DefaultAppState for Client {
msgs: Vec::new(),
username,
});
main_view(self, ui).set_ptr(&self.main_ui, ui);
main_view(rsc).set_ptr(self.main_ui, rsc);
}
ServerMsg::Error(error) => {
let msg = format!("{error:?}");
self.notif.get_mut().inner = Some(werror(ui, &msg));
rsc.ui[self.notif].inner = Some(werror(&msg, rsc));
}
},
ClientEvent::Err(msg) => {
self.notif.get_mut().inner = Some(werror(ui, &msg));
rsc.ui[self.notif].inner = Some(werror(&msg, rsc));
}
}
}
fn exit(&mut self, _ui: &mut Ui, _state: &UiState) {
fn exit(&mut self, _: &mut DefaultRsc<Self>) {
self.state.exit();
self.dir.save(CLIENT_DATA, &self.data);
}
fn window_event(&mut self, event: WindowEvent, ui: &mut Ui, state: &UiState) {
fn window_event(&mut self, event: WindowEvent, rsc: &mut DefaultRsc<Self>) {
if let WindowEvent::MouseInput {
state: ElementState::Pressed,
button: MouseButton::Middle,
..
} = event
{
debug::debug(self, ui, state);
self.debug(rsc);
}
}
}

View File

@@ -1,9 +1,8 @@
use crate::net::NetHandle;
use iris::prelude::*;
use openworm::net::NetServerMsg;
use std::thread::JoinHandle;
use crate::net::NetHandle;
#[derive(Default)]
pub struct Connect {
pub handle: Option<JoinHandle<()>>,

View File

@@ -1,6 +1,6 @@
use super::*;
pub fn start_ui(client: &mut Client, ui: &mut Ui) -> WidgetRef {
pub fn start_ui(rsc: &mut Rsc) -> WidgetRef {
let mut accounts = Span::empty(Dir::DOWN);
accounts.push(
@@ -9,16 +9,14 @@ pub fn start_ui(client: &mut Client, ui: &mut Ui) -> WidgetRef {
.center_text()
.color(Color::GRAY)
.height(60)
.add(ui),
.add_strong(rsc),
);
let connect = Button::submit("connect", ui);
let create = Button::submit("create", ui);
let connect_ = connect.clone();
let create_ = create.clone();
ui.on(connect.view(), Submit, move |_| {
connect_.disable();
create_.disable();
let connect = Button::submit("connect", rsc);
let create = Button::submit("create", rsc);
connect.on(Submit, move |_, rsc| {
connect.disable();
create.disable();
});
let connect_ = connect.clone();

View File

@@ -1,22 +1,19 @@
use crate::Rsc;
use super::*;
use crate::state::{ClientState, LoggedIn};
use openworm::net::{ClientMsg, NetClientMsg};
pub const SIZE: u32 = 20;
pub fn main_view(client: &mut Client, ui: &mut Ui) -> WidgetRef {
let ClientState::LoggedIn(state) = &mut client.state else {
panic!("we ain't logged in buh");
};
let msg_panel = msg_panel(ui, state);
pub fn main_view(rsc: &mut Rsc) -> WidgetRef {
let msg_panel = msg_panel(rsc);
let side_bar = rect(Color::BLACK.brighter(0.05)).width(80);
(side_bar, msg_panel).span(Dir::RIGHT).add(ui)
(side_bar, msg_panel).span(Dir::RIGHT).add(rsc)
}
pub fn msg_widget(username: &str, content: &str) -> impl WidgetRet {
pub fn msg_widget(username: &str, content: &str) -> impl WidgetIdFn<Rsc> {
let content = wtext(content)
.editable(false)
.editable(EditMode::MultiLine)
.size(SIZE)
.wrap(true)
.attr::<Selectable>(());
@@ -36,40 +33,32 @@ pub fn msg_widget(username: &str, content: &str) -> impl WidgetRet {
.to_any()
}
pub fn msg_panel(ui: &mut Ui, state: &mut LoggedIn) -> WidgetRef {
let msg_area = Span::empty(Dir::DOWN).gap(15).add(ui);
state.channel = Some(msg_area.clone());
pub fn msg_panel(rsc: &mut Rsc) -> WidgetRef {
let msg_area = Span::empty(Dir::DOWN).gap(15);
let send_text = wtext("")
.editable(false)
.editable(EditMode::MultiLine)
.size(SIZE)
.wrap(true)
.hint(hint("send message"))
.add(ui);
.add(rsc);
let msg_area = msg_area.add(rsc);
(
msg_area
.clone()
.scroll()
.scrollable()
.pad(Padding::x(15).with_top(15))
.height(rest(1)),
send_text
.clone()
.on(Submit, move |ctx| {
let ClientState::LoggedIn(state) = &mut ctx.state.state else {
panic!("we ain't logged in buh");
};
let content = ctx.widget.get_mut().take();
let msg = NetClientMsg {
content: content.clone(),
};
state.network.send(ClientMsg::SendMsg(msg.clone()));
let msg = msg_widget(&state.username, &content).add(ctx.ui);
msg_area.get_mut().children.push(msg);
.on(Submit, move |ctx, rsc| {
let content = ctx.widget.edit(rsc).take();
let msg = msg_widget("ur mothe:", &content).add_strong(rsc);
rsc.ui[msg_area].children.push(msg);
})
.pad(15)
.attr::<Selector>(send_text)
.scroll()
.scrollable()
.masked()
.background(rect(Color::BLACK.brighter(0.05)).radius(15))
.pad(15)
@@ -78,5 +67,5 @@ pub fn msg_panel(ui: &mut Ui, state: &mut LoggedIn) -> WidgetRef {
)
.span(Dir::DOWN)
.width(rest(1))
.add(ui)
.add(rsc)
}

View File

@@ -1,106 +1,101 @@
use super::*;
pub fn werror(ui: &mut Ui, msg: &str) -> WidgetRef {
pub fn werror(msg: &str, rsc: &mut Rsc) -> WidgetHandle {
wtext(msg)
.size(20)
.pad(10)
.background(rect(Color::RED).radius(10))
.add(ui)
.add_strong(rsc)
}
pub fn hint(msg: impl Into<String>) -> TextBuilder {
pub fn hint(msg: impl Into<String>) -> TextBuilder<Rsc> {
wtext(msg).size(20).color(Color::GRAY)
}
pub fn field_widget(name: &str, hint_text: &str, ui: &mut Ui) -> WidgetRef<TextEdit> {
pub fn field_widget(name: &str, hint_text: &str, rsc: &mut Rsc) -> WidgetRef<TextEdit> {
wtext(name)
.editable(true)
.editable(EditMode::SingleLine)
.size(20)
.hint(hint(hint_text))
.add(ui)
.add(rsc)
}
pub fn field_box(field: WidgetRef<TextEdit>, ui: &mut Ui) -> WidgetRef {
pub fn field_box(field: WidgetRef<TextEdit>, rsc: &mut Rsc) -> WidgetRef {
field
.clone()
.pad(10)
.background(rect(Color::BLACK.brighter(0.1)).radius(15))
.attr::<Selector>(field)
.add(ui)
.add(rsc)
}
#[derive(Clone)]
#[derive(Clone, Copy, WidgetView)]
pub struct Button {
color: UiColor,
#[root]
root: WidgetRef,
rect: WidgetRef<Rect>,
enabled: Handle<bool>,
}
impl WidgetView for Button {
fn view(&self) -> &WidgetRef<Self::Widget> {
&self.root
}
}
// impl WidgetView for Button {
// fn view(&self) -> &WidgetRef<Self::Widget> {
// &self.root
// }
// }
impl Button {
pub fn new(text: &str, color: UiColor, ui: &mut Ui) -> Self {
let rect = rect(color).radius(15).add(ui);
let enabled = Handle::from(true);
let enabled_ = enabled.clone();
let enabled__ = enabled.clone();
pub fn new(text: &str, color: UiColor, rsc: &mut Rsc) -> Self {
let rect = rect(color).radius(15).add(rsc);
// let enabled = Handle::from(true);
// let enabled_ = enabled.clone();
// let enabled__ = enabled.clone();
let root = rect
.clone()
.on(
CursorSense::HoverStart | CursorSense::unclick(),
move |ctx| {
if !*enabled_.get() {
return;
}
ctx.widget.get_mut().color = color.brighter(0.1);
move |ctx, rsc: &mut Rsc| {
// if !*enabled_.get() {
// return;
// }
rsc.ui[ctx.widget].color = color.brighter(0.1);
},
)
.on(CursorSense::HoverEnd, move |ctx| {
if !*enabled__.get() {
return;
}
ctx.widget.get_mut().color = color;
.on(CursorSense::HoverEnd, move |ctx, rsc| {
// if !*enabled__.get() {
// return;
// }
rsc.ui[ctx.widget].color = color;
})
.height(60)
.foreground(wtext(text).size(25).text_align(Align::CENTER))
.add(ui);
let root_ = root.clone();
let enabled_ = enabled.clone();
rect.clone()
.on(CursorSense::click(), move |ctx| {
if !*enabled_.get() {
return;
}
ctx.widget.get_mut().color = color.darker(0.2);
ctx.ui.run_event(ctx.state, &root_, Submit, ());
})
.add(ui);
.add(rsc);
// let enabled_ = enabled.clone();
rect.on(CursorSense::click(), move |ctx, rsc: &mut Rsc| {
// if !*enabled_.get() {
// return;
// }
rsc.ui[ctx.widget].color = color.darker(0.2);
rsc.run_event::<Submit>(root, (), ctx.state);
})
.add(rsc);
Self {
root,
rect,
color,
enabled,
// enabled,
}
}
pub fn submit(text: &str, ui: &mut Ui) -> Self {
Self::new(text, color::GREEN, ui)
pub fn submit(text: &str, rsc: &mut Rsc) -> Self {
Self::new(text, color::GREEN, rsc)
}
pub fn disable(&self) {
pub fn disable(&self, rsc: &mut Rsc) {
*self.enabled.get_mut() = false;
self.rect.get_mut().color = self.color.darker(0.8);
}
}
widget_trait! {
pub trait Stuff;
fn modal(self, width: impl UiNum) -> impl WidgetIdFn {
pub trait Stuff<Rsc: HasUi + 'static>;
fn modal(self, width: impl UiNum) -> impl WidgetIdFn<Rsc> {
|ui| {
self
.pad(15)

View File

@@ -1,14 +1,11 @@
use crate::Client;
use crate::Rsc;
use iris::prelude::*;
use len_fns::*;
pub mod color;
mod connect;
mod main;
mod misc;
pub mod color;
pub use connect::*;
pub use main::*;
pub use misc::*;
event_ctx!(Client);