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
|
#include "Minimap.h"
#include "EffectChain.h"
#include "ModularSynth.h"
#include "OpenFrameworksPort.h"
#include "Prefab.h"
#include "TitleBar.h"
#include "UserPrefs.h"
namespace
{
const float kMaxLength = 150;
const float kBookmarkSize = 15;
const float kNumBookmarks = 9;
}
Minimap::Minimap()
{
}
Minimap::~Minimap()
{
}
void Minimap::CreateUIControls()
{
IDrawableModule::CreateUIControls();
mGrid = new UIGrid("uigrid", 0, 0, kMaxLength, kBookmarkSize, kNumBookmarks, 1, this);
}
void Minimap::GetDimensions(float& width, float& height)
{
float windowWidth = ofGetWidth();
float windowHeight = ofGetHeight();
float ratio = windowWidth / windowHeight;
if (ofGetWidth() > ofGetHeight())
{
width = kMaxLength;
height = floor(kMaxLength / ratio) + kBookmarkSize;
}
else
{
height = kMaxLength;
width = floor(kMaxLength * ratio) + kBookmarkSize;
}
}
void Minimap::GetDimensionsMinimap(float& width, float& height)
{
GetDimensions(width, height);
if (width < height)
{
width -= kBookmarkSize;
}
else
{
height -= kBookmarkSize;
}
}
void Minimap::ComputeBoundingBox(ofRectangle& rect)
{
std::vector<IDrawableModule*> modules;
TheSynth->GetRootContainer()->GetAllModules(modules);
if (modules.empty())
{
rect = TheSynth->GetDrawRect();
return;
}
rect = modules[0]->GetRect();
for (int i = 1; i < modules.size(); ++i)
{
if (!modules[i]->IsShowing())
{
continue;
}
ofRectangle moduleRect = modules[i]->GetRect();
RectUnion(rect, moduleRect);
}
float minimapWidth, minimapHeight;
GetDimensionsMinimap(minimapWidth, minimapHeight);
float boundsAspectRatio = rect.width / rect.height;
float minimapAspectRatio = minimapWidth / minimapHeight;
//retain aspect ratio
if (boundsAspectRatio > minimapAspectRatio)
rect.height = rect.width / minimapAspectRatio;
else
rect.width = rect.height * minimapAspectRatio;
}
ofRectangle Minimap::CoordsToMinimap(ofRectangle& boundingBox, ofRectangle& source)
{
float width;
float height;
GetDimensionsMinimap(width, height);
float x1 = (source.getMinX() - boundingBox.x) / boundingBox.width * width;
float y1 = (source.getMinY() - boundingBox.y) / boundingBox.height * height;
float x2 = (source.getMaxX() - boundingBox.x) / boundingBox.width * width;
float y2 = (source.getMaxY() - boundingBox.y) / boundingBox.height * height;
return { x1, y1, x2 - x1, y2 - y1 };
}
ofVec2f Minimap::CoordsToViewport(ofRectangle& boundingBox, float x, float y)
{
float width;
float height;
GetDimensionsMinimap(width, height);
float x1 = x / width * boundingBox.width + boundingBox.x;
float y1 = y / height * boundingBox.height + boundingBox.y;
return { x1, y1 };
}
void Minimap::DrawModulesOnMinimap(ofRectangle& boundingBox)
{
std::vector<IDrawableModule*> modules;
std::vector<IDrawableModule*> second_pass_modules;
TheSynth->GetRootContainer()->GetAllModules(modules);
ofPushStyle();
for (auto& module : modules)
{
if (!module->IsShowing() ||
(dynamic_cast<IDrawableModule*>(module->GetParent()) && dynamic_cast<IDrawableModule*>(module->GetParent())->Minimized()))
{
continue;
}
if (dynamic_cast<Prefab*>(module->GetParent()) != nullptr || dynamic_cast<EffectChain*>(module->GetParent()) != nullptr)
{
second_pass_modules.push_back(module);
continue;
}
if (dynamic_cast<EffectChain*>(module) != nullptr && !module->GetChildren().empty() && !module->Minimized())
for (auto& effect : module->GetChildren())
second_pass_modules.push_back(effect);
DrawModuleOnMinimap(boundingBox, module);
}
for (auto& module : second_pass_modules)
DrawModuleOnMinimap(boundingBox, module);
ofPopStyle();
}
void Minimap::DrawModuleOnMinimap(ofRectangle& boundingBox, IDrawableModule* module)
{
ofRectangle moduleRect = module->GetRect();
ofColor moduleColor(IDrawableModule::GetColor(module->GetModuleCategory()));
if (!module->GetChildren().empty())
moduleColor.a = 127;
ofSetColor(moduleColor);
ofFill();
ofRect(CoordsToMinimap(boundingBox, moduleRect));
}
void Minimap::RectUnion(ofRectangle& target, ofRectangle& unionRect)
{
float x2 = target.getMaxX();
float y2 = target.getMaxY();
if (target.x > unionRect.x)
{
target.x = unionRect.x;
target.width = fabs(x2) - target.x;
}
if (target.y > unionRect.y)
{
target.y = unionRect.y;
target.height = fabs(y2) - target.y;
}
if (target.getMaxX() < unionRect.getMaxX())
{
x2 = unionRect.getMaxX();
}
if (target.getMaxY() < unionRect.getMaxY())
{
y2 = unionRect.getMaxY();
}
target.width = fabs(x2) - target.x;
target.height = fabs(y2) - target.y;
}
void Minimap::DrawModule()
{
float width;
float height;
ofRectangle boundingBox;
ofRectangle viewport = TheSynth->GetDrawRect();
ForcePosition();
ComputeBoundingBox(boundingBox);
GetDimensions(width, height);
DrawModulesOnMinimap(boundingBox);
for (int i = 0; i < mGrid->GetCols() * mGrid->GetRows(); ++i)
{
float val = 0.0f;
if (TheSynth->GetLocationZoomer()->HasLocation(i + '1'))
val = .5f;
mGrid->SetVal(i % mGrid->GetCols(), i / mGrid->GetCols(), val);
}
if (width < height)
{
mGrid->SetDimensions(kBookmarkSize, height);
mGrid->SetPosition(width - kBookmarkSize, 0);
mGrid->SetGrid(1, kNumBookmarks);
}
else
{
mGrid->SetDimensions(width, kBookmarkSize);
mGrid->SetPosition(0, height - kBookmarkSize);
mGrid->SetGrid(kNumBookmarks, 1);
}
ofPushMatrix();
float widthMM;
float heightMM;
GetDimensionsMinimap(widthMM, heightMM);
ofClipWindow(0, 0, widthMM, heightMM, true);
ofPushStyle();
ofSetColor(255, 255, 255, 80);
ofRect(CoordsToMinimap(boundingBox, viewport));
ofSetColor(255, 255, 255, 10);
ofFill();
ofRect(CoordsToMinimap(boundingBox, viewport));
ofPopStyle();
ofPopMatrix();
mGrid->Draw();
if (mHoveredBookmarkPos.mCol != -1)
{
ofPushStyle();
ofSetColor(255, 255, 255);
ofFill();
ofVec2f pos = mGrid->GetCellPosition(mHoveredBookmarkPos.mCol, mHoveredBookmarkPos.mRow) + mGrid->GetPosition(true);
float xsize = float(mGrid->GetWidth()) / mGrid->GetCols();
float ysize = float(mGrid->GetHeight()) / mGrid->GetRows();
if (GetKeyModifiers() == kModifier_Shift)
{
ofRect(pos.x + xsize / 2 - 1, pos.y + 2, 2, ysize - 4, 0);
ofRect(pos.x + 2, pos.y + ysize / 2 - 1, xsize - 4, 2, 0);
}
else
{
ofCircle(pos.x + xsize / 2, pos.y + ysize / 2, xsize / 2 - 2);
}
ofPopStyle();
}
}
void Minimap::OnClicked(float x, float y, bool right)
{
if (mGrid->TestClick(x, y, right, true))
{
float gridX, gridY;
mGrid->GetPosition(gridX, gridY, true);
GridCell cell = mGrid->GetGridCellAt(x - gridX, y - gridY);
int number = cell.mCol + cell.mRow + '1';
if (GetKeyModifiers() == kModifier_Shift)
TheSynth->GetLocationZoomer()->WriteCurrentLocation(number);
else
TheSynth->GetLocationZoomer()->MoveToLocation(number);
}
else
{
ofRectangle boundingBox;
ofRectangle viewport = TheSynth->GetDrawRect();
ComputeBoundingBox(boundingBox);
ofVec2f viewportCoords = CoordsToViewport(boundingBox, x, y);
TheSynth->SetDrawOffset(ofVec2f(-viewportCoords.x + viewport.width / 2, -viewportCoords.y + viewport.height / 2));
mClick = true;
}
}
void Minimap::MouseReleased()
{
mClick = false;
}
bool Minimap::MouseMoved(float x, float y)
{
if (mClick)
{
ofRectangle boundingBox;
ofRectangle viewport = TheSynth->GetDrawRect();
ComputeBoundingBox(boundingBox);
ofVec2f viewportCoords = CoordsToViewport(boundingBox, x, y);
TheSynth->SetDrawOffset(ofVec2f(-viewportCoords.x + viewport.width / 2, -viewportCoords.y + viewport.height / 2));
}
mGrid->NotifyMouseMoved(x, y);
float gridX, gridY;
mGrid->GetPosition(gridX, gridY, true);
if (mGrid->TestHover(x - gridX, y - gridY))
{
mHoveredBookmarkPos = mGrid->GetGridCellAt(x - gridX, y - gridY);
}
else
{
mHoveredBookmarkPos.mCol = -1;
mHoveredBookmarkPos.mRow = -1;
}
return false;
}
void Minimap::ForcePosition()
{
float width, height, scale;
scale = 1 / TheSynth->GetUIScale();
GetDimensions(width, height);
const int margin = static_cast<int>(UserPrefs.minimap_margin.Get());
switch (UserPrefs.minimap_corner.GetIndex())
{
default:
case static_cast<int>(MinimapCorner::TopRight):
mX = floor((ofGetWidth() * scale) - (width + margin));
mY = margin + TheTitleBar->GetRect().height;
break;
case static_cast<int>(MinimapCorner::TopLeft):
mX = margin;
mY = margin + TheTitleBar->GetRect().height;
break;
case static_cast<int>(MinimapCorner::BottomRight):
mX = floor((ofGetWidth() * scale) - (width + margin));
mY = floor((ofGetHeight() * scale) - (height + margin));
break;
case static_cast<int>(MinimapCorner::BottomLeft):
mX = margin;
mY = floor((ofGetHeight() * scale) - (height + margin));
break;
}
}
|