Author: dfranke
Date: Tue Apr 14 10:18:56 2009
New Revision: 34890

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34890&view=rev
Log:
Accepted patch by cornmander implementing better semantics for sorting 
unit/recall list by XP. (bug #13360)

Modified:
    trunk/data/core/about.cfg
    trunk/src/menu_events.cpp
    trunk/src/widgets/menu.cpp
    trunk/src/widgets/menu.hpp

Modified: trunk/data/core/about.cfg
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/core/about.cfg?rev=34890&r1=34889&r2=34890&view=diff
==============================================================================
--- trunk/data/core/about.cfg (original)
+++ trunk/data/core/about.cfg Tue Apr 14 10:18:56 2009
@@ -795,6 +795,11 @@
         name = "Fredrik Wikstrom (salass00)"
     [/entry]
     [entry]
+       name = "Gregory Shikhman (cornmander)"
+       comment = "sort by xp fixed, bug #13360"
+       email = "cornmander_AT_cornmander.com"
+    [/entry]
+    [entry]
         name = "Hans-Joachim Gurt (HaJo)"
     [/entry]
     [entry]

Modified: trunk/src/menu_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/menu_events.cpp?rev=34890&r1=34889&r2=34890&view=diff
==============================================================================
--- trunk/src/menu_events.cpp (original)
+++ trunk/src/menu_events.cpp Tue Apr 14 10:18:56 2009
@@ -209,7 +209,7 @@
 
                gui::menu::basic_sorter sorter;
                
sorter.set_alpha_sort(0).set_alpha_sort(1).set_numeric_sort(2).set_numeric_sort(3)
-                         
.set_numeric_sort(4).set_alpha_sort(5).set_numeric_sort(6);
+                         
.set_xp_sort(4,2).set_alpha_sort(5).set_numeric_sort(6);
 
                std::vector<std::string> items;
                items.push_back(heading);
@@ -875,7 +875,7 @@
 #endif
 
                        gui::menu::basic_sorter sorter;
-                       
sorter.set_alpha_sort(1).set_alpha_sort(2).set_id_sort(3).set_numeric_sort(4).set_alpha_sort(5);
+                       
sorter.set_alpha_sort(1).set_alpha_sort(2).set_id_sort(3).set_xp_sort(4,3).set_alpha_sort(5);
 
                        options.push_back(heading.str());
                        options_to_filter.push_back(options.back());

Modified: trunk/src/widgets/menu.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/widgets/menu.cpp?rev=34890&r1=34889&r2=34890&view=diff
==============================================================================
--- trunk/src/widgets/menu.cpp (original)
+++ trunk/src/widgets/menu.cpp Tue Apr 14 10:18:56 2009
@@ -47,6 +47,13 @@
        return *this;
 }
 
+menu::basic_sorter& menu::basic_sorter::set_xp_sort(int xp_column, int 
level_column)
+{
+       xp_sort_.insert(xp_column);
+       level_col_ = level_column;
+       return *this;
+}
+
 menu::basic_sorter& menu::basic_sorter::set_id_sort(int column)
 {
        id_sort_.insert(column);
@@ -76,7 +83,7 @@
        }
 
        return alpha_sort_.count(column) == 1 || numeric_sort_.count(column) == 
1 ||
-                  pos_sort_.count(column) == 1 || id_sort_.count(column) == 1;
+                  pos_sort_.count(column) == 1 || id_sort_.count(column) == 1 
|| xp_sort_.count(column) == 1;
 }
 
 bool menu::basic_sorter::less(int column, const item& row1, const item& row2) 
const
@@ -129,6 +136,45 @@
                }
 
                return atoi(a) > atoi(b);
+       } else if(xp_sort_.count(column) == 1) {
+               const std::string& item1 = font::del_tags(row1.fields[column]);
+               const std::string& item2 = font::del_tags(row2.fields[column]);
+               const std::string& item1_lev = 
font::del_tags(row1.fields[level_col_]);
+               const std::string& item2_lev = 
font::del_tags(row2.fields[level_col_]);
+
+               const char* digits[4] = 
{item1.c_str(),item2.c_str(),item1_lev.c_str(),item2_lev.c_str()};
+               //we must move past any non-digit characters for atoi() to work 
later
+               //outer loop iterates over the strings we have to fix, inner 
loop over characters
+               for(int i = 0; i < 4; i++) {
+                       while(*digits[i] != 0 && !isdigit(*digits[i])) {
+                               ++digits[i];
+                       }
+               }
+
+               //we need to further parse xp into x and y components instead 
of x/y
+               //so we grab the position of slash character
+               char *slash1 = strchr(digits[0],'/');
+               char *slash2 = strchr(digits[1],'/');
+               
+               int xp1_y,xp2_y;
+               if(slash1)
+                       xp1_y = atoi(slash1+1);
+               else
+                       xp1_y = 0;
+               if(slash2)
+                       xp2_y = atoi(slash2+1);
+               else
+                       xp2_y = 0;
+               int xp1_x = atoi(digits[0]); //atoi stops at the first invalid 
char, which is slash character
+               int xp2_x = atoi(digits[1]);
+               
+               int level1 = atoi(digits[2]);
+               int level2 = atoi(digits[3]);
+               if(level1 > level2)
+                       return true;
+               if(level1 < level2)
+                       return false;
+               return (xp1_y - xp1_x) < (xp2_y - xp2_x);
        }
 
        const std::map<int,std::vector<int> >::const_iterator itor = 
pos_sort_.find(column);

Modified: trunk/src/widgets/menu.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/widgets/menu.hpp?rev=34890&r1=34889&r2=34890&view=diff
==============================================================================
--- trunk/src/widgets/menu.hpp (original)
+++ trunk/src/widgets/menu.hpp Tue Apr 14 10:18:56 2009
@@ -140,6 +140,7 @@
 
                basic_sorter& set_alpha_sort(int column);
                basic_sorter& set_numeric_sort(int column);
+               basic_sorter& set_xp_sort(int xp_column, int level_column);
                basic_sorter& set_id_sort(int column);
                basic_sorter& set_redirect_sort(int column, int to);
                basic_sorter& set_position_sort(int column, const 
std::vector<int>& pos);
@@ -148,9 +149,10 @@
                virtual bool less(int column, const item& row1, const item& 
row2) const;
 
        private:
-               std::set<int> alpha_sort_, numeric_sort_, id_sort_;
+               std::set<int> alpha_sort_, numeric_sort_, id_sort_, xp_sort_;
                std::map<int,int> redirect_sort_;
                std::map<int,std::vector<int> > pos_sort_;
+               int level_col_; //used by xp sort
        };
 
        menu(CVideo& video, const std::vector<std::string>& items,


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

Reply via email to