|
| 1 | +""" |
| 2 | +Bitmap icons for BandWatch. |
| 3 | +
|
| 4 | +Each icon is a CSV file in the `icons` folder, one row of the file per row of |
| 5 | +pixels, with 1 for a lit pixel and 0 for an unlit pixel. Every row of an icon |
| 6 | +must have the same number of columns. The icons carry no color, so the same |
| 7 | +shape can be drawn in any color, for example one indicator light for good, |
| 8 | +fair, and poor ratings. |
| 9 | +
|
| 10 | +To add an icon, put its CSV file in `icons`, add a `load()` line for it below, |
| 11 | +and add it to ICONS under the name the render code will use. |
| 12 | +
|
| 13 | +API: |
| 14 | + bitmap(name, color): widget for an icon drawn in a color |
| 15 | + bitmap_width(name): width of an icon in pixels |
| 16 | + bitmap_height(name): height of an icon in pixels |
| 17 | +""" |
| 18 | + |
| 19 | +load("encoding/csv.star", "csv") |
| 20 | +load("icons/arrow_down.csv", ARROW_DOWN_CSV = "file") |
| 21 | +load("icons/arrow_flat.csv", ARROW_FLAT_CSV = "file") |
| 22 | +load("icons/arrow_up.csv", ARROW_UP_CSV = "file") |
| 23 | +load("icons/light_outline.csv", LIGHT_OUTLINE_CSV = "file") |
| 24 | +load("icons/light_rated.csv", LIGHT_RATED_CSV = "file") |
| 25 | +load("icons/moon.csv", MOON_CSV = "file") |
| 26 | +load("icons/sun.csv", SUN_CSV = "file") |
| 27 | +load("render.star", "render") |
| 28 | + |
| 29 | +#------------------------------------------------------------------------------- |
| 30 | +# Loading |
| 31 | +#------------------------------------------------------------------------------- |
| 32 | + |
| 33 | +def read_bitmap(csv_file): |
| 34 | + """Read an icon from a CSV file. |
| 35 | +
|
| 36 | + Args: |
| 37 | + csv_file: A file loaded with `load("icons/NAME.csv", NAME_CSV = "file")`. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + List of rows, each a list of "0" and "1" strings. |
| 41 | + """ |
| 42 | + rows = csv.read_all(csv_file.readall()) |
| 43 | + if len(rows) == 0: |
| 44 | + fail("{} has no rows".format(csv_file.path)) |
| 45 | + for row in rows: |
| 46 | + for cell in row: |
| 47 | + if cell not in ("0", "1"): |
| 48 | + fail("{} has a value other than 0 or 1: {}".format(csv_file.path, cell)) |
| 49 | + return rows |
| 50 | + |
| 51 | +# Icons are read once, when the library is loaded. |
| 52 | +ICONS = { |
| 53 | + "arrow_down": read_bitmap(ARROW_DOWN_CSV), |
| 54 | + "arrow_flat": read_bitmap(ARROW_FLAT_CSV), |
| 55 | + "arrow_up": read_bitmap(ARROW_UP_CSV), |
| 56 | + "light_outline": read_bitmap(LIGHT_OUTLINE_CSV), |
| 57 | + "light_rated": read_bitmap(LIGHT_RATED_CSV), |
| 58 | + "moon": read_bitmap(MOON_CSV), |
| 59 | + "sun": read_bitmap(SUN_CSV), |
| 60 | +} |
| 61 | + |
| 62 | +#------------------------------------------------------------------------------- |
| 63 | +# Drawing |
| 64 | +#------------------------------------------------------------------------------- |
| 65 | + |
| 66 | +def bitmap_width(name): |
| 67 | + """Width of an icon. |
| 68 | +
|
| 69 | + Args: |
| 70 | + name: Icon name, a key of ICONS. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + Width in pixels. |
| 74 | + """ |
| 75 | + return len(ICONS[name][0]) |
| 76 | + |
| 77 | +def bitmap_height(name): |
| 78 | + """Height of an icon. |
| 79 | +
|
| 80 | + Args: |
| 81 | + name: Icon name, a key of ICONS. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + Height in pixels. |
| 85 | + """ |
| 86 | + return len(ICONS[name]) |
| 87 | + |
| 88 | +def bitmap(name, color): |
| 89 | + """Draw an icon in a color. |
| 90 | +
|
| 91 | + Each horizontal run of lit pixels becomes one `render.Box`, which keeps the |
| 92 | + number of widgets low compared with drawing pixel by pixel. |
| 93 | +
|
| 94 | + Args: |
| 95 | + name: Icon name, a key of ICONS. |
| 96 | + color: Hex color for the lit pixels. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + A `render.Stack` exactly the size of the icon. |
| 100 | + """ |
| 101 | + rows = ICONS[name] |
| 102 | + children = [render.Box(width = bitmap_width(name), height = bitmap_height(name))] |
| 103 | + for y, row in enumerate(rows): |
| 104 | + start = None |
| 105 | + |
| 106 | + # The extra "0" closes a run that reaches the right edge. |
| 107 | + for x, cell in enumerate(row + ["0"]): |
| 108 | + if cell == "1" and start == None: |
| 109 | + start = x |
| 110 | + elif cell != "1" and start != None: |
| 111 | + children.append( |
| 112 | + render.Padding( |
| 113 | + pad = (start, y, 0, 0), |
| 114 | + child = render.Box(width = x - start, height = 1, color = color), |
| 115 | + ), |
| 116 | + ) |
| 117 | + start = None |
| 118 | + return render.Stack(children = children) |
0 commit comments