-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.py
340 lines (249 loc) · 10.2 KB
/
editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
PyPlant 1.0
GUI: Seed File Editor
Copyright(c) 2010 Chris Ponticello
s4234549@student.uq.edu.au
DESCRIPTION:
A small GUI application used for creating and editing seed properties.
The 21 parameters are changed through command-line input, buttons and sliders.
Seed files can be created and edited for later use by the grower application.
2 windows/displays: 'Controls' and 'Data'
LICENSE:
I, Chris Ponticello, hereby grant the rights to copy, redistribute, modify and
otherwise edit any source code file included within PyPlant 1.0, provided that
this license agreement and my ownership of the code is maintained and
acknowledged.
I also grant the right to non-commercial use of the source code without my
express permission. Commercial users must seek the permission of the author.
"""
# Get Directories
import sys, os
sys.path.append(os.getcwd()+'\lib')#adds lib folder to import directories
# PyPlant Libraries
from visual import *
from visual.controls import *
from seeds import *
#from common import *
# Standard Libraries
from random import random
# -------------------------------------------------------------------------------------
#CONSTANTS & EXTERNAL FUNCTIONS
"""
Using the Controls window:
http://www.vpython.org/contents/docs/visual/controls.html
"""
#Constants
s=0.9
ctrlWinW=650
ctrlWinH=350
ctrlWinAspect=0.7857#0.7857#1.*ctrlWinW/ctrlWinH
dataWinW=650
dataWinH=350
dataWinAspect=dataWinW/dataWinH
sliderLen=125*s
sliderW=15*s
slider_txtbuf=22*s
labelW=145*s
labelH=20*s
vspacing=52
red=(1,0,0)
green=(0,1,0)
blue=(0,0,1)
ltgrey=(0.7,0.7,0.7)
white=(1,1,1)
sliderCol=(0.6,0.6,1)
def testReturn(x):
pass
def cWinPos(x,y):
"""Convert regular screen-pixel coordinates to a control window position."""
return (-ctrlWinW/2+75 + x*ctrlWinAspect, ctrlWinH/2 - y)
def dWinPos(x,y):
"""Convert regular screen-pixel coordinate to a display window position."""
return (-dataWinW/2 + x*dataWinAspect, dataWinH/2 - y)
# ------------------------------------------------------------------------------
#WIDGET ABSTRACTION
class text(menu):
"""A simple wrapper for the menu class, to create a static label on Control
windows.
"""
def __init__(self,x,y,text,color=ltgrey):
menu.__init__(self, pos=cWinPos(x,y), text=text, width=labelW,
height=labelH, color=color)
class dragbar:
"""A widget combining a label, a slider and associated formatting.
Constructor: dragbar(tuple<float,float,float>,string,float,float,float,function,
color)
Class invariant: action_change takes a float as input
"""
def _move(self):
"""Send slider value to be processed"""
self._sendValue(self.sliderObj.value)
def getValue(self):
"""Return the slider reading."""
return self.sliderObj.value
def setValue(self,val):
"""Change the slider position"""
self.sliderObj.value=val
self._move()
def __init__(self, x, y, title, minval, maxval, action_change,
color=sliderCol):
"""Create GUI elements"""
self._sendValue = action_change
self._title=title
self.x,self.y=cWinPos(x,y)
#Create the components
self._title=title
self.sliderObj = slider(pos=( self.x-sliderLen/2+5, self.y ),
width = sliderW,
length = sliderLen,
axis = (1,0,0),
color = color,
min = minval, max = maxval,
action = self._move)
if self._title<>'':
self.titleLbl = text(x,y-slider_txtbuf,title)
class colorSlider:
"""A widget combining three dragbars, a title and color display.
Constructor: colorSlider(float,float,function,color)
Class invariant: action_change takes a color as input
"""
def getColor(self):
"""Return the combined color from slider values"""
return (self.s1.getValue(),self.s2.getValue(),self.s3.getValue())
def update(self):
"""Fill the color box; pass the value to the change action"""
val=self.getColor()
self._updateColor(val)
def sliderMove(self, sliderValue):
"""Slider change event"""
self.update()
def setColor(self,color):
"""Overwrite color represented by sliders"""
self.s1.setValue(color[0])
self.s2.setValue(color[1])
self.s3.setValue(color[2])
self.update()
def __init__(self,x,y,action_change):
"""Initialise and update GUI elements"""
self._updateColor=action_change
#Create the components
self.s1=dragbar(x,y-15,'',0,1,self.sliderMove,color=red)
self.s2=dragbar(x,y ,'',0,1,self.sliderMove,color=green)
self.s3=dragbar(x,y+15,'',0,1,self.sliderMove,color=blue)
self.update()
class colorTribar:
"""A widget using a Curve object to show 3 color values.
Constructor: colorTribar(float,float,float,float,color,color,color)
"""
def update(self):
"""Update the color values on the model from memory"""
self._model.color[0]=self._col1
self._model.color[1]=self._col1
self._model.color[2]=self._col2
self._model.color[3]=self._col2
self._model.color[4]=self._col3
self._model.color[5]=self._col3
def setColor(self,col1=None,col2=None,col3=None):
"""Change the color values.
setColor(color,color,color) -> void
"""
if col1: self._col1=col1
if col2: self._col2=col2
if col3: self._col3=col3
self.update()
def __init__(self,x,y,w,h,col1,col2,col3):
"""Initialise and update GUI elements"""
#Store colors
self._col1=col1
self._col2=col2
self._col3=col3
#Create component
pos=dWinPos(x,y)
ylist=[pos[1]-h,
pos[1]-0.33*h, pos[1]-0.33*h,
pos[1]+0.33*h, pos[1]+0.33*h,
pos[1]+h]
self._model=curve(radius=w,x=pos[0],y=ylist)
self.update()
# ------------------------------------------------------------------------------
#GUI BUILDING/OPERATION
class GUI:
"""The user interaction handler and main application GUI. """
#functions: initialisation; all buttons; all sliders
#do return statements where needed: load/changename button; colorsliders
def color1Change(self,value):
"""Call the general color changer"""
self._colorChange(1,value)
def color2Change(self,value):
"""Call the general color changer"""
self._colorChange(2,value)
def color3Change(self,value):
"""Call the general color changer"""
self._colorChange(3,value)
def _colorChange(self,colorNum,value):
"""Update seed data and display with the new color"""
if colorNum==1: self.colorBar.setColor(col1=value)
if colorNum==2: self.colorBar.setColor(col2=value)
if colorNum==3: self.colorBar.setColor(col3=value)
#self.mainseed.color[colorNum] = value
def globalUpdate(self):
"""Update all display widgets from the current seed settings"""
pass
#self.colorSlider1.setColor(ltgrey)
#update all widgets. after loading a new seed
#(Override control settings with seed data; automatically triggers)
def __init__(self):
"""Create all GUI components"""
#Initialiser: get file/keep data
#Add widgets for the Data display
##self.xx=box(pos=dWinPos(dataWinW*0.25,dataWinH*0.3), width=10, height=10, length=10,
##color=color.red)
self.colorBar=colorTribar(dataWinW*0.5,dataWinH*0.5,10,100,red,green,blue)
#Add widgets for the Controls display
text(0,100,'COLORS',white)
self.colorSlider1 = colorSlider(-8,100+vspacing*1,self.color1Change)
self.colorSlider2 = colorSlider(-8,100+vspacing*2,self.color2Change)
self.colorSlider3 = colorSlider(-8,100+vspacing*3,self.color3Change)
x=220
text(x,100,'FIRST STEM',blue)
self.iniLenBar = dragbar(x,110+vspacing*1,'Length:',0,1,testReturn)
self.iniWidthBar = dragbar(x,110+vspacing*2,'Width:',0,1,testReturn)
self.iniAngleBar = dragbar(x,110+vspacing*3,'Branch Angle:',0,1,testReturn)
self.iniSplitsBar = dragbar(x,110+vspacing*4,'Branches/Split:',0,1,testReturn)
x=435
text(x,100,'CHANGE RATES',red)
self.changeLenBar = dragbar(x,110+vspacing*1,'Length:',0,1,testReturn)
self.changeWidthBar = dragbar(x,110+vspacing*2,'Width:',0,1,testReturn)
self.changeAngleBar = dragbar(x,110+vspacing*3,'Branch Angle:',0,1,testReturn)
self.changeSplitsBar = dragbar(x,110+vspacing*4,'Branches/Split',0,1,testReturn)
x=650
text(x,100,'GROW CYCLE',green)
self.cycleTimeBar = dragbar(x,110+vspacing*1,'Time:',0,1,testReturn)
self.cycleMutationBar = dragbar(x,110+vspacing*2,'Mutation:',0,1,testReturn)
self.cycleIterationsBar = dragbar(x,110+vspacing*3,'Iterations:',0,1,testReturn)
#Update state
self.globalUpdate()
# --------------------------------------------------------------------------
#MAIN APPLICATION
class runtime:
"""The main application """
def __init__(self):
"""Set up the controls window and process input."""
#Make controls window
self.ctrlWin = controls(x=25, y=25, width=ctrlWinW,
title='==PyPlant Seed Editor== Controls',
height=ctrlWinH, range=ctrlWinH)
slider(pos=(-999,-999))
#Make data window
self.dataWin = display(height=dataWinH, width=dataWinW,
range=dataWinH,
title='==PyPlant Seed Editor== Data',
x = self.ctrlWin.x,
y=self.ctrlWin.y+self.ctrlWin.height)
self.g = GUI()
#Main loop
while True:
rate(100)
self.ctrlWin.interact()
r=runtime()
# ----------------------------------------------------------------------------