package com.boombuler.piraten.map.data;

import java.util.List;

import android.content.Intent;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.api.IMapView;
import org.osmdroid.views.overlay.ItemizedOverlay;

import com.boombuler.piraten.map.PirateMap;
import com.boombuler.piraten.map.PlakatDetailsActivity;


public class PlakatOverlay extends ItemizedOverlay<PlakatOverlayItem> {

	private List<PlakatOverlayItem> mItems;
	private final PirateMap mContext;
	
	public PlakatOverlay(PirateMap context, List<PlakatOverlayItem> items) {
		super(PlakatOverlayItem.getDefaultDrawable(), new DefaultResourceProxyImpl(context));
		mContext = context;
		if (items != null) {
			mItems = items;
			populate();
		}
	}

	@Override
	protected PlakatOverlayItem createItem(int i) {
		return mItems.get(i);
	}

	@Override
	public int size() {
		return mItems.size();
	}

    @Override
    protected boolean onTap(int index) {
    	PlakatOverlayItem item = mItems.get(index);
    	mContext.startActivityForResult(
    			new Intent(mContext, PlakatDetailsActivity.class)
    				.putExtra(PlakatDetailsActivity.EXTRA_PLAKAT_ID, item.getId()),
    				PirateMap.REQUEST_EDIT_PLAKAT);
    	
    	return true;
    }
/*
	public static void Prepare(Drawable[] mIcons) {
		for(int i = 0; i < mIcons.length; i++)
			mIcons[i] = PlakatOverlay. boundCenter(mIcons[i]);
	}
  */

    @Override
    public boolean onSnapToItem(int i, int i2, Point point, IMapView iMapView) {
        return false;
    }
}

@@ -69,235 +69,394 @@
 ort 
-android.content.Intent;%0Aimport android.graphics.Point;%0Aimport android.graphics.drawable.Drawable;%0Aimport org.osmdroid.DefaultResourceProxyImpl;%0Aimport org.osmdroid.api.IMapView;%0Aimport org.osmdroid.views.overlay.ItemizedOverlay
+org.osmdroid.DefaultResourceProxyImpl;%0Aimport org.osmdroid.api.IMapView;%0Aimport org.osmdroid.views.MapView;%0Aimport org.osmdroid.views.MapView.Projection;%0Aimport org.osmdroid.views.overlay.ItemizedOverlay;%0Aimport org.osmdroid.views.safecanvas.ISafeCanvas;%0A%0Aimport android.content.ClipData.Item;%0Aimport android.content.Intent;%0Aimport android.graphics.Canvas;%0Aimport android.graphics.Point
 ;%0A%0Ai
@@ -705,16 +705,63 @@
 ontext;%0A
+%09private Point mCurScreenCoords = new Point();%0A
 %09%0A%09publi
@@ -1001,16 +1001,625 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09@Override%0A%09protected void drawSafe(ISafeCanvas canvas, MapView mapView, boolean shadow) %7B%0A%0A%09%09if (shadow) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09final Projection pj = mapView.getProjection();%0A%09%09final int size = this.size() - 1;%0A%0A%09%09/* Draw in backward cycle, so the items with the least index are on the front. */%0A%09%09for (int i = size; i %3E= 0; i--) %7B%0A%09%09%09final PlakatOverlayItem item = getItem(i);%0A%09%09%09pj.toMapPixels(item.getPoint(), mCurScreenCoords);%0A%0A%09%09%09if (mapView.getProjection().getBoundingBox().increaseByScale(1.2f).contains(item.getPoint())) %7B%09%09%09%09%0A%09%09%09%09onDrawItem((Canvas) canvas, item, mCurScreenCoords);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%7D%0A%09%7D
 %0A%0A%09@Over

call onDrawItem only for items within the bounding box of the MapView