Skip to content

Commit aee80d6

Browse files
authored
Merge pull request #5709 from WarriorOfWire/width_and_height
vectorio: Add width and height properties to rectangle
2 parents 7f5aa1a + 17b53c7 commit aee80d6

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

locale/circuitpython.pot

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2726,6 +2726,10 @@ msgstr ""
27262726
msgid "calibration value out of range +/-127"
27272727
msgstr ""
27282728

2729+
#: shared-module/vectorio/Rectangle.c
2730+
msgid "can only be registered in one parent"
2731+
msgstr ""
2732+
27292733
#: py/emitinlinethumb.c
27302734
msgid "can only have up to 4 parameters to Thumb assembly"
27312735
msgstr ""
@@ -3957,7 +3961,9 @@ msgstr ""
39573961
#: ports/espressif/boards/atmegazero_esp32s2/mpconfigboard.h
39583962
#: ports/espressif/boards/crumpspace_crumps2/mpconfigboard.h
39593963
#: ports/espressif/boards/electroniccats_bastwifi/mpconfigboard.h
3964+
#: ports/espressif/boards/espressif_esp32s3_box/mpconfigboard.h
39603965
#: ports/espressif/boards/espressif_esp32s3_devkitc_1/mpconfigboard.h
3966+
#: ports/espressif/boards/espressif_esp32s3_devkitc_1_nopsram/mpconfigboard.h
39613967
#: ports/espressif/boards/espressif_hmi_devkit_1/mpconfigboard.h
39623968
#: ports/espressif/boards/espressif_kaluga_1.3/mpconfigboard.h
39633969
#: ports/espressif/boards/espressif_kaluga_1/mpconfigboard.h
@@ -3972,7 +3978,6 @@ msgstr ""
39723978
#: ports/espressif/boards/lilygo_ttgo_t8_s2_st7789/mpconfigboard.h
39733979
#: ports/espressif/boards/lolin_s2_mini/mpconfigboard.h
39743980
#: ports/espressif/boards/lolin_s2_pico/mpconfigboard.h
3975-
#: ports/espressif/boards/microdev_macro_s3/mpconfigboard.h
39763981
#: ports/espressif/boards/microdev_micro_c3/mpconfigboard.h
39773982
#: ports/espressif/boards/microdev_micro_s2/mpconfigboard.h
39783983
#: ports/espressif/boards/morpheans_morphesp-240/mpconfigboard.h

shared-bindings/vectorio/Rectangle.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,51 @@ STATIC const vectorio_draw_protocol_t rectangle_draw_protocol = {
6060
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
6161
};
6262

63+
//| width : int
64+
//| """The width of the rectangle in pixels."""
65+
//|
66+
STATIC mp_obj_t vectorio_rectangle_obj_get_width(mp_obj_t self_in) {
67+
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
68+
return mp_obj_new_int(common_hal_vectorio_rectangle_get_width(self));
69+
}
70+
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_get_width_obj, vectorio_rectangle_obj_get_width);
71+
72+
STATIC mp_obj_t vectorio_rectangle_obj_set_width(mp_obj_t self_in, mp_obj_t width) {
73+
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
74+
common_hal_vectorio_rectangle_set_width(self, mp_obj_get_int(width));
75+
return mp_const_none;
76+
}
77+
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_set_width_obj, vectorio_rectangle_obj_set_width);
78+
79+
const mp_obj_property_t vectorio_rectangle_width_obj = {
80+
.base.type = &mp_type_property,
81+
.proxy = {(mp_obj_t)&vectorio_rectangle_get_width_obj,
82+
(mp_obj_t)&vectorio_rectangle_set_width_obj,
83+
MP_ROM_NONE},
84+
};
85+
86+
//| height : int
87+
//| """The height of the rectangle in pixels."""
88+
//|
89+
STATIC mp_obj_t vectorio_rectangle_obj_get_height(mp_obj_t self_in) {
90+
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
91+
return mp_obj_new_int(common_hal_vectorio_rectangle_get_height(self));
92+
}
93+
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_rectangle_get_height_obj, vectorio_rectangle_obj_get_height);
94+
95+
STATIC mp_obj_t vectorio_rectangle_obj_set_height(mp_obj_t self_in, mp_obj_t height) {
96+
vectorio_rectangle_t *self = MP_OBJ_TO_PTR(self_in);
97+
common_hal_vectorio_rectangle_set_height(self, mp_obj_get_int(height));
98+
return mp_const_none;
99+
}
100+
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_rectangle_set_height_obj, vectorio_rectangle_obj_set_height);
101+
102+
const mp_obj_property_t vectorio_rectangle_height_obj = {
103+
.base.type = &mp_type_property,
104+
.proxy = {(mp_obj_t)&vectorio_rectangle_get_height_obj,
105+
(mp_obj_t)&vectorio_rectangle_set_height_obj,
106+
MP_ROM_NONE},
107+
};
63108

64109
// Documentation for properties inherited from VectorShape.
65110

@@ -80,6 +125,8 @@ STATIC const mp_rom_map_elem_t vectorio_rectangle_locals_dict_table[] = {
80125
// Properties
81126
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
82127
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
128+
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&vectorio_rectangle_width_obj) },
129+
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&vectorio_rectangle_height_obj) },
83130
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
84131
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
85132
};

shared-bindings/vectorio/Rectangle.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@
33

44
#include "shared-module/vectorio/Rectangle.h"
55
#include "shared-module/displayio/area.h"
6+
#include "shared-module/vectorio/__init__.h"
67

78
extern const mp_obj_type_t vectorio_rectangle_type;
89

910
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height);
11+
void common_hal_vectorio_rectangle_set_on_dirty(vectorio_rectangle_t *self, vectorio_event_t on_dirty);
1012

1113
uint32_t common_hal_vectorio_rectangle_get_pixel(void *rectangle, int16_t x, int16_t y);
1214

1315
void common_hal_vectorio_rectangle_get_area(void *rectangle, displayio_area_t *out_area);
1416

1517
mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle);
1618

19+
int16_t common_hal_vectorio_rectangle_get_width(void *obj);
20+
void common_hal_vectorio_rectangle_set_width(void *obj, int16_t width);
21+
22+
int16_t common_hal_vectorio_rectangle_get_height(void *obj);
23+
void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height);
24+
1725
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_VECTORIO_RECTANGLE_H

shared-bindings/vectorio/VectorShape.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pix
6262
if (mp_obj_is_type(shape, &vectorio_polygon_type)) {
6363
common_hal_vectorio_polygon_set_on_dirty(self->ishape.shape, on_dirty);
6464
} else if (mp_obj_is_type(shape, &vectorio_rectangle_type)) {
65+
common_hal_vectorio_rectangle_set_on_dirty(self->ishape.shape, on_dirty);
6566
} else if (mp_obj_is_type(shape, &vectorio_circle_type)) {
6667
common_hal_vectorio_circle_set_on_dirty(self->ishape.shape, on_dirty);
6768
} else {

shared-module/vectorio/Rectangle.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
#include "shared-module/vectorio/__init__.h"
12
#include "shared-bindings/vectorio/Rectangle.h"
23
#include "shared-module/displayio/area.h"
34

45
#include "py/runtime.h"
6+
#include "stdlib.h"
57

68

79
void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height) {
810
self->width = width;
911
self->height = height;
1012
}
1113

14+
void common_hal_vectorio_rectangle_set_on_dirty(vectorio_rectangle_t *self, vectorio_event_t on_dirty) {
15+
if (self->on_dirty.obj != NULL) {
16+
mp_raise_TypeError(translate("can only be registered in one parent"));
17+
}
18+
self->on_dirty = on_dirty;
19+
}
1220

1321
uint32_t common_hal_vectorio_rectangle_get_pixel(void *obj, int16_t x, int16_t y) {
1422
vectorio_rectangle_t *self = obj;
@@ -32,3 +40,29 @@ mp_obj_t common_hal_vectorio_rectangle_get_draw_protocol(void *rectangle) {
3240
vectorio_rectangle_t *self = rectangle;
3341
return self->draw_protocol_instance;
3442
}
43+
44+
int16_t common_hal_vectorio_rectangle_get_width(void *obj) {
45+
vectorio_rectangle_t *self = obj;
46+
return self->width;
47+
}
48+
49+
void common_hal_vectorio_rectangle_set_width(void *obj, int16_t width) {
50+
vectorio_rectangle_t *self = obj;
51+
self->width = abs(width);
52+
if (self->on_dirty.obj != NULL) {
53+
self->on_dirty.event(self->on_dirty.obj);
54+
}
55+
}
56+
57+
int16_t common_hal_vectorio_rectangle_get_height(void *obj) {
58+
vectorio_rectangle_t *self = obj;
59+
return self->height;
60+
}
61+
62+
void common_hal_vectorio_rectangle_set_height(void *obj, int16_t height) {
63+
vectorio_rectangle_t *self = obj;
64+
self->height = abs(height);
65+
if (self->on_dirty.obj != NULL) {
66+
self->on_dirty.event(self->on_dirty.obj);
67+
}
68+
}

shared-module/vectorio/Rectangle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
#include <stdint.h>
55

66
#include "py/obj.h"
7+
#include "shared-module/vectorio/__init__.h"
78

89
typedef struct {
910
mp_obj_base_t base;
1011
uint16_t width;
1112
uint16_t height;
13+
vectorio_event_t on_dirty;
1214
mp_obj_t draw_protocol_instance;
1315
} vectorio_rectangle_t;
1416

0 commit comments

Comments
 (0)