QTfrontend/itemNum.cpp
author displacer
Mon, 10 Dec 2007 22:36:54 +0000
changeset 640 e69b651bb336
parent 624 e7673b036db5
child 642 3178a83c7043
permissions -rw-r--r--
first buggy infinity state for weapons
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     1
/*
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     3
 * Copyright (c) 2006, 2007 Ulyanov Igor <iulyanov@gmail.com>
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     4
 *
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     8
 *
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    12
 * GNU General Public License for more details.
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    13
 *
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    17
 */
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    18
 
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    19
#include "itemNum.h"
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    20
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    21
#include <QMouseEvent>
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    22
#include <QPainter>
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    23
624
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    24
ItemNum::ItemNum(const QImage& im, QWidget * parent, unsigned char min, unsigned char max) :
640
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    25
  m_im(im), QWidget(parent), nonInteractive(false), minItems(min), maxItems(max), numItems(min),
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    26
  infinityState(false)
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    27
{
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    28
}
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    29
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    30
ItemNum::~ItemNum()
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    31
{
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    32
}
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    33
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    34
void ItemNum::mousePressEvent ( QMouseEvent * event )
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    35
{
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    36
  if(nonInteractive) return;
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    37
  if(event->button()==Qt::LeftButton) {
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    38
    event->accept();
640
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    39
    if(infinityState && numItems==maxItems) {
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    40
      incItems();
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    41
    }
624
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    42
    if(numItems < maxItems) {
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    43
      incItems();
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    44
    }
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    45
  } else if (event->button()==Qt::RightButton) {
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    46
    event->accept();
624
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    47
    if(numItems > minItems) {
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    48
      decItems();
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    49
    }
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    50
  } else {
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    51
    event->ignore();
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    52
    return;
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    53
  }
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    54
  repaint();
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    55
}
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    56
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    57
void ItemNum::paintEvent(QPaintEvent* event)
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    58
{
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    59
  QPainter painter(this);
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    60
640
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    61
  if (numItems==maxItems+1) {
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    62
    QRect target(0, 0, 250, 32);
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    63
    painter.drawImage(target, QImage(":/res/infinity.png"));
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    64
  } else {
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    65
    for(int i=0; i<numItems; i++) {
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    66
      QRect target(11 * i, i % 2, 25, 25);
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    67
      painter.drawImage(target, m_im);
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    68
    }
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    69
  }
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
diff changeset
    70
}
624
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    71
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    72
unsigned char ItemNum::getItemsNum() const
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    73
{
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    74
  return numItems;
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    75
}
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    76
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    77
void ItemNum::setItemsNum(const unsigned char num)
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    78
{
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    79
  numItems=num;
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    80
}
640
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    81
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    82
void ItemNum::setInfinityState(bool value)
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    83
{
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    84
  infinityState=value;
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 624
diff changeset
    85
}