File save dialog bugfix.
authorspixx <spixx>
Mon, 27 Nov 2006 11:42:01 +0000 (11:42 +0000)
committerspixx <spixx>
Mon, 27 Nov 2006 11:42:01 +0000 (11:42 +0000)
Widgets / L-system sync rewrite.

src/gui.cpp

index 0b5d0276c6369e426341ff47f670a545599f48d6..16d712ac90d76ba06e63da0d07b60d298f482082 100644 (file)
@@ -183,7 +183,9 @@ GUI::GUI(FXApp *application, RenderingSurface *renderingSurface, LindenmayerSyst
     _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();
 }
 
 
@@ -220,53 +222,7 @@ void GUI::create()
  */
 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();
@@ -289,7 +245,8 @@ long GUI::onLoad(FXObject *sender, FXSelector selector, void *data)
     if (!loadFilename.empty())
     {
         // load from disk
-        loadLSystem(loadFilename.text());
+        _lsystemParameters.load(_lsystem, loadFilename.text());
+        lsystemToWidgets();
     }
 }
 
@@ -306,21 +263,26 @@ long GUI::onSave(FXObject *sender, FXSelector selector, void *data)
 {
     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());
-    }
 }
 
 
@@ -370,17 +332,63 @@ long GUI::onHelpRules(FXObject *sender, FXSelector selector, void *data)
 
 
 /**
- * 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());