From 3ce862d294f50ff33193067a002177931dcd33c6 Mon Sep 17 00:00:00 2001 From: Callan Barrett Date: Sun, 20 Apr 2014 20:21:24 +0800 Subject: [PATCH] Fix modded race rendering, new way to make item descs --- starcheat/assets.py | 12 +++--- starcheat/gui/appearance.py | 11 ++++- starcheat/gui/itembrowser.py | 67 +++++++++++++++++++++++++----- starcheat/gui/itemedit.py | 21 ++++------ starcheat/gui/mainwindow.py | 11 ++++- starcheat/templates/ItemBrowser.ui | 43 +++++++------------ 6 files changed, 105 insertions(+), 60 deletions(-) diff --git a/starcheat/assets.py b/starcheat/assets.py index 8d34028..a46344d 100644 --- a/starcheat/assets.py +++ b/starcheat/assets.py @@ -473,7 +473,7 @@ def get_item_image(self, name): icon_file = item[0]["image"] icon = icon_file.split(':') icon = icon[0] - except KeyError: + except (KeyError, TypeError): logging.warning("No image key for "+name) return None @@ -888,15 +888,16 @@ def get_preview_image(self, name, gender): def render_player(self, player): name = player.get_race() gender = player.get_gender() + asset_loc = self.get_species(name)[0][1] body_sprites = self.assets.read("/humanoid/%s/%sbody.png" % (name, gender), - self.assets.vanilla_assets, True) + asset_loc, True) frontarm_sprites = self.assets.read("/humanoid/%s/frontarm.png" % name, - self.assets.vanilla_assets, True) + asset_loc, True) backarm_sprites = self.assets.read("/humanoid/%s/backarm.png" % name, - self.assets.vanilla_assets, True) + asset_loc, True) head_sprites = self.assets.read("/humanoid/%s/%shead.png" % (name, gender), - self.assets.vanilla_assets, True) + asset_loc, True) body_img = Image.open(BytesIO(body_sprites)).crop((43, 0, 86, 43)) frontarm_img = Image.open(BytesIO(frontarm_sprites)).crop((43, 0, 86, 43)) @@ -920,7 +921,6 @@ def render_player(self, player): base.paste(body_img, mask=body_img) base.paste(frontarm_img, mask=frontarm_img) - return base def get_hair_image(self, name, hair_type, hair_group, gender): diff --git a/starcheat/gui/appearance.py b/starcheat/gui/appearance.py index b5695c0..ce44671 100644 --- a/starcheat/gui/appearance.py +++ b/starcheat/gui/appearance.py @@ -102,9 +102,16 @@ def write_appearance_values(self): self.player.set_facial_hair_directives(self.colors["facial_hair"]) self.player.set_facial_mask_directives(self.colors["facial_mask"]) - image = self.assets.species().render_player(self.player) - pixmap = QPixmap.fromImage(ImageQt(image)).scaled(86, 86) + # render player preview + try: + image = self.assets.species().render_player(self.player) + pixmap = QPixmap.fromImage(ImageQt(image)).scaled(86, 86) + except (OSError, TypeError, AttributeError): + logging.exception("Couldn't load species images") + pixmap = QPixmap() + self.ui.player_preview.setPixmap(pixmap) + self.main_window.window.setWindowModified(True) def new_color_edit(self, type): diff --git a/starcheat/gui/itembrowser.py b/starcheat/gui/itembrowser.py index dcbae2a..9c0cc9d 100644 --- a/starcheat/gui/itembrowser.py +++ b/starcheat/gui/itembrowser.py @@ -10,12 +10,66 @@ import assets, qt_itembrowser from config import Config +def format_status_effects(data): + info = "Status Effects:
" + for status in data: + if "amount" in status: + info += "%s (%s)
" % (status["kind"], str(status["amount"])) + else: + info += "%s
" % status["kind"] + return info + +def format_effects(data): + info = "Effects:
" + for status in data[0]: + if "amount" in status: + info += "%s (%s)
" % (status["kind"], str(status["amount"])) + else: + info += "%s
" % status["kind"] + return info + +data_format = ( + # (key name, format/func) + # key name is name of the key in item data it corresponds to + # format/func is either a format string with 1 %s replacement that the + # key gets passed to or a function that outputs a string using the option + # as input + # order matters + ("shortdescription", "%s "), + ("itemName", "(%s)
"), + ("objectName", "(%s)
"), + ("description", "%s

"), + ("inspectionKind", "Type: %s
"), + ("rarity", "Rarity: %s

"), + ("statusEffects", format_status_effects), + ("effects", format_effects), + ("blockRadius", "Mining Radius: %s blocks") +) + +def generate_item_info(item_data): + """Takes inventory item data and makes a detailed description (HTML).""" + info = "" + + if item_data == None: + return "" + + for fmt in data_format: + if fmt[0] in item_data: + if type(fmt[1]) is str: + info += fmt[1] % str(item_data[fmt[0]]) + else: + info += fmt[1](item_data[fmt[0]]) + + return info + + class BrowserItem(QListWidgetItem): def __init__(self, name, desc): if desc == "": display = name else: - display = "%s (%s)" % (desc, name) + #display = "%s (%s)" % (desc, name) + display = desc QListWidgetItem.__init__(self, display) self.name = name @@ -87,16 +141,7 @@ def update_item_view(self): logging.warning("Unable to load item image: "+selected) self.ui.item_icon.setPixmap(QPixmap()) - # TODO: update qt objectnames, already not making sense - try: - self.ui.item_name.setText(item[0]["shortdescription"]) - except KeyError: - self.ui.item_name.setText("Missing short description") - - try: - self.ui.short_desc.setText(item[0]["description"]) - except KeyError: - self.ui.short_desc.setText("Missing description") + self.ui.short_desc.setText(generate_item_info(item[0])) # populate default variant table row = 0 diff --git a/starcheat/gui/itemedit.py b/starcheat/gui/itemedit.py index 95febce..e614823 100644 --- a/starcheat/gui/itemedit.py +++ b/starcheat/gui/itemedit.py @@ -15,7 +15,7 @@ import assets, qt_itemedit, qt_itemeditoptions, saves from gui.common import inv_icon, ItemWidget, empty_slot -from gui.itembrowser import ItemBrowser +from gui.itembrowser import ItemBrowser, generate_item_info from config import Config class ItemEditOptions(): @@ -112,18 +112,15 @@ def __init__(self, parent, item, player, browser_category=""): def update_item_info(self, name, data): item_info = "" - try: - item_info += "" + data["shortdescription"] + "" - except KeyError: - try: - item_info += "" + self.assets.items().get_item(name)[0]["shortdescription"] + "" - except: - pass + #try: + # item_info += "" + data["shortdescription"] + "
" + #except KeyError: + # try: + # item_info += "" + self.assets.items().get_item(name)[0]["shortdescription"] + "
" + # except: + # pass - try: - item_info += "

" + data["description"] + "

" - except KeyError: - pass + item_info += generate_item_info(data) item_info += "" self.ui.desc.setText(item_info) diff --git a/starcheat/gui/mainwindow.py b/starcheat/gui/mainwindow.py index c263d87..d6027a9 100644 --- a/starcheat/gui/mainwindow.py +++ b/starcheat/gui/mainwindow.py @@ -450,8 +450,15 @@ def update_bag(self, bag_name): column = 0 def update_player_preview(self): - image = self.assets.species().render_player(self.player) - pixmap = QPixmap.fromImage(ImageQt(image)).scaled(86, 86) + try: + image = self.assets.species().render_player(self.player) + pixmap = QPixmap.fromImage(ImageQt(image)).scaled(86, 86) + except (OSError, TypeError, AttributeError): + # TODO: more specific error handling. may as well except all errors + # at this point jeez + logging.exception("Couldn't load species images") + pixmap = QPixmap() + self.ui.player_preview.setPixmap(pixmap) self.window.setWindowModified(True) diff --git a/starcheat/templates/ItemBrowser.ui b/starcheat/templates/ItemBrowser.ui index 702492d..4d32b2e 100644 --- a/starcheat/templates/ItemBrowser.ui +++ b/starcheat/templates/ItemBrowser.ui @@ -6,8 +6,8 @@ 0 0 - 616 - 386 + 600 + 350 @@ -87,30 +87,6 @@ - - - - 0 - 0 - - - - - 16777215 - 15 - - - - - true - - - - item_name - - - - @@ -138,7 +114,7 @@ - + @@ -162,6 +138,19 @@ + + + + Qt::Vertical + + + + 20 + 18 + + + +