# *********************************************************************** # Extension of OGLlike with animations # # License: freeware, under the terms of the BSD-license # Copyright (C) 2008 Stef Mientki # mailto: ... # Please let me know if it works or not under different conditions # # max ( self._x ) ) or \ ( y < min ( self._y ) ) or \ ( y > max ( self._y ) ) : return False else : # If it's a control, don't allow select, # but toggle the value if self.Control : if self.value == 0 : self.value = 1 else : self.value = 0 return False return True # *********************************************************************** # *********************************************************************** # *********************************************************************** class Circle_Shape ( Rot_Shape ) : def __init__( self, Canvas, Radius = 30, Center =[50,50] ): self._XY_Org = list ( Center ) self._R_Phi = [] self._R_Phi.append ( ( Radius, -pi / 2 ) ) # bottom is corner trail self._R_Phi.append ( ( Radius, 0 ) ) self._R_Phi.append ( ( Radius, pi / 2 ) ) self._R_Phi.append ( ( Radius, pi ) ) self.Canvas = Canvas Rot_Shape.__init__ ( self, Canvas ) self.Trail = True # ********************************************************* # ********************************************************* def draw ( self, dc ) : Shape.draw ( self, dc ) P = self.Calc_xy () # for controls, color is depending on it's value if self.Control and ( self.value == 0 ) : a = 2 rgb = [self.Color.Red()/a, self.Color.Green()/a, self.Color.Blue()/a] dc.SetBrush ( wx.Brush (wx.Color(*rgb) ) ) else : dc.SetBrush ( wx.Brush ( self.Color ) ) dc.DrawCircle ( self.Canvas.W2Sx ( self._XY_Org [0] ), self.Canvas.W2Sy ( self._XY_Org [1] ), self.Canvas.W2S_w ( self._R_Phi [0] [0] ) ) self._Draw_Trails ( dc ) # *********************************************************************** # *********************************************************************** # *********************************************************************** class Button_Shape ( Circle_Shape ) : def __init__( self, *args, ** kwargs ) : #Canvas, Radius = 30, Center =[50,50] ): Circle_Shape.__init__ ( self, *args, **kwargs ) self.Control = True self.Color = wx.GREEN # *********************************************************************** # *********************************************************************** # *********************************************************************** class Rectangle_Shape ( Rot_Shape ) : def __init__( self, Canvas, LB = ( 0, 0 ), WH = ( 50, 10 ), RT = None, RPhi = None ): R, phi = self.Get_Init_Pars ( LB, WH, RT, RPhi ) R = 0.5 * R self._XY_Org = [ LB [0] + R * cos ( phi ), LB [1] + R * sin ( phi ) ] self._R_Phi = [] self._R_Phi.append ( ( R, pi + phi ) ) # left bottom is first point self._R_Phi.append ( ( R, - phi ) ) self._R_Phi.append ( ( R, phi ) ) self._R_Phi.append ( ( R, pi - phi ) ) self.Canvas = Canvas Rot_Shape.__init__ ( self ) # *********************************************************************** # *********************************************************************** # *********************************************************************** class Free_Shape ( Rot_Shape ) : def __init__( self, Canvas, Points ) : self._XY_Org = xy = [ Points [0] [0], Points [0] [1] ] self._R_Phi = [ ( 0, pi ) ] for p in Points [ 1 : ] : W = p[0] - xy[0] H = p[1] - xy[1] R = sqrt ( W ** 2 + H ** 2 ) if W == 0 : Phi = pi / 2 else : Phi = arctan ( 1.0 * H / W ) self._R_Phi.append ( ( R, Phi ) ) self.Canvas = Canvas Rot_Shape.__init__ ( self ) # *********************************************************************** # *********************************************************************** # *********************************************************************** class Points_Shape ( Rot_Shape ) : def __init__( self, Canvas, Points ) : # translate the points to XY-org and R-Phi array self._XY_Org = xy = [ Points [0] [0], Points [0] [1] ] self._R_Phi = [ ( 0, pi ) ] for p in Points [ 1 : ] : W = p[0] - xy[0] H = p[1] - xy[1] R = sqrt ( W ** 2 + H ** 2 ) if W == 0 : Phi = pi / 2 else : Phi = arctan ( 1.0 * H / W ) self._R_Phi.append ( ( R, Phi ) ) self.Canvas = Canvas Rot_Shape.__init__ ( self ) self.shape_filled = False # *********************************************************************** # *********************************************************************** # *********************************************************************** class Function_Shape ( Points_Shape ) : def __init__( self, Canvas, Curve, XRange ) : # execute the function, to get the results line = 'x = arange ( *XRange ) \n' line += 'y = ' + Curve +'\n' line += 'print y \n' #print line exec (line) # put the results into Points Points = [] for i, xi in enumerate ( x ) : Points.append ( (xi, y[i] ) ) Points_Shape.__init__ ( self, Canvas, Points ) # *********************************************************************** # *********************************************************************** # *********************************************************************** class Line_Shape ( Rot_Shape ) : def __init__( self, Canvas, LB = ( 0, 0 ), WH = ( 50, 10 ), RT = None, RPhi = None ): R, phi = self.Get_Init_Pars ( LB, WH, RT, RPhi ) self._XY_Org = [ LB [0], LB [1] ] self._R_Phi = [] self._R_Phi.append ( ( 0, pi + phi ) ) # left bottom is first point self._R_Phi.append ( ( R, phi ) ) self.Canvas = Canvas Rot_Shape.__init__ ( self ) self.shape_filled = False self.Color = wx.RED self.Line_Width = 4 # *********************************************************************** # *********************************************************************** # *********************************************************************** class Arrow_Shape ( Line_Shape ) : # ********************************************************* # ********************************************************* def draw ( self, dc ) : Rot_Shape.draw ( self, dc ) # draw an arrow point R = 4 + 2 * self.Line_Width P = [ ( self._x[1], self._y[1] ) ] Phi = 1.1 * pi + ( self._R_Phi [1][1] + self.rot ) x2 = self.Canvas.W2Sx ( self._XY_Org [0] + self._R_Phi [1][0] * cos (self.rot + self._R_Phi[1][1]) + R * cos ( Phi ) ) y2 = self.Canvas.W2Sy ( self._XY_Org [1] + self._R_Phi [1][0] * sin (self.rot + self._R_Phi[1][1]) + R * sin ( Phi ) ) P.append ( ( x2, y2 ) ) #dc.DrawLine ( x1, y1, x2, y2) Phi = 0.9 * pi + ( self._R_Phi [1][1] + self.rot ) x2 = self.Canvas.W2Sx ( self._XY_Org [0] + self._R_Phi [1][0] * cos (self.rot + self._R_Phi[1][1]) + R * cos ( Phi ) ) y2 = self.Canvas.W2Sy ( self._XY_Org [1] + self._R_Phi [1][0] * sin (self.rot + self._R_Phi[1][1]) + R * sin ( Phi ) ) P.append ( ( x2, y2 ) ) #dc.DrawLine ( x1, y1, x2, y2) dc.SetBrush ( wx.Brush ( dc.GetPen().GetColour() ) ) dc.DrawPolygon( P ) # *********************************************************************** # *********************************************************************** # *********************************************************************** class Text_Shape ( Rot_Shape ) : def __init__( self, Canvas, line, LB = [0,0] ): self.text = line self._XY_Org = [ LB [0], LB [1] ] self.Canvas = Canvas # we need some coordinates here otherwise init crashes self._R_Phi = [] self._R_Phi.append ( ( 0, 0 ) ) self._R_Phi.append ( ( 30, pi/4 ) ) Rot_Shape.__init__ ( self ) self.Color = wx.BLUE self.Line_Width = 1 def draw ( self, dc ) : fs = 5 + 6 * self.Line_Width dc.SetFont ( wx.Font ( fs, wx.SWISS, wx.NORMAL, wx.NORMAL ) ) dc.SetTextForeground ( self.Color ) te = dc.GetTextExtent ( self.text ) # calculate the upper-right corner self._R_Phi = [] self._R_Phi.append ( ( 0, 0 ) ) R = self.S2W_w ( sqrt ( te[0]**2 + te[1]**2 ) ) self._R_Phi.append ( ( R, arctan ( 1.0 * te[1] / te[0] ) ) ) self.Calc_xy () x = self.Canvas.W2Sx ( self._XY_Org [0] ) # Text is defined from the upper-left corner # but we want to define it from the lower-left corner y = self.Canvas.W2Sy ( self._XY_Org [1] ) - te[1] # rotation is specified in radians, but we need degrees here rot = 180 * self.rot / pi dc.DrawRotatedText ( self.text, x, y, rot ) # ***********************************************************************