OSDN Git Service

Change build system ant to sbt.
[mikumikustudio/MikuMikuStudio.git] / desktop / src / main / java / com / jme3 / system / awt / AwtPanelsContext.java
1 package com.jme3.system.awt;
2
3 import com.jme3.input.JoyInput;
4 import com.jme3.input.KeyInput;
5 import com.jme3.input.MouseInput;
6 import com.jme3.input.TouchInput;
7 import com.jme3.input.awt.AwtKeyInput;
8 import com.jme3.input.awt.AwtMouseInput;
9 import com.jme3.renderer.Renderer;
10 import com.jme3.system.*;
11
12 import java.util.ArrayList;
13
14 public class AwtPanelsContext implements JmeContext {
15
16     protected JmeContext actualContext;
17     protected AppSettings settings = new AppSettings(true);
18     protected SystemListener listener;
19     protected ArrayList<AwtPanel> panels = new ArrayList<AwtPanel>();
20     protected AwtPanel inputSource;
21     
22     protected AwtMouseInput mouseInput = new AwtMouseInput();
23     protected AwtKeyInput keyInput = new AwtKeyInput();
24     
25     protected boolean lastThrottleState = false;
26     
27     private class AwtPanelsListener implements SystemListener {
28
29         public void initialize() {
30             initInThread();
31         }
32
33         public void reshape(int width, int height) {
34             throw new IllegalStateException();
35         }
36
37         public void update() {
38             updateInThread();
39         }
40
41         public void requestClose(boolean esc) {
42             // shouldn't happen
43             throw new IllegalStateException();
44         }
45
46         public void gainFocus() {
47             // shouldn't happen
48             throw new IllegalStateException();
49         }
50
51         public void loseFocus() {
52             // shouldn't happen
53             throw new IllegalStateException();
54         }
55
56         public void handleError(String errorMsg, Throwable t) {
57             listener.handleError(errorMsg, t);
58         }
59
60         public void destroy() {
61             destroyInThread();
62         }
63     }
64     
65     public void setInputSource(AwtPanel panel){
66         if (!panels.contains(panel))
67             throw new IllegalArgumentException();
68         
69         inputSource = panel;
70         mouseInput.setInputSource(panel);
71         keyInput.setInputSource(panel);
72     }
73     
74     public Type getType() {
75         return Type.OffscreenSurface;
76     }
77     
78     public void setSystemListener(SystemListener listener) {
79         this.listener = listener;
80     }
81
82     public AppSettings getSettings() {
83         return settings;
84     }
85
86     public Renderer getRenderer() {
87         return actualContext.getRenderer();
88     }
89
90     public MouseInput getMouseInput() {
91         return mouseInput;
92     }
93
94     public KeyInput getKeyInput() {
95         return keyInput;
96     }
97
98     public JoyInput getJoyInput() {
99         return null;
100     }
101
102     public TouchInput getTouchInput() {
103         return null;
104     }
105
106     public Timer getTimer() {
107         return actualContext.getTimer();
108     }
109
110     public boolean isCreated() {
111         return actualContext != null && actualContext.isCreated();
112     }
113
114     public boolean isRenderable() {
115         return actualContext != null && actualContext.isRenderable();
116     }
117     
118     public AwtPanelsContext(){
119     }
120     
121     public AwtPanel createPanel(PaintMode paintMode){
122         AwtPanel panel = new AwtPanel(paintMode);
123         panels.add(panel);
124         return panel;
125     }
126     
127     private void initInThread(){
128         listener.initialize();
129     }
130     
131     private void updateInThread(){
132         // Check if throttle required
133         boolean needThrottle = true;
134         
135         for (AwtPanel panel : panels){
136             if (panel.isActiveDrawing()){
137                 needThrottle = false;
138                 break;
139             }
140         }
141         
142         if (lastThrottleState != needThrottle){
143             lastThrottleState = needThrottle;
144             if (lastThrottleState){
145                 System.out.println("OGL: Throttling update loop.");
146             }else{
147                 System.out.println("OGL: Ceased throttling update loop.");
148             }
149         }
150         
151         if (needThrottle){
152             try {
153                 Thread.sleep(100);
154             } catch (InterruptedException ex) {
155             }
156         }
157         
158         listener.update();
159     }
160     
161     private void destroyInThread(){
162         listener.destroy();
163     }
164     
165     public void setSettings(AppSettings settings) {
166         this.settings.copyFrom(settings);
167         this.settings.setRenderer(AppSettings.LWJGL_OPENGL2);
168         if (actualContext != null){
169             actualContext.setSettings(settings);
170         }
171     }
172
173     public void create(boolean waitFor) {
174         if (actualContext != null){
175             throw new IllegalStateException("Already created");
176         }
177         
178         actualContext = JmeSystem.newContext(settings, Type.OffscreenSurface);
179         actualContext.setSystemListener(new AwtPanelsListener());
180         actualContext.create(waitFor);
181     }
182
183     public void destroy(boolean waitFor) {
184         if (actualContext == null)
185             throw new IllegalStateException("Not created");
186         
187         // destroy parent context
188         actualContext.destroy(waitFor);
189     }
190     
191     public void setTitle(String title) {
192         // not relevant, ignore
193     }
194     
195     public void setAutoFlushFrames(boolean enabled) {
196         // not relevant, ignore
197     }
198
199     public void restart() {
200         // only relevant if changing pixel format.
201     }
202     
203 }