After making it and comparing the code, it looks like the core logic is the same (means I'm not too dense), and they both achieve the following:
- Customisation grid size
- Auto grid population
- Pagination
- Multiple images can be assigned per thumbnail
- Proportional scaling used on thumbnails
- Thumbnails reference image path directly, so more customised thumbnails can be used
- Custom locked thumbnail can be used for each image, otherwise generic locked thumbnail would be used
- Auto thumbnail size calculation based on grid size and margin values supplied. This is necessary for me as I have images of various aspect ratios
- Name labels for thumbnails to indicate what they are
- Persistent cheat code used to unlock whole gallery by default (you need your own code to set the persistent variable)
- Previous as well as next page buttons
Replace the ProportionalScale function with this:
Code: Select all
def ProportionalScale(img, maxwidth, maxheight):
currentwidth, currentheight = renpy.image_size(img)
xscale = float(maxwidth) / float(currentwidth)
yscale = float(maxheight) / float(currentheight)
if xscale < yscale:
minscale = xscale
else:
minscale = yscale
return im.FactorScale(img,minscale,minscale)
Now onto the gallery code.
Initialise the gallery:
Code: Select all
init python:
from collections import OrderedDict
gallery_items = OrderedDict() # maintain dictionary order so you can assign custom key names
gallery_page = 1
# Grid values
gallery_xgrid = 3
gallery_ygrid = 2
gallery_page_margin = 30 #for things like buttons at the top
gallery_thumb_margin = 50 #for things like thumbnail heading and spacing
# Work out the thumbnail bounding size
gallery_xmin = ((config.screen_width - gallery_page_margin) / gallery_xgrid) - gallery_thumb_margin
gallery_ymin = ((config.screen_height - gallery_page_margin) / gallery_ygrid) - gallery_thumb_margin
# Locked image thumbnail
gallery_locked_thumb = "gal/locked.png"
# Your gallery items here
gallery_items["scene 1"] = {
"name":"Scene 001",
"thumb": "scene/scene001.jpg",
"images" :["scene001a","scene001b","scene001c"],
"locked" : "scene/scene001-locked.jpg"
}
gallery_items["scene 2"] = {
"name":"Scene 002",
"thumb": "scene/scene002.jpg",
"images" :["scene002"]
}
gallery_items["scene 3"] = {
"name":"Scene 003",
"thumb": "scene/scene003.jpg",
"images" :["scene003"]
}
g = Gallery()
for key, gallery_item in gallery_items.items():
g.button(key)
if persistent.unlock_gallery: # Cheat code variable
g.condition("True")
for p_image in gallery_item["images"]:
g.image(p_image)
else:
for p_image in gallery_item["images"]:
g.unlock_image(p_image)
g.transform(truecenter) # center image if smaller than full screen
if not "locked" in gallery_item:
gallery_items[key]["locked"] = gallery_locked_thumb
g.transition = dissolve # choose your transition type
Code: Select all
screen gallery:
tag menu
add "bg black"
if gallery_page > 1:
textbutton "{size=24}Previous Page" action [SetVariable('gallery_page', gallery_page-1), ShowMenu("gallery")] xalign 0.25 yalign 0.0 ypadding 10 xpadding 20
if (gallery_page*gallery_xgrid*gallery_ygrid)<len(gallery_items):
textbutton "{size=24}Next Page" action [SetVariable('gallery_page', gallery_page+1), ShowMenu("gallery")] xalign 0.75 yalign 0.0 ypadding 10 xpadding 20
vbox:
spacing 10
textbutton "{size=24}Return" action Return() xalign 0.5 yalign 0.05 ypadding 10 xpadding 20
grid gallery_xgrid gallery_ygrid:
xfill True
yfill True
$ gallery_item_start = (gallery_page - 1)*gallery_xgrid*gallery_ygrid
$ gallery_item_end = gallery_item_start + gallery_xgrid*gallery_ygrid - 1
$ gallery_item_pointer = 0
for key, gallery_item in gallery_items.items():
if gallery_item_start <= gallery_item_pointer <= gallery_item_end:
vbox:
spacing 2
xalign 0.5
yalign 0.5
text gallery_item["name"] xalign 0.5
add g.make_button(key, ProportionalScale(gallery_item["thumb"], gallery_xmin, gallery_ymin), ProportionalScale(gallery_item["locked"], gallery_xmin, gallery_ymin), xalign=0.5, yalign = 0.5, bottom_margin=15)
$ gallery_item_pointer += 1
if gallery_item_pointer < gallery_item_end:
for i in range(0, gallery_item_end - gallery_item_pointer + 1):
null
kivik