-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.rs
1243 lines (1124 loc) · 40.5 KB
/
window.rs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use super::*;
use r3d::*;
use std::path;
// todo - rename APP as owner
// application owns frame window
// frame window owns panes
// etc
//
// Window Framework and a few (hopefully generally useful) windows.
// glRasterPos3f
//type sto<T>=Rc<RefCell<T>>;g_mo
pub type sto<T>=Box<T>; //might be shared trait object
pub type Renderer=();
pub trait TextOutput {
}
//type sp<T> = Rc<RekeyfCell<T>>;
//fn new_sp<T>()
// glut/whatever interaction, and handles states.
pub type KeyCode=char;
#[derive(Debug,Clone,Copy)]
pub enum KeyTransition{
KeyDown,KeyUp
}
pub use self::KeyTransition::*;
#[derive(Debug,Clone,Copy)]
pub enum WinKey{
KeyCode(char),KeyF1,KeyF2,KeyF3,KeyF4,KeyF5,KeyF6,KeyF7,KeyF8,KeyF9,
KeyF10,KeyF11,KeyF12, KeyCursorLeft,KeyCursorUp,KeyCursorDown,KeyCursorRight, KeyPageUp,KeyPageDown,KeyHome,KeyEnd,KeyInsert
}
pub type Modifiers=u32;
#[derive(Debug,Clone)]
pub struct KeyAt(pub WinKey,pub Modifiers, pub KeyTransition,pub ScreenPos);
impl KeyAt{ pub fn pos(&self)->ScreenPos{self.3}}
pub type PixelPosi=(i32,i32);
pub type PixelPosf=(f32,f32);
pub type PixelVec=(i32,i32);
pub type PixelSizei=(i32,i32);
pub type PixelSizef=(f32,f32);
pub type PixelRectf=(PixelPosf,PixelPosf);
pub type PixelRecti=(PixelPosi,PixelPosi);
pub type ScreenPos=Vec2f;
#[derive(Debug,Clone,Copy)]
pub struct ScrPos(pub f32,pub f32);
impl HasElem for ScrPos{
type Elem=f32;
}
impl HasXY for ScrPos{
fn x(&self)->Self::Elem{self.0}
fn y(&self)->Self::Elem{self.1}
fn from_xy(x:Self::Elem, y:Self::Elem)->Self{ScrPos(x,y)}
}
fn foo(){
let a=ScrPos(1.0,2.0);
let b=v2add(&a,&a);
//let b=v2add(&a);
}
pub type ScreenRect=Extents<ScreenPos>;
pub fn screen_sizei()->PixelPosi{ unsafe{g_screen_pixel_sizei} }
pub fn screen_sizef()->PixelPosf{ unsafe{g_screen_pixel_sizef} }
pub type ViewController<A> = Window<A>;
// example,
//#[derive(Clone,Debug)]
pub enum Command {
Start,
Stop,
Reset,
Move((f32,f32,f32)),
Create((f32,f32,f32),String),
}
/// UI events passed to window interfaces.
#[derive(Clone,Debug)]
pub enum Event {
None,
Render(f32),
Update(f32),
Activate(),
Deactivate(),
Key(KeyAt),
Move(ScreenPos),
Clicked(MouseButtons,ScreenPos),
TryBeginDrag(MouseButtons,ScreenPos),
Dragging(MouseButtons,ScreenPos,ScreenPos,DragMode),
Dragged(MouseButtons,ScreenPos,ScreenPos,DragMode),
Button(MouseButtons,bool,ScreenPos),
DropFile(&'static str)
}
impl Event{
fn pos(&self)->Option<ScreenPos>{
match self{
&Event::None=>None,
&Event::Render(f32)=>None,
&Event::Update(f32)=>None,
&Event::Activate()=>None,
&Event::Deactivate()=>None,
&Event::Key(ref kc)=>Some(kc.pos()),
&Event::Move(pos)=>Some(pos),
&Event::Clicked(_,pos)=>Some(pos),
&Event::TryBeginDrag(_,pos)=>Some(pos),
&Event::Dragging(_,_,pos,_)=>Some(pos),
&Event::Dragged(_,_,pos,_)=>Some(pos),
&Event::Button(_,_,pos)=>Some(pos),
&Event::DropFile(_)=>None,//TODO - dropfile needs pos!
}
}
}
pub type MouseAt=(MouseButtons,ScreenPos);
static mut g_firstdrag:bool=false;
#[repr(u32)]
#[derive(Clone,Debug,Copy)]
pub enum DragMode {
None,
Line,
Rect,
Freehand,
Lasso,
Circle,
Default,
}
enum Drag {
Line(ScreenPos,ScreenPos),
Rect(ScreenPos,ScreenPos),
Circle(ScreenPos,ScreenPos),
FreeHand(Vec<ScreenPos>),
Lasso(Vec<ScreenPos>)
}
/// returned value controls flow between states
#[repr(u32)]
pub enum Flow<A>{
Continue(),
Redraw(), /// if not animating, input responses can request redraw
Info(String), /// feedback text, otherwise 'continue'
Push(sto<Window<A>>),
PassThru(), /// Send command to next, if an overlay.
Pop(),
SendToOwner(),
SendToAll(),
Replace(sto<Window<A>>),
Overlay(sto<Window<A>>), /// e.g. popup menu
SetBackground(sto<Window<A>>), /// equivalent to Replace(x)+Overlay(Self)
SetRoot(sto<Window<A>>),
SwapWith(i32), /// swap with relative indexed state
Cycle(), //
Back(), //
Forward(), //
Toggle(), // rotate top with forward stack
NewWindow(sto<Window<A>>), // multi-windowing
}
fn split_x(r:&Rect,f0:f32,f1:f32)->Rect {
let size=r.size();
Extents(&vec2(r.min.x+size.x*f0, r.min.y),&vec2(r.min.x+size.x*f1, r.max.y))
}
fn split_y(r:&Rect,f0:f32,f1:f32)->Rect {
let size=r.size();
Extents(&vec2(r.min.x, r.min.y+size.y*f0),&vec2(r.max.x, r.min.y+size.y*f1))
}
pub enum ForeachResult{
Continue,
Quit,
}
pub struct Dragging{
pub start:ScreenPos,
pub delta:ScreenPos,
pub pos:ScreenPos,
}
pub type KeyMappings<A> = FnMut(KeyCode,&str, &mut FnMut()->Flow<A>);
pub trait Window<A> { //'C' the user defined commands it can respond to.
fn win_ask_size(&self)->Option<ScreenPos> { None }
fn win_name(&self)->&str {"none"}
fn win_activate(&mut self, app:&mut A) {}
fn win_deactivate(&mut self, app:&mut A) {}
fn win_render(&self,a:&A, _:&WinCursor) {} // todo render with mousepos
fn win_info(&self)->String { String::new()}
fn win_update(&mut self,app:&mut A,dt:f32)->Flow<A> {Flow::Continue()} // controller access..
//todo win_enter/on_exit
// TODO: all this could be eliminated?
// just use event() and clients switch.
// no need to dispatch the fixed set
// however, there's the nesting issue.
// iterate key mappings, along with functionality, to automate
// rolling statusbar/tooltips/menu assignment
fn win_key_mappings(&mut self,owner:&mut A, kmf:&mut KeyMappings<A>){}
fn win_mouse_move(&mut self, owner:&mut A, wc:&WinCursor)->Flow<A> {
// TODO - unsure if this is better dispatched by framework.
let delta=v2sub(&wc.pos, &wc.old_pos);
if let Some(ds)=unsafe{g_ldrag_start} {
let d=Dragging{
start: to_screenpos(ds),
delta: delta,
pos:wc.pos
};
unsafe {
if g_firstdrag {
println!("first drag begin\n");
self.win_ldrag_begin(owner, wc);
g_firstdrag = false;
}
}
self.win_ldragging(owner, &d, wc)
} else
if let Some(ds)=unsafe{g_rdrag_start}{
let d=Dragging{
start:to_screenpos(ds),
delta: delta,
pos: wc.pos
};
unsafe {
if g_firstdrag {
self.win_rdrag_begin(owner, wc);
unsafe { g_firstdrag = false; }
}
}
self.win_rdragging(owner, &d,wc)
} else{
self.win_passive_move(owner,wc)
}
}
// hooks for each specific dragable button state,
// streamline rolling a tool
fn win_passive_move(&mut self,owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
// first frame of dragging might have special behaviour; by default, just issue normal move
fn win_ldrag_begin(&mut self,owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_mdrag_begin(&mut self,owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_rdrag_begin(&mut self,owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_ldrag_end(&mut self, owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_mdrag_end(&mut self, owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_rdrag_end(&mut self, owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_ldragging(&mut self,owner:&mut A,d:&Dragging,w:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_mdragging(&mut self,owner:&mut A,d:&Dragging,w:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_rdragging(&mut self,owner:&mut A,d:&Dragging,w:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_wheel_up(&mut self, owner:&mut A,_:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_wheel_down(&mut self, owner:&mut A,_:&WinCursor)->Flow<A>{Flow::PassThru()}
// click - mouse pressed with no motion
fn win_lclick(&mut self,owner:&mut A, w:&WinCursor)->Flow<A>{println!("lclick{:?}",w);Flow::PassThru()}
fn win_rclick(&mut self,owner:&mut A, w:&WinCursor)->Flow<A>{println!("rclick{:?}",w);Flow::PassThru()}
fn win_mclick(&mut self,owner:&mut A, w:&WinCursor)->Flow<A>{println!("mclick{:?}",w);Flow::PassThru()}
fn win_mouse_dragged(&mut self, owner:&mut A, mb:MouseButtons, w:&WinCursor, mode:DragMode)->Flow<A>{
println!("dragged{:?} {:?} {:?}",w.drag_start,w.pos,mode);
match mb {
LeftButton=>self.win_ldrag_end(owner, w),
MiddleButton=>self.win_mdrag_end(owner, w),
RightButton=>self.win_rdrag_end(owner, w),
_=>Flow::PassThru()
}
}
fn win_mouse_dragging(&mut self, owner:&mut A, mb:MouseButtons, w:&WinCursor, mode:DragMode)->Flow<A>{
let d=Dragging{
start:w.drag_start.unwrap(),
delta: v2sub(&to_screenpos(unsafe{g_mouse_pos}),&to_screenpos(unsafe{g_mouse_opos})),
pos:w.pos
};
match mb{
LeftButton =>{
if unsafe{g_firstdrag} {
println!("firthist drag begin\n");
self.win_ldrag_begin(owner, w);
unsafe{g_firstdrag = false;}
}
self.win_ldragging(owner,&d,w)
},
RightButton => self.win_ldragging(owner,&d,w),
MidButton => self.win_mdragging(owner,&d,w),
_=>Flow::PassThru()
}
}
fn win_lbutton_down(&mut self,owner:&mut A, w:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_rbutton_down(&mut self,owner:&mut A, w:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_lbutton_up(&mut self,owner:&mut A, w:&WinCursor)->Flow<A>{
match unsafe{g_ldrag_start} {
Some(prev)=>{println!("lbutton up (dragged)");}
None=>{println!("lbutton up (no drag)");}
}
Flow::PassThru()
}
fn win_rbutton_up(&mut self,owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_mbutton_down(&mut self,owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_mbutton_up(&mut self,owner:&mut A, _:&WinCursor)->Flow<A>{Flow::PassThru()}
fn win_mouse_button(&mut self,owner:&mut A, mb:MouseButtons,s:bool, w:&WinCursor)->Flow<A> {
match (mb,s){
(MouseButtons::Left,true)=>self.win_lbutton_down(owner,w),
(MouseButtons::Left,false)=>self.win_lbutton_up(owner,w),
(MouseButtons::Right,true)=>self.win_lbutton_down(owner,w),
(MouseButtons::Right,false)=>self.win_rbutton_up(owner,w),
(MouseButtons::Mid,true)=>self.win_mbutton_down(owner,w),
(MouseButtons::Mid,false)=>self.win_mbutton_up(owner,w),
(MouseButtons::WheelUp,true)=>self.win_wheel_up(owner,w),
(MouseButtons::WheelDown,true)=>self.win_wheel_down(owner,w),
_=>Flow::PassThru()
}
}
fn win_try_drag(&self, owner:&A, mbpos:MouseButtons, w:&WinCursor)->DragMode{
trace!();
DragMode::Rect
}
fn win_click(&mut self, owner:&mut A, mb:MouseButtons, w:&WinCursor)->Flow<A>{
// dispatch to specific button if so desired.
match mb{
MouseButtons::Left=>self.win_lclick(owner,w),
MouseButtons::Right=>self.win_rclick(owner,w),
MouseButtons::Mid=>self.win_mclick(owner,w),
_=>{warn!();Flow::Continue()}
}
}
fn win_key_down(&mut self,owner:&mut A, k:window::WinKey, modifiers:u32, w:&WinCursor)->Flow<A> { Flow::PassThru() }
fn win_key_up(&mut self, owner:&mut A,k:window::WinKey, w:&WinCursor)->Flow<A> { Flow::PassThru() }
fn win_key(&mut self,owner:&mut A,k:KeyAt,w:&WinCursor)->Flow<A>{
let KeyAt(keycode,modk,state,pos)=k;
//default : route to seperate keydown,keyup
match state{
KeyDown=>self.win_key_down(owner,keycode,modk,w),
KeyUp=>self.win_key_up(owner,keycode,w)}
}
fn win_drop(&mut self, f:&str, w:&WinCursor) {}
fn win_command( &mut self, owner:&mut A,c:Command)->Flow<A>{ Flow::PassThru() }
// enum of every event,
// defaults to calling the fn's
fn win_event_dispatch(&mut self, owner:&mut A, e:Event,r:&ScreenRect)->Flow<A>{
match e{
Event::Update(dt) =>{self.win_update(owner,dt);Flow::Continue()},
Event::Render(t) =>{
self.win_render(owner,
&WinCursor
{
rect:r.clone(),
old_pos: get_mouse_ovpos(),
pos: get_mouse_vpos(),
drag_start:get_drag_start(),
aspect_ratio:get_aspect_ratio(),
t:0.0f32});
Flow::Continue()
},
Event::Activate() =>{self.win_activate(owner);Flow::Continue()},
Event::Deactivate() =>{self.win_deactivate(owner);Flow::Continue()},
Event::Key(k) =>self.win_key(owner,k,&mkwc(r,&get_mouse_vpos())),
Event::Move(pos) =>self.win_mouse_move(owner,&mkwcm(r,&pos,&get_mouse_ovpos())),
Event::TryBeginDrag(mb,pos)=>{
match get_dragmode(){
DragMode::None=>set_dragmode(self.win_try_drag(owner,mb,&mkwc(r,&pos))),
_=>{}
};
Flow::Continue()
}
Event::Clicked(mb,pos)=>self.win_click(owner,mb, &mkwc(r,&pos)),
Event::Dragging(mb,start,current,dmode) =>
self.win_mouse_dragging(owner,mb,&mkwcd(r,¤t,&start),dmode),
Event::Dragged(mb,start,current,dmode) =>
self.win_mouse_dragged(owner,mb,&mkwcd(r,¤t,&start),dmode),
Event::Button(mb,s,pos) =>self.win_mouse_button(owner,mb,s,&mkwc(r,&pos)),
_ =>Flow::Continue(),
}
}
// main event dispatch: override to iter sub
// TODO is this pointless forwarding to event_dispatch - should it just be one
fn win_event(&mut self,owner:&mut A, e:Event, r:&ScreenRect)->Flow<A>{
use self::Event as Ev;
self.win_event_dispatch(owner,e,r)
}
//fn foreach_child(&self, r:Option<Rect>, f:&FnMut(&Window<A>,Option<Rect>)->ForeachResult);
//fn foreach_child_mut(&mut self, r:Option<Rect>, f:&FnMut(&mut Window<A>,Option<Rect>)->ForeachResult);
}
// actually we could make it a mutable trait object taking the message
// and sugar wrapper for
// implement this to open a 'gridview' on something
// texture-palette, toolbox etc.
type Idx=i32;
pub trait WindowContainer<A> {
fn sub_win_count(&self)->usize;
fn sub_win_get(&self,i:usize)->Option<&window::Window<A>>;
fn sub_win_set(&mut self, i:usize, optbox<window::Window<A>>);
fn sub_win_push(&mut self, optbox<window::Window<A>>);
fn sub_win_take(&self,i:usize)->optbox<window::Window<A>>;
}
//impl<A> HasCells<A> for WindowContainer<A> {
//}
/*
pub trait HasCells<A> {
fn render_cell(&self, i:usize, r:RenderContext);
fn num_cells(&self)->usize;
}
pub trait TabView<A> : WindowContainer<A>{
}
pub trait GridView<A> : HasCells<A>{
fn select(item:Index);
fn scroll(&mut self, dxy:(i32,i32));
fn grid_size(&self,r:Rect)->(Index,Index);
}
*/
/*
impl<T:A> window::Window<A> for T:GridView<A> {
// render grid..
// click down , ..
//event -
}
*/
/*
inheritance:-
class window
class gridView : window
class mygridview..
rustic:-
impl window for T:gridview
impl gridview for mygridview // gets 'window'
*/
static mut g_key:[bool;256]=[false;256];
static mut g_mouse_button:u32=0;
static mut g_mouse_pos:PixelPosi=(0,0);
static mut g_mouse_opos:PixelPosi=(0,0);
static mut g_dragmode:DragMode=DragMode::None;
static mut g_ldrag_start:Option<PixelPosi>=None;
static mut g_rdrag_start:Option<PixelPosi>=None;
static mut g_mdrag_start:Option<PixelPosi>=None;
static mut g_drag_points:*mut Vec<ScreenPos>=0 as *mut Vec<ScreenPos>;
static mut g_joystick:((f32,f32),u32)=((0.0f32,0.0f32),0);
static mut g_screen_pixel_sizei:PixelPosi=(960,480);
static mut g_screen_pixel_sizef:PixelPosf=(960.0f32,480.0f32);
const MaxEvent:usize=256;
static mut g_aspect_ratio:f32=1.0f32;
fn get_aspect_ratio()->f32{unsafe{return g_aspect_ratio}}
static mut g_ui_event:Option<Vec<Event>>=None;
static mut g_head:i32=0;
static mut g_tail:i32=0;
pub const CTRL:u32=0x0001;
pub const SHIFT:u32=0x0002;
pub const ALT:u32=0x0004;
fn get_modifiers()->u32{
unsafe {let modk:u32=glutGetModifiers() as u32;
let ret=0u32
|if modk & GLUT_ACTIVE_SHIFT!=0{SHIFT}else{0}
|if modk & GLUT_ACTIVE_CTRL!=0{CTRL}else{0}
|if modk & GLUT_ACTIVE_ALT!=0{ALT}else{0};
ret}
}
fn xlat_special_key(skey:GLuint)->WinKey{
match skey{
GLUT_KEY_LEFT=>WinKey::KeyCursorLeft,
GLUT_KEY_RIGHT=>WinKey::KeyCursorRight,
GLUT_KEY_UP=>WinKey::KeyCursorUp,
GLUT_KEY_DOWN=>WinKey::KeyCursorDown,
GLUT_KEY_INSERT=>WinKey::KeyInsert,
GLUT_KEY_HOME=>WinKey::KeyHome,
GLUT_KEY_END=>WinKey::KeyEnd,
GLUT_KEY_PAGE_UP=>WinKey::KeyPageUp,
GLUT_KEY_PAGE_DOWN=>WinKey::KeyPageDown,
GLUT_KEY_F1=>WinKey::KeyF1,
GLUT_KEY_F2=>WinKey::KeyF2,
GLUT_KEY_F3=>WinKey::KeyF3,
GLUT_KEY_F4=>WinKey::KeyF4,
GLUT_KEY_F5=>WinKey::KeyF5,
GLUT_KEY_F6=>WinKey::KeyF6,
GLUT_KEY_F7=>WinKey::KeyF7,
GLUT_KEY_F8=>WinKey::KeyF8,
GLUT_KEY_F9=>WinKey::KeyF9,
GLUT_KEY_F10=>WinKey::KeyF10,
GLUT_KEY_F11=>WinKey::KeyF11,
GLUT_KEY_F12=>WinKey::KeyF12,
_=>WinKey::KeyCode(0 as char),
}
}
mod callbacks {
use super::*;
pub fn reshape_func(x: i32, y: i32) {
println!("resizing..{:?} {:?}", x, y);
unsafe {
let fx=x as f32;let fy=y as f32;
g_screen_pixel_sizei = (x, y);
g_screen_pixel_sizef = (fx,fy);
g_aspect_ratio = fx/fy;
glViewport(0, 0, x, y);
}
// panic!();
}
pub fn keyboard_func_sub(key: u8, isdown: bool, x: i32, y: i32) {
unsafe {
g_key[key as usize] = isdown;
let kp = KeyAt(WinKey::KeyCode(key as KeyCode),get_modifiers(), if isdown{KeyDown}else{KeyUp}, to_screenpos((x, y)));
push_event(Event::Key(kp));
}
}
pub fn keyboard_func(key: u8, x: i32, y: i32) {
keyboard_func_sub(key, true, x, y);
}
pub fn special_func_sub(key: GLuint, isdown: bool, x: i32, y: i32) {
assert!((key as u32 & 0xff) == key as u32);
unsafe {
println!("special key {:?}",key);
let kp = KeyAt(xlat_special_key(key),get_modifiers(), if isdown{KeyDown}else{KeyUp}, to_screenpos((x, y)));
push_event(Event::Key(kp));
//g_key[key as usize] = isdown; /// specialkeys do not
}
}
pub fn special_func(key: GLuint, x: i32, y: i32) {
special_func_sub(key, true, x, y);
}
pub fn special_up_func(key: GLuint, x: i32, y: i32) {
special_func_sub(key, false, x, y);
}
//pub fn modifiers()->GLuint{glutGetModifiers()}
pub fn keyboard_up_func(key: u8, x: i32, y: i32) {
keyboard_func_sub(key, false, x, y);
}
pub fn motion_func(x:i32,y:i32) {
set_mouse_pos(x, y);
let cvp = to_screenpos((x, y));
unsafe {
use self::MouseButtons as MB;
let dm=g_dragmode;
push_event(Event::TryBeginDrag(
match(g_ldrag_start,g_rdrag_start,g_mdrag_start){
(Some(_),_,_)=>MB::Left,
(None,Some(_),_)=>MB::Right,
(None,None,Some(_))=>MB::Mid,
(None,None,None)=>{panic!()}
},
cvp));
if let Some(op) = g_ldrag_start {
push_event(Event::Dragging(MB::Left, to_screenpos(op), cvp,dm))
}
if let Some(op) = g_rdrag_start {
push_event(Event::Dragging(MB::Right, to_screenpos(op), cvp,dm))
}
if let Some(op) = g_mdrag_start {
push_event(Event::Dragging(MB::Mid, to_screenpos(op), cvp,dm))
}
}
}
pub fn passive_motion_func(x:i32,y:i32){
set_mouse_pos(x,y);
push_event(Event::Move(to_screenpos((x,y))));
}
pub fn render_null(){}
pub fn idle_func(){
unsafe {glutPostRedisplay(); }
}
pub fn joystick_func(button:c_uint, dx:c_int,dy:c_int,dz:c_int){
let s:f32=1.0f32/1024.0f32;
unsafe{ g_joystick=((dx as f32 * s, dy as f32 * s),button as c_uint);}
println!("JS:{:?} {:?},{:?},{:?}",button,dx,dy,dz);
}
pub unsafe fn mouse_func(button:u32, state:u32, x:i32, y:i32){
let posi=(x,y);
let vpos = to_screenpos(posi);
let oldbs=unsafe{g_mouse_button};
//println!("mouse event {:?} {:?} fd={:?}",state, (x,y),unsafe{g_firstdrag});
if state==GLUT_DOWN{
g_mouse_button|=button;
unsafe{
g_firstdrag=true;
}
}
else {g_mouse_button&=!button};
// raw up-down events pass to window,
push_event(Event::Button (
match button as u32{
GLUT_LEFT_BUTTON =>MouseButtons::Left,
GLUT_RIGHT_BUTTON =>MouseButtons::Right,
GLUT_MID_BUTTON =>MouseButtons::Mid,
GLUT_WHEEL_UP =>MouseButtons::WheelUp,
GLUT_WHEEL_DOWN =>MouseButtons::WheelDown,
_=>MouseButtons::None
},
match state as u32{GLUT_DOWN=>true,_=>false},
to_screenpos((x,y))
));
// Now decode a bit more to establish click vs drag.
if state == GLUT_DOWN {
// we have seperate l/m/r drags because drags may be combined
// each button may be pressed or released at different times.
match button {
GLUT_LEFT_BUTTON =>g_ldrag_start=Some(posi),
GLUT_RIGHT_BUTTON =>g_rdrag_start=Some(posi),
GLUT_MID_BUTTON =>g_mdrag_start=Some(posi)
}
} else {
// Process mouse release: it may be a click or drag
let (oldpos,mb)=match button{
GLUT_LEFT_BUTTON=>(&mut g_ldrag_start,MouseButtons::Left),
GLUT_RIGHT_BUTTON=>(&mut g_rdrag_start,MouseButtons::Right),
GLUT_MID_BUTTON=>(&mut g_mdrag_start,MouseButtons::Mid),
};
if drag_mdist(oldpos.unwrap(),posi)==0{
push_event(Event::Clicked(mb,vpos))
} else{
push_event(Event::Dragged(mb,to_screenpos(oldpos.unwrap()),vpos,g_dragmode))
}
// clear the old position.
*oldpos=Option::None;
set_dragmode(DragMode::None);
}
set_mouse_pos(x,y);
}
}
fn drag_mdist(a:PixelPosi,b:PixelPosi)->i32{
let dx=b.0-a.0;
let dy=b.1-a.1;
dx.abs()+dy.abs()
}
fn drag_dist(a:PixelPosf,b:PixelPosf)->f32{
let dx=b.0-a.0;
let dy=b.1-a.1;
sqrt(dx*dx+dy*dy)
}
fn divf32(a:i32,b:i32)->f32{a as f32 / b as f32}
fn to_screenpos(s:PixelPosi)->ScreenPos{
unsafe{
vec2( divf32(s.0,g_screen_pixel_sizei.0)*2.0f32-1.0f32,
-(divf32(s.1,g_screen_pixel_sizei.1)*2.0f32-1.0f32)
)}
}
fn set_mouse_pos(x:i32,y:i32){unsafe{g_mouse_opos=g_mouse_pos; g_mouse_pos=(x,y)};}
fn get_mouse_vpos()->ScreenPos{unsafe{to_screenpos(g_mouse_pos)}}
fn get_mouse_ovpos()->ScreenPos{unsafe{to_screenpos(g_mouse_opos)}}
fn get_mouse_ppos()->PixelPosi{unsafe{g_mouse_pos}}
fn get_drag_start()->Option<ScreenPos>{unsafe{
if let Some(ipos)=g_ldrag_start { Some(to_screenpos(ipos))} else {None}
}}
fn lazy_init_ui_events(){
unsafe{
if g_ui_event.is_none(){
let mut uiv=vec![];
for x in 0..MaxEvent{uiv.push(Event::None);}
g_ui_event=Some(uiv)
}
}
}
/// push event: uses move, event queues involve passing ownership..
fn push_event(e:Event){
unsafe {
lazy_init_ui_events();
let next = (g_head + 1) & ((MaxEvent - 1) as i32);
if next != g_tail {
if let Some(ref mut uiv)=g_ui_event {uiv[g_head as usize] = e;}
g_head=next;
} else {
println!("buffer full,lost event");
}
}
}
/*
struct Singleton<T>(Option<T>);
impl<T> Singleton{
fn apply_mut<R,F:Fn(&mut T)->R>(&mut self,f:F)->R{
self.get();
if let Some(ref mut x)=self.0{f(x)}else{panic!("singleton couldn't create")}
}
fn apply<R,F:Fn(&T)->R>(&mut self,f:F)->R{
self.get();
if let Some(ref mut x)=self.0{f(x)}else{panic!("singleton couldn't create")}
}
}
*/
fn pop_event()->Option<Event>{
unsafe {
lazy_init_ui_events();
let next = (g_tail + 1) & ((MaxEvent - 1) as i32);
if g_tail == g_head {
None
} else {
let e = if let Some(ref mut uiv)=g_ui_event{uiv[g_tail as usize].clone()}else{Event::None};
g_tail = next;
Some(e)
}
}
}
#[derive(Clone,Debug,Copy)]
#[repr(u32)]
pub enum MouseButtons{
None=0,Left=0x0001,Mid=0x0002,Right=0x0004,WheelUp=0x0008,WheelDown=0x0010,WheelLeft=0x0020,WheelRight=0x0040
}
type WindowId=c_int;
//static mut g_windows:*mut Vec<(Box<Window>,WindowId)> ;
fn render_begin(){
unsafe {
gl_scissor_s(&Extents(&vec2(-1.0f32,-1.0f32),&vec2(1.0f32,1.0f32)));
//glScissor(0,0,g_screen_pixel_sizei.0,g_screen_pixel_sizei.1);
glClearColor(0.5f32, 0.5f32, 0.5f32, 1.0f32);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
fn render_end(){
unsafe {
glFlush();
glutSwapBuffers();
}
}
type IsOverlay=bool;
type Windows<A>=(Vec<(Box<Window<A>>,IsOverlay)>,WindowId);
#[cfg(target_os = "emscripten")]
fn glutGetWindow()->i32{0}
fn process_flow<APP>(next:Flow<APP>,wins:&mut Windows<APP>){
// process state flow.
//assert!(unsafe {glutGetWindow()}==wins.1);
let top=wins.0 .len();
if top==0 {return}
match next {
//todo - spawn , new glut window.
Flow::Pop() => { wins.0.pop(); }
Flow::Push(w) => { wins.0.push((w, false)); }
Flow::Replace(w) => { wins.0[top - 1] = (w, false); }
Flow::Overlay(w) => { wins.0[top - 1] = (w, true); }
Flow::SwapWith(i)=> wins.0 .swap((top-1) as usize,(top as i32 -1+i) as usize),
Flow::SetBackground(w)=>{ // current becomes an overlay on 'w'
wins.0.push((w,false));
let l=wins.0 .len();
wins.0 .swap(l-1,l-2);
wins.0[l-2].1=true;
}
Flow::SetRoot(w) => {
wins.0 .truncate(0);//discard all
wins.0 .push((w, false));
}
Flow::Cycle() => {
for i in 1..top {
wins.0.swap(i - 1, i);
}
}
_ => () // default - continue
}
}
fn from_tuple_z((x,y):(f32,f32),z:f32)->Vec3f{vec3(x,y,z)}
/// window and cursor state, passed down because the framework calculates sizes.
#[derive(Clone,Debug)]
pub struct WinCursor {
pub rect:ScreenRect,
pub aspect_ratio:f32,
pub drag_start:Option<ScreenPos>,
pub old_pos:ScreenPos,
pub pos:ScreenPos,
pub t:f32, //TODO - time ticks
}
fn mkwc(r:&ScreenRect,p:&ScreenPos)->WinCursor{
WinCursor{
rect:r.clone(), pos:p.clone(), old_pos:p.clone(), drag_start:None, t:0.0f32, aspect_ratio:get_aspect_ratio()
}
}
// arg swap because it's like a default ('none')
fn mkwcd(r:&ScreenRect,p:&ScreenPos,dragstart:&ScreenPos)->WinCursor{
WinCursor{
rect:r.clone(), pos:p.clone(), old_pos:p.clone(), drag_start:Some(dragstart.clone()),t:0.0f32, aspect_ratio:get_aspect_ratio()
}
}
fn mkwcm(r:&ScreenRect,p:&ScreenPos,opos:&ScreenPos)->WinCursor{
WinCursor{
rect:r.clone(), pos:p.clone(), old_pos:opos.clone(), drag_start:None,t:0.0f32, aspect_ratio:get_aspect_ratio()
}
}
static g_drag_color:u32=0xffffffff;
fn set_dragmode(d:DragMode){
unsafe{ g_dragmode=d;}
}
fn get_dragmode()->DragMode{
unsafe{g_dragmode}
}
unsafe fn get_drag_points()->&'static mut Vec<ScreenPos>{
if g_drag_points==0 as *mut _{
let mut f=Box::new( Vec::<ScreenPos>::new());
g_drag_points = &mut *f as _;
std::mem::forget(f);
}
&mut *g_drag_points as _
}
unsafe fn render_drag_overlay(){
match g_dragmode{
DragMode::None=>return,
_=>{},
}
let cvp=get_mouse_vpos();
// clear all rendering states
draw::identity();
let sv=to_screenpos(g_ldrag_start.unwrap()).to_vec3_z(0.0);
let ev=vec3(cvp.x,cvp.y,0.0);//from_tuple_z(cvp.into(),0.0);
match g_dragmode{
DragMode::Line=>{
draw::line(&sv, &ev);
},
DragMode::Lasso=>{
draw::lines_xy(get_drag_points(), 0.0f32, false);
}
DragMode::Freehand=>{
draw::lines_xy(get_drag_points(), 0.0f32, true);
}
DragMode::Default=> {
draw::line(&sv, &ev);
draw::rect_corners_xy(&sv, &ev, 0.1, g_drag_color);
}
DragMode::Rect=>{
draw::rect_outline(&sv, &ev);
},
DragMode::Circle=>{
draw::circle_xy(&sv, (ev.vsub(&sv)).vmagnitude() );
},
_=>{},
}
}
fn render_and_update(wins:&mut Windows<()>, a:&mut ()){
let whole_rect = Extents(&vec2(-1.0,-1.0),&vec2(1.0,1.0));
unsafe{
//render.
let top=wins.0 .len();
if top>0 {
render_begin();
let i = top - 1;
{
let wc=WinCursor{
rect:Extents(&vec2(-1.0,-1.0),&vec2(1.0,1.0)),
old_pos:get_mouse_ovpos(),
pos:get_mouse_vpos(),
drag_start:None,
aspect_ratio:get_aspect_ratio(),
t:0.0f32
};
let win = &wins.0[i];
// if it's an overlay , render previous first.
if i > 0 && win.1 {
// todo- generalize, any number of overlays
wins.0[i - 1].0.win_render(a,&wc);
}
win.0.win_render(a,&wc);
// check the keymappings,
}
{
let mut y = 0.9f32;
let win = &mut wins.0[i];
win.0.win_key_mappings(a,&mut move |k, name, _| {
draw::char_at(&(-0.95f32, y, 0.0f32), 0x00ff00, k);
draw::string_at(&(-0.9f32, y, 0.0f32), 0x00ff00, name);
y -= 0.05f32;
});
}
render_drag_overlay();
render_end();
}
// Update: process all the events.
while let Some(e)=pop_event() {
let top=wins.0 .len();
if top>0 {
process_flow(
{let win = &mut wins.0[top - 1];
win.0 .win_event(a,e,&whole_rect)}
,wins);
}
}
let dt=1.0f32 / 60.0f32;
let top=wins.0 .len();
if top>0{
// overlays still allow base to update.
if top>1 && wins.0[top-1].1 {
let flow=wins.0[top-1].0 .win_update(a,dt);
process_flow(flow,wins);
}
let flow= {
let l=wins .0 .len();
let win = &mut wins.0[l - 1];
win.0 .win_update(a,dt)
};
process_flow(flow,wins)
}
}
}
#[cfg(target_os = "emscripten")]
unsafe fn glutCheckLoop(){
}
#[cfg(target_os = "linux")]
unsafe fn glutCheckLoop(){
glutMainLoopEvent();
}
#[cfg(target_os = "emscripten")]
unsafe fn extra_callbacks(){
}
#[cfg(not(target_os = "emscripten"))]
unsafe fn extra_callbacks(){
// joystick on linux broken :(
#[cfg(not(target_os = "linux"))]
glutJoystickFunc(callbacks::joystick_func as *const u8,16);
}
#[cfg(not(target_os = "emscripten"))]
fn run_main_loop(){
}
#[cfg(target_os = "emscripten")]
fn run_main_loop(){
}
#[cfg(target_os = "emscripten")]
extern { pub fn emscripten_set_main_loop(_:*const u8,framerate:i32,one:i32);}
static mut g_windows:Option<Windows<()>>=None;
static mut g_wins:*mut Windows<()> =0 as *mut _;
static mut g_app_ptr:usize=0;
fn wins()->&'static mut Windows<()>{unsafe {
return mem::transmute::<*mut Windows<()>,&'static mut Windows<()>>(g_wins);
}}
#[cfg(target_os = "emscripten")]
unsafe fn em_main_loop_body<APP>(){
println!("emscripten main loop invokation..");
render_and_update(
&mut*g_wins,&mut ()
);
}
#[cfg(target_os = "emscripten")]
unsafe fn mock_main_loop(){
//let winptr=(g_wins_ptr as *mut Windows<T>) as &mut Windows<T>;
render_and_update(&mut*g_wins,&mut ());
glFlush();