Skip to content

Commit

Permalink
Generate pure-colour images dynamically, instead of reading them
Browse files Browse the repository at this point in the history
We don't need these PNG files in a directory.
  • Loading branch information
TeamSpen210 committed Nov 23, 2016
1 parent 60fff76 commit 7c1f707
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 28 deletions.
Binary file removed images/BEE2/alpha_64.png
Binary file not shown.
Binary file removed images/BEE2/black_64.png
Binary file not shown.
Binary file removed images/BEE2/black_96.png
Binary file not shown.
Binary file removed images/BEE2/blank.png
Binary file not shown.
Binary file removed images/BEE2/blank_96.png
Binary file not shown.
11 changes: 4 additions & 7 deletions src/UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -1395,11 +1395,10 @@ def init_preview(f):
borderwidth=0,
relief="solid",
)
blank = img.png('BEE2/blank')
pal_picked_fake = [
ttk.Label(
frames['preview'],
image=blank,
image=img.PAL_BG_64,
)
for _ in range(32)
]
Expand Down Expand Up @@ -1507,12 +1506,10 @@ def flow_picker(e=None):
)
frmScroll['height'] = height

# this adds extra blank items on the end to finish the grid nicely.
blank_img = img.png('BEE2/blank')

# This adds extra blank items on the end to finish the grid nicely.
for i in range(width):
if i not in pal_items_fake:
pal_items_fake.append(ttk.Label(frmScroll, image=blank_img))
pal_items_fake.append(ttk.Label(frmScroll, image=img.PAL_BG_64))
if (num_items % width) <= i < width: # if this space is empty
pal_items_fake[i].place(
x=((i % width)*65 + 1),
Expand All @@ -1535,7 +1532,7 @@ def init_drag_icon():
drag_win.bind(utils.EVENTS['LEFT_RELEASE'], drag_stop)
UI['drag_lbl'] = Label(
drag_win,
image=img.png('BEE2/blank'),
image=img.PAL_BG_64,
)
UI['drag_lbl'].grid(row=0, column=0)
windows['drag_win'] = drag_win
Expand Down
13 changes: 6 additions & 7 deletions src/contextWin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
from enum import Enum
import functools
import webbrowser
import itertools

from richTextBox import tkRichText
import img as png
import img
import sound as snd
import itemPropWin
import tkMarkdown
Expand Down Expand Up @@ -105,7 +104,7 @@ class SPR(Enum):
def set_sprite(pos, sprite):
"""Set one of the property sprites to a value."""
widget = wid['sprite', pos]
widget['image'] = png.spr(sprite)
widget['image'] = img.spr(sprite)
widget.tooltip_text = SPRITE_TOOL.get(sprite, '')


Expand Down Expand Up @@ -207,7 +206,7 @@ def load_item_data():

for ind, pos in enumerate(SUBITEM_POS[selected_item.num_sub]):
if pos == -1:
wid['subitem', ind]['image'] = png.png('BEE2/alpha_64')
wid['subitem', ind]['image'] = img.invis_square(64)
else:
wid['subitem', ind]['image'] = selected_item.get_icon(pos)
wid['subitem', ind]['relief'] = 'flat'
Expand Down Expand Up @@ -439,7 +438,7 @@ def init_widgets():
text="",
anchor="e",
compound="left",
image=png.spr('gear_ent'),
image=img.spr('gear_ent'),
)
wid['ent_count'].grid(row=0, column=2, rowspan=2, sticky=E)
tooltip.add_tooltip(
Expand All @@ -457,7 +456,7 @@ def init_widgets():
for i in range(5):
wid['subitem', i] = ttk.Label(
sub_frame,
image=png.png('BEE2/alpha_64'),
image=img.invis_square(64),
)
wid['subitem', i].grid(row=0, column=i)
utils.bind_leftclick(
Expand Down Expand Up @@ -485,7 +484,7 @@ def init_widgets():
for spr_id in SPR:
wid['sprite', spr_id] = sprite = ttk.Label(
spr_frame,
image=png.spr('ap_grey'),
image=img.spr('ap_grey'),
relief="raised",
)
sprite.grid(row=0, column=spr_id.value)
Expand Down
35 changes: 29 additions & 6 deletions src/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
from srctools import Vec
import utils

from typing import Dict, Tuple
from typing import Union, Dict, Tuple

LOGGER = utils.getLogger('img')

cached_img = {} # type: Dict[Tuple[str, int], ImageTk.PhotoImage]
# r, g, b, size -> image
cached_squares = {} # type: Dict[Tuple[float, float, float, int], ImageTk.PhotoImage]
cached_squares = {} # type: Dict[Union[Tuple[float, float, float, int], Tuple[str, int]], ImageTk.PhotoImage]

# Colour of the palette item background
PETI_ITEM_BG = Vec(229, 232, 233)

def png(path, resize_to=None, error=None, algo=Image.NEAREST):

def png(path, resize_to=0, error=None, algo=Image.NEAREST):
"""Loads in an image for use in TKinter.
- The .png suffix will automatically be added.
Expand All @@ -35,7 +38,7 @@ def png(path, resize_to=None, error=None, algo=Image.NEAREST):
orig_path = path

try:
return cached_img[orig_path, resize_to]
return cached_img[path, resize_to]
except KeyError:
pass

Expand Down Expand Up @@ -106,10 +109,30 @@ def color_square(color: Vec, size=16):
color=(int(color.x), int(color.y), int(color.z)),
)
tk_img = ImageTk.PhotoImage(image=img)
cached_squares[color.as_tuple(), size] = tk_img
cached_squares[key] = tk_img


def invis_square(size):
"""Create a square image of the given size, filled with 0-alpha pixels."""

try:
return cached_squares['alpha', size]
except KeyError:
img = Image.new(
mode='RGBA',
size=(size, size),
color=(0, 0, 0, 0),
)
tk_img = ImageTk.PhotoImage(image=img)
cached_squares['alpha', size] = tk_img

return tk_img

BLACK_64 = color_square(Vec(0, 0, 0), size=64)
BLACK_96 = color_square(Vec(0, 0, 0), size=96)
PAL_BG_64 = color_square(PETI_ITEM_BG, size=64)
PAL_BG_96 = color_square(PETI_ITEM_BG, size=96)

# If image is not readable, use this instead
img_error = png('BEE2/error')
# If this actually fails, use the black image.
img_error = png('BEE2/error', error=BLACK_64)
21 changes: 13 additions & 8 deletions src/selectorWin.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,16 @@ def __init__(
else:
self.context_lbl = self.longName
if icon is None:
self.icon = img.png(
'BEE2/blank_96',
error=err_icon,
resize_to=ICON_SIZE,
self.icon = img.color_square(
img.PETI_ITEM_BG,
ICON_SIZE,
)
self.ico_file = None
elif icon == '<black>':
self.icon = img.color_square(
Vec(),
ICON_SIZE,
)
self.ico_file = 'BEE2/blank_96'
else:
self.icon = img.png(
icon,
Expand Down Expand Up @@ -547,9 +551,10 @@ def __init__(
)
self.prop_icon_frm.grid(row=0, column=0, columnspan=4)

self.prop_icon = ttk.Label(self.prop_icon_frm)
self.prop_icon.img = img.png('BEE2/blank_96')
self.prop_icon['image'] = self.prop_icon.img
self.prop_icon = ttk.Label(
self.prop_icon_frm,
image=img.PAL_BG_96,
)
self.prop_icon.grid(row=0, column=0)

name_frame = ttk.Frame(self.prop_frm)
Expand Down

0 comments on commit 7c1f707

Please sign in to comment.