Advertisement
Guest User

Seeds of Chaos gallery unlock

a guest
Jan 17th, 2021
11,223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.87 KB | None | 0 0
  1. # Copyright 2004-2019 Tom Rothamel <[email protected]>
  2. #
  3. # Permission is hereby granted, free of charge, to any person
  4. # obtaining a copy of this software and associated documentation files
  5. # (the "Software"), to deal in the Software without restriction,
  6. # including without limitation the rights to use, copy, modify, merge,
  7. # publish, distribute, sublicense, and/or sell copies of the Software,
  8. # and to permit persons to whom the Software is furnished to do so,
  9. # subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be
  12. # included in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. init -1500 python:
  23.  
  24.     class __GalleryAllPriorCondition(object):
  25.  
  26.         def check(self, all_prior):
  27.             return all_prior
  28.  
  29.  
  30.     class __GalleryArbitraryCondition(object):
  31.         def __init__(self, condition):
  32.             self.condition = condition
  33.  
  34.         def check(self, all_prior):
  35.             return eval(self.condition)
  36.  
  37.  
  38.     class __GalleryUnlockCondition(object):
  39.         def __init__(self, images):
  40.             self.images = images
  41.  
  42.         def check(self, all_prior):
  43.             for i in self.images:
  44.  
  45.                 print(i, renpy.seen_image(i))
  46.  
  47.                 if not renpy.seen_image(i):
  48.                     return True
  49.  
  50.             return True
  51.  
  52.  
  53.     class __GalleryImage(object):
  54.         def __init__(self, gallery, displayables):
  55.  
  56.             # The gallery object we belong to.
  57.             self.gallery = gallery
  58.  
  59.             # A list of conditions for this image to be displayed.
  60.             self.conditions = [ ]
  61.  
  62.             # A list of displayables to show.
  63.             self.displayables = displayables
  64.  
  65.             # A list of transforms to apply to those displayables, or None
  66.             # to not apply a transform.
  67.             self.transforms = [ None ] * len(displayables)
  68.  
  69.  
  70.         def check_unlock(self, all_prior):
  71.             """
  72.            Returns True if the image is unlocked.
  73.            """
  74.  
  75.             for i in self.conditions:
  76.                 if not i.check(all_prior):
  77.                     return False
  78.  
  79.             return True
  80.  
  81.         def show(self, locked, index, count):
  82.             """
  83.            Shows this image when it's unlocked.
  84.            """
  85.  
  86.             renpy.transition(self.gallery.transition)
  87.             ui.saybehavior()
  88.  
  89.             displayables = [ ]
  90.  
  91.             for d, transform in zip(self.displayables, self.transforms):
  92.  
  93.                 if transform is not None:
  94.                     d = transform(d)
  95.                 else:
  96.                     d = config.default_transform(d)
  97.  
  98.                 d = renpy.display.layout.AdjustTimes(d, None, None)
  99.  
  100.                 displayables.append(d)
  101.  
  102.             renpy.show_screen("_gallery", locked=locked, index=index + 1, count=count, displayables=displayables, gallery=self.gallery)
  103.  
  104.             return ui.interact()
  105.  
  106.  
  107.     class __GalleryButton(object):
  108.         def __init__(self, gallery, index):
  109.             self.gallery = gallery
  110.             self.images = [ ]
  111.             self.conditions = [ ]
  112.             self.index = index
  113.  
  114.         def check_unlock(self):
  115.             for i in self.conditions:
  116.                 if not i.check(True):
  117.                     return False
  118.  
  119.             for i in self.images:
  120.                 if i.check_unlock(False):
  121.                     return True
  122.  
  123.             return False
  124.  
  125.  
  126.     @renpy.pure
  127.     class __GalleryToggleSlideshow(Action, FieldEquality):
  128.  
  129.         identity_fields = [ "gallery" ]
  130.  
  131.         def __init__(self, gallery):
  132.             self.gallery = gallery
  133.  
  134.         def __call__(self):
  135.             self.gallery.slideshow = not self.gallery.slideshow
  136.             renpy.restart_interaction()
  137.  
  138.         def get_selected(self):
  139.             return self.gallery.slideshow
  140.  
  141.     @renpy.pure
  142.     class __GalleryAction(Action, FieldEquality):
  143.  
  144.         identity_fields = [ "gallery" ]
  145.         equality_fields = [ "index" ]
  146.  
  147.         def __init__(self, gallery, index):
  148.             self.gallery = gallery
  149.             self.index = index
  150.  
  151.         def __call__(self):
  152.             renpy.invoke_in_new_context(self.gallery.show, self.index)
  153.  
  154.     class Gallery(object):
  155.         """
  156.        :doc: gallery class
  157.  
  158.        This class supports the creation of an image gallery by handling the
  159.        locking of images, providing an action that can show one or more images,
  160.        and a providing method that creates buttons that use that action.
  161.  
  162.        .. attribute:: transition
  163.  
  164.            The transition that is used when changing images.
  165.  
  166.        .. attribute:: locked_button
  167.  
  168.            The default displayable used by make_button for a locked button.
  169.  
  170.        .. attribute:: hover_border
  171.  
  172.            The default hover border used by make_button.
  173.  
  174.        .. attribute:: idle_border
  175.  
  176.            The default idle border used by make_button.
  177.  
  178.        .. attribute:: unlocked_advance
  179.  
  180.            If true, the gallery will only advance through unlocked images.
  181.  
  182.        .. attribute:: navigation
  183.  
  184.            If true, the gallery will display navigation and slideshow
  185.            buttons on top of the images.
  186.  
  187.            To customize the look of the navigation, you may override the
  188.            gallery_navigation screen. The default screen is defined in
  189.            common/00gallery.rpy
  190.  
  191.        .. attribute:: span_buttons
  192.  
  193.            If true, the gallery will advance between buttons.
  194.  
  195.        .. attribute:: slideshow_delay
  196.  
  197.            The time it will take for the gallery to advance between images
  198.            in slideshow mode.
  199.        """
  200.  
  201.         transition = None
  202.  
  203.         hover_border = None
  204.         idle_border = None
  205.  
  206.         locked_button = None
  207.  
  208.         def __init__(self):
  209.  
  210.             # A map from button name (or image) to __GalleryButton object.
  211.             self.buttons = { }
  212.  
  213.             # A list of buttons.
  214.             self.button_list = [ ]
  215.  
  216.             self.button_ = None
  217.             self.image_ = None
  218.  
  219.             self.unlockable = None
  220.  
  221.             self.unlocked_advance = False
  222.  
  223.             self.navigation = False
  224.  
  225.             self.span_buttons = False
  226.  
  227.             self.slideshow_delay = 5
  228.  
  229.             self.slideshow = False
  230.  
  231.         def button(self, name):
  232.             """
  233.            :doc: gallery method
  234.  
  235.            Creates a new button, named `name`.
  236.  
  237.            `name`
  238.                The name of the button being created.
  239.            """
  240.  
  241.             button = __GalleryButton(self, len(self.button_list))
  242.  
  243.             self.unlockable = button
  244.             self.buttons[name] = button
  245.             self.button_list.append(button)
  246.             self.button_ = button
  247.  
  248.         def image(self, *displayables):
  249.             """
  250.            :doc: gallery method
  251.  
  252.            Adds a new image to the current button, where an image consists
  253.            of one or more displayables.
  254.            """
  255.  
  256.             self.image_ = __GalleryImage(self, displayables)
  257.             self.button_.images.append(self.image_)
  258.             self.unlockable = self.image_
  259.  
  260.         display = image
  261.  
  262.         def transform(self, *transforms):
  263.             """
  264.            :doc: gallery method
  265.  
  266.            Applies transforms to the last image registered. This should be
  267.            called with the same number of transforms as the image has
  268.            displayables. The transforms are applied to the corresponding
  269.            displayables.
  270.  
  271.            If a transform is None, the default transform is used.
  272.            """
  273.  
  274.             self.image_.transforms = transforms
  275.  
  276.         def unlock(self, *images):
  277.             """
  278.            :doc: gallery method
  279.  
  280.            A condition that takes one or more image names as argument, and
  281.            is satisfied when all the named images have been seen by the
  282.            player. The image names should be given as strings.
  283.            """
  284.  
  285.             self.unlockable.conditions.append(__GalleryUnlockCondition(images))
  286.  
  287.         def condition(self, expression):
  288.             """
  289.            :doc: gallery method
  290.  
  291.            A condition that is satisfied when an expression evaluates to true.
  292.  
  293.            `expression`
  294.                A string giving a Python expression.
  295.            """
  296.  
  297.             if not isinstance(expression, basestring):
  298.                 raise Exception("Gallery condition must be a string containing an expression.")
  299.  
  300.             self.unlockable.conditions.append(__GalleryArbitraryCondition(expression))
  301.  
  302.         def allprior(self):
  303.             """
  304.            :doc: gallery method
  305.  
  306.            A condition that is true if all prior images associated with the
  307.            current button have been unlocked.
  308.            """
  309.  
  310.             self.unlockable.conditions.append(__GalleryAllPriorCondition())
  311.  
  312.         def unlock_image(self, *images):
  313.             """
  314.            :doc: gallery method
  315.  
  316.            A convenience method that is equivalent to calling image and unlock
  317.            with the same parameters. This will cause an image to be displayed
  318.            if it has been seen before.
  319.  
  320.            The images should be specified as strings giving image names.
  321.            """
  322.  
  323.             self.image(*images)
  324.             self.unlock(*images)
  325.  
  326.         def Action(self, name):
  327.             """
  328.            :doc: gallery method
  329.  
  330.            An action that displays the images associated with the given button
  331.            name.
  332.            """
  333.  
  334.             if name not in self.buttons:
  335.                 raise Exception("{0!r} is not a button defined in this gallery.".format(name))
  336.  
  337.             b = self.buttons[name]
  338.  
  339.             if b.check_unlock():
  340.                 return __GalleryAction(self, b.index)
  341.             else:
  342.                 return None
  343.  
  344.         def make_button(self, name, unlocked, locked=None, hover_border=None, idle_border=None, style=None, **properties):
  345.             """
  346.            :doc: gallery method
  347.  
  348.            This creates a button that displays the images associated with the given
  349.            button name.
  350.  
  351.            `name`
  352.                The name of the button that will be created.
  353.  
  354.            `unlocked`
  355.                A displayable that is displayed for this button when it is
  356.                unlocked.
  357.  
  358.            `locked`
  359.                A displayable that is displayed for this button when it is
  360.                locked. If None, the locked_button field of the gallery
  361.                object is used instead.
  362.  
  363.            `hover_border`
  364.                A displayable that is used to overlay this button when
  365.                it is unlocked and has focus. If None, the hover_border
  366.                field of the gallery object is used.
  367.  
  368.            `idle_border`
  369.                A displayable that is used to overlay this button when
  370.                it is unlocked but unfocused. If None, the idle_border
  371.                field of the gallery object is used.
  372.  
  373.            `style`
  374.                The style the button inherits from. When None, defaults
  375.                to the "empty" style, so as not to inherit borders and
  376.                so on.
  377.  
  378.            Additional keyword arguments become style properties of the
  379.            created button object.
  380.            """
  381.  
  382.             action = self.Action(name)
  383.  
  384.             if locked is None:
  385.                 locked = self.locked_button
  386.  
  387.             if hover_border is None:
  388.                 hover_border = self.hover_border
  389.  
  390.             if idle_border is None:
  391.                 idle_border = self.idle_border
  392.  
  393.             if style is None:
  394.  
  395.                 if (config.script_version is not None) and (config.script_version <= (7, 0, 0)):
  396.                     style = "button"
  397.                 else:
  398.                     style = "empty"
  399.  
  400.             return Button(action=action, child=unlocked, insensitive_child=locked, hover_foreground=hover_border, idle_foreground=idle_border, style=style, **properties)
  401.  
  402.         def get_fraction(self, name, format="{seen}/{total}"):
  403.             """
  404.            :doc: gallery method
  405.  
  406.            Returns a text string giving the number of unlocked images and total number of images in the button
  407.            named `name`.
  408.  
  409.            `format`
  410.                A Python format string that's used to format the numbers. This has three values that
  411.                can be substituted in:
  412.  
  413.                {seen}
  414.                    The number of images that have been seen.
  415.                {total}
  416.                    The total number of images in the button.
  417.                {locked}
  418.                    The number of images that are still locked.
  419.            """
  420.  
  421.             seen = 0
  422.             total = 0
  423.  
  424.             all_prior = True
  425.  
  426.             for i in self.buttons[name].images:
  427.                 total += 1
  428.                 if i.check_unlock(all_prior):
  429.                     seen += 1
  430.                 else:
  431.                     all_prior = False
  432.  
  433.             return format.format(seen=seen, total=total, locked=total - seen)
  434.  
  435.         def show(self, button=0, image=0):
  436.             """
  437.            Starts showing gallery images.
  438.  
  439.            `button`
  440.                The index of the button to start showing.
  441.            """
  442.  
  443.             # A list of (button, image) index pairs for all of the images we know
  444.             # about.
  445.             all_images = [ ]
  446.  
  447.             # A list of (button, image) index pairs for all of the unlocked
  448.             # images.
  449.             unlocked_images = [ ]
  450.  
  451.             for bi, b in enumerate(self.button_list):
  452.  
  453.                 all_unlocked = True
  454.  
  455.                 for ii, i in enumerate(b.images):
  456.  
  457.                     all_images.append((bi, ii))
  458.  
  459.                     unlocked = i.check_unlock(all_unlocked)
  460.  
  461.                     if unlocked:
  462.                         unlocked_images.append((bi, ii))
  463.                     else:
  464.                         all_unlocked = False
  465.  
  466.                         if self.unlocked_advance and (button == bi) and (image == ii):
  467.                             image += 1
  468.  
  469.             self.slideshow = False
  470.  
  471.             # Loop, displaying the images.
  472.             while True:
  473.  
  474.                 if button >= len(self.button_list):
  475.                     break
  476.  
  477.                 b = self.button_list[button]
  478.  
  479.                 if image >= len(b.images):
  480.                     break
  481.  
  482.                 i = b.images[image]
  483.  
  484.                 result = i.show((button, image) not in unlocked_images, image, len(b.images))
  485.  
  486.                 # Default action for click.
  487.  
  488.                 if result is True:
  489.                     result = "next"
  490.  
  491.                 if result == 'return':
  492.                     break
  493.  
  494.                 # At this point, result is either 'next', "next_unlocked", "previous", or "previous_unlocked"
  495.                 # Go through the common advance code.
  496.  
  497.                 if self.unlocked_advance:
  498.                     images = unlocked_images
  499.                 else:
  500.                     images = all_images
  501.  
  502.                 if (button, image) in images:
  503.                     index = images.index((button, image))
  504.                 else:
  505.                     index = -1
  506.  
  507.                 if result.startswith('previous'):
  508.                     index -= 1
  509.                 else:
  510.                     index += 1
  511.  
  512.                 if index < 0 or index >= len(images):
  513.                     break
  514.  
  515.                 new_button, new_image = images[index]
  516.  
  517.                 if not self.span_buttons:
  518.                     if new_button != button:
  519.                         break
  520.  
  521.                 button = new_button
  522.                 image = new_image
  523.  
  524.             renpy.transition(self.transition)
  525.  
  526.         def Return(self):
  527.             """
  528.            :doc: gallery method
  529.  
  530.            Stops displaying gallery images.
  531.            """
  532.  
  533.             return ui.returns("return")
  534.  
  535.         def Next(self, unlocked=False):
  536.             """
  537.            :doc: gallery method
  538.  
  539.            Advances to the next image in the gallery.
  540.  
  541.            `unlocked`
  542.                If true, only considers unlocked images.
  543.            """
  544.  
  545.             if unlocked:
  546.                 return ui.returns("next_unlocked")
  547.             else:
  548.                 return ui.returns("next")
  549.  
  550.         def Previous(self, unlocked=False):
  551.             """
  552.            :doc: gallery method
  553.  
  554.            Goes to the previous image in the gallery.
  555.  
  556.            `unlocked`
  557.                If true, only considers unlocked images.
  558.            """
  559.  
  560.             if unlocked:
  561.                 return ui.returns("previous_unlocked")
  562.             else:
  563.                 return ui.returns("previous")
  564.  
  565.         def ToggleSlideshow(self):
  566.             """
  567.            :doc: gallery method
  568.  
  569.            Toggles slideshow mode.
  570.            """
  571.             return __GalleryToggleSlideshow(self)
  572.  
  573. init -1500:
  574.  
  575.     # Displays a set of images in the gallery, or indicates that the images
  576.     # are locked. This is given the following arguments:
  577.     #
  578.     # locked
  579.     #     True if the image is locked.
  580.     # displayables
  581.     #     A list of transformed displayables that should be shown to the user.
  582.     # index
  583.     #     A 1-based index of the image being shown.
  584.     # count
  585.     #     The number of images attached to the current button.
  586.     # gallery
  587.     #     The image gallery object.
  588.     screen _gallery:
  589.  
  590.         if locked:
  591.             add "#000"
  592.             text _("Image [index] of [count] locked.") align (0.5, 0.5)
  593.         else:
  594.             for d in displayables:
  595.                 add d
  596.  
  597.         if gallery.slideshow:
  598.             timer gallery.slideshow_delay action Return("next") repeat True
  599.  
  600.         key "game_menu" action gallery.Return()
  601.  
  602.         if gallery.navigation:
  603.             use gallery_navigation
  604.  
  605.     screen gallery_navigation:
  606.         hbox:
  607.             spacing 20
  608.  
  609.             style_group "gallery"
  610.             align (.98, .98)
  611.  
  612.             textbutton _("prev") action gallery.Previous(unlocked=gallery.unlocked_advance)
  613.             textbutton _("next") action gallery.Next(unlocked=gallery.unlocked_advance)
  614.             textbutton _("slideshow") action gallery.ToggleSlideshow()
  615.             textbutton _("return") action gallery.Return()
  616.  
  617.     python:
  618.         style.gallery = Style(style.default)
  619.         style.gallery_button.background = None
  620.         style.gallery_button_text.color = "#666"
  621.         style.gallery_button_text.hover_color = "#fff"
  622.         style.gallery_button_text.selected_color = "#fff"
  623.         style.gallery_button_text.size = 16
  624.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement