Skip to content

Commit

Permalink
Merge pull request #660 from Starmapo/force-step-property
Browse files Browse the repository at this point in the history
Add `forceStep` property to NumberStepper
  • Loading branch information
ianharrigan authored Jan 28, 2025
2 parents cffd161 + 5b18e59 commit 0fddeba
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
17 changes: 13 additions & 4 deletions haxe/ui/components/NumberStepper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class NumberStepper extends InteractiveComponent implements ICompositeInteractiv
*/
@:clonable @:behaviour(DefaultBehaviour, null) public var step:Null<Float>;

/**
* If true, values that differ from the specified `step` will be marked as invalid.
*/
@:clonable @:behaviour(DefaultBehaviour, false) public var forceStep:Bool;

/**
* The highest value this stepper can get to, even when set through code.
*/
Expand Down Expand Up @@ -308,7 +313,7 @@ private class Events extends haxe.ui.events.Events {
}
var parsedValue = Std.parseFloat(value.text);
parsedValue = MathUtil.clamp(parsedValue, _stepper.min, _stepper.max);
if (_stepper.step != null && parsedValue % _stepper.step != 0) {
if (_stepper.step != null && _stepper.forceStep && parsedValue % _stepper.step != 0) {
parsedValue = MathUtil.roundToNearest(parsedValue, _stepper.step);
}

Expand Down Expand Up @@ -491,7 +496,7 @@ private class ValueHelper {
valid = false;
}

if (step != null && MathUtil.fmodulo(parsedValue, step) != 0) {
if (step != null && stepper.forceStep && MathUtil.fmodulo(parsedValue, step) != 0) {
valid = false;
parsedValue = MathUtil.roundToNearest(parsedValue, step);
}
Expand Down Expand Up @@ -523,7 +528,9 @@ private class ValueHelper {
}
} else {
newValue += step;
newValue = MathUtil.roundToNearest(newValue, step);
if (stepper.forceStep) {
newValue = MathUtil.roundToNearest(newValue, step);
}
}

if (max != null && newValue > max) {
Expand All @@ -549,7 +556,9 @@ private class ValueHelper {
}
} else {
newValue -= step;
newValue = MathUtil.roundToNearest(newValue, step);
if (stepper.forceStep) {
newValue = MathUtil.roundToNearest(newValue, step);
}
}

if (min != null && newValue < min) {
Expand Down
1 change: 1 addition & 0 deletions haxe/ui/containers/properties/Property.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Property extends HBox implements IDataComponent {
@:clonable @:behaviour(DefaultBehaviour, null) public var min:Null<Float>;
@:clonable @:behaviour(DefaultBehaviour, null) public var max:Null<Float>;
@:clonable @:behaviour(DefaultBehaviour, null) public var step:Null<Float>;
@:clonable @:behaviour(DefaultBehaviour, null) public var forceStep:Null<Bool>;
@:clonable @:behaviour(DefaultBehaviour, null) public var precision:Null<Int>;
@:behaviour(DataSourceBehaviour) public var dataSource:DataSource<Dynamic>;
}
Expand Down
3 changes: 3 additions & 0 deletions haxe/ui/containers/properties/PropertyEditor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class PropertyEditorNumber extends PropertyEditor {
if (property.step != null) {
numberStepper.step = property.step;
}
if (property.forceStep != null) {
numberStepper.forceStep = property.forceStep;
}
if (property.precision != null) {
numberStepper.precision = property.precision;
}
Expand Down
3 changes: 3 additions & 0 deletions haxe/ui/containers/properties/PropertyGroup_OLD.hx
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ private class Builder extends CompositeBuilder {
if (property.step != null) {
stepper.step = property.step;
}
if (property.forceStep != null) {
stepper.forceStep = property.forceStep;
}
if (property.precision != null) {
stepper.precision = property.precision;
}
Expand Down
1 change: 1 addition & 0 deletions haxe/ui/containers/properties/Property_OLD.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Property_OLD extends HBox implements IDataComponent {
@:behaviour(DataSourceBehaviour) public var dataSource:DataSource<Dynamic>;
@:clonable @:behaviour(PropertyValueBehaviour) public var value:Dynamic;
@:clonable @:behaviour(DefaultBehaviour) public var step:Null<Float>;
@:clonable @:behaviour(DefaultBehaviour) public var forceStep:Null<Bool>;
@:clonable @:behaviour(DefaultBehaviour) public var min:Null<Float>;
@:clonable @:behaviour(DefaultBehaviour) public var max:Null<Float>;
@:clonable @:behaviour(DefaultBehaviour) public var precision:Null<Int>;
Expand Down

0 comments on commit 0fddeba

Please sign in to comment.