1
// $Id: EditorView.cc,v 1.10 2002/01/15 10:48:51 grumbel Exp $
3
// Pingus - A free Lemmings clone
4
// Copyright (C) 2000 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.
20
#include "../Sprite.hh"
21
#include "EditorView.hh"
23
EditorView::EditorView (int x1, int y1, int x2, int y2,
24
int x_offset, int y_offset)
25
: x1 (x1), y1 (y1), x2 (x2), y2 (y2), offset (-(x2 - x1)/2.0f, -(y2-x1)/2.0f, 1.0f)
27
center = CL_Vector ((x2 - x1)/2.0f + x1,
29
std::cout << "View: " << x1 << ", " << y1 << ", " << x2 << ", " << y2
33
EditorView::~EditorView ()
38
EditorView::get_offset ()
44
EditorView::get_zoom ()
50
EditorView::set_zoom (float new_zoom)
53
//std::cout << "Zoom: " << offset.z << std::endl;
57
EditorView::zoom_to (CL_Rect arg_rect)
61
rect.x1 = min (arg_rect.x1, arg_rect.x2);
62
rect.x2 = max (arg_rect.x1, arg_rect.x2);
63
rect.y1 = min (arg_rect.y1, arg_rect.y2);
64
rect.y2 = max (arg_rect.y1, arg_rect.y2);
66
CL_Vector pos1 = screen_to_world (CL_Vector(rect.x1, rect.y1));
67
CL_Vector pos2 = screen_to_world (CL_Vector(rect.x2, rect.y2));
69
CL_Vector center = (pos2 + pos1) * 0.5f;
72
float width = pos2.x - pos1.x;
73
float height = pos2.y - pos1.y;
75
if (width < 10 && height < 10)
78
float screen_relation = get_width () / get_height ();
79
float rect_reation = width / height;
81
if (rect_reation > screen_relation)
83
set_zoom (get_width () / (pos2.x - pos1.x));
87
set_zoom (get_height () / (pos2.y - pos1.y));
92
EditorView::get_width ()
98
EditorView::get_height ()
104
EditorView::move (CL_Vector delta)
110
EditorView::screen_to_world (CL_Vector pos)
112
return ((pos - center) * (1.0f/offset.z)) - offset;
116
EditorView::world_to_screen (CL_Vector pos)
118
return ((pos + offset) * offset.z) + center;
122
EditorView::get_x_offset ()
128
EditorView::get_y_offset ()
134
EditorView::draw (Sprite& sprite, const CL_Vector& pos)
136
CL_Surface sur (sprite.get_surface ());
138
(int) pos.x + sprite.get_x_align (),
139
(int) pos.y + sprite.get_y_align ());
143
EditorView::draw (Sprite& sprite, const CL_Vector& pos, int frame)
145
CL_Surface sur (sprite.get_surface ());
147
(int) pos.x + sprite.get_x_align (),
148
(int) pos.y + sprite.get_y_align (),
153
EditorView::draw (CL_Surface& sur, const CL_Vector& pos)
157
sur.put_screen (int(pos.x + get_x_offset () + center.x),
158
int(pos.y + get_y_offset () + center.y));
162
sur.put_screen (int((pos.x + get_x_offset ()) * offset.z + center.x),
163
int((pos.y + get_y_offset ()) * offset.z + center.y),
166
//CL_Display::draw_line (x1, y1, x2, y2, 1.0, 1.0, 0.0);
167
//CL_Display::draw_line (x1, y2, x2, y1, 1.0, 1.0, 0.0);
171
EditorView::draw (CL_Surface& sur, const CL_Vector& pos, int frame)
173
draw (sur, int(pos.x), int(pos.y), frame);
177
EditorView::draw (CL_Surface& sur, int x_pos, int y_pos)
181
sur.put_screen (int(x_pos + get_x_offset () + center.x),
182
int(y_pos + get_y_offset () + center.y));
186
sur.put_screen (int((x_pos + get_x_offset ()) * offset.z + center.x),
187
int((y_pos + get_y_offset ()) * offset.z + center.y),
193
EditorView::draw (CL_Surface& sur, int x_pos, int y_pos, int frame)
197
sur.put_screen (int(x_pos + get_x_offset () + center.x),
198
int(y_pos + get_y_offset () + center.y),
203
sur.put_screen (int((x_pos + get_x_offset ()) * offset.z + center.x),
204
int((y_pos + get_y_offset ()) * offset.z + center.y),
211
EditorView::draw (CL_Surface& sur, int x_pos, int y_pos,
212
float size_x, float size_y, int frame)
214
sur.put_screen (int(x_pos + get_x_offset () + center.x),
215
int(y_pos + get_y_offset () + center.y),
217
size_y * offset.z, frame);
221
EditorView::draw_line (const CL_Vector& pos1, const CL_Vector& pos2,
222
float r, float g, float b, float a)
224
draw_line (int(pos1.x), int(pos1.y), int(pos2.x), int(pos2.y), r, g, b, a);
228
EditorView::draw_line (int x1, int y1, int x2, int y2,
229
float r, float g, float b, float a)
231
CL_Display::draw_line (int((x1 + get_x_offset ()) * offset.z + center.x),
232
int((y1 + get_y_offset ()) * offset.z + center.y),
233
int((x2 + get_x_offset ()) * offset.z + center.x),
234
int((y2 + get_y_offset ()) * offset.z + center.y),
239
EditorView::draw_fillrect (int x1, int y1, int x2, int y2,
240
float r, float g, float b, float a)
242
CL_Display::fill_rect (int((x1 + get_x_offset ()) * offset.z + center.x),
243
int((y1 + get_y_offset ()) * offset.z + center.y),
244
int((x2 + get_x_offset ()) * offset.z + center.x),
245
int((y2 + get_y_offset ()) * offset.z + center.y),
250
EditorView::draw_rect (int x1, int y1, int x2, int y2,
251
float r, float g, float b, float a)
253
CL_Display::draw_rect (int((x1 + get_x_offset ()) * offset.z + center.x),
254
int((y1 + get_y_offset ()) * offset.z + center.y),
255
int((x2 + get_x_offset ()) * offset.z + center.x),
256
int((y2 + get_y_offset ()) * offset.z + center.y),
261
EditorView::draw_pixel (int x_pos, int y_pos,
262
float r, float g, float b, float a)
264
//CL_Display::put_pixel (x1 + get_x_offset (),
265
// y1 + get_y_offset (), r, g, b, a);
266
std::cout << "View::draw_pixel () not implemented" << std::endl;
270
EditorView::draw_circle (int x_pos, int y_pos, int radius,
271
float r, float g, float b, float a)
273
// FIXME: Probally not the fast circle draw algo on this world...
274
const float pi = 3.1415927f * 2.0f;
275
const float steps = 8;
276
CL_Vector current (radius, 0);
277
CL_Vector next = current.rotate (pi/steps, CL_Vector (0, 0, 1.0f));
279
for (int i = 0; i < steps; ++i)
281
draw_line (int(x_pos + current.x), int(y_pos + current.y),
282
int(x_pos + next.x), int(y_pos + next.y),
285
next = next.rotate (pi/8, CL_Vector (0, 0, 1.0f));