vectorio: Add width and height properties to rectangle

Closes: https://github.com/adafruit/circuitpython/issues/5695
This commit is contained in:
Kenny 2021-12-12 19:50:00 -08:00
parent 05e0734157
commit 17b53c7ebe
6 changed files with 98 additions and 1 deletions

View File

@ -2726,6 +2726,10 @@ msgstr ""
msgid "calibration value out of range +/-127"
msgstr ""
#: shared-module/vectorio/Rectangle.c
msgid "can only be registered in one parent"
msgstr ""
#: py/emitinlinethumb.c
msgid "can only have up to 4 parameters to Thumb assembly"
msgstr ""
@ -3957,7 +3961,9 @@ msgstr ""
#: ports/espressif/boards/atmegazero_esp32s2/mpconfigboard.h
#: ports/espressif/boards/crumpspace_crumps2/mpconfigboard.h
#: ports/espressif/boards/electroniccats_bastwifi/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s3_box/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s3_devkitc_1/mpconfigboard.h
#: ports/espressif/boards/espressif_esp32s3_devkitc_1_nopsram/mpconfigboard.h
#: ports/espressif/boards/espressif_hmi_devkit_1/mpconfigboard.h
#: ports/espressif/boards/espressif_kaluga_1.3/mpconfigboard.h
#: ports/espressif/boards/espressif_kaluga_1/mpconfigboard.h
@ -3972,7 +3978,6 @@ msgstr ""
#: ports/espressif/boards/lilygo_ttgo_t8_s2_st7789/mpconfigboard.h
#: ports/espressif/boards/lolin_s2_mini/mpconfigboard.h
#: ports/espressif/boards/lolin_s2_pico/mpconfigboard.h
#: ports/espressif/boards/microdev_macro_s3/mpconfigboard.h
#: ports/espressif/boards/microdev_micro_c3/mpconfigboard.h
#: ports/espressif/boards/microdev_micro_s2/mpconfigboard.h
#: ports/espressif/boards/morpheans_morphesp-240/mpconfigboard.h

View File

@ -60,6 +60,51 @@ STATIC const vectorio_draw_protocol_t rectangle_draw_protocol = {
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
};
//| width : int
//| """The width of the rectangle in pixels."""
//|
STATIC mp_obj_t vectorio_rectangle_obj_get_width(mp_obj_t self_in) {
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(common_hal_vectorio_rectangle_get_width(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_get_width_obj, vectorio_rectangle_obj_get_width);
STATIC mp_obj_t vectorio_rectangle_obj_set_width(mp_obj_t self_in, mp_obj_t width) {
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_vectorio_rectangle_set_width(self, mp_obj_get_int(width));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_set_width_obj, vectorio_rectangle_obj_set_width);
const mp_obj_property_t vectorio_rectangle_width_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&vectorio_rectangle_get_width_obj,
(mp_obj_t)&vectorio_rectangle_set_width_obj,
MP_ROM_NONE},
};
//| height : int
//| """The height of the rectangle in pixels."""
//|
STATIC mp_obj_t vectorio_rectangle_obj_get_height(mp_obj_t self_in) {
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(common_hal_vectorio_rectangle_get_height(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_get_height_obj, vectorio_rectangle_obj_get_height);
STATIC mp_obj_t vectorio_rectangle_obj_set_height(mp_obj_t self_in, mp_obj_t height) {
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_vectorio_rectangle_set_height(self, mp_obj_get_int(height));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_set_height_obj, vectorio_rectangle_obj_set_height);
const mp_obj_property_t vectorio_rectangle_height_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&vectorio_rectangle_get_height_obj,
(mp_obj_t)&vectorio_rectangle_set_height_obj,
MP_ROM_NONE},
};
// Documentation for properties inherited from VectorShape.
@ -80,6 +125,8 @@ STATIC const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = {
// Properties
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
};

View File

@ -3,10 +3,12 @@
#include "shared-module/vectorio/Rectangle.h"
#include "shared-module/displayio/area.h"
#include "shared-module/vectorio/__init__.h"
extern const mp_obj_type_t vectorio_rectangle_type;
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height);
void common_hal_vectorio_rectangle_set_on_dirty(vectorio_rectangle_t *self, vectorio_event_t on_dirty);
uint32_t common_hal_vectorio_rectangle_get_pixel(void *rectangle, int16_t x, int16_t y);
@ -14,4 +16,10 @@ void common_hal_vectorio_rectangle_get_area(void *rectangle, displayio_area_t *o
mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle);
int16_t common_hal_vectorio_rectangle_get_width(void *obj);
void common_hal_vectorio_rectangle_set_width(void *obj, int16_t width);
int16_t common_hal_vectorio_rectangle_get_height(void *obj);
void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_RECTANGLE_H

View File

@ -62,6 +62,7 @@ mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pix
if (mp_obj_is_type(shape, &vectorio_polygon_type)) {
common_hal_vectorio_polygon_set_on_dirty(self->ishape.shape, on_dirty);
} else if (mp_obj_is_type(shape, &vectorio_rectangle_type)) {
common_hal_vectorio_rectangle_set_on_dirty(self->ishape.shape, on_dirty);
} else if (mp_obj_is_type(shape, &vectorio_circle_type)) {
common_hal_vectorio_circle_set_on_dirty(self->ishape.shape, on_dirty);
} else {

View File

@ -1,7 +1,9 @@
#include "shared-module/vectorio/__init__.h"
#include "shared-bindings/vectorio/Rectangle.h"
#include "shared-module/displayio/area.h"
#include "py/runtime.h"
#include "stdlib.h"
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height) {
@ -9,6 +11,12 @@ void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_
self->height = height;
}
void common_hal_vectorio_rectangle_set_on_dirty(vectorio_rectangle_t *self, vectorio_event_t on_dirty) {
if (self->on_dirty.obj != NULL) {
mp_raise_TypeError(translate("can only be registered in one parent"));
}
self->on_dirty = on_dirty;
}
uint32_t common_hal_vectorio_rectangle_get_pixel(void *obj, int16_t x, int16_t y) {
vectorio_rectangle_t *self = obj;
@ -32,3 +40,29 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) {
vectorio_rectangle_t *self = rectangle;
return self->draw_protocol_instance;
}
int16_t common_hal_vectorio_rectangle_get_width(void *obj) {
vectorio_rectangle_t *self = obj;
return self->width;
}
void common_hal_vectorio_rectangle_set_width(void *obj, int16_t width) {
vectorio_rectangle_t *self = obj;
self->width = abs(width);
if (self->on_dirty.obj != NULL) {
self->on_dirty.event(self->on_dirty.obj);
}
}
int16_t common_hal_vectorio_rectangle_get_height(void *obj) {
vectorio_rectangle_t *self = obj;
return self->height;
}
void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height) {
vectorio_rectangle_t *self = obj;
self->height = abs(height);
if (self->on_dirty.obj != NULL) {
self->on_dirty.event(self->on_dirty.obj);
}
}

View File

@ -4,11 +4,13 @@
#include <stdint.h>
#include "py/obj.h"
#include "shared-module/vectorio/__init__.h"
typedef struct {
mp_obj_base_t base;
uint16_t width;
uint16_t height;
vectorio_event_t on_dirty;
mp_obj_t draw_protocol_instance;
} vectorio_rectangle_t;