Skip to content

Commit

Permalink
* Refactor resource icon code a bit more
Browse files Browse the repository at this point in the history
Now by default checks whether the icon path is valid
and returns placeholder paths if problems were found.
  • Loading branch information
ChrisOelmueller committed Apr 16, 2012
1 parent c2138c1 commit 6caf5ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
25 changes: 14 additions & 11 deletions horizons/gui/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os

from fife.extensions import pychan
from fife.extensions.pychan.widgets import Icon

from horizons.i18n import translate_widget
from horizons.util.python import decorators, Callback
Expand Down Expand Up @@ -69,10 +70,10 @@ def load_uh_widget(filename, style=None, center_widget=False):

return widget

@decorators.cachedfunction
def get_res_icon_path(res, size, greyscale=False):
"""Returns icons of a resource
"""Returns path of a resource icon or placeholder path, if icon does not exist.
@param res: resource id. Pass 'placeholder' to get placeholder path.
@return: tuple: (icon_50_path, icon_disabled_path, icon_24_path, icon_16_path, icon_32_path)
"""
icon_path = 'content/gui/icons/resources/{size}/'.format(size=size)
if greyscale:
Expand All @@ -81,23 +82,25 @@ def get_res_icon_path(res, size, greyscale=False):
icon_path = icon_path + 'placeholder.png'
else:
icon_path = icon_path + '{res:03d}.png'.format(res=res)
try:
Icon(image=icon_path)
except RuntimeError: # ImageManager: image not found, use placeholder or die
if res == 'placeholder':
raise Exception('Image not found: {icon_path}'.format(icon_path=icon_path))
else:
print '[WW] Image not found: {icon_path}'.format(icon_path=icon_path)
icon_path = get_res_icon_path('placeholder', size)
return icon_path

def create_resource_icon(res_id, db, size=50):
"""Creates a pychan Icon for a resource. Helptext is set to res name.
Returns None if size parameter is invalid.
"""Creates a pychan Icon for a resource. Helptext is set to name of *res_id*.
Returns None if *size* parameter is invalid (not one of 16, 24, 32, 50).
@param res_id: resource id
@param db: dbreader for main db
@param size: Size of icon in px. Valid: 16, 24, 32, 50."""
from fife.extensions.pychan.widgets import Icon
widget = None
if size in (16, 24, 32, 50):
icon_path = get_res_icon_path(res_id, size)
try:
widget = Icon(image=icon_path)
except RuntimeError: # ImageManager: image not found, use placeholder
print '[WW] Image not found: {icon_path}'.format(icon_path=icon_path)
widget = Icon(image=get_res_icon_path('placeholder', size))
widget = Icon(image=get_res_icon_path(res_id, size))
widget.helptext = db.get_res_name(res_id)
return widget

Expand Down
10 changes: 3 additions & 7 deletions horizons/gui/widgets/imagefillstatusbutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,12 @@ def init_for_res(cls, db, res, amount=0, filled=0, use_inactive_icon=True, uncac
@param res: resource id
@param amount: int amount of res (used to decide inactiveness and as text)
@param filled: percent of fill status (values are ints in [0, 100])
@param use_inactive_icon: wheter to use inactive icon if amount == 0
@param use_inactive_icon: whether to use inactive icon if amount == 0
@param uncached: force no cache. see __init__()
@return: ImageFillStatusButton instance"""
icon = get_res_icon_path('placeholder', size=32)
if use_inactive_icon:
icon_disabled = get_res_icon_path(res, size=50, greyscale=True)
else:
icon_disabled = icon
greyscale = use_inactive_icon and amount == 0
image = get_res_icon_path(res, 32, greyscale)
helptext = db.get_res_name(res)
image = icon_disabled if amount == 0 else icon
return cls(up_image=image, down_image=image, hover_image=image,
text=unicode(amount),
helptext=helptext,
Expand Down

0 comments on commit 6caf5ac

Please sign in to comment.