Package DejaVu :: Module PropertyEditor
[hide private]
[frames] | no frames]

Source Code for Module DejaVu.PropertyEditor

  1  ############################################################################# 
  2  # 
  3  # Author: Michel F. SANNER 
  4  # 
  5  # Copyright: M. Sanner TSRI 2000 
  6  # 
  7  ############################################################################# 
  8   
  9  # $Header: /opt/cvs/python/packages/share1.5/DejaVu/PropertyEditor.py,v 1.4 2007/03/01 20:21:29 vareille Exp $ 
 10  # 
 11  # $Id: PropertyEditor.py,v 1.4 2007/03/01 20:21:29 vareille Exp $ 
 12  # 
 13   
 14  import Tkinter, Numeric 
 15  import Slider, ColorWheel, ColorChooser, colorTool 
 16  from EventHandler import CallbackFunctions 
 17   
 18   
19 -class MaterialEditor(CallbackFunctions):
20 """Class for a material editor""" 21 22 property = [ 'ambient', 'diffuse', 'specular', 'emission' ] 23
24 - def Callbacks(self, event=None):
25 """Implement callback functions""" 26 27 if type(event) == type(0.0): 28 for f in self.callbacks: 29 f('shininess', event) 30 else: 31 tkrgb = self.colorChooser.hsWheel.Get(mode='TkRGB') 32 c = self.currentComponent.get() 33 self.mat[c] = self.colorChooser.hsWheel.Get(mode='RGB')[:3] 34 self.colw[c].config( background = tkrgb ) 35 for f in self.callbacks: 36 f(self.property[c], self.mat[c])
37 38
39 - def RestoreColor(self):
40 """Set the wheel cursor to the color of the current component""" 41 c = self.currentComponent.get() 42 self.colorChooser.Set(self.mat[c], 'RGB')
43 44
45 - def Set(self, ambi=None, diff=None, spec=None, emis=None, shini=None, 46 mode = 'RGB'):
47 """Set the material Editor to a given material""" 48 49 assert mode in ('HSV', 'RGB') 50 c = self.currentComponent.get() 51 if ambi: 52 ambi = colorTool.OneColor(ambi) 53 tkrgb = colorTool.TkColor(ambi[:3]) 54 self.colw[0].config( background = tkrgb ) 55 self.mat[0][:3] = ambi[:3] 56 if c==0: self.colorChooser.Set( ambi, 'RGB' ) 57 if diff: 58 diff = colorTool.OneColor(diff) 59 tkrgb = colorTool.TkColor(diff[:3]) 60 self.colw[1].config( background = tkrgb ) 61 self.mat[1][:3] = diff[:3] 62 if c==1: self.colorChooser.Set( diff, 'RGB' ) 63 if spec: 64 spec = colorTool.OneColor(spec) 65 tkrgb = colorTool.TkColor(spec[:3]) 66 self.colw[2].config( background = tkrgb ) 67 self.mat[2][:3] = spec[:3] 68 if c==2: self.colorChooser.Set( spec, 'RGB' ) 69 if emis: 70 emis = colorTool.OneColor(emis) 71 tkrgb = colorTool.TkColor(emis[:3]) 72 self.colw[3].config( background = tkrgb ) 73 self.mat[3][:3] = emis[:3] 74 if c==3: self.colorChooser.Set( emis, 'RGB' ) 75 if shini: 76 self.shini.Set(shini)
77 78
79 - def __init__(self, root=None, colorChooser=None):
80 81 CallbackFunctions.__init__(self) 82 83 self.frame = Tkinter.Frame(root, relief=Tkinter.RIDGE, borderwidth=3) 84 self.mat = Numeric.ones( (4,3), 'f') 85 self.currentComponent = Tkinter.IntVar() 86 self.currentComponent.set(0) 87 self.colw = [0,]*4 88 width = 9 89 self.colw[0] = Tkinter.Radiobutton(self.frame, text='Ambient', 90 variable = self.currentComponent, value=0, 91 command=self.RestoreColor, relief = Tkinter.SUNKEN, 92 width=width, borderwidth=3, 93 background = '#FFFFFF', foreground = '#000000', ) 94 self.colw[0].grid(row=0,column=0) 95 self.colw[1] = Tkinter.Radiobutton(self.frame, text='Diffuse', 96 value=1, variable = self.currentComponent, 97 command=self.RestoreColor, borderwidth=3, 98 width=width, relief = Tkinter.SUNKEN, 99 background = '#FFFFFF', foreground = '#000000' ) 100 self.colw[1].grid(row=1,column=0) 101 self.colw[2] = Tkinter.Radiobutton(self.frame, text='Specular', 102 value=2, variable = self.currentComponent, 103 command=self.RestoreColor, relief = Tkinter.SUNKEN, 104 width=width, borderwidth=3, 105 background = '#FFFFFF', foreground = '#000000' ) 106 self.colw[2].grid(row=0,column=1) 107 self.colw[3] = Tkinter.Radiobutton(self.frame, text='Emissive', 108 value=3, variable = self.currentComponent, 109 command=self.RestoreColor, relief = Tkinter.SUNKEN, 110 width=width, borderwidth=3, 111 background = '#FFFFFF', foreground = '#000000') 112 self.colw[3].grid(row=1,column=1) 113 114 self.shini = Slider.Slider(self.frame, label='Shininess', immediate=0, 115 minval=0.0, maxval = 128.0, init=30.0) 116 self.shini.frame.grid(row=2,column=0,columnspan=2) 117 self.shini.AddCallback(self.Callbacks) 118 119 if not colorChooser: 120 self.colorChooser = ColorChooser.ColorChooser(self.frame) 121 self.colorChooser.frame.grid(row=3, column=0, columnspan=2) 122 else: 123 self.colorChooser = colorChooser 124 self.colorChooser.AddCallback(self.Callbacks)
125 126
127 -class LightColorEditor(CallbackFunctions):
128 """Class for a light source color editor""" 129 130 property = [ 'ambient', 'diffuse', 'specular' ] 131
132 - def Callbacks(self, event=None):
133 """Implement callback functions""" 134 135 tkrgb = self.colorChooser.hsWheel.Get(mode='TkRGB') 136 c = self.currentComponent.get() 137 self.mat[c] = self.colorChooser.hsWheel.Get(mode='RGB')[:3] 138 self.colw[c].config( background = tkrgb ) 139 for f in self.callbacks: 140 f(self.property[c], self.mat[c])
141 142
143 - def RestoreColor(self):
144 """Set the wheel cursor to the color of the current component""" 145 c = self.currentComponent.get() 146 self.colorChooser.Set(self.mat[c], 'RGB')
147 148
149 - def Set(self, ambi=None, diff=None, spec=None, mode = 'RGB'):
150 """Set the material Editor to a given material""" 151 152 assert mode in ('HSV', 'RGB') 153 c = self.currentComponent.get() 154 if ambi: 155 ambi = colorTool.OneColor(ambi) 156 tkrgb = colorTool.TkColor(ambi[:3]) 157 self.colw[0].config( background = tkrgb ) 158 self.mat[0][:3] = ambi[:3] 159 if c==0: self.colorChooser.Set( ambi, 'RGB' ) 160 161 if diff: 162 diff = colorTool.OneColor(diff) 163 tkrgb = colorTool.TkColor(diff[:3]) 164 self.colw[1].config( background = tkrgb ) 165 self.mat[1][:3] = diff[:3] 166 if c==1: self.colorChooser.Set( diff, 'RGB' ) 167 168 if spec: 169 spec = colorTool.OneColor(spec) 170 tkrgb = colorTool.TkColor(spec[:3]) 171 self.colw[2].config( background = tkrgb ) 172 self.mat[2][:3] = spec[:3] 173 if c==2: self.colorChooser.Set( spec, 'RGB' )
174 175
176 - def __init__(self, root=None, colorChooser=None):
177 178 CallbackFunctions.__init__(self) 179 180 self.frame = Tkinter.Frame(root, relief=Tkinter.RIDGE, borderwidth=3) 181 self.mat = Numeric.ones( (3,3), 'f') 182 self.currentComponent = Tkinter.IntVar() 183 self.currentComponent.set(0) 184 self.colw = [0,]*4 185 width = 9 186 self.colw[0] = Tkinter.Radiobutton(self.frame, text='Ambient', 187 value=0, variable = self.currentComponent, 188 command=self.RestoreColor, relief = Tkinter.SUNKEN, 189 width=width, borderwidth=3, 190 background = '#FFFFFF', foreground = '#000000', ) 191 self.colw[0].grid(row=0,column=0) 192 self.colw[1] = Tkinter.Radiobutton(self.frame, text='Diffuse', 193 value=1, variable = self.currentComponent, 194 command=self.RestoreColor, borderwidth=3, 195 width=width, relief = Tkinter.SUNKEN, 196 background = '#FFFFFF', foreground = '#000000' ) 197 self.colw[1].grid(row=1,column=0) 198 self.colw[2] = Tkinter.Radiobutton(self.frame, text='Specular', 199 value=2, variable = self.currentComponent, 200 command=self.RestoreColor, relief = Tkinter.SUNKEN, 201 width=width, borderwidth=3, 202 background = '#FFFFFF', foreground = '#000000' ) 203 self.colw[2].grid(row=0,column=1) 204 205 if not colorChooser: 206 self.colorChooser = ColorChooser.ColorChooser(self.frame) 207 self.colorChooser.frame.grid(row=3, column=0, columnspan=2) 208 else: 209 self.colorChooser = colorChooser 210 self.colorChooser.AddCallback(self.Callbacks)
211 212 213 if __name__ == '__main__': 214 root = Tkinter.Tk() 215 root.title( 'Material Editor' ) 216 me = MaterialEditor(root) 217 me.frame.pack() 218 219 lce = LightColorEditor(root) 220 lce.frame.pack() 221