diff --git a/classes/common.h b/classes/common.h new file mode 100644 index 0000000..14438d6 --- /dev/null +++ b/classes/common.h @@ -0,0 +1,123 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2016 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: krakjoe | + +----------------------------------------------------------------------+ +*/ +#ifndef HAVE_PHP_UI_COMMON_H +#define HAVE_PHP_UI_COMMON_H + +#include +#include + +static inline void php_ui_point_size_add(php_ui_point_t *result, php_ui_point_t *point, double x, double y) { + result->x = point->x + x; + result->y = point->y + y; +} + +static inline void php_ui_point_size_assign_add(php_ui_point_t*result, double x, double y) { + result->x += x; + result->y += y; +} + +static inline void php_ui_point_size_mul(php_ui_point_t *result, php_ui_point_t *point, double x, double y) { + result->x = point->x * x; + result->y = point->y * y; +} + +static inline void php_ui_point_size_assign_mul(php_ui_point_t*result, double x, double y) { + result->x *= x; + result->y *= y; +} + +static inline void php_ui_point_size_div(php_ui_point_t *result, php_ui_point_t *point, double x, double y) { + result->x = point->x / x; + result->y = point->y / y; +} + +static inline void php_ui_point_size_assign_div(php_ui_point_t*result, double x, double y) { + result->x /= x; + result->y /= y; +} + +static inline void php_ui_point_size_sub(php_ui_point_t *result, php_ui_point_t *point, double x, double y) { + result->x = point->x - x; + result->y = point->y - y; +} + +static inline void php_ui_point_size_assign_sub(php_ui_point_t*result, double x, double y) { + result->x -= x; + result->y -= y; +} + +/* {{{ */ +static inline int php_ui_point_size_operation(zend_uchar opcode, zval *retval, zval *op1, zval *op2, zend_class_entry *target) { + zval *l, *r; + double x = 0, y = 0; + php_ui_point_t *point, *result; + zend_bool assign = 0; + + if (retval == op1) { + assign = 1; + } else { + object_init_ex(retval, target); + } + + if (Z_TYPE_P(op1) == IS_OBJECT && Z_TYPE_P(op2) == IS_OBJECT) { + if ((instanceof_function(Z_OBJCE_P(op1), uiSize_ce) || instanceof_function(Z_OBJCE_P(op1), uiPoint_ce)) && + (instanceof_function(Z_OBJCE_P(op2), uiSize_ce) || instanceof_function(Z_OBJCE_P(op2), uiPoint_ce))) { + l = op1; + r = op2; + goto operate; + } + } + + if ((Z_TYPE_P(op1) == IS_OBJECT) && + (instanceof_function(Z_OBJCE_P(op1), uiPoint_ce) || instanceof_function(Z_OBJCE_P(op1), uiSize_ce))) { + l = op1; + r = op2; + } else if ((Z_TYPE_P(op2) == IS_OBJECT) && + (instanceof_function(Z_OBJCE_P(op2), uiPoint_ce) || instanceof_function(Z_OBJCE_P(op2), uiSize_ce))) { + l = op2; + r = op1; + } else { + return FAILURE; + } + +operate: + point = php_ui_point_fetch(l); + + if (Z_TYPE_P(r) != IS_OBJECT) { + x = zval_get_double(r); + y = zval_get_double(r); + } else { + php_ui_point_t *value = php_ui_point_fetch(r); + + x = value->x; + y = value->y; + } + + result = php_ui_point_fetch(retval); + + switch (opcode) { + case ZEND_ADD: assign ? php_ui_point_size_assign_add(result, x, y) : php_ui_point_size_add(result, point, x, y); break; + case ZEND_MUL: assign ? php_ui_point_size_assign_mul(result, x, y) : php_ui_point_size_mul(result, point, x, y); break; + case ZEND_DIV: assign ? php_ui_point_size_assign_div(result, x, y) : php_ui_point_size_div(result, point, x, y); break; + case ZEND_SUB: assign ? php_ui_point_size_assign_sub(result, x, y) : php_ui_point_size_sub(result, point, x, y); break; + } + + return SUCCESS; +} /* }}} */ + +#endif diff --git a/classes/point.c b/classes/point.c index ba3656d..79e9419 100644 --- a/classes/point.c +++ b/classes/point.c @@ -21,7 +21,7 @@ #include "php.h" -#include +#include zend_object_handlers php_ui_point_handlers; @@ -130,6 +130,40 @@ PHP_METHOD(Point, setY) point->y = y; } /* }}} */ +/* {{{ proto Point Point::at(double value) + Point Point::at(UI\Size size) */ +PHP_METHOD(Point, at) +{ + php_ui_point_t *point; + zval *location = NULL; + + if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z", &location) != SUCCESS) { + return; + } + + if (Z_TYPE_P(location) == IS_OBJECT && instanceof_function(Z_OBJCE_P(location), uiPoint_ce)) { + RETURN_ZVAL(location, 1, 0); + } + + if (Z_TYPE_P(location) == IS_OBJECT && instanceof_function(Z_OBJCE_P(location), uiSize_ce)) { + php_ui_size_t *size = php_ui_size_fetch(location); + + object_init_ex(return_value, uiPoint_ce); + + point = php_ui_point_fetch(return_value); + + point->x = size->width; + point->y = size->height; + return; + } + + object_init_ex(return_value, uiPoint_ce); + + point = php_ui_point_fetch(return_value); + point->x = zval_get_double(location); + point->y = zval_get_double(location); +} /* }}} */ + /* {{{ */ const zend_function_entry php_ui_point_methods[] = { PHP_ME(Point, __construct, php_ui_point_construct_info, ZEND_ACC_PUBLIC) @@ -137,65 +171,13 @@ const zend_function_entry php_ui_point_methods[] = { PHP_ME(Point, getY, php_ui_point_get_point_info, ZEND_ACC_PUBLIC) PHP_ME(Point, setX, php_ui_point_set_point_info, ZEND_ACC_PUBLIC) PHP_ME(Point, setY, php_ui_point_set_point_info, ZEND_ACC_PUBLIC) + PHP_ME(Point, at, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; /* }}} */ /* {{{ */ static int php_ui_point_operate(zend_uchar opcode, zval *result, zval *op1, zval *op2) { - php_ui_point_t *point = php_ui_point_fetch(op1); - -#define php_ui_point_do_operation(operator) do { \ - if (Z_TYPE_P(op2) == IS_OBJECT && instanceof_function(Z_OBJCE_P(op2), uiPoint_ce)) { \ - php_ui_point_t *operand = php_ui_point_fetch(op2); \ - php_ui_point_t *retval; \ - \ - if (result != op1) { \ - object_init_ex(result, uiPoint_ce); \ - } \ - \ - retval = php_ui_point_fetch(result); \ - retval->x = point->x operator operand->x; \ - retval->y = point->x operator operand->y; \ - \ - return SUCCESS; \ - } \ - \ - if (Z_TYPE_P(op2) == IS_LONG || Z_TYPE_P(op2) == IS_DOUBLE) { \ - php_ui_point_t *retval; \ - \ - if (result != op1) { \ - object_init_ex(result, uiPoint_ce); \ - } \ - \ - retval = php_ui_point_fetch(result); \ - retval->x = point->x operator zval_get_double(op2); \ - retval->y = point->y operator zval_get_double(op2); \ - \ - return SUCCESS; \ - } \ -} while(0) - - switch (opcode) { - case ZEND_MUL: - php_ui_point_do_operation(*); - break; - - case ZEND_DIV: - php_ui_point_do_operation(/); - break; - - case ZEND_SUB: - php_ui_point_do_operation(-); - break; - - case ZEND_ADD: - php_ui_point_do_operation(+); - break; - } - -#undef php_ui_point_do_operation - - return FAILURE; + return php_ui_point_size_operation(opcode, result, op1, op2, uiPoint_ce); } /* }}} */ /* {{{ */ diff --git a/classes/size.c b/classes/size.c index 19b80ba..37e5e30 100644 --- a/classes/size.c +++ b/classes/size.c @@ -21,7 +21,7 @@ #include "php.h" -#include +#include zend_object_handlers php_ui_size_handlers; @@ -130,6 +130,40 @@ PHP_METHOD(Size, setHeight) size->height = height; } /* }}} */ +/* {{{ proto Size Size::of(double value) + Size Size::of(UI\Point point) */ +PHP_METHOD(Size, of) +{ + php_ui_size_t *size; + zval *location = NULL; + + if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z", &location) != SUCCESS) { + return; + } + + if (Z_TYPE_P(location) == IS_OBJECT && instanceof_function(Z_OBJCE_P(location), uiSize_ce)) { + RETURN_ZVAL(location, 1, 0); + } + + if (Z_TYPE_P(location) == IS_OBJECT && instanceof_function(Z_OBJCE_P(location), uiPoint_ce)) { + php_ui_point_t *point = php_ui_point_fetch(location); + + object_init_ex(return_value, uiSize_ce); + + size = php_ui_size_fetch(return_value); + + size->width = point->x; + size->height = point->y; + return; + } + + object_init_ex(return_value, uiSize_ce); + + size = php_ui_size_fetch(return_value); + size->width = zval_get_double(location); + size->height = zval_get_double(location); +} /* }}} */ + /* {{{ */ const zend_function_entry php_ui_size_methods[] = { PHP_ME(Size, __construct, php_ui_size_construct_info, ZEND_ACC_PUBLIC) @@ -137,65 +171,13 @@ const zend_function_entry php_ui_size_methods[] = { PHP_ME(Size, getHeight, php_ui_size_get_size_info, ZEND_ACC_PUBLIC) PHP_ME(Size, setWidth, php_ui_size_set_size_info, ZEND_ACC_PUBLIC) PHP_ME(Size, setHeight, php_ui_size_set_size_info, ZEND_ACC_PUBLIC) + PHP_ME(Size, of, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_FE_END }; /* }}} */ /* {{{ */ static int php_ui_size_operate(zend_uchar opcode, zval *result, zval *op1, zval *op2) { - php_ui_size_t *size = php_ui_size_fetch(op1); - -#define php_ui_size_do_operation(operator) do { \ - if (Z_TYPE_P(op2) == IS_OBJECT && instanceof_function(Z_OBJCE_P(op2), uiSize_ce)) { \ - php_ui_size_t *operand = php_ui_size_fetch(op2); \ - php_ui_size_t *retval; \ - \ - if (result != op1) { \ - object_init_ex(result, uiSize_ce); \ - } \ - \ - retval = php_ui_size_fetch(result); \ - retval->width = size->width operator operand->width; \ - retval->height = size->height operator operand->height; \ - \ - return SUCCESS; \ - } \ - \ - if (Z_TYPE_P(op2) == IS_LONG || Z_TYPE_P(op2) == IS_DOUBLE) { \ - php_ui_size_t *retval; \ - \ - if (result != op1) { \ - object_init_ex(result, uiSize_ce); \ - } \ - \ - retval = php_ui_size_fetch(result); \ - retval->width = size->width operator zval_get_double(op2); \ - retval->height = size->height operator zval_get_double(op2); \ - \ - return SUCCESS; \ - } \ -} while(0) - - switch (opcode) { - case ZEND_MUL: - php_ui_size_do_operation(*); - break; - - case ZEND_DIV: - php_ui_size_do_operation(/); - break; - - case ZEND_SUB: - php_ui_size_do_operation(-); - break; - - case ZEND_ADD: - php_ui_size_do_operation(+); - break; - } - -#undef php_ui_size_do_operation - - return FAILURE; + return php_ui_point_size_operation(opcode, result, op1, op2, uiSize_ce); } /* }}} */ /* {{{ */ diff --git a/examples/starfield.php b/examples/starfield.php index cb76a07..4583077 100644 --- a/examples/starfield.php +++ b/examples/starfield.php @@ -51,12 +51,10 @@ protected function onKey(string $key, int $ext, int $flags) { } protected function onDraw(UI\Draw\Pen $pen, UI\Size $size, UI\Point $clip, UI\Size $clipSize) { - $hWidth = $size->width / 2; - $hHeight = $size->height /2; - + $hSize = $size / 2; + $path = new Path(Path::Winding); - $path->addRectangle( - new Point(0, 0), $size); + $path->addRectangle(Point::at(0), $size); $path->end(); $pen->fill($path, new Brush(Brush::Solid, new Color(0, 1))); @@ -69,18 +67,16 @@ protected function onDraw(UI\Draw\Pen $pen, UI\Size $size, UI\Point $clip, UI\Si $star[1] = $this->depth; } - $k = 128 / $star[1]; - $px = $star[0]->x * $k + $hWidth; - $py = $star[0]->y * $k + $hHeight; - - if ($px >= 0 && $px <= $size->width && $py >= 0 && $py <= $size->height) { + $pos = $star[0] * (128 / $star[1]) + $hSize; + + if ($pos->x >= 0 && $pos->x <= $size->width && $pos->y >= 0 && $pos->y <= $size->height) { $starSize = (1 - $star[1] / 32) * 5; $path = new Path(Path::Winding); if (PHP_OS == "WINNT") { - $path->addRectangle(new Point($px, $py), new Size($starSize, $starSize)); + $path->addRectangle($pos, new Size($starSize, $starSize)); } else { - $path->arcTo(new Point($px, $py), $starSize/2, 0, M_PI*2, 0); + $path->newFigureWithArc($pos, $starSize/2, 0, M_PI*2, 0); } $path->end(); diff --git a/package.xml b/package.xml index 7a335e7..c5c71d2 100644 --- a/package.xml +++ b/package.xml @@ -10,11 +10,11 @@ krakjoe@php.net yes - 2016-10-26 - + 2016-10-28 + - 1.0.1 - 1.0.1 + 1.0.2 + 1.0.2 beta @@ -22,7 +22,9 @@ PHP License - Fix windows onTick + UI\Size and UI\Point support for basic math operators (+,/,*,-) fixed + UI\Point cast to UI\Size with UI\Size::of(UI\Point) (can also take double-ish) + UI\Size cast to UI\Point with UI\Point::at(UI\Size) (can also take double-ish) @@ -30,6 +32,7 @@ + diff --git a/php_ui.h b/php_ui.h index 84f000b..3697b31 100644 --- a/php_ui.h +++ b/php_ui.h @@ -24,7 +24,7 @@ extern zend_module_entry ui_module_entry; #define phpext_ui_ptr &ui_module_entry -#define PHP_UI_VERSION "1.0.1" +#define PHP_UI_VERSION "1.0.2" #ifdef PHP_WIN32 # define PHP_UI_API __declspec(dllexport)