pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r3101 - in trunk/pingus/src: . display editor math


From: grumbel at BerliOS
Subject: [Pingus-CVS] r3101 - in trunk/pingus/src: . display editor math
Date: Fri, 7 Sep 2007 00:28:13 +0200

Author: grumbel
Date: 2007-09-07 00:28:12 +0200 (Fri, 07 Sep 2007)
New Revision: 3101

Modified:
   trunk/pingus/src/display/drawing_context.cpp
   trunk/pingus/src/display/drawing_context.hpp
   trunk/pingus/src/editor/editor_screen.cpp
   trunk/pingus/src/editor/editor_viewport.cpp
   trunk/pingus/src/editor/editor_viewport.hpp
   trunk/pingus/src/graphic_context_state.cpp
   trunk/pingus/src/graphic_context_state.hpp
   trunk/pingus/src/ground_map.cpp
   trunk/pingus/src/math/vector2i.cpp
   trunk/pingus/src/math/vector2i.hpp
Log:
- cleaned up world/screen coordinate conversion a bit (set_limit() still 
doesn't work as expected)

Modified: trunk/pingus/src/display/drawing_context.cpp
===================================================================
--- trunk/pingus/src/display/drawing_context.cpp        2007-09-06 20:41:54 UTC 
(rev 3100)
+++ trunk/pingus/src/display/drawing_context.cpp        2007-09-06 22:28:12 UTC 
(rev 3101)
@@ -360,13 +360,25 @@
 }
 
 Rect
-DrawingContext::get_clip_rect() const
+DrawingContext::get_world_clip_rect() const
 {
   return Rect(Vector2i(static_cast<int>(-translate_stack.back().x),
                           static_cast<int>(-translate_stack.back().y)),
                  Size((int)get_width(), (int)get_height()));
 }
 
+void
+DrawingContext::set_rect(const Rect& rect)
+{
+  std::cout << "DrawingContext::set_rect(const Rect& rect): unimplemented" << 
std::endl;
+}
+
+Rect
+DrawingContext::get_rect() const
+{
+  return rect;
+}
+
 float
 DrawingContext::get_width() const
 {
@@ -415,13 +427,29 @@
 Vector3f
 DrawingContext::screen_to_world (Vector3f pos)
 {
-  return pos - Vector3f(translate_stack.back().x, translate_stack.back().y);
+  return pos - Vector3f(translate_stack.back().x + rect.left,
+                        translate_stack.back().y + rect.top);
 }
 
 Vector3f
 DrawingContext::world_to_screen (Vector3f pos)
 {
-  return pos + Vector3f(translate_stack.back().x, translate_stack.back().y);
+  return pos + Vector3f(translate_stack.back().x + rect.left, 
+                        translate_stack.back().y + rect.top);
 }
 
+Vector2i
+DrawingContext::screen_to_world(const Vector2i pos)
+{
+  return pos - Vector2i(int(translate_stack.back().x + rect.left), 
+                        int(translate_stack.back().y + rect.top));
+}
+
+Vector2i
+DrawingContext::world_to_screen(const Vector2i pos)
+{
+  return pos + Vector2i(int(translate_stack.back().x + rect.left), 
+                        int(translate_stack.back().y + rect.top));
+}
+
 /* EOF */

Modified: trunk/pingus/src/display/drawing_context.hpp
===================================================================
--- trunk/pingus/src/display/drawing_context.hpp        2007-09-06 20:41:54 UTC 
(rev 3100)
+++ trunk/pingus/src/display/drawing_context.hpp        2007-09-06 22:28:12 UTC 
(rev 3101)
@@ -110,8 +110,11 @@
   void reset_modelview();
 
   /** Return the area of the screen that will be visible*/
-  Rect get_clip_rect() const;
+  Rect get_world_clip_rect() const;
 
+  void set_rect(const Rect& rect);
+  Rect get_rect() const;
+
   float get_width() const;
   float get_height() const;
 
@@ -127,6 +130,9 @@
   Vector3f screen_to_world (Vector3f pos);
   Vector3f world_to_screen (Vector3f pos);
 
+  Vector2i screen_to_world(const Vector2i pos);
+  Vector2i world_to_screen(const Vector2i pos);
+
 private:
   DrawingContext (const DrawingContext&);
   DrawingContext& operator= (const DrawingContext&);

Modified: trunk/pingus/src/editor/editor_screen.cpp
===================================================================
--- trunk/pingus/src/editor/editor_screen.cpp   2007-09-06 20:41:54 UTC (rev 
3100)
+++ trunk/pingus/src/editor/editor_screen.cpp   2007-09-06 22:28:12 UTC (rev 
3101)
@@ -125,7 +125,7 @@
 EditorScreen::draw(DrawingContext& gc)
 {
   // Black out screen
-  gc.fill_screen(Color(0,0,0));
+  gc.fill_screen(Color(255,0,255));
   gui_manager->draw(gc);
   
   if (show_help)

Modified: trunk/pingus/src/editor/editor_viewport.cpp
===================================================================
--- trunk/pingus/src/editor/editor_viewport.cpp 2007-09-06 20:41:54 UTC (rev 
3100)
+++ trunk/pingus/src/editor/editor_viewport.cpp 2007-09-06 22:28:12 UTC (rev 
3101)
@@ -54,42 +54,48 @@
 }
 
 void
-EditorViewport::on_secondary_button_press(int x, int y)
+EditorViewport::on_secondary_button_press(int x_, int y_)
 {
+  mouse_world_pos = screen2world(x_, y_);
+  mouse_screen_pos = Vector2i(x_, y_);
+
   if (current_action == NOTHING)
     {
-      drag_start_pos = mouse_at;  
+      drag_screen_pos = mouse_screen_pos;  
       current_action = SCROLLING;
     }
 }
 
 void
-EditorViewport::on_secondary_button_release(int x, int y)
+EditorViewport::on_secondary_button_release(int x_, int y_)
 {
+  mouse_world_pos = screen2world(x_, y_);
+  mouse_screen_pos = Vector2i(x_, y_);
+
   if (current_action == SCROLLING)
     current_action = NOTHING;
 }
 
 // When someone right-clicks inside the viewport
 void
-EditorViewport::on_secondary_button_click(int x, int y)
+EditorViewport::on_secondary_button_click(int x_, int y_)
 {
+  mouse_world_pos = screen2world(x_, y_);
+  mouse_screen_pos = Vector2i(x_, y_);
+  
   if (0) // old context menu code
     {
       remove_context_menu();
 
-      Vector3f mouse_pos(x - (state.get_width()/2 - state.get_pos().x),
-                         y - (state.get_height()/2 - state.get_pos().y));
+      //Vector3f mouse_pos(x - (state.get_width()/2  - state.get_pos().x),
+      //                   y - (state.get_height()/2 - state.get_pos().y));
 
-      std::cout << "Right-click at " << mouse_pos.x << ", " 
-                << mouse_pos.y << std::endl;
-       
       //       LevelObj* obj = object_at((int)mouse_pos.x, (int)mouse_pos.y);
       if (!selected_objs.empty())
         {
           //   std::vector<LevelObj*> objs;
           //   objs.push_back(obj);
-          context_menu = new ContextMenu(selected_objs, Vector3f((float)x, 
(float)y), this);
+          context_menu = new ContextMenu(selected_objs, Vector3f((float)x_, 
(float)y_), this);
           editor->get_gui_manager()->add(context_menu, true);
         }
     }
@@ -97,14 +103,16 @@
 
 // Select 1 or more LevelObjs, or drag them.
 void 
-EditorViewport::on_primary_button_press(int x, int y)
+EditorViewport::on_primary_button_press(int x_, int y_)
 {
+  mouse_world_pos  = screen2world(x_, y_);
+  mouse_screen_pos = Vector2i(x_, y_);
+  
   remove_context_menu();
 
   if (current_action == NOTHING)
     {
-      LevelObj* obj = object_at(x - (state.get_width()/2 - 
(int)state.get_pos().x),
-                                y - (state.get_height()/2 - 
(int)state.get_pos().y));
+      LevelObj* obj = object_at(mouse_world_pos.x, mouse_world_pos.y);
 
       if (obj)
         {
@@ -117,39 +125,34 @@
               obj->select();
               selected_objs.push_back(obj);
             }
+
           // Allow dragging of the currently selected objects
           current_action = DRAGGING;
-          drag_start_pos = mouse_at_world;
+          drag_world_pos = mouse_world_pos;
         }
       else
         {
           selected_objs.clear();
           current_action = HIGHLIGHTING;
-          highlighted_area.left = highlighted_area.right = x;
-          highlighted_area.top = highlighted_area.bottom = y;
+          highlighted_area.left = highlighted_area.right  = mouse_world_pos.x;
+          highlighted_area.top  = highlighted_area.bottom = mouse_world_pos.y;
         }
     }
 }
 
 void 
-EditorViewport::on_primary_button_release(int x, int y)
+EditorViewport::on_primary_button_release(int x_, int y_)
 {
+  mouse_world_pos = screen2world(x_, y_);
+  mouse_screen_pos = Vector2i(x_, y_);
+
   if (current_action == HIGHLIGHTING)
     {
-      // Make sure CL_Rect starts at the top left
-      if (highlighted_area.right < highlighted_area.left)
-        std::swap(highlighted_area.left, highlighted_area.right);
-      if (highlighted_area.bottom < highlighted_area.top)
-        std::swap(highlighted_area.top, highlighted_area.bottom);
-
+      highlighted_area.normalize();
       for (unsigned i = 0; i < objs.size(); i++)
         {
-          // Calculate the object's position
-          Vector2i obj_pos((int)objs[i]->get_pos().x + (state.get_width()/2 - 
-                                                        
(int)state.get_pos().x), (int)objs[i]->get_pos().y + (state.get_height()/2 - 
-                                                                               
                               (int)state.get_pos().y));
-                       
-          if (highlighted_area.is_inside(obj_pos))
+          if (highlighted_area.is_inside(Vector2i(int(objs[i]->get_pos().x),
+                                                  int(objs[i]->get_pos().y))))
             {
               selected_objs.push_back(objs[i]);
               objs[i]->select();
@@ -168,17 +171,16 @@
 }
 
 void
-EditorViewport::on_pointer_move(int x, int y)
+EditorViewport::on_pointer_move(int x_, int y_)
 {
-  mouse_at = Vector2f(float(x), float(y));
-  mouse_at_world = Vector2f(x - (state.get_width()/2 - state.get_pos().x),
-                            y - (state.get_height()/2 - state.get_pos().y));
-
+  mouse_world_pos = screen2world(x_, y_);
+  mouse_screen_pos = Vector2i(x_, y_);
+  
   switch(current_action)
     {
       case HIGHLIGHTING:
-        highlighted_area.right  = x;
-        highlighted_area.bottom = y;
+        highlighted_area.right  = mouse_world_pos.x;
+        highlighted_area.bottom = mouse_world_pos.y;
         break;
         
       case DRAGGING:
@@ -188,8 +190,8 @@
           for (unsigned i = 0; i < selected_objs.size(); i++)
             {
               Vector3f orig_pos(selected_objs[i]->get_orig_pos());
-              float x_offset = mouse_at_world.x - drag_start_pos.x;
-              float y_offset = mouse_at_world.y - drag_start_pos.y;
+              float x_offset = mouse_world_pos.x - drag_world_pos.x;
+              float y_offset = mouse_world_pos.y - drag_world_pos.y;
 
               if (snap_to)
                 {
@@ -222,17 +224,15 @@
   drawing_context->clear();
   state.push(*drawing_context);
        
-  // Now, draw all of the objects
-
   // Draw the level objects
   for (unsigned i = 0; i < objs.size(); i++)
     objs[i]->draw(*drawing_context);
 
   if (current_action == HIGHLIGHTING)
-    gc.draw_rect((float)highlighted_area.left, (float)highlighted_area.top, 
-                 (float)highlighted_area.right, 
(float)highlighted_area.bottom, 
-                 Color(255,0,255));
-
+    drawing_context->draw_rect((float)highlighted_area.left, 
(float)highlighted_area.top, 
+                               (float)highlighted_area.right, 
(float)highlighted_area.bottom, 
+                               Color(255,0,255), 1000.0f);
+  
   state.pop(*drawing_context);
   gc.draw(*drawing_context, -150);
 }
@@ -241,10 +241,7 @@
 bool
 EditorViewport::is_at(int x, int y)
 {
-  if (y >= 38) // FIXME: Could be done more flexible
-    return true;
-  else 
-    return false;
+  return drawing_context->get_rect().is_inside(Vector2i(x,y));
 }
 
 void
@@ -253,7 +250,7 @@
   UNUSED_ARG(delta);
 
   if (current_action == SCROLLING)
-    state.set_pos(state.get_pos() + (mouse_at - drag_start_pos) * delta * 
5.0f);
+    state.set_pos(state.get_pos() + Vector2f(mouse_screen_pos - 
drag_screen_pos) * delta * 5.0f);
 
   // Autoscroll if necessary
   if (autoscroll)
@@ -261,13 +258,13 @@
       const int autoscroll_border = 10;
       if (autoscroll)
         {
-          if (mouse_at.x < autoscroll_border)
+          if (mouse_screen_pos.x < autoscroll_border)
             state.set_pos(state.get_pos() - Vector2f(5, 0));
-          else if ((float)Display::get_width() - mouse_at.x < 
autoscroll_border)
+          else if ((float)Display::get_width() - mouse_screen_pos.x < 
autoscroll_border)
             state.set_pos(state.get_pos() + Vector2f(5, 0));
-          else if (mouse_at.y < autoscroll_border)
+          else if (mouse_screen_pos.y < autoscroll_border)
             state.set_pos(state.get_pos() - Vector2f(0, 5));
-          else if ((float)Display::get_height() - mouse_at.y < 
autoscroll_border)
+          else if ((float)Display::get_height() - mouse_screen_pos.y < 
autoscroll_border)
             state.set_pos(state.get_pos() + Vector2f(0, 5));
         }
     }
@@ -281,7 +278,7 @@
   for (std::vector<LevelObj*>::reverse_iterator i = objs.rbegin ();
        i != objs.rend (); ++i)
     {
-      if ((*i)->is_at (x, y))
+      if ((*i)->is_at(x, y))
         return *i;
     }
   return 0;
@@ -360,6 +357,12 @@
     }  
 }
 
+Vector2i
+EditorViewport::screen2world(int x, int y) const
+{
+  return 
Vector2i(state.screen2world(drawing_context->screen_to_world(Vector2i(x, y))));
+}
+
 } // Editor namespace
 
 /* EOF */

Modified: trunk/pingus/src/editor/editor_viewport.hpp
===================================================================
--- trunk/pingus/src/editor/editor_viewport.hpp 2007-09-06 20:41:54 UTC (rev 
3100)
+++ trunk/pingus/src/editor/editor_viewport.hpp 2007-09-06 22:28:12 UTC (rev 
3101)
@@ -53,13 +53,12 @@
   bool autoscroll;
 
   /** Where the mouse is right now - used for autoscrolling */
-  Vector2f mouse_at;
+  Vector2i mouse_world_pos;
+  Vector2i mouse_screen_pos;
 
-  /** Where the mouse is at in relation to the world/level */
-  Vector2f mouse_at_world;
-
   /** Where the mouse started dragging from */
-  Vector2f drag_start_pos;
+  Vector2i drag_world_pos;
+  Vector2i drag_screen_pos;
 
   /** All objects in the level */
   std::vector<LevelObj*> objs;
@@ -134,6 +133,7 @@
   void rotate_90_selected_objects();
   void rotate_270_selected_objects();
 
+  Vector2i screen2world(int x, int y) const;
 private:
   EditorViewport();
   EditorViewport (const EditorViewport&);

Modified: trunk/pingus/src/graphic_context_state.cpp
===================================================================
--- trunk/pingus/src/graphic_context_state.cpp  2007-09-06 20:41:54 UTC (rev 
3100)
+++ trunk/pingus/src/graphic_context_state.cpp  2007-09-06 22:28:12 UTC (rev 
3101)
@@ -222,7 +222,7 @@
 }
 
 Vector2f
-GraphicContextState::screen2world(const Vector2i& pos_)
+GraphicContextState::screen2world(const Vector2i& pos_) const
 {
   Vector2f pos(float(pos_.x - impl->rect.left),
                float(pos_.y - impl->rect.top));

Modified: trunk/pingus/src/graphic_context_state.hpp
===================================================================
--- trunk/pingus/src/graphic_context_state.hpp  2007-09-06 20:41:54 UTC (rev 
3100)
+++ trunk/pingus/src/graphic_context_state.hpp  2007-09-06 22:28:12 UTC (rev 
3101)
@@ -80,7 +80,7 @@
 
   void zoom_to (const Rectf& rect);
 
-  Vector2f screen2world(const Vector2i& pos);
+  Vector2f screen2world(const Vector2i& pos) const;
 
 private:
   boost::shared_ptr<GraphicContextStateImpl> impl;

Modified: trunk/pingus/src/ground_map.cpp
===================================================================
--- trunk/pingus/src/ground_map.cpp     2007-09-06 20:41:54 UTC (rev 3100)
+++ trunk/pingus/src/ground_map.cpp     2007-09-06 22:28:12 UTC (rev 3101)
@@ -114,7 +114,7 @@
 void
 GroundMap::draw(SceneContext& gc)
 {
-  const Rect& display = gc.color().get_clip_rect();
+  const Rect& display = gc.color().get_world_clip_rect();
 
   // FIXME: delete the next four lines and replace them with gc.get_clip_rect()
   if (draw_collision_map)

Modified: trunk/pingus/src/math/vector2i.cpp
===================================================================
--- trunk/pingus/src/math/vector2i.cpp  2007-09-06 20:41:54 UTC (rev 3100)
+++ trunk/pingus/src/math/vector2i.cpp  2007-09-06 22:28:12 UTC (rev 3101)
@@ -17,8 +17,13 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
+#include "vector2f.hpp"
 #include "vector2i.hpp"
 
+Vector2i::Vector2i(const Vector2f& v)
+  : x(int(v.x)), y(int(v.y))
+{}
+
 Vector2i
 Vector2i::operator+ (const Vector2i& add) const
 {

Modified: trunk/pingus/src/math/vector2i.hpp
===================================================================
--- trunk/pingus/src/math/vector2i.hpp  2007-09-06 20:41:54 UTC (rev 3100)
+++ trunk/pingus/src/math/vector2i.hpp  2007-09-06 22:28:12 UTC (rev 3101)
@@ -20,6 +20,8 @@
 #ifndef HEADER_VECTOR2_HPP
 #define HEADER_VECTOR2_HPP
 
+class Vector2f;
+
 /** */
 class Vector2i
 {
@@ -30,6 +32,8 @@
   Vector2i()
     : x(0), y(0) {}
 
+  explicit Vector2i(const Vector2f& v);
+
   Vector2i(int x_, int y_) 
     : x(x_), y(y_) {}
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]