Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement internal math module #114

Merged
merged 10 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pax-chassis-common/src/core_graphics_c_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::mem::{transmute, ManuallyDrop};
use std::os::raw::c_char;

use core_graphics::context::CGContext;
use pax_runtime::math::Point2;
use piet_coregraphics::CoreGraphicsContext;

use flexbuffers;
Expand Down Expand Up @@ -95,7 +96,7 @@ pub extern "C" fn pax_interrupt(
NativeInterrupt::Click(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
match prospective_hit {
Some(topmost_node) => {
let modifiers = args
Expand Down
10 changes: 8 additions & 2 deletions pax-chassis-web/interface/src/events/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function setupEventListeners(chassis: PaxChassisWeb, layer: any) {

// @ts-ignore
layer.addEventListener('click', (evt) => {

let clickEvent = {
"Click": {
"x": evt.clientX,
Expand Down Expand Up @@ -72,11 +73,13 @@ export function setupEventListeners(chassis: PaxChassisWeb, layer: any) {
}, true);
// @ts-ignore
layer.addEventListener('mousemove', (evt) => {
// @ts-ignore
let button = window.current_button || 'Left';
let event = {
"MouseMove": {
"x": evt.clientX,
"y": evt.clientY,
"button": getMouseButton(evt),
"button": button,
"modifiers": convertModifiers(evt)
}
};
Expand All @@ -97,6 +100,9 @@ export function setupEventListeners(chassis: PaxChassisWeb, layer: any) {
}, {"passive": true, "capture": true});
// @ts-ignore
layer.addEventListener('mousedown', (evt) => {
let button = getMouseButton(evt);
// @ts-ignore
window.current_button = button;
let event = {
"MouseDown": {
"x": evt.clientX,
Expand Down Expand Up @@ -231,4 +237,4 @@ export function setupEventListeners(chassis: PaxChassisWeb, layer: any) {
};
chassis.interrupt(JSON.stringify(event), []);
}, true);
}
}
27 changes: 14 additions & 13 deletions pax-chassis-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use pax_runtime::api::ArgsButtonClick;
use pax_runtime::api::ArgsCheckboxChange;
use pax_runtime::api::ArgsTextboxChange;
use pax_runtime::api::RenderContext;
use pax_runtime::math::Point2;
use pax_runtime::ExpressionTable;
use std::cell::RefCell;

Expand Down Expand Up @@ -215,7 +216,7 @@ impl PaxChassisWeb {
NativeInterrupt::Click(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_click = ArgsClick {
mouse: MouseEventArgs {
Expand Down Expand Up @@ -245,7 +246,7 @@ impl PaxChassisWeb {
NativeInterrupt::Clap(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_clap = ArgsClap {
x: args.x,
Expand All @@ -258,7 +259,7 @@ impl PaxChassisWeb {
let first_touch = args.touches.get(0).unwrap();
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((first_touch.x, first_touch.y));
.get_topmost_element_beneath_ray(Point2::new(first_touch.x, first_touch.y));
if let Some(topmost_node) = prospective_hit {
let touches = args.touches.iter().map(|x| Touch::from(x)).collect();
let args_touch_start = ArgsTouchStart { touches };
Expand All @@ -273,7 +274,7 @@ impl PaxChassisWeb {
let first_touch = args.touches.get(0).unwrap();
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((first_touch.x, first_touch.y));
.get_topmost_element_beneath_ray(Point2::new(first_touch.x, first_touch.y));
if let Some(topmost_node) = prospective_hit {
let touches = args.touches.iter().map(|x| Touch::from(x)).collect();
let args_touch_move = ArgsTouchMove { touches };
Expand All @@ -288,7 +289,7 @@ impl PaxChassisWeb {
let first_touch = args.touches.get(0).unwrap();
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((first_touch.x, first_touch.y));
.get_topmost_element_beneath_ray(Point2::new(first_touch.x, first_touch.y));
if let Some(topmost_node) = prospective_hit {
let touches = args.touches.iter().map(|x| Touch::from(x)).collect();
let args_touch_end = ArgsTouchEnd { touches };
Expand Down Expand Up @@ -360,7 +361,7 @@ impl PaxChassisWeb {
NativeInterrupt::DoubleClick(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_double_click = ArgsDoubleClick {
mouse: MouseEventArgs {
Expand All @@ -384,7 +385,7 @@ impl PaxChassisWeb {
NativeInterrupt::MouseMove(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_mouse_move = ArgsMouseMove {
mouse: MouseEventArgs {
Expand All @@ -408,7 +409,7 @@ impl PaxChassisWeb {
NativeInterrupt::Wheel(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let modifiers = args
.modifiers
Expand All @@ -428,7 +429,7 @@ impl PaxChassisWeb {
NativeInterrupt::MouseDown(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_mouse_down = ArgsMouseDown {
mouse: MouseEventArgs {
Expand All @@ -452,7 +453,7 @@ impl PaxChassisWeb {
NativeInterrupt::MouseUp(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_mouse_up = ArgsMouseUp {
mouse: MouseEventArgs {
Expand All @@ -472,7 +473,7 @@ impl PaxChassisWeb {
NativeInterrupt::MouseOver(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_mouse_over = ArgsMouseOver {
mouse: MouseEventArgs {
Expand All @@ -496,7 +497,7 @@ impl PaxChassisWeb {
NativeInterrupt::MouseOut(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_mouse_out = ArgsMouseOut {
mouse: MouseEventArgs {
Expand All @@ -520,7 +521,7 @@ impl PaxChassisWeb {
NativeInterrupt::ContextMenu(args) => {
let prospective_hit = engine
.runtime_context
.get_topmost_element_beneath_ray((args.x, args.y));
.get_topmost_element_beneath_ray(Point2::new(args.x, args.y));
if let Some(topmost_node) = prospective_hit {
let args_context_menu = ArgsContextMenu {
mouse: MouseEventArgs {
Expand Down
2 changes: 1 addition & 1 deletion pax-compiler/templates/cartridge_generation/cartridge.tera
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,4 @@ impl DefinitionToInstanceTraverser {
}
None
}
}
}
2 changes: 1 addition & 1 deletion pax-compiler/templates/cartridge_generation/macros.tera
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ impl TypeFactory for {{active_type.type_id_escaped}}TypeFactory {
}
}

{%- endmacro -%}
{%- endmacro -%}
2 changes: 2 additions & 0 deletions pax-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub extern crate pax_macro;
pub use pax_macro::*;

pub use pax_runtime::api;
pub use pax_runtime::engine::node_interface::*;
pub use pax_runtime::math;
pub use pax_runtime::rendering;

pub use pax_runtime::api::log;
Expand Down
39 changes: 38 additions & 1 deletion pax-runtime/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use lazy_static::lazy_static;
use mut_static::MutStatic;
use piet::PaintBrush;

use crate::math::{Point2, Space};
use crate::node_interface::NodeInterface;
pub use crate::numeric::Numeric;
use crate::{PropertyExpression, RuntimeContext};
use pax_manifest::constants::COMMON_PROPERTIES_TYPE;
Expand Down Expand Up @@ -143,12 +145,47 @@ pub struct NodeContext<'a> {
/// The bounds of this element in px
pub bounds_self: (f64, f64),
/// Borrow of the RuntimeContext, used at least for exposing raycasting to userland
pub runtime_context: &'a RuntimeContext,
pub(crate) runtime_context: &'a RuntimeContext,

#[cfg(feature = "designtime")]
pub designtime: Rc<RefCell<DesigntimeManager>>,
}

pub struct Window;

impl Space for Window {}

#[cfg(feature = "designtime")]
impl NodeContext<'_> {
pub fn raycast(&self, point: Point2<Window>) -> Vec<NodeInterface> {
let expanded_nodes =
self.runtime_context
.get_elements_beneath_ray(point.to_world(), false, vec![]);
expanded_nodes
.into_iter()
.map(Into::<NodeInterface>::into)
.collect()
}

pub fn get_nodes_by_global_id(&self, type_id: &str, template_id: usize) -> Vec<NodeInterface> {
let expanded_nodes = self
.runtime_context
.get_expanded_nodes_by_global_ids(type_id, template_id);
expanded_nodes
.into_iter()
.map(Into::<NodeInterface>::into)
.collect()
}

pub fn get_nodes_by_id(&self, id: &str) -> Vec<NodeInterface> {
let expanded_nodes = self.runtime_context.get_expanded_nodes_by_id(id);
expanded_nodes
.into_iter()
.map(Into::<NodeInterface>::into)
.collect()
}
}

// Unified events

/// A Clap describes either a "click" (mousedown followed by mouseup), OR a
Expand Down
7 changes: 3 additions & 4 deletions pax-runtime/src/engine/expanded_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use crate::constants::{
MOUSE_OVER_HANDLERS, MOUSE_UP_HANDLERS, SCROLL_HANDLERS, TEXTBOX_CHANGE_HANDLERS,
TOUCH_END_HANDLERS, TOUCH_MOVE_HANDLERS, TOUCH_START_HANDLERS, WHEEL_HANDLERS,
};
use crate::math::Point2;
use crate::Globals;
#[cfg(debug_assertions)]
use core::fmt;
use std::any::Any;
use std::cell::RefCell;
use std::rc::{Rc, Weak};

use kurbo::Point;

use crate::api::{
ArgsButtonClick, ArgsCheckboxChange, ArgsClap, ArgsClick, ArgsContextMenu, ArgsDoubleClick,
ArgsKeyDown, ArgsKeyPress, ArgsKeyUp, ArgsMouseDown, ArgsMouseMove, ArgsMouseOut,
Expand Down Expand Up @@ -412,7 +411,7 @@ impl ExpandedNode {

/// Determines whether the provided ray, orthogonal to the view plane,
/// intersects this `ExpandedNode`.
pub fn ray_cast_test(&self, ray: &(f64, f64)) -> bool {
pub fn ray_cast_test(&self, ray: Point2) -> bool {
// Don't vacuously hit for `invisible_to_raycasting` nodes
if self.instance_node.base().flags().invisible_to_raycasting {
return false;
Expand All @@ -422,7 +421,7 @@ impl ExpandedNode {
let computed_tab = &props.as_ref().unwrap().computed_tab;

let inverted_transform = computed_tab.transform.inverse();
let transformed_ray = inverted_transform * Point { x: ray.0, y: ray.1 };
let transformed_ray = inverted_transform * ray;

let relevant_bounds = computed_tab.bounds;

Expand Down
17 changes: 12 additions & 5 deletions pax-runtime/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ use std::rc::Rc;
use pax_message::{NativeMessage, OcclusionPatch};

use crate::api::{
log, CommonProperties, Interpolatable, Layer, NodeContext, OcclusionLayerGen, RenderContext,
CommonProperties, Interpolatable, Layer, NodeContext, OcclusionLayerGen, RenderContext,
TransitionManager,
};
use crate::math::Point2;
use piet::InterpolationMode;

use crate::declarative_macros::{handle_vtable_update, handle_vtable_update_optional};
use crate::{
Affine, ComponentInstance, ExpressionContext, InstanceNode, RuntimeContext,
ComponentInstance, ExpressionContext, InstanceNode, RuntimeContext,
RuntimePropertiesStackFrame, TransformAndBounds,
};

pub mod node_interface;

/// The atomic unit of rendering; also the container for each unique tuple of computed properties.
/// Represents an expanded node, that is "expanded" in the context of computed properties and repeat expansion.
/// For example, a Rectangle inside `for i in 0..3` and a `for j in 0..4` would have 12 expanded nodes representing the 12 virtual Rectangles in the
Expand Down Expand Up @@ -253,12 +256,14 @@ impl PaxEngine {
logger: crate::api::PlatformSpecificLogger,
viewport_size: (f64, f64),
) -> Self {
use crate::math::Transform2;

crate::api::register_logger(logger);

let globals = Globals {
frames_elapsed: 0,
viewport: TransformAndBounds {
transform: Affine::default(),
transform: Transform2::identity(),
bounds: viewport_size,
},
};
Expand All @@ -281,12 +286,14 @@ impl PaxEngine {
viewport_size: (f64, f64),
designtime: Rc<RefCell<DesigntimeManager>>,
) -> Self {
use crate::math::Transform2;

crate::api::register_logger(logger);

let globals = Globals {
frames_elapsed: 0,
viewport: TransformAndBounds {
transform: Affine::default(),
transform: Transform2::default(),
bounds: viewport_size,
},
designtime: designtime.clone(),
Expand Down Expand Up @@ -388,7 +395,7 @@ impl PaxEngine {
pub fn get_focused_element(&self) -> Option<Rc<ExpandedNode>> {
let (x, y) = self.runtime_context.globals().viewport.bounds;
self.runtime_context
.get_topmost_element_beneath_ray((x / 2.0, y / 2.0))
.get_topmost_element_beneath_ray(Point2::new(x / 2.0, y / 2.0))
}

/// Called by chassis when viewport size changes, e.g. with native window resizes
Expand Down
Loading
Loading