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-2023 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  return "anim " + animName + "\n" + animList + "mina\n";
167  }
168 
169  @NotNull
170  @Override
171  public String getDisplayIconName() {
172  for (int facing = 1; facing < facings; facing++) {
173  final String frame = getFirstFrame(facing);
174  if (!frame.startsWith("dummy.")) {
175  return frame;
176  }
177  }
178  return getFirstFrame(0);
179  }
180 
181  @NotNull
182  @Override
183  public String getFirstFrame(final int facing) {
184  return getFrame(facing, 0);
185  }
186 
194  @NotNull
195  private String getFrame(final int facing, final int frame) throws IndexOutOfBoundsException {
196  if (facing < 0 || facing >= facings) {
197  throw new IndexOutOfBoundsException("Invalid facing " + facing + " (anim only has " + facings + " facings)");
198  }
199  if (frame < 0 || frame >= frameCount) {
200  throw new IndexOutOfBoundsException("Invalid frame " + frame + " (anim only has " + frameCount + " frames)");
201  }
202  return frames.get(facing * frameCount + frame);
203  }
204 
209  public int getFrameCount() {
210  return frameCount;
211  }
212 
213 }
net.sf.gridarta.model.anim.DefaultAnimationObject.toString
String toString()
The String representation of an AnimationObject is suitable for writing into the animations file.
Definition: DefaultAnimationObject.java:165
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.anim.DefaultAnimationObject
Abstract base implementation of AnimationObject.
Definition: DefaultAnimationObject.java:34
net.sf.gridarta.model.anim.DefaultAnimationObject.getAnimName
String getAnimName()
Definition: DefaultAnimationObject.java:150
net.sf
net.sf.gridarta.model.anim.DefaultAnimationObject.getFrame
String getFrame(final int facing, final int frame)
Get a specific frame.
Definition: DefaultAnimationObject.java:195
net.sf.gridarta.model.anim.DefaultAnimationObject.getAnimList
String getAnimList()
Returns the animation list of this animation.
Definition: DefaultAnimationObject.java:156
net.sf.gridarta.model.anim.DefaultAnimationObject.getFirstFrame
String getFirstFrame(final int facing)
Get the first frame.
Definition: DefaultAnimationObject.java:183
net.sf.gridarta.model.anim.DefaultAnimationObject.getFrameCount
int getFrameCount()
Get the number of frames per facing.
Definition: DefaultAnimationObject.java:209
net.sf.gridarta.model.anim.DefaultAnimationObject.facings
final int facings
The number of facings, which is different sub animations, for instance for different directions.
Definition: DefaultAnimationObject.java:65
net
net.sf.gridarta.model.data.AbstractNamedObject
An.
Definition: AbstractNamedObject.java:32
net.sf.gridarta.model.anim.DefaultAnimationObject.DefaultAnimationObject
DefaultAnimationObject(@NotNull final String animName, @NotNull final String animList, @NotNull final String path)
Creates a new instance.
Definition: DefaultAnimationObject.java:93
net.sf.gridarta.model.anim.DefaultAnimationObject.getFacings
int getFacings()
Get the facings, which is the number of different sub-animations, for instance for different directio...
Definition: DefaultAnimationObject.java:141
net.sf.gridarta.model.anim.DefaultAnimationObject.animName
final String animName
The name of this animation.
Definition: DefaultAnimationObject.java:72
net.sf.gridarta.model.data.AbstractNamedObject.path
String path
The path of this object.
Definition: AbstractNamedObject.java:44
net.sf.gridarta.model.data
Classes for handling data that is organized in a tree.
Definition: AbstractNamedObject.java:20
net.sf.gridarta.model.anim.DefaultAnimationObject.animList
final String animList
The animation list of this animation.
Definition: DefaultAnimationObject.java:80
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.model.anim.DefaultAnimationObject.frameCount
final int frameCount
The number of frames per facing in the animation.
Definition: DefaultAnimationObject.java:58
net.sf.gridarta.model.anim.DefaultAnimationObject.frames
final List< String > frames
The names of the animation frames.
Definition: DefaultAnimationObject.java:52
net.sf.gridarta.model.anim.DefaultAnimationObject.LOG
static final Category LOG
The Logger for printing log messages.
Definition: DefaultAnimationObject.java:45
net.sf.gridarta.model
net.sf.gridarta.utils.StringUtils.PATTERN_NEWLINE
static final Pattern PATTERN_NEWLINE
The pattern that matches a single newline ("\n").
Definition: StringUtils.java:109
net.sf.gridarta.model.anim.DefaultAnimationObject.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: DefaultAnimationObject.java:39
net.sf.gridarta.model.anim.AnimationObject
An AnimationObject reflects the animation ("@code anim\n @endcode " ...
Definition: AnimationObject.java:30
net.sf.gridarta.model.anim.DefaultAnimationObject.getName
String getName()
Definition: DefaultAnimationObject.java:136
net.sf.gridarta.model.anim.DefaultAnimationObject.getDisplayIconName
String getDisplayIconName()
Returns the face name of the display icon for this AbstractNamedObject.
Definition: DefaultAnimationObject.java:171
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20