From 07074bebc3ae4aaaf22b484c2526bc32016b4861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=BCller?= Date: Fri, 9 Mar 2012 17:56:31 +0000 Subject: [PATCH] Subfolder with new palette class hierarchy. --- src/editor/palette/common_palette.hpp | 59 ++++ src/editor/palette/editor_palettes.cpp | 366 ++++++++++++++++++++++++ src/editor/palette/editor_palettes.hpp | 170 +++++++++++ src/editor/palette/terrain_palettes.cpp | 199 +++++++++++++ src/editor/palette/terrain_palettes.hpp | 57 ++++ src/editor/palette/unit_palette.cpp | 261 +++++++++++++++++ src/editor/palette/unit_palette.hpp | 72 +++++ 7 files changed, 1184 insertions(+) create mode 100644 src/editor/palette/common_palette.hpp create mode 100644 src/editor/palette/editor_palettes.cpp create mode 100644 src/editor/palette/editor_palettes.hpp create mode 100644 src/editor/palette/terrain_palettes.cpp create mode 100644 src/editor/palette/terrain_palettes.hpp create mode 100644 src/editor/palette/unit_palette.cpp create mode 100644 src/editor/palette/unit_palette.hpp diff --git a/src/editor/palette/common_palette.hpp b/src/editor/palette/common_palette.hpp new file mode 100644 index 00000000000..b03642ca20d --- /dev/null +++ b/src/editor/palette/common_palette.hpp @@ -0,0 +1,59 @@ +/* + Copyright (C) 2003 - 2012 by David White + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +#ifndef COMMON_PALETTES_H_INCLUDED +#define COMMON_PALETTES_H_INCLUDED + +namespace editor { + +/** + * Stores the info about the groups in a nice format. + */ +struct item_group +{ + item_group(const config& cfg): + id(cfg["id"]), name(cfg["name"].t_str()), + icon(cfg["icon"]), core(cfg["core"].to_bool()) {}; + + std::string id; + t_string name; + std::string icon; + bool core; +}; + +class common_palette { + +public: + virtual void set_group(size_t index) = 0; + + virtual size_t active_group_index() = 0; + + virtual const std::vector& get_groups() = 0; + + /** Scroll the editor-palette up one step if possible. */ + virtual void scroll_up() = 0; + + /** Scroll the editor-palette down one step if possible. */ + virtual void scroll_down() = 0; + + virtual void swap() = 0; + + virtual void adjust_size() = 0; + + virtual void draw(bool) = 0; +}; + +} + +#endif diff --git a/src/editor/palette/editor_palettes.cpp b/src/editor/palette/editor_palettes.cpp new file mode 100644 index 00000000000..9a540adc2b1 --- /dev/null +++ b/src/editor/palette/editor_palettes.cpp @@ -0,0 +1,366 @@ +/* $Id$ */ +/* + Copyright (C) 2003 - 2012 by David White + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +#define GETTEXT_DOMAIN "wesnoth-editor" + +#include "editor_palettes.hpp" + +#include "../../gettext.hpp" +#include "../../tooltips.hpp" +#include "../../marked-up_text.hpp" + +namespace editor { + +template +void editor_palette::set_group(const std::string& id) +{ + active_group_ = id; + + if(active_group().empty()) { + ERR_ED << "No items found.\n"; + } + gui_.set_terrain_report(active_group_report()); + scroll_top(); +} +template void editor_palette::set_group(const std::string& id); + +template +void editor_palette::set_group(size_t index) +{ + set_group(groups_[index].id); +} +template void editor_palette::set_group(size_t index); + +template size_t editor_palette::active_group_index() +{ + for (size_t i = 0 ; i < groups_.size(); i++) { + if (groups_[i].id == active_group_) + return i; + } + return 0; +} +template size_t editor_palette::active_group_index(); + + +template +const config editor_palette::active_group_report() +{ + config cfg; + config& report = cfg.add_child("element"); + for (size_t i = 0 ; i < groups_.size(); i++) { + if (groups_[i].id == active_group_) { + std::string groupname = groups_[i].name; + report["image"] = "buttons/" + groups_[i].icon + ".png"; + report["tooltip"] = groupname; + } + } + return cfg; +} +template const config editor_palette::active_group_report(); + + +template +void editor_palette::adjust_size() +{ + scroll_top(); + + SDL_Rect rect = create_rect(size_specs_.palette_x + , size_specs_.palette_y + , size_specs_.palette_w + , size_specs_.palette_h); + + set_location(rect); + palette_start_ = size_specs_.palette_y; + const size_t space_for_items = size_specs_.palette_h; + rect.y = items_start_; + rect.h = space_for_items; + bg_register(rect); + const unsigned items_fitting = + static_cast (space_for_items / size_specs_.terrain_space) * + size_specs_.terrain_width; + nitems_ = std::min(items_fitting, nmax_items_); + + set_dirty(); +} +template void editor_palette::adjust_size(); + +template +void editor_palette::scroll_down() +{ + SDL_Rect rect = create_rect(size_specs_.palette_x + , size_specs_.palette_y + , size_specs_.palette_w + , size_specs_.palette_h); + + if(items_start_ + nitems_ + size_specs_.terrain_width <= num_items()) { + items_start_ += size_specs_.terrain_width; + bg_restore(rect); + set_dirty(); + } + else if (items_start_ + nitems_ + (num_items() % size_specs_.terrain_width) <= num_items()) { + items_start_ += num_items() % size_specs_.terrain_width; + bg_restore(rect); + set_dirty(); + } +} +template void editor_palette::scroll_down(); + +template +void editor_palette::scroll_up() +{ + SDL_Rect rect = create_rect(size_specs_.palette_x + , size_specs_.palette_y + , size_specs_.palette_w + , size_specs_.palette_h); + + unsigned int decrement = size_specs_.terrain_width; + if (items_start_ + nitems_ == num_items() && num_items() % size_specs_.terrain_width != 0) { + decrement = num_items() % size_specs_.terrain_width; + } + if(items_start_ >= decrement) { + bg_restore(rect); + set_dirty(); + items_start_ -= decrement; + } +} +template void editor_palette::scroll_up(); + +template +void editor_palette::scroll_top() +{ + SDL_Rect rect = create_rect(size_specs_.palette_x + , size_specs_.palette_y + , size_specs_.palette_w + , size_specs_.palette_h); + + items_start_ = 0; + bg_restore(rect); + set_dirty(); +} +template void editor_palette::scroll_top(); + +template +void editor_palette::scroll_bottom() +{ + unsigned int old_start = num_items(); + while (old_start != items_start_) { + old_start = items_start_; + scroll_down(); + } +} +template void editor_palette::scroll_bottom(); + +template +void editor_palette::select_fg_item(Item item) +{ + if (selected_fg_item_ != item) { + set_dirty(); + selected_fg_item_ = item; + update_report(); + } +} +template void editor_palette::select_fg_item(t_translation::t_terrain terrain); + +template +void editor_palette::select_bg_item(Item item) +{ + if (selected_bg_item_ != item) { + set_dirty(); + selected_bg_item_ = item; + update_report(); + } +} +template void editor_palette::select_bg_item(t_translation::t_terrain terrain); + +template +void editor_palette::swap() +{ + std::swap(selected_fg_item_, selected_bg_item_); + set_dirty(); + update_report(); +} +template void editor_palette::swap(); + +template +void editor_palette::left_mouse_click(const int mousex, const int mousey) { + int tselect = tile_selected(mousex, mousey); + if(tselect >= 0 && (items_start_+tselect) < active_group().size()) { + select_fg_item(item_map_[active_group()[items_start_+tselect]]); + gui_.invalidate_game_status(); + } +} +template void editor_palette::left_mouse_click(const int mousex, const int mousey); + +template +void editor_palette::right_mouse_click(const int mousex, const int mousey) { + int tselect = tile_selected(mousex, mousey); + if(tselect >= 0 && (items_start_+tselect) < active_group().size()) { + select_bg_item(item_map_[active_group()[items_start_+tselect]]); + gui_.invalidate_game_status(); + } +} +template void editor_palette::right_mouse_click(const int mousex, const int mousey); + +template +size_t editor_palette::num_items() { + size_t size = group_map_[active_group_].size(); + return size; +} +template size_t editor_palette::num_items(); + +template +void editor_palette::handle_event(const SDL_Event& event) { + if (event.type == SDL_MOUSEMOTION) { + // If the mouse is inside the palette, give it focus. + if (point_in_rect(event.button.x, event.button.y, location())) { + if (!focus(&event)) { + set_focus(true); + } + } + // If the mouse is outside, remove focus. + else { + if (focus(&event)) { + set_focus(false); + } + } + } + if (!focus(&event)) { + return; + } + int mousex, mousey; + SDL_GetMouseState(&mousex,&mousey); + const SDL_MouseButtonEvent mouse_button_event = event.button; + if (mouse_button_event.type == SDL_MOUSEBUTTONDOWN) { + if (mouse_button_event.button == SDL_BUTTON_LEFT) { + left_mouse_click(mousex, mousey); + } + if (mouse_button_event.button == SDL_BUTTON_RIGHT) { + right_mouse_click(mousex, mousey); + } + if (mouse_button_event.button == SDL_BUTTON_WHEELUP) { + scroll_up(); + } + if (mouse_button_event.button == SDL_BUTTON_WHEELDOWN) { + scroll_down(); + } + if (mouse_button_event.button == SDL_BUTTON_WHEELLEFT) { + set_group( (active_group_index() -1) % (groups_.size() -1)); + gui_.set_terrain_report(active_group_report()); + } + if (mouse_button_event.button == SDL_BUTTON_WHEELRIGHT) { + set_group( (active_group_index() +1) % (groups_.size() -1)); + gui_.set_terrain_report(active_group_report()); + } + } + if (mouse_button_event.type == SDL_MOUSEBUTTONUP) { + if (mouse_button_event.button == SDL_BUTTON_LEFT) { + } + } +} +template void editor_palette::handle_event(const SDL_Event& event); + +template +void editor_palette::draw(bool force) { + if (!dirty() && !force) { + return; + } + unsigned int starting = items_start_; + unsigned int ending = starting + nitems_; + + if(ending > num_items() ){ + ending = num_items(); + } + const SDL_Rect &loc = location(); + int y = palette_start_; + SDL_Rect palrect; + palrect.x = loc.x; + palrect.y = palette_start_; + palrect.w = size_specs_.palette_w; + palrect.h = size_specs_.palette_h; + tooltips::clear_tooltips(palrect); + for(unsigned int counter = starting; counter < ending; counter++){ + + const int counter_from_zero = counter - starting; + SDL_Rect dstrect; + dstrect.x = loc.x + (counter_from_zero % size_specs_.terrain_width) * size_specs_.terrain_space; + dstrect.y = y; + dstrect.w = size_specs_.terrain_size; + dstrect.h = size_specs_.terrain_size; + + // Reset the tile background + bg_restore(dstrect); + + std::stringstream tooltip_text; + + const std::string item_id = active_group()[counter]; + const Item& item = item_map_[item_id]; + + draw_item(dstrect, item, tooltip_text); + + surface screen = gui_.video().getSurface(); + Uint32 color; + if (item == selected_bg_item() && item == selected_fg_item()) { + color = SDL_MapRGB(screen->format,0xFF,0x00,0xFF); + } + else if (item == selected_bg_item()) { + color = SDL_MapRGB(screen->format,0x00,0x00,0xFF); + } + else if (item == selected_fg_item()) { + color = SDL_MapRGB(screen->format,0xFF,0x00,0x00); + } + else { + color = SDL_MapRGB(screen->format,0x00,0x00,0x00); + } + + draw_rectangle(dstrect.x, dstrect.y, dstrect.w, dstrect.h, color, screen); + /* TODO The call above overdraws the border of the terrain image. + The following call is doing better but causing other drawing glitches. + draw_rectangle(dstrect.x -1, dstrect.y -1, image->w +2, image->h +2, color, screen); + */ + + bool is_core = non_core_items_.find(get_id(item)) == non_core_items_.end(); + if (!is_core) { + tooltip_text << " " + << font::span_color(font::BAD_COLOR) + << _("(non-core)") << "\n" + << _("Will not work in game without extra care.") + << ""; + } + tooltips::add_tooltip(dstrect, tooltip_text.str()); + if (counter_from_zero % size_specs_.terrain_width == size_specs_.terrain_width - 1) + y += size_specs_.terrain_space; + } + update_rect(loc); + set_dirty(false); +} +template void editor_palette::draw(bool force); + +template +int editor_palette::tile_selected(const int x, const int y) const { + for(unsigned int i = 0; i != nitems_; i++) { + const int px = size_specs_.palette_x + (i % size_specs_.terrain_width) * size_specs_.terrain_space; + const int py = palette_start_ + (i / size_specs_.terrain_width) * size_specs_.terrain_space; + const int pw = size_specs_.terrain_space; + const int ph = size_specs_.terrain_space; + + if(x > px && x < px + pw && y > py && y < py + ph) { + return i; + } + } + return -1; +} + + +} // end namespace editor diff --git a/src/editor/palette/editor_palettes.hpp b/src/editor/palette/editor_palettes.hpp new file mode 100644 index 00000000000..b6448bb23f0 --- /dev/null +++ b/src/editor/palette/editor_palettes.hpp @@ -0,0 +1,170 @@ +/* $Id$ */ +/* + Copyright (C) 2003 - 2012 by David White + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +#ifndef EDITOR_PALETTES_H_INCLUDED +#define EDITOR_PALETTES_H_INCLUDED + +#include "../editor_display.hpp" +#include "../editor_layout.hpp" +#include "common_palette.hpp" + +namespace editor { + +template +class editor_palette: public gui::widget, public common_palette{ +public: + editor_palette(editor_display &gui, const size_specs &sizes, const config& /*cfg*/, + Item& fore, Item& back) + : gui::widget(gui.video()) + , size_specs_(sizes) + , gui_(gui) + , palette_start_(0) + , item_map_() + , selected_fg_item_(fore) + , selected_bg_item_(back) +{ +} + virtual void set_group(size_t index); + + std::vector& get_groups() { return groups_; }; + + virtual size_t active_group_index(); + + /** Scroll the editor-palette up one step if possible. */ + virtual void scroll_up(); + + /** Scroll the editor-palette down one step if possible. */ + virtual void scroll_down(); + + /** + * Draw the palette. + * + * If force is true everything will be redrawn, + * even though it is not invalidated. + */ + virtual void draw(bool force=false); + virtual void draw() { draw(false); }; + virtual void handle_event(const SDL_Event& event); + + /** + * Update the size of this widget. + * + * Use if the size_specs have changed. + */ + virtual void adjust_size(); + +private: + + virtual void draw_item(SDL_Rect& dstrect, const Item& item, std::stringstream& tooltip) = 0; + + virtual const std::string& get_id(const Item& item) = 0; + + /** Setup the internal data structure. */ + virtual void setup(const config& cfg) = 0; + + + /** Scroll the editor-palette to the top. */ + void scroll_top(); + + /** Scroll the editor-palette to the bottom. */ + void scroll_bottom(); + + + + virtual const std::string& active_group_id() {return active_group_;}; + + + virtual const config active_group_report(); + +protected: + /** + * Sets a group active id + * + * This can result in no visible + * selected items. + */ + virtual void set_group(const std::string& id); + const std::vector& active_group() { return group_map_[active_group_]; }; + + /** Return the currently selected foreground item. */ + Item selected_fg_item() const { return selected_fg_item_; }; + + /** + * The editor_groups as defined in editor-groups.cfg. + * + * Note the user must make sure the id's here are the same as the + * editor_group in terrain.cfg. + */ + std::vector groups_; + + + +private: + /** Return the currently selected background item. */ + Item selected_bg_item() const { return selected_bg_item_; }; + + virtual void swap(); + + /** Select a foreground item. */ + virtual void select_fg_item(Item); + virtual void select_bg_item(Item); + + /** Return the number of terrains in the palette. */ + size_t num_items(); + + void draw_old(bool); + + int item_width_; + int item_size_; + + /** + * To be called when a mouse click occurs. + * + * Check if the coordinates is a terrain that may be chosen, + * and select the terrain if that is the case. + */ + void left_mouse_click(const int mousex, const int mousey); + void right_mouse_click(const int mousex, const int mousey); + + /** Return the number of the tile that is at coordinates (x, y) in the panel. */ + int tile_selected(const int x, const int y) const; + + /** Update the report with the currently selected items. */ + virtual void update_report() = 0; + +protected: + const size_specs &size_specs_; + editor_display &gui_; + +private: + unsigned int palette_start_; + +protected: + std::map > group_map_; + + std::map item_map_; + size_t nitems_, nmax_items_, items_start_; + std::set non_core_items_; + +private: + std::string active_group_; + Item& selected_fg_item_; + Item& selected_bg_item_; +}; + + +} //end namespace editor +#endif // EDITOR_PALETTES_H_INCLUDED + diff --git a/src/editor/palette/terrain_palettes.cpp b/src/editor/palette/terrain_palettes.cpp new file mode 100644 index 00000000000..68417b4cd09 --- /dev/null +++ b/src/editor/palette/terrain_palettes.cpp @@ -0,0 +1,199 @@ +/* $Id: editor_palettes.cpp -1 $ */ +/* + Copyright (C) 2003 - 2012 by David White + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +/** + * Manage the terrain-palette in the editor. + */ + +#define GETTEXT_DOMAIN "wesnoth-editor" + +#include "terrain_palettes.hpp" + +#include "../../foreach.hpp" +#include "../../gettext.hpp" + +namespace { + static std::string selected_terrain; +} + +namespace editor { + +std::string get_selected_terrain() +{ + return selected_terrain; +} + +static bool is_valid_terrain(t_translation::t_terrain c) { + return !(c == t_translation::VOID_TERRAIN || c == t_translation::FOGGED); +} + +void terrain_palette::update_report() +{ + std::ostringstream msg; + msg << _("FG: ") << map().get_terrain_editor_string(selected_fg_item()) + << '\n' << _("BG: ") << map().get_terrain_editor_string(selected_fg_item()); + selected_terrain = msg.str(); +} + +void terrain_palette::setup(const config& cfg) +{ + // Get the available terrains temporary in items + t_translation::t_list items = map().get_terrain_list(); + + //move "invalid" items to the end + std::stable_partition(items.begin(), items.end(), is_valid_terrain); + + // Get the available groups and add them to the structure + std::set group_names; + foreach (const config &g, cfg.child_range("editor_group")) + { + if (group_names.find(g["id"]) == group_names.end()) { + groups_.push_back(item_group(g)); + group_names.insert(groups_.back().id); + } + } + + std::map id_to_group; + foreach (item_group& group, groups_) { + id_to_group.insert(std::make_pair(group.id, &group)); + } + + // add the groups for all terrains to the map + foreach (const t_translation::t_terrain& t, items) { + + const terrain_type& t_info = map().get_terrain_info(t); + DBG_ED << "Palette: processing terrain " << t_info.name() + << "(editor name: '" << t_info.editor_name() << "') " + << "(" << t_info.number() << ")" + << ": " << t_info.editor_group() << "\n"; + + // don't display terrains that were automatically created from base+overlay + if (t_info.is_combined()) continue; + // nor display terrains that have hide_in_editor=true + if (t_info.hide_in_editor()) continue; + + // add the terrain to the requested groups + const std::vector& keys = utils::split(t_info.editor_group()); + bool core = false; + + item_map_[get_id(t)] = t; + + foreach (const std::string& k, keys) { + group_map_[k].push_back(get_id(t)); + nmax_items_ = std::max(nmax_items_, group_map_[k].size()); + std::map::iterator i = id_to_group.find(k); + if (i != id_to_group.end()) { + if (i->second->core) { + core = true; + } + } + } + + // A terrain is considered core iff it appears in at least + // one core terrain group + if (core) { + // Add the terrain to the default group + group_map_["all"].push_back(get_id(t)); + nmax_items_ = std::max(nmax_items_, group_map_["all"].size()); + } else { + non_core_items_.insert(get_id(t)); + } + + } + + // Set the default group + set_group("all"); + + if(active_group().empty()) { + ERR_ED << "No items found.\n"; + } + + update_report(); +} + +void terrain_palette::draw_item(SDL_Rect& dstrect, const t_translation::t_terrain& terrain, std::stringstream& tooltip_text) { + + const t_translation::t_terrain base_terrain = map().get_terrain_info(terrain).default_base(); + + surface screen = gui_.video().getSurface(); + + //Draw default base for overlay terrains + if(base_terrain != t_translation::NONE_TERRAIN) { + const std::string base_filename = "terrain/" + map().get_terrain_info(base_terrain).editor_image() + ".png"; + surface base_image(image::get_image(base_filename)); + + if(base_image == NULL) { + tooltip_text << "BASE IMAGE NOT FOUND\n"; + ERR_ED << "image for terrain : '" << base_filename << "' not found\n"; + base_image = image::get_image(game_config::images::missing); + if (base_image == NULL) { + ERR_ED << "Placeholder image not found\n"; + return; + } + } + + if(static_cast(base_image->w) != size_specs_.terrain_size || + static_cast(base_image->h) != size_specs_.terrain_size) { + + base_image.assign(scale_surface(base_image, + size_specs_.terrain_size, size_specs_.terrain_size)); + } + + sdl_blit(base_image, NULL, screen, &dstrect); + } + + const std::string filename = "terrain/" + map().get_terrain_info(terrain).editor_image() + ".png"; + surface image(image::get_image(filename)); + if(image == NULL) { + tooltip_text << "IMAGE NOT FOUND\n"; + ERR_ED << "image for terrain: '" << filename << "' not found\n"; + image = image::get_image(game_config::images::missing); + if (image == NULL) { + ERR_ED << "Placeholder image not found\n"; + return; + } + } + + if(static_cast(image->w) != size_specs_.terrain_size || + static_cast(image->h) != size_specs_.terrain_size) { + + image.assign(scale_surface(image, + size_specs_.terrain_size, size_specs_.terrain_size)); + } + + sdl_blit(image, NULL, screen, &dstrect); + + tooltip_text << map().get_terrain_editor_string(terrain); + if (gui_.get_draw_terrain_codes()) { + tooltip_text << " " + utils::unicode_em_dash + " " << terrain; + } +} + +terrain_palette::terrain_palette(editor_display &gui, const size_specs &sizes, + const config& cfg, + t_translation::t_terrain& fore, + t_translation::t_terrain& back) + : editor_palette(gui, sizes, cfg, fore, back) +{ +} + +const std::string& terrain_palette::get_id(const t_translation::t_terrain& terrain) +{ + const terrain_type& t_info = map().get_terrain_info(terrain); + return t_info.id(); +} + + +} diff --git a/src/editor/palette/terrain_palettes.hpp b/src/editor/palette/terrain_palettes.hpp new file mode 100644 index 00000000000..6fc34e6c4e0 --- /dev/null +++ b/src/editor/palette/terrain_palettes.hpp @@ -0,0 +1,57 @@ + +/* + Copyright (C) 2003 - 2012 by David White + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +/** + * Manage the terrain-palette in the editor. + * Note: this is a near-straight rip from the old editor. +*/ + +#ifndef TERRAIN_PALETTES_H_INCLUDED +#define TERRAIN_PALETTES_H_INCLUDED + +#include "editor_palettes.hpp" + +namespace editor { + +std::string get_selected_terrain(); + + +/** Palette where the terrain to be drawn can be selected. */ +class terrain_palette : public editor_palette { +public: + terrain_palette(editor_display &gui, const size_specs &sizes, + const config& cfg, + t_translation::t_terrain& fore, + t_translation::t_terrain& back); + + const gamemap& map() const { return gui_.get_map(); } + + virtual void setup(const config& cfg); + +private: + + virtual const std::string& get_id(const t_translation::t_terrain& terrain); + + virtual void draw_item(SDL_Rect& dstrect, const t_translation::t_terrain& terrain, std::stringstream& tooltip_text); + + virtual void update_report(); + + /** Return a string representing the terrain and the underlying ones. */ + std::string get_terrain_string(const t_translation::t_terrain); + +}; + +} +#endif diff --git a/src/editor/palette/unit_palette.cpp b/src/editor/palette/unit_palette.cpp new file mode 100644 index 00000000000..97148d187a9 --- /dev/null +++ b/src/editor/palette/unit_palette.cpp @@ -0,0 +1,261 @@ +//unit_palette::unit_palette(editor_display &gui, const size_specs &sizes, +// const config& cfg, unit_type& fore, unit_type& back) +// : editor_palette(gui, sizes, cfg, fore, back) +//{ +// // // Get the available unit groups and add them to the structure +// // std::set unit_group_names; +// // foreach (const unit_type_data::unit_type_map::value_type &i, unit_types.types()) +// // { +// // if (unit_group_names.find(i.second.race()) == unit_group_names.end()) { +// // unit_groups_.push_back() +// // unit_group_names.insert(i.second.race()); +// // } +// // //ERR_ED << i.first; +// // //std::string race_label; +// // //if (const unit_race *r = unit_types.find_race(i.second.race())) { +// // // race_label = r->plural_name(); +// // //} +// // } +//} + +/* $Id: editor_palettes.cpp -1 $ */ +/* + Copyright (C) 2003 - 2012 by David White + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +/** + * Manage the terrain-palette in the editor. + * Note: this is a near-straight rip from the old editor. + */ + +#define GETTEXT_DOMAIN "wesnoth-editor" + +#include "../editor_common.hpp" +#include "terrain_palettes.hpp" + +#include "editor_palettes.hpp" + +#include "../../foreach.hpp" +#include "../../gettext.hpp" +#include "../../serialization/string_utils.hpp" +#include "../../sound.hpp" +#include "../../tooltips.hpp" +#include "../../marked-up_text.hpp" + +namespace { + static std::string selected_terrain; +} + +namespace editor { + +std::string get_selected_terrain() +{ + return selected_terrain; +} + +static bool is_valid_terrain(t_translation::t_terrain c) { + return !(c == t_translation::VOID_TERRAIN || c == t_translation::FOGGED); +} + +/* TODO find a solution where this is not needed. */ +terrain_group::terrain_group(): + id(), + name(), + icon(), + core(false) + {} + +terrain_group::terrain_group(const config& cfg): + id(cfg["id"]), name(cfg["name"].t_str()), + icon(cfg["icon"]), core(cfg["core"].to_bool()) +{ +} + +void terrain_palette::update_report() +{ +// std::ostringstream msg; +// msg << _("FG: ") << get_terrain_string(selected_fg_terrain()) +// << '\n' << _("BG: ") << get_terrain_string(selected_bg_terrain()); +// selected_terrain = msg.str(); +} + +void terrain_palette::setup(const config& cfg) +{ + // items füllen + // Get the available terrains temporary in terrains_ + t_translation::t_list items = map().get_terrain_list(); + + ERR_ED << "grosse von items_ nach dem get_terrain_list:" << items.size() << "\n"; + + //move "invalid" items to the end + std::stable_partition(items.begin(), items.end(), is_valid_terrain); + + ERR_ED << "grosse von items_ nach dem sortieren:" << items.size() << "\n"; + + // Get the available groups and add them to the structure + std::set group_names; + foreach (const config &g, cfg.child_range("editor_group")) + { + if (group_names.find(g["id"]) == group_names.end()) { + groups_.push_back(terrain_group(g)); + group_names.insert(groups_.back().id); + } + } + + std::map id_to_group; + foreach (terrain_group& group, groups_) { + id_to_group.insert(std::make_pair(group.id, &group)); + } + + ERR_ED << "grosse von items nach dem group setup:" << items.size() << "\n"; + + // add the groups for all terrains to the map + foreach (const t_translation::t_terrain& t, items) { + + // Terrain specific stuff + const terrain_type& t_info = map().get_terrain_info(t); + DBG_ED << "Palette: processing terrain " << t_info.name() + << "(editor name: '" << t_info.editor_name() << "') " + << "(" << t_info.number() << ")" + << ": " << t_info.editor_group() << "\n"; + + // don't display terrains that were automatically created from base+overlay + if (t_info.is_combined()) continue; + // nor display terrains that have hide_in_editor=true + if (t_info.hide_in_editor()) continue; + + // add the terrain to the requested groups + const std::vector& keys = utils::split(t_info.editor_group()); + bool core = false; + + ERR_ED << "grosse von items_ nach dem splitt:" << items.size() << "\n"; + + item_map_[get_id(t)] = t; + + foreach (const std::string& k, keys) { + group_map_[k].push_back(get_id(t)); + nmax_items_ = std::max(nmax_items_, group_map_[k].size()); + std::map::iterator i = id_to_group.find(k); + if (i != id_to_group.end()) { + if (i->second->core) { + core = true; + } + } + } + + // A terrain is considered core iff it appears in at least + // one core terrain group + if (core) { + // Add the terrain to the default group + group_map_["all"].push_back(get_id(t)); + nmax_items_ = std::max(nmax_items_, group_map_["all"].size()); + } else { + non_core_items_.insert(get_id(t)); + } + + } + //typedef std::pair map_pair; + + ERR_ED << "grosse von items bevor dem set_group:" << items.size() << "\n"; + + // Set the default group + set_group("all"); + + ERR_ED << "grosse von items vor dem output:" << items.size() << "\n"; + if(active_group().empty()) { + ERR_ED << "No items found.\n"; + } + + update_report(); +} + + +void terrain_palette::draw_item(SDL_Rect& dstrect, const t_translation::t_terrain& terrain) { + + const t_translation::t_terrain base_terrain = map().get_terrain_info(terrain).default_base(); + + surface screen = gui_.video().getSurface(); + + //Draw default base for overlay terrains + if(base_terrain != t_translation::NONE_TERRAIN) { + const std::string base_filename = "terrain/" + map().get_terrain_info(base_terrain).editor_image() + ".png"; + surface base_image(image::get_image(base_filename)); + + if(base_image == NULL) { + // tooltip_text << "BASE IMAGE NOT FOUND\n"; + // ERR_ED << "image for terrain " << counter << ": '" << base_filename << "' not found\n"; + base_image = image::get_image(game_config::images::missing); + if (base_image == NULL) { + ERR_ED << "Placeholder image not found\n"; + return; + } + } + + if(static_cast(base_image->w) != size_specs_.terrain_size || + static_cast(base_image->h) != size_specs_.terrain_size) { + + base_image.assign(scale_surface(base_image, + size_specs_.terrain_size, size_specs_.terrain_size)); + } + + sdl_blit(base_image, NULL, screen, &dstrect); + } + + const std::string filename = "terrain/" + map().get_terrain_info(terrain).editor_image() + ".png"; + surface image(image::get_image(filename)); + if(image == NULL) { + // tooltip_text << "IMAGE NOT FOUND\n"; + // ERR_ED << "image for terrain " << counter << ": '" << filename << "' not found\n"; + image = image::get_image(game_config::images::missing); + if (image == NULL) { + ERR_ED << "Placeholder image not found\n"; + return; + } + } + + if(static_cast(image->w) != size_specs_.terrain_size || + static_cast(image->h) != size_specs_.terrain_size) { + + image.assign(scale_surface(image, + size_specs_.terrain_size, size_specs_.terrain_size)); + } + + sdl_blit(image, NULL, screen, &dstrect); +} + + +terrain_palette::terrain_palette(editor_display &gui, const size_specs &sizes, + const config& cfg, + t_translation::t_terrain& fore, + t_translation::t_terrain& back) + : editor_palette(gui, sizes, cfg, fore, back) +{ +} + +const std::string& terrain_palette::get_id(const t_translation::t_terrain& terrain) +{ + const terrain_type& t_info = map().get_terrain_info(terrain); + return t_info.id(); +} + +const std::string& terrain_palette::get_id(const terrain_group& group) +{ + return group.id; +} + +const std::vector& terrain_palette::get_items() +{ + return map().get_terrain_list(); +} + +} diff --git a/src/editor/palette/unit_palette.hpp b/src/editor/palette/unit_palette.hpp new file mode 100644 index 00000000000..8c4e9c60c28 --- /dev/null +++ b/src/editor/palette/unit_palette.hpp @@ -0,0 +1,72 @@ + +/* + Copyright (C) 2012 - 2012 by Fabian Mueller + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +/** + * Manage the unit-palette in the editor. + */ + +#ifndef UNIT_PALETTES_H_INCLUDED +#define UNIT_PALETTES_H_INCLUDED + +#include "editor_palettes.hpp" + +namespace editor { + +//std::string get_selected_terrain(); + +/** + * Stores the info about the data in editor-groups.cfg in a nice format. + * + * Helper struct which for some reason can't be moved to the cpp file. + */ +//struct terrain_group +//{ +// terrain_group(); +// terrain_group(const config& cfg); +// +// std::string id; +// t_string name; +// std::string icon; +// bool core; +//}; + +/** Palette where the terrain to be drawn can be selected. */ +class unit_palette : public editor_palette { +public: + unit_palette(editor_display &gui, const size_specs &sizes, + const config& cfg, + t_translation::t_terrain& fore, + t_translation::t_terrain& back); + + virtual const std::vector& get_items(); + + virtual void setup(const config& cfg); + +private: + + virtual const std::string& get_id(const t_translation::t_terrain& terrain); + virtual const std::string& get_id(const terrain_group& group); + + virtual void draw_item(SDL_Rect& dstrect, const t_translation::t_terrain& terrain); + + virtual void update_report(); + + /** Return a string representing the terrain and the underlying ones. */ + std::string get_terrain_string(const t_translation::t_terrain); + +}; + +} +#endif