Картинки в кнопках выбора

Материал из Ren'Py Wiki
Перейти к навигации Перейти к поиску
Choice.jpg

init -2 python:
    # автоматическое объявление картинок
    config.automatic_images_minimum_components = 1
    config.automatic_images = [' ', '_', '/']
    config.automatic_images_strip = ['images']

init:
    # пустая картинка
    image icn empty = Null(48, 48)

init python:
    # отступы для кнопок выбора
    pad = 8
    # выравнивание текста кнопки выбора по умолчанию
    default_align = .0 # по центру  .5
    style.menu_choice_button.left_padding = pad
    style.menu_choice_button.right_padding = pad
    style.menu_choice_button.top_padding = pad
    style.menu_choice_button.bottom_padding = pad

    # вспомогательные функции для вырезания имен картинок
    def get_left(s, divider = "|"):
        res = s
        tmp = s.split(divider)
        if len(tmp) > 1:
            res = tmp[0]
        return res

    def get_right(s, divider = "|"):
        res = ""
        tmp = s.split(divider)
        if len(tmp) > 1:
            res = tmp[1]
        return res

    def get_align(s, divider = "|"):
        res = default_align
        tmp = s.split(divider)
        if len(tmp) > 2:
            res = float(tmp[2])
        return res

# экран меню выборов в игре
screen choice:
    window:
        style "menu_window"
        xalign 0.5
        yalign 0.5
        vbox:
            style "menu"
            spacing pad
            for caption, action, chosen in items:
                if action:
                    button:
                        action action
                        style "menu_choice_button"
                        hbox:
                            # добавление картинок при необходимости
                            if get_right(caption) != "":
                                text "{image=%s}" % (get_right(caption))
                            hbox:
                                yalign .5 # центрируем текст по высоте кнопки
                                xfill True
                                text get_left(caption) style "menu_choice" xalign get_align(caption)
                else:
                    text caption style "menu_caption"

label start:
    scene black
    menu:
        "С картинкой, по центру|icn prefs|.5":
            pass
        "С картинкой|icn vk":
            pass
        "С картинкой (пустой)|icn empty":
            pass
        "Без картинки, по центру||.5":
            pass
    return

На главную страницу Книга рецептов Энциклопедии Ren'Py Renpy для чайников Вернуться к началу статьи