Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into kurt-circle-rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Irev-Dev committed Sep 19, 2024
2 parents 006692d + d1f9a02 commit 85a9322
Show file tree
Hide file tree
Showing 29 changed files with 239 additions and 124 deletions.
42 changes: 33 additions & 9 deletions src/clientSideScene/CameraControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,19 @@ export class CameraControls {
}

onMouseWheel = (event: WheelEvent) => {
const interaction = this.getInteractionType(event)
if (interaction === 'none') return
event.preventDefault()

if (this.syncDirection === 'engineToClient') {
this.zoomDataFromLastFrame = event.deltaY
if (interaction === 'zoom') {
this.zoomDataFromLastFrame = event.deltaY
} else {
// This case will get handled when we add pan and rotate using Apple trackpad.
console.error(
`Unexpected interaction type for engineToClient wheel event: ${interaction}`
)
}
return
}

Expand All @@ -455,8 +466,16 @@ export class CameraControls {
// zoom commands to engine. This means dropping some zoom
// commands too.
// From onMouseMove zoom handling which seems to be really smooth

this.handleStart()
this.pendingZoom = 1 + (event.deltaY / window.devicePixelRatio) * 0.001
if (interaction === 'zoom') {
this.pendingZoom = 1 + (event.deltaY / window.devicePixelRatio) * 0.001
} else {
// This case will get handled when we add pan and rotate using Apple trackpad.
console.error(
`Unexpected interaction type for wheel event: ${interaction}`
)
}
this.handleEnd()
}

Expand Down Expand Up @@ -1123,7 +1142,7 @@ export class CameraControls {
this.deferReactUpdate(this.reactCameraProperties)
Object.values(this._camChangeCallbacks).forEach((cb) => cb())
}
getInteractionType = (event: any) =>
getInteractionType = (event: MouseEvent) =>
_getInteractionType(
this.interactionGuards,
event,
Expand Down Expand Up @@ -1231,16 +1250,21 @@ function _lookAt(position: Vector3, target: Vector3, up: Vector3): Quaternion {

function _getInteractionType(
interactionGuards: MouseGuard,
event: any,
event: MouseEvent | WheelEvent,
enablePan: boolean,
enableRotate: boolean,
enableZoom: boolean
): interactionType | 'none' {
let state: interactionType | 'none' = 'none'
if (enablePan && interactionGuards.pan.callback(event)) return 'pan'
if (enableRotate && interactionGuards.rotate.callback(event)) return 'rotate'
if (enableZoom && interactionGuards.zoom.dragCallback(event)) return 'zoom'
return state
if (event instanceof WheelEvent) {
if (enableZoom && interactionGuards.zoom.scrollCallback(event))
return 'zoom'
} else {
if (enablePan && interactionGuards.pan.callback(event)) return 'pan'
if (enableRotate && interactionGuards.rotate.callback(event))
return 'rotate'
if (enableZoom && interactionGuards.zoom.dragCallback(event)) return 'zoom'
}
return 'none'
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/components/Stream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export const Stream = () => {
if (state.matches('Sketch')) return
if (state.matches({ idle: 'showPlanes' })) return

if (btnName(e).left) {
if (btnName(e.nativeEvent).left) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
sendSelectEventToEngine(e, videoRef.current)
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib/cameraControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const META =
PLATFORM === 'macos' ? 'Cmd' : PLATFORM === 'windows' ? 'Win' : 'Super'
const ALT = PLATFORM === 'macos' ? 'Option' : 'Alt'

const noModifiersPressed = (e: React.MouseEvent) =>
const noModifiersPressed = (e: MouseEvent) =>
!e.ctrlKey && !e.shiftKey && !e.altKey && !e.metaKey

export type CameraSystem =
Expand Down Expand Up @@ -53,14 +53,14 @@ export function mouseControlsToCameraSystem(

interface MouseGuardHandler {
description: string
callback: (e: React.MouseEvent) => boolean
callback: (e: MouseEvent) => boolean
lenientDragStartButton?: number
}

interface MouseGuardZoomHandler {
description: string
dragCallback: (e: React.MouseEvent) => boolean
scrollCallback: (e: React.MouseEvent) => boolean
dragCallback: (e: MouseEvent) => boolean
scrollCallback: (e: WheelEvent) => boolean
lenientDragStartButton?: number
}

Expand All @@ -70,7 +70,7 @@ export interface MouseGuard {
rotate: MouseGuardHandler
}

export const btnName = (e: React.MouseEvent) => ({
export const btnName = (e: MouseEvent) => ({
middle: !!(e.buttons & 4) || e.button === 1,
right: !!(e.buttons & 2) || e.button === 2,
left: !!(e.buttons & 1) || e.button === 0,
Expand Down
16 changes: 9 additions & 7 deletions src/wasm-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/wasm-lib/kcl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ git_rev = "0.1.0"
gltf-json = "1.4.1"
http = { workspace = true }
image = { version = "0.25.1", default-features = false, features = ["png"] }
indexmap = { version = "2.5.0", features = ["serde"] }
kittycad = { workspace = true }
kittycad-modeling-cmds = { workspace = true }
lazy_static = "1.5.0"
Expand Down
9 changes: 4 additions & 5 deletions src/wasm-lib/kcl/src/ast/modify.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::sync::Arc;

use kcmc::each_cmd as mcmd;
use kcmc::ok_response::OkModelingCmdResponse;
use kcmc::shared::PathCommand;
use kcmc::websocket::OkWebSocketResponseData;
use kcmc::ModelingCmd;
use kcmc::{
each_cmd as mcmd, ok_response::OkModelingCmdResponse, shared::PathCommand, websocket::OkWebSocketResponseData,
ModelingCmd,
};
use kittycad_modeling_cmds as kcmc;

use crate::{
Expand Down
22 changes: 11 additions & 11 deletions src/wasm-lib/kcl/src/engine/conn.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//! Functions for setting up our WebSocket and WebRTC connections for communications with the
//! engine.

use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use std::sync::{Arc, Mutex};

use anyhow::{anyhow, Result};
use dashmap::DashMap;
use futures::{SinkExt, StreamExt};
use kcmc::websocket::{
BatchResponse, FailureWebSocketResponse, ModelingCmdReq, ModelingSessionData, OkWebSocketResponseData,
SuccessWebSocketResponse, WebSocketRequest, WebSocketResponse,
use indexmap::IndexMap;
use kcmc::{
websocket::{
BatchResponse, FailureWebSocketResponse, ModelingCmdReq, ModelingSessionData, OkWebSocketResponseData,
SuccessWebSocketResponse, WebSocketRequest, WebSocketResponse,
},
ModelingCmd,
};
use kcmc::ModelingCmd;
use kittycad_modeling_cmds as kcmc;
use tokio::sync::{mpsc, oneshot, RwLock};
use tokio_tungstenite::tungstenite::Message as WsMsg;
Expand All @@ -39,7 +39,7 @@ pub struct EngineConnection {
tcp_read_handle: Arc<TcpReadHandle>,
socket_health: Arc<Mutex<SocketHealth>>,
batch: Arc<Mutex<Vec<(WebSocketRequest, crate::executor::SourceRange)>>>,
batch_end: Arc<Mutex<HashMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>>,
batch_end: Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>>,

/// The default planes for the scene.
default_planes: Arc<RwLock<Option<DefaultPlanes>>>,
Expand Down Expand Up @@ -269,7 +269,7 @@ impl EngineConnection {
responses,
socket_health,
batch: Arc::new(Mutex::new(Vec::new())),
batch_end: Arc::new(Mutex::new(HashMap::new())),
batch_end: Arc::new(Mutex::new(IndexMap::new())),
default_planes: Default::default(),
session_data,
})
Expand All @@ -282,7 +282,7 @@ impl EngineManager for EngineConnection {
self.batch.clone()
}

fn batch_end(&self) -> Arc<Mutex<HashMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>> {
fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>> {
self.batch_end.clone()
}

Expand Down
17 changes: 10 additions & 7 deletions src/wasm-lib/kcl/src/engine/conn_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ use std::{
};

use anyhow::Result;
use kcmc::ok_response::OkModelingCmdResponse;
use kcmc::websocket::{
BatchResponse, ModelingBatch, OkWebSocketResponseData, SuccessWebSocketResponse, WebSocketRequest,
WebSocketResponse,
use indexmap::IndexMap;
use kcmc::{
ok_response::OkModelingCmdResponse,
websocket::{
BatchResponse, ModelingBatch, OkWebSocketResponseData, SuccessWebSocketResponse, WebSocketRequest,
WebSocketResponse,
},
};
use kittycad_modeling_cmds::{self as kcmc};

Expand All @@ -19,14 +22,14 @@ use crate::{errors::KclError, executor::DefaultPlanes};
#[derive(Debug, Clone)]
pub struct EngineConnection {
batch: Arc<Mutex<Vec<(WebSocketRequest, crate::executor::SourceRange)>>>,
batch_end: Arc<Mutex<HashMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>>,
batch_end: Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>>,
}

impl EngineConnection {
pub async fn new() -> Result<EngineConnection> {
Ok(EngineConnection {
batch: Arc::new(Mutex::new(Vec::new())),
batch_end: Arc::new(Mutex::new(HashMap::new())),
batch_end: Arc::new(Mutex::new(IndexMap::new())),
})
}
}
Expand All @@ -37,7 +40,7 @@ impl crate::engine::EngineManager for EngineConnection {
self.batch.clone()
}

fn batch_end(&self) -> Arc<Mutex<HashMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>> {
fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>> {
self.batch_end.clone()
}

Expand Down
7 changes: 4 additions & 3 deletions src/wasm-lib/kcl/src/engine/conn_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
};

use anyhow::Result;
use indexmap::IndexMap;
use kcmc::websocket::{WebSocketRequest, WebSocketResponse};
use kittycad_modeling_cmds as kcmc;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -43,7 +44,7 @@ extern "C" {
pub struct EngineConnection {
manager: Arc<EngineCommandManager>,
batch: Arc<Mutex<Vec<(WebSocketRequest, crate::executor::SourceRange)>>>,
batch_end: Arc<Mutex<HashMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>>,
batch_end: Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>>,
}

// Safety: WebAssembly will only ever run in a single-threaded context.
Expand All @@ -55,7 +56,7 @@ impl EngineConnection {
Ok(EngineConnection {
manager: Arc::new(manager),
batch: Arc::new(Mutex::new(Vec::new())),
batch_end: Arc::new(Mutex::new(HashMap::new())),
batch_end: Arc::new(Mutex::new(IndexMap::new())),
})
}
}
Expand All @@ -66,7 +67,7 @@ impl crate::engine::EngineManager for EngineConnection {
self.batch.clone()
}

fn batch_end(&self) -> Arc<Mutex<HashMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>> {
fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>> {
self.batch_end.clone()
}

Expand Down
21 changes: 12 additions & 9 deletions src/wasm-lib/kcl/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ use std::{
sync::{Arc, Mutex},
};

use kcmc::each_cmd as mcmd;
use kcmc::length_unit::LengthUnit;
use kcmc::ok_response::OkModelingCmdResponse;
use kcmc::shared::Color;
use kcmc::websocket::ModelingBatch;
use kcmc::websocket::{
BatchResponse, ModelingCmdReq, ModelingSessionData, OkWebSocketResponseData, WebSocketRequest, WebSocketResponse,
use indexmap::IndexMap;
use kcmc::{
each_cmd as mcmd,
length_unit::LengthUnit,
ok_response::OkModelingCmdResponse,
shared::Color,
websocket::{
BatchResponse, ModelingBatch, ModelingCmdReq, ModelingSessionData, OkWebSocketResponseData, WebSocketRequest,
WebSocketResponse,
},
ModelingCmd,
};
use kcmc::ModelingCmd;
use kittycad_modeling_cmds as kcmc;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -44,7 +47,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
fn batch(&self) -> Arc<Mutex<Vec<(WebSocketRequest, crate::executor::SourceRange)>>>;

/// Get the batch of end commands to be sent to the engine.
fn batch_end(&self) -> Arc<Mutex<HashMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>>;
fn batch_end(&self) -> Arc<Mutex<IndexMap<uuid::Uuid, (WebSocketRequest, crate::executor::SourceRange)>>>;

/// Get the default planes.
async fn default_planes(
Expand Down
10 changes: 6 additions & 4 deletions src/wasm-lib/kcl/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::{collections::HashMap, sync::Arc};

use anyhow::Result;
use async_recursion::async_recursion;
use kcmc::each_cmd as mcmd;
use kcmc::ok_response::{output::TakeSnapshot, OkModelingCmdResponse};
use kcmc::websocket::{ModelingSessionData, OkWebSocketResponseData};
use kcmc::{ImageFormat, ModelingCmd};
use kcmc::{
each_cmd as mcmd,
ok_response::{output::TakeSnapshot, OkModelingCmdResponse},
websocket::{ModelingSessionData, OkWebSocketResponseData},
ImageFormat, ModelingCmd,
};
use kittycad_modeling_cmds as kcmc;
use kittycad_modeling_cmds::length_unit::LengthUnit;
use parse_display::{Display, FromStr};
Expand Down
Loading

0 comments on commit 85a9322

Please sign in to comment.