Exemplo n.º 1
0
 def setup_area(self):
     """ Setup / resetup board setting
     """
     self.setup_shadow()           # Shadow used to speed testing
     new_area = False            # Indicate reset
     if self.canvas is not None:
         self.canvas.destroy()
         self.canvas = None 
         new_area = True
     self.canvas = CanvasTracked(self.frame,
              width=self.width, height=self.height,
              bg="white")
     self.canvas.set_parts_control(self)     # connect the dots - for info :)
     self.canvas.pack(expand=YES, fill=BOTH)
     
     self.area = SelectArea(self.canvas, mw=self.mw,
                            board=self,
                            image_hash=self.image_hash,
                            display_game=self.display_game,
                            tbmove=self.tbmove,
                            stroke_checking=self.stroke_checking,
                            check_mod=self.check_mod,
                            down_click_call=self.down_click_call,
                            highlight_limit=self.highlight_limit,
                            highlighting=self.highlighting)
     
     rects =  []
     rects_rows = []         # So we can pass row, col
     rects_cols = []
     
     def rn(val):
         return int(round(val))
     
     xmin = .1*float(self.width)
     xmax = .9*float(self.width)
     xlen = (xmax-xmin)/float(self.ncols)
     min_xlen = float(self.min_xlen)
     if xlen < min_xlen:
         SlTrace.lg("xlen(%.0f) set to %.0f" % (xlen, min_xlen))
         xlen = min_xlen
     ymin = .1*float(self.height)
     ymax = .9*float(self.height)
     ylen = (ymax-ymin)/float(self.nrows)
     min_ylen = float(self.min_ylen)
     if ylen < min_ylen:
         SlTrace.lg("ylen(%.0f) set to %.0f" % (ylen, min_ylen))
         ylen = min_ylen
     for i in range(int(self.ncols)):
         col = i+1
         x1 = xmin + i*xlen
         x2 = x1 + xlen
         for j in range(int(self.nrows)):
             row = j+1
             y1 = ymin + j*ylen
             y2 = y1 + ylen
             rect = ((rn(x1), rn(y1)), (rn(x2), rn(y2)))
             rects.append(rect)
             rects_rows.append(row)
             rects_cols.append(col)
    
     
     for i, rect in enumerate(rects):
         row = rects_rows[i]
         col = rects_cols[i]
         self.area.add_rect(rect, row=row, col=col,
                         color=self.region_off_color,
                         do_corners=self.do_corners,
                         do_edges=self.do_edges,
                         draggable_edge=False,
                         draggable_corner=False,
                         draggable_region=False,
                         on_color=self.region_on_color,
                         off_color=self.region_off_color,   
                         invisible_region=not self.region_visible,
                         invisible_edge=not self.edge_visible,
                         region_width=self.region_width,
                         edge_width=self.edge_width)
     ####if not self.display_game:
     ####    return
     
     for part in self.area.get_parts():
         self.shadow.set_part(part)                  # Add to shadow data
         if self.do_corners and part.is_corner():
             part.set(display_shape="circle",
                        display_size=self.corner_width,
                        color="blue")
             if new_area:
                 if self.display_game:
                     part.display()
         elif self.do_edges and part.is_edge():
             part.set(edge_width_select=50,
                        edge_width_display=self.edge_width,
                        on_highlighting=True,
                        off_highlighting=True,
                        color="lightgreen")
         elif self.do_regions and part.is_region():
             part.set(color=self.region_off_color)
             top_edge = part.get_top_edge()
             if top_edge is not None:
                 top_edge.row = part.row 
                 top_edge.col = part.col
             right_edge = part.get_right_edge()
             if right_edge is not None:
                 right_edge.row = part.row 
                 right_edge.col = part.col + 1
             botom_edge = part.get_botom_edge()
             if botom_edge is not None:
                 botom_edge.row = part.row + 1 
                 botom_edge.col = part.col
             left_edge = part.get_left_edge()
             if left_edge is not None:
                 left_edge.row = part.row 
                 left_edge.col = part.col
     self.complete_square_call = None                # Setup for complete square call
     self.new_edge_call = None                       # Setup for new edge call
Exemplo n.º 2
0
def set_figure_button():
    global frame, sr, figure_new, sar
    global width, height, nx, ny
    global n_rearrange_cycles, rearrange_cycle
    SlTrace.lg("srtest Set Button", "button")
    ###    if canvas is not None:
    ###        SlTrace.lg("delete canvas")
    ###        canvas.delete()
    ###        canvas = None
    if frame is not None:
        SlTrace.lg("destroy frame", "destroy frame")
        frame.destroy()
        frame = None
    
    app.update_form()
    n_rearrange_cycles = app.get_current_val("arrange_number", 1)
    rearrange_cycle = 1    
    width = app.get_current_val("window_width", width)
    width = int(width)
    height = app.get_current_val("window_height", height)
    height = int(height)
    nx = app.get_current_val("figure_columns", nx)
    if nx == 0:
        new_nx = 1
        SlTrace.lg("nx:%d is too low - set to %d" % (nx, new_nx), "adjust_size")
        nx = new_nx
    ny = app.get_current_val("figure_rows", ny)
    if ny == 0:
        new_ny = 1
        SlTrace.lg("ny:%d is too low - set to %d" % (ny, new_ny), "adjust_size")
        ny = new_ny
        
        
        
    rects =  []
    rects_rows = []         # So we can pass row, col
    rects_cols = []
    min_xlen = app.get_component_val("figure_size", "min", 10)
    min_xlen = float(min_xlen)
    min_xlen = str(min_xlen)
    min_ylen = min_xlen
    
    ###rects.append(rect1)
    ###rects.append(rect2)
    xmin = .1*float(width)
    xmax = .9*float(width)
    xlen = (xmax-xmin)/float(nx)
    min_xlen = float(min_xlen)
    if xlen < min_xlen:
        SlTrace.lg("xlen(%.0f) set to %.0f" % (xlen, min_xlen))
        xlen = min_xlen
    ymin = .1*float(height)
    ymax = .9*float(height)
    ylen = (ymax-ymin)/float(ny)
    min_ylen = float(min_ylen)
    if ylen < min_ylen:
        SlTrace.lg("ylen(%.0f) set to %.0f" % (ylen, min_ylen))
        ylen = min_ylen
    def rn(val):
        return int(round(val))
    
    min_side = min(xlen, ylen)
    corner_width = max(min_side/15., 4)               
    for i in range(int(nx)):
        col = i+1
        x1 = xmin + i*xlen
        x2 = x1 + xlen
        for j in range(int(ny)):
            row = j+1
            y1 = ymin + j*ylen
            y2 = y1 + ylen
            rect = ((rn(x1), rn(y1)), (rn(x2), rn(y2)))
            rects.append(rect)
            rects_rows.append(row)
            rects_cols.append(col)
    
    im = Image.new("RGB", (width, height))
    frame = Frame(mw, width=width, height=height, bg="", colormap="new")
    frame.pack()
    canvas = Canvas(frame, width=width, height=height)
    canvas.pack()   
    sr = SelectArea(canvas, image=im)
    color_spec = app.get_current_val("color_spec", default= "freq")
    color_prog = app.get_current_val("color_prog", default="ascend")
    color_min = None        # Use internal defaults
    color_max = None
    ###if color_spec == "freq":
    ###    color_min = app.get_component_val("color_value", "min", default=400)
    ###    color_max = app.get_component_val("color_value", "max", default=700)
        
    reg_cc = SelectColor(ncolor=len(rects),
                         spec=color_spec,
                         prog=color_prog,
                         cmin=color_min,
                         cmax=color_max)

    SlTrace.lg("ncolor=%d" % len(rects), "get_color")
    SelectRegion.reset()
    for i, rect in enumerate(rects):
        row = rects_rows[i]
        col = rects_cols[i]
        col_rect = reg_cc.get_color()
        sr.add_rect(rect, color=col_rect, row=row, col=col)
    s_width = 20
    for part in sr.get_parts():
        if part.is_corner():
            part.set(display_shape="circle",
            ###part.set(display_shape="dot",
                    color="blue",
                    edge_width_display=corner_width,
                    edge_width_select=corner_width,
                    edge_width_enlarge=corner_width*.1,
                    )
        elif part.is_edge():
            part.set(
                    edge_width_select=s_width,
                    edge_width_enlarge=s_width*.1,
                    )

    sr.display()        
    app.set_call("run", run_button)
    app.set_call("set", set_figure_button)
    app.set_call("pause", pause_button)
    app.set_call("step", step_button)
    app.set_call("step_down", step_down_button)
    if SlTrace.trace("step_button"):
        SlTrace.lg("color_spec: %s color_prog: %s color_min: %d color_max: %d")
    sar = SelectArrange(arc, sr, n_major_cycle = n_arrange, mw=mw)        # Setup for color rearrangement
    figure_new = False
Exemplo n.º 3
0
    def __init__(self,
                 canvas,
                 mw=None,
                 nrows=10,
                 ncols=None,
                 width=None,
                 height=None,
                 tbmove=.1,
                 check_mod=None,
                 down_click_call=None,
                 highlight_limit=None):
        """
        :canvas: - canvas within we are placed
        :nrows: number of rows of squares default: 10
        :ncols: number of columns of squares default: rows
        :width: window width
        :height: window height
        :tbmove: minimum time(seconds) between move detection
        :check_mod: routine called, if present, before & after
                part modification
        :highlight_limit: limit highlighting (seconds)
                default: None (None - no limit)
        """
        if ncols is None:
            ncols = nrows
        self.canvas = canvas
        self.nrows = nrows
        self.ncols = ncols
        if width is None:
            width = canvas.winfo_width()
        self.width = width
        if height is None:
            height = canvas.winfo_height()
        self.height = height
        self.drawn_lines = []  # lines drawn

        min_xlen = 10
        min_ylen = min_xlen
        self.check_mod = check_mod
        self.tbmove = tbmove
        self.highlight_limit = highlight_limit

        rects = []
        rects_rows = []  # So we can pass row, col
        rects_cols = []
        self.down_click_call = down_click_call

        def rn(val):
            return int(round(val))

        xmin = .1 * float(width)
        xmax = .9 * float(width)
        xlen = (xmax - xmin) / float(ncols)
        min_xlen = float(min_xlen)
        if xlen < min_xlen:
            SlTrace.lg("xlen(%.0f) set to %.0f" % (xlen, min_xlen))
            xlen = min_xlen
        ymin = .1 * float(height)
        ymax = .9 * float(height)
        ylen = (ymax - ymin) / float(nrows)
        min_ylen = float(min_ylen)
        if ylen < min_ylen:
            SlTrace.lg("ylen(%.0f) set to %.0f" % (ylen, min_ylen))
            ylen = min_ylen
        for i in range(int(ncols)):
            col = i + 1
            x1 = xmin + i * xlen
            x2 = x1 + xlen
            for j in range(int(nrows)):
                row = j + 1
                y1 = ymin + j * ylen
                y2 = y1 + ylen
                rect = ((rn(x1), rn(y1)), (rn(x2), rn(y2)))
                rects.append(rect)
                rects_rows.append(row)
                rects_cols.append(col)

        self.area = SelectArea(canvas,
                               mw=mw,
                               tbmove=self.tbmove,
                               check_mod=self.check_mod,
                               down_click_call=self.down_click_call,
                               highlight_limit=self.highlight_limit)
        ###SelectRegion.reset()
        for i, rect in enumerate(rects):
            row = rects_rows[i]
            col = rects_cols[i]
            self.area.add_rect(rect,
                               row=row,
                               col=col,
                               draggable_edge=False,
                               draggable_corner=False,
                               draggable_region=False,
                               invisible_region=True,
                               invisible_edge=True)
        for part in self.area.get_parts():
            if part.is_corner():
                part.set(display_shape="circle", display_size=10, color="blue")
            elif part.is_edge():
                part.set(edge_width_select=50,
                         edge_width_display=5,
                         on_highlighting=True,
                         off_highlighting=True,
                         color="lightgreen")
            elif part.is_region():
                part.set(color='light slate gray')
                top_edge = part.get_top_edge()
                top_edge.row = part.row
                top_edge.col = part.col
                right_edge = part.get_right_edge()
                right_edge.row = part.row
                right_edge.col = part.col + 1
                botom_edge = part.get_botom_edge()
                botom_edge.row = part.row + 1
                botom_edge.col = part.col
                left_edge = part.get_left_edge()
                left_edge.row = part.row
                left_edge.col = part.col
        self.complete_square_call = None  # Setup for complete square call
        self.new_edge_call = None  # Setup for new edge call
        ###self.area.add_turned_on_part_call(self.new_edge)
        self.player_control = PlayerControl(self, display=False)