File: weapon_menu.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (431 lines) | stat: -rw-r--r-- 13,711 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Interface showing various informations about the game.
 *****************************************************************************/

#include <sstream>

#include "game/config.h"
#include "game/game_time.h"
#include "game/game.h"
#include "graphic/font.h"
#include "graphic/polygon_generator.h"
#include "graphic/sprite.h"
#include "graphic/video.h"
#include "include/action_handler.h"
#include "include/app.h"
#include "interface/interface.h"
#include "interface/mouse.h"
#include "interface/weapon_menu.h"
#include "map/maps_list.h"
#include "team/team.h"
#include "team/teams_list.h"
#include "tool/resource_manager.h"
#include "sound/jukebox.h"
#include "weapon/weapon.h"
#include "weapon/weapons_list.h"

// Weapon menu
const uint ICONS_DRAW_TIME = 400;       // Time to display all icons (in ms)
const uint BLING_ICONS_DRAW_TIME = 600; // Bling bling version

const uint ICON_ZOOM_TIME = 150;        // Time to zoom one icon.

const uint JELLY_TIME = 0;              // Jelly time when appearing
const uint BLING_JELLY_TIME = 300;      // Bling bling version

const uint ROTATION_TIME = 0;           // Number of rotation
const uint BLING_ROTATION_TIME = 2;     // bling bling !

const Double DEFAULT_ICON_SCALE = 0.7;
const Double MAX_ICON_SCALE = 1.1;

const int WeaponsMenu::MAX_NUMBER_OF_WEAPON = 7;

#define MAX_ICON_SIZE          45

WeaponMenuItem::WeaponMenuItem(Weapon * new_weapon, const Point2d & position)
  : PolygonItem()
  , zoom(false)
  , weapon(new_weapon)
  , zoom_start_time(0)
{
  SetSprite(new Sprite(weapon->GetIcon()));
  SetPosition(position);
  SetZoomTime(ICON_ZOOM_TIME);
}

bool WeaponMenuItem::IsMouseOver()
{
  if (!ActiveTeam().ReadNbAmmos(weapon->GetType())) {
    if (zoom)
      SetZoom(false);
    return false;
  }

  // Compute the size of the icon bounding box
  Point2i size(MAX_ICON_SIZE, MAX_ICON_SIZE);

  // The icon bounding box for this is centered around the transformed position
  Rectanglei r(transformed_position + 1 - size/2, size);
  if (r.Contains(Mouse::GetInstance()->GetPosition())) {
    if (!zoom)
      SetZoom(true);
    return true;
  }
  if (zoom)
    SetZoom(false);
  return false;
}

void WeaponMenuItem::SetZoom(bool value)
{
  zoom = value;
  zoom_start_time = GameTime::GetInstance()->Read();
}

void WeaponMenuItem::Draw(Surface * dest)
{
  Double scale = DEFAULT_ICON_SCALE;
  if (zoom || zoom_start_time + GetZoomTime() > GameTime::GetInstance()->Read()) {
    scale = (GameTime::GetInstance()->Read() - zoom_start_time) / (Double)GetZoomTime();
    if (zoom) {
      scale = DEFAULT_ICON_SCALE + (MAX_ICON_SCALE - DEFAULT_ICON_SCALE) * scale;
      scale = (scale > MAX_ICON_SCALE ? MAX_ICON_SCALE : scale);
    } else {
      scale = MAX_ICON_SCALE - (MAX_ICON_SCALE - DEFAULT_ICON_SCALE) * scale;
      scale = (scale > DEFAULT_ICON_SCALE ? scale : DEFAULT_ICON_SCALE);
    }
  }
  item->SetAlpha(1);
  item->Scale(scale, scale);

  int nb_bullets = ActiveTeam().ReadNbAmmos(weapon->GetType());
  Point2i tmp = GetOffsetAlignment() + Point2i(0, item->GetWidth() - 10);

  char buffer[5] = { 0, };
  if (nb_bullets ==  INFINITE_AMMO) {
#define UTF8_INFINITE "\xE2\x88\x9E"
    PolygonItem::Draw(dest);
    Font::GetInstance(Font::FONT_MEDIUM, Font::FONT_BOLD)->WriteLeft(tmp, UTF8_INFINITE, dark_gray_color);
  } else if (nb_bullets == 0) {
      int num = weapon->AvailableAfterTurn() - (int)Game::GetInstance()->GetCurrentTurn();
      if (num > -1){
        PolygonItem::Draw(dest);
        tmp.y -= 4;
        m_parent->m_not_yet_available->Blit(*dest, tmp);

        tmp.x += m_parent->m_not_yet_available->GetWidth()-5;
        tmp.y += 10;
        snprintf(buffer, 4, "%i ", num);
        Font::GetInstance(Font::FONT_SMALL, Font::FONT_BOLD)->WriteLeft(tmp, buffer, dark_red_color);
      } else {
        item->SetAlpha(0.3);
        PolygonItem::Draw(dest);
      }
  } else {
    PolygonItem::Draw(dest);
    std::ostringstream txt;
    snprintf(buffer, 4, "%i ", nb_bullets);
    Font::GetInstance(Font::FONT_MEDIUM, Font::FONT_BOLD)->WriteLeft(tmp, buffer, dark_gray_color);
  }
}


WeaponsMenu::WeaponsMenu()
  : m_not_yet_available(NULL)
  , weapons_menu(NULL)
  , tools_menu(NULL)
  , current_overfly_item(NULL)
  , position()
  , shear()
  , rotation()
  , zoom()
  , show(false)
  , motion_start_time(0)
  , icons_draw_time(ICONS_DRAW_TIME)
  , jelly_time(JELLY_TIME)
  , rotation_time(ROTATION_TIME)
  , nbr_weapon_type(0)
  , nb_weapon_type(new int[MAX_NUMBER_OF_WEAPON])
  , old_pointer(Mouse::POINTER_SELECT)
{
  // Loading value from XML
  Profile *res = GetResourceManager().LoadXMLProfile("graphism.xml", false);
  m_not_yet_available = LOAD_RES_SPRITE("interface/hourglass");

  // Polygon Size
  Point2i size = LOAD_RES_POINT("interface/weapons_interface_size");
  weapons_menu = PolygonGenerator::GenerateDecoratedBox(size.x, size.y);
  size = LOAD_RES_POINT("interface/tools_interface_size");
  tools_menu = PolygonGenerator::GenerateDecoratedBox(size.x, size.y);
  // Setting colors
  Color plane_color = LOAD_RES_COLOR("interface/background_color");
  Color border_color = LOAD_RES_COLOR("interface/border_color");
  weapons_menu->SetPlaneColor(plane_color);
  weapons_menu->SetBorderColor(border_color);
  tools_menu->SetPlaneColor(plane_color);
  tools_menu->SetBorderColor(border_color);
}

WeaponsMenu::~WeaponsMenu()
{
  if (weapons_menu)
    delete weapons_menu;
  if (tools_menu)
    delete tools_menu;
  if (m_not_yet_available)
    delete m_not_yet_available;
  if (nb_weapon_type)
    delete[] nb_weapon_type;
}

// Add a new weapon to the weapon menu.
void WeaponsMenu::AddWeapon(Weapon* new_item)
{
  if (!new_item->CanBeUsedOnClosedMap() && !ActiveMap()->LoadedInfo()->IsOpened())
    return;

  Point2f pos;
  Weapon::category_t num_sort = new_item->Category();
  Polygon *menu;

  if (num_sort < 6) {
    menu = weapons_menu;
    pos  = Point2f(nb_weapon_type[num_sort-1], num_sort-1)*MAX_ICON_SIZE;
  } else {
    menu = tools_menu;
    pos  = Point2f(num_sort-6, nb_weapon_type[num_sort-1])*MAX_ICON_SIZE;
  }
  pos += P2D_TO_P2F(menu->GetMin()) + Point2f(30,25);

  WeaponMenuItem * item = new WeaponMenuItem(new_item, pos);
  item->SetParent(this);
  menu->AddItem(item);

  nb_weapon_type[num_sort - 1]++;
}

// Weapon menu display (init of the animation)
void WeaponsMenu::Show(const Point2i& pos)
{
  if (!ActiveTeam().GetWeapon().CanChangeWeapon())
    return;

  Surface& window = GetMainWindow();
  uint scroll_border = 0;
  
  if (Config::GetInstance()->GetScrollOnBorder()) {
    scroll_border = Config::GetInstance()->GetScrollBorderSize();
  }

  click_pos = pos;
  int p = 5 + scroll_border;
  if (click_pos.x < p)
    click_pos.x = p;
  if (click_pos.y < p)
    click_pos.y = window.GetHeight(); // Force moving to bottom
  p = window.GetWidth()-10
    - (weapons_menu->GetWidth() + 10 + tools_menu->GetWidth());
  if (click_pos.x > p)
    click_pos.x = p;
  p = window.GetHeight() - Interface::GetRef().GetMenuHeight()
    - weapons_menu->GetHeight() - 10;
  if (click_pos.y > p)
    click_pos.y = p;

  ShowGameInterface();
  if (!show) {
    if (motion_start_time + GetIconsDrawTime() < GameTime::GetInstance()->Read())
      motion_start_time = GameTime::GetInstance()->Read();
    else
      motion_start_time = GameTime::GetInstance()->Read() - (GetIconsDrawTime() - (GameTime::GetInstance()->Read() - motion_start_time));
    show = true;

    JukeBox::GetInstance()->Play("default", "menu/weapon_menu_show");

    old_pointer = Mouse::GetInstance()->SetPointer(Mouse::POINTER_SELECT);
  }
}

void WeaponsMenu::Hide(bool play_sound)
{
  if (show) {
    Interface::GetInstance()->SetCurrentOverflyWeapon(NULL);
    if (motion_start_time + GetIconsDrawTime() < GameTime::GetInstance()->Read())
      motion_start_time = GameTime::GetInstance()->Read();
    else
      motion_start_time = GameTime::GetInstance()->Read() - (GetIconsDrawTime() - (GameTime::GetInstance()->Read() - motion_start_time));
    show = false;

    if (play_sound)
      JukeBox::GetInstance()->Play("default", "menu/weapon_menu_hide");

    Mouse::GetInstance()->SetPointer(old_pointer);
  }
}

void WeaponsMenu::Reset()
{
  Interface::GetInstance()->SetCurrentOverflyWeapon(NULL);
  RefreshWeaponList();
  motion_start_time = 0;
  show = false;
  if (Config::GetInstance()->IsBlingBlingInterface()) {
    SetJellyTime(BLING_JELLY_TIME);
    SetIconsDrawTime(BLING_ICONS_DRAW_TIME);
    SetRotationTime(BLING_ROTATION_TIME);
  } else {
    SetJellyTime(JELLY_TIME);
    SetIconsDrawTime(ICONS_DRAW_TIME);
    SetRotationTime(ROTATION_TIME);
  }
}

void WeaponsMenu::RefreshWeaponList()
{
  // reset number of weapon
  for (int i = 0; i < MAX_NUMBER_OF_WEAPON; i++)
    nb_weapon_type[i] = 0;
  weapons_menu->ResetTransformation();
  tools_menu->ResetTransformation();
  // Refreshing Weapons menu

  const std::vector<PolygonItem *>& weapons = weapons_menu->GetItem();
  std::vector<PolygonItem *>::const_iterator item = weapons.begin();
  for (; item != weapons.end(); item++) {
    delete (*item);
  }
  weapons_menu->ClearItem(false);

  // Tools menu
  const std::vector<PolygonItem *>& tools = tools_menu->GetItem();
  item = tools.begin();
  for (; item != tools.end(); item++) {
    delete (*item);
  }
  tools_menu->ClearItem(false);
  // Reinserting weapon
  WeaponsList * weapons_list = Game::GetInstance()->GetWeaponsList();
  for (WeaponsList::weapons_list_it it=weapons_list->GetList().begin();
       it != weapons_list->GetList().end();
       ++it)
    AddWeapon(*it);
}

AffineTransform2D WeaponsMenu::ComputeToolTransformation()
{
  uint scroll_border = 0;
  if (Config::GetInstance()->GetScrollOnBorder()) {
    scroll_border = Config::GetInstance()->GetScrollBorderSize();
  }

  Point2i start = click_pos - Point2i(tools_menu->GetWidth(), 0);
  Point2i end = click_pos + Point2i(weapons_menu->GetWidth()+10, 0);

  // Define the animation
  position.SetTranslationAnimation(motion_start_time, GetIconsDrawTime(),
                                   GameTime::GetInstance()->Read(),
                                   !show, start, end);

  return position ;
}

AffineTransform2D WeaponsMenu::ComputeWeaponTransformation()
{
  uint scroll_border = 0;
  if (Config::GetInstance()->GetScrollOnBorder()) {
    scroll_border = Config::GetInstance()->GetScrollBorderSize();
  }

  // Init animation parameter
  Point2i start = click_pos
                - Point2i(weapons_menu->GetWidth()+10+tools_menu->GetWidth(), 0);

  // Define the animation
  position.SetTranslationAnimation(motion_start_time, GetIconsDrawTime(),
                                   GameTime::GetInstance()->Read(),
                                   !show, start, click_pos);

  return position;
}

void WeaponsMenu::Draw()
{
  if (!show && (motion_start_time == 0 || GameTime::GetInstance()->Read() >= motion_start_time + GetIconsDrawTime()))
    return;

  // Update animation
  m_not_yet_available->Update();

  Rectanglei clip(click_pos, weapons_menu->GetSize() + Point2i(10, 0) + tools_menu->GetSize());
  SwapWindowClip(clip);
  // Draw weapons menu
  weapons_menu->ApplyTransformation(ComputeWeaponTransformation());
  weapons_menu->DrawOnScreen();
  // Tools
  tools_menu->ApplyTransformation(ComputeToolTransformation());
  tools_menu->DrawOnScreen();
  // Update overfly weapon/tool
  if (!UpdateCurrentOverflyItem(weapons_menu))
    UpdateCurrentOverflyItem(tools_menu);
  SwapWindowClip(clip);
}

Weapon * WeaponsMenu::UpdateCurrentOverflyItem(const Polygon * poly)
{
  if (!show)
    return 0;
  const std::vector<PolygonItem *>& items = poly->GetItem();
  WeaponMenuItem * tmp;
  Interface::GetInstance()->SetCurrentOverflyWeapon(NULL);
  std::vector<PolygonItem *>::const_iterator item = items.begin();
  for (; item != items.end(); item++) {
    tmp = (WeaponMenuItem *)(*item);
    if (tmp->IsMouseOver()) {
      Interface::GetInstance()->SetCurrentOverflyWeapon(tmp->GetWeapon());
      if (current_overfly_item != tmp) {
        if (current_overfly_item)
          current_overfly_item->SetZoom(false);
        current_overfly_item = tmp;
      }
      return tmp->GetWeapon();
    }
  }
  return NULL;
}

bool WeaponsMenu::ActionClic(const Point2i &/*mouse_pos*/)
{
  if (!show)
    return false;
  Weapon * tmp = UpdateCurrentOverflyItem(weapons_menu);
  if (!tmp)
    tmp = UpdateCurrentOverflyItem(tools_menu);
  if (tmp) {
    // Check we have enough ammo
    int nb_bullets = ActiveTeam().ReadNbAmmos(tmp->GetType());
    if ((nb_bullets == INFINITE_AMMO || nb_bullets > 0) && ActiveTeam().GetWeapon().CanChangeWeapon()) {
      ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_PLAYER_CHANGE_WEAPON, tmp->GetType()));
      Hide(false);
      return true;
    }
  }
  return false;
}