editor/palette/editor_palettes.cpp

Go to the documentation of this file.
00001 /* $Id: editor_palettes.cpp 53694 2012-03-30 12:35:40Z fendrin $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-editor"
00017 
00018 #include "editor_palettes.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gettext.hpp"
00022 #include "marked-up_text.hpp"
00023 #include "tooltips.hpp"
00024 
00025 #include "editor/action/mouse/mouse_action.hpp"
00026 
00027 #include "wml_separators.hpp"
00028 
00029 namespace editor {
00030 
00031 //TODO move to the palette
00032 template<class Item>
00033 void editor_palette<Item>::expand_palette_groups_menu(std::vector<std::string>& items)
00034 {
00035     //TODO
00036     //active_menu_ = editor::PALETTE;
00037     for (unsigned int i = 0; i < items.size(); ++i) {
00038         if (items[i] == "editor-palette-groups") {
00039             items.erase(items.begin() + i);
00040 
00041             std::vector<std::string> groups;
00042             const std::vector<item_group>& item_groups = get_groups();
00043 
00044             for (size_t mci = 0; mci < item_groups.size(); ++mci) {
00045                 std::string groupname = item_groups[mci].name;
00046                 if (groupname.empty()) {
00047                     groupname = _("(Unknown Group)");
00048                 }
00049                 std::string img = item_groups[mci].icon;
00050                 std::stringstream str;
00051                 //TODO
00052                 //std::string postfix = ".png"; //(toolkit_->active_group_index() == mci) ? "-pressed.png" : ".png";
00053                 //str << IMAGE_PREFIX << "buttons/" << img << postfix << COLUMN_SEPARATOR << groupname;
00054                 str << IMAGE_PREFIX << img << COLUMN_SEPARATOR << groupname;
00055                 groups.push_back(str.str());
00056             }
00057             items.insert(items.begin() + i, groups.begin(), groups.end());
00058             break;
00059         }
00060     }
00061 }
00062 template void editor_palette<t_translation::t_terrain>::expand_palette_groups_menu(std::vector<std::string>& items);
00063 template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector<std::string>& items);
00064 
00065 template<class Item>
00066 bool editor_palette<Item>::left_mouse_click(const int mousex, const int mousey)
00067 {
00068     int tselect = tile_selected(mousex, mousey);
00069     if(tselect >= 0 && (items_start_+tselect) < active_group().size()) {
00070         select_fg_item(active_group()[items_start_+tselect]);
00071         return true;
00072     }
00073     return false;
00074 }
00075 template bool editor_palette<t_translation::t_terrain>::left_mouse_click(const int mousex, const int mousey);
00076 template bool editor_palette<unit_type>::left_mouse_click(const int mousex, const int mousey);
00077 
00078 template<class Item>
00079 bool editor_palette<Item>::right_mouse_click(const int mousex, const int mousey)
00080 {
00081     int tselect = tile_selected(mousex, mousey);
00082     if(tselect >= 0 && (items_start_+tselect) < active_group().size()) {
00083         select_bg_item(active_group()[items_start_+tselect]);
00084         return true;
00085     }
00086     return false;
00087 }
00088 template bool editor_palette<t_translation::t_terrain>::right_mouse_click(const int mousex, const int mousey);
00089 template bool editor_palette<unit_type>::right_mouse_click(const int mousex, const int mousey);
00090 
00091 template<class Item>
00092 bool editor_palette<Item>::scroll_up()
00093 {
00094     unsigned int decrement = item_width_;
00095     if (items_start_ + nitems_ == num_items() && num_items() % item_width_ != 0) {
00096         decrement = num_items() % item_width_;
00097     }
00098     if(items_start_ >= decrement) {
00099         items_start_ -= decrement;
00100         return true;
00101     }
00102     return false;
00103 }
00104 template bool editor_palette<t_translation::t_terrain>::scroll_up();
00105 template bool editor_palette<unit_type>::scroll_up();
00106 
00107 template<class Item>
00108 bool editor_palette<Item>::scroll_down()
00109 {
00110     bool end_reached = (!(items_start_ + nitems_ + item_width_ <= num_items()));
00111 
00112     // move downwards
00113     if(!end_reached) {
00114         items_start_ += item_width_;
00115         return true;
00116     }
00117     else if (items_start_ + nitems_ + (num_items() % item_width_) <= num_items()) {
00118         items_start_ += num_items() % item_width_;
00119         return true;
00120     }
00121     return false;
00122 }
00123 template bool editor_palette<t_translation::t_terrain>::scroll_down();
00124 template bool editor_palette<unit_type>::scroll_down();
00125 
00126 template<class Item>
00127 void editor_palette<Item>::set_group(const std::string& id)
00128 {
00129     assert(!id.empty());
00130 
00131     bool found = false;
00132     foreach (const item_group& group, groups_) {
00133         if (group.id == id)
00134             found = true;
00135     }
00136     assert(found);
00137 
00138     active_group_ = id;
00139 
00140     if(active_group().empty()) {
00141         ERR_ED << "No items found in group with the id: '" << id << "'.\n";
00142     }
00143     gui_.set_palette_report(active_group_report());
00144 }
00145 template void editor_palette<t_translation::t_terrain>::set_group(const std::string& id);
00146 template void editor_palette<unit_type>::set_group(const std::string& id);
00147 //template void editor_palette<void*>::set_group(const std::string& id);
00148 
00149 template<class Item>
00150 void editor_palette<Item>::set_group(size_t index)
00151 {
00152     assert(groups_.size() > index);
00153     set_group(groups_[index].id);
00154 }
00155 template void editor_palette<t_translation::t_terrain>::set_group(size_t index);
00156 template void editor_palette<unit_type>::set_group(size_t index);
00157 //template void editor_palette<void*>::set_group(size_t index);
00158 
00159 template<class Item>
00160 size_t editor_palette<Item>::active_group_index()
00161 {
00162     assert(!active_group_.empty());
00163 
00164     for (size_t i = 0 ; i < groups_.size(); i++) {
00165         if (groups_[i].id == active_group_)
00166             return i;
00167     }
00168 
00169     return static_cast<size_t>(-1);
00170 }
00171 template size_t editor_palette<t_translation::t_terrain>::active_group_index();
00172 template size_t editor_palette<unit_type>::active_group_index();
00173 //template size_t editor_palette<void*>::active_group_index();
00174 
00175 template<class Item>
00176 const config editor_palette<Item>::active_group_report()
00177 {
00178     config cfg;
00179     config& report = cfg.add_child("element");
00180     for (size_t i = 0 ; i < groups_.size(); i++) {
00181         if (groups_[i].id == active_group_) {
00182             std::string groupname = groups_[i].name;
00183             report["image"] = groups_[i].icon;
00184             report["tooltip"] = groupname;
00185         }
00186     }
00187     return cfg;
00188 }
00189 template const config editor_palette<t_translation::t_terrain>::active_group_report();
00190 template const config editor_palette<unit_type>::active_group_report();
00191 //template const config editor_palette<void*>::active_group_report();
00192 
00193 template<class Item>
00194 void editor_palette<Item>::adjust_size(const size_specs& size)
00195 {
00196     palette_x_ = size.palette_x;
00197     palette_y_ = size.palette_y;
00198     const size_t space_for_items = size.palette_h;
00199     const unsigned items_fitting =
00200         static_cast<unsigned> (space_for_items / item_space_) *
00201         item_width_;
00202     nitems_ = std::min<int>(items_fitting, nmax_items_);
00203 }
00204 template void editor_palette<t_translation::t_terrain>::adjust_size(const size_specs& size);
00205 template void editor_palette<unit_type>::adjust_size(const size_specs& size);
00206 //template void editor_palette<void*>::adjust_size(const size_specs& size);
00207 
00208 template<class Item>
00209 void editor_palette<Item>::select_fg_item(std::string item_id)
00210 {
00211     if (selected_fg_item_ != item_id) {
00212         selected_fg_item_ = item_id;
00213     }
00214 }
00215 template void editor_palette<t_translation::t_terrain>::select_fg_item(std::string terrain_id);
00216 template void editor_palette<unit_type>::select_fg_item(std::string unit_id);
00217 //template void editor_palette<void*>::select_fg_item(std::string unit_id);
00218 
00219 template<class Item>
00220 void editor_palette<Item>::select_bg_item(std::string item_id)
00221 {
00222     if (selected_bg_item_ != item_id) {
00223         selected_bg_item_ = item_id;
00224     }
00225 }
00226 template void editor_palette<t_translation::t_terrain>::select_bg_item(std::string terrain_id);
00227 template void editor_palette<unit_type>::select_bg_item(std::string unit_id);
00228 //template void editor_palette<void*>::select_bg_item(std::string unit_id);
00229 
00230 template<class Item>
00231 void editor_palette<Item>::swap()
00232 {
00233     std::swap(selected_fg_item_, selected_bg_item_);
00234 }
00235 template void editor_palette<t_translation::t_terrain>::swap();
00236 template void editor_palette<unit_type>::swap();
00237 //template void editor_palette<void*>::swap();
00238 
00239 template<class Item>
00240 size_t editor_palette<Item>::num_items()
00241 {
00242     const size_t size = group_map_[active_group_].size();
00243     return size;
00244 }
00245 template size_t editor_palette<t_translation::t_terrain>::num_items();
00246 template size_t editor_palette<unit_type>::num_items();
00247 
00248 template<class Item>
00249 void editor_palette<Item>::draw(bool)
00250 {
00251     unsigned int y = palette_y_;
00252     unsigned int x = palette_x_;
00253     unsigned int starting = items_start_;
00254     unsigned int ending = starting + nitems_;
00255     if(ending > num_items() ){
00256         ending = num_items();
00257     }
00258     for(unsigned int counter = starting; counter < ending; counter++){
00259 
00260         const int counter_from_zero = counter - starting;
00261         SDL_Rect dstrect;
00262         dstrect.x = x + (counter_from_zero % item_width_) * item_space_;
00263         dstrect.y = y;
00264         dstrect.w = item_size_;
00265         dstrect.h = item_size_;
00266 
00267         std::stringstream tooltip_text;
00268 
00269         const std::string item_id = active_group()[counter];
00270 
00271         typedef std::map<std::string, Item> item_map_wurscht;
00272 
00273         typename item_map_wurscht::iterator item = item_map_.find(item_id);
00274 
00275         draw_item(dstrect, (*item).second, tooltip_text);
00276 
00277         surface screen = gui_.video().getSurface();
00278         Uint32 color;
00279 
00280         if (get_id((*item).second) == selected_bg_item_
00281                 && get_id((*item).second) == selected_fg_item_) {
00282             color = SDL_MapRGB(screen->format,0xFF,0x00,0xFF);
00283         }
00284         else if (get_id((*item).second) == selected_bg_item_) {
00285             color = SDL_MapRGB(screen->format,0x00,0x00,0xFF);
00286         }
00287         else if (get_id((*item).second) == selected_fg_item_) {
00288             color = SDL_MapRGB(screen->format,0xFF,0x00,0x00);
00289         }
00290         else {
00291             color = SDL_MapRGB(screen->format,0x00,0x00,0x00);
00292         }
00293 
00294         draw_rectangle(dstrect.x, dstrect.y, dstrect.w, dstrect.h, color, screen);
00295         /* TODO The call above overdraws the border of the terrain image.
00296                The following call is doing better but causing other drawing glitches.
00297                draw_rectangle(dstrect.x -1, dstrect.y -1, image->w +2, image->h +2, color, screen);
00298          */
00299 
00300         bool is_core = non_core_items_.find(get_id((*item).second)) == non_core_items_.end();
00301         if (!is_core) {
00302             tooltip_text << " "
00303                     << font::span_color(font::BAD_COLOR)
00304             << _("(non-core)") << "\n"
00305             << _("Will not work in game without extra care.")
00306             << "</span>";
00307         }
00308         tooltips::add_tooltip(dstrect, tooltip_text.str());
00309         if (counter_from_zero % item_width_ == item_width_ - 1)
00310             y += item_space_;
00311     }
00312 }
00313 template void editor_palette<t_translation::t_terrain>::draw(bool);
00314 template void editor_palette<unit_type>::draw(bool);
00315 //template void editor_palette<void*>::draw(bool);
00316 
00317 template<class Item>
00318 int editor_palette<Item>::tile_selected(const int x, const int y) const
00319 {
00320     for(unsigned int i = 0; i != nitems_; i++) {
00321         const int px = size_specs_.palette_x + (i % item_width_) * item_space_;
00322         const int py = palette_y_ + (i / item_width_) * item_space_;
00323         const int pw = item_space_;
00324         const int ph = item_space_;
00325 
00326         if(x > px && x < px + pw && y > py && y < py + ph) {
00327             return i;
00328         }
00329     }
00330     return -1;
00331 }
00332 
00333 
00334 } // end namespace editor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:35 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs