Hello!

I have been looking around in the demos and in the floarcanvas 
documentation without any luck.

Is it possible to list all the shapes added to a canvas? Something like 
print Floatcanvas.GetObjects()
If I know the shape object, it's a MovingBitmap like in the demo, is it 
possible to find the coordinates
to that shape on the canvas? Something like 
MovingBitmap.GetCoordinates() og MovingBitmap.GetPosition().

Oerjan Pettersen

 #!/usr/bin/env python
"""

This is a small demo, showing how to make an object that can be moved 
around.

It also contains a simple prototype for a "Connector" object
  -- a line connecting two other objects

"""

import wx
import sys
from wx.lib.floatcanvas import NavCanvas, Resources, FloatCanvas
from wx.lib.floatcanvas import FloatCanvas as FC
from wx.lib.floatcanvas.Utilities import BBox

import numpy as N

## here we create some new mixins:

class MovingObjectMixin:
    """
    Methods required for a Moving object
    """
    def GetOutlinePoints(self):
        BB = self.BoundingBox
        OutlinePoints = N.array( ( (BB[0,0], BB[0,1]), (BB[0,0], BB[1,1]),
                                    (BB[1,0], BB[1,1]), (BB[1,0], 
BB[0,1]),))

        return OutlinePoints
 
class ConnectorObjectMixin:
    """
    Mixin class for DrawObjects that can be connected with lines
  
    NOte that this versionony works for Objects that have an "XY" attribute:
      that is, one that is derived from XHObjectMixin.
    """
    def GetConnectPoint(self):
        return self.XY

class MovingBitmap(FC.ScaledBitmap, MovingObjectMixin, 
ConnectorObjectMixin):
    """
    ScaledBitmap Object that can be moved
    """
    ## All we need to do is is inherit from:
    ##      ScaledBitmap, MovingObjectMixin and ConnectorObjectMixin
    pass

class ConnectorLine(FC.LineOnlyMixin, FC.DrawObject,):
    """
    A Line that connects two objects -- it uses the objects to get its 
coordinates
    """
    ##fixme: this should be added to the Main FloatCanvas Objects some day.
    def __init__(self, Object1, Object2, LineColor = "Black", LineStyle 
= "Solid",
                 LineWidth = 1, InForeground = False):
        FC.DrawObject.__init__(self, InForeground)

        self.Object1 =      Object1         
        self.Object2 =      Object2         
        self.LineColor = LineColor
        self.LineStyle = LineStyle
        self.LineWidth = LineWidth

        self.CalcBoundingBox()
        self.SetPen(LineColor,LineStyle,LineWidth)

        self.HitLineWidth = max(LineWidth,self.MinHitLineWidth)

    def CalcBoundingBox(self):
        self.BoundingBox = BBox.fromPoints((self.Object1.GetConnectPoint(),
                                            
self.Object2.GetConnectPoint()) )
        if self._Canvas:
            self._Canvas.BoundingBoxDirty = True


    def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
        Points = N.array( (self.Object1.GetConnectPoint(), 
self.Object2.GetConnectPoint()))
        Points = WorldToPixel(Points)
        dc.SetPen(self.Pen)
        dc.DrawLines(Points)
        if HTdc and self.HitAble:
            HTdc.SetPen(self.HitPen)
            HTdc.DrawLines(Points)

class DrawFrame(wx.Frame):
    """
    A simple frame used for the Demo
    """
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.CreateStatusBar()              
        # Add the Canvas
        Canvas = FloatCanvas.FloatCanvas(self,-1,(300,300), 
ProjectionFun = None, Debug = 0, BackgroundColor = "WHITE")
      
        self.Canvas = Canvas

        Canvas.Bind(FC.EVT_MOTION, self.OnMove )
        Canvas.Bind(FC.EVT_LEFT_UP, self.OnLeftUp )

        self.Bitmaps = []
        ## create the bitmaps first
        for Point in (((-100,-100), ('images/out.png')), ((55,1), 
('images/circle.png'))):
            btm = Resources.getMondrianImage()
            mbtm = MovingBitmap(Resources.getMondrianImage(), Point[0], 
btm.GetHeight(), Position='cc')
            self.Bitmaps.append((mbtm))

        for bmp in self.Bitmaps:
            Canvas.AddObject(bmp)
            bmp.Bind(FC.EVT_FC_LEFT_DOWN, self.ObjectHit)

        self.Show(True)

        # How can I get all the objects on the canvas? 
<---------------------------------------------------------
        # print Canvas.GetObjects() 
<--------------------------------------------------------------------------------------
        # And for each object, can I get position? 
<-----------------------------------------------------------------

        self.MoveObject = None
        self.Moving = False

        return None

    def ObjectHit(self, object):
        if not self.Moving:
            print object.CalcBoundingBox()
            self.Moving = True
            self.StartPoint = object.HitCoordsPixel
            self.StartObject = 
self.Canvas.WorldToPixel(object.GetOutlinePoints())
            self.MoveObject = None
            self.MovingObject = object

    def OnMove(self, event):
        """
        Updates the status bar with the world coordinates
        and moves the object it is clicked on
        """
        self.SetStatusText("%.4f, %.4f"%tuple(event.Coords))

        if self.Moving:
            dxy = event.GetPosition() - self.StartPoint # Not start 
point, but prevoius move point.
            self.StartPoint = event.GetPosition()
            dxy = self.Canvas.ScalePixelToWorld(dxy)
            self.MovingObject.Move(dxy)
            self.Canvas.Draw(True)

    def OnLeftUp(self, event):
        if self.Moving:
            self.Moving = False

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    DrawFrame(None, -1, "FloatCanvas Moving Object App", 
wx.DefaultPosition, (700,700) )
    app.MainLoop()

_______________________________________________
FloatCanvas mailing list
[email protected]
http://paulmcnett.com/cgi-bin/mailman/listinfo/floatcanvas

Reply via email to