dragger.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package widget
  2. import "gitlab.com/beoran/ebsgo/zori/event"
  3. // import . "gitlab.com/beoran/ebsgo/zori/types"
  4. // import _ "gitlab.com/beoran/ebsgo/zori/backend"
  5. // import "gitlab.com/beoran/ebsgo/zori/state"
  6. // import "gitlab.com/beoran/ebsgo/zori/style"
  7. type Draggable interface {
  8. event.MouseHandler
  9. Drag(x, y, start_w, start_y int)
  10. Drop(x, y, start_w, start_y int)
  11. CanDrag() bool
  12. IsInside(x, y int) bool
  13. }
  14. type Dragger struct {
  15. Draggable
  16. Dragging bool
  17. StartX int
  18. StartY int
  19. }
  20. func (dr Dragger) MouseAxes(ev event.Mouse) event.Result {
  21. if dr.Draggable != nil {
  22. dr.Draggable.MouseAxes(ev)
  23. if dr.Draggable.CanDrag() && dr.Dragging {
  24. dr.Draggable.Drag(ev.X(), ev.Y(), dr.StartX, dr.StartY)
  25. }
  26. }
  27. return event.Result(false)
  28. }
  29. func (dr Dragger) MouseButtonPress(ev event.Mouse) event.Result {
  30. if dr.Draggable != nil {
  31. dr.Draggable.MouseButtonPress(ev)
  32. if (!dr.Draggable.IsInside(ev.X(), ev.Y()) || !dr.Draggable.CanDrag()) {
  33. return event.Result(false)
  34. }
  35. dr.Dragging = true
  36. dr.StartX = ev.X()
  37. dr.StartY = ev.Y()
  38. return event.Result(true)
  39. } else {
  40. return event.Result(false)
  41. }
  42. }
  43. func (dr Dragger) MouseButtonRelease(ev event.Mouse) event.Result {
  44. if dr.Draggable != nil {
  45. dr.Draggable.MouseButtonRelease(ev)
  46. if (!dr.Dragging) {
  47. return event.Result(false)
  48. }
  49. dr.Dragging = false
  50. dr.Draggable.Drop(ev.X(), ev.Y(), dr.StartX, dr.StartY)
  51. return event.Result(true)
  52. } else {
  53. return event.Result(false)
  54. }
  55. }