Slider control (UI)

This commit is contained in:
yvt 2013-12-02 21:26:44 +09:00
parent 8d176e6220
commit 7726e9a3c1

View File

@ -380,7 +380,7 @@ namespace spades {
}
bool Redo() {
if(historyPos >= history.length) return false;
if(historyPos >= int(history.length)) return false;
SetHistoryPos(historyPos + 1);
return true;
}
@ -754,10 +754,10 @@ namespace spades {
}
class ScrollBarFill: ButtonBase {
private ScrollBar@ scrollBar;
private ScrollBarBase@ scrollBar;
private bool up;
ScrollBarFill(ScrollBar@ scrollBar, bool up) {
ScrollBarFill(ScrollBarBase@ scrollBar, bool up) {
super(scrollBar.Manager);
@this.scrollBar = scrollBar;
IsMouseInteractive = true;
@ -959,6 +959,186 @@ namespace spades {
}
}
class SliderKnob: UIElement {
private Slider@ slider;
private bool dragging = false;
private double startValue;
private float startCursorPos;
private bool hover = false;
SliderKnob(Slider@ slider) {
super(slider.Manager);
@this.slider = slider;
IsMouseInteractive = true;
}
private float GetCursorPos(Vector2 pos) {
return pos.x + Position.x;
}
void MouseDown(MouseButton button, Vector2 clientPosition) {
if(button != spades::ui::MouseButton::LeftMouseButton) {
return;
}
dragging = true;
startValue = slider.Value;
startCursorPos = GetCursorPos(clientPosition);
}
void MouseMove(Vector2 clientPosition) {
if(dragging) {
double val = startValue;
float delta = GetCursorPos(clientPosition) - startCursorPos;
val += delta * (slider.MaxValue - slider.MinValue) /
double(slider.TrackBarMovementRange);
slider.ScrollTo(val);
}
}
void MouseUp(MouseButton button, Vector2 clientPosition) {
if(button != spades::ui::MouseButton::LeftMouseButton) {
return;
}
dragging = false;
}
void MouseEnter() {
hover = true;
}
void MouseLeave() {
hover = false;
}
void Render() {
Renderer@ renderer = Manager.Renderer;
Vector2 pos = ScreenPosition;
Vector2 size = Size;
Image@ img = renderer.RegisterImage("Gfx/White.tga");
if (hover) {
renderer.Color = Vector4(1.f, 1.f, 1.f, 0.5f);
} else {
renderer.Color = Vector4(1.f, 1.f, 1.f, 0.3f);
}
renderer.DrawImage(img,
AABB2(pos.x + size.x * 0.5f - 3.f, pos.y,
6.f, size.y));
renderer.Color = Vector4(0.f, 0.f, 0.f, 0.6f);
renderer.DrawImage(img,
AABB2(pos.x + size.x * 0.5f - 2.f, pos.y + 1.f,
4.f, size.y - 2.f));
}
}
class Slider: ScrollBarBase {
private SliderKnob@ knob;
private ScrollBarFill@ fill1;
private ScrollBarFill@ fill2;
Slider(UIManager@ manager) {
super(manager);
@knob = SliderKnob(this);
AddChild(knob);
@fill1 = ScrollBarFill(this, false);
@fill1.Activated = EventHandler(this.LargeDown);
AddChild(fill1);
@fill2 = ScrollBarFill(this, true);
@fill2.Activated = EventHandler(this.LargeUp);
AddChild(fill2);
}
private void LargeDown(UIElement@ e) {
ScrollBy(-LargeChange);
}
private void LargeUp(UIElement@ e) {
ScrollBy(LargeChange);
}/*
private void SmallDown(UIElement@ e) {
ScrollBy(-SmallChange);
}
private void SmallUp(UIElement@ e) {
ScrollBy(SmallChange);
}*/
void OnChanged() {
Layout();
ScrollBarBase::OnChanged();
Layout();
}
void Layout() {
Vector2 size = Size;
float tPos = TrackBarPosition;
float tLen = TrackBarLength;
fill1.Bounds = AABB2(0.f, 0.f, tPos, size.y);
fill2.Bounds = AABB2(tPos + tLen, 0.f, size.x - tPos - tLen, size.y);
knob.Bounds = AABB2(tPos, 0.f, tLen, size.y);
}
void OnResized() {
Layout();
UIElement::OnResized();
}
float Length {
get {
if(Orientation == spades::ui::ScrollBarOrientation::Horizontal) {
return Size.x;
} else {
return Size.y;
}
}
}
float TrackBarAreaLength {
get {
return Length;
}
}
float TrackBarLength {
get {
return 16.f;
}
}
float TrackBarMovementRange {
get {
return TrackBarAreaLength - TrackBarLength;
}
}
float TrackBarPosition {
get {
return float((Value - MinValue) / (MaxValue - MinValue) * TrackBarMovementRange);
}
}
void Render() {
Layout();
Renderer@ renderer = Manager.Renderer;
Vector2 pos = ScreenPosition;
Vector2 size = Size;
Image@ img = renderer.RegisterImage("Gfx/White.tga");
renderer.Color = Vector4(1.f, 1.f, 1.f, 0.1f);
renderer.DrawImage(img,
AABB2(pos.x, pos.y + size.y * 0.5f - 3.f,
size.x, 6.f));
renderer.Color = Vector4(0.f, 0.f, 0.f, 0.2f);
renderer.DrawImage(img,
AABB2(pos.x + 1.f, pos.y + size.y * 0.5f - 2.f,
size.x - 2.f, 4.f));
ScrollBarBase::Render();
}
}
class ListViewModel {
int NumRows { get { return 0; } }
UIElement@ CreateElement(int row) { return null; }