View Javadoc

1   /*
2    * All Rights Reserved.
3    * Copyright (C) 1999-2005 Tsukuba Bunko.
4    *
5    * Licensed under the BSD License ("the License"); you may not use
6    * this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *       http://www.tsukuba-bunko.org/licenses/LICENSE.txt
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   * $Id: Stage.java,v 1.3 2005/08/19 03:18:11 ppoi Exp $
18   */
19  package tsukuba_bunko.peko.canvas.stage;
20  
21  import	java.awt.Color;
22  import	java.awt.Image;
23  
24  import	java.io.Serializable;
25  
26  import	java.util.HashMap;
27  import	java.util.Iterator;
28  import	java.util.Map;
29  
30  import	tsukuba_bunko.peko.Logger;
31  
32  import	tsukuba_bunko.peko.resource.ColorManager;
33  
34  
35  /***
36   * 舞台を表示するキャンバスです。
37   * @author	$Author: ppoi $
38   * @version	$Revision: 1.3 $
39   */
40  public class Stage	implements Serializable	{
41  
42  	/***
43  	 * serial version UID
44  	 */
45  	private static final long	serialVersionUID	= -5885153839184445498L;
46  
47  	/***
48  	 * このステージを描画する StageCanvas
49  	 */
50  	transient private StageCanvas	_canvas = null;
51  
52  	/***
53  	 * 音楽を再生する AudioPlayer
54  	 */
55  	private AudioPlayer	_audioPlayer = new AudioPlayer();
56  
57  
58  	/***
59  	 * 登場人物
60  	 */
61  	transient private Map	_actors = new HashMap();
62  
63  	/***
64  	 * 確定した登場人物
65  	 */
66  	private HashMap	_committedActors = new HashMap();
67  
68  	/***
69  	 * 背景
70  	 */
71  	transient private String	_background = null;
72  
73  	/***
74  	 * 確定した背景
75  	 */
76  	private String	_committedBackground = null;
77  
78  	/***
79  	 * 背景画像
80  	 */
81  	transient private Image	_backgroundImage = null;
82  
83  	/***
84  	 * 背景色
85  	 */
86  	transient private Color	_backgroundColor = null;
87  
88  	/***
89  	 * 確定した背景色
90  	 */
91  	private Color	_committedBackgroundColor = null;
92  
93  	/***
94  	 * スライド名
95  	 */
96  	transient private String	_slide = null;
97  
98  	/***
99  	 * 確定したスライド名
100 	 */
101 	private String	_committedSlide = null;
102 
103 	/***
104 	 * スライド画像
105 	 */
106 	transient private Image	_slideImage = null;
107 
108 
109 	/***
110 	 * <code>Stage</code> のインスタンスを作成します。
111 	 */
112 	public Stage()
113 	{
114 		super();
115 	}
116 
117 
118 	/***
119 	 * 人物を舞台に登場させます。
120 	 * @param	actor	人物
121 	 */
122 	public void enter( Actor actor )
123 	{
124 		_actors.put( actor.getName(), actor );
125 	}
126 
127 	/***
128 	 * 人物を取得します。
129 	 */
130 	public Actor getName( String name )
131 	{
132 		return (Actor)_actors.get( name );
133 	}
134 
135 	/***
136 	 * 人物を舞台から退場させます。
137 	 */
138 	public Actor exit( String name )
139 	{
140 		Actor	actor = (Actor)_actors.remove( name );
141 		if( actor != null )	{
142 			actor.disposeLooks();
143 		}
144 		return actor;
145 	}
146 
147 	/***
148 	 * 全ての人物を舞台から退場させます。
149 	 */
150 	public void exitAll()
151 	{
152 		String[]	names = (String[])_actors.keySet().toArray( new String[_actors.size()] );
153 		for( int i = 0; i < names.length; ++i )	{
154 			exit( names[i] );
155 		}
156 	}
157 
158 	/***
159 	 * 人物を取得します。
160 	 */
161 	public Actor getActor( String name )
162 	{
163 		return (Actor)_actors.get( name );
164 	}
165 
166 	/***
167 	 * 人物リストを取得します。
168 	 */
169 	public Map getActors()
170 	{
171 		return _actors;	//	クローニングした方が良いかも?
172 	}
173 
174 	/***
175 	 * 背景画像を設定します。このメソッドを呼び出すと、背景色は null に設定されます。
176 	 * @param	image	背景画像
177 	 */
178 	public void setBackgroundImage( String image )
179 	{
180 		ImageManager	images = ImageManager.getInstance();
181 		if( (_backgroundImage != null) && !_background.equals(image)  )	{
182 			images.putImage( _background, _backgroundImage );
183 			_backgroundImage = null;
184 		}
185 
186 		Image	background = images.getImage( image );
187 		if( background != null )	{
188 			_backgroundImage = background;
189 			_background = image;
190 			_backgroundColor = null;
191 		}
192 		else if( background == null )	{
193 			Logger.error( "[canvas.stage] invalid background-image :" + image );
194 			_backgroundColor = Color.black;
195 			_background = "black";
196 		}
197 	}
198 
199 	/***
200 	 * 背景画像を取得します。
201 	 * @return	背景画像
202 	 */
203 	public Image getBackgroundImage()
204 	{
205 		return _backgroundImage;
206 	}
207 
208 	/***
209 	 * 背景色を設定します。このメソッドを呼び出すと、背景画像は null に設定されます。
210 	 * @param	color	背景色
211 	 */
212 	public void setBackgroundColor( String color )
213 	{
214 		if( _backgroundImage != null )	{
215 			ImageManager	images = ImageManager.getInstance();
216 			images.putImage( _background, _backgroundImage );
217 			_backgroundImage = null;
218 		}
219 		ColorManager	colors = ColorManager.getInstance();
220 		_backgroundColor = colors.getColor( color );
221 		_background = color;
222 	}
223 
224 	/***
225 	 * 背景色を取得します。
226 	 * @return	背景色
227 	 */
228 	public Color getBackgroundColor()
229 	{
230 		return _backgroundColor;
231 	}
232 
233 	/***
234 	 * スライドを表示します。
235 	 * @param	slide	スライド名
236 	 */
237 	public void showSlide( String slide )
238 	{
239 		ImageManager	images = ImageManager.getInstance();
240 		if( (_slide != null) && !_slide.equals(slide) )	{
241 			images.putImage( _slide, _slideImage );
242 		}
243 		_slideImage = images.getImage( slide );
244 		_slide = slide;
245 	}
246 
247 	/***
248 	 * スライドを隠します。
249 	 */
250 	public void hideSlide()
251 	{
252 		if( _slideImage != null )	{
253 			ImageManager	images = ImageManager.getInstance();
254 			images.putImage( _slide, _slideImage );
255 		}
256 		_slide = null;
257 		_slideImage = null;
258 	}
259 
260 	/***
261 	 * スライド名を取得します。
262 	 * @return	スライド名
263 	 */
264 	public String getSlide()
265 	{
266 		return _slide;
267 	}
268 
269 	/***
270 	 * スライド画像を取得します。
271 	 * @return	スライド画像
272 	 */
273 	public Image getSlideImage()
274 	{
275 		return _slideImage;
276 	}
277 
278 	/***
279 	 * BGM を再生します。
280 	 * @param	id	BGM ID
281 	 * @param	clipName	クリップ名
282 	 * @param	loop	ループする場合は <code>true</code>、しない場合は <code>false</code>
283 	 */
284 	public void playBGM( String id, String clipName, boolean loop )
285 	{
286 		_audioPlayer.playBGM( id, clipName, loop );
287 	}
288 
289 	/***
290 	 * BGM を停止します。
291 	 * @param	id	停止する BGM の ID
292 	 * @param	mode	停止モード
293 	 */
294 	public void stopBGM( String id, int mode )
295 	{
296 		_audioPlayer.stop( id, mode );
297 	}
298 
299 	/***
300 	 * SE を停止します。
301 	 * @param	id	停止する SE の ID
302 	 */
303 	public void stopSE( String id, int mode )
304 	{
305 		_audioPlayer.stop( id, mode );
306 	}
307 
308 	/***
309 	 * SE を再生します。
310 	 * @param	id	SE ID
311 	 * @param	clipName	クリップ名
312 	 * @param	loop	ループする場合は <code>true</code>、しない場合は <code>false</code>
313 	 */
314 	public void playSE( String id, String clipName, boolean loop )
315 	{
316 		_audioPlayer.playSE( id, clipName, loop );
317 	}
318 
319 	/***
320 	 * サウンド効果を処理するオーディオプレーヤーを設定します。
321 	 * @param	audioPlayer	オーディオプレーヤー
322 	 */
323 	public void setAudioPlayer( AudioPlayer audioPlayer )
324 	{
325 		_audioPlayer = audioPlayer;
326 	}
327 
328 	/***
329 	 * サウンド効果を処理するオーディオプレーヤーを取得します。
330 	 * @return	オーディオプレーヤー
331 	 */
332 	public AudioPlayer getAudioPlayer()
333 	{
334 		return _audioPlayer;
335 	}
336 
337 	/***
338 	 * このステージを描画するキャンバスを設定します。
339 	 * @param	canvas	このステージを描画するキャンバス
340 	 */
341 	void setStageCanvas( StageCanvas canvas )
342 	{
343 		_canvas = canvas;
344 	}
345 
346 	/***
347 	 * このステージを描画するキャンバスを取得します。
348 	 * @return	ステージキャンバス
349 	 */
350 	public StageCanvas getStageCanvas()
351 	{
352 		return _canvas;
353 	}
354 
355 	/***
356 	 * このステージを描画しているキャンバスを最新の状態に更新します。
357 	 */
358 	public void updateCanvas()
359 	{
360 		_canvas.updateCanvas( null );
361 	}
362 
363 	/***
364 	 * このステージを描画しているキャンバスを最新の状態に更新します。
365 	 */
366 	public void updateCanvas( String effect )
367 	{
368 		if( _canvas != null )	{
369 			Logger.debug( "[canvas.stage] effect :" + effect );
370 			_canvas.updateCanvas( effect );
371 		}
372 	}
373 
374 
375 	public void commit()
376 	{
377 		_committedActors.clear();
378 		_committedActors.putAll( _actors );
379 		_committedBackground = _background;
380 		_committedBackgroundColor = _backgroundColor;
381 		_committedSlide = _slide;
382 	}
383 
384 	/***
385 	 * Stage を破棄します。
386 	 */
387 	void dispose()
388 	{
389 		Iterator	itr = _actors.values().iterator();
390 		while( itr.hasNext() )	{
391 			((Actor)itr.next()).disposeLooks();
392 		}
393 
394 		if( _backgroundImage != null )	{
395 			ImageManager	images = ImageManager.getInstance();
396 			images.putImage( _background, _backgroundImage );
397 		}
398 
399 		_audioPlayer.stopAll();
400 	}
401 
402 	/***
403 	 * Stage を描画する準備を実行します。
404 	 */
405 	public void prepare()
406 	{
407 		if( _actors == null )	{
408 			_actors = (Map)_committedActors.clone();
409 		}
410 
411 		Iterator	itr = _actors.values().iterator();
412 		while( itr.hasNext() )	{
413 			((Actor)itr.next()).prepare();
414 		}
415 
416 		if( _committedBackgroundColor == null )	{
417 			setBackgroundImage( _committedBackground );
418 		}
419 		else	{
420 			_background = _committedBackground;
421 			_backgroundColor = _committedBackgroundColor;
422 		}
423 
424 		if( _committedSlide != null )	{
425 			showSlide( _committedSlide );
426 		}
427 
428 		updateCanvas();
429 	}
430 }