Author: ilor
Date: Tue Jul 29 11:49:17 2008
New Revision: 28258

URL: http://svn.gna.org/viewcvs/wesnoth?rev=28258&view=rev
Log:
port map manipulation code from old editor -- resize and flip (unbound yet)

Modified:
    trunk/src/editor2/editor_map.cpp
    trunk/src/editor2/editor_map.hpp

Modified: trunk/src/editor2/editor_map.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/editor2/editor_map.cpp?rev=28258&r1=28257&r2=28258&view=diff
==============================================================================
--- trunk/src/editor2/editor_map.cpp (original)
+++ trunk/src/editor2/editor_map.cpp Tue Jul 29 11:49:17 2008
@@ -231,5 +231,211 @@
        trim_stack(to);
 }
 
+void editor_map::resize(int width, int height, int x_offset, int y_offset,
+       bool do_expand, t_translation::t_terrain filler)
+{
+       int old_w = w();
+       int old_h = h();
+       if (old_w == width && old_h == height && x_offset == 0 && y_offset == 
0) {
+               return;
+       }
+       if (do_expand) {
+               filler = t_translation::NONE_TERRAIN;
+       }
+
+       // Determine the amount of resizing is required
+       const int left_resize = -x_offset;
+       const int right_resize = (width - old_w) + x_offset;
+       const int top_resize = -y_offset;
+       const int bottom_resize = (height - old_h) + y_offset;
+
+       if(right_resize > 0) {
+               expand_right(right_resize, filler);
+       } else if(right_resize < 0) {
+               shrink_right(-right_resize);
+       }
+       if(bottom_resize > 0) {
+               expand_bottom(bottom_resize, filler);
+       } else if(bottom_resize < 0) {
+               shrink_bottom(-bottom_resize);
+       }
+       if(left_resize > 0) {
+               expand_left(left_resize, filler);
+       } else if(left_resize < 0) {
+               shrink_left(-left_resize);
+       }
+       if(top_resize > 0) {
+               expand_top(top_resize, filler);
+       } else if(top_resize < 0) {
+               shrink_top(-top_resize);
+       }
+
+       // fix the starting positions
+       if(x_offset || y_offset) {
+               for(size_t i = 0; i < MAX_PLAYERS+1; ++i) {
+                       if(startingPositions_[i] != gamemap::location()) {
+                               startingPositions_[i].x -= x_offset;
+                               startingPositions_[i].y -= y_offset;
+                       }
+               }
+       }
+}
+
+void editor_map::flip_x()
+{
+       // Due to the hexes we need some mirror tricks when mirroring over the
+       // X axis. We resize the map and fill it. The odd columns will be 
extended
+       // with the data in row 0 the even columns are extended with the data in
+       // the last row
+       const size_t middle = (tiles_[0].size() / 2); // the middle if reached 
we flipped all
+       const size_t end = tiles_[0].size() - 1; // the last row _before_ 
resizing
+       for(size_t x = 0; x < tiles_.size(); ++x) {
+               if(x % 2) {
+                       // odd lines
+                       tiles_[x].resize(tiles_[x].size() + 1, tiles_[x][0]);
+                       for(size_t y1 = 0, y2 = end; y1 < middle; ++y1, --y2) {
+                               swap_starting_position(x, y1, x, y2);
+                               std::swap(tiles_[x][y1], tiles_[x][y2]);
+                       }
+               } else {
+                       // even lines
+                       tiles_[x].resize(tiles_[x].size() + 1, tiles_[x][end]);
+                       for(size_t y1 = 0, y2 = end + 1; y1 < middle; ++y1, 
--y2) {
+                               swap_starting_position(x, y1, x, y2);
+                               std::swap(tiles_[x][y1], tiles_[x][y2]);
+                       }
+
+               }
+       }
+}
+
+void editor_map::flip_y()
+{
+       // Flipping on the Y axis requires no resize,
+       // so the code is much simpler.
+       const size_t middle = (tiles_.size() / 2);
+       const size_t end = tiles_.size() - 1;
+       for(size_t y = 0; y < tiles_[0].size(); ++y) {
+               for(size_t x1 = 0, x2 = end; x1 < middle; ++x1, --x2) {
+                       swap_starting_position(x1, y, x2, y);
+                       std::swap(tiles_[x1][y], tiles_[x2][y]);
+               }
+       }
+}
+
+void editor_map::set_starting_position(int pos, const location& loc)
+{
+       startingPositions_[pos] = loc;
+}
+
+void editor_map::swap_starting_position(int x1, int y1, int x2, int y2)
+{
+       int pos1 = is_starting_position(location(x1, y1));
+       int pos2 = is_starting_position(location(x2, y2));
+       if(pos1 != -1) {
+               set_starting_position(pos1 + 1, location(x2, y2));
+       }
+       if(pos2 != -1) {
+               set_starting_position(pos2 + 1, location(x1, y1));
+       }
+}
+
+t_translation::t_list editor_map::clone_column(int x, t_translation::t_terrain 
filler)
+{
+       int h = tiles_[1].size();
+       t_translation::t_list column(h);
+       for (int y = 0; y < h; ++y) {
+               column[y] =
+                       filler != t_translation::NONE_TERRAIN ?
+                       filler :
+                       tiles_[x][y];
+               assert(column[y] != t_translation::NONE_TERRAIN);
+       }
+       return column;
+}
+
+void editor_map::expand_right(int count, t_translation::t_terrain filler)
+{
+       int w = tiles_.size();
+       for (int x = 0; x < count; ++x) {
+               tiles_.push_back(clone_column(w, filler));
+       }
+}
+
+void editor_map::expand_left(int count, t_translation::t_terrain filler)
+{
+       for (int x = 0; x < count; ++x) {
+               tiles_.insert(tiles_.begin(), 1, clone_column(0, filler));
+               clear_border_cache();
+       }
+}
+
+void editor_map::expand_top(int count, t_translation::t_terrain filler)
+{
+       for (int y = 0; y < count; ++y) {
+               for (int x = 0; x < tiles_.size(); ++x) {
+                       t_translation::t_terrain terrain =
+                               filler != t_translation::NONE_TERRAIN ?
+                               filler :
+                               tiles_[x][0];
+                       assert(terrain != t_translation::NONE_TERRAIN);
+                       tiles_[x].insert(tiles_[x].begin(), 1, terrain);
+                       clear_border_cache();
+               }
+       }
+}
+
+void editor_map::expand_bottom(int count, t_translation::t_terrain filler)
+{
+       int h = tiles_[1].size();
+       for (int y = 0; y < count; ++y) {
+               for (int x = 0; x < tiles_.size(); ++x) {
+                       t_translation::t_terrain terrain =
+                               filler != t_translation::NONE_TERRAIN ?
+                               filler :
+                               tiles_[x][h];
+                       assert(terrain != t_translation::NONE_TERRAIN);
+                       tiles_[x].push_back(terrain);
+               }
+       }
+}
+
+void editor_map::shrink_right(int count)
+{
+       if(count < 0 || count > tiles_.size()) {
+               throw editor_map_operation_exception();
+       }
+       tiles_.resize(tiles_.size() - count);
+}
+
+void editor_map::shrink_left(int count)
+{
+       if(count < 0 || count > tiles_.size()) {
+               throw editor_map_operation_exception();
+       }
+       tiles_.erase(tiles_.begin(), tiles_.begin() + count);
+}
+
+void editor_map::shrink_top(int count)
+{
+       if(count < 0 || count > tiles_[0].size()) {
+               throw editor_map_operation_exception();
+       }
+       for (size_t x = 0; x < tiles_.size(); ++x) {
+               tiles_[x].erase(tiles_[x].begin(), tiles_[x].begin() + count);
+       }
+}
+
+void editor_map::shrink_bottom(int count)
+{
+       if(count < 0 || count > tiles_[0].size()) {
+               throw editor_map_operation_exception();
+       }
+       for (size_t x = 0; x < tiles_.size(); ++x) {
+               tiles_[x].erase(tiles_[x].end() - count, tiles_[x].end());
+       }
+}
+
+
 
 } //end namespace editor2

Modified: trunk/src/editor2/editor_map.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/editor2/editor_map.hpp?rev=28258&r1=28257&r2=28258&view=diff
==============================================================================
--- trunk/src/editor2/editor_map.hpp (original)
+++ trunk/src/editor2/editor_map.hpp Tue Jul 29 11:49:17 2008
@@ -23,10 +23,13 @@
 
 namespace editor2 {
 
+struct editor_map_operation_exception : public editor_exception
+{
+};
+
 /**
  * This class adds extra editor-specific functionality to a normal gamemap.
- */
-       
+ */    
 class editor_map : public gamemap 
 {
 public:
@@ -116,7 +119,25 @@
        /** Re-does a previousle undid action, and puts it back in the undo 
stack. */
        void redo();
        
+       void resize(int width, int height, int x_offset, int y_offset,
+               bool do_expand, t_translation::t_terrain filler);
+
+       void flip_x();
+       void flip_y();
+
 protected:
+       void set_starting_position(int pos, const location& loc);
+       void swap_starting_position(int x1, int y1, int x2, int y2);
+       t_translation::t_list clone_column(int x, t_translation::t_terrain 
filler);
+       void expand_right(int count, t_translation::t_terrain filler);
+       void expand_left(int count, t_translation::t_terrain filler);
+       void expand_top(int count, t_translation::t_terrain filler);
+       void expand_bottom(int count, t_translation::t_terrain filler);
+       void shrink_right(int count);
+       void shrink_left(int count);
+       void shrink_top(int count);
+       void shrink_bottom(int count);
+
        /**
         * Container type used to store actions in the undo and redo stacks
         */


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to