Source code: com/anotherbigidea/flash/structs/ButtonRecord.java
1 /****************************************************************
2 * Copyright (c) 2001, David N. Main, All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the
6 * following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or
18 * promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ****************************************************************/
34 package com.anotherbigidea.flash.structs;
35
36 import java.io.*;
37 import java.util.*;
38 import com.anotherbigidea.io.*;
39
40 public class ButtonRecord
41 {
42 public static final int BUTTON_HITTEST = 0x08;
43 public static final int BUTTON_DOWN = 0x04;
44 public static final int BUTTON_OVER = 0x02;
45 public static final int BUTTON_UP = 0x01;
46
47 protected int flags;
48 protected int id;
49 protected int layer;
50 protected Matrix matrix;
51
52 public int getCharId() { return id; }
53 public int getLayer() { return layer; }
54 public Matrix getMatrix() { return matrix; }
55 public int getFlags() { return flags; }
56
57 public boolean isHitTest() { return ( (flags & BUTTON_HITTEST ) != 0 ); }
58 public boolean isDown() { return ( (flags & BUTTON_DOWN ) != 0 ); }
59 public boolean isOver() { return ( (flags & BUTTON_OVER ) != 0 ); }
60 public boolean isUp() { return ( (flags & BUTTON_UP ) != 0 ); }
61
62 public void setCharId( int id ) { this.id = id; }
63 public void setLayer( int layer ) { this.layer = layer; }
64 public void setMatrix( Matrix matrix ) { this.matrix = matrix; }
65 public void setFlags( int flags ) { this.flags = flags; }
66
67 /**
68 * Read a button record array
69 */
70 public static Vector read( InStream in ) throws IOException
71 {
72 Vector records = new Vector();
73
74 int firstByte = 0;
75 while( (firstByte = in.readUI8()) != 0 )
76 {
77 records.addElement( new ButtonRecord( in, firstByte ));
78 }
79
80 return records;
81 }
82
83 /**
84 * Write a button record array
85 */
86 public static void write( OutStream out, Vector records ) throws IOException
87 {
88 for( Enumeration enum = records.elements(); enum.hasMoreElements(); )
89 {
90 ButtonRecord rec = (ButtonRecord)enum.nextElement();
91 rec.write( out );
92 }
93
94 out.writeUI8( 0 );
95 }
96
97 public ButtonRecord( int id, int layer, Matrix matrix, int flags )
98 {
99 this.id = id;
100 this.layer = layer;
101 this.matrix = matrix;
102 this.flags = flags;
103 }
104
105 protected ButtonRecord( InStream in, int firstByte ) throws IOException
106 {
107 flags = firstByte;
108 id = in.readUI16();
109 layer = in.readUI16();
110 matrix = new Matrix( in );
111 }
112
113 protected void write( OutStream out ) throws IOException
114 {
115 out.writeUI8 ( flags );
116 out.writeUI16( id );
117 out.writeUI16( layer );
118 matrix.write ( out );
119 }
120
121 public String toString()
122 {
123 return "layer=" + layer + " id=" + id +
124 " flags=" + Integer.toBinaryString(flags) + " " + matrix;
125 }
126 }