OSDN Git Service

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