Readd the tportrait class.

The code is used in a locally uncommitted branch, reverts 2010-03-28T13:27:17Z!guillaume.melquiond@gmail.com.
This commit is contained in:
Mark de Wever 2010-04-11 05:34:51 +00:00
parent 2c47276afb
commit ead3d0be5b
10 changed files with 159 additions and 3 deletions

View file

@ -406,6 +406,7 @@ set(wesnoth-main_SRC
playmp_controller.cpp
playsingle_controller.cpp
playturn.cpp
portrait.cpp
replay.cpp
replay_controller.cpp
resources.cpp

View file

@ -234,6 +234,7 @@ wesnoth_source = \
playmp_controller.cpp \
playsingle_controller.cpp \
playturn.cpp \
portrait.cpp \
replay.cpp \
replay_controller.cpp \
resources.cpp \

View file

@ -223,6 +223,7 @@ wesnoth_sources = Split("""
playmp_controller.cpp
playsingle_controller.cpp
playturn.cpp
portrait.cpp
replay.cpp
replay_controller.cpp
resources.cpp

View file

@ -2851,6 +2851,28 @@ WML_HANDLER_FUNCTION(message, event_info, cfg)
if(dlg_result == gui2::twindow::CANCEL) {
current_context->skip_messages = true;
}
/**
* @todo enable portrait code in 1.7 and write a clean api.
*/
#if 0
const tportrait* portrait =
speaker->second.portrait(400, tportrait::LEFT);
if(portrait) {
gui2::twml_message_left dlg(
caption,
cfg["message"],
portrait->image,
portrait->mirror);
dlg.show(screen->video());
if(dlg.get_retval() == gui2::twindow::CANCEL) {
handler.skip_messages(true);
}
return;
}
#endif
}
// Otherwise if an input has to be made, get it from the replay data

49
src/portrait.cpp Normal file
View file

@ -0,0 +1,49 @@
/* $Id$ */
/*
Copyright (C) 2008 - 2010 by mark de wever <koraq@xs4all.nl>
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 version 2
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.
*/
#include "portrait.hpp"
#include "config.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include "util.hpp"
#include "wml_exception.hpp"
static lg::log_domain log_config("config");
#define WRN_CF LOG_STREAM(warn, log_config)
static tportrait::tside get_side(const std::string& side)
{
if(side == "both") {
return tportrait::BOTH;
} else if(side == "right") {
return tportrait::RIGHT;
} else if(side == "left") {
return tportrait::LEFT;
} else {
WRN_CF << "Unknown portrait side '" << side << "' defaulting to left.\n";
return tportrait::LEFT;
}
}
tportrait::tportrait(const config& cfg) :
image(cfg["image"]),
side(get_side(cfg["side"])),
size(lexical_cast_default<unsigned>(cfg["size"])),
mirror(utils::string_bool(cfg["mirror"]))
{
VALIDATE(!image.empty(), missing_mandatory_wml_key("portrait", "image"));
VALIDATE(size != 0, missing_mandatory_wml_key("portrait", "size"));
}

41
src/portrait.hpp Normal file
View file

@ -0,0 +1,41 @@
/* $Id$ */
/*
Copyright (C) 2008 - 2010 by mark de wever <koraq@xs4all.nl>
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 version 2
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 PORTRAIT_HPP_INCLUDED
#define PORTRAIT_HPP_INCLUDED
class config;
#include <string>
/**
* @todo this is a proof-of-concept version and undocumented. It should be
* documented later and also allow WML to modify the portraits. This all
* starts to make sense when the new dialogs become mainline.
*/
/** Contains the definition of a portrait for a unit(type). */
struct tportrait {
tportrait(const config& cfg);
enum tside { LEFT, RIGHT, BOTH };
std::string image;
tside side;
unsigned size;
bool mirror;
};
#endif

View file

@ -2873,6 +2873,20 @@ std::string unit::image_mods() const{
return modifier.str();
}
const tportrait* unit::portrait(
const unsigned size, const tportrait::tside side) const
{
foreach(const tportrait& portrait, (type()->portraits())) {
if(portrait.size == size
&& (side == portrait.side || portrait.side == tportrait::BOTH)) {
return &portrait;
}
}
return NULL;
}
void unit::remove_attacks_ai()
{
if (attacks_left_ == max_attacks_) {

View file

@ -20,6 +20,7 @@
#include "config.hpp"
#include "formula_callable.hpp"
#include "map_location.hpp"
#include "portrait.hpp"
#include "race.hpp"
#include "unit_types.hpp"
#include "unit_map.hpp"
@ -324,6 +325,18 @@ public:
std::string TC_image_mods() const;
std::string image_mods() const;
/**
* Gets the portrait for a unit.
*
* @param size The size of the portrait.
* @param side The side the portrait is shown on.
*
* @returns The portrait with the wanted size.
* @retval NULL The wanted portrait doesn't exist.
*/
const tportrait* portrait(
const unsigned size, const tportrait::tside side) const;
private:
bool internal_matches_filter(const vconfig& cfg,const map_location& loc,

View file

@ -586,7 +586,8 @@ unit_type::unit_type() :
possibleTraits_(),
genders_(),
animations_(),
build_status_(NOT_BUILT)
build_status_(NOT_BUILT),
portraits_()
{
gender_types_[0] = NULL;
gender_types_[1] = NULL;
@ -628,7 +629,8 @@ unit_type::unit_type(const unit_type& o) :
possibleTraits_(o.possibleTraits_),
genders_(o.genders_),
animations_(o.animations_),
build_status_(o.build_status_)
build_status_(o.build_status_),
portraits_(o.portraits_)
{
gender_types_[0] = o.gender_types_[0] != NULL ? new unit_type(*o.gender_types_[0]) : NULL;
gender_types_[1] = o.gender_types_[1] != NULL ? new unit_type(*o.gender_types_[1]) : NULL;
@ -673,7 +675,8 @@ unit_type::unit_type(const config& cfg, const movement_type_map& mv_types,
possibleTraits_(),
genders_(),
animations_(),
build_status_(NOT_BUILT)
build_status_(NOT_BUILT),
portraits_()
{
build_full(cfg, mv_types, races, traits);
}
@ -780,6 +783,11 @@ void unit_type::build_full(const config& cfg, const movement_type_map& mv_types,
flag_rgb_ = cfg["flag_rgb"];
game_config::add_color_info(cfg);
foreach (const config &portrait, cfg_.child_range("portrait")) {
portraits_.push_back(tportrait(portrait));
}
// Deprecation messages, only seen when unit is parsed for the first time.
build_status_ = FULL;

View file

@ -17,6 +17,7 @@
#include "unit_animation.hpp"
#include "config.hpp"
#include "map_location.hpp"
#include "portrait.hpp"
#include "race.hpp"
#include <set>
@ -267,6 +268,8 @@ public:
BUILD_STATUS build_status() const { return build_status_; }
const std::vector<tportrait>& portraits() const { return portraits_; }
const config &get_cfg() const { return cfg_; }
void set_config(const config& cfg);
@ -323,6 +326,9 @@ private:
mutable std::vector<unit_animation> animations_;
BUILD_STATUS build_status_;
/** List with the portraits available for the unit. */
std::vector<tportrait> portraits_;
};
class unit_type_data