00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "simulation_tab.hpp"
00035 #include "main_window.hpp"
00036
00037 #include "data/exception.hpp"
00038 #include "data/pointer.hpp"
00039 #include "data/config.hpp"
00040 #include "data/file.hpp"
00041 #include "data/directory.hpp"
00042 #include "math/time.hpp"
00043 #include "math/vector.hpp"
00044 #include "scenegraph/node.hpp"
00045 #include "scenegraph/simulation.hpp"
00046 #include "space/space_context.hpp"
00047
00048 #include "framework/textbox.hpp"
00049 #include "framework/button.hpp"
00050 #include "framework/treebox.hpp"
00051 #include "framework/application.hpp"
00052
00053
00054 using namespace gsgl;
00055 using namespace gsgl::data;
00056 using namespace gsgl::io;
00057 using namespace gsgl::math;
00058 using namespace gsgl::framework;
00059 using namespace gsgl::scenegraph;
00060
00061 namespace periapsis
00062 {
00063
00064 static config_variable<int> BUTTON_SPACE(L"ui/simulation_tab/button_space", 10);
00065
00066
00067 simulation_tab::simulation_tab()
00068 : widget(0, 0, 0, 0, 0, main_window::FOREGROUND, main_window::BACKGROUND),
00069 title_box(0), time_box(0), view_box(0), load_button(0), save_button(0), go_button(0),
00070 waiting_for_scenery(false), sim_is_ready(false),
00071 current_sim_record(0)
00072 {
00073 }
00074
00075
00076 simulation_tab::~simulation_tab()
00077 {
00078
00079 }
00080
00081
00082 static void handle_run_simulation(button *);
00083
00084 void simulation_tab::draw()
00085 {
00086 int eighth = get_h() / 8;
00087
00088
00089 if (!title_box)
00090 {
00091 int baseline = 7 * eighth;
00092
00093 title_box = new textbox(this,
00094 BUTTON_SPACE, baseline + BUTTON_SPACE,
00095 get_w() - 2*BUTTON_SPACE, eighth - 2*BUTTON_SPACE,
00096 main_window::FOREGROUND, main_window::BACKGROUND,
00097 main_window::FONT_FACE, main_window::FONT_SIZE);
00098 title_box->get_text() = L"<simulation description>";
00099 }
00100
00101
00102 if (!time_box)
00103 {
00104 int baseline = 6 * eighth + eighth/2;
00105
00106 time_box = new datetime_box(this,
00107 BUTTON_SPACE, baseline + BUTTON_SPACE,
00108 get_w() - 2*BUTTON_SPACE, main_window::FONT_SIZE*4/3,
00109 main_window::FOREGROUND, main_window::BACKGROUND);
00110
00111
00112
00113
00114 time_box->set_text(L"2007-03-20 12:0:0");
00115 }
00116
00117
00118 if (!view_box)
00119 {
00120 int baseline = eighth/2 + BUTTON_SPACE/2;
00121
00122 view_box = new sim_view_box(this,
00123 BUTTON_SPACE, baseline + BUTTON_SPACE,
00124 get_w()/2 - 2*BUTTON_SPACE, 6*eighth - 2*BUTTON_SPACE,
00125 main_window::FOREGROUND, main_window::BACKGROUND);
00126 treebox *view_tree = view_box->get_scenery_box();
00127 treebox_node *temp_node = new treebox_node(view_tree, 0, main_window::FOREGROUND, main_window::BACKGROUND, L"Loading...", 0);
00128
00129 waiting_for_scenery = true;
00130 }
00131
00132
00133 if (!load_button)
00134 {
00135 load_button = new button(this,
00136 BUTTON_SPACE, BUTTON_SPACE,
00137 (get_w() / 3) - 2*BUTTON_SPACE, main_window::FONT_SIZE * 4 / 3,
00138 main_window::FOREGROUND, main_window::BACKGROUND,
00139 main_window::FONT_FACE, main_window::FONT_SIZE,
00140 L"Load Simulation");
00141 }
00142
00143 if (!save_button)
00144 {
00145 save_button = new button(this,
00146 (get_w() / 3) + BUTTON_SPACE, BUTTON_SPACE,
00147 (get_w() / 3) - 2*BUTTON_SPACE, main_window::FONT_SIZE * 4 / 3,
00148 main_window::FOREGROUND, main_window::BACKGROUND,
00149 main_window::FONT_FACE, main_window::FONT_SIZE,
00150 L"Save Simulation");
00151 }
00152
00153 if (!go_button)
00154 {
00155 go_button = new button(this,
00156 (get_w() * 2 / 3) + BUTTON_SPACE, BUTTON_SPACE,
00157 (get_w() / 3) - 2*BUTTON_SPACE, main_window::FONT_SIZE * 4 / 3,
00158 main_window::FOREGROUND, main_window::BACKGROUND,
00159 main_window::FONT_FACE, main_window::FONT_SIZE,
00160 L"Run Simulation");
00161 go_button->set_on_click_handler(&handle_run_simulation);
00162 }
00163
00164
00165 if (waiting_for_scenery)
00166 {
00167 if (application::global_instance()->get_global_scenery())
00168 {
00169 view_box->load_scenery_info();
00170 waiting_for_scenery = false;
00171 }
00172 }
00173
00174
00175 sim_is_ready = (time_box->get_jdn() != 0)
00176 && (view_box->get_scenery_box()->get_selected_node() != 0);
00177
00178 if (sim_is_ready)
00179 {
00180 go_button->set_flags(widget::WIDGET_INACTIVE, false);
00181 }
00182 else
00183 {
00184 go_button->set_flags(widget::WIDGET_INACTIVE, true);
00185 }
00186
00187
00188
00189 }
00190
00191
00192 static string get_temp_sim_fname()
00193 {
00194 return application::USER_DATA_PATH + L"Currently Running.sim";
00195 }
00196
00197
00198 static void handle_run_simulation(button *b)
00199 {
00200 simulation_tab *stab = dynamic_cast<simulation_tab *>(b->get_parent());
00201 if (!stab)
00202 throw internal_exception(__FILE__, __LINE__, L"Run Simulation button is not correctly parented.");
00203
00204
00205 config_record *sim_rec = stab->current_sim_record;
00206 if (!sim_rec)
00207 {
00208 string fname = get_temp_sim_fname();
00209 if (file::exists(fname))
00210 file::remove(fname);
00211
00212
00213 {
00214 file f(fname);
00215 smart_pointer<ft_stream> s(f.open_text(io::FILE_OPEN_WRITE));
00216 *s << L"<simulation name=\"Running Simulation\"></simulation>";
00217 }
00218
00219 sim_rec = new config_record(fname);
00220 }
00221
00222
00223 config_record & time_param = sim_rec->get_child(L"start_time");
00224 if (time_param.get_text().is_empty())
00225 {
00226 time_param.get_text() = string::format(L"%f", stab->get_time_box()->get_jdn());
00227 }
00228
00229
00230 config_record & view_param = sim_rec->get_child(L"viewpoint");
00231 if (view_param.get_text().is_empty())
00232 {
00233 node *n = reinterpret_cast<node *>(stab->get_view_box()->get_scenery_box()->get_selected_node()->get_user_data());
00234 assert(n);
00235
00236 view_param[L"parent"] = n->get_name();
00237 }
00238
00239
00240 string fname = sim_rec->get_file().get_full_path();
00241 sim_rec->save();
00242 delete sim_rec;
00243 sim_rec = 0;
00244
00245
00246 application::global_instance()->load_and_run_simulation(fname, new space::space_context());
00247 }
00248
00249
00250 }