1 // Copyright (C) 2006 Erik Dahlberg
3 // This file is part of LSystem3d.
5 // LSystem3D is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // LSystem3D is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with LSystem3D; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "lindenmayersystem.h"
29 #include "lsystemparameters.h"
30 #include "renderingsurface.h"
35 FXDEFMAP(GUI
) GUIMap
[] =
37 FXMAPFUNC(SEL_COMMAND
, GUI::ID_GENERATE
, GUI::onGenerateButtonPressed
)
40 // macro to set up class implementation
41 FXIMPLEMENT(GUI
, FXMainWindow
, GUIMap
, ARRAYNUMBER(GUIMap
))
47 * @param application the FOX application object
48 * @param renderingSurface the rendering surface
49 * @param lsystem the Lindenmayer-system
51 GUI::GUI(FXApp
*application
, RenderingSurface
*renderingSurface
, LindenmayerSystem
*lsystem
) : FXMainWindow(application
, "LSystem3D" , NULL
, NULL
, DECOR_ALL
, 920, 100, 0, 0)
54 _renderingSurface
= renderingSurface
;
60 FXVerticalFrame
*mainFrame
= new FXVerticalFrame(this, LAYOUT_FILL_X
);
63 FXHorizontalFrame
*axiomFrame
= new FXHorizontalFrame(mainFrame
, LAYOUT_FILL_X
);
64 new FXLabel(axiomFrame
, "Axiom:");
65 _axiomTextField
= new FXTextField(axiomFrame
, 10, NULL
, 0, LAYOUT_FILL_X
);
68 FXHorizontalFrame
*rulesFrame
= new FXHorizontalFrame(mainFrame
, LAYOUT_FILL_X
);
69 new FXLabel(rulesFrame
, "Rules:");
70 _rulesText
= new FXText(rulesFrame
, NULL
, 0, LAYOUT_FILL_X
);
71 _rulesText
->setVisibleColumns(30);
72 _rulesText
->setVisibleRows(10);
75 FXHorizontalFrame
*angleFrame
= new FXHorizontalFrame(mainFrame
);
76 new FXLabel(angleFrame
, "Angle:");
77 _angleRealSpinner
= new FXRealSpinner(angleFrame
, 5);
80 FXHorizontalFrame
*iterationsFrame
= new FXHorizontalFrame(mainFrame
);
81 new FXLabel(iterationsFrame
, "Iterations:");
82 _iterationsSpinner
= new FXSpinner(iterationsFrame
, 5);
85 FXHorizontalFrame
*diameterFrame
= new FXHorizontalFrame(mainFrame
);
86 new FXLabel(diameterFrame
, "Diameter:");
87 _diameterRealSpinner
= new FXRealSpinner(diameterFrame
, 5);
88 _diameterRealSpinner
->setValue(0.2);
89 _diameterRealSpinner
->setIncrement(0.1);
92 new FXLabel(diameterFrame
, "Factor:");
93 _diameterFactorRealSpinner
= new FXRealSpinner(diameterFrame
, 5);
94 _diameterFactorRealSpinner
->setValue(0.8);
95 _diameterFactorRealSpinner
->setIncrement(0.1);
98 new FXHorizontalSeparator(mainFrame
, SEPARATOR_RIDGE
| LAYOUT_FILL_X
);
101 new FXButton(mainFrame
, "&Generate", NULL
, this, GUI::ID_GENERATE
);
105 // load L-system parameters from file and sync with GUI
107 _lsystemParameters
.load(_lsystem
, "lsystem.xml");
110 _axiomTextField
->setText(_lsystem
->getAxiom().c_str());
113 map
<string
,string
> allRulesSrc
= _lsystem
->getRules();
115 for (map
<string
,string
>::iterator currentRule
= allRulesSrc
.begin();
116 currentRule
!= allRulesSrc
.end();
120 string rule
= currentRule
->first
+ "=" + currentRule
->second
+ '\n';
121 allRulesDst
+= rule
.c_str();
123 _rulesText
->setText(allRulesDst
.c_str(), allRulesDst
.size());
126 _angleRealSpinner
->setValue(_lsystem
->getAngle());
129 _iterationsSpinner
->setValue(_lsystem
->getNumIterations());
135 * Create and initialize the window
139 // create the window and make it appear
140 FXMainWindow::create();
147 * Called by the system when the "generate"-button is pressed
148 * @param sender the sender object
149 * @param selector message type and id
150 * @param data event related data
153 long GUI::onGenerateButtonPressed(FXObject
*sender
, FXSelector selector
, void *data
)
159 // load data from widgets into L-system
162 _lsystem
->setAxiom(_axiomTextField
->getText().text());
165 string ruleSet
= _rulesText
->getText().text();
169 for (int i
= 0; i
< ruleSet
.size(); i
++)
174 ruleName
= ruleSet
[i
];
179 if (ruleSet
[i
] != '\n' || i
== ruleSet
.size() - 1)
181 // part of current rule
182 currentRule
+= ruleSet
[i
];
185 if (ruleSet
[i
] == '\n' || i
== ruleSet
.size() - 1)
188 _lsystem
->setRule(ruleName
, currentRule
);
196 _lsystem
->setAngle(_angleRealSpinner
->getValue());
199 _lsystem
->setNumIterations(_iterationsSpinner
->getValue());
202 _lsystem
->setDiameter(_diameterRealSpinner
->getValue());
205 _lsystem
->setDiameterFactor(_diameterFactorRealSpinner
->getValue());
208 // generate and render L-system to screen
209 _lsystem
->generate();
210 _renderingSurface
->draw();