Gridarta Editor
DefaultAnimationObject.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more 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  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.anim;
21 
22 import java.util.ArrayList;
23 import java.util.List;
26 import org.apache.log4j.Category;
27 import org.apache.log4j.Logger;
28 import org.jetbrains.annotations.NotNull;
29 
35 
39  private static final long serialVersionUID = 1L;
40 
44  @NotNull
45  private static final Category LOG = Logger.getLogger(DefaultAnimationObject.class);
46 
51  @NotNull
52  private final List<String> frames = new ArrayList<>();
53 
58  private final int frameCount;
59 
65  private final int facings;
66 
71  @NotNull
72  private final String animName;
73 
79  @NotNull
80  private final String animList;
81 
93  public DefaultAnimationObject(@NotNull final String animName, @NotNull final String animList, @NotNull final String path) {
94  super(path);
95  this.animName = animName;
96  this.animList = animList;
97  int tmpFacings = 1;
98  for (final String line : StringUtils.PATTERN_NEWLINE.split(animList, 0)) {
99  if (line.startsWith("facings ")) {
100  tmpFacings = Integer.parseInt(line.substring(8));
101  break;
102  }
103  }
104  if (tmpFacings < 1) {
105  LOG.warn("Ignoring invalid facings value: " + tmpFacings);
106  tmpFacings = 1;
107  }
108  facings = tmpFacings;
109  boolean first = true;
110  for (final String line : StringUtils.PATTERN_NEWLINE.split(animList, 0)) {
111  if (first && line.startsWith("facings ")) {
112  first = false;
113  } else {
114  frames.add(line);
115  }
116  }
117  if (frames.size() <= 0) {
118  LOG.warn("Animation " + animName + " has no frames");
119  for (int i = 0; i < facings; i++) {
120  frames.add("bug.111");
121  }
122  } else if (frames.size() % facings != 0) {
123  LOG.warn("Animation " + animName + " has " + frames.size() + " frames but " + facings + " facings");
124  while (frames.size() % facings != 0) {
125  frames.add(frames.get(frames.size() - 1));
126  }
127  }
128  frameCount = frames.size() / facings;
129  }
130 
134  @NotNull
135  @Override
136  public String getName() {
137  return animName;
138  }
139 
140  @Override
141  public int getFacings() {
142  return facings;
143  }
144 
148  @NotNull
149  @Override
150  public String getAnimName() {
151  return animName;
152  }
153 
154  @NotNull
155  @Override
156  public String getAnimList() {
157  return animList;
158  }
159 
164  @Override
165  public String toString() {
166  final StringBuilder sb = new StringBuilder();
167  sb.append("anim ");
168  sb.append(animName);
169  sb.append('\n');
170  sb.append(animList);
171  sb.append("mina\n");
172  return sb.toString();
173  }
174 
175  @NotNull
176  @Override
177  public String getDisplayIconName() {
178  for (int facing = 1; facing < facings; facing++) {
179  final String frame = getFirstFrame(facing);
180  if (!frame.startsWith("dummy.")) {
181  return frame;
182  }
183  }
184  return getFirstFrame(0);
185  }
186 
187  @NotNull
188  @Override
189  public String getFirstFrame(final int facing) {
190  return getFrame(facing, 0);
191  }
192 
200  @NotNull
201  public String getFrame(final int facing, final int frame) throws IndexOutOfBoundsException {
202  if (facing < 0 || facing >= facings) {
203  throw new IndexOutOfBoundsException("Invalid facing " + facing + " (anim only has " + facings + " facings)");
204  }
205  if (frame < 0 || frame >= frameCount) {
206  throw new IndexOutOfBoundsException("Invalid frame " + frame + " (anim only has " + frameCount + " frames)");
207  }
208  return frames.get(facing * frameCount + frame);
209  }
210 
215  public int getFrameCount() {
216  return frameCount;
217  }
218 
219 }
Utility class for string manipulation.
The data package contains classes for handling data that is organized in a tree.
final List< String > frames
The names of the animation frames.
String toString()
The String representation of an AnimationObject is suitable for writing into the animations file...
static final Pattern PATTERN_NEWLINE
The pattern that matches a single newline ("\n").
String getAnimList()
Returns the animation list of this animation.
int getFrameCount()
Get the number of frames per facing.
Base package of all Gridarta classes.
int getFacings()
Get the facings, which is the number of different sub-animations, for instance for different directio...
String getFirstFrame(final int facing)
Get the first frame.
static final long serialVersionUID
The serial version UID.
final String animName
The name of this animation.
String getFrame(final int facing, final int frame)
Get a specific frame.
An AnimationObject reflects the animation ("@code anim\n @endcode " ...
Abstract base implementation of AnimationObject.
final int facings
The number of facings, which is different sub animations, for instance for different directions...
DefaultAnimationObject(@NotNull final String animName, @NotNull final String animList, @NotNull final String path)
Creates a new instance.
final int frameCount
The number of frames per facing in the animation.
static final Category LOG
The Logger for printing log messages.
String getDisplayIconName()
Returns the face name of the display icon for this AbstractNamedObject.
final String animList
The animation list of this animation.