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

« back to all changes in this revision

Viewing changes to src/editor/surface_selector.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Goulais
  • Date: 2004-08-09 10:26:00 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040809102600-lg2q9lfars0q1p42
Tags: 0.6.0-8
Applied patch from Andreas Jochens (Closes: #263992)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: surface_selector.cxx,v 1.7 2003/02/19 09:50:36 grumbel Exp $
 
2
//
 
3
//  Pingus - A free Lemmings clone
 
4
//  Copyright (C) 2000 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 <ClanLib/Core/System/system.h>
 
22
#include <ClanLib/Display/Display/display.h>
 
23
#include <ClanLib/Display/Input/mouse.h>
 
24
#include "../gui/display.hxx"
 
25
#include "../pingus_resource.hxx"
 
26
#include "surface_selector.hxx"
 
27
 
 
28
SurfaceSelector::SurfaceSelector (std::vector<surface_obj>* s)
 
29
  : font(PingusResource::load_font("Fonts/courier_small", "fonts")),
 
30
    sur_list(s),
 
31
    y_of(0),
 
32
    width(CL_Display::get_width() - (CL_Display::get_width() % 50)),
 
33
    height((sur_list->size() / (CL_Display::get_width() / 50)) * 50),
 
34
    c_obj(std::vector<surface_obj>::iterator())
 
35
{
 
36
}
 
37
 
 
38
SurfaceSelector::~SurfaceSelector ()
 
39
{
 
40
  
 
41
}
 
42
 
 
43
std::vector<surface_obj>::iterator
 
44
SurfaceSelector::get_current_obj ()
 
45
{
 
46
  int x = 0;
 
47
  int y = -y_of;
 
48
  
 
49
  for(std::vector<surface_obj>::iterator i = sur_list->begin(); 
 
50
      i != sur_list->end(); 
 
51
      ++i)
 
52
    {
 
53
      if (CL_Mouse::get_x() > x && CL_Mouse::get_x() <= x + 50
 
54
          && CL_Mouse::get_y() > y && CL_Mouse::get_y() <= y + 50)
 
55
        {
 
56
          return i;
 
57
        }
 
58
      
 
59
      x += 50;
 
60
      if (x + 50 > CL_Display::get_width())
 
61
        {
 
62
          y += 50;
 
63
          x = 0;
 
64
        }
 
65
    }
 
66
 
 
67
  return std::vector<surface_obj>::iterator();
 
68
}
 
69
 
 
70
void
 
71
SurfaceSelector::draw ()
 
72
{
 
73
  // FIXME: This could heavily optimized if ClanLib would have a put_target(x,y,w,h)
 
74
  std::vector<surface_obj>::iterator tmp_obj = get_current_obj();
 
75
  
 
76
  if (c_obj != tmp_obj)
 
77
    {
 
78
      if (tmp_obj != std::vector<surface_obj>::iterator())
 
79
        tmp_obj->display_time = CL_System::get_time();
 
80
      
 
81
      if (c_obj != std::vector<surface_obj>::iterator())
 
82
        c_obj->display_time = 0;
 
83
    }
 
84
  
 
85
  c_obj = tmp_obj;
 
86
 
 
87
  int x = 0;
 
88
  int y = -y_of;
 
89
 
 
90
  CL_System::keep_alive();
 
91
 
 
92
  CL_Display::clear_display();
 
93
 
 
94
  // Draw all surfaces
 
95
  for(std::vector<surface_obj>::iterator i = sur_list->begin(); i != sur_list->end(); ++i)
 
96
    {
 
97
      if (i->thumbnail.get_width() <= 50 && i->thumbnail.get_height() <= 50)
 
98
        {
 
99
          i->thumbnail.put_screen(x + 25 - (i->thumbnail.get_width()  / 2), 
 
100
                                  y + 25 - (i->thumbnail.get_height() / 2));
 
101
        }
 
102
      else
 
103
        {
 
104
          i->thumbnail.put_screen(x, y);
 
105
        }
 
106
 
 
107
      if (i == c_obj)
 
108
        {
 
109
          Display::draw_rect(x, y, x + 50, y + 50, 1.0, 1.0, 1.0, 1.0);
 
110
        }
 
111
 
 
112
      x += 50;
 
113
      if (x + 50 > CL_Display::get_width()) 
 
114
        {
 
115
          y += 50;
 
116
          x = 0;
 
117
        }
 
118
    }
 
119
 
 
120
  //Display::draw_rect(CL_Display::get_width() - 16, y/10,
 
121
  //CL_Display::get_width(), y+1/10,
 
122
  //1.0, 1.0, 1.0, 1.0);
 
123
 
 
124
  // Draw the current object in the bottom/left corner when the
 
125
  // surface is selected for more then 1sec
 
126
  if (c_obj != std::vector<surface_obj>::iterator()
 
127
      && (c_obj->display_time + 350 < CL_System::get_time ()
 
128
          || c_obj->large_sur))
 
129
    {
 
130
      if (!c_obj->large_sur)
 
131
        {
 
132
          std::cout << "Loading: " << c_obj->name << " " << c_obj->datafile << std::endl;
 
133
          c_obj->large_sur = PingusResource::load_surface (c_obj->name, c_obj->datafile);
 
134
        }
 
135
 
 
136
      CL_Display::fill_rect(0, CL_Display::get_height() - c_obj->large_sur.get_height(),
 
137
                            c_obj->large_sur.get_width(), CL_Display::get_height(),
 
138
                            0.5f, 0.5f, 0.5f, 0.8f);
 
139
      c_obj->large_sur.put_screen(0, CL_Display::get_height() - c_obj->large_sur.get_height());
 
140
    }
 
141
  Display::flip_display();
 
142
}
 
143
 
 
144
void
 
145
SurfaceSelector::scroll ()
 
146
{
 
147
  const int range = 100;
 
148
 
 
149
  if (CL_Mouse::get_y() > CL_Display::get_height() - range)
 
150
    {
 
151
      y_of += range - (CL_Display::get_height() - CL_Mouse::get_y());
 
152
    }
 
153
  else if (CL_Mouse::get_y() < range)
 
154
    {
 
155
      y_of -= range - CL_Mouse::get_y();
 
156
    }
 
157
  
 
158
  if (y_of < 0) y_of = 0;    
 
159
 
 
160
  if (y_of > height) y_of = height;
 
161
}
 
162
 
 
163
std::string
 
164
SurfaceSelector::select ()
 
165
{
 
166
  std::string str;
 
167
  std::vector<surface_obj>::iterator iter;
 
168
 
 
169
  while (!CL_Mouse::left_pressed())
 
170
    {
 
171
      draw();
 
172
 
 
173
      scroll();
 
174
      
 
175
      CL_System::keep_alive();
 
176
    } 
 
177
 
 
178
  iter = get_current_obj();
 
179
  
 
180
  if (iter != std::vector<surface_obj>::iterator())
 
181
    str = iter->name;
 
182
 
 
183
  std::cout << "str: " << str << std::endl;
 
184
 
 
185
  return str;
 
186
}
 
187
 
 
188
surface_obj::surface_obj ()
 
189
{
 
190
}
 
191
 
 
192
surface_obj::surface_obj (const surface_obj& old) : thumbnail(old.thumbnail),
 
193
                                                    large_sur(old.large_sur),
 
194
                                                    name(old.name),
 
195
                                                    datafile(old.datafile),
 
196
                                                    display_time(old.display_time)
 
197
{
 
198
}
 
199
 
 
200
surface_obj& surface_obj::operator= (const surface_obj& old)
 
201
{
 
202
  if (this != &old)
 
203
    {
 
204
      thumbnail    = old.thumbnail;
 
205
      large_sur    = old.large_sur;
 
206
      name         = old.name;
 
207
      datafile     = old.datafile;
 
208
      display_time = old.display_time;
 
209
    }
 
210
 
 
211
  return *this;
 
212
}
 
213
 
 
214
/* EOF */