Убраны лишние комментарии
This commit is contained in:
@@ -7,14 +7,14 @@ const Scale2_f = basic_models.Scale2_f;
|
||||
const Rect_i = basic_models.Rect_i;
|
||||
const Color = dvui.Color;
|
||||
|
||||
/// Трансформ объекта в мировых координатах документа (позиция, угол, масштаб, непрозрачность).
|
||||
/// Трансформ объекта: позиция, угол, масштаб, непрозрачность.
|
||||
pub const Transform = struct {
|
||||
position: Point2_f = .{},
|
||||
angle: f32 = 0,
|
||||
scale: Scale2_f = .{},
|
||||
opacity: f32 = 1.0,
|
||||
|
||||
/// Композиция: мировой трансформ = parent * local (local в пространстве родителя).
|
||||
/// Композиция: world = parent * local.
|
||||
pub fn compose(parent: Transform, local: Transform) Transform {
|
||||
const cos_a = std.math.cos(parent.angle);
|
||||
const sin_a = std.math.sin(parent.angle);
|
||||
@@ -36,8 +36,7 @@ pub const Transform = struct {
|
||||
}
|
||||
};
|
||||
|
||||
/// Единый конвейер: принимает позицию в локальных координатах фигуры и цвет пикселя,
|
||||
/// применяет трансформ (вращение, масштаб, перенос) и непрозрачность, накладывает на буфер.
|
||||
/// Конвейер отрисовки: локальные координаты -> трансформ -> буфер.
|
||||
pub const DrawContext = struct {
|
||||
pixels: []Color.PMA,
|
||||
buf_width: u32,
|
||||
@@ -51,7 +50,7 @@ pub const DrawContext = struct {
|
||||
self.transform = t;
|
||||
}
|
||||
|
||||
/// Локальные координаты фигуры -> мировые (документ).
|
||||
/// Локальные -> мировые.
|
||||
pub fn localToWorld(self: *const DrawContext, local_x: f32, local_y: f32) Point2_f {
|
||||
const t = &self.transform;
|
||||
const cos_a = std.math.cos(t.angle);
|
||||
@@ -62,7 +61,7 @@ pub const DrawContext = struct {
|
||||
};
|
||||
}
|
||||
|
||||
/// Мировые координаты документа -> координаты в буфере (float; могут быть вне [0, buf_w] x [0, buf_h]).
|
||||
/// Мировые -> буфер (float).
|
||||
pub fn worldToBufferF(self: *const DrawContext, wx: f32, wy: f32) Point2_f {
|
||||
const canvas_x = wx * self.scale_x;
|
||||
const canvas_y = wy * self.scale_y;
|
||||
@@ -74,7 +73,7 @@ pub const DrawContext = struct {
|
||||
};
|
||||
}
|
||||
|
||||
/// Мировые координаты документа -> целочисленные координаты в буфере (округление до ближайшего пикселя).
|
||||
/// Мировые -> буфер (целые).
|
||||
pub fn worldToBuffer(self: *const DrawContext, wx: f32, wy: f32) Point2_i {
|
||||
const b = self.worldToBufferF(wx, wy);
|
||||
return .{
|
||||
@@ -83,7 +82,7 @@ pub const DrawContext = struct {
|
||||
};
|
||||
}
|
||||
|
||||
/// Координаты буфера -> мировые (документ). scale_x/scale_y не должны быть 0.
|
||||
/// Буфер -> мировые.
|
||||
pub fn bufferToWorld(self: *const DrawContext, buf_x: f32, buf_y: f32) Point2_f {
|
||||
const vx = @as(f32, @floatFromInt(self.visible_rect.x));
|
||||
const vy = @as(f32, @floatFromInt(self.visible_rect.y));
|
||||
@@ -97,7 +96,7 @@ pub const DrawContext = struct {
|
||||
};
|
||||
}
|
||||
|
||||
/// Мировые координаты -> локальные фигуры (обратное к localToWorld).
|
||||
/// Мировые -> локальные.
|
||||
pub fn worldToLocal(self: *const DrawContext, wx: f32, wy: f32) Point2_f {
|
||||
const t = &self.transform;
|
||||
const dx = wx - t.position.x;
|
||||
@@ -112,14 +111,13 @@ pub const DrawContext = struct {
|
||||
};
|
||||
}
|
||||
|
||||
/// Смешивает цвет в пикселе буфера (bx, by). color уже в PMA; opacity трансформа применяется к альфе и к RGB.
|
||||
/// Смешивает цвет в пикселе буфера с учётом opacity трансформа.
|
||||
pub fn blendPixelAtBuffer(self: *DrawContext, bx: u32, by: u32, color: Color.PMA) void {
|
||||
if (bx >= self.buf_width or by >= self.buf_height) return;
|
||||
const t = &self.transform;
|
||||
const idx = by * self.buf_width + bx;
|
||||
const dst = &self.pixels[idx];
|
||||
const a = @as(f32, @floatFromInt(color.a)) / 255.0 * t.opacity;
|
||||
// PMA: color.r/g/b уже помножены на color.a; при opacity только масштабируем их на t.opacity
|
||||
const src_r = @as(f32, @floatFromInt(color.r)) * t.opacity;
|
||||
const src_g = @as(f32, @floatFromInt(color.g)) * t.opacity;
|
||||
const src_b = @as(f32, @floatFromInt(color.b)) * t.opacity;
|
||||
@@ -130,7 +128,7 @@ pub const DrawContext = struct {
|
||||
dst.a = @intFromFloat(std.math.clamp(a * 255 + inv_a * @as(f32, @floatFromInt(dst.a)), 0, 255));
|
||||
}
|
||||
|
||||
/// Записывает пиксель в локальных координатах фигуры с учётом трансформа и прозрачности (PMA blend).
|
||||
/// Пиксель в локальных координатах (трансформ + PMA).
|
||||
pub fn blendPixelLocal(self: *DrawContext, local_x: f32, local_y: f32, color: Color.PMA) void {
|
||||
const w = self.localToWorld(local_x, local_y);
|
||||
const b = self.worldToBufferF(w.x, w.y);
|
||||
@@ -143,7 +141,7 @@ pub const DrawContext = struct {
|
||||
}
|
||||
};
|
||||
|
||||
/// u32 в порядке 0xRRGGBBAA: старший байт R, младший A (чёрный = 0x000000FF).
|
||||
/// Конвертирует u32 0xRRGGBBAA в Color.PMA.
|
||||
pub fn rgbaToPma(rgba: u32) Color.PMA {
|
||||
const r: u8 = @intCast((rgba >> 24) & 0xFF);
|
||||
const g: u8 = @intCast((rgba >> 16) & 0xFF);
|
||||
|
||||
Reference in New Issue
Block a user