Skip to content

Commit

Permalink
Add helper methods to Background
Browse files Browse the repository at this point in the history
  • Loading branch information
jmc-88 committed May 22, 2017
1 parent a398fd7 commit 981b1e5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
29 changes: 3 additions & 26 deletions src/util/area.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,11 @@ void Area::Draw() {
}

void Area::DrawBackground(cairo_t* c) {
Color fill_color;
if (mouse_state_ == MouseState::kMouseNormal) {
fill_color = bg_.fill_color();
} else if (mouse_state_ == MouseState::kMouseOver) {
fill_color = bg_.fill_color_hover();
} else {
fill_color = bg_.fill_color_pressed();
}

const int w = bg_.border().width();
util::Rect extents{0, 0, width_, height_};
extents.ShrinkBy(w);

Color fill_color = bg_.fill_color_for(mouse_state_);
if (fill_color.alpha() > 0.0) {
DrawRect(c, extents.top_left().first, extents.top_left().second,
extents.bottom_right().first, extents.bottom_right().second,
Expand All @@ -432,34 +424,19 @@ void Area::DrawBackground(cairo_t* c) {
cairo_fill(c);
}

int gradient_id = -1;
if (mouse_state_ == MouseState::kMouseNormal) {
gradient_id = bg_.gradient_id();
} else if (mouse_state_ == MouseState::kMouseOver) {
gradient_id = bg_.gradient_id_hover();
} else {
gradient_id = bg_.gradient_id_pressed();
}
int gradient_id = bg_.gradient_id_for(mouse_state_);
if (gradient_id >= 0 && gradient_id < gradients.size()) {
gradients[gradient_id].Draw(c, extents);
}

Color border_color;
if (mouse_state_ == MouseState::kMouseNormal) {
border_color = bg_.border();
} else if (mouse_state_ == MouseState::kMouseOver) {
border_color = bg_.border_color_hover();
} else {
border_color = bg_.border_color_pressed();
}

if (w > 0) {
cairo_set_line_width(c, w);

// draw border inside (x, y, width, height)
DrawRect(c, w / 2.0, w / 2.0, width_ - w, height_ - w,
bg_.border().rounded());

Color border_color = bg_.border_color_for(mouse_state_);
cairo_set_source_rgba(c, border_color[0], border_color[1], border_color[2],
border_color.alpha());
cairo_stroke(c);
Expand Down
31 changes: 31 additions & 0 deletions src/util/color.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <utility>

#include "util/area.hh"
#include "util/color.hh"

namespace {
Expand Down Expand Up @@ -123,6 +124,16 @@ bool Border::operator!=(Border const& other) const { return !(*this == other); }
Background::Background()
: gradient_id_(-1), gradient_id_hover_(-1), gradient_id_pressed_(-1) {}

Color Background::fill_color_for(MouseState mouse_state) const {
if (mouse_state == MouseState::kMouseNormal) {
return fill_color();
}
if (mouse_state == MouseState::kMouseOver) {
return fill_color_hover();
}
return fill_color_pressed();
}

Color Background::fill_color() const { return fill_color_; }

void Background::set_fill_color(Color const& color) { fill_color_ = color; }
Expand All @@ -149,6 +160,16 @@ void Background::set_fill_color_pressed(Color const& color) {
fill_color_pressed_ = color;
}

Color Background::border_color_for(MouseState mouse_state) const {
if (mouse_state == MouseState::kMouseNormal) {
return border();
}
if (mouse_state == MouseState::kMouseOver) {
return border_color_hover();
}
return border_color_pressed();
}

Border const& Background::border() const { return border_; }

Border& Background::border() { return border_; }
Expand Down Expand Up @@ -177,6 +198,16 @@ void Background::set_border_color_pressed(Color const& color) {
border_color_pressed_ = color;
}

int Background::gradient_id_for(MouseState mouse_state) const {
if (mouse_state == MouseState::kMouseNormal) {
return gradient_id();
}
if (mouse_state == MouseState::kMouseOver) {
return gradient_id_hover();
}
return gradient_id_pressed();
}

int Background::gradient_id() const { return gradient_id_; }

void Background::set_gradient_id(int id) { gradient_id_ = id; }
Expand Down
12 changes: 6 additions & 6 deletions src/util/color.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "util/nullable.hh"

// forward declaration from area.hh
enum class MouseState;

class Color {
public:
friend std::ostream& operator<<(std::ostream& os, Color const& color);
Expand Down Expand Up @@ -63,31 +66,28 @@ class Background {
Background(Background&&) = default;
Background& operator=(Background const&) = default;

Color fill_color_for(MouseState mouse_state) const;
Color fill_color() const;
void set_fill_color(Color const& color);

Color fill_color_hover() const;
void set_fill_color_hover(Color const& color);

Color fill_color_pressed() const;
void set_fill_color_pressed(Color const& color);

Color border_color_for(MouseState mouse_state) const;
Border const& border() const;
Border& border();
void set_border(Border const& border);

Color border_color_hover() const;
void set_border_color_hover(Color const& border);

Color border_color_pressed() const;
void set_border_color_pressed(Color const& border);

int gradient_id_for(MouseState mouse_state) const;
int gradient_id() const;
void set_gradient_id(int id);

int gradient_id_hover() const;
void set_gradient_id_hover(int id);

int gradient_id_pressed() const;
void set_gradient_id_pressed(int id);

Expand Down

0 comments on commit 981b1e5

Please sign in to comment.