github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/framework/input.go (about) 1 package framework 2 3 import ( 4 gkey "github.com/gop9/olt/gio/io/key" 5 "github.com/gop9/olt/gio/io/pointer" 6 "image" 7 8 "github.com/gop9/olt/framework/rect" 9 ) 10 11 type mouseButton struct { 12 Down bool 13 Clicked bool 14 ClickedPos image.Point 15 } 16 17 type MouseInput struct { 18 valid bool 19 clip rect.Rect 20 Buttons [6]mouseButton 21 Pos image.Point 22 Prev image.Point 23 Delta image.Point 24 ScrollDelta int 25 } 26 27 type KeyboardInput struct { 28 Keys []gkey.Event 29 Text string 30 } 31 32 type Input struct { 33 Keyboard KeyboardInput 34 Mouse MouseInput 35 } 36 37 func (win *Window) Input() *Input { 38 if !win.toplevel() { 39 return &Input{} 40 } 41 win.ctx.Input.Mouse.clip = win.cmds.Clip 42 return &win.ctx.Input 43 } 44 45 func (win *Window) scrollwheelInput() *Input { 46 if win.ctx.scrollwheelFocus == win.idx { 47 return &win.ctx.Input 48 } 49 return &Input{} 50 } 51 52 func (win *Window) KeyboardOnHover(bounds rect.Rect) KeyboardInput { 53 if !win.toplevel() || !win.ctx.Input.Mouse.HoveringRect(bounds) { 54 return KeyboardInput{} 55 } 56 return win.ctx.Input.Keyboard 57 } 58 59 func (i *MouseInput) HasClickInRect(id pointer.Buttons, b rect.Rect) bool { 60 btn := &i.Buttons[id] 61 return unify(b, i.clip).Contains(btn.ClickedPos) 62 } 63 64 func (i *MouseInput) IsClickInRect(id pointer.Buttons, b rect.Rect) bool { 65 return i.IsClickDownInRect(id, b, false) 66 } 67 68 func (i *MouseInput) IsClickDownInRect(id pointer.Buttons, b rect.Rect, down bool) bool { 69 btn := &i.Buttons[id] 70 return i.HasClickInRect(id, b) && btn.Down == down && btn.Clicked 71 } 72 73 func (i *MouseInput) AnyClickInRect(b rect.Rect) bool { 74 return i.IsClickInRect(pointer.ButtonLeft, b) || i.IsClickInRect(pointer.ButtonMiddle, b) || i.IsClickInRect(pointer.ButtonRight, b) 75 } 76 77 func (i *MouseInput) HoveringRect(rect rect.Rect) bool { 78 return i.valid && unify(rect, i.clip).Contains(i.Pos) 79 } 80 81 func (i *MouseInput) PrevHoveringRect(rect rect.Rect) bool { 82 return i.valid && unify(rect, i.clip).Contains(i.Prev) 83 } 84 85 func (i *MouseInput) Clicked(id pointer.Buttons, rect rect.Rect) bool { 86 if !i.HoveringRect(rect) { 87 return false 88 } 89 return i.IsClickInRect(id, rect) 90 } 91 92 func (i *MouseInput) Down(id pointer.Buttons) bool { 93 return i.Buttons[id].Down 94 } 95 96 func (i *MouseInput) Pressed(id pointer.Buttons) bool { 97 return i.Buttons[id].Down && i.Buttons[id].Clicked 98 } 99 100 func (i *MouseInput) Released(id pointer.Buttons) bool { 101 return !(i.Buttons[id].Down) && i.Buttons[id].Clicked 102 } 103 104 func (i *KeyboardInput) Pressed(key gkey.Code) bool { 105 for _, k := range i.Keys { 106 if k.Code == key { 107 return true 108 } 109 } 110 return false 111 } 112 113 func (win *Window) inputMaybe(widgetValid bool) *Input { 114 if widgetValid && win.toplevel() && win.flags&windowEnabled != 0 { 115 win.ctx.Input.Mouse.clip = win.cmds.Clip 116 return &win.ctx.Input 117 } 118 return &Input{} 119 } 120 121 func (win *Window) toplevel() bool { 122 if win.moving { 123 return false 124 } 125 if win.ctx.dockedWindowFocus != 0 && win.idx == win.ctx.dockedWindowFocus { 126 return true 127 } 128 return win.idx == win.ctx.floatWindowFocus 129 }