00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "colors.hxx"
00021 #include "controller.hxx"
00022 #include "world_gui_manager.hxx"
00023 #include "worldview_tool.hxx"
00024 #include "worldview_insert_tool.hxx"
00025 #include "worldview_select_tool.hxx"
00026 #include "worldview_zoom_tool.hxx"
00027 #include "worldview_collider_tool.hxx"
00028 #include "worldview_component.hxx"
00029
00030 WorldViewComponent* WorldViewComponent::instance_;
00031
00032 WorldViewComponent::WorldViewComponent ()
00033 : GUIComponent(0, 0, graphic_context->get_width (), graphic_context->get_height ())
00034 {
00035 instance_ = this;
00036
00037 scrolling = false;
00038
00039 select_tool = new WorldViewSelectTool ();
00040 insert_tool = new WorldViewInsertTool ();
00041 zoom_tool = new WorldViewZoomTool ();
00042 collider_tool = new WorldViewColliderTool ();
00043
00044 current_tool = insert_tool;
00045 mode = INSERT_MODE;
00046 }
00047
00048 void
00049 WorldViewComponent::set_mode (Mode m)
00050 {
00051 current_tool->deactivate ();
00052
00053 if (m == INSERT_MODE)
00054 {
00055 current_tool = insert_tool;
00056 mode = INSERT_MODE;
00057 }
00058 else if (m == SELECT_MODE)
00059 {
00060 current_tool = select_tool;
00061 mode = SELECT_MODE;
00062 }
00063 else if (m == ZOOM_MODE)
00064 {
00065 current_tool = zoom_tool;
00066 mode = ZOOM_MODE;
00067 }
00068 else if (m == COLLIDER_MODE)
00069 {
00070 current_tool = collider_tool;
00071 mode = COLLIDER_MODE;
00072 }
00073 else
00074 {
00075 std::cout << "Unknown Mode" << std::endl;
00076 assert (false);
00077 }
00078
00079 current_tool->activate ();
00080
00081 std::cout << "Setting Mode: " << m << " " << current_tool << std::endl;
00082 }
00083
00084 WorldViewComponent::~WorldViewComponent ()
00085 {
00086 instance_ = 0;
00087 }
00088
00089 void
00090 WorldViewComponent::draw (GraphicContext* parent_gc)
00091 {
00092
00093
00094
00095 gc.set_parent_gc (parent_gc);
00096
00097
00098 gc.draw_line (-10000, 599, 10000, 599, Colors::rect_collider_fg);
00099
00100 World& world = *Controller::instance()->get_world();
00101
00102 if (Controller::instance()->get_action_cam()
00103 && Controller::instance()->is_running())
00104 {
00105
00106 const WorldBoundingBox& box = world.calc_bounding_box();
00107 gc.zoom_to((int) box.x1, (int)box.y1,
00108 (int)box.x2, (int)box.y2);
00109 gc.zoom_out (get_width()/2, get_height()/2);
00110 gc.zoom_out (get_width()/2, get_height()/2);
00111 }
00112
00113 current_tool->draw_background (&gc);
00114
00115 world.draw_colliders (&gc);
00116 world.draw_springs (&gc);
00117 if (!Controller::instance()->get_hide_dots())
00118 world.draw_particles (&gc);
00119
00120 current_tool->draw_foreground (&gc);
00121
00122 if (0)
00123 {
00124 switch (mode)
00125 {
00126 case ZOOM_MODE:
00127 parent_gc->draw_string (10, parent_gc->get_height () - 15, "[ Zoom Mode ]");
00128 break;
00129 case INSERT_MODE:
00130 parent_gc->draw_string (10, parent_gc->get_height () - 15, "[ Insert Mode ]");
00131 break;
00132 case SELECT_MODE:
00133 parent_gc->draw_string (10, parent_gc->get_height () - 15, "[ Select Mode ]");
00134 break;
00135 case COLLIDER_MODE:
00136 parent_gc->draw_string (10, parent_gc->get_height () - 15, "[Collider Mode]");
00137 break;
00138 }
00139 }
00140
00141
00142
00143 }
00144
00145 void
00146 WorldViewComponent::wheel_up (int x, int y)
00147 {
00148 gc.zoom_in (x, y);
00149 }
00150
00151 void
00152 WorldViewComponent::wheel_down (int x, int y)
00153 {
00154 gc.zoom_out (x, y);
00155 }
00156
00157 void
00158 WorldViewComponent::on_button_press (int button_id, int x, int y)
00159 {
00160 current_tool->on_button_press (button_id, x, y);
00161 }
00162
00163 void
00164 WorldViewComponent::on_primary_button_press (int screen_x, int screen_y)
00165 {
00166 current_tool->on_primary_button_press (screen_x, screen_y);
00167 }
00168
00169 void
00170 WorldViewComponent::on_primary_button_release (int screen_x, int screen_y)
00171 {
00172 current_tool->on_primary_button_release (screen_x, screen_y);
00173 }
00174
00175 void
00176 WorldViewComponent::on_secondary_button_press (int screen_x, int screen_y)
00177 {
00178 current_tool->on_secondary_button_press (screen_x, screen_y);
00179 }
00180
00181 void
00182 WorldViewComponent::on_secondary_button_release (int screen_x, int screen_y)
00183 {
00184 current_tool->on_secondary_button_release (screen_x, screen_y);
00185 }
00186
00187 void
00188 WorldViewComponent::on_delete_press (int screen_x, int screen_y)
00189 {
00190 current_tool->on_delete_press (screen_x, screen_y);
00191 }
00192
00193 void
00194 WorldViewComponent::on_duplicate_press (int screen_x, int screen_y)
00195 {
00196 current_tool->on_duplicate_press (screen_x, screen_y);
00197 }
00198
00199 void
00200 WorldViewComponent::on_fix_press (int screen_x, int screen_y)
00201 {
00202 current_tool->on_fix_press (screen_x, screen_y);
00203 }
00204
00205 void
00206 WorldViewComponent::scroll_left ()
00207 {
00208 gc.translate_offset (-20, 0);
00209 }
00210
00211 void
00212 WorldViewComponent::scroll_right ()
00213 {
00214 gc.translate_offset (20, 0);
00215 }
00216
00217 void
00218 WorldViewComponent::scroll_up ()
00219 {
00220 gc.translate_offset (0, -20);
00221 }
00222
00223 void
00224 WorldViewComponent::scroll_down ()
00225 {
00226 gc.translate_offset (0, 20);
00227 }
00228
00229 void
00230 WorldViewComponent::on_tertiary_button_press (int x, int y)
00231 {
00232 scrolling = true;
00233 x_offset = gc.get_x_offset ();
00234 y_offset = gc.get_y_offset ();
00235 WorldGUIManager::instance()->grab_mouse (this);
00236
00237 scroll_pos_x = gc.screen_to_world_x(x);
00238 scroll_pos_y = gc.screen_to_world_y(y);
00239 }
00240
00241 void
00242 WorldViewComponent::on_tertiary_button_release (int x, int y)
00243 {
00244 scrolling = false;
00245 WorldGUIManager::instance()->ungrab_mouse (this);
00246 }
00247
00248 void
00249 WorldViewComponent::on_mouse_move (int x, int y, int of_x, int of_y)
00250 {
00251 if (scrolling)
00252 {
00253 int new_scroll_pos_x = int(x/gc.get_zoom() - x_offset);
00254 int new_scroll_pos_y = int(y/gc.get_zoom() - y_offset);
00255
00256 gc.set_offset (x_offset + (new_scroll_pos_x - scroll_pos_x),
00257 y_offset + (new_scroll_pos_y - scroll_pos_y));
00258
00259 }
00260 else
00261 {
00262 current_tool->on_mouse_move (x, y, of_x, of_y);
00263 }
00264 }
00265
00266 float
00267 WorldViewComponent::get_zoom ()
00268 {
00269 return gc.get_zoom ();
00270 }
00271
00272