1
// $Id: object_selector_list.cpp 3255 2007-09-28 21:52:01Z grumbel $
3
// Pingus - A free Lemmings clone
4
// Copyright (C) 2007 Ingo Ruhnke <grumbel@gmx.de>
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.
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.
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.
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"
34
ObjectSelectorList::ObjectSelectorList(EditorScreen* editor_, ObjectSelector* object_selector_, const Rect& rect_)
35
: RectComponent(rect_),
37
object_selector(object_selector_),
38
drawing_context(new DrawingContext(rect, true)),
48
ObjectSelectorList::~ObjectSelectorList()
50
delete drawing_context;
54
ObjectSelectorList::draw(DrawingContext& parent_gc)
56
DrawingContext& gc = *drawing_context;
58
gc.fill_screen(Color(100, 100, 100));
63
gc.translate(0, offset);
65
for(Objects::const_iterator i = set->get_objects().begin(); i != set->get_objects().end(); ++i)
67
int x = (i - set->get_objects().begin()) % 5;
68
int y = (i - set->get_objects().begin()) / 5;
70
gc.draw((*i)->thumbnail, Vector2i(x * 48, y * 48));
72
gc.draw_rect(x * 48, y * 48,
73
x * 48 + 48, y * 48 + 48,
76
if (has_mouse_over() && current_object != -1 && (i - set->get_objects().begin()) == current_object)
78
gc.draw_fillrect(x * 48, y * 48,
79
x * 48 + 48, y * 48 + 48,
80
Color(255,255,255, 100));
82
gc.draw_rect(x * 48, y * 48,
83
x * 48 + 48, y * 48 + 48,
92
if (set && mode == OBJECT_DRAG)
94
Sprite sprite = set->get_objects()[current_object]->sprite;
95
parent_gc.draw(sprite,
97
- Vector2i(sprite.get_width()/2, sprite.get_height()/2)
98
+ sprite.get_offset(),
104
ObjectSelectorList::on_primary_button_press (int x, int y)
108
if (mode == NOTHING && current_object != -1)
110
drag_object = current_object;
116
ObjectSelectorList::on_primary_button_release (int x, int y)
120
if (mode == OBJECT_DRAG)
124
if (current_object != -1)
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)))
133
Vector2i p = editor->get_viewport()->screen2world(x + object_selector->get_rect().left,
134
y + object_selector->get_rect().top);
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);
140
p += set->get_objects()[current_object]->sprite.get_offset();
142
LevelObj* obj = set->get_objects()[current_object]->create(p, editor->get_level()->get_level_impl());
144
editor->add_object(obj);
146
std::cout << "ObjectSelectorList::Object: create() not implemented" << std::endl;
153
ObjectSelectorList::on_secondary_button_press (int x, int y)
159
drag_start = Vector2i(x,y);
166
ObjectSelectorList::on_secondary_button_release (int x, int y)
170
if (mode == SCROLLING)
175
ObjectSelectorList::on_pointer_move (int x, int y)
179
real_mouse_pos = Vector2i(x, y);
181
mouse_pos = Vector2i(x - rect.left, y - rect.top);
184
int height = (set->get_objects().size() / width) + ((set->get_objects().size() % width > 0) ? 1 : 0);
186
if (mode != OBJECT_DRAG)
188
if (!set->get_objects().empty())
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);
193
current_object = Math::clamp(-1, (obj_y * 5) + obj_x, int(set->get_objects().size()-1));
197
if (mode == SCROLLING)
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);
205
ObjectSelectorList::set_objects(ObjectSelectorSet* new_set)
208
set->set_offset(offset);
211
offset = new_set->get_offset();
221
ObjectSelectorList::update_layout()
223
drawing_context->set_rect(rect);
226
} // namespace Editor