View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.modifier;
20  
21  import java.awt.Graphics;
22  import java.awt.Image;
23  import java.util.List;
24  
25  import net.sf.magicproject.clickable.ability.Ability;
26  import net.sf.magicproject.clickable.targetable.card.CardFactory;
27  import net.sf.magicproject.clickable.targetable.card.MCard;
28  import net.sf.magicproject.event.MEventListener;
29  import net.sf.magicproject.expression.Expression;
30  import net.sf.magicproject.operation.Operation;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.test.Test;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.85
37   */
38  public class ObjectRegisterModifier extends RegisterModifier {
39  
40  	/***
41  	 * Creates a new instance of IdCardModifiers <br>
42  	 * 
43  	 * @param name
44  	 *          the object's name
45  	 * @param to
46  	 *          the component to be attached to
47  	 * @param ability
48  	 *          is the ability owning this test. The card component of this
49  	 *          ability should correspond to the card owning this test too.
50  	 * @param whileCondition
51  	 *          the test conditionning the activation of this modifier.
52  	 * @param linkedEvents
53  	 *          the events to be registered. This events would cause this modifier
54  	 *          to be updated.
55  	 * @param until
56  	 *          this modifier exists while this test is true.
57  	 * @param linked
58  	 *          is the creator is linked to this modifier.
59  	 * @param layer
60  	 *          is the strategy used to add this modifier within the existing
61  	 *          chain.
62  	 * @param index
63  	 *          the modified index
64  	 * @param rightExpression
65  	 *          the expression used to modify the register
66  	 * @param op
67  	 *          the operation applied to previous value with the value of this
68  	 *          modifier.
69  	 * @param main
70  	 *          is this object modifier is the main modifier.
71  	 */
72  	ObjectRegisterModifier(String name, MCard to, Ability ability,
73  			Test whileCondition, List<MEventListener> linkedEvents,
74  			List<MEventListener> until, boolean linked, int layer, int index,
75  			Expression rightExpression, Operation op, boolean main) {
76  		super(name, to, ability, whileCondition, linkedEvents, until, linked,
77  				layer, index, rightExpression, op);
78  		objectPicture = ObjectFactory.getObjectPicture(name);
79  		this.main = main;
80  	}
81  
82  	@Override
83  	public Modifier removeObject(String objectName, Test objectTest) {
84  		if (name.equals(objectName) && objectTest.test(ability, to)) {
85  			StackManager.postRefreshRegisters(to, index);
86  			return next;
87  		}
88  		return super.removeObject(objectName, objectTest);
89  	}
90  
91  	@Override
92  	public int paintObject(Graphics g, int startX, int startY) {
93  		if (main) {
94  			if (startX + 13 > CardFactory.cardWidth) {
95  				return paintObject(g, 3, startY - 16);
96  			}
97  			g.drawImage(objectPicture, startX, startY, 13, 15, null);
98  			return super.paintObject(g, startX + 3, startY);
99  		}
100 		return super.paintObject(g, startX, startY);
101 	}
102 
103 	@Override
104 	public int getNbObjects(String objectName, Test objectTest) {
105 		if (main && objectName.equals(name) && objectTest.test(ability, to)) {
106 			if (next == null) {
107 				return 1;
108 			}
109 			return 1 + next.getNbObjects(objectName, objectTest);
110 		}
111 		return super.getNbObjects(objectName, objectTest);
112 	}
113 
114 	/***
115 	 * Picture representing this object
116 	 */
117 	private Image objectPicture;
118 
119 	/***
120 	 * is this object modifier is the main modifier
121 	 */
122 	private boolean main;
123 }