_helpRulesMessageBox = new FXMessageBox(getApp(), "Rules", rulesHelpText.str().c_str(), NULL, MBOX_OK);
- loadLSystem("lsystem.xml");
+ // load default L-system
+ _lsystemParameters.load(_lsystem, "lsystem.xml");
+ lsystemToWidgets();
}
*/
long GUI::onGenerate(FXObject *sender, FXSelector selector, void *data)
{
- // clear L-system
- _lsystem->clear();
-
-
- // get user input form widgets
-
- // axiom
- Rule axiom("axiom", _axiomTextField->getText().text(), 1.0);
- _lsystem->setAxiom(axiom);
-
- // rules
- string ruleSet = _rulesText->getText().text();
- string currentRule;
- for (int i = 0; i < ruleSet.size(); i++)
- {
- if (ruleSet[i] != '\n')
- {
- // rule component
- currentRule += ruleSet[i];
-
- if (i == ruleSet.size() - 1)
- {
- // last char in whole rule string
- _lsystem->setRule(Rule(currentRule));
- currentRule.clear();
- }
- }
- else
- {
- // last char in current rule
- _lsystem->setRule(Rule(currentRule));
- currentRule.clear();
- }
- }
-
- // angle
- _lsystem->setAngle(_angleRealSpinner->getValue());
-
- // depth
- _lsystem->setDepth(_depthSpinner->getValue());
-
- // diameter
- _lsystem->setDiameter(_diameterRealSpinner->getValue());
-
- // diameter factor
- _lsystem->setDiameterFactor(_diameterFactorRealSpinner->getValue());
-
+ widgetsToLsystem();
// generate and render L-system to screen
_lsystem->generate();
if (!loadFilename.empty())
{
// load from disk
- loadLSystem(loadFilename.text());
+ _lsystemParameters.load(_lsystem, loadFilename.text());
+ lsystemToWidgets();
}
}
{
FXString saveFilename = FXFileDialog::getSaveFilename(this, "Save file...", ".");
- if (FXStat::exists(saveFilename))
+ if (!saveFilename.empty())
{
- // the file already exists
-
- if (MBOX_CLICKED_YES == FXMessageBox::question(this, MBOX_YES_NO, "Overwrite file", "Overwrite existing file?"))
+ if (FXStat::exists(saveFilename))
+ {
+ // the file already exists
+
+ if (MBOX_CLICKED_YES == FXMessageBox::question(this, MBOX_YES_NO, "Overwrite file", "Overwrite existing file?"))
+ {
+ // overwrite file
+ widgetsToLsystem();
+ _lsystemParameters.save(_lsystem, saveFilename.text());
+ }
+ }
+ else
{
- // overwrite file
+ // save to file
+ widgetsToLsystem();
_lsystemParameters.save(_lsystem, saveFilename.text());
}
}
- else
- {
- // save to file
- _lsystemParameters.save(_lsystem, saveFilename.text());
- }
}
/**
- * Load L-system from file and sync with GUI
- * @param filename path of the L-system file
+ * Put content of widgets into the L-system
*/
-void GUI::loadLSystem(string filename)
+void GUI::widgetsToLsystem()
{
- // load from file
- _lsystemParameters.load(_lsystem, filename);
+ // clear L-system
+ _lsystem->clear();
+
+
+ // axiom
+ Rule axiom("axiom", _axiomTextField->getText().text(), 1.0);
+ _lsystem->setAxiom(axiom);
+
+ // rules
+ string ruleSet = _rulesText->getText().text();
+ string currentRule;
+ for (int i = 0; i < ruleSet.size(); i++)
+ {
+ if (ruleSet[i] != '\n')
+ {
+ // rule component
+ currentRule += ruleSet[i];
+
+ if (i == ruleSet.size() - 1)
+ {
+ // last char in whole rule string
+ _lsystem->setRule(Rule(currentRule));
+ currentRule.clear();
+ }
+ }
+ else
+ {
+ // last char in current rule
+ _lsystem->setRule(Rule(currentRule));
+ currentRule.clear();
+ }
+ }
+
+ // angle
+ _lsystem->setAngle(_angleRealSpinner->getValue());
+ // depth
+ _lsystem->setDepth(_depthSpinner->getValue());
- // sync with GUI
+ // segment diameter
+ _lsystem->setDiameter(_diameterRealSpinner->getValue());
+ // segment diameter factor
+ _lsystem->setDiameterFactor(_diameterFactorRealSpinner->getValue());
+}
+
+
+
+/**
+ * Put content of the L-system into widgets
+ */
+void GUI::lsystemToWidgets()
+{
// axiom
_axiomTextField->setText(_lsystem->getAxiom().getContent().c_str());