Skip to content

Commit

Permalink
Merge pull request #594 from inc0der-forks/add-scroll-mouse-button-pr…
Browse files Browse the repository at this point in the history
…operty

add mouse button option for drag scrolling
  • Loading branch information
ianharrigan authored Apr 6, 2024
2 parents b44e521 + 090f90b commit db93937
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
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

0 comments on commit db93937

Please sign in to comment.