1/*
2 * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6#include "TwoDimensionalSliderView.h"
7
8#include <View.h>
9
10
11TwoDimensionalSliderView::TwoDimensionalSliderView(BMessage* message,
12	BMessenger target)
13	: View(BRect(0, 0, 4, 4)),
14	  BInvoker(message, target),
15	  fMinLocation(0, 0),
16	  fMaxLocation(0, 0),
17	  fDragging(false)
18{
19	SetViewColor((rgb_color){255, 0, 0, 255});
20}
21
22
23void
24TwoDimensionalSliderView::SetLocationRange(BPoint minLocation,
25	BPoint maxLocation)
26{
27	if (maxLocation.x < minLocation.x)
28		maxLocation.x = minLocation.x;
29	if (maxLocation.y < minLocation.y)
30		maxLocation.y = minLocation.y;
31
32	fMinLocation = minLocation;
33	fMaxLocation = maxLocation;
34
35	// force valid value
36	SetValue(Value());
37}
38
39
40BPoint
41TwoDimensionalSliderView::MinLocation() const
42{
43	return fMinLocation;
44}
45
46
47BPoint
48TwoDimensionalSliderView::MaxLocation() const
49{
50	return fMaxLocation;
51}
52
53
54BPoint
55TwoDimensionalSliderView::Value() const
56{
57	return Location() - fMinLocation;
58}
59
60
61void
62TwoDimensionalSliderView::SetValue(BPoint value)
63{
64	BPoint location = fMinLocation + value;
65	if (location.x < fMinLocation.x)
66		location.x = fMinLocation.x;
67	if (location.y < fMinLocation.y)
68		location.y = fMinLocation.y;
69	if (location.x > fMaxLocation.x)
70		location.x = fMaxLocation.x;
71	if (location.y > fMaxLocation.y)
72		location.y = fMaxLocation.y;
73
74	if (location != Location()) {
75		SetFrame(Frame().OffsetToCopy(location));
76
77		// send the message
78		if (Message()) {
79			BMessage message(*Message());
80			message.AddPoint("value", Value());
81			InvokeNotify(&message);
82		}
83	}
84}
85
86
87void
88TwoDimensionalSliderView::MouseDown(BPoint where, uint32 buttons,
89	int32 modifiers)
90{
91	if (fDragging)
92		return;
93
94	fOriginalLocation = Frame().LeftTop();
95	fOriginalPoint = ConvertToContainer(where);
96	fDragging = true;
97}
98
99
100void
101TwoDimensionalSliderView::MouseUp(BPoint where, uint32 buttons, int32 modifiers)
102{
103	if (!fDragging || (buttons & B_PRIMARY_MOUSE_BUTTON))
104		return;
105
106	fDragging = false;
107}
108
109
110void
111TwoDimensionalSliderView::MouseMoved(BPoint where, uint32 buttons,
112	int32 modifiers)
113{
114	if (!fDragging)
115		return;
116
117	BPoint moved = ConvertToContainer(where) - fOriginalPoint;
118	SetValue(fOriginalLocation - fMinLocation + moved);
119}
120