include Debug


class GoSimple < GoBasic


  def place_obj (object,col,row)

    if @items.has_value?(object)
      if object.class == Stone
	@board[object.col][object.row]=nil
      end
    end
    add(object,col,row)

  end

  def set_obj(klass,color,col,row)
    obj= klass.new(@canvas,@grid,color)
    add(obj,col,row)
    obj
  end

  def set_mark(klass,col,row)
    obj= klass.new(@canvas,@grid)
    add(obj,col,row)
  end

  def set_text(text,col,row)
    obj= TextBoard.new(@canvas,@grid,text)
    add(obj,col,row)
  end

  def set_2p(klass,col,row,c2,r2)
    obj= klass.new(@canvas,@grid)
    add(obj,col,row,c2,r2)
  end

end


class Goban < GoSimple

  def initialize(parent,dimx=19, dimy=19)
    super(parent,dimx,dimy)
    @h_margin= 4

    @canvas.configure('closeenough' => Defaults::DEFS['close_enough'])

    create_tools
 $ip._eval("#{@canvas.path} bind item   <Control-ButtonPress-1> {set motion_x %x
    set motion_y %y
    #{@canvas.path} addtag movable withtag current}")

  $ip._eval("#{@canvas.path} bind movable <Control-B1-Motion> {#{@canvas.path} move movable [expr %x - $motion_x] [expr %y - $motion_y]
    set motion_x %x
    set motion_y %y }")

 @canvas.bind("ButtonPress-1",proc { |ev| do_click_1(ev)})
 @canvas.bind("ButtonPress-3",proc { |ev| do_click_3(ev)})
	
	@canvas.focus
  
  end

  def do_click_1(ev) 
    
  return if ev.state & 0x0F != 0 # without <Control>
    col,row = @grid.grid_round(ev.x,ev.y)
  if col and row
    if  @board[col][row]
      delete(@board[col][row])
    else
      set_obj(Stone,'B',col,row)
    end
  end
  end

 def do_click_3(ev) 
	return if ev.state & 0x0F != 0 # without <Control>
    col,row = @grid.grid_round(ev.x,ev.y)
  if col and row
    if  @board[col][row]
      delete(@board[col][row])
    else
      set_obj(Stone,'W',col,row)
    end
  end
  end

  def do_resize(event)
    super(event)
    @toolsW = @step * 2
    resize_tools
  end

  def create_tools
    @toolBox = ToolBox.new(self)
    @toolBox.add_tool(Stone.new(@canvas,@grid,'W'))
    @toolBox.add_tool(Stone.new(@canvas,@grid,'B'))
    @toolBox.add_tool(Rectangle.new(@canvas,@grid))
    @toolBox.add_tool(Triangle.new(@canvas,@grid))
    @toolBox.add_tool(Circle.new(@canvas,@grid))
    @toolBox.add_tool(Cross.new(@canvas,@grid))
    @toolBox.add_tool(TextBoard.new(@canvas,@grid))
    @toolBox.add_tool(Dame.new(@canvas,@grid))
    @toolBox.add_tool(Line.new(@canvas,@grid))
    @toolBox.add_tool(Arrow.new(@canvas,@grid))
    @toolBox.add_tool(Dim.new(@canvas,@grid))
    @toolBox.add_tool(Select.new(@canvas,@grid))
  end

  def resize_tools
    @toolBox.coords(@dimx+2 , 2,  @step)
  end

  def resize_items
    super
    @items.each_value { |object|
      object.set_sq if object.kind_of? TwoPoints
    }
  end

  def bind_move(tool)
    tool.movable
    tool.bind('Shift-ButtonPress-1',proc {|x,y| move_group(tool)}) if tool.class == Stone
#modif
    tool.bind('Shift-ButtonRelease-1', proc {|x,y| do_release(x,y,tool)})
    tool.bind('Control-ButtonRelease-1', proc {|x,y| do_release(x,y,tool)})
  end

  def move_group(tool)
    @group= make_group(tool)
    @group.each {|stone| stone.highlight}
  end

  def do_release(x,y,object)
    if @group.nil?
      $ip._eval("#{@canvas.path} dtag movable movable")
      tool_do_release(x,y,object)
    else
      @group.each {|stone| stone.normal
      x,y= stone.get_coords
      tool_do_release(x,y,stone)
    }
    @group=nil
  end
end

def tool_do_release(x,y,object)

  col,row = @grid.grid_round(x,y)
  #debug "#{col.inspect} #{row.inspect}"
  if @toolBox.include?(object)
    @toolBox.replace(object,object.duplicate)
  end
  if col and row
    place_obj(object,col,row)
    raise_all
  else
    delete(object)
  end
end

def place_obj (object,col,row)
  if @board[col][row]
    if object.class == Stone
      cancel_move(object)
      return
    end
  end
  super(object,col,row)

  object.set_sq if object.kind_of? TwoPoints

end

def cancel_move(object)
  if @toolBox.include?(object)
    @toolBox.place_items
  else
    object.paint
  end
end

def set_obj(klass,color,col,row)
  obj= super(klass,color,col,row)
  bind_move(obj)
end

def set_mark(klass,col,row)

  super(klass,color,col,row)
  bind_move(obj)
end

def set_text(text,col,row)
  super(klass,color,col,row)
  bind_move(obj)
end

def set_2p(klass,col,row,c2,r2)
  super(klass,color,col,row)
  bind_move(obj)
  obj.set_sq
end

      end


