#!/usr/bin/env python2.4

"""
A simple demo to show how to drw a bitmap on top of the Canvas at given pixel coords.

"""

import wx
import numpy as np

try:
    # See if there is a local copy
    import sys
    sys.path.append("../")
    from floatcanvas import NavCanvas, FloatCanvas
except ImportError:
    from wx.lib.floatcanvas import NavCanvas, FloatCanvas
FC = FloatCanvas

class PixelBitmap:
    def __init__(self, Bitmap, XY, Position = 'tl'):

        if type(Bitmap) == wx._gdi.Bitmap:
            self.Bitmap = Bitmap
        elif type(Bitmap) == wx._core.Image:
            self.Bitmap = wx.BitmapFromImage(Bitmap)
        else:
            raise FC.FloatCanvasError("PixelBitmap takes only a wx.Bitmap or a wx.Image as input")

        self.XY = np.asarray(XY, dtype=np.int).reshape((2,))
        self.Position = Position
        
        (self.Width, self.Height) = self.Bitmap.GetWidth(), self.Bitmap.GetHeight()
        self.ShiftFun = FC.TextObjectMixin.ShiftFunDict[Position]

    def _Draw(self, dc, Canvas):
        w, h = Canvas.Size
        print "Windows size:", w, h
        XY = self.XY
        print self.Position, "start:", XY, 
        #XY = self.ShiftFun(XY[0], XY[1], self.Width, self.Height)
        print "shifted to:", XY, 
        if self.Position[0] == 'b':
            XY = (XY[0], h - XY[1] - self.Height)
        elif self.Position[0] == 'c':
            XY = (XY[0], XY[1] + (h - self.Height)/2)
            
        if self.Position[1] == 'r':
            XY = (w - XY[0] - self.Height, XY[1])
        elif self.Position[1] == 'c':
            XY = (XY[0] + (w - self.Width)/2, XY[1])
        print "shifted to:", XY
        dc.DrawBitmapPoint(self.Bitmap, XY, True)

class GridGroup:
    def __init__(self, grids=[]):
        self.Grids = grids

    def _Draw(self, *args):
        for grid in self.Grids:
            grid._Draw(*args)
        
class DrawFrame(wx.Frame):

    """
    A frame used for the FloatCanvas Demo

    """

    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.CreateStatusBar()

        # Add the Canvas
        Canvas = NavCanvas.NavCanvas(self,-1,
                                     size = (500,500),
                                     ProjectionFun = None,
                                     Debug = 0,
                                     BackgroundColor = "DARK SLATE BLUE",
                                     ).Canvas
        
        
        Point = (45,40)
        Box = Canvas.AddCircle(Point,
                               Diameter = 10,
                               FillColor = "Black",
                               LineColor = "Red",
                               LineWidth = 6)
        bmp = wx.Bitmap('NOAA.png')
        grids = GridGroup([PixelBitmap(bmp, (10, 10), Position='tl'),
                           PixelBitmap(bmp, (10, 10), Position='br'),
                           PixelBitmap(bmp, (10, 10), Position='tr'),
                           PixelBitmap(bmp, (10, 10), Position='bl'),
                           PixelBitmap(bmp, (10, 10), Position='cl'),
                           PixelBitmap(bmp, (10, 10), Position='cr'),
                           PixelBitmap(bmp, (10, 10), Position='cc'),
                           PixelBitmap(bmp, (10, 10), Position='tc'),
                           PixelBitmap(bmp, (10, 10), Position='bc'),
                           ])
        Canvas.GridOver = grids

        FloatCanvas.EVT_MOTION(Canvas, self.OnMove ) 

        self.Show()
        Canvas.ZoomToBB()

    def OnMove(self, event):
        """
        Updates the status bar with the world coordinates

        """
        self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))



app = wx.App(False) # true to get its own output window.
F = DrawFrame(None, title="FloatCanvas Demo App", size=(700,700) )
app.MainLoop()
    
    
    
    









