Arrows: rewrote the update_symbols method to use the new images,

...and did a general cleanup of the class. Updated the code referring
to the arrows to use the new styles.
This commit is contained in:
Gabriel Morin 2010-08-12 09:54:59 +00:00
parent 2d23b5ced6
commit d2638aa463
7 changed files with 96 additions and 120 deletions

View file

@ -35,9 +35,7 @@ static lg::log_domain log_arrows("arrows");
arrow::arrow()
: layer_(display::LAYER_ARROWS)
, color_("red")
, style_("")
, alpha_()
, draw_last_hex_()
, style_(STYLE_STANDARD)
, path_()
, previous_path_()
, symbols_map_()
@ -53,7 +51,7 @@ arrow::~arrow()
}
}
void arrow::set_path(const arrow_path_t &path)
void arrow::set_path(arrow_path_t const& path)
{
if (valid_path(path))
{
@ -72,7 +70,7 @@ void arrow::reset()
previous_path_.clear();
}
void arrow::set_color(const std::string& color)
void arrow::set_color(std::string const& color)
{
color_ = color;
if (valid_path(path_))
@ -81,6 +79,10 @@ void arrow::set_color(const std::string& color)
}
}
std::string const arrow::STYLE_STANDARD = "standard";
std::string const arrow::STYLE_HIGHLIGHTED = "highlighted";
std::string const arrow::STYLE_FOCUS = "focus";
void arrow::set_style(const std::string& style)
{
style_ = style;
@ -90,7 +92,7 @@ void arrow::set_style(const std::string& style)
}
}
void arrow::set_layer(const display::tdrawing_layer & layer)
void arrow::set_layer(display::tdrawing_layer const& layer)
{
layer_ = layer;
if (valid_path(path_))
@ -100,41 +102,32 @@ void arrow::set_layer(const display::tdrawing_layer & layer)
}
}
void arrow::set_alpha(double alpha)
{
alpha_ = ftofxp(alpha);
if (valid_path(path_))
{
update_symbols(path_);
}
}
const arrow_path_t & arrow::get_path() const
arrow_path_t const& arrow::get_path() const
{
return path_;
}
const arrow_path_t & arrow::get_previous_path() const
arrow_path_t const& arrow::get_previous_path() const
{
return previous_path_;
}
bool arrow::path_contains(const map_location & hex) const
bool arrow::path_contains(map_location const& hex) const
{
bool contains = symbols_map_.find(hex) != symbols_map_.end();
return contains;
}
void arrow::draw_hex(const map_location & hex)
void arrow::draw_hex(map_location const& hex)
{
if(SCREEN && path_contains(hex))
{
SCREEN->render_image(SCREEN->get_location_x(hex), SCREEN->get_location_y(hex), layer_,
hex, image::get_image(symbols_map_[hex], image::SCALED_TO_ZOOM), false, false, alpha_);
hex, image::get_image(symbols_map_[hex], image::SCALED_TO_ZOOM));
}
}
bool arrow::valid_path(arrow_path_t path) const
bool arrow::valid_path(arrow_path_t path)
{
if (path.size() >= 2)
return true;
@ -154,82 +147,96 @@ void arrow::update_symbols(arrow_path_t old_path)
invalidate_arrow_path(old_path);
const std::string mods = "~RC(FF00FF>"+ color_ + ")"; //magenta to current color
std::string const mods = "~RC(FF00FF>"+ color_ + ")"; //magenta to current color
const std::string dirname = "arrows/";
std::string style = style_;
if (!style.empty())
style = style + "/";
std::string const dirname = "arrows/";
map_location::DIRECTION exit_dir = map_location::NDIRECTIONS;
map_location::DIRECTION enter_dir = map_location::NDIRECTIONS;
std::string prefix = "";
std::string suffix = "";
std::string image_filename = "";
bool begin = false;
arrow_path_t::const_iterator const arrow_start_hex = path_.begin();
arrow_path_t::const_iterator const arrow_pre_end_hex = path_.end() - 2;
arrow_path_t::const_iterator const arrow_end_hex = path_.end() - 1;
bool start = false;
bool pre_end = false;
bool end = false;
bool empty_after_end = false;
bool teleport_out = false;
bool teleport_in = false;
arrow_path_t::iterator hex;
for (hex = path_.begin(); hex != path_.end(); ++hex)
{
exit_dir = map_location::NDIRECTIONS;
enter_dir = map_location::NDIRECTIONS;
prefix = "";
suffix = "";
image_filename = "";
begin = end = false;
start = pre_end = end = false;
// teleport in if we teleported out last hex
teleport_in = teleport_out;
teleport_out = false;
// Determine some special cases
if (hex == path_.begin())
begin = true;
if (hex == path_.end() - 1 && draw_last_hex_)
if (hex == arrow_start_hex)
start = true;
if (hex == arrow_pre_end_hex)
pre_end = true;
else if (hex == arrow_end_hex)
end = true;
else if (hex == path_.end() - 2 && !draw_last_hex_)
end = true;
else if (hex == path_.end() - 1 && !draw_last_hex_)
empty_after_end = true;
if (hex != path_.end() - 1 && !tiles_adjacent(*hex, *(hex + 1)))
if (hex != arrow_end_hex && !tiles_adjacent(*hex, *(hex + 1)))
teleport_out = true;
// calculate enter and exit directions, if available
enter_dir = map_location::NDIRECTIONS;
if (!start && !teleport_in)
{
enter_dir = hex->get_relative_dir(*(hex-1));
}
exit_dir = map_location::NDIRECTIONS;
if (!end && !teleport_out)
{
exit_dir = hex->get_relative_dir(*(hex+1));
}
// Now figure out the actual images
if (teleport_out)
{
image_filename = dirname + style + "teleport-out.png";
prefix = "teleport-out";
if (enter_dir != map_location::NDIRECTIONS)
{
suffix = map_location::write_direction(enter_dir);
}
}
else if (teleport_in)
{
image_filename = dirname + style + "teleport-in.png";
}
else if (empty_after_end)
prefix = "teleport-in";
if (exit_dir != map_location::NDIRECTIONS)
{
image_filename = "";
suffix = map_location::write_direction(exit_dir);
}
else if (begin)
}
else if (start)
{
prefix = "start";
exit_dir = hex->get_relative_dir(*(hex+1));
suffix = map_location::write_direction(exit_dir);
if (end)
if (pre_end)
{
suffix = suffix + "_end";
suffix = suffix + "_ending";
}
image_filename = dirname + style + prefix + "-" + suffix + ".png";
}
else if (end)
{
prefix = "end";
suffix = map_location::write_direction(enter_dir);
}
else
{
enter_dir = hex->get_relative_dir(*(hex-1));
exit_dir = hex->get_relative_dir(*(hex+1));
std::string enter, exit;
enter = map_location::write_direction(enter_dir);
exit = map_location::write_direction(exit_dir);
if (end)
if (pre_end)
{
exit = exit + "_end";
exit = exit + "_ending";
}
assert(abs(enter_dir - exit_dir) > 1); //impossible turn?
@ -243,11 +250,16 @@ void arrow::update_symbols(arrow_path_t old_path)
prefix = exit;
suffix = enter;
}
image_filename = dirname + style + prefix + "-" + suffix + ".png";
}
if (image_filename != "")
image_filename = dirname + style_ + "/" + prefix;
if (suffix != "")
{
image_filename += ("-" + suffix);
}
image_filename += ".png";
assert(image_filename != "");
image::locator image = image::locator(image_filename, mods);
if (!image.file_exists())
{
@ -256,11 +268,6 @@ void arrow::update_symbols(arrow_path_t old_path)
}
symbols_map_[*hex] = image;
}
else
{
symbols_map_[*hex] = image::locator();
}
}
invalidate_arrow_path(path_);
@ -271,7 +278,7 @@ void arrow::invalidate_arrow_path(arrow_path_t path)
{
if(!SCREEN) return;
foreach(const map_location& loc, path)
foreach(map_location const& loc, path)
{
SCREEN->invalidate(loc);
}

View file

@ -33,16 +33,14 @@ typedef std::vector<map_location> arrow_path_t;
*/
class arrow {
typedef std::map<map_location, image::locator> arrow_symbols_map_t;
public:
//operations
arrow();
virtual ~arrow();
virtual void set_path(const arrow_path_t &path);
virtual void set_path(arrow_path_t const& path);
///invalidates and clears the present path, forgets the previous path, clears the symbols map
virtual void reset();
/**
@ -50,7 +48,7 @@ public:
* image::locator modifiers parameter. Examples: red is "red" or "FF0000" or "255,0,0".
* Feel free to add another method that accepts an Uint32 as a parameter instead.
*/
virtual void set_color(const std::string& color);
virtual void set_color(std::string const& color);
virtual std::string get_color() const { return color_; }
@ -60,54 +58,44 @@ public:
* If it doesn't exist or has missing images, you'll get "under construction"
* symbols instead of arrow graphics.
*/
void set_style(const std::string& style);
void set_style(std::string const& style);
static std::string const STYLE_STANDARD;
static std::string const STYLE_HIGHLIGHTED;
static std::string const STYLE_FOCUS;
void set_layer(const display::tdrawing_layer & layer);
void set_layer(display::tdrawing_layer const& layer);
/// Sets transparency to the specified alpha value
/// 0.5 is 50% transparent, anything above 1.0 brightens the arrow images
void set_alpha(double alpha);
arrow_path_t const& get_path() const;
arrow_path_t const& get_previous_path() const;
/// If set to false (default), the end symbol is drawn in the second-to-last hex
void set_draw_last_hex(bool draw_last_hex) { draw_last_hex_ = draw_last_hex; }
bool path_contains(map_location const& hex) const;
const arrow_path_t & get_path() const;
const arrow_path_t & get_previous_path() const;
bool path_contains(const map_location & hex) const;
virtual void draw_hex(const map_location & hex);
virtual void draw_hex(map_location const& hex);
/// Checks that the path is not of length 0 or 1
virtual bool valid_path(arrow_path_t path) const;
static bool valid_path(arrow_path_t path);
/// Invalidates every hex along the given path
static void invalidate_arrow_path(arrow_path_t path);
virtual void notify_arrow_changed();
protected:
//operations
/**
* @param old_path : the path to erase and replace with the new symbols
*/
virtual void update_symbols(arrow_path_t old_path);
virtual void invalidate_arrow_path(arrow_path_t path);
protected:
//properties
display::tdrawing_layer layer_;
std::string color_;
/// represents the subdirectory that holds images for this arrow style
std::string style_;
float alpha_;
bool draw_last_hex_;
arrow_path_t path_;
arrow_path_t previous_path_;
typedef std::map<map_location, image::locator> arrow_symbols_map_t;
arrow_symbols_map_t symbols_map_;
};
#endif

View file

@ -43,7 +43,6 @@ highlight_visitor::highlight_visitor(const unit_map& unit_map, side_actions_ptr
, selection_candidate_(NULL)
, main_highlight_()
, secondary_highlights_()
, color_backup_()
{
}
@ -222,9 +221,7 @@ void highlight_visitor::visit_move(move_ptr move)
case HIGHLIGHT_MAIN:
if (move->arrow_)
{
color_backup_ = move->arrow_->get_color();
move->arrow_->set_alpha(move::ALPHA_HIGHLIGHT);
move->arrow_->set_color("white");
move->arrow_->set_style(arrow::STYLE_FOCUS);
}
if (move->fake_unit_)
{
@ -234,7 +231,7 @@ void highlight_visitor::visit_move(move_ptr move)
case HIGHLIGHT_SECONDARY:
if (move->arrow_)
{
move->arrow_->set_alpha(move::ALPHA_HIGHLIGHT);
move->arrow_->set_style(arrow::STYLE_HIGHLIGHTED);
}
if (move->fake_unit_)
{
@ -244,11 +241,7 @@ void highlight_visitor::visit_move(move_ptr move)
case UNHIGHLIGHT:
if (move->arrow_)
{
if (move == main_highlight_.lock())
{
move->arrow_->set_color(color_backup_);
}
move->arrow_->set_alpha(move::ALPHA_NORMAL);
move->arrow_->set_style(arrow::STYLE_STANDARD);
}
if (move->fake_unit_)
{

View file

@ -83,8 +83,6 @@ private:
weak_action_ptr main_highlight_;
std::deque<weak_action_ptr> secondary_highlights_;
std::string color_backup_;
};
} // end namespace wb

View file

@ -395,7 +395,7 @@ void manager::create_temp_move()
move_arrow_.reset(new arrow());
move_arrow_->set_color(team::get_side_color_index(
viewer_side()));
move_arrow_->set_alpha(move::ALPHA_HIGHLIGHT);
move_arrow_->set_style(arrow::STYLE_HIGHLIGHTED);
resources::screen->add_arrow(*move_arrow_);
}

View file

@ -56,11 +56,6 @@ std::ostream& move::print(std::ostream &s) const
return s;
}
const double move::ALPHA_HIGHLIGHT = 1.0;
const double move::ALPHA_NORMAL = 0.6;
const std::string move::ARROW_STYLE_VALID = "";
const std::string move::ARROW_STYLE_INVALID = "invalid";
move::move(size_t team_index, const pathfind::marked_route& route,
arrow_ptr arrow, fake_unit_ptr fake_unit)
: action(team_index),
@ -105,7 +100,7 @@ bool move::execute()
bool move_finished_completely = false;
arrow_->set_alpha(ALPHA_HIGHLIGHT);
arrow_->set_style(arrow::STYLE_HIGHLIGHTED);
map_location final_location;
bool steps_finished;
@ -115,7 +110,7 @@ bool move::execute()
try {
steps_finished = mouse_handler.move_unit_along_route(*route_, &final_location, owner_team.auto_shroud_updates());
} catch (end_turn_exception e) {
arrow_->set_alpha(ALPHA_NORMAL);
arrow_->set_style(arrow::STYLE_STANDARD);
throw; // we rely on the caller to delete this action
}
// final_location now contains the final unit location
@ -176,7 +171,7 @@ bool move::execute()
LOG_WB << "Unit disappeared from map during move execution.\n";
}
arrow_->set_alpha(ALPHA_NORMAL);
arrow_->set_style(arrow::STYLE_STANDARD);
return move_finished_completely;
}

View file

@ -35,11 +35,6 @@ public:
friend class validate_visitor;
friend class highlight_visitor;
static const double ALPHA_HIGHLIGHT;
static const double ALPHA_NORMAL;
static const std::string ARROW_STYLE_VALID;
static const std::string ARROW_STYLE_INVALID;
///Future unit map must be valid during construction, so that move can find its unit
move(size_t team_index, const pathfind::marked_route& route, arrow_ptr arrow,
fake_unit_ptr fake_unit);