Made the palette a widget.

This commit is contained in:
fendrin 2013-05-01 16:56:43 +02:00
parent b1cef372d6
commit 530ac238ec
10 changed files with 82 additions and 55 deletions

View file

@ -15,6 +15,7 @@
#ifndef COMMON_PALETTES_H_INCLUDED
#define COMMON_PALETTES_H_INCLUDED
#include "display.hpp"
#include "config.hpp"
#include "gui/widgets/widget.hpp"
@ -38,10 +39,12 @@ struct item_group
};
class common_palette {
class common_palette : public gui::widget {
public:
common_palette(display& gui) : gui::widget(gui.video(), true) {}
virtual ~common_palette() {}
//event handling
@ -61,7 +64,7 @@ public:
//drawing
virtual void adjust_size(const SDL_Rect& target) = 0;
virtual void draw(bool force) = 0;
virtual void draw() = 0;
//group
virtual void set_group(size_t index) = 0;

View file

@ -43,11 +43,6 @@ template handler_vector editor_palette<unit_type>::handler_members();
template<class Item>
void editor_palette<Item>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items)
{
//for (unsigned int i = 0; i < items.size(); ++i) {
// if (items[i] == "editor-palette-groups") {
// items.erase(items.begin() + i);
//std::vector<std::string> groups;
const std::vector<item_group>& item_groups = get_groups();
for (size_t mci = 0; mci < item_groups.size(); ++mci) {
@ -55,11 +50,9 @@ void editor_palette<Item>::expand_palette_groups_menu(std::vector< std::pair< st
if (groupname.empty()) {
groupname = _("(Unknown Group)");
}
const std::string img = item_groups[mci].icon + ".png";
items.push_back(std::pair<std::string, std::string>(img, groupname));
const std::string& img = item_groups[mci].icon;
items.push_back(std::pair<std::string, std::string>( img, groupname));
}
//items.insert(items.begin() + i, groups.begin(), groups.end());
//break;
}
template void editor_palette<t_translation::t_terrain>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items);
template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items);
@ -73,6 +66,7 @@ bool editor_palette<Item>::scroll_up()
}
if(items_start_ >= decrement) {
items_start_ -= decrement;
draw();
return true;
}
return false;
@ -111,7 +105,8 @@ bool editor_palette<Item>::scroll_down()
items_start_ += num_items() % item_width_;
scrolled = true;
}
draw(scrolled);
set_dirty(scrolled);
draw();
return scrolled;
}
template bool editor_palette<t_translation::t_terrain>::scroll_down();
@ -126,10 +121,11 @@ void editor_palette<Item>::set_group(const std::string& id)
BOOST_FOREACH(const item_group& group, groups_) {
if (group.id == id) {
found = true;
gui::button* palette_action_button = gui_.find_action_button("menu-editor-terrain");
if (palette_action_button) {
palette_action_button->set_label(group.name);
palette_action_button->set_image(group.icon);
gui::button* palette_menu_button = gui_.find_action_button("menu-editor-terrain");
if (palette_menu_button) {
//palette_menu_button->set_label(group.name);
palette_menu_button->set_tooltip_string(group.name);
palette_menu_button->set_overlay(group.icon);
}
}
}
@ -141,7 +137,6 @@ void editor_palette<Item>::set_group(const std::string& id)
ERR_ED << "No items found in group with the id: '" << id << "'.\n";
}
//if (groups_.size() >= 3)
gui_.set_palette_report(active_group_report());
}
template void editor_palette<t_translation::t_terrain>::set_group(const std::string& id);
@ -199,6 +194,8 @@ void editor_palette<Item>::adjust_size(const SDL_Rect& target)
item_width_;
nitems_ = std::min<int>(items_fitting, nmax_items_);
buttons_.resize(nitems_, gui::tristate_button(gui_.video(), "", this));
set_location(target);
set_dirty(true);
}
template void editor_palette<t_translation::t_terrain>::adjust_size(const SDL_Rect& target);
template void editor_palette<unit_type>::adjust_size(const SDL_Rect& target);
@ -208,6 +205,7 @@ void editor_palette<Item>::select_fg_item(const std::string& item_id)
{
if (selected_fg_item_ != item_id) {
selected_fg_item_ = item_id;
set_dirty();
}
}
template void editor_palette<t_translation::t_terrain>::select_fg_item(const std::string& terrain_id);
@ -218,6 +216,7 @@ void editor_palette<Item>::select_bg_item(const std::string& item_id)
{
if (selected_bg_item_ != item_id) {
selected_bg_item_ = item_id;
set_dirty();
}
}
template void editor_palette<t_translation::t_terrain>::select_bg_item(const std::string& terrain_id);
@ -241,9 +240,9 @@ template size_t editor_palette<t_translation::t_terrain>::num_items();
template size_t editor_palette<unit_type>::num_items();
template<class Item>
void editor_palette<Item>::draw(bool force)
void editor_palette<Item>::draw_contents()
{
if (!force) return;
// if (!force && !dirty()) return;
unsigned int y = palette_y_;
unsigned int x = palette_x_;
unsigned int starting = items_start_;
@ -259,6 +258,7 @@ void editor_palette<Item>::draw(bool force)
if (downscroll_button)
downscroll_button->enable(ending != num_items());
unsigned int counter = starting;
for (unsigned int i = 0 ; i < buttons_.size() ; i++) {
//TODO check if the conditions still hold for the counter variable
@ -295,7 +295,7 @@ void editor_palette<Item>::draw(bool force)
dstrect.h = item_size_ + 2;
tile.set_location(dstrect);
tile.set_help_string(tooltip_text.str());
tile.set_tooltip_string(tooltip_text.str());
tile.set_item_image(item_image);
tile.set_item_id(item_id);
@ -312,15 +312,17 @@ void editor_palette<Item>::draw(bool force)
tile.set_dirty(true);
tile.hide(false);
tile.draw();
// Adjust location
if (counter_from_zero % item_width_ == item_width_ - 1)
y += item_space_;
counter++;
}
update_rect(location());
}
template void editor_palette<t_translation::t_terrain>::draw(bool);
template void editor_palette<unit_type>::draw(bool);
template void editor_palette<t_translation::t_terrain>::draw_contents();
template void editor_palette<unit_type>::draw_contents();
} // end namespace editor

View file

@ -19,6 +19,8 @@
#include "common_palette.hpp"
#include "tristate_button.hpp"
#include <boost/foreach.hpp>
namespace editor {
template<class Item>
@ -28,12 +30,13 @@ public:
editor_palette(editor_display &gui, const config& /*cfg*/
, size_t item_size, size_t item_width, mouse_action** active_mouse_action)
: groups_()
: common_palette(gui)
, groups_()
, gui_(gui)
, item_size_(item_size)
, item_width_(item_width)
//TODO avoid magic number
, item_space_(item_size + 5)
, item_space_(item_size + 3)
, palette_y_(0)
, palette_x_(0)
, group_map_()
@ -50,6 +53,8 @@ public:
{
};
virtual handler_vector handler_members();
void set_start_item(size_t index) { items_start_ = index; };
@ -60,16 +65,20 @@ public:
void expand_palette_groups_menu(std::vector< std::pair<std::string, std::string> >& items);
void set_group(size_t index);
// int active_group();
const std::vector<item_group>& get_groups() const { return groups_; };
virtual void draw(bool force);
virtual void draw() {
widget::draw();
};
virtual void draw_contents();
void next_group() {
set_group( (active_group_index() +1) % (groups_.size() -1) );
set_group( (active_group_index() +1) % (groups_.size()) );
};
void prev_group() {
set_group( (active_group_index() -1) % (groups_.size() -1) );
set_group( (active_group_index() -1) % (groups_.size()) );
};
/**
@ -94,7 +103,6 @@ public:
private:
/** TODO */
size_t active_group_index();
/** Scroll the editor-palette to the top. */
@ -115,7 +123,12 @@ private:
/** Return the number of items in the palette. */
size_t num_items();
//void draw_old(bool);
void hide(bool hidden) {
widget::hide(hidden);
BOOST_FOREACH(gui::widget& w, buttons_) {
w.hide(hidden);
}
}
protected:
/**

View file

@ -28,7 +28,9 @@ class empty_palette : public common_palette {
public:
empty_palette(display& gui) : gui_(gui), empty_() {};
empty_palette(display& gui) :
common_palette(gui),
gui_(gui), empty_() {};
//event handling
virtual bool mouse_click() { return false;};
@ -43,7 +45,7 @@ public:
//drawing
virtual void adjust_size(const SDL_Rect& /*target*/) {};
virtual void draw(bool) {
virtual void draw() {
//TODO
/*
gui::button* upscroll_button = gui_.find_button("upscroll-button-editor");

View file

@ -60,10 +60,10 @@ void palette_manager::scroll_down()
if (scrolled) {
const SDL_Rect& rect = gui_.palette_area();
bg_restore(rect);
// const SDL_Rect& rect = gui_.palette_area();
// bg_restore(rect);
set_dirty();
draw(true);
draw();
}
}
@ -81,10 +81,10 @@ void palette_manager::scroll_up()
{
bool scrolled_up = active_palette().scroll_up();
if(scrolled_up) {
const SDL_Rect rect = gui_.palette_area();
bg_restore(rect);
// const SDL_Rect rect = gui_.palette_area();
// bg_restore(rect);
set_dirty();
draw(true);
draw();
}
}
@ -111,11 +111,11 @@ void palette_manager::scroll_bottom()
}
}
void palette_manager::draw(bool force)
void palette_manager::draw_contents()
{
if (!dirty() && !force) {
return;
}
//if (!dirty() && !force) {
// return;
//}
const SDL_Rect &loc = location();
@ -124,20 +124,19 @@ void palette_manager::draw(bool force)
gui::button* upscroll_button = gui_.find_action_button("upscroll-button-editor");
if (upscroll_button)
upscroll_button->hide(false);
gui::button* downscroll_button = gui_.find_action_button("downscroll-button-editor");
if (downscroll_button)
downscroll_button->hide(false);
gui::button* palette_menu_button = gui_.find_action_button("menu-editor-terrain");
if (palette_menu_button)
palette_menu_button->hide(false);
bg_restore(loc);
active_palette().draw(dirty() || force);
// bg_restore(loc);
active_palette().set_dirty(true);
active_palette().draw();
//active_palette().hide(false);
update_rect(loc);
set_dirty(false);
// set_dirty(false);
}
handler_vector palette_manager::handler_members()

View file

@ -62,8 +62,8 @@ public:
* If force is true everything will be redrawn,
* even though it is not invalidated.
*/
void draw(bool force=false);
void draw() { draw(false); };
//void draw(bool force=false);
void draw_contents(); // { draw(false); };
public:

View file

@ -96,7 +96,7 @@ void terrain_palette::setup(const config& cfg)
cfg["id"] = g["id"];
cfg["name"] = g["name"];
cfg["icon"] = "buttons/editor/group/" + g["icon"].t_str();
cfg["icon"] = "icons/terrain/terrain_" + g["icon"].t_str();
cfg["core"] = "yes";
groups_.push_back(item_group(cfg));
@ -194,7 +194,7 @@ void terrain_palette::draw_item(const t_translation::t_terrain& terrain,
}
const std::string filename = "terrain/" + map().get_terrain_info(terrain).editor_image() + ".png";
image = image::get_image(filename);
image = surface(image::get_image(filename));
if(image == NULL) {
tooltip_text << "IMAGE NOT FOUND\n";
ERR_ED << "image for terrain: '" << filename << "' not found\n";
@ -219,7 +219,7 @@ void terrain_palette::draw_item(const t_translation::t_terrain& terrain,
terrain_palette::terrain_palette(editor_display &gui, const config& cfg,
mouse_action** active_mouse_action)
//TODO avoid magic numbers
: editor_palette<t_translation::t_terrain>(gui, cfg, 36, 4, active_mouse_action)
: editor_palette<t_translation::t_terrain>(gui, cfg, 38, 4, active_mouse_action)
{
}

View file

@ -1,5 +1,5 @@
/*
Copyright (C) 2013 by Fabian Mueller <fabianmueller5@gmx.de>
Copyright (C) 2003 - 2013 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
@ -47,6 +47,8 @@ public:
void set_pressed(PRESSED_STATE new_pressed_state);
// void set_active(bool active);
bool pressed();
PRESSED_STATE pressed_state() const;
@ -87,9 +89,11 @@ private:
surface baseImage_, touchedBaseImage_, activeBaseImage_,
itemImage_,
// normalImage_, activeImage_,
pressedDownImage_, pressedUpImage_, pressedBothImage_,
pressedBothActiveImage_, pressedDownActiveImage_, pressedUpActiveImage_,
touchedDownImage_, touchedUpImage_, touchedBothImage_;
// disabledImage_, pressedDownDisabledImage_, pressedUpDisabledImage_, pressedBothDisabledImage_;
SDL_Rect textRect_;
@ -100,10 +104,13 @@ private:
PRESSED_LEFT, PRESSED_RIGHT, PRESSED_BOTH
};
STATE state_;
bool pressed_;
SPACE_CONSUMPTION spacing_;
int base_height_, base_width_;
editor::common_palette* palette_;

View file

@ -97,7 +97,7 @@ void unit_palette::draw_item(const unit_type& u, surface& image, std::stringstre
filename << u.image() << "~RC(" << u.flag_rgb() << '>'
<< team::get_side_color_index(gui_.viewing_side()) << ')';
image = image::get_image(filename.str());
image = surface(image::get_image(filename.str()));
if(image == NULL) {
tooltip_text << "IMAGE NOT FOUND\n";
ERR_ED << "image for unit type: '" << filename.str() << "' not found\n";

View file

@ -112,6 +112,7 @@ void editor_toolkit::hotkey_set_mouse_action(hotkey::HOTKEY_COMMAND command)
{
std::map<hotkey::HOTKEY_COMMAND, mouse_action*>::iterator i = mouse_actions_.find(command);
if (i != mouse_actions_.end()) {
palette_manager_->active_palette().hide(true);
mouse_action_ = i->second;
palette_manager_->adjust_size();
@ -120,6 +121,7 @@ void editor_toolkit::hotkey_set_mouse_action(hotkey::HOTKEY_COMMAND command)
set_mouseover_overlay();
gui_.invalidate_game_status();
palette_manager_->active_palette().hide(false);
} else {
ERR_ED << "Invalid hotkey command ("
<< static_cast<int>(command) << ") passed to set_mouse_action\n";
@ -175,7 +177,6 @@ void editor_toolkit::cycle_brush()
void editor_toolkit::adjust_size()
{
palette_manager_->adjust_size();
palette_manager_->draw(true);
}