From: <pst...@us...> - 2008-03-17 06:22:17
|
Revision: 326 http://jazzplusplus.svn.sourceforge.net/jazzplusplus/?rev=326&view=rev Author: pstieber Date: 2008-03-16 23:22:14 -0700 (Sun, 16 Mar 2008) Log Message: ----------- Added a knob to the build. This will be used in dialogs. Modified Paths: -------------- trunk/jazz/src/Makefile.am trunk/jazz/vc8/JazzPlusPlus-VC8.vcproj Added Paths: ----------- trunk/jazz/src/Knob.cpp trunk/jazz/src/Knob.h Added: trunk/jazz/src/Knob.cpp =================================================================== --- trunk/jazz/src/Knob.cpp (rev 0) +++ trunk/jazz/src/Knob.cpp 2008-03-17 06:22:14 UTC (rev 326) @@ -0,0 +1,314 @@ +//***************************************************************************** +// The JAZZ++ Midi Sequencer +// +// Copyright (C) 2008 Peter J. Stieber +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +//***************************************************************************** + +#include "WxWidgets.h" + +#include <wx/dcbuffer.h> + +#include "Knob.h" +#include "Globals.h" + +#include <cmath> + +//***************************************************************************** +// Description: +// This is the knob class definition. +//***************************************************************************** +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +BEGIN_EVENT_TABLE(JZKnob,wxControl) + EVT_SIZE(JZKnob::OnSize) + EVT_ERASE_BACKGROUND(JZKnob::OnEraseBackground) + EVT_PAINT(JZKnob::OnPaint) + EVT_LEFT_DOWN(JZKnob::OnMouse) + EVT_LEFT_UP(JZKnob::OnMouse) + EVT_MOTION(JZKnob::OnMouse) + EVT_MOUSEWHEEL(JZKnob::OnMouse) +END_EVENT_TABLE() + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +JZKnob::JZKnob() + : wxControl() +{ +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +JZKnob::JZKnob( + wxWindow* pParent, + wxWindowID Id, + int Value, + int MinValue, + int MaxValue, + unsigned int MinAngle, + unsigned int Range, + const wxPoint& Position, + const wxSize& Size, + long WindowStyle, + const wxValidator& Validator, + const wxString& Name) + : wxControl() +{ + Create( + pParent, + Id, + Value, + MinValue, + MaxValue, + MinAngle, + Range, + Position, + Size, + WindowStyle, + Validator, + Name); +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +void JZKnob::Create( + wxWindow* pParent, + wxWindowID Id, + int Value, + int MinValue, + int MaxValue, + unsigned int MinAngle, + unsigned int Range, + const wxPoint& Position, + const wxSize& Size, + long WindowStyle, + const wxValidator& Validator, + const wxString& Name) +{ + wxControl::Create( + pParent, + Id, + Position, + Size, + WindowStyle | wxNO_BORDER, + Validator, + Name); + + SetInitialSize(Size); + + mMin = MinValue; + mMax = MaxValue; + Range %= 360; + MinAngle %= 360; + mMaxAngle = (MinAngle + 360 - Range) % 360; + + mRange = Range; + SetValue(Value); +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +void JZKnob::SetRange(int MinValue, int MaxValue) +{ + if (MinValue < MaxValue) + { + mMin = MinValue; + mMax = MaxValue; + SetValue(mSetting); + } +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +int JZKnob::SetValue(int Value) +{ + if (Value < mMin) + { + Value = mMin; + } + if (Value > mMax) + { + Value = mMax; + } + + if (Value != mSetting) + { + mSetting = Value; + Refresh(false); + Update(); + } + return mSetting; +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +void JZKnob::OnSize(wxSizeEvent& Event) +{ + int Width, Height; + GetClientSize(&Width, &Height); + if (Width > 0 && Height > 0) + { + mBuffer.Create(Width, Height); + } +} + +//----------------------------------------------------------------------------- +// Description: +// This code always erasew when painting so we override this function to +// avoid flicker. +//----------------------------------------------------------------------------- +void JZKnob::OnEraseBackground(wxEraseEvent& Event) +{ +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +void JZKnob::OnPaint(wxPaintEvent& Event) +{ + wxSize Size = GetSize(); + + double Theta = gDegreesToRadians * + (mMaxAngle + (((double)mMax - mSetting) / (mMax - mMin)) * mRange); + + double DeltaX = cos(Theta); + + // Negate because of the upside down coordinates + double DeltaY = -sin(Theta); + + wxPaintDC PaintDc(this); + + wxBufferedDC Dc(&PaintDc, mBuffer); + + Dc.SetBackground(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE))); + + Dc.Clear(); + + int XCenter, YCenter; + GetCenter(XCenter, YCenter); + + int OuterRadius = static_cast<int>( + (((Size.x < Size.y) ? Size.x : Size.y) * .48) + 0.5); + int InnerRadius = static_cast<int>(OuterRadius * 0.6 + 0.5); + + wxColour Color(120, 100, 100); + wxBrush Brush(Color, wxSOLID); + wxPen Pen(Color); + int KnobRadius = OuterRadius; + for (unsigned char Red = 120; KnobRadius > 0 && Red < 250; Red += 5) + { + Color.Set(Red, 100, 100); + Brush.SetColour(Color); + Pen.SetColour(Color); + Dc.SetBrush(Brush); + Dc.SetPen(Pen); + Dc.DrawCircle(XCenter, YCenter, KnobRadius); + --KnobRadius; + } + + wxPen WhitePen(*wxWHITE, 3); + Dc.SetPen(WhitePen); + Dc.DrawLine( + XCenter + static_cast<int>(OuterRadius * DeltaX + 0.5), + YCenter + static_cast<int>(OuterRadius * DeltaY + 0.5), + XCenter + static_cast<int>(InnerRadius * DeltaX + 0.5), + YCenter + static_cast<int>(InnerRadius * DeltaY + 0.5)); +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +void JZKnob::OnMouse(wxMouseEvent& Event) +{ + wxEventType ScrollEvent = wxEVT_NULL; + + if (Event.Moving()) + { + Event.Skip(); + return; + } + + if (Event.GetWheelRotation() < 0) + { + SetValue(GetValue() - 1); + Event.Skip(); + return; + } + + if (Event.GetWheelRotation() > 0) + { + SetValue(GetValue() + 1); + Event.Skip(); + return; + } + + int XCenter, YCenter; + GetCenter(XCenter, YCenter); + + double DeltaX = Event.m_x - XCenter; + double DeltaY = YCenter - Event.m_y; + if (DeltaX == 0.0 && DeltaY == 0.0) + { + return; + } + + double Theta = atan2(DeltaY, DeltaX) * gRadiansToDegrees; + if (Theta < 0.0) + { + Theta += 360.0; + } + + double DeltaTheta = Theta - mMaxAngle; + if (DeltaTheta < 0.0) + { + DeltaTheta += 360; + } + if (DeltaTheta > mRange) + { + return; + } + int NewValue = int(mMax - (DeltaTheta / mRange) * (mMax - mMin)); + + SetValue(NewValue); + if (Event.Dragging() || Event.ButtonUp()) + { + if (Event.ButtonUp()) + { + ScrollEvent = wxEVT_SCROLL_THUMBRELEASE; + } + else + { + ScrollEvent = wxEVT_SCROLL_THUMBTRACK; + } + + wxScrollEvent ScrollEvent(ScrollEvent, m_windowId); + ScrollEvent.SetPosition(NewValue); + ScrollEvent.SetEventObject(this); + GetEventHandler()->ProcessEvent(ScrollEvent); + + wxCommandEvent CommandEvent(wxEVT_COMMAND_SLIDER_UPDATED, m_windowId); + CommandEvent.SetInt(NewValue); + CommandEvent.SetEventObject(this); + GetEventHandler()->ProcessEvent(CommandEvent); + } +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +void JZKnob::GetCenter(int& x, int& y) const +{ + wxSize Size = GetSize(); + x = Size.x / 2; + y = Size.y / 2; +} Property changes on: trunk/jazz/src/Knob.cpp ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/jazz/src/Knob.h =================================================================== --- trunk/jazz/src/Knob.h (rev 0) +++ trunk/jazz/src/Knob.h 2008-03-17 06:22:14 UTC (rev 326) @@ -0,0 +1,171 @@ +//***************************************************************************** +// The JAZZ++ Midi Sequencer +// +// Copyright (C) 2008 Peter J. Stieber +// +// 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., 675 Mass Ave, Cambridge, MA 02139, USA. +//***************************************************************************** + +#ifndef JZ_KNOB_H +#define JZ_KNOB_H + +//***************************************************************************** +// Description: +// This is the knob class declaration. This is a custom control that looks +// like a mixer knob. +//***************************************************************************** +class JZKnob : public wxControl +{ + public: + + JZKnob(); + + JZKnob( + wxWindow* pParent, + wxWindowID Id, + int Value, + int MinValue, + int MaxValue, + unsigned int MinAngle = 240, + unsigned int Range = 300, + const wxPoint& Position = wxDefaultPosition, + const wxSize& Size = wxSize(40, 40), + long WindowStyle = wxNO_BORDER, + const wxValidator& Validator = wxDefaultValidator, + const wxString& Name = wxT("knob")); + + void Create( + wxWindow* pParent, + wxWindowID Id, + int Value, + int MinValue, + int MaxValue, + unsigned int MinAngle = 240, + unsigned int Range = 300, + const wxPoint& Position = wxDefaultPosition, + const wxSize& Size = wxSize(40, 40), + long WindowStyle = wxNO_BORDER, + const wxValidator& Validator = wxDefaultValidator, + const wxString& Name = wxT("knob")); + + // Retrieve/change the range + void SetRange(int MinValue, int MaxValue); + + int GetMin() const; + + int GetMax() const; + + void SetMin(int MinValue); + + void SetMax(int MaxValue); + + unsigned int GetMinAngle() const; + + int GetMaxAngle() const; + + int GetValue() const; + + int SetValue(int Value); + + private: + + void GetCenter(int& x, int& y) const; + + void OnSize(wxSizeEvent& Event); + + void OnEraseBackground(wxEraseEvent& Event); + + void OnPaint(wxPaintEvent& Event); + + void OnMouse(wxMouseEvent& Event); + + private: + + int mMin; + + int mMax; + + int mSetting; + + unsigned int mMaxAngle; + + unsigned int mRange; + + wxBitmap mBuffer; + + DECLARE_EVENT_TABLE() +}; + +//***************************************************************************** +// Description: +// These are the knob inline member functions. +//***************************************************************************** +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +inline +int JZKnob::GetMin() const +{ + return mMin; +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +inline +int JZKnob::GetMax() const +{ + return mMax; +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +inline +void JZKnob::SetMin(int MinValue) +{ + SetRange(MinValue, GetMax()); +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +inline +void JZKnob::SetMax(int MaxValue) +{ + SetRange(GetMin(), MaxValue); +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +inline +unsigned int JZKnob::GetMinAngle() const +{ + return (mMaxAngle - mRange) % 360; +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +inline +int JZKnob::GetMaxAngle() const +{ + return mMaxAngle; +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +inline +int JZKnob::GetValue() const +{ + return mSetting; +} + +#endif // !defined(JZ_KNOB_H) Property changes on: trunk/jazz/src/Knob.h ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/jazz/src/Makefile.am =================================================================== --- trunk/jazz/src/Makefile.am 2008-03-17 05:16:19 UTC (rev 325) +++ trunk/jazz/src/Makefile.am 2008-03-17 06:22:14 UTC (rev 326) @@ -39,6 +39,7 @@ JazzPlusPlusApplication.cpp \ KeyDialog.cpp \ KeyStringConverters.cpp \ +Knob.cpp \ Mapper.cpp \ MeasureChoice.cpp \ MidiDeviceDialog.cpp \ Modified: trunk/jazz/vc8/JazzPlusPlus-VC8.vcproj =================================================================== --- trunk/jazz/vc8/JazzPlusPlus-VC8.vcproj 2008-03-17 05:16:19 UTC (rev 325) +++ trunk/jazz/vc8/JazzPlusPlus-VC8.vcproj 2008-03-17 06:22:14 UTC (rev 326) @@ -453,6 +453,14 @@ > </File> <File + RelativePath="..\src\Knob.cpp" + > + </File> + <File + RelativePath="..\src\Knob.h" + > + </File> + <File RelativePath="..\src\Mapper.cpp" > </File> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |