1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/***************************************************************************
* Copyright (C) 2011 by Pere RÃ fols Soler *
* sapista2@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include <cmath>
#include <cstdio>
#include "knob2.h"
#include "colors.h"
#include <gdkmm.h>//For the function Gdk::Cairo::set_source_pixbuf()
//#include <gdkmm/general.h> //Switched back to gdkmm.h for cairo portability problems
#define KNOB_RADIUS 0.4
#define SCROLL_EVENT_PERCENT 0.005
#define MOUSE_EVENT_PERCENT 0.008
#define KNOB_CENTER_X 0.5
#define KNOB_CENTER_Y 0.5
#define TEXT_SIZE 22
#define KNOB_R_CALIBRATION 0.93
#define SLOW_MOTION_MULTIPLIER 0.05
KnobWidget2::KnobWidget2(float fMin, float fMax, std::string sLabel, std::string sUnits, const char *knobIconPath, int iType, bool snap2ZerodB ):
m_fMin(fMin),
m_fMax(fMax),
bMotionIsConnected(false),
m_Value(fMin),
m_Label(sLabel),
m_Units(sUnits),
m_TypeKnob(iType),
mouse_move_ant(0),
m_snap2Zero(snap2ZerodB),
m_focus(false),
m_slowMultiplier(1.0),
m_knobIconPath(knobIconPath)
{
m_image_ptr = Gdk::Pixbuf::create_from_file(m_knobIconPath);
// Detect transparent colors for loaded image
Cairo::Format format = Cairo::FORMAT_RGB24;
if (m_image_ptr->get_has_alpha())
{
format = Cairo::FORMAT_ARGB32;
}
// Create a new ImageSurface
m_image_surface_ptr = Cairo::ImageSurface::create (format, m_image_ptr->get_width(), m_image_ptr->get_height());
// Create the new Context for the ImageSurface
m_image_context_ptr = Cairo::Context::create (m_image_surface_ptr);
// Draw the image on the new Context
Gdk::Cairo::set_source_pixbuf (m_image_context_ptr, m_image_ptr, 0.0, 0.0);
m_image_context_ptr->paint();
//Size Request acording knob image
set_size_request((int)(1.5*(double)(m_image_ptr->get_width())), TEXT_SIZE + (int)(1.5*(double)(m_image_ptr->get_height())));
add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::POINTER_MOTION_MASK | Gdk::SCROLL_MASK | Gdk::LEAVE_NOTIFY_MASK);
signal_button_press_event().connect(sigc::mem_fun(*this, &KnobWidget2::on_button_press_event),true);
signal_button_release_event().connect(sigc::mem_fun(*this, &KnobWidget2::on_button_release_event),true);
signal_scroll_event().connect(sigc::mem_fun(*this, &KnobWidget2::on_scrollwheel_event),true);
signal_motion_notify_event().connect(sigc::mem_fun(*this, &KnobWidget2::on_mouse_motion_event),true);
signal_leave_notify_event().connect(sigc::mem_fun(*this, &KnobWidget2::on_mouse_leave_widget),true);
}
KnobWidget2::~KnobWidget2()
{
}
KnobWidget2::signal_KnobChanged KnobWidget2::signal_changed()
{
return m_KnobChangedSignal;
}
void KnobWidget2::set_value(float fValue)
{
m_Value = fValue;
m_Value = m_Value < m_fMin ? m_fMin : m_Value;
m_Value = m_Value > m_fMax ? m_fMax : m_Value;
redraw();
}
double KnobWidget2::get_value()
{
return m_Value;
}
void KnobWidget2::redraw()
{
Glib::RefPtr<Gdk::Window> win = get_window();
if(win)
{
Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
win->invalidate_rect(r, false);
}
}
bool KnobWidget2::on_mouse_leave_widget(GdkEventCrossing* event)
{
if(!bMotionIsConnected)
{
m_slowMultiplier = 1.0;
m_focus = false;
redraw();
}
return true;
}
//Mouse events
bool KnobWidget2::on_button_press_event(GdkEventButton* event)
{
int x,y;
get_pointer(x,y);
if( x > 0 &&
x < width &&
y > 0 &&
y < width && //I use width for y becous knob must be square, this discards text area
event->type == GDK_BUTTON_PRESS) //Only grab single click
{
mouse_move_ant = y;
if(event->button == 1)
{
bMotionIsConnected = true;
m_slowMultiplier = 1.0;
}
else if(event->button == 3)
{
bMotionIsConnected = true;
m_slowMultiplier = SLOW_MOTION_MULTIPLIER;
}
}
return true;
}
bool KnobWidget2::on_button_release_event(GdkEventButton* event)
{
bMotionIsConnected = false;
return true;
}
bool KnobWidget2::on_mouse_motion_event(GdkEventMotion* event)
{
if(bMotionIsConnected)
{
double increment = 0.0;
switch(m_TypeKnob)
{
case KNOB_TYPE_FREQ:
increment = m_slowMultiplier * MOUSE_EVENT_PERCENT*(m_fMax - m_fMin)*0.0002*m_Value;
break;
case KNOB_TYPE_LIN:
increment = m_slowMultiplier * MOUSE_EVENT_PERCENT*(m_fMax - m_fMin);
break;
case KNOB_TYPE_TIME:
increment = m_slowMultiplier * MOUSE_EVENT_PERCENT*5.0*(m_Value + 1.0);
break;
}
float val = 0.0f;
bool ismoving = false;
if(event->y - mouse_move_ant < 0)
{
//Move up
val = m_Value + increment*(abs(event->y - mouse_move_ant));
ismoving = true;
}
if(event->y - mouse_move_ant > 0)
{
//Move down
val = m_Value - increment*(abs(event->y - mouse_move_ant));
ismoving = true;
}
//Snap to 0 dB
if(m_snap2Zero && val < 0.5f && val > -0.5f)
{
val = 0.0f;
}
if(ismoving)
{
set_value(val);
}
mouse_move_ant = event->y;
m_KnobChangedSignal.emit();
}
else
{
//Grab focus if mouse over knob
m_focus = event->x > 0 && event->x < width && event->y > 0 && event->y < width;
redraw();
}
return true;
}
bool KnobWidget2::on_scrollwheel_event(GdkEventScroll* event)
{
double increment = 0.0;
switch(m_TypeKnob)
{
case KNOB_TYPE_FREQ:
increment = SCROLL_EVENT_PERCENT*(m_fMax - m_fMin)*0.0001*m_Value;
break;
case KNOB_TYPE_LIN:
increment = SCROLL_EVENT_PERCENT*(m_fMax - m_fMin);
break;
case KNOB_TYPE_TIME:
increment = SCROLL_EVENT_PERCENT*5.0*(m_Value + 1.0);
break;
}
if (event->direction == GDK_SCROLL_UP)
{
// up code
set_value(m_Value + increment);
}
else if (event->direction == GDK_SCROLL_DOWN)
{
// down code
set_value(m_Value - increment);
}
m_KnobChangedSignal.emit();
return true;
}
//Drawing Knob
bool KnobWidget2::on_expose_event(GdkEventExpose* event)
{
Glib::RefPtr<Gdk::Window> window = get_window();
if(window)
{
Gtk::Allocation allocation = get_allocation();
width = allocation.get_width();
height = allocation.get_height();
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
//Clip inside acording the expose event
cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height);
cr->clip();
cr->set_source_rgb(BACKGROUND_R, BACKGROUND_G, BACKGROUND_B);
cr->paint(); //Fill all with background color
//Set text
Glib::RefPtr<Pango::Layout> pangoLayout = Pango::Layout::create(cr);
Pango::FontDescription font_desc("sans 9px");
pangoLayout->set_font_description(font_desc);
cr->move_to(0, height - TEXT_SIZE);
cr->set_source_rgba(0.9, 0.9, 0.9, 1.0);
pangoLayout->update_from_cairo_context(cr); //gets cairo cursor position
pangoLayout->set_text(m_Label);
pangoLayout->set_width(Pango::SCALE * width);
pangoLayout->set_alignment(Pango::ALIGN_CENTER);
pangoLayout->show_in_cairo_context(cr);
cr->stroke();
cr->move_to(0, height - TEXT_SIZE/2);
cr->set_source_rgba(0.9, 0.9, 0.9, 1.0);
pangoLayout->update_from_cairo_context(cr); //gets cairo cursor position
std::stringstream ss;
ss.precision(1);
if(m_TypeKnob == KNOB_TYPE_FREQ && m_Value >= 1000.0)
{
ss<<std::fixed<<m_Value/1000.0<<" k"<<m_Units;
}
else if(m_TypeKnob == KNOB_TYPE_TIME && m_Value >= 1000.0)
{
ss<<std::fixed<<m_Value/1000.0<<" s";
}
else if(m_TypeKnob == KNOB_TYPE_TIME && m_Value < 1.0)
{
ss<<std::fixed<<m_Value*1000.0<<" us";
}
else
{
ss<<std::fixed<<m_Value<<" "<<m_Units;
}
pangoLayout->set_text(ss.str());
pangoLayout->set_width(Pango::SCALE * width);
pangoLayout->set_alignment(Pango::ALIGN_CENTER);
pangoLayout->show_in_cairo_context(cr);
cr->stroke();
cr->save();
//Calc konb angle (pos)
double pos = 0.0, m, n;
switch(m_TypeKnob)
{
case KNOB_TYPE_FREQ:
case KNOB_TYPE_TIME:
m = (1.48*M_PI)/log10(m_fMax/m_fMin);
n = 0.76*M_PI;
pos = m*log10(m_Value/m_fMin) + n;
break;
case KNOB_TYPE_LIN:
m = (1.48*M_PI)/(m_fMax-m_fMin);
n = 0.76*M_PI - m*m_fMin;
pos = m*m_Value + n;
break;
}
//Scale to 1
cr->rectangle(0, 0, width,height - TEXT_SIZE);
cr->clip();
cr->scale(width,height - TEXT_SIZE);
//Draw glow, mouse is over
if(m_focus)
{
Cairo::RefPtr<Cairo::RadialGradient> glow_gradient_ptr = Cairo::RadialGradient::create(
KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS - 0.1, KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS + 0.1);
glow_gradient_ptr->add_color_stop_rgba (0, 0.4, 0.6, 0.8, 0.6);
glow_gradient_ptr->add_color_stop_rgba (1, BACKGROUND_R, BACKGROUND_G, BACKGROUND_B, 0.1);
cr->set_source( glow_gradient_ptr);
cr->set_line_width(0.8);
cr->arc(KNOB_CENTER_X, KNOB_CENTER_Y, 0.2, 0.0, 2.0 * M_PI);
cr->stroke();
}
//Draw Background gradient full circle
Cairo::RefPtr<Cairo::RadialGradient> bkg_gradient_ptr = Cairo::RadialGradient::create(
KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS - 0.08, KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS + 0.1);
bkg_gradient_ptr->add_color_stop_rgba (0, 0.0, 0.8, 0.3, 0.2);
bkg_gradient_ptr->add_color_stop_rgba (1, BACKGROUND_R, BACKGROUND_G, BACKGROUND_B, 0.1);
cr->set_source( bkg_gradient_ptr);
cr->set_line_width(0.8);
cr->arc(KNOB_CENTER_X, KNOB_CENTER_Y, 0.2, 0.0, 2.0 * M_PI);
cr->stroke();
//Draw colored circle
Cairo::RefPtr<Cairo::RadialGradient> rad_gradient_ptr = Cairo::RadialGradient::create(
KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS - 0.08, KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS + 0.1);
rad_gradient_ptr->add_color_stop_rgba (0, 0.0, 1.0, 0.0, 0.8);
rad_gradient_ptr->add_color_stop_rgba (1, BACKGROUND_R, BACKGROUND_G, BACKGROUND_B, 0.1);
cr->set_source( rad_gradient_ptr);
cr->set_line_width(0.2);
cr->arc(KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS + 0.04, 0.76 * M_PI, pos);
cr->stroke();
//Draw color circle frame
cr->set_source_rgba(BACKGROUND_R + 0.4, BACKGROUND_G + 0.4, BACKGROUND_B + 0.4, 1.0);
cr->set_line_width(1.0/width);
//cr->set_line_width(0.01);
cr->arc(KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS + 0.04, 0.76 * M_PI, 0.24 * M_PI);
cr->arc(KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS - 0.06, 0.24 * M_PI, 2.76*M_PI);
cr->close_path();
cr->stroke();
cr->set_source_rgba(0.0, 0.6, 0.6, 0.1);
cr->set_line_width(0.1);
std::valarray< double > dashes(2);
dashes[0] = 0.01;//1.0/width;
dashes[1] = 0.02;//4.0/width;
cr->set_dash (dashes, 0.5);
cr->arc(KNOB_CENTER_X, KNOB_CENTER_Y, KNOB_RADIUS - 0.01, 0.76 * M_PI, 0.24 * M_PI);
cr->stroke();
cr->restore();
//Draw knob and rotate
cr->save();
cr->translate(width/2, (height-TEXT_SIZE)/2);
cr->rotate(pos + KNOB_R_CALIBRATION);
//Draw the knob icon
cr->set_source (m_image_surface_ptr, -m_image_surface_ptr->get_width()/2, -m_image_surface_ptr->get_height()/2);
cr->rectangle (-m_image_surface_ptr->get_width()/2, -m_image_surface_ptr->get_height()/2, m_image_surface_ptr->get_width(), m_image_surface_ptr->get_height());
cr->clip();
cr->paint();
cr->restore();
}
return true;
}
|