﻿init -97 python:
    try:
        mmm.add(
            gui0_menu(
                _("gallery"),
                ShowMenu("gallery"),
                True, False, True
                ),
            )
    except:
        pass


# --------------------------------------------- Classes
init python:
    class gallpic:
        def __init__(self, name, img, locked = True, icon = None, info = "", align = (0.5, 1.0)):
            self.name = name
            self.img = img
            self.icon = icon
            self.info = info
            self.locked = locked
            self.align = align

    class gallery_class:
        def __init__(self, lst = [], thumbs = 6, type = "thumbs"):
            self.lst = lst
            self.current = 0
            self.slc = False
            self.thumbs = thumbs
            self.currentPage = 0
            self.type = type

        def add(self, x):
            self.lst.append(x)

        def next(self):
            if self.current < len(self.lst)-1:
                self.current += 1
        def back(self):
            if self.current > 0:
                self.current -= 1
        def set(self, x):
            self.current = x
            self.slc = True
        def unset(self):
            self.slc = False
        def nextPage(self):
            if self.currentPage + self.thumbs < len(self.lst):
                self.currentPage += self.thumbs
                self.current = self.currentPage
        def pervPage(self):
            if self.currentPage - self.thumbs > -1:
                self.currentPage -= self.thumbs
                self.current = self.currentPage
        def toggle(self):
            if self.type == "thumbs":
                self.type = "simple"
            else:
                self.type = "thumbs"
        def thumbsless(self):
            if self.thumbs - 1 > 0:
                self.thumbs -= 1
        def thumbsmore(self):
            if self.thumbs + 1 <  len(self.lst)+1:
                self.thumbs += 1



# --------------------------------------------- Define your images here
define gall_bg = gallpic(
    name = "1- Background",
    img = "demo/bg.jpg",
    locked = False,
    icon = "gall/bg.jpg",
    info = "A simple background.",
    align = (0.5, 1.0)
    )
define gall_ayame = gallpic(
    "2- Ayame",
    "demo/Ayame Errkk.png",
    False,
    "gall/Ayame Errkk.jpg",
    "Ayume is the shy girl.",
    )
define gall_nana = gallpic(
    "3- Nana",
    "demo/Nana Confident.png",
    False,
    "gall/Nana Confident.jpg",
    "Nana is the wild girl.",
    )
define gall_icon = gallpic(
    "4- Background",
    "gui/window_icon.png",
    False,
    "gall/icon.png",
    "It's the games icon.",
    (.5,.5)
    )
define gall_abdul = gallpic(
    "5- Abdul",
    "gall/confused.png",
    False,
    "gall/confused - icon.png",
    "A guest appearance?.",
    )
define gall_test = gallpic(
    "6- I don't know",
    "gall/test.png",
    False,
    "gall/test icon.png",
    "eehh!.",
    (.5,.5)
    )
define gall_4 = gallpic(
    "6- Some render",
    "gall/4.png",
    False,
    "gall/4icon.png",
    "Nana is the wild girl.",
    )
define gall_nana_locked = gallpic(
    "7- Nana",
    "demo/Nana Confident.png",
    True,
    "gall/Nana Confident.jpg",
    "Nana is the wild girl.",
    )
# --------------------------------------------- Define a gallery and add images/settings to it
default gallery_ins = gallery_class(
    [gall_bg, gall_ayame, gall_nana, gall_icon, gall_abdul, gall_test, gall_4, gall_nana_locked],
    6
    )


# --------------------------------------------- The screens
screen gallery(g=gallery_ins):
    tag menu
    modal True
    default hovbar = 0
    default settings = 0

    # ----- BackGround
    add "#666"

    # ----- the key navigation
    key 'd' action Function(g.next)
    key 'a' action Function(g.back)

    # ----- Selector
    if g.type == "thumbs":
        use gallery_thumbnail
    elif g.type == "simple":
        use gallery_simple

    # ----- the floating navigation
    if hovbar:
        drag:
            frame:
                has hbox
                textbutton _("✖") action SetScreenVariable("hovbar", 0) at btn align(.5,1.0)
                button:
                    text "<"
                    action Function(g.back)
                button:
                    text ">"
                    action Function(g.next)
                text "{} - {}".format(g.lst[g.current].name, g.lst[g.current].info)

    # ----- the return button
    hbox:
        align 0.5,1.0
        textbutton _("⚙") action ToggleScreenVariable("settings", 1, 0) at btn

        textbutton _("✖") action Return() at btn

    # ----- settings
    if settings:
        drag:
            yalign .8
            frame:
                has vbox
                hbox:
                    textbutton _("✖") action SetScreenVariable("settings", 0) at btn align(.5,1.0)
                    textbutton _("▬") action ToggleScreenVariable("hovbar", 1, 0) at btn
                    textbutton _("⚏") action Function(g.toggle) at btn
                text "Number of thumbnails per page"
                hbox:
                    button:
                        text "<"
                        action Function(g.thumbsless)
                    text "{}".format(g.thumbs)
                    button:
                        text ">"
                        action Function(g.thumbsmore)
                
                
# -------------------------------- Simple Gallery
screen gallery_simple:
    # ----- the image
    if g.lst[g.current].locked:
        vbox:
            text "⚷" size 200 color "#d00"
            text "Locked"
    else:
        add g.lst[g.current].img align g.lst[g.current].align
    frame:
        align(.5,0.0)
        text "{} - {}".format(g.lst[g.current].name, g.lst[g.current].info)

    # ----- the side buttons
    button:
        style "gal"
        xalign 0.0
        action Function(g.back)
    button:
        style "gal"
        xalign 1.0
        action Function(g.next)



# -------------------------------- Thumbnail Gallery
screen gallery_thumbnail:

    # ----- the thumbnails
    hbox:
        box_wrap True spacing 20 box_wrap_spacing 20
        for i in range(g.thumbs):
            if i+(int(g.current/g.thumbs)*g.thumbs) < len(g.lst):
                if g.lst[i+(int(g.current/g.thumbs)*g.thumbs)].locked:
                    button:
                        fixed:
                            fit_first True
                            add g.lst[i+(int(g.current/g.thumbs)*g.thumbs)].icon
                            vbox:
                                text "⚷" size 200 color "#d00"
                                text "Locked"
                else:
                    button:
                        at btn
                        add g.lst[i+(int(g.current/g.thumbs)*g.thumbs)].icon
                        action Function(g.set, i+(int(g.current/g.thumbs)*g.thumbs)), SelectedIf(i+(int(g.current/g.thumbs)*g.thumbs) == g.current)

    # ----- the side buttons
    button:
        style "gal"
        xalign 0.0
        action Function(g.pervPage)
    button:
        style "gal"
        xalign 1.0
        action Function(g.nextPage)

    # ----- the image
    if g.slc:
        button:
            align(.5,1.0) background "#000d" padding 0,0 xfill True yfill True
            add g.lst[g.current].img align g.lst[g.current].align
            action Function(g.unset)
        frame:
            align(.5,0.0)
            text "{} - {}".format(g.lst[g.current].name, g.lst[g.current].info)



style gal:
    idle_background None
    hover_background "#fff3"
    yfill True
    xsize 400
    