Skip to content

Commit

Permalink
Gravity: Add ColorItem and RainbowItem
Browse files Browse the repository at this point in the history
instead of using boring StringItems

ColorItem draws a small colored rectangle left of the string
RainbowItem draws a rainbow colored rectangle left of the string
  • Loading branch information
jscipione committed Apr 13, 2016
1 parent 8997220 commit 4b83455
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 8 deletions.
96 changes: 96 additions & 0 deletions src/add-ons/screen_savers/gravity/ColorItem.cpp
@@ -0,0 +1,96 @@
/*
* Copyright 2002-2016 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* John Scipione, jscipione@gmail.com
*
* Based on ColorWhichIcon by DarkWyrm (bpmagic@columbus.rr.com)
*/


#include "ColorItem.h"

#include <math.h>

#include <ControlLook.h>
#include <View.h>


// golden ratio
#ifdef M_PHI
# undef M_PHI
#endif
#define M_PHI 1.61803398874989484820


// #pragma mark - ColorItem


ColorItem::ColorItem(const char* string, rgb_color color)
:
BStringItem(string, 0, false),
fColor(color)
{
}


void
ColorItem::DrawItem(BView* owner, BRect frame, bool complete)
{
rgb_color highColor = owner->HighColor();
rgb_color lowColor = owner->LowColor();

if (IsSelected() || complete) {
if (IsSelected()) {
owner->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR));
owner->SetLowColor(owner->HighColor());
} else
owner->SetHighColor(lowColor);

owner->FillRect(frame);
}

float spacer = floorf(be_control_look->DefaultItemSpacing() / 2);

BRect colorRect(frame);
colorRect.InsetBy(2.0f, 2.0f);
colorRect.left += spacer;
colorRect.right = colorRect.left + colorRect.Height() * M_PHI;

// draw the colored box
owner->SetHighColor(fColor);
owner->FillRect(colorRect);

// draw the border
owner->SetHighColor(ui_color(B_CONTROL_BORDER_COLOR));
owner->StrokeRect(colorRect);

// draw the string
owner->MovePenTo(colorRect.right + spacer, frame.top + BaselineOffset());

if (!IsEnabled()) {
rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
if (textColor.red + textColor.green + textColor.blue > 128 * 3)
owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
else
owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
} else {
if (IsSelected())
owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
else
owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
}

owner->DrawString(Text());

owner->SetHighColor(highColor);
owner->SetLowColor(lowColor);
}


void
ColorItem::SetColor(rgb_color color)
{
fColor = color;
}
30 changes: 30 additions & 0 deletions src/add-ons/screen_savers/gravity/ColorItem.h
@@ -0,0 +1,30 @@
/*
* Copyright 2002-2016 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* John Scipione, jscipione@gmail.com
*
* Based on ColorWhichIcon by DarkWyrm, bpmagic@columbus.rr.com
*/
#ifndef _COLOR_ITEM_H
#define _COLOR_ITEM_H


#include <InterfaceDefs.h>
#include <StringItem.h>


class ColorItem : public BStringItem {
public:
ColorItem(const char* string, rgb_color color);

virtual void DrawItem(BView* owner, BRect frame, bool complete);
virtual void SetColor(rgb_color color);

private:
rgb_color fColor;
};


#endif // _COLOR_ITEM_H
17 changes: 9 additions & 8 deletions src/add-ons/screen_savers/gravity/ConfigView.cpp
Expand Up @@ -19,7 +19,9 @@
#include <StringView.h>
#include <View.h>

#include "ColorItem.h"
#include "Gravity.h"
#include "RainbowItem.h"


static const int32 kMsgSize = 'size';
Expand All @@ -43,14 +45,13 @@ ConfigView::ConfigView(BRect frame, Gravity* parent)
SetViewUIColor(B_PANEL_BACKGROUND_COLOR);

fShadeList->SetSelectionMessage(new BMessage(kMsgShade));

fShadeList->AddItem(new BStringItem("Red"));
fShadeList->AddItem(new BStringItem("Green"));
fShadeList->AddItem(new BStringItem("Blue"));
fShadeList->AddItem(new BStringItem("Orange"));
fShadeList->AddItem(new BStringItem("Purple"));
fShadeList->AddItem(new BStringItem("White"));
fShadeList->AddItem(new BStringItem("Rainbow"));
fShadeList->AddItem(new ColorItem("Red", (rgb_color){ 255, 65, 54 }));
fShadeList->AddItem(new ColorItem("Green", (rgb_color){ 46, 204, 64 }));
fShadeList->AddItem(new ColorItem("Blue", (rgb_color){ 0, 116, 217 }));
fShadeList->AddItem(new ColorItem("Orange", (rgb_color){ 255, 133, 27 }));
fShadeList->AddItem(new ColorItem("Purple", (rgb_color){ 177, 13, 201 }));
fShadeList->AddItem(new ColorItem("White", (rgb_color){ 255, 255, 255 }));
fShadeList->AddItem(new RainbowItem("Rainbow"));

fShadeList->Select(parent->Config.ShadeID);

Expand Down
2 changes: 2 additions & 0 deletions src/add-ons/screen_savers/gravity/Jamfile
Expand Up @@ -10,11 +10,13 @@ if $(TARGET_GCC_VERSION_$(TARGET_PACKAGING_ARCH)[1]) < 3 {
AddResources Gravity : Gravity.rdef ;

local sources =
ColorItem.cpp
ConfigView.cpp
Gravity.cpp
GravitySource.cpp
GravityView.cpp
Particle.cpp
RainbowItem.cpp
main.cpp
;

Expand Down
97 changes: 97 additions & 0 deletions src/add-ons/screen_savers/gravity/RainbowItem.cpp
@@ -0,0 +1,97 @@
/*
* Copyright 2006 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* John Scipione, jscipione@gmail.com
*/


#include "RainbowItem.h"

#include <math.h>

#include <ControlLook.h>
#include <GradientLinear.h>
#include <InterfaceDefs.h>
#include <View.h>


// golden ratio
#ifdef M_PHI
# undef M_PHI
#endif
#define M_PHI 1.61803398874989484820


// #pragma mark - RainbowItem


RainbowItem::RainbowItem(const char* string)
:
BStringItem(string, 0, false)
{
}


void
RainbowItem::DrawItem(BView* owner, BRect frame, bool complete)
{
rgb_color highColor = owner->HighColor();
rgb_color lowColor = owner->LowColor();

if (IsSelected() || complete) {
if (IsSelected()) {
owner->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR));
owner->SetLowColor(owner->HighColor());
} else
owner->SetHighColor(lowColor);

owner->FillRect(frame);
}

float spacer = floorf(be_control_look->DefaultItemSpacing() / 2);

BRect colorRect(frame);
colorRect.InsetBy(2.0f, 2.0f);
colorRect.left += spacer;
colorRect.right = colorRect.left + colorRect.Height() * M_PHI;

// draw the rainbow
BGradientLinear gradient;
gradient.AddColor((rgb_color){ 255, 65, 54 }, 0); // red
gradient.AddColor((rgb_color){ 255, 133, 27 }, 60); // orange
gradient.AddColor((rgb_color){ 255, 220, 0 }, 102); // yellow
gradient.AddColor((rgb_color){ 46, 204, 64 }, 153); // green
gradient.AddColor((rgb_color){ 0, 116, 217 }, 195); // blue
// indigo ;)
gradient.AddColor((rgb_color){ 177, 13, 201 }, 255); // violet
gradient.SetStart(colorRect.LeftTop());
gradient.SetEnd(colorRect.RightBottom());
owner->FillRect(colorRect, gradient);

// draw the border
owner->SetHighColor(ui_color(B_CONTROL_BORDER_COLOR));
owner->StrokeRect(colorRect);

// draw the string
owner->MovePenTo(colorRect.right + spacer, frame.top + BaselineOffset());

if (!IsEnabled()) {
rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
if (textColor.red + textColor.green + textColor.blue > 128 * 3)
owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
else
owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
} else {
if (IsSelected())
owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
else
owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
}

owner->DrawString(Text());

owner->SetHighColor(highColor);
owner->SetLowColor(lowColor);
}
26 changes: 26 additions & 0 deletions src/add-ons/screen_savers/gravity/RainbowItem.h
@@ -0,0 +1,26 @@
/*
* Copyright 2001-2008, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* DarkWyrm, bpmagic@columbus.rr.com
* Rene Gollent, rene@gollent.com
* Ryan Leavengood, leavengood@gmail.com
* John Scipione, jscipione@gmail.com
*/
#ifndef _RAINBOW_ITEM_H
#define _RAINBOW_ITEM_H


#include <StringItem.h>


class RainbowItem : public BStringItem {
public:
RainbowItem(const char* string);

virtual void DrawItem(BView* owner, BRect frame, bool complete);
};


#endif // _RAINBOW_ITEM_H

0 comments on commit 4b83455

Please sign in to comment.