Skip to content

Commit

Permalink
Make Slider use CanvasIO
Browse files Browse the repository at this point in the history
Applies to AbstractSlider and SliderHandle as well
  • Loading branch information
ArthaTi committed Jan 17, 2025
1 parent 668979c commit 66bce2e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
12 changes: 9 additions & 3 deletions source/fluid/slider.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import fluid.backend;
import fluid.structs;

import fluid.io.hover;
import fluid.io.canvas;

@safe:

Expand Down Expand Up @@ -93,6 +94,7 @@ abstract class AbstractSlider : InputNode!Node {
enum railWidth = 4;
enum minStepDistance = 10;

CanvasIO canvasIO;
HoverIO hoverIO;

public {
Expand Down Expand Up @@ -137,6 +139,7 @@ abstract class AbstractSlider : InputNode!Node {

override void resizeImpl(Vector2 space) {

use(canvasIO);
use(hoverIO);

resizeChild(handle, space);
Expand All @@ -157,7 +160,7 @@ abstract class AbstractSlider : InputNode!Node {
_isPressed = checkIsPressed();

// Draw the rail
style.drawBackground(io, rail);
style.drawBackground(io, canvasIO, rail);

const availableWidth = rail.width - handle.size.x;
const handleOffset = availableWidth * index / (length - 1f);
Expand All @@ -180,7 +183,7 @@ abstract class AbstractSlider : InputNode!Node {
const start = Vector2(firstStepX + visualStepDistance * step, end(rail).y);
const end = Vector2(start.x, end(outer).y);

style.drawLine(io, start, end);
style.drawLine(io, canvasIO, start, end);

}

Expand Down Expand Up @@ -296,6 +299,8 @@ class SliderRangeImpl(T) : SliderRange!(ElementType!T) {
/// Defines the handle of a slider.
class SliderHandle : Node {

CanvasIO canvasIO;

public {

Vector2 size = Vector2(16, 20);
Expand All @@ -310,13 +315,14 @@ class SliderHandle : Node {

override void resizeImpl(Vector2 space) {

use(canvasIO);
minSize = size;

}

override void drawImpl(Rectangle outer, Rectangle inner) {

style.drawBackground(io, outer);
style.drawBackground(io, canvasIO, outer);

}

Expand Down
38 changes: 38 additions & 0 deletions tests/nodes/slider.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ import fluid;

@safe:

@("Slider draws a rail and a handle")
unittest {

auto input = slider!int(
.layout!"fill",
iota(1, 4)
);
auto root = sizeLock!testSpace(
.sizeLimit(500, 200),
nullTheme.derive(
rule!AbstractSlider(
Rule.backgroundColor = color("#000"),
Rule.lineColor = color("#f00"),
),
rule!SliderHandle(
Rule.backgroundColor = color("#0f0"),
),
),
input,
);

root.drawAndAssert(

// Rail
input.drawsRectangle(0, 8, 500, 4).ofColor("#000"),

// Marks
input.drawsLine().from( 8, 12).to( 8, 20).ofWidth(1).ofColor("#f00"),
input.drawsLine().from(250, 12).to(250, 20).ofWidth(1).ofColor("#f00"),
input.drawsLine().from(492, 12).to(492, 20).ofWidth(1).ofColor("#f00"),

// Handle
input.handle.drawsRectangle(0, 0, 16, 20).ofColor("#0f0"),

);

}

@("Slider can be changed with mouse movements")
unittest {

Expand Down

0 comments on commit 66bce2e

Please sign in to comment.