cg gallery wip vol2: commenting, minor code cleanup

This commit is contained in:
2021-06-16 21:54:10 +02:00
parent bd6c710380
commit f819958d80

View File

@ -1,51 +1,70 @@
init python: init python:
# GALLERY OBJECT
# Handles unlockables via ren'py
g = Gallery() g = Gallery()
# CONST PARAMS
GALLERY_COLS = 3
NOT_UNLOCKED_COVER = im.FactorScale("gui/gallery/unlocked_cg_button_cover.png", 0.225, 0.225)
gallery_cols = 3 # GALLERY ITEMS
gallery_items = [] # Data structure that holds the data for each cg and button
not_unlocked_cover = im.FactorScale("gui/gallery/unlocked_cg_button_cover.png", 0.225, 0.225) # item is the key in the Gallery
# { item: string; cg: Displayable; }[]
galleryItems = []
# Make a scaled cg button
# (cg: string, unlocked?: boolean): Displayable
def cg(fname, unlocked = False): def cg(fname, unlocked = False):
#if not unlocked: if not unlocked:
# return not_unlocked_cover return NOT_UNLOCKED_COVER
return im.FactorScale("images/cgs/" + fname + ".png", 0.225, 0.225) return im.FactorScale("images/cgs/" + fname + ".png", 0.225, 0.225)
def add_gallery_item(image_name, unlocked = False): # Create an object in g:Gallery, add to galleryItems
g.button(image_name) def addGalleryItem(imageName, unlocked = False):
g.image(image_name) g.button(imageName)
g.image(imageName)
if unlocked: if unlocked:
g.unlock(image_name) g.unlock(imageName)
else: else:
g.condition("persistent." + image_name) g.condition("persistent." + imageName)
gallery_items.append({ galleryItems.append({
"item": image_name, "item": imageName,
"cg": cg(image_name, unlocked) "cg": cg(imageName, unlocked)
}) })
# Reads /images/cgs dir for all .png files
# Populates g:Gallery and galleryItems
# Appends extra spaces at the end
def loadGallery():
from os import listdir, getcwd from os import listdir, getcwd
from os.path import isfile, join from os.path import isfile, join
cg_path = "images/cgs/" cgPath = "images/cgs/"
working_dir_path = getcwd().replace("\\", "/") workingDirPath = getcwd().replace("\\", "/")
cg_dir_path = working_dir_path + "/game/" + cg_path cgDirPath = workingDirPath + "/game/" + cgPath
for cgFile in listdir(cg_dir_path): # Add each .png to the gallery
if isfile(join(cg_dir_path, cgFile)): # TODO: make case insensitive
for cgFile in listdir(cgDirPath):
if isfile(join(cgDirPath, cgFile)):
if (cgFile[-4:] == '.png'): if (cgFile[-4:] == '.png'):
add_gallery_item(cgFile[0:-4], True) addGalleryItem(cgFile[0:-4], True)
extra_spaces = gallery_cols - (len(gallery_items) % gallery_cols) # Add empty items to fill grid after last cg button
for i in range(0, extra_spaces): extraSpaces = GALLERY_COLS - (len(galleryItems) % GALLERY_COLS)
gallery_items.append({ for i in range(0, extraSpaces):
galleryItems.append({
"item": None, "item": None,
"cg": None "cg": None
}) })
# Call to loading the gallery
loadGallery()
## CG Gallery screen ######################################################## ## CG Gallery screen ########################################################
@ -55,14 +74,15 @@ screen cg_gallery():
use game_menu(_("Gallery"), scroll="viewport"): use game_menu(_("Gallery"), scroll="viewport"):
fixed: fixed:
$ gallery_rows = len(gallery_items) / gallery_cols $ galleryRows = len(galleryItems) / GALLERY_COLS
grid gallery_cols gallery_rows: grid GALLERY_COLS galleryRows:
spacing gui.slot_spacing spacing gui.slot_spacing
for item in gallery_items: # Iterate through galleryItems and add cgs buttons
for item in galleryItems:
if item["item"] == None: if item["item"] == None:
# TODO: empty space # TODO: empty space
add g.make_button(gallery_items[0]["item"], gallery_items[0]["cg"], xalign = 0.5, yalign = 0.5) add g.make_button(galleryItems[0]["item"], galleryItems[0]["cg"], xalign = 0.5, yalign = 0.5)
else: else:
add g.make_button(item["item"], item["cg"], xalign = 0.5, yalign = 0.5) add g.make_button(item["item"], item["cg"], xalign = 0.5, yalign = 0.5)