001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.game.integration;
042:
043: import org.netbeans.modules.vmd.api.codegen.CodeClassLevelPresenter;
044: import org.netbeans.modules.vmd.api.codegen.CodeWriter;
045: import org.netbeans.modules.vmd.api.codegen.MultiGuardedSection;
046: import org.netbeans.modules.vmd.api.model.DesignComponent;
047: import org.netbeans.modules.vmd.api.model.Presenter;
048: import org.netbeans.modules.vmd.api.model.PropertyValue;
049: import org.netbeans.modules.vmd.game.integration.components.GameTypes;
050: import org.netbeans.modules.vmd.game.model.*;
051: import org.netbeans.modules.vmd.midp.components.MidpTypes;
052:
053: import javax.swing.text.StyledDocument;
054: import java.awt.*;
055: import java.util.*;
056: import java.util.List;
057:
058: /**
059: * @author David Kaspar, Karel Herink
060: */
061: public class GameCodeSupport {
062:
063: public static Presenter createSequenceCodePresenter() {
064: return new CodeClassLevelPresenter.Adapter() {
065: protected void generateFieldSectionCode(
066: MultiGuardedSection section) {
067: CodeWriter writer = section.getWriter();
068: DesignComponent component = getComponent();
069:
070: String name = MidpTypes.getString(component
071: .readProperty(SequenceCD.PROPERTY_NAME));
072: int frameMillis = MidpTypes.getInteger(component
073: .readProperty(SequenceCD.PROPERTY_FRAME_MS));
074:
075: writer.write("public int " + name + "Delay = "
076: + frameMillis + ";\n"); // NOI18N
077: writer.write("public int[] " + name + " = {"); // NOI18N
078:
079: int[] frames = GameTypes.getFrames(component
080: .readProperty(SequenceCD.PROPERTY_FRAMES));
081: for (int i = 0; i < frames.length; i++) {
082: if (i > 0) {
083: writer.write(", "); // NOI18N
084: }
085: writer.write(Integer.toString(frames[i]));
086: }
087:
088: writer.write("};\n"); // NOI18N
089: }
090: };
091: }
092:
093: public static Presenter createImageResourceCodePresenter() {
094: return new CodeClassLevelPresenter.Adapter() {
095: protected void generateFieldSectionCode(
096: MultiGuardedSection section) {
097: CodeWriter writer = section.getWriter();
098: DesignComponent component = getComponent();
099: String name = MidpTypes.getString(component
100: .readProperty(ImageResourceCD.PROPERTY_NAME));
101: writer.write("private Image " + name + ";\n"); // NOI18N
102: }
103:
104: protected void generateClassBodyCode(StyledDocument document) {
105: DesignComponent component = getComponent();
106: MultiGuardedSection section = MultiGuardedSection
107: .create(document, component.getComponentID()
108: + "-getter"); // NOI18N
109: String name = MidpTypes.getString(component
110: .readProperty(ImageResourceCD.PROPERTY_NAME));
111: String path = MidpTypes
112: .getString(component
113: .readProperty(ImageResourceCD.PROPERTY_IMAGE_PATH));
114: section.getWriter().write(
115: "public Image "
116: + CodeUtils
117: .createGetterMethodName(name)
118: + "() throws java.io.IOException {\n"); // NOI18N
119: section.getWriter().write(
120: "if (" + name + " == null) {\n"); // NOI18N
121:
122: section.getWriter().commit();
123: section.switchToEditable(getComponent()
124: .getComponentID()
125: + "-preInit"); // NOI18N
126: section.getWriter().write(
127: " // write pre-init user code here\n").commit(); // NOI18N
128: section.switchToGuarded();
129:
130: section.getWriter().write(
131: name + " = Image.createImage(\"" + path
132: + "\");\n"); // NOI18N
133:
134: //XXX insert editable code here
135:
136: section.getWriter().write("}\n"); // NOI18N
137:
138: section.getWriter().commit();
139: section.switchToEditable(getComponent()
140: .getComponentID()
141: + "-postInit"); // NOI18N
142: section.getWriter().write(
143: " // write post-init user code here\n")
144: .commit(); // NOI18N
145: section.switchToGuarded();
146:
147: section.getWriter()
148: .write("return this." + name + ";\n"); // NOI18N
149: section.getWriter().write("}\n"); // NOI18N
150: section.getWriter().write("\n"); // NOI18N
151:
152: section.getWriter().commit();
153: section.close();
154: }
155: };
156: }
157:
158: public static Presenter createSceneCodePresenter() {
159: return new CodeClassLevelPresenter.Adapter() {
160: protected void generateClassBodyCode(StyledDocument document) {
161: DesignComponent component = getComponent();
162: MultiGuardedSection section = MultiGuardedSection
163: .create(document, component.getComponentID()
164: + "-updateLayerManager");// NOI18N
165: String sceneName = MidpTypes.getString(component
166: .readProperty(SceneCD.PROPERTY_NAME));
167:
168: section
169: .getWriter()
170: .write(
171: "public void updateLayerManagerFor"
172: + CodeUtils
173: .capitalize(sceneName)
174: + "(LayerManager lm) throws java.io.IOException {\n"); // NOI18N
175:
176: section.getWriter().commit();
177: section.switchToEditable(getComponent()
178: .getComponentID()
179: + "-preUpdate"); // NOI18N
180: section.getWriter().write(
181: " // write pre-update user code here\n")
182: .commit(); // NOI18N
183: section.switchToGuarded();
184:
185: List<PropertyValue> tmp = component.readProperty(
186: SceneCD.PROPERTY_SCENE_ITEMS).getArray();
187: List<PropertyValue> sceneItems = new ArrayList<PropertyValue>();
188: sceneItems.addAll(tmp);
189:
190: Collections.sort(sceneItems,
191: new Comparator<PropertyValue>() {
192: public int compare(PropertyValue a,
193: PropertyValue b) {
194: DesignComponent aItem = a
195: .getComponent();
196: int aZ = MidpTypes
197: .getInteger(aItem
198: .readProperty(SceneItemCD.PROPERTY_Z_ORDER));
199:
200: DesignComponent bItem = b
201: .getComponent();
202: int bZ = MidpTypes
203: .getInteger(bItem
204: .readProperty(SceneItemCD.PROPERTY_Z_ORDER));
205:
206: return new Integer(aZ).compareTo(bZ);
207: }
208: });
209: for (PropertyValue propertyValue : sceneItems) {
210: DesignComponent sceneItem = propertyValue
211: .getComponent();
212:
213: DesignComponent layer = sceneItem.readProperty(
214: SceneItemCD.PROPERTY_LAYER).getComponent();
215: String layerName = MidpTypes.getString(layer
216: .readProperty(LayerCD.PROPERTY_NAME));
217: Point position = GameTypes
218: .getPoint(sceneItem
219: .readProperty(SceneItemCD.PROPERTY_POSITION));
220: boolean visible = MidpTypes
221: .getBoolean(sceneItem
222: .readProperty(SceneItemCD.PROPERTY_VISIBLE));
223:
224: section.getWriter().write(
225: CodeUtils.createGetterMethodName(layerName)
226: + "().setPosition(" + position.x
227: + ", " + position.y + ");\n"); // NOI18N
228: section.getWriter().write(
229: CodeUtils.createGetterMethodName(layerName)
230: + "().setVisible(" + visible
231: + ");\n"); // NOI18N
232: section
233: .getWriter()
234: .write(
235: "lm.append("
236: + CodeUtils
237: .createGetterMethodName(layerName)
238: + "());\n"); // NOI18N
239: }
240:
241: section.getWriter().commit();
242: section.switchToEditable(getComponent()
243: .getComponentID()
244: + "-postUpdate"); // NOI18N
245: section.getWriter().write(
246: " // write post-update user code here\n")
247: .commit(); // NOI18N
248: section.switchToGuarded();
249:
250: section.getWriter().write("}\n"); // NOI18N
251: section.getWriter().write("\n"); // NOI18N
252:
253: section.getWriter().commit();
254: section.close();
255: }
256: };
257: }
258:
259: public static Presenter createSpriteCodePresenter() {
260: return new CodeClassLevelPresenter.Adapter() {
261:
262: protected void generateFieldSectionCode(
263: MultiGuardedSection section) {
264: DesignComponent component = getComponent();
265: String layerName = MidpTypes.getString(component
266: .readProperty(LayerCD.PROPERTY_NAME));
267: section.getWriter().write(
268: "private Sprite " + layerName + ";\n"); // NOI18N
269: }
270:
271: protected void generateClassBodyCode(StyledDocument document) {
272: DesignComponent component = getComponent();
273: MultiGuardedSection section = MultiGuardedSection
274: .create(document, component.getComponentID()
275: + "-getter");// NOI18N
276: String layerName = MidpTypes.getString(component
277: .readProperty(LayerCD.PROPERTY_NAME));
278:
279: DesignComponent imageResource = component.readProperty(
280: LayerCD.PROPERTY_IMAGE_RESOURCE).getComponent();
281: String imageName = MidpTypes.getString(imageResource
282: .readProperty(ImageResourceCD.PROPERTY_NAME));
283:
284: int tileWidth = MidpTypes.getInteger(component
285: .readProperty(LayerCD.PROPERTY_TILE_WIDTH));
286: int tileHeight = MidpTypes.getInteger(component
287: .readProperty(LayerCD.PROPERTY_TILE_HEIGHT));
288:
289: DesignComponent defSeq = component
290: .readProperty(
291: SequenceContainerCDProperties.PROP_DEFAULT_SEQUENCE)
292: .getComponent();
293: String defSeqName = MidpTypes.getString(defSeq
294: .readProperty(SequenceCD.PROPERTY_NAME));
295:
296: section
297: .getWriter()
298: .write(
299: "public Sprite "
300: + CodeUtils
301: .createGetterMethodName(layerName)
302: + "() throws java.io.IOException {\n"); // NOI18N
303: section.getWriter().write(
304: "if (" + layerName + " == null) {\n"); // NOI18N
305:
306: section.getWriter().commit();
307: section.switchToEditable(getComponent()
308: .getComponentID()
309: + "-preInit"); // NOI18N
310: section.getWriter().write(
311: " // write pre-init user code here\n").commit(); // NOI18N
312: section.switchToGuarded();
313:
314: section
315: .getWriter()
316: .write(
317: layerName
318: + " = new Sprite("
319: + CodeUtils
320: .createGetterMethodName(imageName)
321: + "(), " + tileWidth + ", "
322: + tileHeight + ");\n"); // NOI18N
323: section.getWriter().write(
324: layerName + ".setFrameSequence(" + defSeqName
325: + ");\n"); // NOI18N
326:
327: section.getWriter().commit();
328: section.switchToEditable(getComponent()
329: .getComponentID()
330: + "-postInit"); // NOI18N
331: section.getWriter().write(
332: " // write post-init user code here\n")
333: .commit(); // NOI18N
334: section.switchToGuarded();
335:
336: section.getWriter().write("}\n"); // NOI18N
337:
338: //XXX insert editable code here
339:
340: section.getWriter().write(
341: " return " + layerName + ";\n"); // NOI18N
342: section.getWriter().write("}\n"); // NOI18N
343: section.getWriter().write("\n"); // NOI18N
344:
345: section.getWriter().commit();
346: section.close();
347: }
348: };
349: }
350:
351: private static Set<Integer> getAnimatedTileIndexesFromTiledLayer(
352: DesignComponent tiledLayer) {
353: assert (tiledLayer.getType().equals(TiledLayerCD.TYPEID));
354: Set<Integer> animSet = new HashSet<Integer>();
355: int[][] tiles = GameTypes.getTiles(tiledLayer
356: .readProperty(TiledLayerCD.PROPERTY_TILES));
357: for (int i = 0; i < tiles.length; i++) {
358: for (int j = 0; j < tiles[i].length; j++) {
359: int index = tiles[i][j];
360: if (index < 0) {
361: animSet.add(index);
362: }
363: }
364: }
365: return animSet;
366: }
367:
368: private static Set<DesignComponent> getAnimatedTilesFromTiledLayer(
369: DesignComponent tiledLayer) {
370: Set<DesignComponent> finalSet = new HashSet<DesignComponent>();
371: Set<Integer> animSet = getAnimatedTileIndexesFromTiledLayer(tiledLayer);
372: DesignComponent dcImgRes = tiledLayer.readProperty(
373: LayerCD.PROPERTY_IMAGE_RESOURCE).getComponent();
374: Collection<DesignComponent> children = dcImgRes.getComponents();
375: for (DesignComponent child : children) {
376: if (child.getType().equals(AnimatedTileCD.TYPEID)) {
377: if (animSet.contains(MidpTypes.getInteger(child
378: .readProperty(AnimatedTileCD.PROPERTY_INDEX)))) {
379: finalSet.add(child);
380: }
381: }
382: }
383: return finalSet;
384: }
385:
386: public static Presenter createTiledLayerCodePresenter() {
387: return new CodeClassLevelPresenter.Adapter() {
388:
389: protected void generateFieldSectionCode(
390: MultiGuardedSection section) {
391: DesignComponent component = getComponent();
392:
393: //define the layer
394: String layerName = MidpTypes.getString(component
395: .readProperty(LayerCD.PROPERTY_NAME));
396: section.getWriter().write(
397: "private TiledLayer " + layerName + ";\n"); // NOI18N
398:
399: Set<DesignComponent> ats = getAnimatedTilesFromTiledLayer(component);
400: for (DesignComponent animtile : ats) {
401: String animTileName = MidpTypes
402: .getString(animtile
403: .readProperty(AnimatedTileCD.PROPERTY_NAME));
404: section.getWriter().write(
405: "public int " + animTileName
406: + CodeUtils.capitalize(layerName)
407: + ";\n"); // NOI18N
408: }
409: }
410:
411: protected void generateClassBodyCode(StyledDocument document) {
412: DesignComponent component = getComponent();
413: MultiGuardedSection section = MultiGuardedSection
414: .create(document, component.getComponentID()
415: + "-getter");// NOI18N
416:
417: String layerName = MidpTypes.getString(component
418: .readProperty(LayerCD.PROPERTY_NAME));
419: int[][] tiles = GameTypes.getTiles(component
420: .readProperty(TiledLayerCD.PROPERTY_TILES));
421:
422: DesignComponent imageResource = component.readProperty(
423: LayerCD.PROPERTY_IMAGE_RESOURCE).getComponent();
424: String imageName = MidpTypes.getString(imageResource
425: .readProperty(ImageResourceCD.PROPERTY_NAME));
426: int tileWidth = MidpTypes.getInteger(component
427: .readProperty(LayerCD.PROPERTY_TILE_WIDTH));
428: int tileHeight = MidpTypes.getInteger(component
429: .readProperty(LayerCD.PROPERTY_TILE_HEIGHT));
430:
431: section
432: .getWriter()
433: .write(
434: "public TiledLayer "
435: + CodeUtils
436: .createGetterMethodName(layerName)
437: + "() throws java.io.IOException {\n"); // NOI18N
438: section.getWriter().write(
439: "if (" + layerName + " == null) {\n"); // NOI18N
440:
441: section.getWriter().commit();
442: section.switchToEditable(getComponent()
443: .getComponentID()
444: + "-preInit"); // NOI18N
445: section.getWriter().write(
446: " // write pre-init user code here\n").commit(); // NOI18N
447: section.switchToGuarded();
448:
449: section
450: .getWriter()
451: .write(
452: layerName
453: + " = new TiledLayer(" // NOI18N
454: + tiles[0].length
455: + ", "
456: + tiles.length
457: + ", "
458: + CodeUtils
459: .createGetterMethodName(imageName) // NOI18N
460: + "(), " + tileWidth + ", "
461: + tileHeight + ");\n"); // NOI18N
462:
463: //init animated tiles
464: Set<DesignComponent> ats = getAnimatedTilesFromTiledLayer(component);
465: for (DesignComponent animtile : ats) {
466: String animTileName = MidpTypes
467: .getString(animtile
468: .readProperty(AnimatedTileCD.PROPERTY_NAME));
469: DesignComponent defSeq = animtile
470: .readProperty(
471: SequenceContainerCDProperties.PROP_DEFAULT_SEQUENCE)
472: .getComponent();
473:
474: String seqName = MidpTypes.getString(defSeq
475: .readProperty(SequenceCD.PROPERTY_NAME));
476: section.getWriter().write(
477: animTileName
478: + CodeUtils.capitalize(layerName)
479: + " = " + layerName
480: + ".createAnimatedTile(" + seqName
481: + "[0]);\n"); // NOI18N
482: }
483: Map<Integer, String> indexNames = new HashMap<Integer, String>();
484: for (DesignComponent at : ats) {
485: indexNames
486: .put(
487: MidpTypes
488: .getInteger(at
489: .readProperty(AnimatedTileCD.PROPERTY_INDEX)),
490: MidpTypes
491: .getString(at
492: .readProperty(AnimatedTileCD.PROPERTY_NAME)));
493: }
494:
495: //write tile matrix
496: section.getWriter().write("int[][] tiles = {\n"); // NOI18N
497: for (int rowIndex = 0; rowIndex < tiles.length; rowIndex++) {
498: section.getWriter().write("{ "); // NOI18N
499: int[] row = tiles[rowIndex];
500: for (int columnIndex = 0; columnIndex < row.length; columnIndex++) {
501: if (columnIndex > 0)
502: section.getWriter().write(", "); // NOI18N
503: //check for animTile
504: int tileIndex = row[columnIndex];
505: if (tileIndex >= 0) {
506: section.getWriter().write(
507: Integer.toString(row[columnIndex]));
508: } else {
509: String atName = indexNames.get(tileIndex);
510: section
511: .getWriter()
512: .write(
513: atName
514: + CodeUtils
515: .capitalize(layerName)); // NOI18N
516: }
517: }
518: if (rowIndex < tiles.length - 1)
519: section.getWriter().write(" },\n"); // NOI18N
520: else
521: section.getWriter().write(" }\n"); // NOI18N
522: }
523: section.getWriter().write("};\n"); // NOI18N
524:
525: section.getWriter().commit();
526: section.switchToEditable(getComponent()
527: .getComponentID()
528: + "-midInit"); // NOI18N
529: section.getWriter().write(
530: " // write mid-init user code here\n").commit(); // NOI18N
531: section.switchToGuarded();
532:
533: section.getWriter().write(
534: "for (int row = 0; row < " + tiles.length
535: + "; row++) {\n"); // NOI18N
536: section.getWriter().write(
537: "for (int col = 0; col < " + tiles[0].length
538: + "; col++) {\n"); // NOI18N
539: section
540: .getWriter()
541: .write(
542: layerName
543: + ".setCell(col, row, tiles[row][col]);\n"); // NOI18N
544: section.getWriter().write("}\n"); // NOI18N
545: section.getWriter().write("}\n"); // NOI18N
546: section.getWriter().write("}\n"); // NOI18N
547:
548: section.getWriter().commit();
549: section.switchToEditable(getComponent()
550: .getComponentID()
551: + "-postInit"); // NOI18N
552: section.getWriter().write(
553: " // write post-init user code here\n")
554: .commit(); // NOI18N
555: section.switchToGuarded();
556:
557: section.getWriter().write(
558: " return " + layerName + ";\n"); // NOI18N
559: section.getWriter().write("}\n"); // NOI18N
560: section.getWriter().write("\n"); // NOI18N
561:
562: section.getWriter().commit();
563: section.close();
564: }
565: };
566: }
567:
568: }
|