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
|
/**
bespoke synth, a software modular synthesizer
Copyright (C) 2021 Ryan Challinor (contact: awwbees@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 3 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, see <http://www.gnu.org/licenses/>.
**/
/*
==============================================================================
CanvasScrollbar.cpp
Created: 22 Mar 2021 12:19:47am
Author: Ryan Challinor
==============================================================================
*/
#include "CanvasScrollbar.h"
#include "Canvas.h"
#include "ModularSynth.h"
CanvasScrollbar::CanvasScrollbar(Canvas* canvas, std::string name, Style style)
: mStyle(style)
, mCanvas(canvas)
{
SetName(name.c_str());
SetParent(canvas->GetModuleParent());
}
void CanvasScrollbar::Render()
{
ofRectangle canvasRect = mCanvas->GetRect(true);
if (mStyle == Style::kHorizontal)
{
SetPosition(canvasRect.x, canvasRect.getMaxY());
SetDimensions(canvasRect.width, 10);
}
if (mStyle == Style::kVertical)
{
SetPosition(canvasRect.getMaxX(), canvasRect.y);
SetDimensions(10, canvasRect.height);
}
if (mAutoHide && GetBarStart() == 0)
{
if (mStyle == Style::kHorizontal && GetBarEnd() == mWidth)
return;
if (mStyle == Style::kVertical && GetBarEnd() == mHeight)
return;
}
ofPushMatrix();
ofTranslate(mX, mY);
ofPushStyle();
ofSetLineWidth(.5f);
float w, h;
GetDimensions(w, h);
ofFill();
ofRect(0, 0, mWidth, mHeight);
ofSetColor(255, 255, 255);
if (mStyle == Style::kHorizontal)
ofRect(GetBarStart(), 0, GetBarEnd() - GetBarStart(), mHeight);
if (mStyle == Style::kVertical)
ofRect(0, GetBarStart(), mWidth, GetBarEnd() - GetBarStart());
ofPopStyle();
ofPopMatrix();
}
float CanvasScrollbar::GetBarStart() const
{
if (mStyle == Style::kHorizontal)
return mCanvas->mViewStart / mCanvas->GetLength() * mWidth;
if (mStyle == Style::kVertical)
return ofMap(mCanvas->GetRowOffset(), 0, mCanvas->GetNumRows(), 0, mHeight);
return 0;
}
float CanvasScrollbar::GetBarEnd() const
{
if (mStyle == Style::kHorizontal)
return mCanvas->mViewEnd / mCanvas->GetLength() * mWidth;
if (mStyle == Style::kVertical)
return ofMap(mCanvas->GetRowOffset() + mCanvas->GetNumVisibleRows(), 0, mCanvas->GetNumRows(), 0, mHeight);
return 1;
}
void CanvasScrollbar::OnClicked(float x, float y, bool right)
{
mClickMousePos.set(TheSynth->GetRawMouseX(), TheSynth->GetRawMouseY());
mDragOffset.set(0, 0);
mClick = true;
if (mStyle == Style::kHorizontal)
mScrollBarOffset = x - GetBarStart();
if (mStyle == Style::kVertical)
mScrollBarOffset = y - GetBarStart();
}
void CanvasScrollbar::MouseReleased()
{
mClick = false;
}
bool CanvasScrollbar::MouseMoved(float x, float y)
{
CheckHover(x, y);
if (mClick)
{
mDragOffset = (ofVec2f(TheSynth->GetRawMouseX(), TheSynth->GetRawMouseY()) - mClickMousePos) / gDrawScale;
if (mStyle == Style::kHorizontal)
{
float viewLength = mCanvas->mViewEnd - mCanvas->mViewStart;
mCanvas->mViewStart = ofClamp((x - mScrollBarOffset) / mWidth * mCanvas->GetLength(), 0, mCanvas->GetLength() - viewLength);
mCanvas->mViewEnd = mCanvas->mViewStart + viewLength;
}
if (mStyle == Style::kVertical)
mCanvas->SetRowOffset(ofClamp(int(ofMap(y - mScrollBarOffset, 0, mHeight, 0, mCanvas->GetNumRows()) + .5f), 0, mCanvas->GetNumRows() - mCanvas->GetNumVisibleRows()));
}
return false;
}
bool CanvasScrollbar::MouseScrolled(float x, float y, float scrollX, float scrollY, bool isSmoothScroll, bool isInvertedScroll)
{
return false;
}
void CanvasScrollbar::SaveState(FileStreamOut& out)
{
}
void CanvasScrollbar::LoadState(FileStreamIn& in, bool shouldSetValue)
{
}
|