clean up indentation and spacing
[supercollider.git] / SCClassLibrary / Common / GUI / PlusGUI / Control / WiiMoteGUI.sc
blob5190d0fd9d23a63a0a24c8c4e005a0bea8086c65
1 /* class to display wiimote data as a gui */
3 WiiMoteGUI {
4         classvar <>xposScreen=0, <>yposScreen=20;
5         classvar <counter = 0;
6         var <>w, <>wiimote, <watcher;
8         // subwindows:
9         var <rm, <nc, <cl;
11         *new { |wiimote, w|
12                 ^super.new.w_(w).wiimote_(wiimote).init;
13         }
15         init {
16                 var f, xsize, ysize;
17                 var ncview, rmview, clview;
18                 counter = counter + 1;
20                 //              xsize = 64 + ((2*44 + 2)*2) + 2;
21                 xsize = 210;
22                 ysize = 105+130+10;
23                 //              ysize = (10+1) * 34 + 2;
26                 //              labelColor = Color.white.alpha_(0.4);
27                 w = w ?? {
28                         w = GUI.window.new("WiiMote Control", Rect(xposScreen, yposScreen, xsize + 10, ysize)).front;
29                         //w.view.background_(Color.black);
30                         w.view.decorator = FlowLayout(Rect(4, 4, w.bounds.width, w.bounds.height), 2@2, 2@2);
31                         w;
32                 };
34                 // unclutter the windows on the screen:
35                 yposScreen = yposScreen + 160;
36                 if ( yposScreen > 700,
37                         { yposScreen = 20; xposScreen = xposScreen + xsize + 10;
38                                 if ( xposScreen > 900,
39                                         { xposScreen = 0; });
40                         });
42                 GUI.staticText.new(w, Rect(0, 0, xsize - 2, 20)).string_("WiiMote" + wiimote.id + wiimote.address )
43                         .align_(0);
44                 //.background_(labelColor);
46                 rmview = GUI.compositeView.new( w, Rect( 5, 30, 205, 130 ));
47                 rm = WiiRemoteGUI.new( rmview, wiimote, 0 ); //30 );
49                 ncview = GUI.compositeView.new( w, Rect( 5, 160, 205, 105 ));
50                 nc = WiiNunchukGUI.new( ncview, wiimote, 0 ); // 160 );
52                 watcher = SkipJack.new( { this.updateVals }, 0.1, { w.isClosed }, (\wiimote_gui_ ++ counter));
53                 watcher.start;
55         }
57         updateVals {
58                 {
59                         rm.updateVals;
60                         if ( wiimote.ext_type == 1, {
61                                 nc.updateVals;
62                                 });
63                         //                      cl.updateVals;
64                 }.defer;
65         }
67         hide{
68                 if ( GUI.scheme.id == \swing,
69                         {
70                                 w.visible_( false );
71                         },{
72                                 w.close;
73                         });
74                 watcher.stop;
75         }
77         show{
78                 if ( GUI.scheme.id == \swing,
79                         {
80                                 w.visible_( true );
81                         });
82                 watcher.start;
83         }
87 WiiRemoteGUI{
88         var <>wiimote;
89         var <>w;
90         var <buttons, <sliders, <led, <rumble;
91         var <onColor, <offColor;
93         *new{ |w,wiimote,offset|
94                 ^super.new.wiimote_(wiimote).w_(w).init( offset );
95         }
97         init{ |offset=0|
98                 var xpos,ypos;
99                 xpos = 5; ypos = offset+5;
101                 onColor = Color.yellow;
102                 offColor = Color.white;
104                 buttons = Array.fill( 11, 0 );
106                 [ "A", "B", "1", "2", "-", "home", "+" ].do{ |it,i|
107                         var size;
108                         size = 20;
109                         if ( it == "home", { size = 45 } );
110                         buttons[i] = GUI.staticText.new( w, Rect( xpos, ypos, size, 20 ) ).string_( it ).background_(onColor).align_( 0 );
111                         xpos = xpos + size + 5;
112                 };
114                 xpos = 5;
115                 ypos = ypos + 25;
117                 buttons[9] = GUI.staticText.new( w, Rect( xpos, ypos+25, 20, 20 ) ).string_( "<-" ).background_(onColor).align_( 0 );
118                 xpos = xpos + 25;
119                 buttons[7] = GUI.staticText.new( w, Rect( xpos, ypos, 20, 20 ) ).string_( "^" ).background_(onColor).align_( 0 );
120                 buttons[8] = GUI.staticText.new( w, Rect( xpos, ypos+50, 20, 20 ) ).string_( "v" ).background_(onColor).align_( 0 );
121                 xpos = xpos + 25;
122                 buttons[10] = GUI.staticText.new( w, Rect( xpos, ypos+25, 20, 20 ) ).string_( "->" ).background_(onColor).align_( 0 );
124                 xpos = xpos + 25;
126                 sliders = wiimote.remote_motion.collect{ |it,i|
127                         xpos = xpos + 25;
128                         GUI.slider.new( w, Rect( xpos-25, ypos, 20, 70 ) );
129                 };
131                 sliders = sliders.add( GUI.slider.new( w, Rect( xpos, ypos, 20, 70 ) ) );
133                 ypos = ypos + 75;
134                 xpos = 5;
136                 led = wiimote.remote_led.collect{ |it,i|
137                         xpos = xpos + 25;
138                         GUI.button.new( w, Rect( xpos-25, ypos, 20, 20 ) )
139                         .states_( [ [ "X", Color.black, Color.yellow ],["O", Color.yellow, Color.black ] ] )
140                         .action_( { |but| wiimote.setLEDState( i, but.value ) } );
141                 };
143                 rumble = GUI.button.new( w, Rect( xpos, ypos, 70, 20 ) )
144                 .states_( [ [ "rumble", Color.black, Color.yellow ],["RUMBLING", Color.yellow, Color.black ] ] )
145                 .action_( { |but| wiimote.rumble( but.value ) } );
146         }
148         updateVals{
149                 led.do{ |it,i| it.value = wiimote.remote_led[i] };
150                 //rumble.value = wiimote.rumble;
151                 wiimote.remote_motion.do{ |it,i| sliders[i].value = it };
152                 //sliders[3].value = wiimote.remote_motion[3]/3;
153                 sliders[4].value = wiimote.battery;
154                 wiimote.remote_buttons.do{ |it,i|
155                         if ( it == 1 ,
156                                 {
157                                         buttons[i].background = onColor;
158                                 },{
159                                         buttons[i].background = offColor;
160                                 });
161                 };
162         }
165 WiiNunchukGUI{
166         var <>wiimote;
167         var <>w;
168         var <buttons, <sliders, <slider2d;
169         var <onColor, <offColor;
171         *new{ |w,wiimote,offset|
172                 ^super.new.wiimote_(wiimote).w_(w).init( offset );
173         }
175         init{ |offset=0|
176                 var xpos,ypos;
177                 xpos = 5; ypos = offset+5;
179                 onColor = Color.yellow;
180                 offColor = Color.white;
182                 buttons = Array.fill( 2, 0 );
184                 [ "Z", "C" ].do{ |it,i|
185                         buttons[i] = GUI.staticText.new( w, Rect( xpos, ypos, 20, 20 ) ).string_( it ).background_(onColor).align_( 0 );
186                         ypos = ypos + 25;
187                 };
189                 xpos = xpos + 25;
190                 ypos = ypos - 50;
192                 sliders = wiimote.nunchuk_motion.collect{ |it,i|
193                         xpos = xpos + 25;
194                         GUI.slider.new( w, Rect( xpos-25, ypos, 20, 70 ) );
195                 };
197                 slider2d = GUI.slider2D.new( w, Rect( xpos, ypos, 70, 70 ) );
199         }
201         updateVals{
202                 //rumble.value = wiimote.rumble;
203                 wiimote.nunchuk_motion.do{ |it,i| sliders[i].value = it };
204                 wiimote.nunchuk_buttons.do{ |it,i|
205                         if ( it == 1 ,
206                                 {
207                                         buttons[i].background = onColor;
208                                 },{
209                                         buttons[i].background = offColor;
210                                 });
211                 };
212                 slider2d.setXY( wiimote.nunchuk_stick[0], wiimote.nunchuk_stick[1] );
214         }
217 /*WiiClassicGUI{
219         }*/