Skip to content

Commit 28dbfa8

Browse files
committed
Scroll; add units_to_scroll_h/v which is in canvas units, and determines what exact amount to scroll when scrolling.
makes scrollbars consistent across platforms and makes the distance configurable. fix #50
1 parent 80bd9d9 commit 28dbfa8

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

mint/Scroll.hx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ typedef ScrollOptions = {
1313

1414
> ControlOptions,
1515

16+
?units_to_scroll_h: Float,
17+
?units_to_scroll_v: Float
18+
1619
} //ScrollOptions
1720

1821

@@ -27,6 +30,9 @@ class Scroll extends Control {
2730
public var onchange : Signal<Void->Void>;
2831
public var onhandlevis : Signal<Bool->Bool->Void>;
2932

33+
public var units_to_scroll_h: Float = 16;
34+
public var units_to_scroll_v: Float = 16;
35+
3036
var drag_v = false;
3137
var drag_y = 0.0;
3238
var percent_v = 0.0;
@@ -51,6 +57,9 @@ class Scroll extends Control {
5157

5258
def(options.name, 'scroll');
5359
def(options.mouse_input, true);
60+
61+
units_to_scroll_h = def(options.units_to_scroll_h, 16);
62+
units_to_scroll_v = def(options.units_to_scroll_v, 16);
5463

5564
super(_options);
5665

@@ -216,8 +225,8 @@ class Scroll extends Control {
216225

217226
} //update_scroll
218227

219-
inline function get_step_h() return 0.01;//(Math.abs(w - container.w)*0.02)/w;
220-
inline function get_step_v() return 0.01;//(Math.abs(h - container.h)*0.02)/h;
228+
inline function get_step_h() return units_to_scroll_h / container.w;
229+
inline function get_step_v() return units_to_scroll_v / container.h;
221230

222231
//Control overrides
223232

@@ -275,11 +284,13 @@ class Scroll extends Control {
275284
super.mousewheel(e);
276285

277286
if(e.x != 0 && visible_h) {
278-
set_scroll_percent(percent_h + (e.x*get_step_h()), percent_v);
287+
var _dir = Helper.sign(e.x);
288+
set_scroll_percent(percent_h + (_dir*get_step_h()), percent_v);
279289
}
280290

281291
if(e.y != 0 && visible_v) {
282-
set_scroll_percent(percent_h, percent_v + (e.y*get_step_v()));
292+
var _dir = Helper.sign(e.y);
293+
set_scroll_percent(percent_h, percent_v + (_dir*get_step_v()));
283294
}
284295

285296
} //mousewheel

mint/types/Types.hx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class Helper {
201201
return ( value < a ) ? a : ( ( value > b ) ? b : value );
202202
}
203203

204+
static inline public function sign(x:Float) : Int {
205+
return (x < 0) ? -1 : ((x > 0) ? 1 : 0);
206+
} //sign
207+
204208
static public function in_rect(x:Float, y:Float, rx:Float, ry:Float, rw:Float, rh:Float) {
205209

206210
if(x < rx) return false;

0 commit comments

Comments
 (0)