# Copyright 2004-2014 Tom Rothamel <pytom@bishoujo.us>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# The displayable and style inspectors.

screen _inspector:
    zorder 1010
    modal True

    frame:
        style_group ""

        xmaximum 770
        xfill True

        xpadding 15
        ypadding 15

        xalign 0.5
        yalign 0.25


        has vbox

        label _("Displayable Inspector")

        null height 10

        if not tree:

            text _("Nothing to inspect.")

        else:

            hbox:
                text " ":
                    min_width 400
                    xmaximum 400

                text _("Size"):
                    min_width 100
                    xmaximum 100
                    underline True

                text _("Style"):
                    underline True

            null height 5

            for depth, width, height, d in tree:

                $ t = "  " * depth + u"\u2022 " + unicode(d)
                $ s = __format_style(d.style.parent)

                button:
                    xfill True
                    xmargin 0
                    ymargin 0
                    xpadding 0
                    ypadding 0
                    xoffset 0

                    background None
                    hover_background "#fff4"

                    action Show("_style_inspector", d=d)

                    hbox:

                        text "[t!q]":
                            min_width 400
                            xmaximum 400

                        text "[width:.0f]x[height:.0f]":
                            min_width 100
                            xmaximum 100

                        text "[s!q]":
                            style "_hyperlink"

        null height 20

        textbutton _("Return") action Return(True)
        key "game_menu" action Return(True)

screen _style_inspector:
    zorder 1020
    modal True

    frame:
        style_group ""

        xmaximum 700
        xfill True

        xpadding 15
        ypadding 15

        xalign 0.5
        yalign 0.25

        has vbox

        $ displayable_name = unicode(d)
        label _("Inspecting Styles of [displayable_name!q]")

        null height 10

        viewport:
            yfill False
            mousewheel True

            vbox:
                for name, properties in d.style.inspect():

                    if name is None:
                        text _("displayable:")
                    else:
                        $ style_name = __format_style(name)
                        text "style [style_name!q]:"


                    if not properties:
                        text _("        (no properties affect the displayable)")
                    elif name in [ ("default",), ("_default",) ]:
                        text _("        (default properties omitted)")
                    else:
                        for propname in sorted(properties):
                            $ value = __safe_repr(properties[propname])
                            text "        [propname] [value!q]"

                    null height 5


        # Code goes here.

        null height 20

        textbutton _("Return") action Hide("_style_inspector")
        key "game_menu" action Hide("_style_inspector")


init python:

    def __format_style(name):
        return name[0] + "".join([ "[%r]" % i for i in name[1:] ])

    def __safe_repr(name):
        try:
            s = unicode(repr(name))
            if len(s) > 51:
                s = s[:50] + u"\u2026"

            return s
        except:
            return _("<repr() failed>")

    def __inspect(tree):
        renpy.context_dynamic("_window")
        store._window = False

        renpy.exports.show_screen("_inspector", transient=True, tree=tree)
        renpy.ui.interact(mouse="screen", type="screen", suppress_overlay=True, suppress_underlay=True)

    config.inspector = __inspect
