示例#1
0
class MeterImageDisplayFrame(BitmapDisplayFrame):
    def __init__(self, *args, **kwargs):
        BitmapDisplayFrame.__init__(self, *args, **kwargs)
        for w in (self.hsb, self.vsb, self.root_canvas):
            w.pack_forget()

        self.preview_label = tk.Label(self, text="Bitmap preview\t")
        self.image_canvas = tk.Canvas(self, highlightthickness=0)
        self.channel_menu = ScrollMenu(self,
                                       menu_width=9,
                                       can_scroll=True,
                                       variable=self.channel_index)
        self.save_button = ttk.Button(self,
                                      width=11,
                                      text="Save as...",
                                      command=self.save_as)

        padx = self.horizontal_padx
        pady = self.horizontal_pady

        self.image_canvas.config(width=1, height=1)
        self.preview_label.pack(side='left', padx=padx, pady=pady, fill='x')
        self.channel_menu.pack(side='left', padx=padx, pady=pady)
        self.save_button.pack(side='left', padx=padx, pady=pady)
        self.image_canvas.pack(side='left',
                               padx=padx,
                               pady=pady,
                               fill='x',
                               expand=True)

        self.apply_style()

    def apply_style(self, seen=None):
        BitmapDisplayFrame.apply_style(self, seen)
        self.image_canvas.config(bg=self.bitmap_canvas_bg_color)
示例#2
0
class HaloBitmapDisplayFrame(BitmapDisplayFrame):
    # these mappings have the 2nd and 3rd faces swapped on pc for some reason
    cubemap_cross_mapping = (
        (-1,  1),
        ( 2,  4,  0,  5),
        (-1,  3),
        )

    def __init__(self, master, bitmap_tag=None, *args, **kwargs):
        self.bitmap_tag = bitmap_tag
        textures = kwargs.get('textures', ())
        BitmapDisplayFrame.__init__(self, master, *args, **kwargs)
        self.sequence_index = tk.IntVar(self)
        self.sprite_index   = tk.IntVar(self)

        labels = []
        labels.append(tk.Label(self.controls_frame0, text="Sequence index"))
        labels.append(tk.Label(self.controls_frame1, text="Sprite index"))
        for lbl in labels:
            lbl.config(width=15, anchor='w')

        self.sequence_menu = ScrollMenu(self.controls_frame0, menu_width=7,
                                        variable=self.sequence_index)
        self.sprite_menu = ScrollMenu(self.controls_frame1, menu_width=7,
                                      variable=self.sprite_index)

        padx = self.horizontal_padx
        pady = self.horizontal_pady
        for lbl in labels:
            lbl.pack(side='left', padx=(15, 0), pady=pady)
        self.sequence_menu.pack(side='left', padx=padx, pady=pady)
        self.sprite_menu.pack(side='left', padx=padx, pady=pady)
        self.write_trace(self.sequence_index, self.sequence_changed)
        self.write_trace(self.sprite_index,   self.sprite_changed)

        self.change_textures(textures)
        self.apply_style()

    def sequence_changed(self, *args):
        tag = self.bitmap_tag
        if tag is None:
            self.sprite_menu.set_options(())
            self.sequence_menu.set_options(("None", ))
            self.sprite_menu.sel_index = -1
            self.sequence_menu.sel_index = 0
            return

        sequence_i = self.sequence_index.get() - 1
        options = ()
        sequences = tag.data.tagdata.sequences.STEPTREE
        if sequence_i in range(len(sequences)):
            sequence = sequences[sequence_i]
            options = range(len(sequence.sprites.STEPTREE))
            if not options:
                options = range(sequence.bitmap_count)

        self.sprite_menu.set_options(options)
        self.sprite_menu.sel_index = (self.sprite_menu.max_index >= 0) - 1

    def sprite_changed(self, *args):
        self.image_canvas.delete(SPRITE_RECTANGLE_TAG)
        self.image_canvas.delete(SPRITE_CENTER_TAG)
        tag = self.bitmap_tag
        if tag is None:
            self.sprite_menu.set_options(())
            self.sequence_menu.set_options(("None", ))
            self.sprite_menu.sel_index = -1
            self.sequence_menu.sel_index = 0
            return

        data = tag.data.tagdata
        sequences = data.sequences.STEPTREE
        bitmaps   = data.bitmaps.STEPTREE

        sequence_i = self.sequence_index.get() - 1
        if sequence_i not in range(len(sequences)):
            return

        sequence = sequences[sequence_i]
        sprite_i = self.sprite_index.get()
        sprites = sequence.sprites.STEPTREE
        bitmap_index = sequence.first_bitmap_index + sprite_i
        x0, y0, x1, y1 = 0, 0, 1, 1
        rx, ry = 0.5, 0.5
        if sprite_i in range(len(sprites)):
            sprite = sprites[sprite_i]
            if sprite.bitmap_index not in range(len(bitmaps)):
                return

            bitmap_index = sprite.bitmap_index
            x0, y0, x1, y1 = sprite.left_side,  sprite.top_side,\
                             sprite.right_side, sprite.bottom_side
            rx = x0 + sprite.registration_point_x
            ry = y0 + sprite.registration_point_y
        elif bitmap_index not in range(len(bitmaps)):
            return

        if bitmap_index != self.bitmap_index.get():
            self.bitmap_menu.sel_index = bitmap_index

        bitmap = bitmaps[bitmap_index]
        mip = self.mipmap_index.get()
        w, h = max(bitmap.width>>mip, 1), max(bitmap.height>>mip, 1)
        x0, y0 = int(round(x0 * w)), int(round(y0 * h))
        x1, y1 = int(round(x1 * w)), int(round(y1 * h))
        rx, ry = int(round(rx * w)), int(round(ry * h))
        rx0, rx1 = rx - 1, rx + 1
        ry0, ry1 = ry - 1, ry + 1
        if x1 < x0: x0, x1 = x1, x0
        if y1 < y0: y0, y1 = y1, y0
        if ry1 < ry0: ry0, ry1 = ry1, ry0
        x0 -= 1; y0 -= 1
        rx0 -= 1; ry0 -= 1

        self.image_canvas.create_rectangle(
            (x0, y0, x1, y1), fill=None, dash=(2, 1),
            tags=SPRITE_RECTANGLE_TAG,
            outline=self.bitmap_canvas_outline_color)

        self.image_canvas.create_rectangle(
            (rx0, ry0, rx1, ry1), fill=None,
            tags=SPRITE_CENTER_TAG,
            outline=self.bitmap_canvas_bg_color)

    def change_textures(self, textures):
        BitmapDisplayFrame.change_textures(self, textures)
        tag = self.bitmap_tag
        if tag is None: return

        data = tag.data.tagdata
        options = {i+1: str(i) for i in range(len(data.sequences.STEPTREE))}
        options[0] = "None"

        if not (hasattr(self, "sprite_menu") and
                hasattr(self, "sequence_menu")):
            return
        self.sequence_menu.set_options(options)
        self.sequence_menu.sel_index = (self.sequence_menu.max_index >= 0) - 1
示例#3
0
class UnionFrame(container_frame.ContainerFrame):
    '''Used for enumerator nodes. When clicked, creates
    a dropdown box of all available enumerator options.'''

    option_cache = None
    u_node_widgets_by_u_index = ()

    def __init__(self, *args, **kwargs):
        field_widget.FieldWidget.__init__(self, *args, **kwargs)
        self.u_node_widgets_by_u_index = {}

        if self.f_widget_parent is None:
            self.pack_padx = self.pack_pady = 0

        kwargs.update(relief='flat',
                      bd=0,
                      highlightthickness=0,
                      bg=self.default_bg_color)

        show_frame = bool(
            kwargs.pop('show_frame', not self.blocks_start_hidden))
        if self.is_empty and self.hide_if_blank:
            show_frame = False

        tk.Frame.__init__(self, *args, **e_c.fix_kwargs(**kwargs))

        self.show = tk.BooleanVar(self)
        self.show.set(show_frame)

        max_u_index = len(self.desc['CASE_MAP'])
        u_index = getattr(self.node, "u_index", None)
        if u_index is None:
            u_index = max_u_index

        toggle_text = '-' if show_frame else '+'

        self.title = tk.Frame(self, relief='raised')
        self.show_btn = ttk.Checkbutton(self.title,
                                        width=3,
                                        text=toggle_text,
                                        command=self.toggle_visible,
                                        style='ShowButton.TButton')
        self.title_label = tk.Label(self.title,
                                    text=self.gui_name,
                                    anchor='w',
                                    width=self.title_size,
                                    justify='left')
        self.title_label.font_type = "frame_title"
        self.sel_menu = ScrollMenu(self.title,
                                   f_widget_parent=self,
                                   sel_index=u_index,
                                   max_index=max_u_index,
                                   disabled=self.disabled,
                                   callback=self.select_option,
                                   option_getter=self.get_options)

        self.show_btn.pack(side="left")
        self.title_label.pack(side="left", fill="x", expand=True)
        self.sel_menu.pack(side="left", fill="x")
        self.title.pack(fill="x", expand=True)

        self.content = tk.Frame(self, relief="sunken")

        # make the default raw bytes union frame
        self.raw_frame = tk.Frame(self.content, relief="flat", bd=0)
        self.raw_label = tk.Label(self.raw_frame,
                                  text='DataUnion',
                                  width=self.title_size,
                                  anchor='w',
                                  disabledforeground=self.text_disabled_color)
        self.import_btn = ttk.Button(self.raw_frame,
                                     text='Import',
                                     command=self.import_node,
                                     width=7)
        self.export_btn = ttk.Button(self.raw_frame,
                                     text='Export',
                                     command=self.export_node,
                                     width=7)

        self.raw_label.pack(side="left", expand=True, fill='x')
        for w in (self.export_btn, self.import_btn):
            w.pack(side="left", padx=(0, 4), pady=2)

        self.populate()
        self._initialized = True

    def apply_style(self, seen=None):
        container_frame.ContainerFrame.apply_style(self, seen)
        self.title.config(bd=self.frame_depth, bg=self.frame_bg_color)
        self.title_label.config(bg=self.frame_bg_color)
        self.content.config(bd=self.frame_depth)

    def load_child_node_data(self):
        desc = self.desc
        sub_node = None
        for wid in self.f_widgets:
            # try and load any existing FieldWidgets with appropriate node data
            w = self.f_widgets[wid]
            attr_index = self.f_widget_ids_map_inv.get(wid)
            if attr_index is None:
                continue
            elif self.node:
                sub_node = self.node.u_node

            w.load_node_data(self.node, sub_node, attr_index)

        return False

    def set_disabled(self, disable=True):
        disable = disable or not self.editable
        if self.node is None and not disable:
            return

        if bool(disable) != self.disabled and getattr(self, "sel_menu", None):
            self.sel_menu.set_disabled(disable)

        container_frame.ContainerFrame.set_disabled(self, disable)

    def get_options(self, opt_index=None):
        '''
        Returns a list of the option strings sorted by option index.
        '''
        if self.option_cache is None:
            return self.generate_options(opt_index)

        if opt_index is None:
            return self.option_cache
        elif opt_index == e_c.ACTIVE_ENUM_NAME:
            opt_index = getattr(self.node, "u_index", None)
            if opt_index is None:
                opt_index = len(self.option_cache) - 1

        if opt_index is None:
            opt_index = -1

        return self.option_cache.get(opt_index, None)

    def generate_options(self, opt_index=None):
        options = {i: c for c, i in self.desc['CASE_MAP'].items()}
        options[len(options)] = e_c.RAW_BYTES

        if opt_index is None:
            self.option_cache = options
            if self.sel_menu is not None:
                self.sel_menu.options_menu_sane = False
            return options
        return options.get(opt_index, None)

    def edit_apply(self=None, *, edit_state, undo=True):
        edit_info = edit_state.edit_info

        w, node = field_widget.FieldWidget.get_widget_and_node(
            nodepath=edit_state.nodepath, tag_window=edit_state.tag_window)

        if undo:
            node.u_index = edit_info.get('undo_u_index')
            u_node = edit_state.undo_node
        else:
            node.u_index = edit_info.get('redo_u_index')
            u_node = edit_state.redo_node

        if node.u_index is None:
            node.u_node = None
            node[:] = u_node
        else:
            node.u_node = u_node

        if w is not None:
            try:
                if w.desc is not edit_state.desc:
                    return

                w.needs_flushing = False
                w.set_edited()
                w.reload()
            except Exception:
                print(format_exc())

    def select_option(self, opt_index=None, force=False, reload=True):
        self.flush()
        if self.node is None:
            return

        node = self.node
        curr_index = self.sel_menu.sel_index

        if (opt_index < 0 or opt_index > self.sel_menu.max_index
                or opt_index is None or force):
            return

        undo_u_index = node.u_index
        undo_node = node.u_node
        if node.u_index is None:
            undo_node = node[:]

        if opt_index == self.sel_menu.max_index:
            # setting to rawdata
            self.node.set_active()
        else:
            self.node.set_active(opt_index)

        self.set_edited()
        # make an edit state
        if undo_u_index != node.u_index:
            self.edit_create(
                undo_u_index=undo_u_index,
                redo_u_index=node.u_index,
                redo_node=node[:] if node.u_index is None else node.u_node,
                undo_node=undo_node)

        self.sel_menu.sel_index = opt_index
        if reload:
            self.reload()

    def populate(self):
        try:
            old_u_node_frames = []
            for u_index in self.u_node_widgets_by_u_index:
                # delete any existing widgets
                if u_index is not None:
                    old_u_node_frames.append(
                        self.u_node_widgets_by_u_index[u_index])

            self.u_node_widgets_by_u_index.clear()
            self.u_node_widgets_by_u_index[None] = self.raw_frame

            for w in (self, self.content, self.title_label):
                w.tooltip_string = self.desc.get('TOOLTIP')

            self.display_comment(self.content)
            self.reload()

            # do things in this order to prevent the window from scrolling up
            for w in old_u_node_frames:
                w.destroy()
        except Exception:
            print(format_exc())

    def reload(self):
        try:
            # clear the f_widget_ids list
            self.f_widget_ids = []
            self.f_widget_ids_map = {}
            self.f_widget_ids_map_inv = {}
            u_index = u_node = None
            if self.node is not None:
                self.raw_label.config(text='DataUnion: %s raw bytes' %
                                      self.node.get_size())
                sel_index = self.sel_menu.sel_index
                u_index = self.node.u_index
                u_node = self.node.u_node

                new_sel_index = (self.sel_menu.max_index
                                 if u_index is None else u_index)
                if new_sel_index != self.sel_menu.sel_index:
                    self.sel_menu.sel_index = new_sel_index

            u_desc = self.desc.get(u_index)
            if hasattr(u_node, 'desc'):
                u_desc = u_node.desc

            active_widget = self.u_node_widgets_by_u_index.get(u_index)
            if active_widget is None:
                if u_index is not None:
                    widget_cls = self.widget_picker.get_widget(u_desc)
                    kwargs = dict(show_title=False,
                                  tag_window=self.tag_window,
                                  attr_index=u_index,
                                  disabled=self.disabled,
                                  f_widget_parent=self,
                                  desc=u_desc,
                                  show_frame=self.show.get(),
                                  dont_padx_fields=True)

                    try:
                        active_widget = widget_cls(self.content, **kwargs)
                    except Exception:
                        print(format_exc())
                        active_widget = data_frame.NullFrame(
                            self.content, **kwargs)
                else:
                    active_widget = self.raw_frame

                self.u_node_widgets_by_u_index[u_index] = active_widget

            wid = id(active_widget)
            self.f_widget_ids.append(wid)
            self.f_widget_ids_map[u_index] = wid
            self.f_widget_ids_map_inv[wid] = u_index
            if hasattr(active_widget, "load_node_data"):
                if active_widget.load_node_data(self.node, u_node, u_index,
                                                u_desc):
                    self.populate()
                    return
                active_widget.reload()
                active_widget.apply_style()

            self.build_f_widget_cache()
            # now that the field widgets are created, position them
            if self.show.get():
                self.pose_fields()
        except Exception:
            print(format_exc())

        self.sel_menu.update_label()
        if self.node is None:
            self.set_disabled(True)
        else:
            self.set_children_disabled(not self.node)

    def pose_fields(self):
        u_index = None if self.node is None else self.node.u_index
        w = self.u_node_widgets_by_u_index.get(u_index)
        for child in self.content.children.values():
            if child not in (w, self.comment_frame):
                child.pack_forget()

        if w:
            # by adding a fixed amount of padding, we fix a problem
            # with difficult to predict padding based on nesting
            w.pack(fill='x',
                   anchor='nw',
                   padx=self.vertical_padx,
                   pady=self.vertical_pady)

        self.content.pack(fill='x', anchor='nw', expand=True)
示例#4
0
class RefineryActionsWindow(tk.Toplevel, BinillaWidget):
    app_root = None
    settings = None
    renamable = True
    propagatable = False
    accept_rename = None
    accept_settings = None
    tag_index_ref = None

    rename_string = None
    newtype_string = None
    recursive_rename = None

    original_name = ""

    def __init__(self, *args, **kwargs):
        title = kwargs.pop('title', None)
        self.renamable = kwargs.pop('renamable', self.renamable)
        self.settings = settings = kwargs.pop('settings', {})
        self.tag_index_ref = kwargs.pop('tag_index_ref', self.tag_index_ref)
        # Zatarita, added propagatable kwarg for displaying checkbox
        self.propagatable = kwargs.pop('propagatable', self.propagatable)
        BinillaWidget.__init__(self, *args, **kwargs)
        tk.Toplevel.__init__(self, *args, **kwargs)

        try:
            self.iconbitmap(e_c.REFINERY_ICON_PATH)
        except Exception:
            if not e_c.IS_LNX:
                print("Could not load window icon.")

        self.bind('<Escape>', lambda e=None, s=self, *a, **kw: s.destroy())

        if self.app_root is None and hasattr(self.master, 'app_root'):
            self.app_root = self.master.app_root

        self.accept_rename = settings.get('accept_rename', tk.IntVar(self))
        self.accept_settings = settings.get('accept_settings', tk.IntVar(self))
        self.rename_string = settings.get('rename_string', tk.StringVar(self))
        self.newtype_string = settings.get('newtype_string',
                                           tk.StringVar(self))
        self.extract_to_dir = settings.get('out_dir', tk.StringVar(self))
        self.tagslist_path = settings.get('tagslist_path', tk.StringVar(self))
        self.extract_mode = settings.get('extract_mode', tk.StringVar(self))
        self.recursive_rename = tk.IntVar(self)
        self.resizable(1, 0)

        if title is None:
            title = self.rename_string.get()
            if not title:
                title = "Options"
        self.title(title)

        self.original_name = PureWindowsPath(self.rename_string.get())
        if self.tag_index_ref is not None:
            # this ActionsWindow is displaying a single tag. the
            # original name will have an extension. remove it
            self.original_name = self.original_name.with_suffix("")

        self.rename_string.set(str(self.original_name))
        self.newtype_string.set("")

        self.accept_rename.set(0)
        self.accept_settings.set(0)

        # frames
        self.rename_frame = tk.LabelFrame(self, text="Rename to")
        self.rename_frame_inner0 = tk.Frame(self.rename_frame)
        self.rename_frame_inner1 = tk.Frame(self.rename_frame)
        self.tags_list_frame = tk.LabelFrame(
            self, text="Tags list log(erase to disable logging)")
        self.extract_to_frame = tk.LabelFrame(self,
                                              text="Directory to extract to")
        self.settings_frame = tk.LabelFrame(self, text="Extract settings")

        self.button_frame = tk.Frame(self)
        self.accept_frame = tk.Frame(self.button_frame)
        self.cancel_frame = tk.Frame(self.button_frame)

        # rename
        self.rename_entry = tk.Entry(self.rename_frame_inner0,
                                     width=50,
                                     textvariable=self.rename_string)
        self.rename_button = tk.Button(self.rename_frame_inner0,
                                       text="Rename",
                                       command=self.rename,
                                       width=6)
        self.class_scroll_menu = ScrollMenu(self.rename_frame_inner1,
                                            menu_width=35)
        self.recursive_rename_cbtn = tk.Checkbutton(
            self.rename_frame_inner1,
            text="Recursive",
            variable=self.recursive_rename)

        if self.tag_index_ref:
            # populate the class_scroll_menu options
            opts = sorted([n for n in self.tag_index_ref.class_1.NAME_MAP])
            self.class_scroll_menu.set_options(opts)
            try:
                self.class_scroll_menu.sel_index = opts.index(
                    self.tag_index_ref.class_1.enum_name)
            except ValueError:
                pass

        # tags list
        self.tags_list_entry = tk.Entry(self.tags_list_frame,
                                        width=50,
                                        textvariable=self.tagslist_path)
        self.browse_tags_list_button = tk.Button(self.tags_list_frame,
                                                 text="Browse",
                                                 command=self.tags_list_browse)

        # extract to dir
        self.extract_to_entry = tk.Entry(self.extract_to_frame,
                                         width=50,
                                         textvariable=self.extract_to_dir)
        self.browse_extract_to_button = tk.Button(
            self.extract_to_frame,
            text="Browse",
            command=self.extract_to_browse)

        # settings
        self.recursive_cbtn = tk.Checkbutton(self.settings_frame,
                                             text="Recursive extraction",
                                             variable=settings.get(
                                                 "recursive", tk.IntVar(self)))
        self.overwrite_cbtn = tk.Checkbutton(
            self.settings_frame,
            text="Overwrite tags(not recommended)",
            variable=settings.get("overwrite", tk.IntVar(self)))
        self.do_printout_cbtn = tk.Checkbutton(
            self.settings_frame,
            text="Print extracted tag names",
            variable=settings.get("do_printout", tk.IntVar(self)))
        # zatarita added do_propegate_settings to place the check box in frame, and link value to variable
        self.do_propegate_settings = tk.Checkbutton(
            self.settings_frame,
            text="Use these settings for each item",
            variable=settings.get("propagate_settings", tk.IntVar(self)))

        # accept/cancel
        self.accept_button = tk.Button(self.accept_frame,
                                       text="Add to queue",
                                       command=self.add_to_queue,
                                       width=14)
        self.cancel_button = tk.Button(self.cancel_frame,
                                       text="Cancel",
                                       command=self.destroy,
                                       width=14)
        self.show_meta_button = tk.Button(self,
                                          text="Display metadata",
                                          command=self.show_meta)

        # pack everything
        # frames
        if self.renamable:
            self.rename_frame.pack(padx=4, pady=2, expand=True, fill="x")
        self.rename_frame_inner0.pack(expand=True, fill="x")
        self.rename_frame_inner1.pack(expand=True, fill="x")
        self.tags_list_frame.pack(padx=4, pady=2, expand=True, fill="x")
        self.extract_to_frame.pack(padx=4, pady=2, expand=True, fill="x")
        self.settings_frame.pack(padx=4, pady=2, expand=True, fill="x")

        self.button_frame.pack(pady=2, expand=True, fill="x")
        self.accept_frame.pack(padx=4, side='left', fill='x', expand=True)
        self.cancel_frame.pack(padx=4, side='right', fill='x', expand=True)

        # rename
        self.rename_entry.pack(padx=4, side='left', fill='x', expand=True)
        self.rename_button.pack(padx=4, side='left', fill='x')
        if self.tag_index_ref:
            self.class_scroll_menu.pack(padx=4, side='left', fill='x')
        #self.recursive_rename_cbtn.pack(padx=4, side='left', fill='x')

        # extract to
        self.extract_to_entry.pack(padx=4, side='left', fill='x', expand=True)
        self.browse_extract_to_button.pack(padx=4, side='left', fill='x')

        # tags list
        self.tags_list_entry.pack(padx=4, side='left', fill='x', expand=True)
        self.browse_tags_list_button.pack(padx=4, side='left', fill='x')

        # settings
        # zatarita, added propagatable check box to top of settings frame. (if enabled)
        if self.propagatable:
            self.do_propegate_settings.pack(padx=4, anchor='w')
        self.recursive_cbtn.pack(padx=4, anchor='w')
        self.overwrite_cbtn.pack(padx=4, anchor='w')
        self.do_printout_cbtn.pack(padx=4, anchor='w')

        # accept/cancel
        self.accept_button.pack(side='right')
        self.cancel_button.pack(side='left')
        if self.tag_index_ref is not None:
            self.show_meta_button.pack(padx=4, pady=4, expand=True, fill='x')

        # make the window not show up on the start bar
        self.transient(self.master)
        self.wait_visibility()
        self.lift()
        self.grab_set()
        self.apply_style()

        try:
            self.update()
            self.app_root.place_window_relative(self)
            # I would use focus_set, but it doesn't seem to always work
            self.accept_button.focus_force()
        except AttributeError:
            pass

    def apply_style(self, seen=None):
        BinillaWidget.apply_style(self, seen)
        self.update()
        w, h = self.winfo_reqwidth(), self.winfo_reqheight()
        self.geometry("%sx%s" % (w, h))
        self.minsize(width=w, height=h)

    def add_to_queue(self, e=None):
        self.accept_settings.set(1)
        self.destroy()

    def rename(self, e=None):
        if not self.renamable:
            return

        new_name = self.rename_string.get()
        new_name = str(PureWindowsPath(new_name)).lower().strip(".")
        if self.tag_index_ref is None and new_name:
            # directory of tags
            new_name += "\\"

        old_class = new_class = None
        try:
            old_class = self.tag_index_ref.class_1.enum_name
        except Exception:
            pass

        try:
            new_class = self.class_scroll_menu.get_option()
        except Exception:
            new_class = ""

        #new_name = os.path.splitext(new_name)[0]
        self.rename_string.set(new_name)
        str_len = len(new_name)
        if str_len > MAX_TAG_NAME_LEN:
            messagebox.showerror(
                "Max name length exceeded",
                ("The max length for a tag is limited to %s characters\n" +
                 "Remove %s characters(excluding extension).") %
                (MAX_TAG_NAME_LEN, str_len - MAX_TAG_NAME_LEN),
                parent=self)
            return
        elif is_protected_tag(new_name):
            messagebox.showerror("Invalid name",
                                 "The entered string is not a valid filename.",
                                 parent=self)
            return
        elif not str_len and self.tag_index_ref is not None:
            messagebox.showerror("Invalid name",
                                 "The entered string cannot be empty.",
                                 parent=self)
            return
        self.accept_rename.set(1)

        # change the type if applicable
        if new_class and new_class != old_class:
            self.newtype_string.set(new_class)

        self.destroy()

    def tags_list_browse(self):
        try:
            init_dir = os.path.dirname(self.tagslist_path.get())
        except Exception:
            init_dir = None
        dirpath = asksaveasfilename(
            initialdir=init_dir,
            parent=self,
            title="Select where to save the tag list log",
            filetypes=(("text log", "*.txt"), ("All", "*")))

        if not dirpath:
            return

        self.tagslist_path.set(str(Path(dirpath)))

    def extract_to_browse(self):
        dirpath = askdirectory(initialdir=self.extract_to_dir.get(),
                               parent=self,
                               title="Select the directory to extract tags to")

        if not dirpath:
            return

        self.extract_to_dir.set(str(Path(dirpath)))

    def show_meta(self):
        index_ref = self.tag_index_ref
        if not index_ref:
            return

        try:
            halo_map = self.settings.get("halo_map")
            if halo_map is None:
                print("Could not get map.")
                return

            disable_safe_mode = getattr(self.app_root, "disable_safe_mode")
            disable_tag_cleaning = getattr(self.app_root,
                                           "disable_tag_cleaning")

            meta = halo_map.get_meta(
                index_ref.id & 0xFFff,
                True,
                allow_corrupt=self.settings["allow_corrupt"].get(),
                disable_safe_mode=disable_safe_mode,
                disable_tag_cleaning=disable_tag_cleaning)
            if meta is None:
                print("Could not get meta.")
                return

            meta_tag = meta_tag_def.build()
            meta_tag.data.tagdata = meta
            tag_path = index_ref.path
            meta_tag.filepath = tag_path
            if index_ref.class_1.enum_name not in BAD_CLASSES:
                ext = ".%s" % index_ref.class_1.enum_name
            else:
                ext = ".INVALID"

            w = MetaWindow(self.app_root,
                           meta_tag,
                           engine=halo_map.engine,
                           tag_path=tag_path + ext)
            self.destroy()
            w.focus_set()
        except Exception:
            print(format_exc())
            return
示例#5
0
class ArrayFrame(container_frame.ContainerFrame):
    '''Used for array nodes. Displays a single element in
    the ArrayBlock represented by it, and contains a combobox
    for selecting which array element is displayed.'''

    sel_index = -1
    sel_menu = None
    populated = False
    option_cache = None
    options_sane = False

    def __init__(self, *args, **kwargs):
        kwargs.update(relief='flat',
                      bd=0,
                      highlightthickness=0,
                      bg=self.default_bg_color)
        field_widget.FieldWidget.__init__(self, *args, **kwargs)
        tk.Frame.__init__(self, *args, **e_c.fix_kwargs(**kwargs))

        show_frame = bool(
            kwargs.pop('show_frame', not self.blocks_start_hidden))
        if self.is_empty and self.hide_if_blank:
            show_frame = False

        self.show = tk.BooleanVar()
        self.show.set(show_frame)
        self.options_sane = False

        node_len = 0
        try:
            node_len = len(self.node)
        except Exception:
            pass

        self.sel_index = (node_len > 0) - 1

        # make the title, element menu, and all the buttons
        self.controls = tk.Frame(self, relief='raised', bd=self.frame_depth)
        self.title = title = tk.Frame(self.controls, relief='flat', bd=0)
        self.buttons = buttons = tk.Frame(self.controls, relief='flat', bd=0)

        toggle_text = '-' if show_frame else '+'

        self.title_label = tk.Label(
            title,
            text=self.gui_name,
            justify='left',
            anchor='w',
            width=self.title_size,
            font=self.get_font("frame_title"),
            disabledforeground=self.text_disabled_color)
        self.title_label.font_type = "frame_title"

        self.show_btn = ttk.Checkbutton(title,
                                        width=3,
                                        text=toggle_text,
                                        command=self.toggle_visible,
                                        style='ShowButton.TButton')
        self.sel_menu = ScrollMenu(title,
                                   f_widget_parent=self,
                                   sel_index=self.sel_index,
                                   max_index=node_len - 1,
                                   option_getter=self.get_options,
                                   callback=self.select_option)

        self.shift_up_btn = ttk.Button(title,
                                       width=7,
                                       text='Shift ▲',
                                       command=self.shift_entry_up)
        self.shift_down_btn = ttk.Button(buttons,
                                         width=7,
                                         text='Shift ▼',
                                         command=self.shift_entry_down)
        self.add_btn = ttk.Button(buttons,
                                  width=4,
                                  text='Add',
                                  command=self.add_entry)
        self.insert_btn = ttk.Button(buttons,
                                     width=6,
                                     text='Insert',
                                     command=self.insert_entry)
        self.duplicate_btn = ttk.Button(buttons,
                                        width=9,
                                        text='Duplicate',
                                        command=self.duplicate_entry)
        self.delete_btn = ttk.Button(buttons,
                                     width=6,
                                     text='Delete',
                                     command=self.delete_entry)
        self.delete_all_btn = ttk.Button(buttons,
                                         width=10,
                                         text='Delete all',
                                         command=self.delete_all_entries)

        self.import_btn = ttk.Button(buttons,
                                     width=6,
                                     text='Import',
                                     command=self.import_node)
        self.export_btn = ttk.Button(buttons,
                                     width=6,
                                     text='Export',
                                     command=self.export_node)

        # pack the title, menu, and all the buttons
        for w in (self.shift_down_btn, self.export_btn, self.import_btn,
                  self.delete_all_btn, self.delete_btn, self.duplicate_btn,
                  self.insert_btn, self.add_btn):
            w.pack(side="right", padx=(0, 4), pady=(2, 2))

        self.show_btn.pack(side="left")
        if self.gui_name != '':
            self.title_label.pack(side="left", fill="x", expand=True)

        self.sel_menu.pack(side="left", fill="x", expand=True, padx=(0, 4))
        self.shift_up_btn.pack(side="right", padx=(0, 1), pady=(2, 2))

        self.title.pack(fill="x", expand=True, padx=0)
        self.buttons.pack(fill="x", expand=True, padx=0)
        self.controls.pack(fill="x", expand=True, padx=0)

        self.populate()
        self._initialized = True

    @property
    def is_empty(self):
        if getattr(self, "node", None) is None:
            return True
        return len(self.node) == 0

    def load_node_data(self, parent, node, attr_index, desc=None):
        field_widget.FieldWidget.load_node_data(self, parent, node, attr_index,
                                                desc)
        sub_node = attr_index = None
        if self.node:
            attr_index = self.sel_index
            if attr_index in range(len(self.node)):
                sub_node = self.node[attr_index]
            else:
                attr_index = len(self.node) - 1
                if attr_index < 0:
                    attr_index = None

        if self.sel_menu is not None:
            self.options_sane = self.sel_menu.options_menu_sane = False

        for wid in self.f_widgets:
            # there must be only one entry in self.f_widgets
            w = self.f_widgets[wid]
            if w.load_node_data(self.node, sub_node, attr_index):
                return True

        return False

    def unload_node_data(self):
        self.sel_menu.update_label(" ")
        container_frame.ContainerFrame.unload_node_data(self)

    def set_disabled(self, disable=True):
        disable = disable or not self.editable
        if self.node is None and not disable:
            return

        if getattr(self, "sel_menu", None):
            self.sel_menu.set_disabled(disable)

        if bool(disable) == self.disabled:
            pass
        elif not disable:
            self.set_all_buttons_disabled(False)
            self.disable_unusable_buttons()
        else:
            new_state = tk.DISABLED if disable else tk.NORMAL
            for w in (self.shift_up_btn, self.shift_down_btn, self.add_btn,
                      self.insert_btn, self.duplicate_btn, self.delete_btn,
                      self.delete_all_btn):
                if w:
                    w.config(state=new_state)

        container_frame.ContainerFrame.set_disabled(self, disable)

    def apply_style(self, seen=None):
        container_frame.ContainerFrame.apply_style(self, seen)
        self.controls.config(bd=self.frame_depth, bg=self.frame_bg_color)
        self.title.config(bg=self.frame_bg_color)
        self.title_label.config(bg=self.frame_bg_color)
        self.buttons.config(bg=self.frame_bg_color)
        #if self.show.get():
        #    self.pose_fields()

    def destroy(self):
        # These will linger and take up RAM, even if the widget is destroyed.
        # Need to remove the references manually
        self.option_cache = None
        container_frame.ContainerFrame.destroy(self)

    def export_node(self):
        try:
            # pass call to the export_node method of the array entry's widget
            w = self.f_widgets[self.f_widget_ids[0]]
        except Exception:
            return
        w.export_node()

    def import_node(self):
        try:
            # pass call to the import_node method of the array entry's widget
            w = self.f_widgets[self.f_widget_ids[0]]
        except Exception:
            return
        w.import_node()

    def get_options(self, opt_index=None):
        '''
        Returns a list of the option strings sorted by option index.
        '''
        if (self.option_cache is None or not self.options_sane
                or opt_index is not None):
            result = self.generate_options(opt_index)
            if opt_index is not None:
                return result

        if opt_index is None:
            return self.option_cache
        elif opt_index == e_c.ACTIVE_ENUM_NAME:
            opt_index = self.sel_index

        if opt_index < 0: opt_index = -1

        return self.option_cache.get(opt_index)

    def generate_options(self, opt_index=None):
        # sort the options by value(values are integers)
        options = {i: n for n, i in self.desc.get('NAME_MAP', {}).items()}

        if self.node:
            node, desc = self.node, self.desc
            sub_desc = desc['SUB_STRUCT']
            def_struct_name = sub_desc['NAME']
            if self.use_gui_names and 'GUI_NAME' in sub_desc:
                def_struct_name = sub_desc['GUI_NAME']

            options_to_generate = range(len(node))
            if opt_index is not None:
                options_to_generate = ((opt_index, ) if opt_index
                                       in options_to_generate else ())

            for i in options_to_generate:
                if i in options:
                    continue
                sub_node = node[i]
                if not hasattr(sub_node, 'desc'):
                    continue
                sub_desc = sub_node.desc
                sub_struct_name = sub_desc.get('GUI_NAME', sub_desc['NAME'])
                if sub_struct_name == def_struct_name:
                    continue

                options[i] = sub_struct_name

        if opt_index is None:
            self.options_sane = True
            self.option_cache = options
            if self.sel_menu is not None:
                self.sel_menu.options_menu_sane = False
                self.sel_menu.max_index = len(node) - 1
            return options
        return options.get(opt_index, None)

    def set_shift_up_disabled(self, disable=True):
        '''
        Disables the move up button if disable is True. Enables it if not.
        '''
        if disable: self.shift_up_btn.config(state="disabled")
        else: self.shift_up_btn.config(state="normal")

    def set_shift_down_disabled(self, disable=True):
        '''
        Disables the move down button if disable is True. Enables it if not.
        '''
        if disable: self.shift_down_btn.config(state="disabled")
        else: self.shift_down_btn.config(state="normal")

    def set_add_disabled(self, disable=True):
        '''Disables the add button if disable is True. Enables it if not.'''
        if disable: self.add_btn.config(state="disabled")
        else: self.add_btn.config(state="normal")

    def set_insert_disabled(self, disable=True):
        '''Disables the insert button if disable is True. Enables it if not.'''
        if disable: self.insert_btn.config(state="disabled")
        else: self.insert_btn.config(state="normal")

    def set_duplicate_disabled(self, disable=True):
        '''
        Disables the duplicate button if disable is True. Enables it if not.
        '''
        if disable: self.duplicate_btn.config(state="disabled")
        else: self.duplicate_btn.config(state="normal")

    def set_delete_disabled(self, disable=True):
        '''Disables the delete button if disable is True. Enables it if not.'''
        if disable: self.delete_btn.config(state="disabled")
        else: self.delete_btn.config(state="normal")

    def set_delete_all_disabled(self, disable=True):
        '''
        Disables the delete_all button if disable is True. Enables it if not.
        '''
        if disable: self.delete_all_btn.config(state="disabled")
        else: self.delete_all_btn.config(state="normal")

    def edit_apply(self=None, *, edit_state, undo=True):
        state = edit_state

        edit_type = state.edit_type
        i = state.attr_index
        undo_node = state.undo_node
        redo_node = state.redo_node

        edit_info = state.edit_info
        sel_index = edit_info.get('sel_index', 0)

        w, node = field_widget.FieldWidget.get_widget_and_node(
            nodepath=state.nodepath, tag_window=state.tag_window)

        if edit_type == 'shift_up':
            node[i], node[i - 1] = node[i - 1], node[i]
        elif edit_type == 'shift_down':
            node[i], node[i + 1] = node[i + 1], node[i]
        elif edit_type in ('add', 'insert', 'duplicate'):
            if undo:
                sel_index = None
                node.pop(i)
            else:
                node.insert(i, redo_node)
        elif edit_type == 'delete':
            if undo:
                node.insert(i, undo_node)
            else:
                sel_index = None
                node.pop(i)
        elif edit_type == 'delete_all':
            if undo:
                node[:] = undo_node
            else:
                del node[:]
                sel_index = None
        else:
            raise TypeError('Unknown edit_state type')

        if w is not None:
            try:
                if w.desc is not state.desc:
                    return

                if sel_index is None:
                    pass
                elif edit_type in ('add', 'insert', 'duplicate', 'delete'):
                    w.sel_index = sel_index
                elif edit_type in ('shift_up', 'shift_down'):
                    w.sel_index = sel_index
                    if undo:
                        pass
                    elif 'down' in edit_type:
                        w.sel_index += 1
                    else:
                        w.sel_index -= 1

                max_index = len(node) - 1
                w.sel_menu.max_index = max_index
                w.options_sane = w.sel_menu.options_menu_sane = False
                if w.sel_index < 0:
                    w.select_option(0, force=True)
                elif w.sel_index > max_index:
                    w.select_option(max_index, force=True)
                else:
                    w.select_option(w.sel_index, force=True)

                w.needs_flushing = False
                w.set_edited()
            except Exception:
                print(format_exc())

    def edit_create(self, **kwargs):
        # add own stuff
        kwargs.setdefault("sel_index", self.sel_index)
        field_widget.FieldWidget.edit_create(self, **kwargs)

    def shift_entry_up(self):
        if not hasattr(self.node, '__len__') or len(self.node) < 2:
            return

        node = self.node
        index = self.sel_index
        if index <= 0:
            return

        self.set_edited()  # do this first so the TagWindow detects that
        #                   the title needs to be updated with an asterisk
        self.edit_create(edit_type='shift_up', attr_index=index)
        node[index], node[index - 1] = node[index - 1], node[index]

        self.sel_index = self.sel_menu.sel_index = index - 1
        self.options_sane = self.sel_menu.options_menu_sane = False
        self.sel_menu.update_label()

    def shift_entry_down(self):
        if not hasattr(self.node, '__len__') or len(self.node) < 2:
            return

        node = self.node
        index = self.sel_index
        if index >= len(node) - 1:
            return

        self.set_edited()  # do this first so the TagWindow detects that
        #                   the title needs to be updated with an asterisk
        self.edit_create(edit_type='shift_down', attr_index=index)
        node[index], node[index + 1] = node[index + 1], node[index]

        self.sel_index = self.sel_menu.sel_index = index + 1
        self.options_sane = self.sel_menu.options_menu_sane = False
        self.sel_menu.update_label()

    def add_entry(self):
        if not hasattr(self.node, '__len__'):
            return

        field_max = self.field_max
        if field_max is not None and len(self.node) >= field_max:
            if self.enforce_max:
                return

        attr_index = len(self.node)
        self.set_edited()  # do this first so the TagWindow detects that
        #                   the title needs to be updated with an asterisk
        self.node.append()

        self.edit_create(edit_type='add',
                         attr_index=attr_index,
                         redo_node=self.node[attr_index],
                         sel_index=attr_index)

        self.options_sane = self.sel_menu.options_menu_sane = False
        self.set_all_buttons_disabled(self.disabled)
        self.disable_unusable_buttons()
        self.select_option(len(self.node) - 1, True)

    def insert_entry(self):
        if not hasattr(self.node, '__len__'):
            return

        field_max = self.field_max
        if field_max is not None and len(self.node) >= field_max:
            if self.enforce_max:
                return

        attr_index = self.sel_index = max(self.sel_index, 0)
        self.set_edited()  # do this first so the TagWindow detects that
        #                   the title needs to be updated with an asterisk
        self.node.insert(attr_index)

        self.edit_create(edit_type='insert',
                         attr_index=attr_index,
                         redo_node=self.node[attr_index],
                         sel_index=attr_index)

        self.options_sane = self.sel_menu.options_menu_sane = False
        self.set_all_buttons_disabled(self.disabled)
        self.disable_unusable_buttons()
        self.select_option(attr_index, True)  # select the new entry

    def duplicate_entry(self):
        if not hasattr(self.node, '__len__') or len(self.node) < 1:
            return

        field_max = self.field_max
        if field_max is not None and len(self.node) >= field_max:
            if self.enforce_max:
                return

        self.sel_index = self.sel_menu.sel_index = max(self.sel_index, 0)
        self.set_edited()  # do this first so the TagWindow detects that
        #                   the title needs to be updated with an asterisk
        new_subnode = deepcopy(self.node[self.sel_index])
        attr_index = len(self.node)

        self.edit_create(edit_type='duplicate',
                         attr_index=attr_index,
                         redo_node=new_subnode,
                         sel_index=attr_index)

        self.node.append(new_subnode)

        self.options_sane = self.sel_menu.options_menu_sane = False
        self.set_all_buttons_disabled(self.disabled)
        self.disable_unusable_buttons()
        self.select_option(attr_index, True)

    def delete_entry(self):
        if not hasattr(self.node, '__len__') or len(self.node) == 0:
            return

        field_min = self.field_min
        if field_min is None:
            field_min = 0

        if len(self.node) <= field_min:
            if self.enforce_min:
                return

        if not len(self.node):
            self.sel_menu.disable()
            return

        attr_index = max(self.sel_index, 0)

        self.set_edited()  # do this first so the TagWindow detects that
        #                   the title needs to be updated with an asterisk
        self.edit_create(edit_type='delete',
                         undo_node=self.node[attr_index],
                         attr_index=attr_index,
                         sel_index=attr_index)

        del self.node[attr_index]
        attr_index = max(-1, min(len(self.node) - 1, attr_index))

        self.options_sane = self.sel_menu.options_menu_sane = False
        self.select_option(attr_index, True)
        self.set_all_buttons_disabled(self.disabled)
        self.disable_unusable_buttons()

    def delete_all_entries(self):
        if not hasattr(self.node, '__len__') or len(self.node) == 0:
            return

        field_min = self.field_min
        if field_min is None:
            field_min = 0

        if len(self.node) <= field_min:
            if self.enforce_min:
                return

        if not len(self.node):
            self.sel_menu.disable()
            return

        self.set_edited()  # do this first so the TagWindow detects that
        #                   the title needs to be updated with an asterisk
        self.edit_create(edit_type='delete_all', undo_node=tuple(self.node[:]))

        del self.node[:]

        self.options_sane = self.sel_menu.options_menu_sane = False
        self.set_all_buttons_disabled(self.disabled)
        self.disable_unusable_buttons()
        self.select_option(self.sel_index, True)

    def set_all_buttons_disabled(self, disable=False):
        for btn in (self.add_btn, self.insert_btn, self.duplicate_btn,
                    self.delete_btn, self.delete_all_btn, self.import_btn,
                    self.export_btn, self.shift_up_btn, self.shift_down_btn):
            if disable:
                btn.config(state=tk.DISABLED)
            else:
                btn.config(state=tk.NORMAL)

    def disable_unusable_buttons(self):
        no_node = not hasattr(self.node, '__len__')
        if no_node or len(self.node) < 2:
            self.set_shift_up_disabled()
            self.set_shift_down_disabled()

        if no_node or (isinstance(self.desc.get('SIZE'), int)
                       and self.enforce_min):
            self.set_add_disabled()
            self.set_insert_disabled()
            self.set_duplicate_disabled()
            self.set_delete_disabled()
            self.set_delete_all_disabled()
            return

        field_max = self.field_max
        field_min = self.field_min
        if field_min is None or field_min < 0: field_min = 0
        enforce_min = self.enforce_min or field_min == 0
        enforce_max = self.enforce_max

        if field_max is not None and len(
                self.node) >= field_max and enforce_max:
            self.set_add_disabled()
            self.set_insert_disabled()
            self.set_duplicate_disabled()

        if len(self.node) <= field_min and (enforce_min or not self.node):
            self.set_delete_disabled()
            self.set_delete_all_disabled()

        if not self.node:
            self.set_export_disabled()
            self.set_import_disabled()
            self.set_duplicate_disabled()

    def populate(self):
        node = self.node
        desc = self.desc
        sub_node = None
        sub_desc = desc['SUB_STRUCT']

        if node and self.sel_index in range(len(node)):
            sub_node = node[self.sel_index]
            sub_desc = desc['SUB_STRUCT']
            if hasattr(sub_node, 'desc'):
                sub_desc = sub_node.desc

        if self.content in (None, self):
            self.content = tk.Frame(self,
                                    relief="sunken",
                                    bd=self.frame_depth,
                                    bg=self.default_bg_color)

        self.sel_menu.default_text = sub_desc.get('GUI_NAME',
                                                  sub_desc.get('NAME', ""))
        self.sel_menu.update_label()
        self.disable_unusable_buttons()

        rebuild = not bool(self.f_widgets)
        if hasattr(node, '__len__') and len(node) == 0:
            # disabling existing widgets
            self.sel_index = -1
            self.sel_menu.max_index = -1
            if self.f_widgets:
                self.unload_child_node_data()
        else:
            for w in self.f_widgets:
                if getattr(w, "desc", None) != sub_desc:
                    rebuild = True
                    break

        if rebuild:
            # destroy existing widgets and make new ones
            self.populated = False
            self.f_widget_ids = []
            self.f_widget_ids_map = {}
            self.f_widget_ids_map_inv = {}

            # destroy any child widgets of the content
            for c in list(self.f_widgets.values()):
                c.destroy()

            for w in (self, self.content, self.title, self.title_label,
                      self.controls, self.buttons):
                w.tooltip_string = self.desc.get('TOOLTIP')

            self.display_comment(self.content)

            widget_cls = self.widget_picker.get_widget(sub_desc)
            try:
                widget = widget_cls(self.content,
                                    node=sub_node,
                                    parent=node,
                                    show_title=False,
                                    dont_padx_fields=True,
                                    attr_index=self.sel_index,
                                    tag_window=self.tag_window,
                                    f_widget_parent=self,
                                    disabled=self.disabled)
            except Exception:
                print(format_exc())
                widget = data_frame.NullFrame(self.content,
                                              node=sub_node,
                                              parent=node,
                                              show_title=False,
                                              dont_padx_fields=True,
                                              attr_index=self.sel_index,
                                              tag_window=self.tag_window,
                                              f_widget_parent=self,
                                              disabled=self.disabled)

            wid = id(widget)
            self.f_widget_ids.append(wid)
            self.f_widget_ids_map[self.sel_index] = wid
            self.f_widget_ids_map_inv[wid] = self.sel_index

            self.populated = True
            self.build_f_widget_cache()

            # now that the field widgets are created, position them
            if self.show.get():
                self.pose_fields()

        if self.node is None:
            self.set_disabled(True)
        else:
            self.set_children_disabled(not self.node)

    def reload(self):
        '''Resupplies the nodes to the widgets which display them.'''
        try:
            node = self.node if self.node else ()
            desc = self.desc

            is_empty = len(node) == 0
            field_max = self.field_max
            field_min = self.field_min
            if field_min is None: field_min = 0

            self.set_all_buttons_disabled(self.disabled)
            self.disable_unusable_buttons()

            if is_empty:
                self.sel_menu.sel_index = -1
                self.sel_menu.max_index = -1
                # if there is no index to select, destroy the content
                if self.sel_index != -1:
                    self.sel_index = -1

                self.unload_child_node_data()
            else:
                self.f_widget_ids_map = {}
                self.f_widget_ids_map_inv = {}

            self.sel_menu.sel_index = self.sel_index
            self.sel_menu.max_index = len(node) - 1

            sub_node = None
            sub_desc = desc['SUB_STRUCT']
            if node and self.sel_index in range(len(node)):
                sub_node = node[self.sel_index]
                sub_desc = desc['SUB_STRUCT']
                if hasattr(sub_node, 'desc'):
                    sub_desc = sub_node.desc

            for wid in self.f_widget_ids:
                w = self.f_widgets[wid]
                wid = id(w)

                if node and self.sel_index not in range(len(node)):
                    # current selection index is invalid. call select_option
                    # to reset it to some valid option. Don't reload though,
                    # as we will be either reloading or repopulating below.
                    self.select_option(force=True, reload=False)

                self.f_widget_ids_map[self.sel_index] = wid
                self.f_widget_ids_map_inv[wid] = self.sel_index

                # if the descriptors are different, gotta repopulate!
                if w.load_node_data(node, sub_node, self.sel_index, sub_desc):
                    self.populate()
                    self.apply_style()
                    return

                w.reload()
                if w.desc.get("PORTABLE", True) and self.node:
                    self.set_import_disabled(False)
                    self.set_export_disabled(False)
                else:
                    self.set_import_disabled()
                    self.set_export_disabled()

            self.sel_menu.update_label()
            if self.node is None:
                self.set_disabled(True)
            else:
                self.set_children_disabled(not self.node)
        except Exception:
            print(format_exc())

    def pose_fields(self):
        # there should only be one wid in here, but for
        # the sake of consistancy we'll loop over them.
        for wid in self.f_widget_ids:
            w = self.f_widgets[wid]

            # by adding a fixed amount of padding, we fix a problem
            # with difficult to predict padding based on nesting
            w.pack(fill='x',
                   side='top',
                   expand=True,
                   padx=self.vertical_padx,
                   pady=self.vertical_pady)

        # if there are no children in the content, we need to
        # pack in SOMETHING, update the idletasks, and then
        # destroy that something to resize the content frame
        if not self.f_widgets:
            f = tk.Frame(self.content, width=0, height=0, bd=0)
            f.pack()
            self.content.update_idletasks()
            f.destroy()

        self.content.pack(fill='both', side='top', anchor='nw', expand=True)

    def select_option(self, opt_index=None, force=False, reload=True):
        node = self.node if self.node else ()
        curr_index = self.sel_index
        if opt_index is None:
            opt_index = curr_index

        if opt_index < 0:
            opt_index = 0

        if opt_index == curr_index and not force:
            return
        elif not node:
            opt_index = -1
        elif opt_index not in range(len(node)):
            opt_index = len(node) - 1

        # flush any lingering changes
        self.flush()
        self.sel_index = opt_index
        self.sel_menu.sel_index = opt_index
        self.sel_menu.max_index = len(node) - 1
        if reload:
            self.reload()

        self.sel_menu.update_label()

    @property
    def visible_field_count(self):
        # array frames only display one item at a time
        return 1
示例#6
0
class EnumFrame(data_frame.DataFrame):
    '''Used for enumerator nodes. When clicked, creates
    a dropdown box of all available enumerator options.'''

    option_cache = None
    sel_menu = None

    def __init__(self, *args, **kwargs):
        kwargs.update(relief='flat', bd=0, highlightthickness=0)
        data_frame.DataFrame.__init__(self, *args, **kwargs)

        try:
            sel_index = self.node.get_index()
        except Exception:
            sel_index = -1

        # make the widgets
        self.content = tk.Frame(self, relief='flat', bd=0)

        self.display_comment()

        self.title_label = tk.Label(
            self.content,
            text=self.gui_name,
            justify='left',
            anchor='w',
            width=self.title_size,
            disabledforeground=self.text_disabled_color)
        self.sel_menu = ScrollMenu(self.content,
                                   f_widget_parent=self,
                                   menu_width=self.widget_width,
                                   sel_index=sel_index,
                                   max_index=self.desc.get('ENTRIES', 0) - 1,
                                   disabled=self.disabled,
                                   default_text="<INVALID>",
                                   option_getter=self.get_options,
                                   callback=self.select_option)

        if self.gui_name != '':
            self.title_label.pack(side="left", fill="x")
        self.content.pack(fill="x", expand=True)
        self.sel_menu.pack(side="left", fill="x")
        self.populate()
        self.pose_fields()
        self._initialized = True

    @property
    def widget_width(self):
        desc = self.desc
        width = desc.get('WIDGET_WIDTH', self.enum_menu_width)
        if width <= 0:
            for s in self.get_options().values():
                width = max(width, len(s))

        return width

    def apply_style(self, seen=None):
        self.sel_menu.menu_width = self.widget_width
        data_frame.DataFrame.apply_style(self, seen)

    def unload_node_data(self):
        field_widget.FieldWidget.unload_node_data(self)
        self.sel_menu.update_label(" ")

    def set_disabled(self, disable=True):
        disable = disable or not self.editable
        if self.node is None and not disable:
            return

        if bool(disable) != self.disabled and getattr(self, "sel_menu", None):
            self.sel_menu.set_disabled(disable)

        data_frame.DataFrame.set_disabled(self, disable)

    def flush(self):
        pass

    def edit_apply(self=None, *, edit_state, undo=True):
        state = edit_state
        w, node = field_widget.FieldWidget.get_widget_and_node(
            nodepath=state.nodepath, tag_window=state.tag_window)

        if undo:
            node.data = state.undo_node
        else:
            node.data = state.redo_node

        if w is not None:
            try:
                if w.desc is not state.desc:
                    return
                try:
                    w.sel_menu.sel_index = node.get_index()
                except Exception:
                    # option doesnt exist, so make the displayed one blank
                    w.sel_menu.sel_index = -1

                w.needs_flushing = False
                w.sel_menu.update_label()
                w.set_edited()
            except Exception:
                print(format_exc())

    def edit_create(self, **kwargs):
        # add own stuff
        kwargs.update(sel_index=self.sel_menu.sel_index,
                      max_index=self.sel_menu.max_index)
        field_widget.FieldWidget.edit_create(self, **kwargs)

    def get_options(self, opt_index=None):
        '''
        Returns a list of the option strings sorted by option index.
        '''
        if self.option_cache is None or opt_index is not None:
            return self.generate_options(opt_index)

        if opt_index is None:
            return self.option_cache
        elif opt_index == e_c.ACTIVE_ENUM_NAME:
            opt_index = self.sel_menu.sel_index

        return self.option_cache.get(opt_index, None)

    def generate_options(self, opt_index=None):
        desc = self.desc
        options = {}
        option_count = desc.get('ENTRIES', 0)
        options_to_generate = range(option_count)
        if opt_index is not None:
            options_to_generate = ((opt_index, )
                                   if opt_index in options_to_generate else ())

        # sort the options by value(values are integers)
        use_gui_names = self.use_gui_names
        for i in options_to_generate:
            opt = desc[i]
            if use_gui_names and 'GUI_NAME' in opt:
                options[i] = opt['GUI_NAME']
            else:
                options[i] = opt.get('NAME', '<UNNAMED %s>' % i)\
                             .replace('_', ' ')

        if opt_index is None:
            self.options_sane = True
            self.option_cache = options
            if self.sel_menu is not None:
                self.sel_menu.options_menu_sane = False
                self.sel_menu.max_index = option_count - 1
            return options
        return options.get(opt_index, None)

    def reload(self):
        try:
            if self.disabled == self.sel_menu.disabled:
                pass
            elif self.disabled:
                self.sel_menu.disable()
            else:
                self.sel_menu.enable()

            for w in (self, self.content, self.sel_menu, self.title_label):
                w.tooltip_string = self.desc.get('TOOLTIP')

            options = self.get_options()
            option_count = len(options)
            if not option_count:
                self.sel_menu.sel_index = -1
                self.sel_menu.max_index = -1
                return

            try:
                curr_index = self.node.get_index()
            except Exception:
                curr_index = -1

            self.sel_menu.sel_index = curr_index
            self.sel_menu.max_index = option_count - 1
            self.sel_menu.update_label()
        except Exception:
            print(format_exc())

    populate = reload

    def pose_fields(self):
        pass

    def select_option(self, opt_index=None):
        if None in (self.parent, self.node):
            return

        node = self.node
        curr_index = self.sel_menu.sel_index

        if (opt_index is None or opt_index < 0
                or opt_index > self.sel_menu.max_index):
            print("Invalid option index '%s'" % opt_index)
            return

        self.sel_menu.sel_index = opt_index

        undo_node = self.node.data
        self.node.set_to(opt_index)

        # make an edit state
        if undo_node != self.node.data:
            self.set_edited()
            self.edit_create(undo_node=undo_node, redo_node=self.node.data)

        self.sel_menu.update_label()
示例#7
0
class DynamicEnumFrame(EnumFrame):
    options_sane = False

    def __init__(self, *args, **kwargs):
        kwargs.update(relief='flat', bd=0, highlightthickness=0)
        data_frame.DataFrame.__init__(self, *args, **kwargs)

        sel_index = -1 if self.node is None else self.node + 1

        # make the widgets
        self.content = tk.Frame(self, relief='flat', bd=0)
        self.title_label = tk.Label(
            self.content,
            text=self.gui_name,
            justify='left',
            anchor='w',
            width=self.title_size,
            disabledforeground=self.text_disabled_color)
        self.sel_menu = ScrollMenu(self.content,
                                   f_widget_parent=self,
                                   menu_width=self.widget_width,
                                   sel_index=sel_index,
                                   max_index=0,
                                   disabled=self.disabled,
                                   default_text="<INVALID>",
                                   option_getter=self.get_options,
                                   callback=self.select_option)

        self.sel_menu.bind('<FocusIn>', self.flag_sanity_change)
        self.sel_menu.arrow_button.bind('<FocusIn>', self.flag_sanity_change)
        self.sel_menu.bind('<Enter>', self.flag_sanity_change)
        self.sel_menu.arrow_button.bind('<Enter>', self.flag_sanity_change)
        self.sel_menu.options_volatile = 'DYN_NAME_PATH' in self.desc

        if self.gui_name != '':
            self.title_label.pack(side="left", fill="x")
        self.content.pack(fill="x", expand=True)
        self.sel_menu.pack(side="left", fill="x")
        self.populate()
        self._initialized = True

    def edit_apply(self=None, *, edit_state, undo=True):
        state = edit_state
        attr_index = state.attr_index

        w_parent, parent = field_widget.FieldWidget.get_widget_and_node(
            nodepath=state.nodepath, tag_window=state.tag_window)

        if undo:
            parent[attr_index] = state.undo_node
        else:
            parent[attr_index] = state.redo_node

        if w_parent is not None:
            try:
                w = w_parent.f_widgets[w_parent.f_widget_ids_map[attr_index]]
                if w.desc is not state.desc:
                    return

                w.sel_menu.sel_index = parent[attr_index] + 1
                w.needs_flushing = False
                w.sel_menu.update_label()
                w.set_edited()
            except Exception:
                print(format_exc())

    def generate_options(self, opt_index=None):
        desc = self.desc
        options = {0: "-1. NONE"}

        dyn_name_path = desc.get('DYN_NAME_PATH')
        if self.node is None:
            if opt_index is None:
                return options
            return None
        elif not dyn_name_path:
            print("Missing DYN_NAME_PATH path in dynamic enumerator.")
            print(self.parent.get_root().def_id, self.name)
            if opt_index is None:
                return options
            return None

        try:
            p_out, p_in = dyn_name_path.split('[DYN_I]')

            # We are ALWAYS going to go to the parent, so we need to slice
            if p_out.startswith('..'): p_out = p_out.split('.', 1)[-1]
            array = self.parent.get_neighbor(p_out)

            options_to_generate = range(len(array))
            if opt_index is not None:
                options_to_generate = ((opt_index - 1, ) if opt_index -
                                       1 in options_to_generate else ())

            for i in options_to_generate:
                name = array[i].get_neighbor(p_in)
                if isinstance(name, list):
                    name = repr(name).strip("[").strip("]")
                else:
                    name = str(name)

                options[i + 1] = '%s. %s' % (i, name.split('\n')[0])
            option_count = len(array) + 1
        except Exception:
            print(format_exc())
            option_count = 1

        if opt_index is None:
            self.option_cache = options
            self.options_sane = True
            if self.sel_menu is not None:
                self.sel_menu.options_menu_sane = False
                self.sel_menu.max_index = option_count - 1
            return options
        return options.get(opt_index, None)

    def reload(self):
        try:
            self.options_sane = False
            if self.disabled == self.sel_menu.disabled:
                pass
            elif self.disabled:
                self.sel_menu.disable()
            else:
                self.sel_menu.enable()

            self.generate_options()
            if self.node is not None:
                self.sel_menu.sel_index = self.node + 1
            self.sel_menu.update_label()
        except Exception:
            print(format_exc())

    populate = reload

    def select_option(self, opt_index=None):
        if None in (self.parent, self.node):
            return

        # make an edit state
        if self.node != opt_index - 1:
            self.set_edited()
            self.edit_create(undo_node=self.node, redo_node=opt_index - 1)

        self.sel_menu.sel_index = opt_index

        # since the node value is actually signed and can be -1, we'll
        # set entry 0 to be a node value of -1 and all other values
        # are one less than the entry index they are located in.
        self.node = self.parent[self.attr_index] = opt_index - 1
        self.sel_menu.update_label()

    def flag_sanity_change(self, e=None):
        self.options_sane = self.sel_menu.options_menu_sane = (
            not self.sel_menu.options_volatile)

        if not self.options_sane:
            self.option_cache = None
示例#8
0
class BitmapDisplayFrame(BinillaWidget, tk.Frame):
    app_root = None
    root_frame_id = None

    bitmap_index = None  # index of the bitmap being displayed
    mipmap_index = None  # the mip level to display of that bitmap
    channel_index = None  # how to display the bitmap:
    #                       0=RGB or L, 1=A, 2=ARGB or AL, 3=R, 4=G, 5=B
    depth_index = None  # since 3d bitmaps must be viewed in 2d slices,
    #                       this is the depth of the slice to display.
    cube_display_index = None  # mode to display the cubemap in:
    #                            0 == horizontal, 1 == vertical,
    #                            2 == linear strip

    prev_bitmap_index = None
    prev_mipmap_index = None
    prev_channel_index = None
    prev_depth_index = None
    prev_cube_display_index = None
    changing_settings = False

    curr_depth = 0
    depth_canvas = None
    depth_canvas_id = None
    depth_canvas_image_id = None

    default_bitmap_mapping = ((0, ), )

    cubemap_cross_mapping = (
        (-1, 2),
        (1, 4, 0, 5),
        (-1, 3),
    )

    cubemap_strip_mapping = ((0, 1, 2, 3, 4, 5), )

    _image_handlers = None
    image_canvas_ids = ()  # does NOT include the depth_canvas_id
    textures = ()  # List of textures ready to be loaded into arbytmap.
    # Structure is as follows:  [ (tex_block0, tex_info0),
    #                             (tex_block1, tex_info1),
    #                             (tex_block2, tex_info2), ... ]

    temp_root = os.path.join(tempfile.gettempdir(), "arbytmaps")
    temp_dir = ''

    def __init__(self, master, *args, **kwargs):
        BinillaWidget.__init__(self)
        self.temp_root = kwargs.pop('temp_root', self.temp_root)
        textures = kwargs.pop('textures', ())
        app_root = kwargs.pop('app_root', ())

        self.image_canvas_ids = []
        self.textures = []
        self._image_handlers = {}

        temp_name = str(int(random.random() * (1 << 32)))
        self.temp_dir = os.path.join(self.temp_root, temp_name)

        kwargs.update(relief='flat',
                      bd=self.frame_depth,
                      bg=self.default_bg_color)
        tk.Frame.__init__(self, master, *args, **kwargs)

        self.bitmap_index = tk.IntVar(self)
        self.mipmap_index = tk.IntVar(self)
        self.depth_index = tk.IntVar(self)
        self.channel_index = tk.IntVar(self)
        self.cube_display_index = tk.IntVar(self)
        self.root_canvas = tk.Canvas(self, highlightthickness=0)
        self.root_frame = tk.Frame(self.root_canvas, highlightthickness=0)

        # create the root_canvas and the root_frame within the canvas
        self.controls_frame0 = tk.Frame(self.root_frame, highlightthickness=0)
        self.controls_frame1 = tk.Frame(self.root_frame, highlightthickness=0)
        self.controls_frame2 = tk.Frame(self.root_frame, highlightthickness=0)
        self.image_root_frame = tk.Frame(self.root_frame, highlightthickness=0)
        self.image_canvas = tk.Canvas(self.image_root_frame,
                                      highlightthickness=0,
                                      bg=self.bitmap_canvas_bg_color)
        self.depth_canvas = tk.Canvas(self.image_canvas,
                                      highlightthickness=0,
                                      bg=self.bitmap_canvas_bg_color)

        self.bitmap_menu = ScrollMenu(self.controls_frame0,
                                      menu_width=7,
                                      variable=self.bitmap_index,
                                      can_scroll=True)
        self.mipmap_menu = ScrollMenu(self.controls_frame1,
                                      menu_width=7,
                                      variable=self.mipmap_index,
                                      can_scroll=True)
        self.depth_menu = ScrollMenu(self.controls_frame2,
                                     menu_width=7,
                                     variable=self.depth_index,
                                     can_scroll=True)
        self.channel_menu = ScrollMenu(self.controls_frame0,
                                       menu_width=9,
                                       variable=self.channel_index,
                                       can_scroll=True)
        self.cube_display_menu = ScrollMenu(self.controls_frame1,
                                            menu_width=9,
                                            variable=self.cube_display_index,
                                            options=("cross", "linear"),
                                            can_scroll=True)

        self.save_button = ttk.Button(self.controls_frame2,
                                      width=11,
                                      text="Browse",
                                      command=self.save_as)
        self.depth_menu.default_text = self.mipmap_menu.default_text =\
                                       self.bitmap_menu.default_text =\
                                       self.channel_menu.default_text =\
                                       self.cube_display_menu.default_text = ""

        labels = []
        labels.append(tk.Label(self.controls_frame0, text="Bitmap index"))
        labels.append(tk.Label(self.controls_frame1, text="Mipmap level"))
        labels.append(tk.Label(self.controls_frame2, text="Depth level"))
        labels.append(tk.Label(self.controls_frame0, text="Channels"))
        labels.append(tk.Label(self.controls_frame1, text="Cubemap display"))
        labels.append(tk.Label(self.controls_frame2, text="Save to file"))
        for lbl in labels:
            lbl.config(width=15,
                       anchor='w',
                       bg=self.default_bg_color,
                       fg=self.text_normal_color,
                       disabledforeground=self.text_disabled_color)

        self.hsb = tk.Scrollbar(self,
                                orient="horizontal",
                                command=self.root_canvas.xview)
        self.vsb = tk.Scrollbar(self,
                                orient="vertical",
                                command=self.root_canvas.yview)
        self.root_canvas.config(xscrollcommand=self.hsb.set,
                                xscrollincrement=1,
                                yscrollcommand=self.vsb.set,
                                yscrollincrement=1)
        for w in [
                self.root_frame, self.root_canvas, self.image_canvas,
                self.controls_frame0, self.controls_frame1,
                self.controls_frame2
        ] + labels:
            if e_c.IS_LNX:
                w.bind('<Shift-4>', self.mousewheel_scroll_x)
                w.bind('<Shift-5>', self.mousewheel_scroll_x)
                w.bind('<4>', self.mousewheel_scroll_y)
                w.bind('<5>', self.mousewheel_scroll_y)
            else:
                w.bind('<Shift-MouseWheel>', self.mousewheel_scroll_x)
                w.bind('<MouseWheel>', self.mousewheel_scroll_y)

        # pack everything
        # pack in this order so scrollbars aren't shrunk
        self.root_frame_id = self.root_canvas.create_window(
            (0, 0), anchor="nw", window=self.root_frame)
        self.hsb.pack(side='bottom', fill='x', anchor='nw')
        self.vsb.pack(side='right', fill='y', anchor='nw')
        self.root_canvas.pack(fill='both', anchor='nw', expand=True)
        self.controls_frame0.pack(side='top', fill='x', anchor='nw')
        self.controls_frame1.pack(side='top', fill='x', anchor='nw')
        self.controls_frame2.pack(side='top', fill='x', anchor='nw')
        self.image_root_frame.pack(fill='both', anchor='nw', expand=True)
        self.image_canvas.pack(fill='both',
                               side='right',
                               anchor='nw',
                               expand=True)

        padx = self.horizontal_padx
        pady = self.horizontal_pady
        for lbl in labels[:3]:
            lbl.pack(side='left', padx=(25, 0), pady=pady)
        self.bitmap_menu.pack(side='left', padx=padx, pady=pady)
        self.mipmap_menu.pack(side='left', padx=padx, pady=pady)
        self.depth_menu.pack(side='left', padx=padx, pady=pady)
        for lbl in labels[3:]:
            lbl.pack(side='left', padx=(15, 0), pady=pady)
        self.save_button.pack(side='left', padx=padx, pady=pady)
        self.channel_menu.pack(side='left', padx=padx, pady=pady)
        self.cube_display_menu.pack(side='left', padx=padx, pady=pady)

        self.change_textures(textures)

        self.write_trace(self.bitmap_index, self.settings_changed)
        self.write_trace(self.mipmap_index, self.settings_changed)
        self.write_trace(self.depth_index, self.settings_changed)
        self.write_trace(self.cube_display_index, self.settings_changed)
        self.write_trace(self.channel_index, self.settings_changed)

        self.apply_style()

    def destroy(self):
        try:
            self.clear_canvas()
            self.clear_depth_canvas()
        except Exception:
            pass
        try:
            del self.textures[:]
        except Exception:
            pass
        try:
            del self._image_handlers[:]
        except Exception:
            pass
        self.image_canvas_ids = self._image_handlers = None
        self.textures = None
        tk.Frame.destroy(self)
        self.delete_all_traces()
        self.delete_all_widget_refs()
        gc.collect()

    @property
    def active_image_handler(self):
        b = self.bitmap_index.get()
        if b not in range(len(self.textures)) or not import_arbytmap():
            return None
        elif b not in self._image_handlers:
            # make a new PhotoImageHandler if one doesnt exist already
            self._image_handlers[b] = PhotoImageHandler(
                self.textures[b][0], self.textures[b][1], self.temp_dir)

        return self._image_handlers[b]

    @property
    def should_update(self):
        return (self.prev_bitmap_index != self.bitmap_index.get()
                or self.prev_mipmap_index != self.mipmap_index.get()
                or self.prev_channel_index != self.channel_index.get())

    def mousewheel_scroll_x(self, e):
        # prevent scrolling if the root_canvas.bbox width >= canvas width
        bbox = self.root_canvas.bbox(tk.ALL)
        if not bbox or (self.root_canvas.winfo_width() >= bbox[2] - bbox[0]):
            return

        delta = getattr(self.app_root, "scroll_increment_x", 20)
        self.root_canvas.xview_scroll(int(get_mouse_delta(e) * delta), "units")

    def mousewheel_scroll_y(self, e):
        # prevent scrolling if the root_canvas.bbox height >= canvas height
        bbox = self.root_canvas.bbox(tk.ALL)
        if not bbox or (self.root_canvas.winfo_height() >= bbox[3] - bbox[1]):
            return

        delta = getattr(self.app_root, "scroll_increment_y", 20)
        self.root_canvas.yview_scroll(int(get_mouse_delta(e) * delta), "units")

    def update_scroll_regions(self):
        if not self.image_canvas_ids and not self.depth_canvas_id:
            return
        rf = self.root_frame
        region = self.image_canvas.bbox(tk.ALL)
        # bbox region isn't actually x, y, w, h, but if the
        # origin is at (0,0) we can treat it like that
        if region is None:
            x, y, w, h = (0, 0, 0, 0)
        else:
            x, y, w, h = region

        self.image_canvas.config(scrollregion=(x, y, w, h))
        rf.update_idletasks()
        max_w = w
        total_h = h
        for widget in self.root_frame.children.values():
            if widget is not self.image_root_frame:
                max_w = max(widget.winfo_reqwidth(), max_w)
                total_h += widget.winfo_reqheight()

        self.root_canvas.itemconfigure(self.root_frame_id,
                                       width=max_w,
                                       height=total_h)
        self.root_canvas.config(scrollregion=(0, 0, max_w, total_h))

    def save_as(self, e=None, initial_dir=None):
        handler = self.active_image_handler
        if handler is None:
            return None
        fp = asksaveasfilename(initialdir=initial_dir,
                               defaultextension='.dds',
                               title="Save bitmap as...",
                               parent=self,
                               filetypes=(("DirectDraw surface", "*.dds"),
                                          ('Portable network graphics',
                                           '*.png'),
                                          ('Truevision graphics adapter',
                                           '*.tga'), ('Raw pixel data',
                                                      '*.bin')))

        fp, ext = os.path.splitext(fp)
        if not fp:
            return
        elif not ext:
            ext = ".dds"

        mip_levels = "all" if ext.lower() == ".dds" else self.mipmap_index.get(
        )

        handler.arby.save_to_file(output_path=fp,
                                  ext=ext,
                                  overwrite=True,
                                  mip_levels=mip_levels,
                                  bitmap_indexes="all",
                                  keep_alpha=handler.channels.get("A"),
                                  swizzle_mode=False,
                                  channel_mapping=handler.channel_mapping,
                                  target_big_endian=False,
                                  tile_mode=False)

    def clear_depth_canvas(self):
        self.depth_canvas.delete(tk.ALL)
        self.prev_depth_index = None
        self.prev_cube_display_index = None
        self.depth_canvas_image_id = None

    def clear_canvas(self):
        self.image_canvas.delete(tk.ALL)
        self.clear_depth_canvas()
        self.depth_canvas_id = None
        self.image_canvas_ids = []

    def hide_depth_canvas(self):
        if self.depth_canvas_id is not None:
            self.image_canvas.delete(self.depth_canvas_id)
            self.depth_canvas_id = None

    def show_depth_canvas(self):
        self.depth_canvas_id = self.image_canvas.create_window(
            (0, 0), anchor="nw", window=self.depth_canvas)

    def change_textures(self, textures):
        assert hasattr(textures, '__iter__')

        for tex in textures:
            assert len(tex) == 2
            assert isinstance(tex[1], dict)

        if self._image_handlers: del self._image_handlers
        if self.textures: del self.textures[:]

        self.textures = list(textures)

        self._image_handlers = {}
        self.bitmap_index.set(0)
        self.mipmap_index.set(0)
        self.channel_index.set(0)
        self.depth_index.set(0)
        self.cube_display_index.set(0)

        self.bitmap_menu.set_options(range(len(textures)))

        self.prev_bitmap_index = None
        self.prev_mipmap_index = None
        self.prev_channel_index = None
        self.prev_depth_index = None
        self.prev_cube_display_index = None
        self.update_bitmap(force=True)

    def get_images(self):
        image_handler = self.active_image_handler
        if not image_handler: return
        images = image_handler.get_images(mip_levels=self.mipmap_index.get())
        return tuple(images[i] for i in sorted(images.keys()))

    def settings_changed(self, *args, force=False):
        handler = self.active_image_handler
        force = False
        if not handler:
            return
        elif self.changing_settings:
            return
        elif self.prev_bitmap_index != self.bitmap_index.get():
            force = True
        elif self.prev_mipmap_index != self.mipmap_index.get():
            force = True
        elif self.prev_channel_index != self.channel_index.get():
            force = True
        elif self.prev_depth_index != self.depth_index.get():
            pass
        elif self.prev_cube_display_index != self.cube_display_index.get():
            force = True
        else:
            return
        self.changing_settings = True

        new_mip_options = range(handler.max_mipmap + 1)
        if self.mipmap_index.get() not in new_mip_options:
            self.mipmap_index.set(0)

        max_depth = handler.mip_depth(self.mipmap_index.get())
        self.mipmap_menu.set_options(new_mip_options)
        self.depth_menu.set_options(range(max_depth))
        if self.depth_menu.sel_index > max_depth - 1:
            self.depth_menu.sel_index = max_depth - 1

        channel_count = handler.channel_count
        if channel_count <= 2:
            opts = ("Luminance", "Alpha", "AL")
        else:
            opts = ("RGB", "Alpha", "ARGB", "Red", "Green", "Blue")
        self.channel_menu.set_options(opts)

        try:
            handler.set_channel_mode(self.channel_index.get())
            self.update_bitmap(force=force)
            self.changing_settings = False
        except Exception:
            self.changing_settings = False
            raise

    def update_bitmap(self, *args, force=False):
        handler = self.active_image_handler
        if handler is None:
            return None

        tex_type = handler.tex_type
        if tex_type == "2D": self._display_2d_bitmap(force)
        elif tex_type == "3D": self._display_3d_bitmap(force)
        elif tex_type == "CUBE": self._display_cubemap(force)

        self.prev_bitmap_index = self.bitmap_index.get()
        self.prev_mipmap_index = self.mipmap_index.get()
        self.prev_channel_index = self.channel_index.get()
        self.prev_depth_index = self.depth_index.get()
        self.prev_cube_display_index = self.cube_display_index.get()

    def _display_cubemap(self, force=False, bitmap_mapping=None):
        mapping_type = self.cube_display_index.get()
        if bitmap_mapping is None:
            if mapping_type == 0:
                bitmap_mapping = self.cubemap_cross_mapping
            else:
                bitmap_mapping = self.cubemap_strip_mapping

        self._display_2d_bitmap(force, bitmap_mapping)

    def _display_2d_bitmap(self, force=False, bitmap_mapping=None):
        images = self.get_images()
        if not images or not (self.should_update or force): return
        w = max(image.width() for image in images)
        h = max(image.height() for image in images)
        if bitmap_mapping is None:
            bitmap_mapping = self.default_bitmap_mapping

        self.clear_canvas()
        # place the bitmaps on the canvas
        y = 0
        for line in bitmap_mapping:
            max_column_ct = max(0, len(line))
            x = 0
            for image_index in line:
                if image_index in range(len(images)):
                    # place the cube face on the canvas
                    self.image_canvas_ids.append(
                        self.image_canvas.create_image(
                            (x, y),
                            anchor="nw",
                            image=images[image_index],
                            tags=("BITMAP", "2D_BITMAP")))
                x += w
            y += h
        self.update_scroll_regions()

    def _display_3d_bitmap(self, force=False):
        if self.should_update or self.depth_canvas_image_id is None or force:
            self.clear_canvas()
            self.show_depth_canvas()
            handler = self.active_image_handler
            images = self.get_images()
            if not (images and handler): return
            m = self.mipmap_index.get()
            w, h = images[0].width(), handler.mip_height(m)
            self.curr_depth = handler.mip_depth(m)

            # place the bitmap on the canvas
            self.depth_canvas_image_id = self.depth_canvas.create_image(
                (0, 0),
                anchor="nw",
                image=images[0],
                tags=("BITMAP", "3D_BITMAP"))
            self.image_canvas.itemconfig(self.depth_canvas_id,
                                         width=w,
                                         height=h)
            self.depth_canvas.config(scrollregion="0 0 %s %s" % (w, h))
            self.update_scroll_regions()

        self.depth_canvas.coords(
            self.depth_canvas_image_id,
            (0, -self.depth_index.get() * self.curr_depth))
示例#9
0
class ObjectConverter(ConverterBase, window_base_class):
    object_types = (
        "vehicle",
        "biped",
        "weapon",
        "equipment",
        "garbage",
        "device_machine",
        "device_control",
        "device_light_fixture",
        "projectile",
        "sound_scenery",
        "scenery",
    )

    def __init__(self, app_root, *args, **kwargs):
        if isinstance(self, tk.Toplevel):
            kwargs.update(bd=0, highlightthickness=0, bg=self.default_bg_color)

        window_base_class.__init__(self, app_root, *args, **kwargs)
        ConverterBase.__init__(self, app_root, *args, **kwargs)
        kwargs["title"] = "Object to object converter"
        self.setup_window(*args, **kwargs)

    @property
    def src_ext(self):
        try:
            return self.object_types[self.src_menu.sel_index]
        except Exception:
            return ""

    @property
    def dst_ext(self):
        try:
            return self.object_types[self.dst_menu.sel_index]
        except Exception:
            return ""

    @property
    def src_exts(self):
        return self.object_types

    def setup_window(self, *args, **kwargs):
        ConverterBase.setup_window(self, *args, **kwargs)

        self.settings_frame = tk.LabelFrame(self, text="Conversion settings")
        self.from_label = tk.Label(self.settings_frame, text="from")
        self.src_menu = ScrollMenu(
            self.settings_frame,
            options=self.object_types,
            menu_width=20,
            sel_index=0,
        )
        self.to_label = tk.Label(self.settings_frame, text="to")
        self.dst_menu = ScrollMenu(
            self.settings_frame,
            options=self.object_types,
            menu_width=20,
            sel_index=10,
        )

        self.pack_widgets()
        self.apply_style()

    def pack_widgets(self):
        ConverterBase.pack_widgets(self)

        # pack everything
        self.settings_frame.pack(fill='both', anchor='nw')
        self.from_label.pack(padx=(10, 0), pady=10, anchor='w', side='left')
        self.src_menu.pack(padx=(10, 0),
                           pady=10,
                           fill='x',
                           anchor='w',
                           side='left')
        self.to_label.pack(padx=(10, 0), pady=10, anchor='w', side='left')
        self.dst_menu.pack(padx=(10, 0),
                           pady=10,
                           fill='x',
                           anchor='w',
                           side='left')

    def destroy(self):
        ConverterBase.destroy(self)
        window_base_class.destroy(self)

    def tag_path_browse(self):
        curr_tag_path = self.tag_path.get()
        ConverterBase.tag_path_browse(self)
        if curr_tag_path == self.tag_path.get(): return

        try:
            ext = Path(curr_tag_path).suffix[1:].lower()
            self.src_menu.sel_index = self.object_types.index(ext)
        except Exception:
            pass

    def convert(self, tag_path):
        return obje_to_obje(tag_path, self.src_ext, self.dst_ext)