Exemplo n.º 1
0
    def do_add_new_field(self, fields_layout, on_input, on_delete):
        if self.popup is not None:
            self.popup.teardown()

        def do_cancel_add(dialog=None):
            self.popup.teardown()
            self.popup = None

        def do_add_field(dialog=None):
            global gDirty
            gDirty = True

            form = self.popup.get_values()
            self.popup.teardown()
            self.popup = None

            name = form['name']
            if self.theme[self.path].has_key(name):
                self.popup_message("%s is already a field name!" % name)
                return
            try:
                value = safe_eval.safe_eval(form['value'])
            except kytten.safe_eval.Unsafe_Source_Error:
                value = form['value']
            self.theme[self.path][name] = value
            fields_layout.add_row([
                kytten.Label(name),
                kytten.Input(id=name, text=str(value), on_input=on_input),
                kytten.Button("Delete", id=name, on_click=on_delete)
            ])

            if not self.delete_button.is_disabled():
                self.delete_button.disable()
            self.dialog.set_needs_layout()

        self.popup = kytten.Dialog(kytten.Frame(
            kytten.VerticalLayout([
                kytten.GridLayout([[
                    kytten.Label('Name'),
                    kytten.Input(id='name', text='new_field')
                ],
                                   [
                                       kytten.Label('Value'),
                                       kytten.Input(id='value',
                                                    text='new_value')
                                   ]]),
                kytten.HorizontalLayout([
                    kytten.Button("Add", on_click=do_add_field), None,
                    kytten.Button("Cancel", on_click=do_cancel_add)
                ])
            ])),
                                   window=self.window,
                                   theme=gTheme,
                                   on_enter=do_add_field,
                                   on_escape=do_cancel_add)
Exemplo n.º 2
0
    def layerDialog(self):
        # Set up a Dialog to choose test dialogs to show
        def createLayer():
            self.canvas.addLayer()
            dialog.teardown()
            self.layerDialog()

        def focusLayer(foo):
            def func():
                self.canvas.setCurrentLayer(foo)
            return func

        def incLayer(foo):
            def func():
                self.canvas.incrementLayer(foo)
                dialog.teardown()
                self.layerDialog()
            return func

        def decLayer(foo):
            def func():
                self.canvas.decrementLayer(foo)
                dialog.teardown()
                self.layerDialog()
            return func

        def delLayer(foo):
            def func():
                self.canvas.deleteLayer(foo)
                dialog.teardown()
                self.layerDialog()
            return func

        content = [item for sublist in
                   [
                       [kytten.HorizontalLayout([
                           kytten.Button("S", on_click=focusLayer(layer.index)),
                           kytten.Checkbox(layer.name, is_checked=layer.visible,
                                           on_click=layer.toggleVisibility),
                           kytten.Button("^", on_click=incLayer(layer.index)),
                           kytten.Button("v", on_click=decLayer(layer.index))
                           ])
                        ] for layer in self.canvas.layers]
                   for item in sublist]
        content.append(kytten.Button("Create Layer", on_click=createLayer))
        dialog = kytten.Dialog(
            kytten.Frame(
                kytten.VerticalLayout(
                    content,
                    padding=0, align=kytten.VALIGN_TOP)
            ),
            window=self.window, batch=self.canvas.batch, group=self.fg_group,
            anchor=kytten.ANCHOR_TOP_RIGHT,
            on_mouse_release=self.on_mouse_release,
            theme=self.cobalt)
Exemplo n.º 3
0
    def do_add_new_image(self):
        if self.popup is not None:
            self.popup.teardown()

        def do_cancel_add(dialog=None):
            self.popup.teardown()
            self.popup = None

        def do_add_image(dialog=None):
            global gDirty
            gDirty = True

            form = self.popup.get_values()
            self.popup.teardown()
            self.popup = None

            name = form['name']
            if self.theme[self.path].has_key(name):
                self.popup_message("%s is already a field name!" % name)
                return
            if not name.startswith('image'):
                self.popup_message("Image names must begin with 'image'")
            texture = self.textures[form['texture']]
            self.theme[self.path][name] = FrameTextureGraphicElementTemplate(
                self.theme,
                texture,
                [0, 0, texture.width, texture.height],  # stretch
                [0, 0, 0, 0])  # padding
            self.manager.push(
                ImageEditState(self.theme_dir, self.theme, self.textures,
                               self.path, name))

        self.popup = kytten.Dialog(kytten.Frame(
            kytten.VerticalLayout([
                kytten.GridLayout([[
                    kytten.Label('Name'),
                    kytten.Input(id='name', text='image')
                ],
                                   [
                                       kytten.Label('Texture'),
                                       kytten.Dropdown(
                                           id='texture',
                                           options=self.textures.keys(),
                                           selected=self.textures.keys()[0])
                                   ]]),
                kytten.HorizontalLayout([
                    kytten.Button("Add", on_click=do_add_image), None,
                    kytten.Button("Cancel", on_click=do_cancel_add)
                ])
            ])),
                                   window=self.window,
                                   theme=gTheme,
                                   on_enter=do_add_image,
                                   on_escape=do_cancel_add)
Exemplo n.º 4
0
    def _get_custom_fields(self):
        # Handle all fields here that we don't have a basic handler for
        fields_layout = None

        def on_input(id, value):
            global gDirty
            gDirty = True
            try:
                self.theme[self.path][id] = safe_eval.safe_eval(value)
            except kytten.safe_eval.Unsafe_Source_Error:
                self.theme[self.path][id] = value

        def on_delete(id):
            self.do_delete_field(fields_layout, id)

        fields = []
        items = self.theme[self.path].items()
        items.sort(lambda x, y: cmp(x[0], y[0]))
        index = 0
        for k, v in items:
            if not k.startswith('image') and not isinstance(v, dict):
                if not k.endswith('color'):
                    fields.append([
                        kytten.Label(k),
                        kytten.Input(id=k, text=str(v), on_input=on_input),
                        kytten.Button("Delete", id=k, on_click=on_delete)
                    ])
                else:
                    fields.append([
                        kytten.Label(k),
                        ColorSelector(id=k, color=v, on_select=on_input),
                        kytten.Button("Delete", id=k, on_click=on_delete)
                    ])
            index += 1
        fields_layout = kytten.GridLayout(fields)

        # Pop up a dialog if we want to add a new field
        def on_add_field():
            self.do_add_new_field(fields_layout, on_input, on_delete)

        return [
            kytten.FoldingSection(
                "Custom Fields",
                kytten.VerticalLayout([
                    kytten.GridLayout(fields, anchor=kytten.ANCHOR_LEFT),
                    kytten.Button("Add Field", on_click=on_add_field),
                ]))
        ]
Exemplo n.º 5
0
        def do_add_field(dialog=None):
            global gDirty
            gDirty = True

            form = self.popup.get_values()
            self.popup.teardown()
            self.popup = None

            name = form['name']
            if self.theme[self.path].has_key(name):
                self.popup_message("%s is already a field name!" % name)
                return
            try:
                value = safe_eval.safe_eval(form['value'])
            except kytten.safe_eval.Unsafe_Source_Error:
                value = form['value']
            self.theme[self.path][name] = value
            fields_layout.add_row([
                kytten.Label(name),
                kytten.Input(id=name, text=str(value), on_input=on_input),
                kytten.Button("Delete", id=name, on_click=on_delete)
            ])

            if not self.delete_button.is_disabled():
                self.delete_button.disable()
            self.dialog.set_needs_layout()
Exemplo n.º 6
0
    def _get_image_fields(self):
        # Display a menu to choose images to edit
        images = []
        items = self.theme[self.path].items()
        items.sort(lambda x, y: cmp(x[0], y[0]))
        for k, v in items:
            if isinstance(v, TextureGraphicElementTemplate):
                assert k.startswith('image')
                images.append(k)

        def edit_image(choice):
            self.do_edit_image(choice)

        def add_image():
            self.do_add_new_image()

        return [
            kytten.FoldingSection(
                "Images",
                kytten.VerticalLayout([
                    kytten.Menu(options=images,
                                on_select=edit_image,
                                align=kytten.HALIGN_LEFT),
                    kytten.Button('Add Image', on_click=add_image)
                ]))
        ]
Exemplo n.º 7
0
    def _get_custom_components(self):
        # Handle all components without built-in handlers here
        components = []
        items = self.theme[self.path].items()
        items.sort(lambda x, y: cmp(x[0], y[0]))
        for k, v in items:
            if isinstance(v, dict):
                components.append(k)

        def edit_component(choice):
            self.do_edit_component(choice)

        def on_add_click():
            self.do_add_new_component()

        return [
            kytten.FoldingSection(
                "Custom Components",
                kytten.VerticalLayout([
                    kytten.Menu(options=components,
                                on_select=edit_component,
                                align=kytten.HALIGN_LEFT),
                    kytten.Button("Add Component", on_click=on_add_click)
                ]))
        ]
Exemplo n.º 8
0
    def do_change_texture(self):
        if self.popup is not None:
            self.popup.teardown()

        def do_cancel_change(dialog=None):
            self.popup.teardown()
            self.popup = None

        def do_change_texture(dialog=None):
            global gDirty
            gDirty = True

            form = self.popup.get_values()
            self.popup.teardown()
            self.popup = None

            texture = self.textures[form['texture']]
            self.theme[self.path][self.image] = \
         FrameTextureGraphicElementTemplate(
             self.theme,
             texture,
             [0, 0, texture.width, texture.height],  # stretch
             [0, 0, 0, 0])                           # padding
            manager = self.manager
            manager.pop()
            manager.push(
                ImageEditState(self.theme_dir, self.theme, self.textures,
                               self.path, self.image))

        self.popup = kytten.Dialog(kytten.Frame(
            kytten.VerticalLayout([
                kytten.HorizontalLayout([
                    kytten.Label('Texture'),
                    kytten.Dropdown(id='texture',
                                    options=self.textures.keys(),
                                    selected=self.textures.keys()[0])
                ]),
                kytten.HorizontalLayout([
                    kytten.Button("Change", on_click=do_change_texture), None,
                    kytten.Button("Cancel", on_click=do_cancel_change)
                ]),
            ])),
                                   window=self.window,
                                   theme=gTheme,
                                   on_enter=do_change_texture,
                                   on_escape=do_cancel_change)
Exemplo n.º 9
0
    def _get_buttons(self):
        if self.path:
            disabled = bool(self.theme.get(self.path))

            def on_return():
                self.manager.pop()

            def on_delete_click():
                self.do_delete_this()

            self.delete_button = kytten.Button("Delete Component",
                                               disabled=disabled,
                                               on_click=on_delete_click)
            return [
                kytten.Button("Back", on_click=on_return), self.delete_button
            ]
        else:
            return []
Exemplo n.º 10
0
    def do_add_new_component(self):
        if self.popup is not None:
            self.popup.teardown()

        def do_cancel_add(dialog=None):
            self.popup.teardown()
            self.popup = None

        def do_add_component(dialog=None):
            global gDirty
            gDirty = True

            form = self.popup.get_values()
            self.popup.teardown()
            self.popup = None

            name = form['name']
            if self.theme[self.path].has_key(name):
                self.popup_message("%s is already a field name!" % name)
                return
            self.theme[self.path][name] = {}
            self.manager.push(
                _NodeEditState(self.theme_dir, self.theme, self.textures,
                               self.path + [name]))

        self.popup = kytten.Dialog(kytten.Frame(
            kytten.VerticalLayout([
                kytten.HorizontalLayout([
                    kytten.Label('Name'),
                    kytten.Input(id='name', text='new_component')
                ]),
                kytten.HorizontalLayout([
                    kytten.Button("Add", on_click=do_add_component), None,
                    kytten.Button("Cancel", on_click=do_cancel_add)
                ])
            ])),
                                   window=self.window,
                                   theme=gTheme,
                                   on_enter=do_add_component,
                                   on_escape=do_cancel_add)
Exemplo n.º 11
0
    def _get_buttons(self):
        def on_save():
            self.do_save()

        def on_save_as():
            self.do_save_as()

        def on_new():
            if gDirty:
                if self.popup is not None:
                    self.popup.teardown()
                self.popup = kytten.PopupConfirm(
                    "Create new theme without saving?",
                    on_ok=self.do_new,
                    theme=gTheme,
                    window=self.window)
            else:
                self.do_new()

        return [
            kytten.Button("Save", on_click=on_save),
            kytten.Button("Save As", on_click=on_save_as),
            kytten.Button("New", on_click=on_new)
        ]
Exemplo n.º 12
0
    def _get_content(self):
        def on_return():
            self.manager.pop()

        def on_delete():
            self.do_delete_image()

        def on_change_texture():
            self.do_change_texture()

        content = [
            kytten.VerticalLayout([
                kytten.Document("""Theme: %s
Path: %s
Image: %s
""" % (self.theme_dir, '/'.join(self.path), self.image),
                                width=700),
                kytten.HorizontalLayout([
                    kytten.Button("Back", on_click=on_return),
                    kytten.Button("Change Texture",
                                  on_click=on_change_texture),
                    kytten.Button("Delete Image", on_click=on_delete)
                ]),
            ],
                                  align=kytten.HALIGN_LEFT)
        ]

        # Locate the base texture for the template
        self.template = self.theme[self.path][self.image]
        texture_id = self.template.texture.id
        our_texture = None
        our_filename = None
        for texture_name, texture in self.textures.iteritems():
            if texture.id == texture_id:
                our_filename = texture_name
                our_texture = texture
                break
        assert our_texture is not None

        # Determine the region that we occupy
        x, y = self.template.texture.x, self.template.texture.y
        width, height = self.template.width, self.template.height
        self.region = (x, y, width, height)
        left, right, top, bottom = self.template.margins
        self.stretch = (left, bottom, width - right - left,
                        height - top - bottom)
        self.padding = self.template.padding

        # Create the Resizable for the example's content
        example_resizable = Resizable(100, 100)

        def enable_example_resizable(is_checked):
            if is_checked:
                example_resizable.enable()
            else:
                example_resizable.disable()

        # Create the example
        example = kytten.Frame(example_resizable,
                               path=self.path,
                               image_name=self.image)

        # Create the ImageRegionPlacer for the editor
        region_placer = None

        def set_region(x, y, width, height):
            global gDirty
            gDirty = True

            if self.state == 'Region':
                # Try to keep stretch area the same but restrict it to
                # the current available region
                ox, oy, _, _ = self.region
                sx, sy, swidth, sheight = self.stretch
                sx += ox
                sy += oy
                if sx < x:
                    swidth = max(0, swidth - x + sx)
                    sx = x
                elif sx > x + width:
                    swidth = 0
                    sx = x + width
                if sy < y:
                    sheight = max(0, sheight - y + sy)
                    sy = y
                elif sy > y + height:
                    sheight = 0
                    sy = y + height
                if (sx - x) + swidth > width:
                    swidth = max(0, width - (sx - x))
                if (sy - y) + sheight > height:
                    sheight = max(0, height - (sy - y))

                self.region = (x, y, width, height)
                self.stretch = (sx - x, sy - y, swidth, sheight)
            elif self.state == 'Stretch':
                rx, ry, rwidth, rheight = self.region
                self.stretch = (x - rx, y - ry, width, height)
            else:  # Padding
                rx, ry, rwidth, rheight = self.region
                left = x - rx
                bottom = y - ry
                top = ry + rheight - height - y
                right = rx + rwidth - width - x
                self.padding = (left, right, top, bottom)

            # Drop our old template and construct a new one
            x, y, width, height = self.region
            texture = self.theme._get_texture_region(our_filename, x, y, width,
                                                     height)
            self.template = FrameTextureGraphicElementTemplate(
                self.theme, texture, self.stretch, self.padding)
            self.theme[self.path][self.image] = self.template
            example.delete()
            self.dialog.set_needs_layout()

        region_placer = ImageRegionPlacer(texture,
                                          x,
                                          y,
                                          width,
                                          height,
                                          on_resize=set_region)

        def set_placer_scale(scale):
            region_placer.set_scale(scale)

        # Create a drop-down to control what region we're setting
        def on_region_select(choice):
            self.state = choice
            if choice == 'Region':
                region_placer.set_region(
                    *self.region, color=ImageRegionPlacer.IMAGE_REGION_COLOR)
            elif choice == 'Stretch':
                rx, ry, rwidth, rheight = self.region
                x, y, width, height = self.stretch
                region_placer.set_region(
                    x + rx,
                    y + ry,
                    width,
                    height,
                    color=ImageRegionPlacer.IMAGE_STRETCH_COLOR,
                    limits=self.region)
            else:  # Padding
                rx, ry, rwidth, rheight = self.region
                left, right, top, bottom = self.padding
                region_placer.set_region(
                    left + rx,
                    bottom + ry,
                    rwidth - left - right,
                    rheight - top - bottom,
                    color=ImageRegionPlacer.IMAGE_PADDING_COLOR,
                    limits=self.region)

        region_control = kytten.Dropdown(
            options=['Region', 'Stretch', 'Padding'],
            selected=self.state,
            on_select=on_region_select)

        content += [
            kytten.FoldingSection(
                'Image Region',
                kytten.VerticalLayout([
                    region_control, region_placer,
                    kytten.GridLayout([[
                        kytten.Label('Scale'),
                        kytten.Slider(value=1.0,
                                      min_value=1.0,
                                      max_value=8.0,
                                      steps=7,
                                      width=400,
                                      on_set=set_placer_scale)
                    ]],
                                      anchor=kytten.ANCHOR_LEFT)
                ],
                                      padding=10)),
            kytten.FoldingSection(
                'Example',
                kytten.VerticalLayout([
                    ProxyDialog(example, self.theme),
                    kytten.Checkbox("Show content sizer",
                                    on_click=enable_example_resizable,
                                    is_checked=True),
                ])),
        ]
        return content
Exemplo n.º 13
0
def create_form_dialog():
    dialog = None

    def on_enter(dialog):
        print("Form submitted!")
        for key, value in dialog.get_values().items():
            print("  %s=%s" % (key, value))
        on_escape(dialog)

    def on_submit():
        on_enter(dialog)

    def on_cancel():
        print("Form canceled.")
        on_escape(dialog)

    dialog = kytten.Dialog(kytten.Frame(
        kytten.Scrollable(kytten.VerticalLayout([
            kytten.SectionHeader("Personnel Data", align=kytten.HALIGN_LEFT),
            kytten.Document(
                "Try tabbing through fields, "
                "if offscreen they'll be moved "
                "automatically",
                width=500),
            kytten.GridLayout([
                [
                    kytten.Label("Name"),
                    kytten.Input("name", "Lynx", max_length=20)
                ],
                [
                    kytten.Label("Job"),
                    kytten.Input("job", "Cat", max_length=80)
                ],
                [kytten.Label("Hobby"),
                 kytten.Input("hobby", "Programming")],
                [kytten.Label("Class"),
                 kytten.Input("class", "Druid")],
                [
                    kytten.Label("Disabled"),
                    kytten.Input("disabled", "Disabled input", disabled=True)
                ],
                [
                    kytten.Label("Sign"),
                    kytten.Input("sign", "Free to good home")
                ],
                [kytten.Label("Blood Type"),
                 kytten.Input("bloodtype", "Red")],
                [
                    kytten.Label("Favored Weapon"),
                    kytten.Input("weapon", "Claws")
                ],
            ]),
            kytten.Checkbox("Full-Time", id="fulltime"),
            kytten.Checkbox("Married", id="married", disabled=True),
            kytten.SectionHeader("Actions", align=kytten.HALIGN_LEFT),
            kytten.HorizontalLayout([
                kytten.Button("Submit", on_click=on_submit),
                kytten.Button("Disabled", disabled=True),
                None,
                kytten.Button("Cancel", on_click=on_cancel),
            ]),
        ],
                                                align=kytten.HALIGN_LEFT),
                          height=200,
                          width=360)),
                           window=window,
                           batch=batch,
                           group=fg_group,
                           anchor=kytten.ANCHOR_CENTER,
                           theme=kytten.themes.felyne_light,
                           on_enter=on_enter,
                           on_escape=on_escape)