summaryrefslogtreecommitdiff
path: root/src/widgets/ScrollWidget.cpp
blob: f8608a5d35b4e6224e11133d2e120d8d264fdf4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "widgets.hpp"
#include "gui.hpp"


namespace rack {

ScrollWidget::ScrollWidget() {
	container = new Widget();
	addChild(container);

	horizontalScrollBar = new ScrollBar();
	horizontalScrollBar->orientation = ScrollBar::HORIZONTAL;
	addChild(horizontalScrollBar);

	verticalScrollBar = new ScrollBar();
	verticalScrollBar->orientation = ScrollBar::VERTICAL;
	addChild(verticalScrollBar);
}

void ScrollWidget::step() {
	// Clamp scroll offset
	Vec containerCorner = container->getChildrenBoundingBox().getBottomRight();
	offset = offset.clamp(Rect(Vec(0, 0), containerCorner.minus(box.size)));

	// Resize scroll bars
	Vec inner = Vec(box.size.x - verticalScrollBar->box.size.x, box.size.y - horizontalScrollBar->box.size.y);
	horizontalScrollBar->box.pos.y = inner.y;
	horizontalScrollBar->box.size.x = inner.x;
	verticalScrollBar->box.pos.x = inner.x;
	verticalScrollBar->box.size.y = inner.y;

	// Update the container's positions from the offset
	container->box.pos = offset.neg().round();

	Widget::step();
}

Widget *ScrollWidget::onMouseMove(Vec pos, Vec mouseRel) {
	if (!gFocusedWidget) {
		const float arrowSpeed = 50.0;
		if (glfwGetKey(gWindow, GLFW_KEY_LEFT) == GLFW_PRESS) {
			offset = offset.minus(Vec(1, 0).mult(arrowSpeed));
		}
		if (glfwGetKey(gWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) {
			offset = offset.minus(Vec(-1, 0).mult(arrowSpeed));
		}
		if (glfwGetKey(gWindow, GLFW_KEY_UP) == GLFW_PRESS) {
			offset = offset.minus(Vec(0, 1).mult(arrowSpeed));
		}
		if (glfwGetKey(gWindow, GLFW_KEY_DOWN) == GLFW_PRESS) {
			offset = offset.minus(Vec(0, -1).mult(arrowSpeed));
		}
	}
	return OpaqueWidget::onMouseMove(pos, mouseRel);
}

bool ScrollWidget::onScrollOpaque(Vec scrollRel) {
	offset = offset.minus(scrollRel);
	return true;
}


} // namespace rack