Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mouse button option for drag scrolling #594

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions haxe/ui/constants/MouseButton.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package haxe.ui.constants;

enum abstract MouseButton(String) from String to String {
var LEFT = 'left';
var MIDDLE = 'middle';
var RIGHT = 'right';
}
41 changes: 37 additions & 4 deletions haxe/ui/containers/ScrollView.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package haxe.ui.containers;

import haxe.ui.constants.MouseButton;
import haxe.ui.actions.ActionType;
import haxe.ui.behaviours.Behaviour;
import haxe.ui.behaviours.DataBehaviour;
Expand Down Expand Up @@ -49,6 +50,7 @@ class ScrollView extends InteractiveComponent implements IScroller {
@:clonable @:behaviour(VScrollPageSize) public var vscrollPageSize:Float;
@:clonable @:behaviour(VScrollThumbSize) public var vscrollThumbSize:Null<Float>;
@:clonable @:behaviour(ThumbSize) public var thumbSize:Null<Float>;
@:clonable @:behaviour(DefaultBehaviour, MouseButton.LEFT) public var scrollMouseButton:MouseButton;
@:clonable @:behaviour(ScrollModeBehaviour, ScrollMode.DRAG) public var scrollMode:ScrollMode;
@:clonable @:behaviour(ScrollPolicyBehaviour) public var scrollPolicy:ScrollPolicy;
@:clonable @:behaviour(HScrollPolicyBehaviour) public var horizontalScrollPolicy:ScrollPolicy;
Expand Down Expand Up @@ -689,9 +691,13 @@ class ScrollViewEvents extends haxe.ui.events.Events {
}

if (_scrollview.scrollMode == ScrollMode.DRAG || _scrollview.scrollMode == ScrollMode.INERTIAL) {
registerEvent(MouseEvent.MOUSE_DOWN, onMouseDown);
} else if (hasEvent(MouseEvent.MOUSE_DOWN, onMouseDown)) {
unregisterEvent(MouseEvent.MOUSE_DOWN, onMouseDown);
registerEvent(MouseEvent.MIDDLE_MOUSE_DOWN, onMiddleMouseDown);
registerEvent(MouseEvent.MOUSE_DOWN, onLeftMouseDown);
registerEvent(MouseEvent.RIGHT_MOUSE_DOWN, onRightMouseDown);
} else if (hasEvent(MouseEvent.MOUSE_DOWN, onLeftMouseDown)) {
unregisterEvent(MouseEvent.MIDDLE_MOUSE_DOWN, onMiddleMouseDown);
unregisterEvent(MouseEvent.MOUSE_DOWN, onLeftMouseDown);
unregisterEvent(MouseEvent.RIGHT_MOUSE_DOWN, onRightMouseDown);
}

if (_scrollview.hasEvent(UIEvent.SHOWN) == false) {
Expand Down Expand Up @@ -736,7 +742,9 @@ class ScrollViewEvents extends haxe.ui.events.Events {
vscroll.unregisterEvent(ScrollEvent.SCROLL, onVScrollScroll);
}

unregisterEvent(MouseEvent.MOUSE_DOWN, onMouseDown);
unregisterEvent(MouseEvent.MIDDLE_MOUSE_DOWN, onMiddleMouseDown);
unregisterEvent(MouseEvent.RIGHT_MOUSE_DOWN, onRightMouseDown);
unregisterEvent(MouseEvent.MOUSE_DOWN, onLeftMouseDown);
unregisterEvent(MouseEvent.MOUSE_WHEEL, onMouseWheel);
unregisterEvent(UIEvent.SHOWN, onShown);
unregisterEvent(UIEvent.COMPONENT_ADDED, onComponentAdded);
Expand Down Expand Up @@ -807,6 +815,27 @@ class ScrollViewEvents extends haxe.ui.events.Events {
private function onVScrollScroll(event:UIEvent) {
_target.dispatch(new ScrollEvent(ScrollEvent.SCROLL));
}

@:access(haxe.ui.core.Component)
private function onLeftMouseDown(event:MouseEvent) {
if (_scrollview.scrollMouseButton == MouseButton.LEFT) {
onMouseDown(event);
}
}

@:access(haxe.ui.core.Component)
private function onMiddleMouseDown(event:MouseEvent) {
if (_scrollview.scrollMouseButton == MouseButton.MIDDLE) {
onMouseDown(event);
}
}

@:access(haxe.ui.core.Component)
private function onRightMouseDown(event:MouseEvent) {
if (_scrollview.scrollMouseButton == MouseButton.RIGHT) {
onMouseDown(event);
}
}

private var _offset:Point;
private static inline var INERTIAL_TIME_CONSTANT:Int = 325;
Expand Down Expand Up @@ -867,6 +896,8 @@ class ScrollViewEvents extends haxe.ui.events.Events {
}

Screen.instance.registerEvent(MouseEvent.MOUSE_MOVE, onMouseMove);
Screen.instance.registerEvent(MouseEvent.MIDDLE_MOUSE_UP, onMouseUp);
Screen.instance.registerEvent(MouseEvent.RIGHT_MOUSE_UP, onMouseUp);
Screen.instance.registerEvent(MouseEvent.MOUSE_UP, onMouseUp);
}

Expand Down Expand Up @@ -967,6 +998,8 @@ class ScrollViewEvents extends haxe.ui.events.Events {

private function onMouseUp(event:MouseEvent) {
Screen.instance.unregisterEvent(MouseEvent.MOUSE_MOVE, onMouseMove);
Screen.instance.unregisterEvent(MouseEvent.MIDDLE_MOUSE_UP, onMouseUp);
Screen.instance.unregisterEvent(MouseEvent.RIGHT_MOUSE_UP, onMouseUp);
Screen.instance.unregisterEvent(MouseEvent.MOUSE_UP, onMouseUp);

if (_scrollview.scrollMode == ScrollMode.INERTIAL) {
Expand Down