summaryrefslogtreecommitdiff
path: root/same-android/src/main/java/com/orbekk/same/GameView.java
blob: d21d05d68add6a7fb7baa0aee68e871217efd242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.orbekk.same;

import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private GameThread thread;
    
    static class Player {
        public Player() {
        }
        public Player(float posX, float posY) {
            this.posX = posX;
            this.posY = posY;
        }
        
        public float posX;
        public float posY;
    }
    
    static class GameThread extends Thread
            implements Variable.OnChangeListener<Player> {
        private Logger logger = LoggerFactory.getLogger(getClass());
        private int height = 0;
        private int width = 0;
        private SurfaceHolder holder;
        private Context context;
        private Paint background;
        private Variable<Player> player;
        private VariableUpdaterTask<Player> updater;
        private AtomicBoolean shouldRedraw = new AtomicBoolean(true);
        
        private Paint color = new Paint();
        
        public GameThread(SurfaceHolder holder, Context context,
                Variable<Player> player) {
            this.holder = holder;
            this.context = context;
            this.player = player;
            background = new Paint();
            background.setARGB(255, 0, 0, 0);
            color.setARGB(255, 255, 0, 0);
        }
        
        public void setUp() {
            player.addOnChangeListener(this);
            updater = new VariableUpdaterTask<Player>(player);
            updater.set(new Player(0.5f, 0.5f));
            updater.start();
        }
        
        public void tearDown() {
            player.removeOnChangeListener(this);
            updater.interrupt();
        }
        
        public void setSize(int width, int height) {
            synchronized(holder) {
                this.width = width;
                this.height = height;
            }
        }
        
        private void doDraw(Canvas c) {
            c.drawRect(0.0f, 0.0f, width+1.0f, height+1.0f, background);
            Player player_ = player.get();
            if (player_ == null) {
                return;
            }
            c.drawCircle(player_.posX * width, player_.posY * height,
                    20.0f, color);
        }
        
        @Override public void run() {
            while (true) {
                Canvas c = null;
                try {
                    c = holder.lockCanvas();
                    if (c != null) {
                        synchronized(holder) {
                            doDraw(c);
                        }
                    }
                } finally {
                    if (c != null) {
                        holder.unlockCanvasAndPost(c);
                    }
                }
                synchronized (this) {
                    if (Thread.interrupted()) {
                        break;
                    }
                    try {
                        while (!shouldRedraw.get()) {
                            wait();
                        }
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }
        
        private synchronized void setShouldRedraw() {
            shouldRedraw.set(true);
            notifyAll();
        }
        
        private synchronized void setPosition(final float x, final float y) {
            if (player.get() == null || player.get().posX != x ||
                    player.get().posY != y) {
                Player newPlayer = new Player(x / width, y / height);
                updater.set(newPlayer);
            }
        }

        @Override
        public synchronized void valueChanged(Variable<Player> unused) {
            logger.info("Variable updated.");
            player.update();
            setShouldRedraw();
        }
    }
    
    public GameView(Context context, Variable<Player> player) {
        super(context);
        getHolder().addCallback(this);
        thread = new GameThread(getHolder(), context, player);
    }

    public void setUp() {
        thread.setUp();
    }
    
    public void tearDown() {
        thread.tearDown();
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setARGB(255, 255, 0, 0);
        canvas.drawCircle(50.0f, 50.0f, 50.0f, paint);
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        logger.info("SurfaceChanged(w={}, h={})", width, height);
        thread.setSize(width, height);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        logger.info("SurfaceCreated()");
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        logger.info("SurfaceDestroyed()");
        thread.interrupt();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        thread.setPosition(e.getX(), e.getY());
        return true;
    }

}