00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "world.hxx"
00021 #include "controller.hxx"
00022 #include "screen_manager.hxx"
00023 #include "world_button.hxx"
00024 #include "construo_error.hxx"
00025
00026 WorldButton::WorldButton (const std::string& arg_filename, Mode m)
00027 : GUIFileButton (arg_filename),
00028 world(0),
00029 file_broken(false),
00030 mode (m)
00031 {
00032 }
00033
00034 WorldButton::~WorldButton ()
00035 {
00036 delete world;
00037 }
00038
00039 void
00040 WorldButton::load_world ()
00041 {
00042 if ((world == 0
00043 && !file_broken)
00044 || mtime != system_context->get_mtime(filename))
00045 {
00046 try {
00047 delete world;
00048 world = new World(filename);
00049 mtime = system_context->get_mtime(filename);
00050 } catch (ConstruoError& err) {
00051 std::cout << "ERROR: " << err.msg << std::endl;
00052 std::cout << "ERROR: WorldButton: Somthing went wrong loading " << filename << std::endl;
00053 world = 0;
00054 file_broken = true;
00055 }
00056 }
00057 }
00058
00059 void
00060 WorldButton::draw (GraphicContext* parent_gc)
00061 {
00062 load_world();
00063
00064 parent_gc->draw_fill_rect (x_pos, y_pos,
00065 x_pos + width, y_pos + height,
00066 Color (0xBB0000FF));
00067
00068 ZoomGraphicContext gc (x_pos, y_pos, x_pos + width, y_pos + height);
00069 gc.set_parent_gc(parent_gc);
00070
00071 gc.lock();
00072
00073 if (world)
00074 {
00075
00076 const WorldBoundingBox& box = world->calc_bounding_box();
00077 gc.zoom_to((int) box.x1, (int)box.y1,
00078 (int)box.x2, (int)box.y2);
00079 world->draw_colliders (&gc);
00080 world->draw_springs (&gc);
00081 }
00082 else
00083 {
00084
00085 gc.draw_line (0,0, gc.get_width (), gc.get_height (), Color (0xFF00FFFF));
00086 gc.draw_line (0,gc.get_height (), gc.get_width (), 0, Color (0xFF00FFFF));
00087 }
00088
00089 gc.unlock();
00090
00091 if (mouse_over)
00092 parent_gc->draw_rect (x_pos, y_pos,
00093 x_pos + width, y_pos + height,
00094 Color (0xFFFFFFFF));
00095 else
00096 parent_gc->draw_rect (x_pos, y_pos,
00097 x_pos + width, y_pos + height,
00098 Color (0xFF0000FF));
00099
00100 parent_gc->draw_string (x_pos + 20, y_pos + 160, filename);
00101 }
00102
00103 void
00104 WorldButton::on_click ()
00105 {
00106 std::cout << "WorldButton: detected click on: " << filename << std::endl;
00107 if (mode == SAVE_BUTTON)
00108 {
00109 Controller::instance()->save_world(filename);
00110 ScreenManager::instance()->set_gui(ScreenManager::WORLD_GUI);
00111 }
00112 else
00113 {
00114 Controller::instance()->load_world(filename);
00115 ScreenManager::instance()->set_gui(ScreenManager::WORLD_GUI);
00116 }
00117 }
00118
00119