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
00035 #include "platform/platform.hpp"
00036 #include "platform/thread.hpp"
00037 #include "framework/textbox.hpp"
00038 #include "framework/button.hpp"
00039 #include "framework/tabbox.hpp"
00040
00041 #include "main_window.hpp"
00042 #include "periapsis_app.hpp"
00043 #include "simulation_tab.hpp"
00044 #include "scenery_tab.hpp"
00045 #include "settings_tab.hpp"
00046
00047 #include "platform/lowlevel.hpp"
00048
00049 using namespace gsgl;
00050 using namespace gsgl::data;
00051 using namespace gsgl::platform;
00052 using namespace gsgl::framework;
00053
00054
00055
00056 namespace gsgl
00057 {
00058 namespace data
00059 {
00060 periapsis::main_window *periapsis::main_window::instance = 0;
00061 }
00062 }
00063
00064
00065 namespace periapsis
00066 {
00067
00068 class load_scenery_thread
00069 : public gsgl::platform::thread
00070 {
00071 public:
00072 synchronized<bool> loading_failed;
00073 synchronized<string> loading_message;
00074
00075 protected:
00076 virtual int run();
00077 };
00078
00079
00080
00081
00082 int load_scenery_thread::run()
00083 {
00084 try
00085 {
00086 loading_message = string(L"Loading scenery...");
00087 application::global_instance()->load_scenery(loading_message);
00088 loading_message = string(L"Finished loading scenery.");
00089 return 1;
00090 }
00091 catch (gsgl::exception & e)
00092 {
00093 loading_message = string(e.get_message()).copy();
00094 loading_failed = true;
00095 return 0;
00096 }
00097 }
00098
00099
00100
00101
00102
00103 config_variable<int> main_window::WIDTH (L"ui/main_window/width", 800);
00104 config_variable<int> main_window::HEIGHT (L"ui/main_window/height", 600);
00105 config_variable<color> main_window::FOREGROUND (L"ui/main_window/foreground", color(1, 1, 1, 1));
00106 config_variable<color> main_window::BACKGROUND (L"ui/main_window/background", color(0, 0, 0.6f, 0.6f));
00107 config_variable<string> main_window::FONT_FACE (L"ui/main_window/font_face", L"Sans");
00108 config_variable<int> main_window::FONT_SIZE (L"ui/main_window/font_size", 18);
00109
00110
00111 config_variable<int> main_window::TITLE_BOX_HEIGHT (L"ui/main_window/spacing/title_box_height", 32);
00112 config_variable<int> main_window::STATUS_BAR_HEIGHT (L"ui/main_window/spacing/status_bar_height", 24);
00113 config_variable<int> main_window::TAB_BOX_SPACE (L"ui/main_window/spacing/tab_box_space", 4);
00114 config_variable<int> main_window::QUIT_BUTTON_WIDTH (L"ui/main_window/spacing/quit_button_width", 64);
00115
00116
00117 static void handle_quit(button *);
00118
00119
00120
00121 main_window::main_window(const string & title, const int x, const int y)
00122 : widget(0, x, y, WIDTH, HEIGHT, FOREGROUND, BACKGROUND), gsgl::data::singleton<main_window>(),
00123 title_box(0), status_bar(0), tab_box(0), sim_tab(0), set_tab(0),
00124 need_to_load_scenery(true), loading_thread(0)
00125 {
00126 title_box = new textbox(this,
00127 0, HEIGHT - TITLE_BOX_HEIGHT,
00128 WIDTH, TITLE_BOX_HEIGHT,
00129 FOREGROUND, BACKGROUND,
00130 FONT_FACE, FONT_SIZE * 4 / 3);
00131 title_box->get_text() = title;
00132
00133
00134 quit_button = new button(this, WIDTH - QUIT_BUTTON_WIDTH, HEIGHT - TITLE_BOX_HEIGHT*3/4, QUIT_BUTTON_WIDTH, TITLE_BOX_HEIGHT * 3 / 4, FOREGROUND, BACKGROUND, FONT_FACE, FONT_SIZE, L"Quit");
00135 quit_button->set_on_click_handler(&handle_quit);
00136
00137
00138 status_bar = new textbox(this, 0, 0, WIDTH, STATUS_BAR_HEIGHT, FOREGROUND, BACKGROUND, FONT_FACE, FONT_SIZE);
00139 status_bar->get_text() = L"Welcome to Periapsis, the spaceflight simulator.";
00140
00141
00142 sim_tab = new simulation_tab();
00143 set_tab = new settings_tab();
00144
00145 tab_box = new tabbox(this,
00146 2*TAB_BOX_SPACE, STATUS_BAR_HEIGHT+2*TAB_BOX_SPACE,
00147 WIDTH-4*TAB_BOX_SPACE, HEIGHT - (TITLE_BOX_HEIGHT + STATUS_BAR_HEIGHT + 4*TAB_BOX_SPACE),
00148 FOREGROUND, BACKGROUND,
00149 FONT_FACE, FONT_SIZE,
00150 TITLE_BOX_HEIGHT * 2 / 3);
00151 tab_box->add_tab(L" Simulation", sim_tab);
00152 tab_box->add_tab(L" Settings", set_tab);
00153 }
00154
00155
00156 main_window::~main_window()
00157 {
00158 delete loading_thread;
00159
00160 }
00161
00162
00163 void main_window::set_status(const string & message)
00164 {
00165 status_bar->get_text() = message;
00166 }
00167
00168
00169
00170
00171 void main_window::draw()
00172 {
00173
00174 if (need_to_load_scenery)
00175 {
00176 need_to_load_scenery = false;
00177
00178 if (application::global_instance()->get_global_scenery())
00179 application::global_instance()->unload_scenery();
00180
00181 if (loading_thread)
00182 delete loading_thread;
00183
00184 loading_thread = new load_scenery_thread();
00185 loading_thread->loading_failed = false;
00186 loading_thread->start();
00187 }
00188
00189
00190 if (loading_thread && loading_thread->is_running())
00191 {
00192 if (loading_thread->loading_failed)
00193 {
00194 string temp = string(loading_thread->loading_message).copy();
00195 application::global_instance()->unload_scenery();
00196 throw runtime_exception(temp.w_string());
00197 }
00198
00199
00200 set_status(loading_thread->loading_message);
00201 }
00202
00203
00204 widget::draw();
00205 }
00206
00207
00208
00209
00210 static void handle_quit(button *)
00211 {
00212 application::global_instance()->quit_application();
00213 }
00214
00215
00216 }