1 /** 2 Copyright: Copyright (c) 2014 Andrey Penechko. 3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 8 module anchovy.gui.behaviors.dragablebehavior; 9 10 import std.algorithm; 11 import std.stdio; 12 13 import anchovy.gui; 14 import anchovy.gui.interfaces.iwidgetbehavior; 15 16 class DragableBehavior : IWidgetBehavior 17 { 18 protected: 19 ivec2 _dragOffset; 20 Widget _widget; 21 22 public: 23 24 override void attachTo(Widget widget) 25 { 26 _widget = widget; 27 widget.addEventHandler(&pointerMoved); 28 widget.addEventHandler(&pointerPressed); 29 widget.addEventHandler(&pointerReleased); 30 31 widget.setProperty!"isFocusable"(true); 32 } 33 34 bool pointerMoved(Widget widget, PointerMoveEvent event) 35 { 36 if (event.context.eventDispatcher.pressedWidget is widget) 37 { 38 ivec2 deltaPos = event.pointerPosition - widget.getPropertyAs!("staticPosition", ivec2) - _dragOffset; 39 40 auto dragEvent = new DragEvent(event.pointerPosition, deltaPos, _dragOffset, _widget); 41 dragEvent.context = event.context; 42 _widget.handleEvent(dragEvent); 43 _dragOffset = dragEvent.dragOffset; 44 45 return true; 46 } 47 return false; 48 } 49 50 bool pointerPressed(Widget widget, PointerPressEvent event) 51 { 52 if (event.button == PointerButton.PB_LEFT && event.bubbling) 53 { 54 _dragOffset = event.pointerPosition - widget.getPropertyAs!("staticPosition", ivec2); 55 56 return true; 57 } 58 59 return false; 60 } 61 62 bool pointerReleased(Widget widget, PointerReleaseEvent event) 63 { 64 if (event.sinking) return false; 65 66 auto dragEndEvent = new DragEndEvent(event.pointerPosition, ivec2(0, 0), _dragOffset, _widget); 67 dragEndEvent.context = event.context; 68 _widget.handleEvent(dragEndEvent); 69 70 return true; 71 } 72 }