~ubuntu-branches/ubuntu/precise/pingus/precise

« back to all changes in this revision

Viewing changes to src/editor/object_selector_list.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-02-28 19:44:25 UTC
  • mfrom: (4.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20080228194425-e8ilohlijv02kgcf
Tags: 0.7.2-2
* Fix FTBFS with gcc-4.3 by adding the missing include in
  src/input/evdev_device.cpp (Closes: #462238):
   + debian/patches/20_fix_FTBFS_with_gcc-4.3.
* Rename former patch so that the filename reflects the order in which
  the patches are applied:
   - debian/patches/data_dir.patch
   + debian/patches/10_fix_data_directory.
* Bump Standards-Version from 3.7.2 to 3.7.3, no changes needed.
* Add a dh_desktop call in the arch-dep part of debian/rules.
* Adjust the “missing-dep-for-interpreter guile” override since lintian
  now lists an alternative for that dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: object_selector_list.cpp 3255 2007-09-28 21:52:01Z grumbel $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2007 Ingo Ruhnke <grumbel@gmx.de>
 
5
//
 
6
//  This program is free software; you can redistribute it and/or
 
7
//  modify it under the terms of the GNU General Public License
 
8
//  as published by the Free Software Foundation; either version 2
 
9
//  of the License, or (at your option) any later version.
 
10
//
 
11
//  This program is distributed in the hope that it will be useful,
 
12
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//  GNU General Public License for more details.
 
15
//
 
16
//  You should have received a copy of the GNU General Public License
 
17
//  along with this program; if not, write to the Free Software
 
18
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
#include <iostream>
 
21
#include "math.hpp"
 
22
#include "editor_screen.hpp"
 
23
#include "object_selector_set.hpp"
 
24
#include "object_selector.hpp"
 
25
#include "editor_viewport.hpp"
 
26
#include "editor_level.hpp"
 
27
#include "groundtype.hpp"
 
28
#include "resource.hpp"
 
29
#include "display/drawing_context.hpp"
 
30
#include "object_selector_list.hpp"
 
31
 
 
32
namespace Editor {
 
33
 
 
34
ObjectSelectorList::ObjectSelectorList(EditorScreen* editor_, ObjectSelector* object_selector_, const Rect& rect_)
 
35
  : RectComponent(rect_),
 
36
    editor(editor_),
 
37
    object_selector(object_selector_),
 
38
    drawing_context(new DrawingContext(rect, true)),
 
39
    offset(0),
 
40
    old_offset(0),
 
41
    mode(NOTHING),
 
42
    current_object(-1),
 
43
    drag_object(-1),
 
44
    set(0)
 
45
{  
 
46
}
 
47
 
 
48
ObjectSelectorList::~ObjectSelectorList()
 
49
{
 
50
  delete drawing_context;
 
51
}
 
52
 
 
53
void
 
54
ObjectSelectorList::draw(DrawingContext& parent_gc)
 
55
{
 
56
  DrawingContext& gc = *drawing_context;
 
57
  gc.clear();
 
58
  gc.fill_screen(Color(100, 100, 100));
 
59
 
 
60
  if (set)
 
61
    {
 
62
      gc.push_modelview();
 
63
      gc.translate(0, offset);
 
64
 
 
65
      for(Objects::const_iterator i = set->get_objects().begin(); i != set->get_objects().end(); ++i)
 
66
        {
 
67
          int x = (i - set->get_objects().begin()) % 5;
 
68
          int y = (i - set->get_objects().begin()) / 5;
 
69
 
 
70
          gc.draw((*i)->thumbnail, Vector2i(x * 48, y * 48));
 
71
 
 
72
          gc.draw_rect(x * 48,      y * 48, 
 
73
                       x * 48 + 48, y * 48 + 48, 
 
74
                       Color(155,155,155));
 
75
 
 
76
          if (has_mouse_over() && current_object != -1 && (i - set->get_objects().begin()) == current_object)
 
77
            {
 
78
              gc.draw_fillrect(x * 48,      y * 48, 
 
79
                               x * 48 + 48, y * 48 + 48, 
 
80
                               Color(255,255,255, 100));
 
81
 
 
82
              gc.draw_rect(x * 48,      y * 48, 
 
83
                           x * 48 + 48, y * 48 + 48, 
 
84
                           Color(255,255,255));
 
85
            }
 
86
        }
 
87
  
 
88
      gc.pop_modelview();
 
89
    }
 
90
  parent_gc.draw(gc);
 
91
 
 
92
  if (set && mode == OBJECT_DRAG)
 
93
    {
 
94
      Sprite sprite = set->get_objects()[current_object]->sprite;
 
95
      parent_gc.draw(sprite,
 
96
                     real_mouse_pos
 
97
                     - Vector2i(sprite.get_width()/2, sprite.get_height()/2)
 
98
                     + sprite.get_offset(), 
 
99
                     2000.0f);
 
100
    }
 
101
}
 
102
 
 
103
void
 
104
ObjectSelectorList::on_primary_button_press (int x, int y)
 
105
{
 
106
  if (!set) return;
 
107
 
 
108
  if (mode == NOTHING && current_object != -1)
 
109
    {
 
110
      drag_object = current_object;
 
111
      mode = OBJECT_DRAG;
 
112
    }
 
113
}
 
114
 
 
115
void
 
116
ObjectSelectorList::on_primary_button_release (int x, int y)
 
117
{
 
118
  if (!set) return;
 
119
 
 
120
  if (mode == OBJECT_DRAG)
 
121
    {
 
122
      mode = NOTHING;
 
123
      
 
124
      if (current_object != -1)
 
125
        {
 
126
          // FIXME: Should check if the current mouse over component
 
127
          // is the Viewport, else no drag should take place, this
 
128
          // checks if the current mouse_over_comp is the
 
129
          // ObjectSelector, which is good enough but not perfect 
 
130
          if (!object_selector->get_rect().is_inside(Vector2i(x + object_selector->get_rect().left,
 
131
                                                              y + object_selector->get_rect().top)))
 
132
            {
 
133
              Vector2i p = editor->get_viewport()->screen2world(x + object_selector->get_rect().left,
 
134
                                                                y + object_selector->get_rect().top);
 
135
 
 
136
              // place object with left/top instead of center origin
 
137
              p -= Vector2i(set->get_objects()[current_object]->sprite.get_width()/2,
 
138
                            set->get_objects()[current_object]->sprite.get_height()/2);
 
139
 
 
140
              p += set->get_objects()[current_object]->sprite.get_offset();
 
141
 
 
142
              LevelObj* obj = set->get_objects()[current_object]->create(p, editor->get_level()->get_level_impl());
 
143
              if (obj)
 
144
                editor->add_object(obj);
 
145
              else
 
146
                std::cout << "ObjectSelectorList::Object: create() not implemented" << std::endl;
 
147
            }
 
148
        }
 
149
    }
 
150
}
 
151
 
 
152
void
 
153
ObjectSelectorList::on_secondary_button_press (int x, int y)
 
154
{
 
155
  if (!set) return;
 
156
 
 
157
  if (mode == NOTHING)
 
158
    {
 
159
      drag_start = Vector2i(x,y);
 
160
      mode = SCROLLING;
 
161
      old_offset = offset;
 
162
    }
 
163
}
 
164
 
 
165
void
 
166
ObjectSelectorList::on_secondary_button_release (int x, int y)
 
167
{  
 
168
  if (!set) return;
 
169
 
 
170
  if (mode == SCROLLING)
 
171
    mode = NOTHING;
 
172
}
 
173
 
 
174
void
 
175
ObjectSelectorList::on_pointer_move (int x, int y)
 
176
{
 
177
  if (!set) return;
 
178
 
 
179
  real_mouse_pos = Vector2i(x, y);
 
180
 
 
181
  mouse_pos = Vector2i(x - rect.left, y - rect.top);
 
182
 
 
183
  int width = 5;
 
184
  int height = (set->get_objects().size() / width) + ((set->get_objects().size() % width > 0) ? 1 : 0);
 
185
 
 
186
  if (mode != OBJECT_DRAG)
 
187
    {
 
188
      if (!set->get_objects().empty())
 
189
        {
 
190
          int obj_x = Math::clamp(0, mouse_pos.x / 48, width - 1);
 
191
          int obj_y = Math::clamp(0, int(mouse_pos.y - offset) / 48, height-1);
 
192
 
 
193
          current_object = Math::clamp(-1, (obj_y * 5) + obj_x, int(set->get_objects().size()-1));
 
194
        }
 
195
    }
 
196
 
 
197
  if (mode == SCROLLING)
 
198
    {
 
199
      offset = old_offset + (y - drag_start.y);
 
200
      offset = Math::clamp(Math::min(rect.get_height() - (height * 48.0f), 0.0f), offset, 0.0f);
 
201
    }
 
202
}
 
203
 
 
204
void
 
205
ObjectSelectorList::set_objects(ObjectSelectorSet* new_set)
 
206
{
 
207
  if (set)
 
208
    set->set_offset(offset);
 
209
 
 
210
  if (new_set)
 
211
    offset = new_set->get_offset();
 
212
  else
 
213
    offset = 0.0f;
 
214
 
 
215
  set = new_set;
 
216
 
 
217
  current_object = -1;
 
218
}
 
219
 
 
220
void
 
221
ObjectSelectorList::update_layout()
 
222
{
 
223
  drawing_context->set_rect(rect);
 
224
}
 
225
 
 
226
} // namespace Editor
 
227
 
 
228
/* EOF */