1 // fg_props.cxx -- support for FlightGear properties.
3 // Written by David Megginson, started 2000.
5 // Copyright (C) 2000, 2001 David Megginson - david@megginson.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # include <simgear/compiler.h>
27 #include <simgear/misc/exception.hxx>
31 #include <ATC/ATCdisplay.hxx>
32 #include <Aircraft/aircraft.hxx>
33 #include <Time/tmp.hxx>
34 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
35 #ifndef FG_NEW_ENVIRONMENT
36 # include <WeatherCM/FGLocalWeatherDatabase.h>
38 # include <Environment/environment.hxx>
39 #endif // FG_NEW_ENVIRONMENT
40 #include <Objects/matlib.hxx>
44 #include "globals.hxx"
46 #include "fg_props.hxx"
47 #include "viewmgr.hxx"
49 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
50 SG_USING_STD(istream);
51 SG_USING_STD(ostream);
54 #if !defined(FG_NEW_ENVIRONMENT)
55 static double getWindNorth ();
56 static double getWindEast ();
57 static double getWindDown ();
58 #endif // FG_NEW_ENVIRONMENT
60 // Allow the view to be set from two axes (i.e. a joystick hat)
61 // This needs to be in FGViewer itself, somehow.
62 static double axisLong = 0.0;
63 static double axisLat = 0.0;
65 static bool winding_ccw = true; // FIXME: temporary
67 static bool fdm_data_logging = false; // FIXME: temporary
74 _set_view_from_axes ()
76 // Take no action when hat is centered
77 if ( ( axisLong < 0.01 ) &&
78 ( axisLong > -0.01 ) &&
86 /* Do all the quick and easy cases */
87 if (axisLong < 0) { // Longitudinal axis forward
88 if (axisLat == axisLong)
90 else if (axisLat == - axisLong)
92 else if (axisLat == 0)
94 } else if (axisLong > 0) { // Longitudinal axis backward
95 if (axisLat == - axisLong)
97 else if (axisLat == axisLong)
99 else if (axisLat == 0)
101 } else if (axisLong == 0) { // Longitudinal axis neutral
104 else if (axisLat > 0)
106 else return; /* And assertion failure maybe? */
109 /* Do all the difficult cases */
111 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axisLat, -axisLong );
112 if ( viewDir < -1 ) viewDir += 360;
114 // SG_LOG(SG_INPUT, SG_ALERT, "Joystick Lat=" << axisLat << " and Long="
115 // << axisLong << " gave angle=" << viewDir );
117 globals->get_current_view()->set_goal_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
118 // globals->get_current_view()->set_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
122 ////////////////////////////////////////////////////////////////////////
123 // Default property bindings (not yet handled by any module).
124 ////////////////////////////////////////////////////////////////////////
126 struct LogClassMapping {
129 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
132 LogClassMapping log_class_mappings [] = {
133 LogClassMapping(SG_NONE, "none"),
134 LogClassMapping(SG_TERRAIN, "terrain"),
135 LogClassMapping(SG_ASTRO, "astro"),
136 LogClassMapping(SG_FLIGHT, "flight"),
137 LogClassMapping(SG_INPUT, "input"),
138 LogClassMapping(SG_GL, "gl"),
139 LogClassMapping(SG_VIEW, "view"),
140 LogClassMapping(SG_COCKPIT, "cockpit"),
141 LogClassMapping(SG_GENERAL, "general"),
142 LogClassMapping(SG_MATH, "math"),
143 LogClassMapping(SG_EVENT, "event"),
144 LogClassMapping(SG_AIRCRAFT, "aircraft"),
145 LogClassMapping(SG_AUTOPILOT, "autopilot"),
146 LogClassMapping(SG_IO, "io"),
147 LogClassMapping(SG_CLIPPER, "clipper"),
148 LogClassMapping(SG_NETWORK, "network"),
149 LogClassMapping(SG_UNDEFD, "")
154 * Get the logging classes.
159 sgDebugClass classes = logbuf::get_log_classes();
161 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
162 if ((classes&log_class_mappings[i].c) > 0) {
163 if (result != (string)"")
165 result += log_class_mappings[i].name;
172 static void addLoggingClass (const string &name)
174 sgDebugClass classes = logbuf::get_log_classes();
175 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
176 if (name == log_class_mappings[i].name) {
177 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
181 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown logging class: " << name);
186 * Set the logging classes.
189 setLoggingClasses (string classes)
191 logbuf::set_log_classes(SG_NONE);
193 if (classes == "none") {
194 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
198 if (classes == "" || classes == "all") { // default
199 logbuf::set_log_classes(SG_ALL);
200 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
201 << getLoggingClasses());
205 string rest = classes;
207 int sep = rest.find('|');
209 name = rest.substr(0, sep);
210 rest = rest.substr(sep+1);
211 addLoggingClass(name);
212 sep = rest.find('|');
214 addLoggingClass(rest);
215 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
216 << getLoggingClasses());
221 * Get the logging priority.
224 getLoggingPriority ()
226 switch (logbuf::get_log_priority()) {
238 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
239 << logbuf::get_log_priority());
246 * Set the logging priority.
249 setLoggingPriority (string priority)
251 if (priority == "bulk") {
252 logbuf::set_log_priority(SG_BULK);
253 } else if (priority == "debug") {
254 logbuf::set_log_priority(SG_DEBUG);
255 } else if (priority == "" || priority == "info") { // default
256 logbuf::set_log_priority(SG_INFO);
257 } else if (priority == "warn") {
258 logbuf::set_log_priority(SG_WARN);
259 } else if (priority == "alert") {
260 logbuf::set_log_priority(SG_ALERT);
262 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
264 SG_LOG(SG_GENERAL, SG_INFO, "Logging priority is " << getLoggingPriority());
270 * Get the pause state of the sim.
275 return globals->get_freeze();
280 * Set the pause state of the sim.
283 setFreeze (bool freeze)
285 globals->set_freeze(freeze);
288 current_atcdisplay->CancelRepeatingMessage();
289 current_atcdisplay->RegisterRepeatingMessage("**** SIM IS FROZEN **** SIM IS FROZEN ****");
292 current_atcdisplay->CancelRepeatingMessage();
298 * Return the current aircraft directory (UIUC) as a string.
308 * Set the current aircraft directory (UIUC).
311 setAircraftDir (string dir)
313 if (getAircraftDir() != dir) {
315 // needReinit(); FIXME!!
321 * Get the current view offset in degrees.
326 return (globals->get_current_view()
327 ->get_view_offset() * SGD_RADIANS_TO_DEGREES);
332 setViewOffset (double offset)
334 globals->get_current_view()->set_view_offset(offset * SGD_DEGREES_TO_RADIANS);
340 return (globals->get_current_view()
341 ->get_goal_view_offset() * SGD_RADIANS_TO_DEGREES);
345 setGoalViewOffset (double offset)
347 while ( offset < 0 ) {
350 while ( offset > 360.0 ) {
353 // Snap to center if we are close
354 if ( fabs(offset) < 1.0 || fabs(offset) > 359.0 ) {
358 globals->get_current_view()
359 ->set_goal_view_offset(offset * SGD_DEGREES_TO_RADIANS);
363 * Get the current view tilt in degrees.
368 return (globals->get_current_view()
369 ->get_view_tilt() * SGD_RADIANS_TO_DEGREES);
374 setViewTilt (double tilt)
376 globals->get_current_view()->set_view_tilt(tilt * SGD_DEGREES_TO_RADIANS);
382 return (globals->get_current_view()
383 ->get_goal_view_tilt() * SGD_RADIANS_TO_DEGREES);
387 setGoalViewTilt (double tilt)
392 while ( tilt > 360.0 ) {
395 // Snap to center if we are close
396 if ( fabs(tilt) < 1.0 || fabs(tilt) > 359.0 ) {
400 globals->get_current_view()
401 ->set_goal_view_tilt(tilt * SGD_DEGREES_TO_RADIANS);
406 * Pilot position offset from CG.
409 getPilotPositionXOffset ()
411 FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
412 float * offset = pilot_view->get_pilot_offset();
417 setPilotPositionXOffset (float x)
419 FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
420 float * offset = pilot_view->get_pilot_offset();
421 pilot_view->set_pilot_offset(x, offset[1], offset[2]);
425 getPilotPositionYOffset ()
427 FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
428 float * offset = pilot_view->get_pilot_offset();
433 setPilotPositionYOffset (float y)
435 FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
436 float * offset = pilot_view->get_pilot_offset();
437 pilot_view->set_pilot_offset(offset[0], y, offset[2]);
441 getPilotPositionZOffset ()
443 FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
444 float * offset = pilot_view->get_pilot_offset();
449 setPilotPositionZOffset (float z)
451 FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
452 float * offset = pilot_view->get_pilot_offset();
453 pilot_view->set_pilot_offset(offset[0], offset[1], z);
458 * Return the current Zulu time.
465 struct tm * t = globals->get_time_params()->getGmt();
466 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
467 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
468 t->tm_hour, t->tm_min, t->tm_sec);
475 * Set the current Zulu time.
478 setDateString (string date_string)
480 static const SGPropertyNode *cur_time_override
481 = fgGetNode("/sim/time/cur-time-override", true);
483 SGTime * st = globals->get_time_params();
484 struct tm * current_time = st->getGmt();
487 // Scan for basic ISO format
488 // YYYY-MM-DDTHH:MM:SS
489 int ret = sscanf(date_string.c_str(), "%d-%d-%dT%d:%d:%d",
490 &(new_time.tm_year), &(new_time.tm_mon),
491 &(new_time.tm_mday), &(new_time.tm_hour),
492 &(new_time.tm_min), &(new_time.tm_sec));
494 // Be pretty picky about this, so
495 // that strange things don't happen
496 // if the save file has been edited
499 SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
500 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
504 // OK, it looks like we got six
505 // values, one way or another.
506 new_time.tm_year -= 1900;
507 new_time.tm_mon -= 1;
509 // Now, tell flight gear to use
510 // the new time. This was far
511 // too difficult, by the way.
513 mktime(&new_time) - mktime(current_time) + globals->get_warp();
514 double lon = current_aircraft.fdm_state->get_Longitude();
515 double lat = current_aircraft.fdm_state->get_Latitude();
516 globals->set_warp(warp);
517 st->update(lon, lat, cur_time_override->getLongValue(), warp);
518 fgUpdateSkyAndLightingParams();
522 * Return the GMT as a string.
529 struct tm *t = globals->get_time_params()->getGmt();
530 sprintf(buf, " %.2d:%.2d:%.2d",
531 t->tm_hour, t->tm_min, t->tm_sec);
532 // cout << t << " " << buf << endl;
539 * Get the texture rendering state.
544 return (material_lib.get_step() == 0);
549 * Set the texture rendering state.
552 setTextures (bool textures)
555 material_lib.set_step(0);
557 material_lib.set_step(1);
562 * Return the magnetic variation
567 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
572 * Return the magnetic dip
577 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
582 * Return the current heading in degrees.
587 return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
591 #if !defined(FG_NEW_ENVIRONMENT)
594 * Get the current visibility (meters).
599 return WeatherDatabase->getWeatherVisibility();
604 * Set the current visibility (meters).
607 setVisibility (double visibility)
609 WeatherDatabase->setWeatherVisibility(visibility);
613 * Get the current wind north velocity (feet/second).
618 return current_aircraft.fdm_state->get_V_north_airmass();
623 * Set the current wind north velocity (feet/second).
626 setWindNorth (double speed)
628 current_aircraft.fdm_state
629 ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
634 * Get the current wind east velocity (feet/second).
639 return current_aircraft.fdm_state->get_V_east_airmass();
644 * Set the current wind east velocity (feet/second).
647 setWindEast (double speed)
649 cout << "Set wind-east to " << speed << endl;
650 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
657 * Get the current wind down velocity (feet/second).
662 return current_aircraft.fdm_state->get_V_down_airmass();
667 * Set the current wind down velocity (feet/second).
670 setWindDown (double speed)
672 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
677 #endif // FG_NEW_ENVIRONMENT
682 return globals->get_current_view()->get_fov();
689 globals->get_current_view()->set_fov( fov );
696 return globals->get_warp();
702 globals->set_warp(warp);
708 return globals->get_warp_delta();
712 setWarpDelta (long delta)
714 globals->set_warp_delta(delta);
718 setViewAxisLong (double axis)
724 setViewAxisLat (double axis)
736 setWindingCCW (bool state)
740 glFrontFace ( GL_CCW );
742 glFrontFace ( GL_CW );
748 #if defined(FX) && !defined(WIN32)
749 return globals->get_fullscreen();
756 setFullScreen (bool state)
758 #if defined(FX) && !defined(WIN32)
759 globals->set_fullscreen(state);
760 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
761 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
769 return fdm_data_logging;
773 setFDMDataLogging (bool state)
775 // kludge; no getter or setter available
776 if (state != fdm_data_logging) {
777 fgToggleFDMdataLogging();
778 fdm_data_logging = state;
783 ////////////////////////////////////////////////////////////////////////
784 // Tie the properties.
785 ////////////////////////////////////////////////////////////////////////
791 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
792 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
793 // fgTie("/sim/freeze", getFreeze, setFreeze);
794 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
795 fgTie("/sim/view/offset-deg", getViewOffset, setViewOffset, false);
796 fgSetArchivable("/sim/view/offset-deg");
797 fgTie("/sim/view/goal-offset-deg", getGoalViewOffset, setGoalViewOffset, false);
798 fgTie("/sim/view/tilt-deg", getViewTilt, setViewTilt, false);
799 fgSetArchivable("/sim/view/tilt-deg");
800 fgTie("/sim/view/goal-tilt-deg", getGoalViewTilt, setGoalViewTilt, false);
801 fgSetArchivable("/sim/view/goal-offset-deg");
802 fgTie("/sim/view/pilot/x-offset-m",
803 getPilotPositionXOffset, setPilotPositionXOffset);
804 fgSetArchivable("/sim/view/pilot/x-offset-m");
805 fgTie("/sim/view/pilot/y-offset-m",
806 getPilotPositionYOffset, setPilotPositionYOffset);
807 fgSetArchivable("/sim/view/pilot/y-offset-m");
808 fgTie("/sim/view/pilot/z-offset-m",
809 getPilotPositionZOffset, setPilotPositionZOffset);
810 fgSetArchivable("/sim/view/pilot/z-offset-m");
811 fgTie("/sim/time/gmt", getDateString, setDateString);
812 fgSetArchivable("/sim/time/gmt");
813 fgTie("/sim/time/gmt-string", getGMTString);
814 fgTie("/sim/rendering/textures", getTextures, setTextures);
817 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
820 #if !defined(FG_NEW_ENVIRONMENT)
821 fgTie("/environment/visibility-m", getVisibility, setVisibility);
822 fgSetArchivable("/environment/visibility-m");
823 fgTie("/environment/wind-from-north-fps", getWindNorth, setWindNorth);
824 fgSetArchivable("/environment/wind-from-north-fps");
825 fgTie("/environment/wind-from-east-fps", getWindEast, setWindEast);
826 fgSetArchivable("/environment/wind-from-east-fps");
827 fgTie("/environment/wind-from-down-fps", getWindDown, setWindDown);
828 fgSetArchivable("/environment/wind-from-down-fps");
831 fgTie("/environment/magnetic-variation-deg", getMagVar);
832 fgTie("/environment/magnetic-dip-deg", getMagDip);
835 fgTie("/sim/field-of-view", getFOV, setFOV);
836 fgSetArchivable("/sim/field-of-view");
837 fgTie("/sim/time/warp", getWarp, setWarp, false);
838 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
839 fgTie("/sim/view/axes/long", (double(*)())0, setViewAxisLong);
840 fgTie("/sim/view/axes/lat", (double(*)())0, setViewAxisLat);
842 // Misc. Temporary junk.
843 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
844 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
845 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
853 _set_view_from_axes();
858 ////////////////////////////////////////////////////////////////////////
860 ////////////////////////////////////////////////////////////////////////
864 * Save the current state of the simulator to a stream.
867 fgSaveFlight (ostream &output, bool write_all)
870 writeProperties(output, globals->get_props(), write_all);
871 } catch (const sg_exception &e) {
872 guiErrorMessage("Error saving flight: ", e);
880 * Restore the current state of the simulator from a stream.
883 fgLoadFlight (istream &input)
885 SGPropertyNode props;
887 readProperties(input, &props);
888 } catch (const sg_exception &e) {
889 guiErrorMessage("Error reading saved flight: ", e);
892 copyProperties(&props, globals->get_props());
893 // When loading a flight, make it the
894 // new initial state.
895 globals->saveInitialState();
901 ////////////////////////////////////////////////////////////////////////
902 // Implementation of FGCondition.
903 ////////////////////////////////////////////////////////////////////////
905 FGCondition::FGCondition ()
909 FGCondition::~FGCondition ()
915 ////////////////////////////////////////////////////////////////////////
916 // Implementation of FGPropertyCondition.
917 ////////////////////////////////////////////////////////////////////////
919 FGPropertyCondition::FGPropertyCondition (const string &propname)
920 : _node(fgGetNode(propname, true))
924 FGPropertyCondition::~FGPropertyCondition ()
930 ////////////////////////////////////////////////////////////////////////
931 // Implementation of FGNotCondition.
932 ////////////////////////////////////////////////////////////////////////
934 FGNotCondition::FGNotCondition (FGCondition * condition)
935 : _condition(condition)
939 FGNotCondition::~FGNotCondition ()
945 FGNotCondition::test () const
947 return !(_condition->test());
952 ////////////////////////////////////////////////////////////////////////
953 // Implementation of FGAndCondition.
954 ////////////////////////////////////////////////////////////////////////
956 FGAndCondition::FGAndCondition ()
960 FGAndCondition::~FGAndCondition ()
962 for (unsigned int i = 0; i < _conditions.size(); i++)
963 delete _conditions[i];
967 FGAndCondition::test () const
969 int nConditions = _conditions.size();
970 for (int i = 0; i < nConditions; i++) {
971 if (!_conditions[i]->test())
978 FGAndCondition::addCondition (FGCondition * condition)
980 _conditions.push_back(condition);
985 ////////////////////////////////////////////////////////////////////////
986 // Implementation of FGOrCondition.
987 ////////////////////////////////////////////////////////////////////////
989 FGOrCondition::FGOrCondition ()
993 FGOrCondition::~FGOrCondition ()
995 for (unsigned int i = 0; i < _conditions.size(); i++)
996 delete _conditions[i];
1000 FGOrCondition::test () const
1002 int nConditions = _conditions.size();
1003 for (int i = 0; i < nConditions; i++) {
1004 if (_conditions[i]->test())
1011 FGOrCondition::addCondition (FGCondition * condition)
1013 _conditions.push_back(condition);
1018 ////////////////////////////////////////////////////////////////////////
1019 // Implementation of FGComparisonCondition.
1020 ////////////////////////////////////////////////////////////////////////
1023 doComparison (const SGPropertyNode * left, const SGPropertyNode *right)
1025 switch (left->getType()) {
1026 case SGPropertyNode::BOOL: {
1027 bool v1 = left->getBoolValue();
1028 bool v2 = right->getBoolValue();
1030 return FGComparisonCondition::LESS_THAN;
1032 return FGComparisonCondition::GREATER_THAN;
1034 return FGComparisonCondition::EQUALS;
1037 case SGPropertyNode::INT: {
1038 int v1 = left->getIntValue();
1039 int v2 = right->getIntValue();
1041 return FGComparisonCondition::LESS_THAN;
1043 return FGComparisonCondition::GREATER_THAN;
1045 return FGComparisonCondition::EQUALS;
1048 case SGPropertyNode::LONG: {
1049 long v1 = left->getLongValue();
1050 long v2 = right->getLongValue();
1052 return FGComparisonCondition::LESS_THAN;
1054 return FGComparisonCondition::GREATER_THAN;
1056 return FGComparisonCondition::EQUALS;
1059 case SGPropertyNode::FLOAT: {
1060 float v1 = left->getFloatValue();
1061 float v2 = right->getFloatValue();
1063 return FGComparisonCondition::LESS_THAN;
1065 return FGComparisonCondition::GREATER_THAN;
1067 return FGComparisonCondition::EQUALS;
1070 case SGPropertyNode::DOUBLE: {
1071 double v1 = left->getDoubleValue();
1072 double v2 = right->getDoubleValue();
1074 return FGComparisonCondition::LESS_THAN;
1076 return FGComparisonCondition::GREATER_THAN;
1078 return FGComparisonCondition::EQUALS;
1081 case SGPropertyNode::STRING:
1082 case SGPropertyNode::NONE:
1083 case SGPropertyNode::UNSPECIFIED: {
1084 string v1 = left->getStringValue();
1085 string v2 = right->getStringValue();
1087 return FGComparisonCondition::LESS_THAN;
1089 return FGComparisonCondition::GREATER_THAN;
1091 return FGComparisonCondition::EQUALS;
1095 throw sg_exception("Unrecognized node type");
1100 FGComparisonCondition::FGComparisonCondition (Type type, bool reverse)
1109 FGComparisonCondition::~FGComparisonCondition ()
1111 delete _right_value;
1115 FGComparisonCondition::test () const
1117 // Always fail if incompletely specified
1118 if (_left_property == 0 ||
1119 (_right_property == 0 && _right_value == 0))
1122 // Get LESS_THAN, EQUALS, or GREATER_THAN
1124 doComparison(_left_property,
1125 (_right_property != 0 ? _right_property : _right_value));
1127 return (cmp == _type);
1129 return (cmp != _type);
1133 FGComparisonCondition::setLeftProperty (const string &propname)
1135 _left_property = fgGetNode(propname, true);
1139 FGComparisonCondition::setRightProperty (const string &propname)
1141 delete _right_value;
1143 _right_property = fgGetNode(propname, true);
1147 FGComparisonCondition::setRightValue (const SGPropertyNode *node)
1149 _right_property = 0;
1150 delete _right_value;
1151 _right_value = new SGPropertyNode(*node);
1156 ////////////////////////////////////////////////////////////////////////
1157 // Read a condition and use it if necessary.
1158 ////////////////////////////////////////////////////////////////////////
1160 // Forward declaration
1161 static FGCondition * readCondition (const SGPropertyNode * node);
1163 static FGCondition *
1164 readPropertyCondition (const SGPropertyNode * node)
1166 return new FGPropertyCondition(node->getStringValue());
1169 static FGCondition *
1170 readNotCondition (const SGPropertyNode * node)
1172 int nChildren = node->nChildren();
1173 for (int i = 0; i < nChildren; i++) {
1174 const SGPropertyNode * child = node->getChild(i);
1175 FGCondition * condition = readCondition(child);
1177 return new FGNotCondition(condition);
1179 SG_LOG(SG_COCKPIT, SG_ALERT, "Panel: empty 'not' condition");
1183 static FGCondition *
1184 readAndConditions (const SGPropertyNode * node)
1186 FGAndCondition * andCondition = new FGAndCondition;
1187 int nChildren = node->nChildren();
1188 for (int i = 0; i < nChildren; i++) {
1189 const SGPropertyNode * child = node->getChild(i);
1190 FGCondition * condition = readCondition(child);
1192 andCondition->addCondition(condition);
1194 return andCondition;
1197 static FGCondition *
1198 readOrConditions (const SGPropertyNode * node)
1200 FGOrCondition * orCondition = new FGOrCondition;
1201 int nChildren = node->nChildren();
1202 for (int i = 0; i < nChildren; i++) {
1203 const SGPropertyNode * child = node->getChild(i);
1204 FGCondition * condition = readCondition(child);
1206 orCondition->addCondition(condition);
1211 static FGCondition *
1212 readComparison (const SGPropertyNode * node,
1213 FGComparisonCondition::Type type,
1216 FGComparisonCondition * condition = new FGComparisonCondition(type, reverse);
1217 condition->setLeftProperty(node->getStringValue("property[0]"));
1218 if (node->hasValue("property[1]"))
1219 condition->setRightProperty(node->getStringValue("property[1]"));
1221 condition->setRightValue(node->getChild("value", 0));
1226 static FGCondition *
1227 readCondition (const SGPropertyNode * node)
1229 const string &name = node->getName();
1230 if (name == "property")
1231 return readPropertyCondition(node);
1232 else if (name == "not")
1233 return readNotCondition(node);
1234 else if (name == "and")
1235 return readAndConditions(node);
1236 else if (name == "or")
1237 return readOrConditions(node);
1238 else if (name == "less-than")
1239 return readComparison(node, FGComparisonCondition::LESS_THAN, false);
1240 else if (name == "less-than-equals")
1241 return readComparison(node, FGComparisonCondition::GREATER_THAN, true);
1242 else if (name == "greater-than")
1243 return readComparison(node, FGComparisonCondition::GREATER_THAN, false);
1244 else if (name == "greater-than-equals")
1245 return readComparison(node, FGComparisonCondition::LESS_THAN, true);
1246 else if (name == "equals")
1247 return readComparison(node, FGComparisonCondition::EQUALS, false);
1248 else if (name == "not-equals")
1249 return readComparison(node, FGComparisonCondition::EQUALS, true);
1256 ////////////////////////////////////////////////////////////////////////
1257 // Implementation of FGConditional.
1258 ////////////////////////////////////////////////////////////////////////
1260 FGConditional::FGConditional ()
1265 FGConditional::~FGConditional ()
1271 FGConditional::setCondition (FGCondition * condition)
1274 _condition = condition;
1278 FGConditional::test () const
1280 return ((_condition == 0) || _condition->test());
1285 // The top-level is always an implicit 'and' group
1287 fgReadCondition (const SGPropertyNode * node)
1289 return readAndConditions(node);
1293 // end of fg_props.cxx