Style clean up.

This commit is contained in:
Stephan Aßmus
2012-05-12 13:56:09 +02:00
parent f50e7b8dc5
commit a184e9f5b1
2 changed files with 69 additions and 62 deletions
-1
View File
@@ -6,7 +6,6 @@
* Stephan Aßmus <[email protected]> * Stephan Aßmus <[email protected]>
*/ */
#include "StyleView.h" #include "StyleView.h"
#include <new> #include <new>
+69 -61
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2006, Haiku. * Copyright 2006-2012, Haiku.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -22,25 +22,28 @@
#include "Group.h" #include "Group.h"
#include "SwatchView.h" #include "SwatchView.h"
enum { enum {
MSG_SET_COLOR = 'stcl', MSG_SET_COLOR = 'stcl',
MSG_COLOR_PICKER = 'clpk', MSG_COLOR_PICKER = 'clpk',
MSG_ALPHA_SLIDER = 'alps', MSG_ALPHA_SLIDER = 'alps',
}; };
#define SWATCH_VIEW_WIDTH 20 #define SWATCH_VIEW_WIDTH 20
#define SWATCH_VIEW_HEIGHT 15 #define SWATCH_VIEW_HEIGHT 15
// constructor
SwatchGroup::SwatchGroup(BRect frame) SwatchGroup::SwatchGroup(BRect frame)
: BView(frame, "style view", B_FOLLOW_NONE, 0), :
BView(frame, "style view", B_FOLLOW_NONE, 0),
fCurrentColor(NULL), fCurrentColor(NULL),
fIgnoreNotifications(false), fIgnoreNotifications(false),
fColorPickerPanel(NULL), fColorPickerPanel(NULL),
fColorPickerMode(H_SELECTED), fColorPickerMode(H_SELECTED),
fColorPickerFrame(100.0, 100.0, 200.0, 200.0) fColorPickerFrame(100.0, 100.0, 200.0, 200.0)
{ {
frame = BRect(0, 0, 100, 15); frame = BRect(0, 0, 100, 15);
fTopSwatchViews = new Group(frame, "top swatch group"); fTopSwatchViews = new Group(frame, "top swatch group");
@@ -62,32 +65,33 @@ SwatchGroup::SwatchGroup(BRect frame)
h = ((float)(i - 9) / 10.0) * 6.0; h = ((float)(i - 9) / 10.0) * 6.0;
v = 0.5; v = 0.5;
} }
HSV_to_RGB(h, s, v, r, g, b); HSV_to_RGB(h, s, v, r, g, b);
color.red = (uint8)(255.0 * r); color.red = (uint8)(255.0 * r);
color.green = (uint8)(255.0 * g); color.green = (uint8)(255.0 * g);
color.blue = (uint8)(255.0 * b); color.blue = (uint8)(255.0 * b);
fSwatchViews[i] = new SwatchView("swatch", new BMessage(MSG_SET_COLOR), fSwatchViews[i] = new SwatchView("swatch", new BMessage(MSG_SET_COLOR),
this, color, this, color, SWATCH_VIEW_WIDTH, SWATCH_VIEW_HEIGHT);
SWATCH_VIEW_WIDTH,
SWATCH_VIEW_HEIGHT);
fSwatchViews[i]->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP); fSwatchViews[i]->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
if (i < 10) if (i < 10)
fTopSwatchViews->AddChild(fSwatchViews[i]); fTopSwatchViews->AddChild(fSwatchViews[i]);
else else
fBottomSwatchViews->AddChild(fSwatchViews[i]); fBottomSwatchViews->AddChild(fSwatchViews[i]);
} }
// crrate current color swatch view
// create current color swatch view
fCurrentColorSV = new SwatchView("current swatch", fCurrentColorSV = new SwatchView("current swatch",
new BMessage(MSG_COLOR_PICKER), new BMessage(MSG_COLOR_PICKER), this, color, 28.0, 28.0);
this, color, 28.0, 28.0);
// create color field and slider // create color field and slider
fColorField = new ColorField(BPoint(0.0, 0.0), H_SELECTED, fColorField = new ColorField(BPoint(0.0, 0.0), H_SELECTED,
1.0, B_HORIZONTAL); 1.0, B_HORIZONTAL);
fColorSlider = new ColorSlider(BPoint(0.0, 0.0), H_SELECTED, fColorSlider = new ColorSlider(BPoint(0.0, 0.0), H_SELECTED,
1.0, 1.0, B_HORIZONTAL); 1.0, 1.0, B_HORIZONTAL);
fAlphaSlider = new AlphaSlider(B_HORIZONTAL, fAlphaSlider = new AlphaSlider(B_HORIZONTAL,
new BMessage(MSG_ALPHA_SLIDER)); new BMessage(MSG_ALPHA_SLIDER));
// layout gui // layout gui
fTopSwatchViews->SetSpacing(0, 0); fTopSwatchViews->SetSpacing(0, 0);
@@ -98,18 +102,18 @@ SwatchGroup::SwatchGroup(BRect frame)
fBottomSwatchViews->SetResizingMode(B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); fBottomSwatchViews->SetResizingMode(B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
float paletteHeight = fBottomSwatchViews->Frame().Height() float paletteHeight = fBottomSwatchViews->Frame().Height()
+ fTopSwatchViews->Frame().Height() + 1; + fTopSwatchViews->Frame().Height() + 1;
fTopSwatchViews->MoveTo(paletteHeight + 2, 4); fTopSwatchViews->MoveTo(paletteHeight + 2, 4);
fBottomSwatchViews->MoveTo(paletteHeight + 2, fBottomSwatchViews->MoveTo(paletteHeight + 2,
fTopSwatchViews->Frame().bottom + 1); fTopSwatchViews->Frame().bottom + 1);
fCurrentColorSV->MoveTo(0, fTopSwatchViews->Frame().top); fCurrentColorSV->MoveTo(0, fTopSwatchViews->Frame().top);
fCurrentColorSV->ResizeTo(paletteHeight, paletteHeight); fCurrentColorSV->ResizeTo(paletteHeight, paletteHeight);
fCurrentColorSV->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP); fCurrentColorSV->SetResizingMode(B_FOLLOW_LEFT | B_FOLLOW_TOP);
float width = fTopSwatchViews->Frame().right float width = fTopSwatchViews->Frame().right
- fCurrentColorSV->Frame().left; - fCurrentColorSV->Frame().left;
fColorField->ResizeTo(width, 40); fColorField->ResizeTo(width, 40);
fColorField->FrameResized(width, 40); fColorField->FrameResized(width, 40);
@@ -136,34 +140,32 @@ SwatchGroup::SwatchGroup(BRect frame)
AddChild(fAlphaSlider); AddChild(fAlphaSlider);
} }
// destructor
SwatchGroup::~SwatchGroup() SwatchGroup::~SwatchGroup()
{ {
SetCurrentColor(NULL); SetCurrentColor(NULL);
} }
// ObjectChanged
void void
SwatchGroup::ObjectChanged(const Observable* object) SwatchGroup::ObjectChanged(const Observable* object)
{ {
if (object == fCurrentColor) { if (object != fCurrentColor || fIgnoreNotifications)
rgb_color color = fCurrentColor->Color(); return;
if (!fIgnoreNotifications) { rgb_color color = fCurrentColor->Color();
float h, s, v;
RGB_to_HSV(color.red / 255.0, float h, s, v;
color.green / 255.0, RGB_to_HSV(color.red / 255.0, color.green / 255.0, color.blue / 255.0,
color.blue / 255.0, h, s, v);
h, s, v);
_SetColor(h, s, v, color.alpha);
_SetColor(h, s, v, color.alpha);
}
}
} }
// #pragma mark - // #pragma mark -
// AttachedToWindow
void void
SwatchGroup::AttachedToWindow() SwatchGroup::AttachedToWindow()
{ {
@@ -172,19 +174,20 @@ SwatchGroup::AttachedToWindow()
fAlphaSlider->SetTarget(this); fAlphaSlider->SetTarget(this);
} }
// MessageReceived
void void
SwatchGroup::MessageReceived(BMessage* message) SwatchGroup::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case MSG_SET_COLOR: { case MSG_SET_COLOR:
{
rgb_color color; rgb_color color;
if (restore_color_from_message(message, color) == B_OK) { if (restore_color_from_message(message, color) == B_OK) {
// TODO: fix color picker panel to respect alpha // TODO: fix color picker panel to respect alpha
if (message->HasRect("panel frame")) if (message->HasRect("panel frame"))
color.alpha = fAlphaSlider->Value(); color.alpha = fAlphaSlider->Value();
// //
if (fCurrentColor) if (fCurrentColor != NULL)
fCurrentColor->SetColor(color); fCurrentColor->SetColor(color);
} }
// if message contains these fields, // if message contains these fields,
@@ -203,30 +206,33 @@ color.alpha = fAlphaSlider->Value();
} }
break; break;
} }
case MSG_COLOR_FIELD: {
case MSG_COLOR_FIELD:
{
// get h from color slider // get h from color slider
float h = ((255 - fColorSlider->Value()) / 255.0) * 6.0; float h = ((255 - fColorSlider->Value()) / 255.0) * 6.0;
float s, v; float s, v;
// s and v are comming from the message // s and v are comming from the message
if (message->FindFloat("value", &s) >= B_OK if (message->FindFloat("value", &s) == B_OK
&& message->FindFloat("value", 1, &v) >= B_OK) { && message->FindFloat("value", 1, &v) == B_OK) {
_SetColor(h, s, v, fAlphaSlider->Value()); _SetColor(h, s, v, fAlphaSlider->Value());
} }
break; break;
} }
case MSG_COLOR_SLIDER: {
case MSG_COLOR_SLIDER:
{
float h; float h;
float s, v; float s, v;
fColorSlider->GetOtherValues(&s, &v); fColorSlider->GetOtherValues(&s, &v);
// h is comming from the message // h is comming from the message
if (message->FindFloat("value", &h) >= B_OK) { if (message->FindFloat("value", &h) == B_OK)
_SetColor(h, s, v, fAlphaSlider->Value()); _SetColor(h, s, v, fAlphaSlider->Value());
}
break; break;
} }
case MSG_ALPHA_SLIDER: {
case MSG_ALPHA_SLIDER:
{
float h = (1.0 - (float)fColorSlider->Value() / 255.0) * 6; float h = (1.0 - (float)fColorSlider->Value() / 255.0) * 6;
float s, v; float s, v;
fColorSlider->GetOtherValues(&s, &v); fColorSlider->GetOtherValues(&s, &v);
@@ -234,18 +240,16 @@ color.alpha = fAlphaSlider->Value();
break; break;
} }
case MSG_COLOR_PICKER: { case MSG_COLOR_PICKER:
{
rgb_color color; rgb_color color;
if (restore_color_from_message(message, color) < B_OK) if (restore_color_from_message(message, color) < B_OK)
break; break;
if (!fColorPickerPanel) {
fColorPickerPanel if (fColorPickerPanel == NULL) {
= new ColorPickerPanel(fColorPickerFrame, fColorPickerPanel = new ColorPickerPanel(fColorPickerFrame,
color, color, fColorPickerMode, Window(),
fColorPickerMode, new BMessage(MSG_SET_COLOR), this);
Window(),
new BMessage(MSG_SET_COLOR),
this);
fColorPickerPanel->Show(); fColorPickerPanel->Show();
} else { } else {
if (fColorPickerPanel->Lock()) { if (fColorPickerPanel->Lock()) {
@@ -263,37 +267,39 @@ color.alpha = fAlphaSlider->Value();
} }
} }
// #pragma mark - // #pragma mark -
// SetCurrentColor
void void
SwatchGroup::SetCurrentColor(CurrentColor* color) SwatchGroup::SetCurrentColor(CurrentColor* color)
{ {
if (fCurrentColor == color) if (fCurrentColor == color)
return; return;
if (fCurrentColor) if (fCurrentColor != NULL)
fCurrentColor->RemoveObserver(this); fCurrentColor->RemoveObserver(this);
fCurrentColor = color; fCurrentColor = color;
if (fCurrentColor) { if (fCurrentColor != NULL) {
fCurrentColor->AddObserver(this); fCurrentColor->AddObserver(this);
ObjectChanged(fCurrentColor); ObjectChanged(fCurrentColor);
} }
} }
// #pragma mark - // #pragma mark -
// _SetColor
void void
SwatchGroup::_SetColor(rgb_color color) SwatchGroup::_SetColor(rgb_color color)
{ {
fCurrentColorSV->SetColor(color); fCurrentColorSV->SetColor(color);
} }
// _SetColor
void void
SwatchGroup::_SetColor(float h, float s, float v, uint8 a) SwatchGroup::_SetColor(float h, float s, float v, uint8 a)
{ {
@@ -322,8 +328,10 @@ SwatchGroup::_SetColor(float h, float s, float v, uint8 a)
} }
fIgnoreNotifications = true; fIgnoreNotifications = true;
if (fCurrentColor) if (fCurrentColor)
fCurrentColor->SetColor(color); fCurrentColor->SetColor(color);
_SetColor(color); _SetColor(color);
fIgnoreNotifications = false; fIgnoreNotifications = false;
} }