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 #ifndef PATTERNHANDLER_H 10 #define PATTERNHANDLER_H 11 12 #include <stdio.h> 13 #include <string.h> 14 #include <GraphicsDefs.h> 15 #include "RGBColor.h" 16 17 class Pattern { 18 public: 19 20 Pattern(void) {} 21 22 Pattern(const uint64& p) 23 { fPattern.type64 = p; } 24 25 Pattern(const int8* p) 26 { fPattern.type64 = *((const uint64*)p); } 27 28 Pattern(const Pattern& src) 29 { fPattern.type64 = src.fPattern.type64; } 30 31 Pattern(const pattern& src) 32 { fPattern.type64 = *(uint64*)src.data; } 33 34 inline const int8* GetInt8(void) const 35 { return fPattern.type8; } 36 37 inline uint64 GetInt64(void) const 38 { return fPattern.type64; } 39 40 inline void Set(const int8* p) 41 { fPattern.type64 = *((const uint64*)p); } 42 43 inline void Set(const uint64& p) 44 { fPattern.type64 = p; } 45 46 Pattern& operator=(const Pattern& from) 47 { fPattern.type64 = from.fPattern.type64; return *this; } 48 49 Pattern& operator=(const int64 &from) 50 { fPattern.type64 = from; return *this; } 51 52 Pattern& operator=(const pattern &from) 53 { memcpy(&fPattern.type64, &from, sizeof(pattern)); return *this; } 54 55 bool operator==(const Pattern& other) const 56 { return fPattern.type64 == other.fPattern.type64; } 57 58 bool operator==(const pattern& other) const 59 { return fPattern.type64 == *(uint64*)other.data; } 60 61 private: 62 63 typedef union 64 { 65 uint64 type64; 66 int8 type8[8]; 67 } pattern_union; 68 69 pattern_union fPattern; 70 }; 71 72 extern const Pattern kSolidHigh; 73 extern const Pattern kSolidLow; 74 extern const Pattern kMixedColors; 75 76 /*! 77 \brief Class for easy calculation and use of patterns 78 79 PatternHandlers are designed specifically for DisplayDriver subclasses. 80 Pattern support can be easily added by setting the pattern to use via 81 SetTarget, and then merely retrieving the value for the coordinates 82 specified. 83 */ 84 class PatternHandler { 85 public: 86 PatternHandler(void); 87 PatternHandler(const int8* p); 88 PatternHandler(const uint64& p); 89 PatternHandler(const Pattern& p); 90 PatternHandler(const PatternHandler& other); 91 virtual ~PatternHandler(void); 92 93 void SetPattern(const int8* p); 94 void SetPattern(const uint64& p); 95 void SetPattern(const Pattern& p); 96 void SetPattern(const pattern& p); 97 98 void SetColors(const RGBColor& high, const RGBColor& low); 99 void SetHighColor(const RGBColor& color); 100 void SetLowColor(const RGBColor& color); 101 102 void SetColors(const rgb_color& high, const rgb_color& low); 103 void SetHighColor(const rgb_color& color); 104 void SetLowColor(const rgb_color& color); 105 106 107 RGBColor HighColor() const 108 { return fHighColor; } 109 RGBColor LowColor() const 110 { return fLowColor; } 111 112 RGBColor ColorAt(const BPoint& pt) const; 113 RGBColor ColorAt(float x, float y) const; 114 inline RGBColor ColorAt(int x, int y) const; 115 // TODO: any ideas for a better name of the rgb_color version of this function? 116 inline rgb_color R5ColorAt(int x, int y) const; 117 118 bool IsHighColor(const BPoint& pt) const; 119 inline bool IsHighColor(int x, int y) const; 120 inline bool IsSolidHigh() const 121 { return fPattern == B_SOLID_HIGH; } 122 inline bool IsSolidLow() const 123 { return fPattern == B_SOLID_LOW; } 124 inline bool IsSolid() const 125 { return IsSolidHigh() || IsSolidLow(); } 126 127 const pattern* GetR5Pattern(void) const 128 { return (const pattern*)fPattern.GetInt8(); } 129 const Pattern& GetPattern(void) const 130 { return fPattern; } 131 132 void SetOffsets(int32 x, int32 y); 133 134 private: 135 Pattern fPattern; 136 RGBColor fHighColor; 137 RGBColor fLowColor; 138 139 uint16 fXOffset; 140 uint16 fYOffset; 141 }; 142 143 /*! 144 \brief Obtains the color in the pattern at the specified coordinates 145 \param x X coordinate to get the color for 146 \param y Y coordinate to get the color for 147 \return Color for the coordinates 148 */ 149 inline RGBColor 150 PatternHandler::ColorAt(int x, int y) const 151 { 152 return IsHighColor(x, y) ? fHighColor : fLowColor; 153 } 154 155 /*! 156 \brief Obtains the color in the pattern at the specified coordinates 157 \param x X coordinate to get the color for 158 \param y Y coordinate to get the color for 159 \return Color for the coordinates 160 */ 161 inline rgb_color 162 PatternHandler::R5ColorAt(int x, int y) const 163 { 164 return IsHighColor(x, y) ? fHighColor.GetColor32() : fLowColor.GetColor32(); 165 } 166 167 /*! 168 \brief Obtains the value of the pattern at the specified coordinates 169 \param pt Coordinates to get the value for 170 \return Value for the coordinates - true if high, false if low. 171 */ 172 173 inline bool 174 PatternHandler::IsHighColor(int x, int y) const 175 { 176 x -= fXOffset; 177 y -= fYOffset; 178 const int8* ptr = fPattern.GetInt8(); 179 int32 value = ptr[y & 7] & (1 << (7 - (x & 7)) ); 180 181 return value != 0; 182 } 183 184 #endif 185