view scripts-java/visualizer/test.py @ 117:fce61ee25d20

run FederatedLinda experiment on torque
author kazz
date Wed, 08 Feb 2012 18:02:00 +0900 (2012-02-08)
parents 1809e2b05824
children
line wrap: on
line source
from wxPython.wx import *
from wxPython.ogl import *
    
#wxOGLInitialize()
    
    
class MyEvtHandler(wxShapeEvtHandler):
    """
    Overwrite the default event handler to implement some custom features. 
    """
    def __init__(self):
	wxShapeEvtHandler.__init__(self)
	
    def OnLeftClick(self, x, y, keys = 0, attachment = 0):
	"""
	The dragging is done here. 
	You should probably comment out the EVT_MOTION below, to see it work. 
	"""
	shape = self.GetShape()
	print shape.__class__, shape.GetClassName(), shape.a
	canvas = shape.GetCanvas()
	dc = wxClientDC(canvas)
	canvas.PrepareDC(dc)
	
	if shape.Selected():
	    shape.Select(False, dc)
	    canvas.Redraw(dc)
	else:
	    redraw = False
	    shapeList = canvas.GetDiagram().GetShapeList()
	    toUnselect = []
	    for s in shapeList:
		if s.Selected():
		    toUnselect.append(s)
		      
	    shape.Select(True, dc)
  
	    if toUnselect:
		for s in toUnselect:
		    s.Select(False, dc)
		canvas.Redraw(dc)


class OGLCanvas(wxShapeCanvas):
    def __init__(self, parent, frame):
	wxShapeCanvas.__init__(self, parent)
	
	self.SetBackgroundColour("LIGHT BLUE")
	self.diagram = wxDiagram()
	self.SetDiagram(self.diagram)
	self.diagram.SetCanvas(self)
  
	self.circle = wxCircleShape(100)
	self.circle.SetCanvas(self)
	self.circle.a="Circle identified"
	self.diagram.AddShape(self.circle)
	self.circle.Show(True)
  
	self.evthandler = MyEvtHandler()
	self.evthandler.SetShape(self.circle)
	self.evthandler.SetPreviousHandler(self.circle.GetEventHandler())
	self.circle.SetEventHandler(self.evthandler)
  
	EVT_MOTION(self, self.OnMotion)
  
    def OnMotion(self, event):
	shape = self.evthandler.GetShape()
  
	bx = shape.GetX()
	by = shape.GetY()
	bw, bh = shape.GetBoundingBoxMax()
	oldrect = wxRect(int(bx-bw/2)-1, int(by-bh/2)-1, int(bw)+2, int(bh)+2)
  
	canvas = shape.GetCanvas()
	dc = wxClientDC(canvas)
	canvas.PrepareDC(dc)
  
	shape.Move(dc, event.GetPosition()[0], event.GetPosition()[1])
	canvas.Refresh(False, oldrect)
	event.Skip()
  

class OGLFrame(wxFrame):
    def __init__(self, *args, **kwds):
	wxFrame.__init__(self, *args, **kwds)
	
	self.SetTitle("OGL TEST")
	self.SetBackgroundColour(wxColour(8, 197, 248))
	self.canvas = OGLCanvas(self, self)
  
class Main(wxApp):
    def OnInit(self):
	wxInitAllImageHandlers()
	frame = OGLFrame(None, -1, "")
	self.SetTopWindow(frame)
	frame.Show(True)
	return True
  
if __name__ == "__main__":
    app = Main()
    app.MainLoop()