Skip to content

Commit

Permalink
Add WidgetEvent and basic treeview example
Browse files Browse the repository at this point in the history
  • Loading branch information
geom3trik committed Oct 17, 2021
1 parent 4304dd0 commit 862f196
Show file tree
Hide file tree
Showing 15 changed files with 646 additions and 40 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ path = "examples/widgets/label.rs"
name = "button"
path = "examples/widgets/button.rs"

[[example]]
name = "button2"
path = "examples/widgets/button2.rs"

[[example]]
name = "checkbox"
path = "examples/widgets/checkbox.rs"
Expand Down Expand Up @@ -173,6 +177,10 @@ name = "treeview2"
path = "examples/binding/treeview2.rs"


[[example]]
name = "basic_treeview"
path = "examples/widgets/treeview/basic_treeview.rs"

[[example]]
name = "model"
path = "examples/binding/model.rs"
Expand Down
8 changes: 8 additions & 0 deletions core/src/layout/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,17 @@ impl Cache for CachedData {
self.get_grid_row_max(node)
}

fn set_grid_row_max(&mut self, node: Self::Item, value: f32) {
self.set_grid_row_max(node, value);
}

fn grid_col_max(&self, node: Self::Item) -> f32 {
self.get_grid_col_max(node)
}

fn set_grid_col_max(&mut self, node: Self::Item, value: f32) {
self.set_grid_col_max(node, value);
}


}
12 changes: 12 additions & 0 deletions core/src/state/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,22 @@ impl CachedData {
self.grid_row_max.get(entity).cloned().unwrap_or_default()
}

pub(crate) fn set_grid_row_max(&mut self, entity: Entity, value: f32) {
if let Some(grid_row_max) = self.grid_row_max.get_mut(entity) {
*grid_row_max = value;
}
}

pub(crate) fn get_grid_col_max(&self, entity: Entity) -> f32 {
self.grid_col_max.get(entity).cloned().unwrap_or_default()
}

pub(crate) fn set_grid_col_max(&mut self, entity: Entity, value: f32) {
if let Some(grid_col_max) = self.grid_col_max.get_mut(entity) {
*grid_col_max = value;
}
}

pub(crate) fn get_stack_child(&self, entity: Entity) -> (bool, bool) {
self.stack_child
.get(entity)
Expand Down
22 changes: 22 additions & 0 deletions core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ const STYLE: &str = r#"
border-color: black;
border-width: 1px;
}
/*
button {
background-color: #CCCCCC;
width: 100px;
height: 30px;
child-space: 1s;
border-radius: 3px;
}
button:hover {
background-color: #E6E6E6;
}
button:active {
background-color: #737373;
}
button:disabled {
color: #737373;
}
*/
"#;

// #[derive(Clone)]
Expand Down
4 changes: 4 additions & 0 deletions core/src/style/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ pub(crate) enum Property {
Transition(Vec<Transition>),

ZIndex(i32),

Translate((f32, f32)),
Rotate(f32),
Scale((f32, f32)),
}

/*
Expand Down
19 changes: 11 additions & 8 deletions core/src/systems/hover_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,31 @@ pub fn apply_hover(state: &mut State) {

//transform.premultiply(&scale_transform);

//transform.translate(origin.0, origin.1);
//transform.translate(-posx - width / 2.0, -posy - height / 2.0);
transform.inverse();
//transform.translate(-origin.0, -origin.1);
//transform.translate(posx + width / 2.0, posy + height / 2.0);

let (cx, cy) = transform.transform_point(cursorx, cursory);
let (clip_x, clip_y) = transform.transform_point(clip_region.x, clip_region.y);
let (clip_w, clip_h) = transform.transform_point(clip_region.x + clip_region.w, clip_region.y + clip_region.h);
//transform.inverse();
//let (clip_x, clip_y) = transform.transform_point(clip_region.x, clip_region.y);
//let (clip_w, clip_h) = transform.transform_point(clip_region.x + clip_region.w, clip_region.y + clip_region.h);
// let clip_posx = state.data.get_posx(clip_widget);
// let clip_posy = state.data.get_posy(clip_widget);
// let clip_width = state.data.get_width(clip_widget);
// let clip_height = state.data.get_height(clip_widget);

//println!("entity: {} {} {} {} {}", entity, posx, posy, width, height);
//println!("entity: {} {} {} {} {}", entity, posx, posy, cx, cy);
//println!("entity: {} clip: {:?} tclip: BoundingBox {{ x: {}, y: {}, w: {}, h: {} }}", entity, clip_region, clip_x, clip_y, clip_w, clip_h);

if cx >= posx
&& cx >= clip_x
&& cx >= clip_region.x
&& cx < (posx + width)
&& cx < (clip_w)
&& cx < (clip_region.x + clip_region.w)
&& cy >= posy
&& cy >= clip_y
&& cy >= clip_region.y
&& cy < (posy + height)
&& cy < (clip_h)
&& cy < (clip_region.y + clip_region.h)
{
hovered_widget = entity;
if entity.is_over(state) == false {
Expand Down
5 changes: 4 additions & 1 deletion core/src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ mod widget;
pub use widget::Widget;

mod builder;
pub use builder::Builder;
pub use builder::Builder;

mod widget_event;
pub use widget_event::WidgetEvent;
5 changes: 3 additions & 2 deletions core/src/widget/widget.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Builder, EventHandler, WindowEvent};
use crate::{Builder, EventHandler, PropSet, WidgetEvent, WindowEvent};
use crate::{AsEntity, BorderCornerShape, Entity, FontOrId, Lens, LensWrapRef, Node, PropType, State, TreeExt, LensWrap};
use femtovg::{PixelFormat, RenderTarget};
use femtovg::{
Expand Down Expand Up @@ -40,7 +40,8 @@ pub trait Widget: std::marker::Sized + 'static {
// Create a new entity
let entity = state.add(parent.entity());

state.insert_event(Event::new(WindowEvent::ChildAdded(entity)).direct(parent.entity()));
//state.insert_event(Event::new(WidgetEvent::ChildAdded(entity)).direct(parent.entity()));
parent.entity().emit(state, WidgetEvent::ChildAdded(entity));

// Call the on_build function of the widget
let ret = self.on_build(state, entity);
Expand Down
9 changes: 9 additions & 0 deletions core/src/widget/widget_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::Entity;




pub enum WidgetEvent {
ChildAdded(Entity),
ChildRemoved(Entity),
}
6 changes: 3 additions & 3 deletions core/src/window/window_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum WindowEvent {
Relayout,
/// Prints the debug message to the console
Debug(String),
///
// TODO - move to WidgetEvent
ChildAdded(Entity),
//
// // TODO - move to WidgetEvent
// ChildAdded(Entity),
}
Loading

0 comments on commit 862f196

Please sign in to comment.