~ubuntu-branches/ubuntu/trusty/plee-the-bear/trusty-proposed

« back to all changes in this revision

Viewing changes to bear-engine/lib/src/generic_items/code/line.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge, Julien Jorge
  • Date: 2010-11-17 20:13:34 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101117201334-o4dp7uq437to7oxb
Tags: 0.5.1-1
[ Julien Jorge ]
* New upstream release (Closes: #565062, #546514).
* Add armhf architecture (Closes: #604689).
* Remove patches integrated upstream: rpath-editors.diff, rpath-game.diff,
  editors-menu-section.diff.
* Bump the Standard-Version, no changes.
* Update my email address.
* Set build dependency of libclaw to 1.6.0.
* Add the missing ${misc:Depends} in debian/control.
* List gettext translations in bear-factory.install and plee-the-bear.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Bear Engine
 
3
 
 
4
  Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
 
5
 
 
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.
 
10
 
 
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
 
14
  more details.
 
15
 
 
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
 
19
 
 
20
  contact: plee-the-bear@gamned.org
 
21
 
 
22
  Please add the tag [Bear] in the subject of your mails.
 
23
*/
 
24
/**
 
25
 * \file line.cpp
 
26
 * \brief Implementation of the bear::line class.
 
27
 * \author Julien Jorge
 
28
 */
 
29
#include "generic_items/line.hpp"
 
30
 
 
31
#include "visual/scene_line.hpp"
 
32
 
 
33
#include <limits>
 
34
 
 
35
BASE_ITEM_EXPORT(line, bear)
 
36
 
 
37
/*----------------------------------------------------------------------------*/
 
38
/**
 
39
 * \brief Constructor.
 
40
 */
 
41
bear::line::line()
 
42
: m_width(1)
 
43
{
 
44
  set_phantom(true);
 
45
  set_can_move_items(false);
 
46
  set_artificial(true);
 
47
} // line::line()
 
48
 
 
49
/*----------------------------------------------------------------------------*/
 
50
/**
 
51
 * \brief Constructor.
 
52
 * \param that The instance of the parent class from which we initialise.
 
53
 */
 
54
bear::line::line( const super& that )
 
55
  : super(that), m_width(1)
 
56
{
 
57
  set_phantom(true);
 
58
  set_can_move_items(false);
 
59
  set_artificial(true);
 
60
} // line::line()
 
61
 
 
62
/*----------------------------------------------------------------------------*/
 
63
/**
 
64
 * \brief Initialise the item.
 
65
 */
 
66
void bear::line::build()
 
67
{
 
68
  super::build();
 
69
 
 
70
  adjust_position_and_size();
 
71
} // line::build()
 
72
 
 
73
/*----------------------------------------------------------------------------*/
 
74
/**
 
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.
 
78
 */
 
79
bool bear::line::set_real_field( const std::string& name, double value )
 
80
{
 
81
  bool result(true);
 
82
 
 
83
  if ( name == "line.width" )
 
84
    m_width = value;
 
85
  else
 
86
    result = super::set_real_field(name, value);
 
87
 
 
88
  return result;
 
89
} // line::set_real_field()
 
90
 
 
91
/*----------------------------------------------------------------------------*/
 
92
/**
 
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.
 
96
 */
 
97
bool bear::line::set_item_list_field
 
98
( const std::string& name, const std::vector<engine::base_item*>& value )
 
99
{
 
100
  bool result(true);
 
101
 
 
102
  if ( name == "line.ends" )
 
103
    m_points = point_list_type(value.begin(), value.end());
 
104
  else
 
105
    result = super::set_item_list_field(name, value);
 
106
 
 
107
  return result;
 
108
} // line::set_item_list_field()
 
109
 
 
110
/*----------------------------------------------------------------------------*/
 
111
/**
 
112
 * \brief Do one step in the progression of the item.
 
113
 * \param elapsed_time Elapsed time since the last call.
 
114
 */
 
115
void bear::line::progress( universe::time_type elapsed_time )
 
116
{
 
117
  super::progress(elapsed_time);
 
118
 
 
119
  adjust_position_and_size();
 
120
} // line::progress()
 
121
 
 
122
/*----------------------------------------------------------------------------*/
 
123
/**
 
124
 * \brief Get the visual of the item.
 
125
 * \param visual (out) The visual representation of the item.
 
126
 */
 
127
void bear::line::get_visual( std::list<engine::scene_visual>& visuals ) const
 
128
{
 
129
  super::get_visual(visuals);
 
130
 
 
131
  if ( m_points.size() <= 1 )
 
132
    return;
 
133
 
 
134
  std::vector<visual::position_type> p;
 
135
  p.reserve(m_points.size());
 
136
 
 
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() );
 
140
 
 
141
  engine::scene_visual v
 
142
    ( get_scene_visual
 
143
      ( visual::scene_line
 
144
        ( 0, 0, claw::graphic::white_pixel, p, m_width) ) );
 
145
 
 
146
  v.scene_element.set_position( get_gap() );
 
147
  visuals.push_front(v);
 
148
} // line::get_visual()
 
149
 
 
150
/*----------------------------------------------------------------------------*/
 
151
/**
 
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.
 
154
 */
 
155
void bear::line::push_back( engine::base_item* item )
 
156
{
 
157
  m_points.push_back(item);
 
158
} // line::push_back()
 
159
 
 
160
/*----------------------------------------------------------------------------*/
 
161
/**
 
162
 * \brief Set the width of the line.
 
163
 * \param w The new width of the line.
 
164
 */
 
165
void bear::line::set_line_width( visual::size_type w )
 
166
{
 
167
  m_width = w;
 
168
} // line::set_line_width()
 
169
 
 
170
/*----------------------------------------------------------------------------*/
 
171
/**
 
172
 * \brief Adjust the position and the size of the item to fit the points.
 
173
 */
 
174
void bear::line::adjust_position_and_size()
 
175
{
 
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;
 
182
 
 
183
  for ( point_list_type::iterator it=m_points.begin(); it!=m_points.end(); )
 
184
    if ( *it == NULL )
 
185
      {
 
186
        point_list_type::iterator tmp(it);
 
187
        ++it;
 
188
        m_points.erase(tmp);
 
189
      }
 
190
    else
 
191
      {
 
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());
 
196
 
 
197
        ++it;
 
198
      }
 
199
 
 
200
  if ( !m_points.empty() )
 
201
    {
 
202
      set_bottom(bottom);
 
203
      set_left(left);
 
204
      set_size(right - left, top - bottom);
 
205
    }
 
206
} // line::adjust_position_and_size()