1/*
2 * Copyright (c) 2001-2007, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
4 *
5 * Author:	DarkWyrm <bpmagic@columbus.rr.com>
6 *			Stephan A��mus <superstippi@gmx.de>
7 */
8
9#include "PatternHandler.h"
10
11#include <stdio.h>
12
13#include <Point.h>
14
15const Pattern kSolidHigh(0xFFFFFFFFFFFFFFFFLL);
16const Pattern kSolidLow((uint64)0);
17const Pattern kMixedColors(0xAAAAAAAAAAAAAAAALL);
18
19const rgb_color kBlack = (rgb_color){ 0, 0, 0, 255 };
20const rgb_color kWhite = (rgb_color){ 255, 255, 255, 255 };
21
22/*!
23	\brief Void constructor
24
25	The pattern is set to B_SOLID_HIGH, high color is set to black, and
26	low color is set to white.
27*/
28PatternHandler::PatternHandler(void)
29	: fPattern(kSolidHigh),
30	  fHighColor(kBlack),
31	  fLowColor(kWhite),
32	  fXOffset(0),
33	  fYOffset(0)
34{
35}
36
37/*!
38	\brief Constructor initializes to given pattern
39	\param pat Pattern to use.
40
41	This initializes to the given pattern or B_SOLID_HIGH if the pattern
42	is NULL. High color is set to black, and low color is set to white.
43*/
44PatternHandler::PatternHandler(const int8* pat)
45	: fPattern(pat ? Pattern(pat) : Pattern(kSolidHigh)),
46	  fHighColor(kBlack),
47	  fLowColor(kWhite),
48	  fXOffset(0),
49	  fYOffset(0)
50{
51}
52
53/*!
54	\brief Constructor initializes to given pattern
55	\param pat Pattern to use.
56
57	This initializes to the given pattern or B_SOLID_HIGH if the pattern
58	is NULL. High color is set to black, and low color is set to white.
59*/
60PatternHandler::PatternHandler(const uint64& pat)
61	: fPattern(pat),
62	  fHighColor(kBlack),
63	  fLowColor(kWhite),
64	  fXOffset(0),
65	  fYOffset(0)
66{
67}
68
69/*!
70	\brief Constructor initializes to given pattern
71	\param pat Pattern to use.
72
73	This initializes to the given Pattern.
74	High color is set to black, and low color is set to white.
75*/
76PatternHandler::PatternHandler(const Pattern& pat)
77	: fPattern(pat),
78	  fHighColor(kBlack),
79	  fLowColor(kWhite),
80	  fXOffset(0),
81	  fYOffset(0)
82{
83}
84
85/*!
86	\brief Constructor initializes to given PatternHandler
87	\param other PatternHandler to copy.
88
89	Copy constructor.
90*/
91PatternHandler::PatternHandler(const PatternHandler& other)
92	: fPattern(other.fPattern),
93	  fHighColor(other.fHighColor),
94	  fLowColor(other.fLowColor),
95	  fXOffset(other.fXOffset),
96	  fYOffset(other.fYOffset)
97{
98}
99
100//! Destructor does nothing
101PatternHandler::~PatternHandler(void)
102{
103}
104
105/*!
106	\brief Sets the pattern for the handler to the one given
107	\param pat Pattern to use.
108
109	This initializes to the given pattern or B_SOLID_HIGH if the pattern
110	is NULL.
111*/
112void
113PatternHandler::SetPattern(const int8* pat)
114{
115	if (pat)
116		fPattern.Set(pat);
117	else
118		fPattern = kSolidHigh;
119}
120
121/*!
122	\brief Sets the pattern for the handler to the one given
123	\param pat Pattern to use.
124*/
125void
126PatternHandler::SetPattern(const uint64& pat)
127{
128	fPattern = pat;
129}
130
131/*!
132	\brief Sets the pattern for the handler to the one given
133	\param pat Pattern to use.
134*/
135void
136PatternHandler::SetPattern(const Pattern& pat)
137{
138	fPattern = pat;
139}
140
141/*!
142	\brief Sets the pattern for the handler to the one given
143	\param pat R5 style pattern to use.
144*/
145void
146PatternHandler::SetPattern(const pattern& pat)
147{
148	fPattern = pat;
149}
150
151/*!
152	\brief Set the colors for the pattern to use
153	\param high High color for the handler
154	\param low Low color for the handler
155*/
156void
157PatternHandler::SetColors(const rgb_color& high, const rgb_color& low)
158{
159	fHighColor = high;
160	fLowColor = low;
161}
162
163/*!
164	\brief Set the high color for the pattern to use
165	\param color High color for the handler
166*/
167void
168PatternHandler::SetHighColor(const rgb_color& color)
169{
170	fHighColor = color;
171}
172
173/*!
174	\brief Set the low color for the pattern to use
175	\param color Low color for the handler
176*/
177void
178PatternHandler::SetLowColor(const rgb_color& color)
179{
180	fLowColor = color;
181}
182
183/*!
184	\brief Obtains the color in the pattern at the specified coordinates
185	\param pt Coordinates to get the color for
186	\return Color for the coordinates
187*/
188rgb_color
189PatternHandler::ColorAt(const BPoint &pt) const
190{
191	return ColorAt(pt.x, pt.y);
192}
193
194/*!
195	\brief Obtains the color in the pattern at the specified coordinates
196	\param x X coordinate to get the color for
197	\param y Y coordinate to get the color for
198	\return Color for the coordinates
199*/
200rgb_color
201PatternHandler::ColorAt(float x, float y) const
202{
203	return ColorAt(int(x), int(y));
204}
205
206/*!
207	\brief Obtains the value of the pattern at the specified coordinates
208	\param pt Coordinates to get the value for
209	\return Value for the coordinates - true if high, false if low.
210*/
211bool
212PatternHandler::IsHighColor(const BPoint &pt) const
213{
214	return IsHighColor((int)pt.x, (int)pt.y);
215}
216
217/*!
218	\brief Transfers the scrolling offset of a BView to affect the pattern.
219	\param x Positive or negative horizontal offset
220	\param y Positive or negative vertical offset
221*/
222void
223PatternHandler::SetOffsets(int32 x, int32 y)
224{
225	fXOffset = x & 7;
226	fYOffset = y & 7;
227}
228
229