• R/O
  • SSH

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Commit MetaInfo

Revisionb682a49a99115a5b4ebcd55869e8059c5fc44831 (tree)
Zeit2015-07-02 07:24:07
AutorMirrgieRiana
CommiterMirrgieRiana

Log Message

add mirrg.game.phosphorus

Ändern Zusammenfassung

Diff

diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/.classpath
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/.classpath Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,9 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<classpath>
3+ <classpathentry kind="src" path="src"/>
4+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+ <classpathentry combineaccessrules="false" kind="src" path="/mirrg.applet.bismuth"/>
6+ <classpathentry combineaccessrules="false" kind="src" path="/mirrg.event.bismuth"/>
7+ <classpathentry combineaccessrules="false" kind="src" path="/mirrg.struct.hydrogen"/>
8+ <classpathentry kind="output" path="bin"/>
9+</classpath>
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/.project
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/.project Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,17 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<projectDescription>
3+ <name>mirrg.game.phosphorus</name>
4+ <comment></comment>
5+ <projects>
6+ </projects>
7+ <buildSpec>
8+ <buildCommand>
9+ <name>org.eclipse.jdt.core.javabuilder</name>
10+ <arguments>
11+ </arguments>
12+ </buildCommand>
13+ </buildSpec>
14+ <natures>
15+ <nature>org.eclipse.jdt.core.javanature</nature>
16+ </natures>
17+</projectDescription>
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/automaton/AutomatonBinary.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/automaton/AutomatonBinary.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,58 @@
1+package mirrg.p.automaton;
2+
3+import mirrg.p.cell.CellBufferBinary;
4+
5+public class AutomatonBinary
6+{
7+
8+ public final CellBufferBinary cellBuffer;
9+
10+ public AutomatonBinary(CellBufferBinary cellBuffer)
11+ {
12+ this.cellBuffer = cellBuffer;
13+ }
14+
15+ //
16+
17+ /**
18+ * readerにあるデータをwriterに書き込む。
19+ */
20+ public void copy()
21+ {
22+ System.arraycopy(
23+ cellBuffer.getReaderBytes(), 0,
24+ cellBuffer.getWriterBytes(), 0,
25+ cellBuffer.getWriterBytes().length);
26+ }
27+
28+ /**
29+ * writerをクリア。
30+ */
31+ public void clear()
32+ {
33+ for (int i = 0; i < cellBuffer.getWriterBytes().length; i++) {
34+ cellBuffer.getWriterBytes()[i] = 0;
35+ }
36+ }
37+
38+ /**
39+ * writerを乱数値で初期化。
40+ */
41+ public void randomize()
42+ {
43+ for (int i = 0; i < cellBuffer.getWriterBytes().length; i++) {
44+ cellBuffer.getWriterBytes()[i] = (byte) (Math.random() * 256);
45+ }
46+ }
47+
48+ /**
49+ * writerをreaderの反転にする。
50+ */
51+ public void nagate()
52+ {
53+ for (int i = 0; i < cellBuffer.getWriterBytes().length; i++) {
54+ cellBuffer.getWriterBytes()[i] = (byte) ~cellBuffer.getReaderBytes()[i];
55+ }
56+ }
57+
58+}
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/automaton/Lifegame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/automaton/Lifegame.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,77 @@
1+package mirrg.p.automaton;
2+
3+import java.util.Arrays;
4+
5+import mirrg.p.cell.CellBufferBinary;
6+
7+public class Lifegame extends AutomatonBinary
8+{
9+
10+ private final boolean[] birthTable, surviveTable;
11+
12+ public Lifegame(
13+ CellBufferBinary cellBuffer)
14+ {
15+ this(cellBuffer,
16+ new boolean[] {
17+ false, false, true, true, false, false, false, false, false,
18+ },
19+ new boolean[] {
20+ false, false, false, true, false, false, false, false, false,
21+ });
22+ }
23+
24+ public Lifegame(
25+ CellBufferBinary cellBuffer,
26+ boolean[] birthTable,
27+ boolean[] surviveTable)
28+ {
29+ super(cellBuffer);
30+
31+ if (birthTable.length < 9) {
32+ throw new RuntimeException("table length < 9");
33+ }
34+ if (surviveTable.length < 9) {
35+ throw new RuntimeException("table length < 9");
36+ }
37+
38+ this.birthTable = Arrays.copyOf(birthTable, birthTable.length);
39+ this.surviveTable = Arrays.copyOf(surviveTable, surviveTable.length);
40+ }
41+
42+ public void spend()
43+ {
44+ for (int y = 0; y < cellBuffer.getImageHeight(); y++) {
45+ for (int x = 0; x < cellBuffer.getImageWidth(); x++) {
46+ cellBuffer.set(x, y, isLiving(cellBuffer.get(x, y), calcNeighborLives(x, y)));
47+ }
48+ }
49+ }
50+
51+ protected final int calcNeighborLives(final int x, final int y)
52+ {
53+ int t = 0;
54+
55+ for (int yi = -1; yi <= 1; yi++) {
56+ for (int xi = -1; xi <= 1; xi++) {
57+ if (xi != 0 || yi != 0) {
58+ if (cellBuffer.get(x + xi, y + yi)) {
59+ t++;
60+ }
61+ }
62+ }
63+ }
64+
65+ return t;
66+ }
67+
68+ protected final boolean isLiving(boolean isLivingNow, int neighborLives)
69+ {
70+ if (isLivingNow) {
71+ return birthTable[neighborLives];
72+ } else {
73+ return surviveTable[neighborLives];
74+ }
75+ }
76+
77+}
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/automaton/LifegameListed.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/automaton/LifegameListed.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,176 @@
1+package mirrg.p.automaton;
2+
3+import java.util.ArrayList;
4+
5+import mirrg.p.cell.CellBufferBinary;
6+
7+public class LifegameListed extends Lifegame
8+{
9+
10+ protected final Integer[] ints;
11+
12+ protected final boolean[][] updateTable;
13+ protected final boolean[][] updatedTable;
14+ protected ArrayList<Integer> updateListX;
15+ protected ArrayList<Integer> updateListY;
16+
17+ public LifegameListed(
18+ CellBufferBinary cellBuffer)
19+ {
20+ super(cellBuffer);
21+
22+ ints = createInts();
23+ updateTable = createBooleanTable();
24+ updatedTable = createBooleanTable();
25+ }
26+
27+ public LifegameListed(
28+ CellBufferBinary cellBuffer,
29+ boolean[] birthTable,
30+ boolean[] surviveTable)
31+ {
32+ super(cellBuffer, birthTable, surviveTable);
33+
34+ ints = createInts();
35+ updateTable = createBooleanTable();
36+ updatedTable = createBooleanTable();
37+ }
38+
39+ public void copy()
40+ {
41+ super.copy();
42+ clearUpdateList();
43+ }
44+
45+ public void clear()
46+ {
47+ super.clear();
48+ clearUpdateList();
49+ }
50+
51+ public void randomize()
52+ {
53+ super.randomize();
54+ clearUpdateList();
55+ }
56+
57+ public void negate()
58+ {
59+ super.nagate();
60+ clearUpdateList();
61+ }
62+
63+ public void clearUpdateList()
64+ {
65+ updateListX = null;
66+ updateListY = null;
67+ }
68+
69+ public void spend()
70+ {
71+ // initialize working buffer
72+ resetBooleanTable(updateTable);
73+ resetBooleanTable(updatedTable);
74+
75+ ArrayList<Integer> updateListX = this.updateListX;
76+ ArrayList<Integer> updateListY = this.updateListY;
77+ copy();
78+ this.updateListX = new ArrayList<Integer>(1000);
79+ this.updateListY = new ArrayList<Integer>(1000);
80+
81+ // process all updates
82+ if (updateListX == null) {
83+ for (int y = 0; y < cellBuffer.getImageHeight(); y++) {
84+ for (int x = 0; x < cellBuffer.getImageWidth(); x++) {
85+
86+ update(x, y);
87+
88+ }
89+ }
90+ } else {
91+ for (int i = 0; i < updateListX.size(); i++) {
92+ int x = updateListX.get(i);
93+ int y = updateListY.get(i);
94+
95+ updateMargin(x, y);
96+
97+ }
98+ }
99+
100+ }
101+
102+ protected Integer[] createInts()
103+ {
104+ Integer[] ints = new Integer[Math.max(cellBuffer.getImageWidth(), cellBuffer.getImageHeight())];
105+ for (int i = 0; i < ints.length; i++) {
106+ ints[i] = Integer.valueOf(i);
107+ }
108+ return ints;
109+ }
110+
111+ protected boolean[][] createBooleanTable()
112+ {
113+ boolean[][] table = new boolean[cellBuffer.getImageHeight()][];
114+ for (int y = 0; y < cellBuffer.getImageHeight(); y++) {
115+ table[y] = new boolean[cellBuffer.getImageWidth()];
116+ }
117+ return table;
118+ }
119+
120+ protected static void resetBooleanTable(boolean[][] table)
121+ {
122+ for (int y = 0; y < table.length; y++) {
123+ for (int x = 0; x < table[y].length; x++) {
124+ table[y][x] = false;
125+ }
126+ }
127+ }
128+
129+ protected final void updateMargin(int x, int y)
130+ {
131+ for (int yi = -1; yi <= 1; yi++) {
132+ for (int xi = -1; xi <= 1; xi++) {
133+
134+ update(
135+ cellBuffer.modLoopX(x + xi),
136+ cellBuffer.modLoopY(y + yi));
137+
138+ }
139+ }
140+ }
141+
142+ protected final void update(int x, int y)
143+ {
144+ if (!updatedTable[y][x]) {
145+ updatedTable[y][x] = true;
146+
147+ boolean isLivingNow = cellBuffer.get(x, y);
148+ boolean isLivingNext = isLiving(cellBuffer.get(x, y), calcNeighborLives(x, y));
149+ cellBuffer.set(x, y, isLivingNext);
150+
151+ if (isLivingNow != isLivingNext) {
152+ addUpdateListFast(x, y);
153+ }
154+
155+ }
156+ }
157+
158+ protected final void addUpdateListFast(int x, int y)
159+ {
160+ if (!updateTable[y][x]) {
161+ updateTable[y][x] = true;
162+
163+ updateListX.add(ints[x]);
164+ updateListY.add(ints[y]);
165+ }
166+ }
167+
168+ public void addUpdateList(int x, int y)
169+ {
170+ if (updateListX == null) {
171+ return;
172+ }
173+ addUpdateListFast(cellBuffer.modLoopX(x), cellBuffer.modLoopY(y));
174+ }
175+
176+}
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/cell/CellBufferBinary.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/cell/CellBufferBinary.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,290 @@
1+package mirrg.p.cell;
2+
3+import java.awt.image.BufferedImage;
4+import java.awt.image.DataBuffer;
5+import java.awt.image.DataBufferByte;
6+import java.awt.image.Raster;
7+
8+import mirrg.struct.hydrogen.Tuple;
9+
10+/**
11+ * セルが0か1の値を取り得るセルオートマトンのための高速描画兼用バッファ。
12+ *
13+ * <pre>
14+ * 常に画像バッファ・Readerバッファ・Writerバッファの内容を意識しながら操作しなければならない。
15+ * バッファの内容はゴミ状態と最新状態の2種類をのうちどれかを取る。
16+ *
17+ * アクション一覧
18+ *
19+ * インスタンス生成に類するもの(画像・Readerは最新状態)
20+ * インスタンス生成:画像・Reader・Writerは空。全てのバッファがゴミ状態であるともいえる。
21+ * createResized:画像・Readerは元のインスタンスのものを受け継ぐ。
22+ *
23+ * pull(画像が最新状態であり、Readerが最新状態になる操作)
24+ * pull:画像バッファの内容をReaderに貼りつける。Writerはゴミ状態になる。
25+ *
26+ * push(Writerが最新状態であり、画像が最新状態になる操作)
27+ * push:Writerの内容を画像バッファに貼りつける。Writerは最新状態のままである。
28+ *
29+ * push & pull(Writerが最新状態であり、Readerが最新状態になる操作)
30+ * pushAndFlip:Writerの内容を画像バッファとReaderに貼りつけ、Writerの内容をゴミ状態にする。
31+ *
32+ * copy(Readerが最新状態であり、Writerが最新状態になる操作)
33+ *
34+ * ピクセルへのアクセス
35+ * get:Readerから読み取る。Writerは最新状態のままである。
36+ * set:Writerに書き込む。これを連続で行うと、一般的にWriterの内容は最新状態になる。
37+ *
38+ * バッファへのアクセス
39+ * setAndDidModify:setと同じだが、変更されたかどうかを取得可能。
40+ * getReaderBytes:Readerバッファを直接読み取りできる。読み取ってもWriterは最新状態のままである。
41+ * getWriterBytes:Writerバッファを直接操作できる。操作を行うと一般的にWriterの内容は最新状態になる。
42+ *
43+ * draw(画像が最新状態であり、依然として画像が最新状態である操作)
44+ * createGraphics().drawImage:画像バッファの内容を書き換えることができる。
45+ *
46+ * 使い方:
47+ *
48+ * // 初期化はサイズを指定する。
49+ * cellBuffer = new これ(width, height);
50+ *
51+ * // ■この時点で画像・reader・writerは空。
52+ * // (1)
53+ *
54+ * // copy
55+ * {
56+ * // writerの編集。
57+ * cellBuffer.getWriterBytes()[4] = (byte) 0xff;
58+ *
59+ * // 編集には主にオートマトンクラスを使う。
60+ * // ■この時点でWriterが最新状態。
61+ * }
62+ *
63+ * // push, pull
64+ * {
65+ * // 編集したwriterを画像に変換し、Readerにも持ってくる。
66+ * cellBuffer.pushAndFlip();
67+ *
68+ * // この時点でcellBufferをdrawImageで読み取り可能。
69+ * // ■この時点でReaderと画像バッファが最新状態。
70+ * }
71+ * // または
72+ * {
73+ * // 編集したwriterを画像に変換する。
74+ * cellBuffer.push();
75+ *
76+ * // この時点でcellBufferをGraphicsで書き込み可能。
77+ * // ■この時点で画像バッファが最新状態。
78+ *
79+ * // 画像を編集する
80+ * cellBuffer.createGraphics().drawImage(image, 0, 0, null);
81+ *
82+ * // この時点でcellBufferをdrawImageで利用可能。
83+ * // ■この時点で画像バッファが最新状態。
84+ *
85+ * // 画像をReaderにコピー。
86+ * cellBuffer.pull();
87+ *
88+ * // ■この時点でReaderが最新状態。
89+ * }
90+ *
91+ * // (1)に戻る
92+ * </pre>
93+ */
94+public class CellBufferBinary extends BufferedImage
95+{
96+
97+ private final int imageWidth, imageHeight, bufferWidth;
98+
99+ private Tuple<Raster, byte[]> reader, writer;
100+
101+ public static CellBufferBinary createInstance(int width, int height)
102+ {
103+ if (width <= 0) width = 1;
104+ if (height <= 0) height = 1;
105+
106+ return new CellBufferBinary(width, height);
107+ }
108+
109+ private CellBufferBinary(int width, int height)
110+ {
111+ super(width, height, BufferedImage.TYPE_BYTE_BINARY);
112+
113+ imageWidth = width;
114+ imageHeight = height;
115+ bufferWidth = nice(width);
116+
117+ pull();
118+ }
119+
120+ public CellBufferBinary createResized(int width, int height)
121+ {
122+ CellBufferBinary cellBuffer = createInstance(width, height);
123+
124+ cellBuffer.createGraphics().drawImage(this, 0, 0, null);
125+ cellBuffer.pull();
126+
127+ return cellBuffer;
128+ }
129+
130+ //
131+
132+ public void pull()
133+ {
134+ reader = createRasterBytes();
135+ writer = createRasterBytes();
136+ }
137+
138+ protected Tuple<Raster, byte[]> createRasterBytes()
139+ {
140+ Raster raster = getData();
141+ DataBuffer db = raster.getDataBuffer();
142+
143+ if (db instanceof DataBufferByte) {
144+ DataBufferByte dbb = (DataBufferByte) db;
145+ byte[] bytes = dbb.getData();
146+
147+ return new Tuple<Raster, byte[]>(raster, bytes);
148+ } else {
149+ throw new RuntimeException("databuffer is not DataBufferByte");
150+ }
151+ }
152+
153+ /**
154+ * primaryにあるデータを書き込む。
155+ */
156+ public void push()
157+ {
158+ setData(writer.getX());
159+ }
160+
161+ /**
162+ * backとprimaryを交換。
163+ */
164+ protected void flip()
165+ {
166+ Tuple<Raster, byte[]> tmp = writer;
167+ writer = reader;
168+ reader = tmp;
169+ }
170+
171+ /**
172+ * pushしてflip。
173+ */
174+ public void pushAndFlip()
175+ {
176+ push();
177+ flip();
178+ }
179+
180+ //
181+
182+ /**
183+ * backから読む。
184+ */
185+ public final boolean get(final int x, final int y)
186+ {
187+ return getBit(modLoopX(x), modLoopY(y));
188+ }
189+
190+ /**
191+ * primaryに書く。
192+ */
193+ public final void set(final int x, final int y, final boolean value)
194+ {
195+ setBit(modLoopX(x), modLoopY(y), value);
196+ }
197+
198+ /**
199+ * primaryに書いて更新されたかどうかを取得。
200+ */
201+ public boolean setAndDidModify(int x, int y, boolean value)
202+ {
203+ x = modLoopX(x);
204+ y = modLoopY(y);
205+ boolean res = getBit(x, y) != value;
206+ setBit(x, y, value);
207+ return res;
208+ }
209+
210+ protected final boolean getBit(final int x, final int y)
211+ {
212+ final int byteIndex = x + bufferWidth * y;
213+ final int index = byteIndex / 8;
214+ final int bit = 7 - (byteIndex % 8);
215+ final int mask = 1 << bit;
216+
217+ return (reader.getY()[index] & mask) != 0;
218+ }
219+
220+ protected final void setBit(final int x, final int y, final boolean value)
221+ {
222+ final int byteIndex = x + bufferWidth * y;
223+ final int index = byteIndex / 8;
224+ final int bit = 7 - (byteIndex % 8);
225+ final int mask = 1 << bit;
226+
227+ final int gauge = writer.getY()[index] & ((~mask) & 0xff);
228+
229+ if (value) {
230+ writer.getY()[index] = (byte) (gauge | mask);
231+ } else {
232+ writer.getY()[index] = (byte) gauge;
233+ }
234+ }
235+
236+ //
237+
238+ public final byte[] getReaderBytes()
239+ {
240+ return reader.getY();
241+ }
242+
243+ public final byte[] getWriterBytes()
244+ {
245+ return writer.getY();
246+ }
247+
248+ public final int getImageWidth()
249+ {
250+ return imageWidth;
251+ }
252+
253+ public final int getImageHeight()
254+ {
255+ return imageHeight;
256+ }
257+
258+ public final int getBufferWidth()
259+ {
260+ return bufferWidth;
261+ }
262+
263+ //
264+
265+ public static final int modLoop(int i, int j)
266+ {
267+ if (j == 1) return 0;
268+ if (i < 0) {
269+ return j + (i % j);
270+ } else {
271+ return i % j;
272+ }
273+ }
274+
275+ public static final int nice(int value)
276+ {
277+ return value - ((value + 7) % 8) + 7;
278+ }
279+
280+ public final int modLoopX(int i)
281+ {
282+ return modLoop(i, imageWidth);
283+ }
284+
285+ public final int modLoopY(int i)
286+ {
287+ return modLoop(i, imageHeight);
288+ }
289+
290+}
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/life/test/JTestLifegame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/life/test/JTestLifegame.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,40 @@
1+package mirrg.p.life.test;
2+
3+import java.awt.CardLayout;
4+
5+import javax.swing.JPanel;
6+
7+public class JTestLifegame extends JPanel
8+{
9+
10+ private TestLifegame testLifegame;
11+
12+ public JTestLifegame()
13+ {
14+ setLayout(new CardLayout());
15+
16+ resetApplet();
17+ }
18+
19+ public TestLifegame getTestLifegame()
20+ {
21+ return testLifegame;
22+ }
23+
24+ public void resetApplet()
25+ {
26+ setTestLifegame(new TestLifegame());
27+ getTestLifegame().setSize(200, 200);
28+ add(getTestLifegame());
29+ validate();
30+
31+ getTestLifegame().init();
32+ getTestLifegame().start();
33+ }
34+
35+ private void setTestLifegame(TestLifegame applet)
36+ {
37+ this.testLifegame = applet;
38+ }
39+
40+}
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/life/test/TestLifegame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/life/test/TestLifegame.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,319 @@
1+package mirrg.p.life.test;
2+
3+import java.awt.Graphics2D;
4+import java.awt.Image;
5+import java.awt.Toolkit;
6+import java.awt.datatransfer.Clipboard;
7+import java.awt.datatransfer.DataFlavor;
8+import java.awt.datatransfer.UnsupportedFlavorException;
9+import java.awt.event.KeyEvent;
10+import java.awt.event.MouseEvent;
11+import java.io.IOException;
12+
13+import mirrg.applet.bismuth.BismuthAppletAdvanced;
14+import mirrg.applet.bismuth.event.BiEventApplet;
15+import mirrg.applet.bismuth.event.BiEventComponent;
16+import mirrg.applet.bismuth.gamethread.BiEventGameThread;
17+import mirrg.applet.bismuth.input.IInputStatus;
18+import mirrg.event.bismuth.BiHandler;
19+import mirrg.p.automaton.LifegameListed;
20+import mirrg.p.cell.CellBufferBinary;
21+import mirrg.p.serial.SerialReaderBinaryPlaintext;
22+import mirrg.p.serial.SerialReaderBinaryPlaintext.PartsBinary;
23+
24+public class TestLifegame extends BismuthAppletAdvanced
25+{
26+
27+ private CellBufferBinary cellBuffer;
28+ private LifegameListed lifegame;
29+
30+ @BiHandler
31+ public void handle(BiEventApplet.Init event)
32+ {
33+ super.handle(event);
34+ resizeBuffer(getWidth(), getHeight());
35+
36+ synchronized (this) {
37+ lifegame.randomize();
38+ cellBuffer.pushAndFlip();
39+ }
40+ }
41+
42+ @BiHandler
43+ public void handle(BiEventComponent.Resized<?> event)
44+ {
45+ resizeBuffer(getWidth(), getHeight());
46+ }
47+
48+ public void resizeBuffer(int width, int height)
49+ {
50+ if (width <= 0) width = 1;
51+ if (height <= 0) height = 1;
52+
53+ if (cellBuffer != null) {
54+ cellBuffer = cellBuffer.createResized(width, height);
55+ } else {
56+ cellBuffer = CellBufferBinary.createInstance(width, height);
57+ }
58+
59+ synchronized (this) {
60+ lifegame = new LifegameListed(cellBuffer);
61+ }
62+ }
63+
64+ @BiHandler
65+ public void handle(BiEventGameThread.DoEvents event)
66+ {
67+
68+ getGadgetGameThread().setSuitableFPS(50);
69+
70+ {
71+ IInputStatus is = getGadgetInputStatus().getInputStatus();
72+
73+ // ■lifegame is Reader-Recent
74+
75+ // input event
76+
77+ if (is.getKb(KeyEvent.VK_A) <= 0) {
78+ synchronized (this) {
79+ if (lifegame != null) {
80+ lifegame.spend();
81+ }
82+ }
83+ } else {
84+ synchronized (this) {
85+ if (lifegame != null) {
86+ lifegame.copy();
87+ }
88+ }
89+ }
90+
91+ // ■lifegame is Reader-Recent -> Writer-Recent
92+
93+ if (is.getKb(KeyEvent.VK_V) == 1) {
94+ Toolkit kit = Toolkit.getDefaultToolkit();
95+ Clipboard clip = kit.getSystemClipboard();
96+
97+ try {
98+ Image image = (Image) clip.getData(DataFlavor.imageFlavor);
99+
100+ lifegame.clear();
101+ cellBuffer.push();
102+
103+ if (is.getKb(KeyEvent.VK_SHIFT) >= 1) {
104+ lifegame.negate();
105+ cellBuffer.push();
106+ }
107+
108+ cellBuffer.createGraphics().drawImage(image, 0, 0, this);
109+ cellBuffer.pull();
110+
111+ if (is.getKb(KeyEvent.VK_SHIFT) >= 1) {
112+ lifegame.negate();
113+ cellBuffer.pushAndFlip();
114+ }
115+
116+ lifegame.copy();
117+
118+ } catch (IOException | UnsupportedFlavorException e) {
119+ System.out.println(e.getClass().getName());
120+ System.out.println(e.getMessage());
121+ }
122+ }
123+
124+ if (is.getKb(KeyEvent.VK_T) == 1) {
125+
126+ Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
127+ String plaintext;
128+
129+ try {
130+ plaintext = (String) clipboard.getData(DataFlavor.stringFlavor);
131+
132+ if (plaintext == null) {
133+ System.out.println("Clipboard does not contain text");
134+ } else {
135+
136+ PartsBinary parts = new SerialReaderBinaryPlaintext().fromCharSequence(plaintext);
137+
138+ if (parts != null) {
139+
140+ CellBufferBinary cellBuffer2 = parts.getCellBuffer();
141+
142+ cellBuffer.push();
143+ cellBuffer.createGraphics().drawImage(cellBuffer2, is.getMx(), is.getMy(), this);
144+ cellBuffer.pull();
145+
146+ lifegame.copy();
147+
148+ } else {
149+ System.out.println("Life Plaintext compile error");
150+ }
151+ }
152+
153+ } catch (UnsupportedFlavorException | IOException e) {
154+ e.printStackTrace();
155+ }
156+
157+ }
158+
159+ if (is.getMb(MouseEvent.BUTTON1) >= 1) {
160+
161+ boolean value = true;
162+ int r = 0;
163+
164+ if (is.getKb(KeyEvent.VK_CONTROL) >= 1) {
165+ r += 3;
166+ }
167+
168+ if (is.getKb(KeyEvent.VK_ALT) >= 1) {
169+ r += 20;
170+ }
171+
172+ if (is.getKb(KeyEvent.VK_SHIFT) >= 1) {
173+ setRectLine(
174+ is.getMmx() - r, is.getMmy() - r,
175+ is.getMx() - r, is.getMy() - r,
176+ 2 * r + 1, 2 * r + 1,
177+ value);
178+ } else {
179+ setRect(
180+ is.getMx() - r, is.getMy() - r,
181+ 2 * r + 1, 2 * r + 1,
182+ value);
183+ }
184+
185+ }
186+
187+ if (is.getMb(MouseEvent.BUTTON3) >= 1) {
188+
189+ boolean value = false;
190+ int r = 0;
191+
192+ if (is.getKb(KeyEvent.VK_CONTROL) >= 1) {
193+ r += 3;
194+ }
195+
196+ if (is.getKb(KeyEvent.VK_ALT) >= 1) {
197+ r += 20;
198+ }
199+
200+ if (is.getKb(KeyEvent.VK_SHIFT) >= 1) {
201+ setRectLine(
202+ is.getMmx() - r, is.getMmy() - r,
203+ is.getMx() - r, is.getMy() - r,
204+ 2 * r + 1, 2 * r + 1,
205+ value);
206+ } else {
207+ setRect(
208+ is.getMx() - r, is.getMy() - r,
209+ 2 * r + 1, 2 * r + 1,
210+ value);
211+ }
212+
213+ }
214+
215+ if (is.getKb(KeyEvent.VK_Z) == 1) {
216+
217+ synchronized (this) {
218+ if (lifegame != null) {
219+ lifegame.randomize();
220+ }
221+ }
222+
223+ }
224+
225+ if (is.getKb(KeyEvent.VK_X) == 1) {
226+
227+ synchronized (this) {
228+ if (lifegame != null) {
229+ lifegame.clear();
230+ }
231+ }
232+
233+ }
234+
235+ // ■lifegame is Writer-Recent
236+
237+ // post input event
238+
239+ synchronized (this) {
240+ if (lifegame != null) {
241+ cellBuffer.pushAndFlip();
242+ }
243+ }
244+
245+ // ■lifegame is Reader-Recent
246+
247+ getGadgetInputStatus().doInputEvents();
248+ }
249+
250+ {
251+ Graphics2D g = getGadgetPainter().getDoubleBuffer().getGraphics();
252+
253+ synchronized (this) {
254+ if (cellBuffer != null) {
255+ g.drawImage(cellBuffer, 0, 0, this);
256+ }
257+ }
258+
259+ getGadgetPainter().getDoubleBuffer().flip();
260+ }
261+
262+ repaint();
263+ }
264+
265+ private void setRectLine(int x0, int y0, int x1, int y1, int w, int h, boolean value)
266+ {
267+ if (x0 == x1 && y0 == y1) {
268+ setRect(x0, y0, w, h, value);
269+ return;
270+ }
271+
272+ if (Math.abs(y1 - y0) < Math.abs(x1 - x0)) {
273+ if (x0 < x1) {
274+ for (int x = x0; x <= x1; x++) {
275+ int y = y0 + (x - x0) * (y1 - y0) / (x1 - x0);
276+ setRect(x, y, w, h, value);
277+ }
278+ } else {
279+ for (int x = x1; x <= x0; x++) {
280+ int y = y0 + (x - x0) * (y1 - y0) / (x1 - x0);
281+ setRect(x, y, w, h, value);
282+ }
283+ }
284+ } else {
285+ if (y0 < y1) {
286+ for (int y = y0; y <= y1; y++) {
287+ int x = x0 + (y - y0) * (x1 - x0) / (y1 - y0);
288+ setRect(x, y, w, h, value);
289+ }
290+ } else {
291+ for (int y = y1; y <= y0; y++) {
292+ int x = x0 + (y - y0) * (x1 - x0) / (y1 - y0);
293+ setRect(x, y, w, h, value);
294+ }
295+ }
296+ }
297+
298+ }
299+
300+ private void setRect(int x0, int y0, int w, int h, boolean value)
301+ {
302+ synchronized (this) {
303+ if (lifegame != null) {
304+
305+ for (int y = y0 + h - 1; y >= y0; y--) {
306+ for (int x = x0 + w - 1; x >= x0; x--) {
307+
308+ if (cellBuffer.setAndDidModify(x, y, value)) {
309+ lifegame.addUpdateList(x, y);
310+ }
311+
312+ }
313+ }
314+
315+ }
316+ }
317+ }
318+
319+}
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/life/test/TestVSFrame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/life/test/TestVSFrame.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,190 @@
1+package mirrg.p.life.test;
2+
3+import java.awt.BorderLayout;
4+import java.awt.event.WindowAdapter;
5+import java.awt.event.WindowEvent;
6+
7+import javax.swing.BorderFactory;
8+import javax.swing.JFrame;
9+import javax.swing.JLabel;
10+import javax.swing.JMenu;
11+import javax.swing.JMenuBar;
12+import javax.swing.JMenuItem;
13+import javax.swing.SwingUtilities;
14+import javax.swing.UIManager;
15+import javax.swing.border.BevelBorder;
16+import javax.swing.event.ChangeEvent;
17+import javax.swing.event.ChangeListener;
18+
19+// VS4E -- DO NOT REMOVE THIS LINE!
20+@SuppressWarnings("all")
21+public class TestVSFrame extends JFrame
22+{
23+
24+ private static final long serialVersionUID = 1L;
25+ private JMenuItem jMenuItem0;
26+ private JMenuItem jMenuItem1;
27+ private JMenuItem jMenuItem2;
28+ private JMenu jMenu0;
29+ private JMenuBar jMenuBar0;
30+ private JLabel jLabel0;
31+ private JTestLifegame jTestLifegame0;
32+ private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
33+
34+ public TestVSFrame()
35+ {
36+ initComponents();
37+ }
38+
39+ private void initComponents()
40+ {
41+ add(getJLabel0(), BorderLayout.SOUTH);
42+ add(getJTestLifegame0(), BorderLayout.CENTER);
43+ addWindowListener(new WindowAdapter() {
44+
45+ @Override
46+ public void windowOpened(WindowEvent event)
47+ {
48+ windowWindowOpened(event);
49+ }
50+ });
51+ setJMenuBar(getJMenuBar0());
52+ setSize(274, 390);
53+ }
54+
55+ private JTestLifegame getJTestLifegame0()
56+ {
57+ if (jTestLifegame0 == null) {
58+ jTestLifegame0 = new JTestLifegame();
59+ }
60+ return jTestLifegame0;
61+ }
62+
63+ private JLabel getJLabel0()
64+ {
65+ if (jLabel0 == null) {
66+ jLabel0 = new JLabel();
67+ jLabel0.setText("jLabel0");
68+ jLabel0.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, null, null, null, null));
69+ }
70+ return jLabel0;
71+ }
72+
73+ private JMenuBar getJMenuBar0()
74+ {
75+ if (jMenuBar0 == null) {
76+ jMenuBar0 = new JMenuBar();
77+ jMenuBar0.add(getJMenu0());
78+ }
79+ return jMenuBar0;
80+ }
81+
82+ private JMenu getJMenu0()
83+ {
84+ if (jMenu0 == null) {
85+ jMenu0 = new JMenu();
86+ jMenu0.setText("操作");
87+ jMenu0.setOpaque(false);
88+ jMenu0.add(getJMenuItem0());
89+ jMenu0.add(getJMenuItem1());
90+ jMenu0.add(getJMenuItem2());
91+ }
92+ return jMenu0;
93+ }
94+
95+ private JMenuItem getJMenuItem2()
96+ {
97+ if (jMenuItem2 == null) {
98+ jMenuItem2 = new JMenuItem();
99+ jMenuItem2.setText("反転");
100+ }
101+ return jMenuItem2;
102+ }
103+
104+ private JMenuItem getJMenuItem1()
105+ {
106+ if (jMenuItem1 == null) {
107+ jMenuItem1 = new JMenuItem();
108+ jMenuItem1.setText("ランダム");
109+ jMenuItem1.setOpaque(false);
110+ jMenuItem1.addChangeListener(new ChangeListener() {
111+
112+ @Override
113+ public void stateChanged(ChangeEvent event)
114+ {
115+ jMenuItem1ChangeStateChanged(event);
116+ }
117+ });
118+ }
119+ return jMenuItem1;
120+ }
121+
122+ private JMenuItem getJMenuItem0()
123+ {
124+ if (jMenuItem0 == null) {
125+ jMenuItem0 = new JMenuItem();
126+ jMenuItem0.setText("クリア");
127+ jMenuItem0.setOpaque(false);
128+ jMenuItem0.addChangeListener(new ChangeListener() {
129+
130+ @Override
131+ public void stateChanged(ChangeEvent event)
132+ {
133+ jMenuItem0ChangeStateChanged(event);
134+ }
135+ });
136+ }
137+ return jMenuItem0;
138+ }
139+
140+ private static void installLnF()
141+ {
142+ try {
143+ String lnfClassname = PREFERRED_LOOK_AND_FEEL;
144+ if (lnfClassname == null)
145+ lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
146+ UIManager.setLookAndFeel(lnfClassname);
147+ } catch (Exception e) {
148+ System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage());
149+ }
150+ }
151+
152+ /**
153+ * Main entry of the class. Note: This class is only created so that you can
154+ * easily preview the result at runtime. It is not expected to be managed by
155+ * the designer. You can modify it as you like.
156+ */
157+ public static void main(String[] args)
158+ {
159+ installLnF();
160+ SwingUtilities.invokeLater(new Runnable() {
161+ @Override
162+ public void run()
163+ {
164+ TestVSFrame frame = new TestVSFrame();
165+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
166+ frame.setTitle("TestVSFrame2");
167+ frame.getContentPane().setPreferredSize(frame.getSize());
168+ frame.pack();
169+ frame.setLocationRelativeTo(null);
170+ frame.setVisible(true);
171+ }
172+ });
173+ }
174+
175+ private void windowWindowOpened(WindowEvent event)
176+ {
177+
178+ }
179+
180+ private void jMenuItem0ChangeStateChanged(ChangeEvent event)
181+ {
182+ //jTestLifegame0.getTestLifegame();
183+ }
184+
185+ private void jMenuItem1ChangeStateChanged(ChangeEvent event)
186+ {
187+
188+ }
189+
190+}
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/serial/ISerialReader.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/serial/ISerialReader.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,8 @@
1+package mirrg.p.serial;
2+
3+public interface ISerialReader<T>
4+{
5+
6+ public T fromCharSequence(CharSequence charSequence);
7+
8+}
diff -r 070a8ccb3952 -r b682a49a9911 mirrg.game.phosphorus/src/mirrg/p/serial/SerialReaderBinaryPlaintext.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.game.phosphorus/src/mirrg/p/serial/SerialReaderBinaryPlaintext.java Thu Jul 02 07:24:07 2015 +0900
@@ -0,0 +1,193 @@
1+package mirrg.p.serial;
2+
3+import java.awt.Toolkit;
4+import java.awt.datatransfer.Clipboard;
5+import java.awt.datatransfer.DataFlavor;
6+import java.util.ArrayList;
7+import java.util.Arrays;
8+import java.util.LinkedList;
9+import java.util.List;
10+import java.util.regex.Matcher;
11+import java.util.regex.Pattern;
12+
13+import mirrg.p.automaton.AutomatonBinary;
14+import mirrg.p.cell.CellBufferBinary;
15+import mirrg.struct.hydrogen.Struct3;
16+
17+public class SerialReaderBinaryPlaintext implements ISerialReader<SerialReaderBinaryPlaintext.PartsBinary>
18+{
19+
20+ @Override
21+ public PartsBinary fromCharSequence(CharSequence charSequence)
22+ {
23+ //charSequence.
24+
25+ Pattern lineSeparator = Pattern.compile("\\r\\n?|\\n");
26+ Pattern lineIsName = Pattern.compile("\\A!Name:\\s*(.*)\\Z", Pattern.CASE_INSENSITIVE);
27+ Pattern lineIsComment = Pattern.compile("\\A!(.*)\\Z");
28+ Pattern lineIsBlock = Pattern.compile("\\A([.O]*)\\s*\\Z");
29+
30+ LinkedList<String> lines = new LinkedList<String>(Arrays.asList(lineSeparator.split(charSequence)));
31+
32+ String name = null;
33+ LinkedList<String> comments = new LinkedList<String>();
34+ ArrayList<String> blockLines = new ArrayList<String>();
35+
36+ {
37+ String nextLine;
38+ nextLine = lines.isEmpty() ? null : lines.removeFirst();
39+
40+ while (nextLine != null) {
41+ Matcher matcher = lineIsName.matcher(nextLine);
42+
43+ if (matcher.matches()) {
44+ name = matcher.group(1);
45+ } else {
46+ break;
47+ }
48+
49+ nextLine = lines.isEmpty() ? null : lines.removeFirst();
50+ }
51+
52+ while (nextLine != null) {
53+ Matcher matcher = lineIsComment.matcher(nextLine);
54+
55+ if (matcher.matches()) {
56+ comments.add(matcher.group(1));
57+ } else {
58+ break;
59+ }
60+
61+ nextLine = lines.isEmpty() ? null : lines.removeFirst();
62+ }
63+
64+ while (nextLine != null) {
65+ Matcher matcher = lineIsBlock.matcher(nextLine);
66+
67+ if (matcher.matches()) {
68+ blockLines.add(matcher.group(1));
69+ } else {
70+ break;
71+ }
72+
73+ nextLine = lines.isEmpty() ? null : lines.removeFirst();
74+ }
75+ }
76+
77+ int height = blockLines.size();
78+ if (height < 1) {
79+ return null;
80+ }
81+
82+ int width = 0;
83+ for (String blockLine : blockLines) {
84+ int length = blockLine.length();
85+ if (width < length) {
86+ width = length;
87+ }
88+ }
89+ if (width < 1) {
90+ return null;
91+ }
92+
93+ CellBufferBinary cellBuffer = CellBufferBinary.createInstance(width, height);
94+ AutomatonBinary automaton = new AutomatonBinary(cellBuffer);
95+ automaton.clear();
96+
97+ {
98+ int y = 0;
99+ for (String blockLine : blockLines) {
100+
101+ for (int x = blockLine.length() - 1; x >= 0; x--) {
102+ if (blockLine.charAt(x) == 'O') {
103+ cellBuffer.set(x, y, true);
104+ }
105+ }
106+
107+ y++;
108+ }
109+ }
110+
111+ cellBuffer.pushAndFlip();
112+
113+ return new PartsBinary(name, comments, cellBuffer);
114+ }
115+
116+ public static void main(String[] args) throws Exception
117+ {
118+ Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
119+ String text = (String) clipboard.getData(DataFlavor.stringFlavor);
120+
121+ if (text == null) {
122+ System.out.println("Not text");
123+ return;
124+ }
125+
126+ System.out.println(text);
127+
128+ eatPlaintext(text);
129+ }
130+
131+ public static void main2(String[] args)
132+ {
133+ String text = "!Name: Paperclip\n!The sixteenth most common still life.\n!http://www.conwaylife.com/wiki/index.php?title=Paperclip\n..OO\n.O..O\n.O.OO\nOO.O\nO..O\n.OO";
134+
135+ eatPlaintext(text);
136+
137+ //PartsBinary parts = a.fromCharSequence("!Name: Glider\n!\n.O.\n..O\nOOO");
138+ }
139+
140+ public static void eatPlaintext(String plaintext)
141+ {
142+ eatParts(new SerialReaderBinaryPlaintext().fromCharSequence(plaintext));
143+ }
144+
145+ public static void eatParts(PartsBinary parts)
146+ {
147+ if (parts == null) {
148+ System.out.println("parts is null");
149+ return;
150+ }
151+
152+ System.out.println(parts.getName());
153+ System.out.println(parts.getComments().size());
154+ for (String comment : parts.getComments()) {
155+ System.out.println(comment);
156+ }
157+ System.out.println(parts.getCellBuffer().getWidth());
158+ System.out.println(parts.getCellBuffer().getHeight());
159+
160+ for (int y = 0; y < parts.getCellBuffer().getHeight(); y++) {
161+ for (int x = 0; x < parts.getCellBuffer().getWidth(); x++) {
162+ System.out.print(parts.getCellBuffer().get(x, y) ? '#' : '-');
163+ }
164+ System.out.println();
165+ }
166+ }
167+
168+ public static class PartsBinary extends Struct3<String, List<String>, CellBufferBinary>
169+ {
170+
171+ public PartsBinary(String name, List<String> comments, CellBufferBinary cellBuffer)
172+ {
173+ super(name, comments, cellBuffer);
174+ }
175+
176+ public String getName()
177+ {
178+ return getX();
179+ }
180+
181+ public List<String> getComments()
182+ {
183+ return getY();
184+ }
185+
186+ public CellBufferBinary getCellBuffer()
187+ {
188+ return getZ();
189+ }
190+
191+ }
192+
193+}