24
24
# pylint: disable=too-many-lines, too-many-instance-attributes, too-many-arguments
25
25
# pylint: disable=too-many-locals, too-many-statements
26
26
27
-
28
27
import math
29
28
import displayio
30
29
import board
31
30
import terminalio
32
31
from adafruit_displayio_layout .widgets .widget import Widget
33
32
from adafruit_displayio_layout .widgets import _blit_rotate_scale
33
+ import vectorio
34
34
35
35
try :
36
36
import bitmaptools
41
41
class Cartesian (Widget ):
42
42
"""A cartesian widget. The origin is set using ``x`` and ``y``.
43
43
44
- :param int x: pixel position TODO
45
- :param int y: pixel position TODO
44
+ :param int x: x position of the plane origin
45
+ :param int y: y position of the plane origin
46
+
47
+ :param int display_color: background color to use defaults to black (0x000000)
48
+ :param int width: requested width, in pixels defaults to screen width
49
+ :param int height: requested height, in pixels defaults to screen height
50
+
51
+ :param int axes_color: axes lines color defaults to white (0xFFFFFF)
52
+ :param int axes_stroke: axes lines thickness in pixels defaults to 2
53
+
54
+ :param int major_tick_stroke: tick lines thickness in pixels dafaults to 1
55
+ :param int major_tick_lenght: tick lines lenght in pixels defaults to 5
46
56
47
- :param int width: requested width, in pixels TODO
48
- :param int height: requested height, in pixels TODO
57
+ :param int tick_label_font: tick label text font
58
+ :param int tick_label_color: tick label text color
59
+
60
+ :param int pointer_radius: pointer radius in pixels defaults to 1
61
+ :param int pointer_color: pointer color defaults to white (0xFFFFFF)
49
62
50
63
"""
51
64
52
65
def __init__ (
53
66
self ,
67
+ x : int = 10 ,
68
+ y : int = board .DISPLAY .height - 10 ,
54
69
display_color = 0x000000 ,
55
70
width : int = board .DISPLAY .width ,
56
71
height : int = board .DISPLAY .height ,
57
- axes_color = 0xFFFFFF ,
58
- axes_stroke = 2 ,
59
- tick_color = 0xFFFFFF ,
60
- major_tick_stroke = 1 ,
61
- major_tick_length = 10 ,
72
+ axes_color : int = 0xFFFFFF ,
73
+ axes_stroke : int = 2 ,
74
+ tick_color : int = 0xFFFFFF ,
75
+ major_tick_stroke : int = 1 ,
76
+ major_tick_length : int = 5 ,
62
77
tick_label_font = terminalio .FONT ,
63
- tick_label_color = 0xFFFFFF ,
78
+ tick_label_color : int = 0xFFFFFF ,
79
+ pointer_radius : int = 1 ,
80
+ pointer_color : int = 0xFFFFFF ,
64
81
** kwargs ,
65
- ):
82
+ ) -> None :
66
83
67
84
super ().__init__ (** kwargs , max_size = 3 )
85
+ self ._origin_x = x
86
+ self ._origin_y = y
68
87
69
88
self ._margin = 10
70
89
71
90
self ._display_color = display_color
72
- self ._display_width = width
73
- self ._display_height = height
91
+ self ._widget_width = width
92
+ self ._widget_height = height
74
93
75
94
self ._axes_line_color = axes_color
76
95
self ._axes_line_thickness = axes_stroke
77
96
78
97
self ._tick_color = tick_color
79
98
self ._tick_line_thickness = major_tick_stroke
80
99
self ._tick_line_height = major_tick_length
100
+
101
+ self ._pointer_radius = pointer_radius
102
+ self ._pointer_color = pointer_color
103
+
81
104
self ._font = tick_label_font
82
105
self ._font_color = tick_label_color
83
106
84
107
self ._font_width = self ._get_font_height (self ._font , 1 )[0 ]
85
108
self ._font_height = self ._get_font_height (self ._font , 1 )[1 ]
86
109
87
- self ._usable_width = self ._display_width - self ._font_width - 2 * self ._margin
88
- self ._usable_height = (
89
- self ._display_height - self ._font_height - 2 * self ._margin
90
- )
110
+ self ._usable_width = self ._widget_width - 2 * self ._margin
111
+ self ._usable_height = self ._widget_height - 2 * self ._margin
91
112
self ._tickx_separation = 2 * self ._font_width + 2
92
113
93
114
self ._tick_bitmap = displayio .Bitmap (
@@ -106,11 +127,11 @@ def __init__(
106
127
self ._axesy_bitmap .fill (2 )
107
128
108
129
self ._screen_bitmap = displayio .Bitmap (
109
- self ._usable_width , self ._usable_height , 6
130
+ self ._usable_width , self ._usable_height , 3
110
131
)
111
132
112
133
self ._screen_palette = displayio .Palette (6 )
113
- self ._screen_palette [ 0 ] = 0x000000
134
+ self ._screen_palette . make_transparent ( 0 )
114
135
self ._screen_palette [1 ] = self ._tick_color
115
136
self ._screen_palette [2 ] = self ._axes_line_color
116
137
self ._screen_palette [3 ] = 0x00FFFF
@@ -120,12 +141,14 @@ def __init__(
120
141
self ._screen_tilegrid = displayio .TileGrid (
121
142
self ._screen_bitmap ,
122
143
pixel_shader = self ._screen_palette ,
123
- x = self ._margin + self . _font_width ,
124
- y = self ._margin ,
144
+ x = self ._origin_x ,
145
+ y = self ._origin_y ,
125
146
)
126
147
127
148
self ._draw_axes ()
128
149
self ._draw_ticks ()
150
+ self ._draw_pointers ()
151
+ self .append (self ._pointer_vector_shape )
129
152
self .append (self ._screen_tilegrid )
130
153
131
154
@staticmethod
@@ -141,7 +164,7 @@ def _get_font_height(font, scale):
141
164
def _draw_axes (self ):
142
165
bitmaptools .rotozoom (
143
166
self ._screen_bitmap ,
144
- ox = self ._margin - 1 ,
167
+ ox = self ._margin ,
145
168
oy = self ._usable_height ,
146
169
source_bitmap = self ._axesx_bitmap ,
147
170
px = self ._axesx_bitmap .width ,
@@ -151,7 +174,7 @@ def _draw_axes(self):
151
174
152
175
bitmaptools .rotozoom (
153
176
self ._screen_bitmap ,
154
- ox = int (self ._usable_width - self ._margin / 2 ),
177
+ ox = int (self ._usable_width + self ._margin ),
155
178
oy = self ._usable_height ,
156
179
source_bitmap = self ._axesy_bitmap ,
157
180
px = self ._axesy_bitmap .width ,
@@ -203,3 +226,28 @@ def _draw_ticks(self):
203
226
py = int (self ._tick_bitmap .height / 2 ),
204
227
angle = (90 * math .pi / 180 ), # in radians
205
228
)
229
+
230
+ def _draw_pointers (self ):
231
+ self ._pointer = vectorio .Circle (3 )
232
+
233
+ self ._circle_palette = displayio .Palette (2 )
234
+ self ._circle_palette .make_transparent (0 )
235
+ self ._circle_palette [1 ] = 0xFFFFFF
236
+
237
+ self ._pointer_vector_shape = vectorio .VectorShape (
238
+ shape = self ._pointer ,
239
+ pixel_shader = self ._circle_palette ,
240
+ x = 0 ,
241
+ y = 0 ,
242
+ )
243
+
244
+ def update_pointer (self , x : int , y : int ):
245
+ """updater_pointer function
246
+ helper function to update pointer in the plane
247
+ :param x: x coordinate in the local plane
248
+ :param y: y coordinate in the local plane
249
+ :return: None
250
+ rtype: None
251
+ """
252
+ self ._pointer_vector_shape .x = self ._origin_x + x + self ._margin
253
+ self ._pointer_vector_shape .y = self ._origin_y + self ._usable_height + y
0 commit comments