Skip to content

Commit

Permalink
Merge pull request #84 from jposada202020/updating_cartesian_widget
Browse files Browse the repository at this point in the history
Updating cartesian widget
  • Loading branch information
FoamyGuy authored Feb 13, 2023
2 parents c7f30ee + a84c96c commit 64b75e1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 105 deletions.
64 changes: 0 additions & 64 deletions adafruit_displayio_layout/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,3 @@
# SPDX-FileCopyrightText: 2021 Kevin Matocha, Tim C, Jose David M
#
# SPDX-License-Identifier: MIT

"""
`adafruit_displayio_layout.widgets`
=======================
"""

try:
import vectorio
except ImportError:
pass

try:
import bitmaptools
except ImportError:
pass


# pylint: disable=invalid-name, too-many-arguments
def rectangle_helper(
x0: int,
y0: int,
height: int,
width: int,
bitmap,
color_index: int,
palette,
bitmaptool: bool = True,
) -> None:
"""rectangle_helper function
Draws a rectangle to the bitmap given using ``bitmapstools.bitmap`` or
``vectorio.rectangle`` functions
:param int x0: rectangle lower corner x position
:param int y0: rectangle lower corner y position
:param int width: rectangle upper corner x position
:param int height: rectangle upper corner y position
:param int color_index: palette color index to be used
:param palette: palette object to be used to draw the rectangle
:param bitmap: bitmap for the rectangle to be drawn
:param bool bitmaptool: uses :py:func:`~bitmaptools.draw_line` to draw the rectanlge.
when `False` uses :py:func:`~vectorio.Rectangle`
:return: None
:rtype: None
┌───────────────────────┐
│ │
│ │
(x0,y0) └───────────────────────┘
"""
if bitmaptool:
bitmaptools.fill_region(bitmap, x0, y0, x0 + width, y0 + height, color_index)
else:
rect = vectorio.Rectangle(width, height)
vectorio.VectorShape(
shape=rect,
pixel_shader=palette,
x=x0,
y=y0,
)
72 changes: 31 additions & 41 deletions adafruit_displayio_layout/widgets/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from adafruit_display_text import bitmap_label
import vectorio
from adafruit_displayio_layout.widgets.widget import Widget
from adafruit_displayio_layout.widgets import rectangle_helper

try:
import bitmaptools
Expand Down Expand Up @@ -138,7 +137,7 @@ class Cartesian(Widget):
- **range**: ``xrange`` and ``yrange`` This is the range in absolute units.
For example, when using (20-90), the X axis will start at 20 finishing at 90.
However the height of the graph is given by the height parameter. The scale
However, the height of the graph is given by the height parameter. The scale
is handled internal to provide a 1:1 experience when you update the graph.
Expand Down Expand Up @@ -269,14 +268,14 @@ def __init__(
self._screen_palette[5] = self._background_color

self._corner_bitmap = displayio.Bitmap(10, 10, 5)
rectangle_helper(

bitmaptools.fill_region(
self._corner_bitmap,
0,
0,
self._axes_line_thickness,
self._axes_line_thickness,
self._corner_bitmap,
2,
self._screen_palette,
)

self._corner_tilegrid = displayio.TileGrid(
Expand Down Expand Up @@ -336,28 +335,23 @@ def _get_font_height(font, scale: int) -> Tuple[int, int]:
return font_width, font_height

def _draw_axes(self) -> None:
# Draw x axes line
rectangle_helper(

bitmaptools.fill_region(
self._axesx_bitmap,
0,
0,
self._axes_line_thickness,
self.width,
self._axesx_bitmap,
self._axes_line_thickness,
2,
self._screen_palette,
True,
)

# Draw y axes line
rectangle_helper(
bitmaptools.fill_region(
self._axesy_bitmap,
self._axesy_width - self._axes_line_thickness,
0,
self._axesy_width,
self.height,
self._axes_line_thickness,
self._axesy_bitmap,
2,
self._screen_palette,
True,
)

def _draw_ticks(self) -> None:
Expand All @@ -382,30 +376,28 @@ def _draw_ticks(self) -> None:
+ 1,
)
self.append(tick_text)
rectangle_helper(

bitmaptools.fill_region(
self._axesx_bitmap,
text_dist,
self._axes_line_thickness,
self._tick_line_height,
self._tick_line_thickness,
self._axesx_bitmap,
text_dist + self._tick_line_thickness,
self._axes_line_thickness + self._tick_line_height,
1,
self._screen_palette,
True,
)

if self._subticks:
if i in subticks:
# calc subtick_line_height; force min lineheigt to 1.
subtick_line_height = max(1, self._tick_line_height // 2)
rectangle_helper(

bitmaptools.fill_region(
self._axesx_bitmap,
text_dist,
self._axes_line_thickness,
subtick_line_height,
text_dist + 1,
self._axes_line_thickness + subtick_line_height,
1,
self._axesx_bitmap,
1,
self._screen_palette,
True,
)

# Y axes ticks
Expand All @@ -425,34 +417,32 @@ def _draw_ticks(self) -> None:
y=0 + self.height - text_dist,
)
self.append(tick_text)
rectangle_helper(

bitmaptools.fill_region(
self._axesy_bitmap,
self._axesy_width
- self._axes_line_thickness
- self._tick_line_height
- 1,
text_dist,
self._tick_line_thickness,
self._tick_line_height,
self._axesy_bitmap,
self._axesy_width - self._axes_line_thickness - 1,
text_dist + self._tick_line_thickness,
1,
self._screen_palette,
True,
)

if self._subticks:
if i in subticks:
rectangle_helper(

bitmaptools.fill_region(
self._axesy_bitmap,
self._axesy_width
- self._axes_line_thickness
- self._tick_line_height // 2
- 1,
text_dist,
self._axesy_width - self._axes_line_thickness - 1,
text_dist + 1,
1,
self._tick_line_height // 2,
self._axesy_bitmap,
1,
self._screen_palette,
True,
)

def _draw_pointers(self, x: int, y: int) -> None:
Expand Down

0 comments on commit 64b75e1

Please sign in to comment.