4
Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
6
This program is free software; you can redistribute it and/or modify it
7
under the terms of the GNU General Public License as published by the
8
Free Software Foundation; either version 2 of the License, or (at your
9
option) any later version.
11
This program is distributed in the hope that it will be useful, but WITHOUT
12
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation, Inc.,
18
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
contact: plee-the-bear@gamned.org
22
Please add the tag [Bear] in the subject of your mails.
26
* \brief Implementation of the bear::line class.
27
* \author Julien Jorge
29
#include "generic_items/line.hpp"
31
#include "visual/scene_line.hpp"
35
BASE_ITEM_EXPORT(line, bear)
37
/*----------------------------------------------------------------------------*/
45
set_can_move_items(false);
49
/*----------------------------------------------------------------------------*/
52
* \param that The instance of the parent class from which we initialise.
54
bear::line::line( const super& that )
55
: super(that), m_width(1)
58
set_can_move_items(false);
62
/*----------------------------------------------------------------------------*/
64
* \brief Initialise the item.
66
void bear::line::build()
70
adjust_position_and_size();
73
/*----------------------------------------------------------------------------*/
75
* \brief Set a field of type real.
76
* \param name The name of the field to set.
77
* \param value The value of the field.
79
bool bear::line::set_real_field( const std::string& name, double value )
83
if ( name == "line.width" )
86
result = super::set_real_field(name, value);
89
} // line::set_real_field()
91
/*----------------------------------------------------------------------------*/
93
* \brief Set a field of type "list of items".
94
* \param name The name of the field to set.
95
* \param value The value of the field.
97
bool bear::line::set_item_list_field
98
( const std::string& name, const std::vector<engine::base_item*>& value )
102
if ( name == "line.ends" )
103
m_points = point_list_type(value.begin(), value.end());
105
result = super::set_item_list_field(name, value);
108
} // line::set_item_list_field()
110
/*----------------------------------------------------------------------------*/
112
* \brief Do one step in the progression of the item.
113
* \param elapsed_time Elapsed time since the last call.
115
void bear::line::progress( universe::time_type elapsed_time )
117
super::progress(elapsed_time);
119
adjust_position_and_size();
120
} // line::progress()
122
/*----------------------------------------------------------------------------*/
124
* \brief Get the visual of the item.
125
* \param visual (out) The visual representation of the item.
127
void bear::line::get_visual( std::list<engine::scene_visual>& visuals ) const
129
super::get_visual(visuals);
131
if ( m_points.size() <= 1 )
134
std::vector<visual::position_type> p;
135
p.reserve(m_points.size());
137
for ( point_list_type::const_iterator it=m_points.begin();
138
it!=m_points.end(); ++it)
139
p.push_back( (*it)->get_center_of_mass() );
141
engine::scene_visual v
144
( 0, 0, claw::graphic::white_pixel, p, m_width) ) );
146
v.scene_element.set_position( get_gap() );
147
visuals.push_front(v);
148
} // line::get_visual()
150
/*----------------------------------------------------------------------------*/
152
* \brief Add a new reference point at the end of the line.
153
* \param item The item to use as the new reference point.
155
void bear::line::push_back( engine::base_item* item )
157
m_points.push_back(item);
158
} // line::push_back()
160
/*----------------------------------------------------------------------------*/
162
* \brief Set the width of the line.
163
* \param w The new width of the line.
165
void bear::line::set_line_width( visual::size_type w )
168
} // line::set_line_width()
170
/*----------------------------------------------------------------------------*/
172
* \brief Adjust the position and the size of the item to fit the points.
174
void bear::line::adjust_position_and_size()
176
universe::coordinate_type left =
177
std::numeric_limits<universe::coordinate_type>::max();
178
universe::coordinate_type right =
179
std::numeric_limits<universe::coordinate_type>::min();
180
universe::coordinate_type bottom = left;
181
universe::coordinate_type top = right;
183
for ( point_list_type::iterator it=m_points.begin(); it!=m_points.end(); )
186
point_list_type::iterator tmp(it);
192
left = std::min(left, (*it)->get_left());
193
bottom = std::min(bottom, (*it)->get_bottom());
194
right = std::max(right, (*it)->get_right());
195
top = std::max(top, (*it)->get_top());
200
if ( !m_points.empty() )
204
set_size(right - left, top - bottom);
206
} // line::adjust_position_and_size()