added new constructor for bbox.
[swftools.git] / lib / python / primitives.c
index 9eae2de..e70f28d 100644 (file)
@@ -115,19 +115,34 @@ typedef struct {
     PyObject_HEAD
     SRECT bbox;
 } BBoxObject;
+//void swf_ExpandRect(SRECT*src, SPOINT add);
+//void swf_ExpandRect2(SRECT*src, SRECT*add);
 
 PyObject* f_BBox(PyObject* self, PyObject* args, PyObject* kwargs)
 {
     static char *kwlist[] = {"xmin", "ymin", "xmax", "ymax", NULL};
     BBoxObject* bbox;
+    float xmin,ymin,xmax,ymax;
+    if(!kwargs) {
+       if (!PyArg_ParseTuple(args, "ffff", &xmin, &ymin, &xmax, &ymax))
+           return NULL;
+    } else {
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ffff", kwlist, &xmin, &ymin, &xmax, &ymax))
+           return NULL;
+    }
     SRECT box;
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiii", kwlist, 
-               &box.xmin,
-               &box.ymin,
-               &box.xmax,
-               &box.ymax));
-       return NULL;
-    mylog("+%08x(%d) bbox_new(%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, box.xmin, box.ymin, box.xmax,box.ymax);
+    box.xmin = (int)(xmin*20);
+    box.ymin = (int)(ymin*20);
+    box.xmax = (int)(xmax*20);
+    box.ymax = (int)(ymax*20);
+    mylog("+%08x(%d) bbox_new(%d,%d,%d,%d)\n", (int)self, self?self->ob_refcnt:0, box.xmin, box.ymin, box.xmax,box.ymax);
+    bbox = PyObject_New(BBoxObject, &BBoxClass);
+    bbox->bbox = box;
+    return (PyObject*)bbox;
+}
+PyObject* f_BBox2(SRECT box)
+{
+    BBoxObject* bbox;
     bbox = PyObject_New(BBoxObject, &BBoxClass);
     bbox->bbox = box;
     return (PyObject*)bbox;
@@ -136,13 +151,13 @@ static PyObject* bbox_getattr(PyObject * self, char* a)
 {
     BBoxObject*bbox = (BBoxObject*)self;
     if(!strcmp(a, "xmin")) {
-       return Py_BuildValue("i", bbox->bbox.xmin);
+       return Py_BuildValue("f", bbox->bbox.xmin/20.0);
     } else if(!strcmp(a, "ymin")) {
-       return Py_BuildValue("i", bbox->bbox.ymin);
+       return Py_BuildValue("f", bbox->bbox.ymin/20.0);
     } else if(!strcmp(a, "xmax")) {
-       return Py_BuildValue("i", bbox->bbox.xmax);
+       return Py_BuildValue("f", bbox->bbox.xmax/20.0);
     } else if(!strcmp(a, "ymax")) {
-       return Py_BuildValue("i", bbox->bbox.ymax);
+       return Py_BuildValue("f", bbox->bbox.ymax/20.0);
     }
     return NULL;
 }
@@ -150,16 +165,24 @@ static int bbox_setattr(PyObject * self, char* a, PyObject* o)
 {
     BBoxObject*bbox= (BBoxObject*)self;
     if(!strcmp(a, "xmin")) {
-       if (!PyArg_Parse(o, "i", &bbox->bbox.xmin)) goto err;
+       float xmin;
+       if (!PyArg_Parse(o, "i", &xmin)) goto err;
+       bbox->bbox.xmin = (int)(xmin*20);
        return 0;
     } else if(!strcmp(a, "ymin")) {
-       if (!PyArg_Parse(o, "i", &bbox->bbox.ymin)) goto err;
+       float ymin;
+       if (!PyArg_Parse(o, "i", &ymin)) goto err;
+       bbox->bbox.ymin = (int)(ymin*20);
        return 0;
     } else if(!strcmp(a, "xmax")) {
-       if (!PyArg_Parse(o, "i", &bbox->bbox.xmax)) goto err;
+       float xmax;
+       if (!PyArg_Parse(o, "i", &xmax)) goto err;
+       bbox->bbox.xmax = (int)(xmax*20);
        return 0;
     } else if(!strcmp(a, "ymax")) {
-       if (!PyArg_Parse(o, "i", &bbox->bbox.ymax)) goto err;
+       float ymax;
+       if (!PyArg_Parse(o, "i", &ymax)) goto err;
+       bbox->bbox.ymax = (int)(ymax*20);
        return 0;
     } 
 err:
@@ -171,7 +194,7 @@ void bbox_dealloc(PyObject* self)
     mylog("-%08x(%d) bbox_dealloc\n", (int)self, self->ob_refcnt);
     PyObject_Del(self);
 }
-SRECT bbox_getBBox(PyObject*self)
+SRECT bbox_getSRECT(PyObject*self)
 {
     BBoxObject*bbox= 0;
     if (!PyArg_Parse(self, "O!", &BBoxClass, &bbox)) {
@@ -206,16 +229,34 @@ PyObject* f_Matrix(PyObject* _self, PyObject* args, PyObject* kwargs)
     PyObject*self = (PyObject*)PyObject_New(MatrixObject, &MatrixClass);
     MatrixObject*matrix = (MatrixObject*)self;
     mylog("+%08x(%d) f_Matrix", self, self->ob_refcnt);
-    static char *kwlist[] = {"x", "y", "scale", "rotate", NULL};
-    float x=0,y=0,scale=1.0,rotate=0;
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ffff", kwlist, &x,&y,&scale,&rotate))
+    static char *kwlist[] = {"x", "y", "scale", "rotate", "pivotx", "pivoty", NULL};
+    float x=0,y=0,scale=1.0,rotate=0,pivotx,pivoty;
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ffffff", kwlist, &x,&y,&scale,&rotate,&pivotx,&pivoty))
        return NULL;
     mylog(" %08x(%d) f_Matrix: x=%f y=%f scale=%f rotate=%f", self, self->ob_refcnt, x,y,scale,rotate);
     swf_GetMatrix(0, &matrix->matrix);
+
     matrix->matrix.tx = (int)(x*20);
     matrix->matrix.ty = (int)(y*20);
-    matrix->matrix.sx = (int)(scale*65536);
-    matrix->matrix.sy = (int)(scale*65536);
+
+    if(!rotate) {
+       matrix->matrix.sx = (int)(scale*65536);
+       matrix->matrix.sy = (int)(scale*65536);
+    } else {
+       matrix->matrix.sx = (int)(scale*cos(rotate)*65536);
+       matrix->matrix.sy = (int)(scale*cos(rotate)*65536);
+       matrix->matrix.r0 = (int)(scale*sin(rotate)*65536);
+       matrix->matrix.r1 = (int)(-scale*sin(rotate)*65536);
+    }
+    if(pivotx || pivoty) {
+       SPOINT p,d;
+       p.x = (int)(pivotx*20);
+       p.y = (int)(pivoty*20);
+       p = swf_TurnPoint(p, &matrix->matrix);
+       matrix->matrix.tx += matrix->matrix.tx-p.x;
+       matrix->matrix.ty += matrix->matrix.ty-p.y;
+    }
+
     /* TODO: rotate */
     return self;
 }
@@ -241,6 +282,8 @@ void matrix_dealloc(PyObject* self)
     mylog("-%08x(%d) matrix_dealloc", self, self->ob_refcnt);
     PyObject_Del(self);
 }
+//SPOINT swf_TurnPoint(SPOINT p, MATRIX* m);
+//SRECT swf_TurnRect(SRECT r, MATRIX* m);
 PyTypeObject MatrixClass = 
 {
     PyObject_HEAD_INIT(NULL)