Skip to content

Commit

Permalink
add live hook
Browse files Browse the repository at this point in the history
  • Loading branch information
syf20020816 committed Jun 13, 2024
1 parent d601d0e commit ba75978
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
- [Redraw](./makepad/syntax/redraw.md)
- [Event](./makepad/event/event.md)
- [MatchEvent](./makepad/event/match_event.md)

- [LiveHook](./makepad/syntax/live_hook.md)

# Makepad Current Issues
---
Expand Down
41 changes: 29 additions & 12 deletions src/makepad/sdf2d/sdf2d.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
##

```rust
/// Sdf2d结构体使用glsl
/// Sdf2d struct use glsl
Sdf2d = struct {
// Fields of the Sdf2d struct
field pos: vec2 // Current position
field result: vec4 // Result color
field last_pos: vec2 // Last position in path
field start_pos: vec2 // Start position in path
field shape: float // Shape distance
field clip: float // Clipping distance
field has_clip: float // Clip flag
field old_shape: float // Previous shape distance
field blur: float // Blur amount
field aa: float // Anti-aliasing factor
field scale_factor: float // Scaling factor
field dist: float // Distance to shape
field pos: vec2 // Current position 当前位置
field result: vec4 // Result color 最终的颜色
field last_pos: vec2 // Last position in path 路径中的最后一个位置
field start_pos: vec2 // Start position in path 路径中的初始的位置
field shape: float // Shape distance 形状
field clip: float // Clipping distance 裁剪长度
field has_clip: float // Clip flag 是否被裁剪(0.0 is false)
field old_shape: float // Previous shape distance 上一个形状
field blur: float // Blur amount 模糊量
field aa: float // Anti-aliasing factor 抗锯齿因子
field scale_factor: float // Scaling factor 比例因子
field dist: float // Distance to shape 到形状的距离

// Calculate anti-aliasing factor based on the derivative of the position
// 根据位置的导数计算抗锯齿因子
fn antialias(p: vec2) -> float {
return 1.0 / length(vec2(length(dFdx(p)), length(dFdy(p))));
}

// Initialize a new Sdf2d struct with the given position
// 使用给定位置初始化新的Sdf2d结构
fn viewport(pos: vec2) -> Self {
return Self {
pos: pos
Expand All @@ -44,12 +48,14 @@ Sdf2d = struct {
}

// Translate the current position by (x, y)
// 将当前位置平移(x,y)
fn translate(inout self, x: float, y: float) -> vec2 {
self.pos -= vec2(x, y);
return self.pos;
}

// Rotate the current position around (x, y) by angle a
// 将当前位置绕(x,y)旋转角度a
fn rotate(inout self, a: float, x: float, y: float) {
let ca = cos(-a);
let sa = sin(-a);
Expand All @@ -58,17 +64,20 @@ Sdf2d = struct {
}

// Scale the current position relative to (x, y) by factor f
// 按因子f相对于(x,y)缩放当前位置
fn scale(inout self, f: float, x: float, y: float) {
self.scale_factor *= f;
self.pos = (self.pos - vec2(x, y)) * f + vec2(x, y);
}

// Clear the result color with the given color
// 使用给定的颜色清除结果颜色
fn clear(inout self, color: vec4) {
self.result = vec4(color.rgb * color.a + self.result.rgb * (1.0 - color.a), color.a);
}

// Calculate blur based on width w
// 根据宽度w计算模糊
fn calc_blur(inout self, w: float) -> float {
let wa = clamp(-w * self.aa, 0.0, 1.0);
let wb = 1.0;
Expand All @@ -79,6 +88,7 @@ Sdf2d = struct {
}

// Fill the current shape with premultiplied alpha, keeping the previous fill
// 使用预乘的alpha填充当前形状,保持先前的填充
fn fill_keep_premul(inout self, source: vec4) -> vec4 {
let f = self.calc_blur(self.shape);
self.result = source * f + self.result * (1. - source.a * f);
Expand All @@ -90,6 +100,7 @@ Sdf2d = struct {
}

// Fill the current shape with premultiplied alpha and reset the shape
// 用预乘的alpha填充当前形状并重置形状
fn fill_premul(inout self, color: vec4) -> vec4 {
self.fill_keep_premul(color);
self.old_shape = self.shape = 1e+20;
Expand All @@ -99,16 +110,19 @@ Sdf2d = struct {
}

// Fill the current shape, keeping the previous fill
// 填充当前形状,保留以前的填充
fn fill_keep(inout self, color: vec4) -> vec4 {
return self.fill_keep_premul(vec4(color.rgb * color.a, color.a));
}

// Fill the current shape and reset the shape
// 填充当前形状并重置形状
fn fill(inout self, color: vec4) -> vec4 {
return self.fill_premul(vec4(color.rgb * color.a, color.a));
}

// Stroke the current shape, keeping the previous stroke
// 笔划当前形状,保持上一个笔划
fn stroke_keep(inout self, color: vec4, width: float) -> vec4 {
let f = self.calc_blur(abs(self.shape) - width / self.scale_factor);
let source = vec4(color.rgb * color.a, color.a);
Expand All @@ -118,6 +132,7 @@ Sdf2d = struct {
}

// Stroke the current shape and reset the shape
// 笔划当前形状并重置形状
fn stroke(inout self, color: vec4, width: float) -> vec4 {
self.stroke_keep(color, width);
self.old_shape = self.shape = 1e+20;
Expand Down Expand Up @@ -145,6 +160,7 @@ Sdf2d = struct {
}

// Union the current shape with the previous shape
// 将当前形状与上一个形状合并
fn union(inout self) {
self.old_shape = self.shape = min(self.dist, self.old_shape);
}
Expand Down Expand Up @@ -200,6 +216,7 @@ Sdf2d = struct {
}

// Draw a rounded box at (x, y) with width w, height h, and radius r
// 在(x,y)处绘制一个宽度为w、高度为h、半径为r的圆角框
fn box(inout self, x: float, y: float, w: float, h: float, r: float) {
let p = self.pos - vec2(x, y);
let size = vec2(0.5 * w, 0.5 * h);
Expand Down
111 changes: 111 additions & 0 deletions src/makepad/syntax/live_hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# LiveHook

`LiveHook` 是一个 trait,用于在 Makepad 的 live design 系统中处理和应用 live 属性更新。每个方法的用途如下:

### `apply_value_unknown`
```rust
fn apply_value_unknown(&mut self, cx: &mut Cx, _apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize
```
- **用途**:当一个属性更新到达时,如果该属性未在该对象上找到,`apply_value_unknown` 被调用。
- **典型实现**:如果节点没有前缀,则生成一个错误并跳过该节点。
- **参数**
- `cx`: 上下文对象,用于处理各种操作。
- `_apply`: 当前的应用状态。
- `index`: 当前处理节点的索引。
- `nodes`: 节点数组。

### `apply_value_instance`
```rust
fn apply_value_instance(&mut self, _cx: &mut Cx, _apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize
```
- **用途**:处理实例属性的应用。
- **典型实现**:通常跳过该节点。
- **参数**
- `_cx`: 上下文对象。
- `_apply`: 当前的应用状态。
- `index`: 当前处理节点的索引。
- `nodes`: 节点数组。

### `skip_apply`
```rust
fn skip_apply(&mut self, _cx: &mut Cx, _apply: &mut Apply, _index: usize, _nodes: &[LiveNode]) -> Option<usize>
```
- **用途**:在应用属性更新之前调用,以确定是否应跳过应用。
- **返回值**:`Option<usize>` 指定要跳过的节点索引,如果不跳过则返回 `None`。
- **参数**
- `_cx`: 上下文对象。
- `_apply`: 当前的应用状态。
- `_index`: 当前处理节点的索引。
- `_nodes`: 节点数组。

### `before_apply`
```rust
fn before_apply(&mut self, _cx: &mut Cx, _apply: &mut Apply, _index: usize, _nodes: &[LiveNode])
```
- **用途**:在应用属性更新之前调用。
- **典型实现**:在应用属性更新之前执行一些准备工作。
- **参数**
- `_cx`: 上下文对象。
- `_apply`: 当前的应用状态。
- `_index`: 当前处理节点的索引。
- `_nodes`: 节点数组。

### `after_apply`
```rust
fn after_apply(&mut self, _cx: &mut Cx, _apply: &mut Apply, _index: usize, _nodes: &[LiveNode])
```
- **用途**:在应用属性更新之后调用。
- **典型实现**:在应用属性更新之后执行一些清理或后续操作。
- **参数**
- `_cx`: 上下文对象。
- `_apply`: 当前的应用状态。
- `_index`: 当前处理节点的索引。
- `_nodes`: 节点数组。

### `after_apply_from`
```rust
fn after_apply_from(&mut self, cx: &mut Cx, apply: &mut Apply)
```
- **用途**:在 `apply` 的特定来源完成后调用。
- **典型实现**:根据应用的来源,调用相应的后续操作方法。
- **参数**
- `cx`: 上下文对象。
- `apply`: 当前的应用状态。

### `after_new_from_doc`
```rust
fn after_new_from_doc(&mut self, _cx: &mut Cx)
```
- **用途**:在文档中创建新的实例后调用。
- **典型实现**:在从文档创建新实例后执行初始化操作。
- **参数**
- `_cx`: 上下文对象。

### `after_update_from_doc`
```rust
fn after_update_from_doc(&mut self, _cx: &mut Cx)
```
- **用途**:在文档更新后调用。
- **典型实现**:在从文档更新实例后执行更新操作。
- **参数**
- `_cx`: 上下文对象。

### `after_apply_from_doc`
```rust
fn after_apply_from_doc(&mut self, _cx: &mut Cx)
```
- **用途**:在应用任何文档更新后调用。
- **典型实现**:在任何文档更新应用之后执行操作。
- **参数**
- `_cx`: 上下文对象。

### `after_new_before_apply`
```rust
fn after_new_before_apply(&mut self, _cx: &mut Cx)
```
- **用途**:在应用新文档更新之前调用。
- **典型实现**:在应用新文档更新之前执行一些初始化操作。
- **参数**
- `_cx`: 上下文对象。

这些方法提供了一个钩子系统,使开发者可以在特定的生命周期阶段处理属性更新,以便灵活地控制和扩展 live 属性的行为。

0 comments on commit ba75978

Please sign in to comment.