1 /* 2 * Copyright (c) 2008-2009, Haiku, Inc. 3 * Distributed under the terms of the MIT license. 4 * 5 * Authors: 6 * Artur Wyszynski <harakash@gmail.com> 7 */ 8 9 10 #include "GradientsView.h" 11 #include <GradientLinear.h> 12 #include <GradientRadial.h> 13 #include <GradientRadialFocus.h> 14 #include <GradientDiamond.h> 15 #include <GradientConic.h> 16 17 18 GradientsView::GradientsView(const BRect &rect) 19 : BView(rect, "gradientsview", B_FOLLOW_ALL, B_WILL_DRAW | B_PULSE_NEEDED), 20 fType(BGradient::TYPE_LINEAR) 21 { 22 } 23 24 25 GradientsView::~GradientsView() 26 { 27 } 28 29 30 void 31 GradientsView::Draw(BRect update) 32 { 33 switch (fType) { 34 case BGradient::TYPE_LINEAR: 35 DrawLinear(update); 36 break; 37 38 case BGradient::TYPE_RADIAL: 39 DrawRadial(update); 40 break; 41 42 case BGradient::TYPE_RADIAL_FOCUS: 43 DrawRadialFocus(update); 44 break; 45 46 case BGradient::TYPE_DIAMOND: 47 DrawDiamond(update); 48 break; 49 50 case BGradient::TYPE_CONIC: 51 DrawConic(update); 52 break; 53 54 case BGradient::TYPE_NONE: 55 default: 56 break; 57 } 58 } 59 60 61 void 62 GradientsView::DrawLinear(BRect update) 63 { 64 BGradientLinear gradient; 65 rgb_color c; 66 c.red = 255; 67 c.green = 0; 68 c.blue = 0; 69 gradient.AddColor(c, 0); 70 c.red = 0; 71 c.green = 255; 72 c.blue = 0; 73 gradient.AddColor(c, 127); 74 c.red = 0; 75 c.green = 0; 76 c.blue = 255; 77 gradient.AddColor(c, 255); 78 79 // RoundRect 80 SetHighColor(0, 0, 0); 81 FillRoundRect(BRect(10, 10, 110, 110), 5, 5); 82 gradient.SetStart(BPoint(120, 10)); 83 gradient.SetEnd(BPoint(220, 110)); 84 FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient); 85 86 // Rect 87 SetHighColor(0, 0, 0); 88 FillRect(BRect(10, 120, 110, 220)); 89 gradient.SetStart(BPoint(120, 120)); 90 gradient.SetEnd(BPoint(220, 220)); 91 FillRect(BRect(120, 120, 220, 220), gradient); 92 93 // Triangle 94 SetHighColor(0, 0, 0); 95 FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330)); 96 gradient.SetStart(BPoint(60, 230)); 97 gradient.SetEnd(BPoint(60, 330)); 98 FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330), 99 gradient); 100 101 // Ellipse 102 SetHighColor(0, 0, 0); 103 FillEllipse(BPoint(60, 390), 50, 50); 104 gradient.SetStart(BPoint(60, 340)); 105 gradient.SetEnd(BPoint(60, 440)); 106 FillEllipse(BPoint(170, 390), 50, 50, gradient); 107 } 108 109 110 void 111 GradientsView::DrawRadial(BRect update) 112 { 113 BGradientRadial gradient; 114 gradient.AddColor(make_color(255, 0, 0), 0); 115 gradient.AddColor(make_color(0, 255, 0), 127); 116 gradient.AddColor(make_color(0, 0, 255), 255); 117 118 // RoundRect 119 SetHighColor(0, 0, 0); 120 FillRoundRect(BRect(10, 10, 110, 110), 5, 5); 121 gradient.SetCenter(BPoint(170, 60)); 122 gradient.SetRadius(50); 123 FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient); 124 125 // Rect 126 SetHighColor(0, 0, 0); 127 FillRect(BRect(10, 120, 110, 220)); 128 gradient.SetCenter(BPoint(170, 170)); 129 FillRect(BRect(120, 120, 220, 220), gradient); 130 131 // Triangle 132 SetHighColor(0, 0, 0); 133 FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330)); 134 gradient.SetCenter(BPoint(170, 280)); 135 FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330), 136 gradient); 137 138 // Ellipse 139 SetHighColor(0, 0, 0); 140 FillEllipse(BPoint(60, 390), 50, 50); 141 gradient.SetCenter(BPoint(170, 390)); 142 FillEllipse(BPoint(170, 390), 50, 50, gradient); 143 } 144 145 146 void 147 GradientsView::DrawRadialFocus(BRect update) 148 { 149 BGradientRadialFocus gradient; 150 rgb_color c; 151 c.red = 255; 152 c.green = 0; 153 c.blue = 0; 154 gradient.AddColor(c, 0); 155 c.red = 0; 156 c.green = 255; 157 c.blue = 0; 158 gradient.AddColor(c, 127); 159 c.red = 0; 160 c.green = 0; 161 c.blue = 255; 162 gradient.AddColor(c, 255); 163 164 // RoundRect 165 SetHighColor(0, 0, 0); 166 FillRoundRect(BRect(10, 10, 110, 110), 5, 5); 167 gradient.SetCenter(BPoint(170, 60)); 168 FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient); 169 170 // Rect 171 SetHighColor(0, 0, 0); 172 FillRect(BRect(10, 120, 110, 220)); 173 gradient.SetCenter(BPoint(170, 170)); 174 FillRect(BRect(120, 120, 220, 220), gradient); 175 176 // Triangle 177 SetHighColor(0, 0, 0); 178 FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330)); 179 gradient.SetCenter(BPoint(170, 280)); 180 FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330), 181 gradient); 182 183 // Ellipse 184 SetHighColor(0, 0, 0); 185 FillEllipse(BPoint(60, 390), 50, 50); 186 gradient.SetCenter(BPoint(170, 390)); 187 FillEllipse(BPoint(170, 390), 50, 50, gradient); 188 } 189 190 191 void 192 GradientsView::DrawDiamond(BRect update) 193 { 194 BGradientDiamond gradient; 195 rgb_color c; 196 c.red = 255; 197 c.green = 0; 198 c.blue = 0; 199 gradient.AddColor(c, 0); 200 c.red = 0; 201 c.green = 255; 202 c.blue = 0; 203 gradient.AddColor(c, 127); 204 c.red = 0; 205 c.green = 0; 206 c.blue = 255; 207 gradient.AddColor(c, 255); 208 209 // RoundRect 210 SetHighColor(0, 0, 0); 211 FillRoundRect(BRect(10, 10, 110, 110), 5, 5); 212 gradient.SetCenter(BPoint(170, 60)); 213 FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient); 214 215 // Rect 216 SetHighColor(0, 0, 0); 217 FillRect(BRect(10, 120, 110, 220)); 218 gradient.SetCenter(BPoint(170, 170)); 219 FillRect(BRect(120, 120, 220, 220), gradient); 220 221 // Triangle 222 SetHighColor(0, 0, 0); 223 FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330)); 224 gradient.SetCenter(BPoint(170, 280)); 225 FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330), 226 gradient); 227 228 // Ellipse 229 SetHighColor(0, 0, 0); 230 FillEllipse(BPoint(60, 390), 50, 50); 231 gradient.SetCenter(BPoint(170, 390)); 232 FillEllipse(BPoint(170, 390), 50, 50, gradient); 233 } 234 235 236 void 237 GradientsView::DrawConic(BRect update) 238 { 239 BGradientConic gradient; 240 rgb_color c; 241 c.red = 255; 242 c.green = 0; 243 c.blue = 0; 244 gradient.AddColor(c, 0); 245 c.red = 0; 246 c.green = 255; 247 c.blue = 0; 248 gradient.AddColor(c, 127); 249 c.red = 0; 250 c.green = 0; 251 c.blue = 255; 252 gradient.AddColor(c, 255); 253 254 // RoundRect 255 SetHighColor(0, 0, 0); 256 FillRoundRect(BRect(10, 10, 110, 110), 5, 5); 257 gradient.SetCenter(BPoint(170, 60)); 258 FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient); 259 260 // Rect 261 SetHighColor(0, 0, 0); 262 FillRect(BRect(10, 120, 110, 220)); 263 gradient.SetCenter(BPoint(170, 170)); 264 FillRect(BRect(120, 120, 220, 220), gradient); 265 266 // Triangle 267 SetHighColor(0, 0, 0); 268 FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330)); 269 gradient.SetCenter(BPoint(170, 280)); 270 FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330), 271 gradient); 272 273 // Ellipse 274 SetHighColor(0, 0, 0); 275 FillEllipse(BPoint(60, 390), 50, 50); 276 gradient.SetCenter(BPoint(170, 390)); 277 FillEllipse(BPoint(170, 390), 50, 50, gradient); 278 } 279 280 281 void 282 GradientsView::SetType(BGradient::Type type) 283 { 284 fType = type; 285 Invalidate(); 286 } 287