mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Merge pull request #2220 from abarth/add_canvas_transform
Introduce Canvas.transform
This commit is contained in:
commit
ebe379ebcd
@ -217,7 +217,21 @@ class Canvas extends NativeFieldWrapperClass2 {
|
||||
void rotate(double radians) native "Canvas_rotate";
|
||||
void skew(double sx, double sy) native "Canvas_skew";
|
||||
void concat(Float64List matrix4) native "Canvas_concat";
|
||||
void setMatrix(Float64List matrix4) native "Canvas_setMatrix";
|
||||
|
||||
void _transform(Float64List matrix4) native "Canvas_transform";
|
||||
void transform(Float64List matrix4) {
|
||||
if (matrix4.length != 16)
|
||||
throw new ArgumentError("[matrix4] must have 16 entries.");
|
||||
_transform(matrix4);
|
||||
}
|
||||
|
||||
void _setMatrix(Float64List matrix4) native "Canvas_setMatrix";
|
||||
void setMatrix(Float64List matrix4) {
|
||||
if (matrix4.length != 16)
|
||||
throw new ArgumentError("[matrix4] must have 16 entries.");
|
||||
_setMatrix(matrix4);
|
||||
}
|
||||
|
||||
Float64List getTotalMatrix() native "Canvas_getTotalMatrix";
|
||||
void clipRect(Rect rect) native "Canvas_clipRect";
|
||||
void clipRRect(RRect rrect) native "Canvas_clipRRect";
|
||||
|
||||
@ -33,17 +33,6 @@ static void Canvas_concat(Dart_NativeArguments args) {
|
||||
Dart_ThrowException(es.GetDartException(args, true));
|
||||
}
|
||||
|
||||
static void Canvas_setMatrix(Dart_NativeArguments args) {
|
||||
DartArgIterator it(args);
|
||||
Float64List matrix4 = it.GetNext<Float64List>();
|
||||
if (it.had_exception())
|
||||
return;
|
||||
ExceptionState es;
|
||||
GetReceiver<Canvas>(args)->setMatrix(matrix4, es);
|
||||
if (es.had_exception())
|
||||
Dart_ThrowException(es.GetDartException(args, true));
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(Canvas);
|
||||
|
||||
#define FOR_EACH_BINDING(V) \
|
||||
@ -55,6 +44,8 @@ IMPLEMENT_WRAPPERTYPEINFO(Canvas);
|
||||
V(Canvas, scale) \
|
||||
V(Canvas, rotate) \
|
||||
V(Canvas, skew) \
|
||||
V(Canvas, transform) \
|
||||
V(Canvas, setMatrix) \
|
||||
V(Canvas, getTotalMatrix) \
|
||||
V(Canvas, clipRect) \
|
||||
V(Canvas, clipRRect) \
|
||||
@ -77,7 +68,6 @@ IMPLEMENT_WRAPPERTYPEINFO(Canvas);
|
||||
|
||||
// These are custom because of ExceptionState:
|
||||
// V(Canvas, concat)
|
||||
// V(Canvas, setMatrix)
|
||||
|
||||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
@ -85,7 +75,6 @@ void Canvas::RegisterNatives(DartLibraryNatives* natives) {
|
||||
natives->Register({
|
||||
{ "Canvas_constructor", Canvas_constructor, 3, true },
|
||||
{ "Canvas_concat", Canvas_concat, 2, true },
|
||||
{ "Canvas_setMatrix", Canvas_setMatrix, 2, true },
|
||||
FOR_EACH_BINDING(DART_REGISTER_NATIVE)
|
||||
});
|
||||
}
|
||||
@ -165,6 +154,20 @@ void Canvas::skew(float sx, float sy)
|
||||
m_canvas->skew(sx, sy);
|
||||
}
|
||||
|
||||
void Canvas::transform(const Float64List& matrix4)
|
||||
{
|
||||
if (!m_canvas)
|
||||
return;
|
||||
m_canvas->concat(toSkMatrix(matrix4));
|
||||
}
|
||||
|
||||
void Canvas::setMatrix(const Float64List& matrix4)
|
||||
{
|
||||
if (!m_canvas)
|
||||
return;
|
||||
m_canvas->setMatrix(toSkMatrix(matrix4));
|
||||
}
|
||||
|
||||
void Canvas::concat(const Float64List& matrix4, ExceptionState& es)
|
||||
{
|
||||
if (!m_canvas)
|
||||
@ -176,17 +179,6 @@ void Canvas::concat(const Float64List& matrix4, ExceptionState& es)
|
||||
m_canvas->concat(sk_matrix);
|
||||
}
|
||||
|
||||
void Canvas::setMatrix(const Float64List& matrix4, ExceptionState& es)
|
||||
{
|
||||
if (!m_canvas)
|
||||
return es.ThrowTypeError("No canvas");
|
||||
|
||||
SkMatrix sk_matrix = toSkMatrix(matrix4, es);
|
||||
if (es.had_exception())
|
||||
return;
|
||||
m_canvas->setMatrix(sk_matrix);
|
||||
}
|
||||
|
||||
Float64List Canvas::getTotalMatrix()
|
||||
{
|
||||
// Maybe we should throw an exception instead of returning an empty matrix?
|
||||
|
||||
@ -45,9 +45,12 @@ public:
|
||||
void scale(float sx, float sy);
|
||||
void rotate(float radians);
|
||||
void skew(float sx, float sy);
|
||||
void transform(const Float64List& matrix4);
|
||||
void setMatrix(const Float64List& matrix4);
|
||||
|
||||
// TODO(abarth): Remove concat.
|
||||
void concat(const Float64List& matrix4, ExceptionState&);
|
||||
|
||||
void setMatrix(const Float64List& matrix4, ExceptionState&);
|
||||
Float64List getTotalMatrix();
|
||||
|
||||
void clipRect(const Rect& rect);
|
||||
|
||||
@ -22,15 +22,29 @@ SkMatrix toSkMatrix(const Float64List& matrix4, ExceptionState& es)
|
||||
return sk_matrix;
|
||||
}
|
||||
|
||||
for (intptr_t i = 0; i < 9; ++i)
|
||||
for (int i = 0; i < 9; ++i)
|
||||
sk_matrix[i] = matrix4[kSkMatrixIndexToMatrix4Index[i]];
|
||||
return sk_matrix;
|
||||
}
|
||||
|
||||
SkMatrix toSkMatrix(const Float64List& matrix4)
|
||||
{
|
||||
ASSERT(matrix4.data());
|
||||
SkMatrix sk_matrix;
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
int matrix4_index = kSkMatrixIndexToMatrix4Index[i];
|
||||
if (matrix4_index < matrix4.num_elements())
|
||||
sk_matrix[i] = matrix4[matrix4_index];
|
||||
else
|
||||
sk_matrix[i] = 0.0;
|
||||
}
|
||||
return sk_matrix;
|
||||
}
|
||||
|
||||
Float64List toMatrix4(const SkMatrix& sk_matrix)
|
||||
{
|
||||
Float64List matrix4(Dart_NewTypedData(Dart_TypedData_kFloat64, 16));
|
||||
for (intptr_t i = 0; i < 9; ++i)
|
||||
for (int i = 0; i < 9; ++i)
|
||||
matrix4[kSkMatrixIndexToMatrix4Index[i]] = sk_matrix[i];
|
||||
matrix4[10] = 1.0; // Identity along the z axis.
|
||||
return matrix4;
|
||||
|
||||
@ -11,7 +11,11 @@
|
||||
|
||||
namespace blink {
|
||||
|
||||
// TODO(abarth): Remove this version of toSkMatrix and do the error checking
|
||||
// inside the VM.
|
||||
SkMatrix toSkMatrix(const Float64List& matrix4, ExceptionState& es);
|
||||
|
||||
SkMatrix toSkMatrix(const Float64List& matrix4);
|
||||
Float64List toMatrix4(const SkMatrix& sk_matrix);
|
||||
|
||||
} // namespace blink
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user