|
| 1 | +# ---------------------------------------------------------------------------- |
| 2 | +# Python A-Turtle |
| 3 | +# ---------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Tiago Montes. |
| 5 | +# See LICENSE for details. |
| 6 | +# ---------------------------------------------------------------------------- |
| 7 | + |
| 8 | +import tkinter |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +class InvertedYCanvas: |
| 13 | + |
| 14 | + """ |
| 15 | + Behaves like a tkinter.Canvas widget, where Y coordinates are negated such |
| 16 | + that larger values are at the top of the screen. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, master, background): |
| 20 | + """ |
| 21 | + Creates a Canvas widget with parent `master` and the given `background` |
| 22 | + color. Sets the underlying tkinter.Canvas widget's highlightthickness |
| 23 | + to zero such that no border/outline is visible. |
| 24 | + """ |
| 25 | + self._canvas = tkinter.Canvas( |
| 26 | + master, |
| 27 | + highlightthickness=0, |
| 28 | + background=background, |
| 29 | + ) |
| 30 | + |
| 31 | + def _inverted_y(self, coords): |
| 32 | + |
| 33 | + # `coords` is an iterable of (x0, y0, x1, y0, ..., xn, yn) numbers, |
| 34 | + # where the ones in odd offsets/indices are Y values that are negated. |
| 35 | + |
| 36 | + return [ |
| 37 | + -value if offset % 2 else value |
| 38 | + for offset, value in enumerate(coords) |
| 39 | + ] |
| 40 | + |
| 41 | + |
| 42 | + def create_polygon(self, coords, *, fill, outline, width): |
| 43 | + """ |
| 44 | + Creates a polygon item with given `coords` and visual attributes. |
| 45 | + """ |
| 46 | + return self._canvas.create_polygon( |
| 47 | + self._inverted_y(coords), |
| 48 | + fill=fill, |
| 49 | + outline=outline, |
| 50 | + width=width, |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | + def create_image(self, x, y, *, image, anchor): |
| 55 | + """ |
| 56 | + Creates an image item at the given (`x`, `y`) position with the `image` |
| 57 | + achored at `anchor`. |
| 58 | + """ |
| 59 | + return self._canvas.create_image(x, -y, image=image, anchor=anchor) |
| 60 | + |
| 61 | + |
| 62 | + def create_line(self, coords, *, fill, width, capstyle): |
| 63 | + """ |
| 64 | + Creates a line item with given `coords` and visual attributes. |
| 65 | + """ |
| 66 | + return self._canvas.create_line( |
| 67 | + self._inverted_y(coords), |
| 68 | + fill=fill, |
| 69 | + width=width, |
| 70 | + capstyle=capstyle, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | + def move(self, item_id, dx, dy): |
| 75 | + """ |
| 76 | + Moves the item identified by `item_id` relatively by (`dx`, `dy`). |
| 77 | + """ |
| 78 | + return self._canvas.move(item_id, dx, -dy) |
| 79 | + |
| 80 | + |
| 81 | + def coords(self, item_id, coords): |
| 82 | + """ |
| 83 | + Sets the coordinates of the item identified by `item_id` to `coords`. |
| 84 | + """ |
| 85 | + return self._canvas.coords(item_id, self._inverted_y(coords)) |
| 86 | + |
| 87 | + |
| 88 | + def __getattr__(self, name): |
| 89 | + """ |
| 90 | + Delegates attribute access to the wrapped tkinter.Canvas object. |
| 91 | + """ |
| 92 | + return getattr(self._canvas, name) |
0 commit comments