1 |
/* |
/* |
2 |
* Copyright (C) 2006-2015 Andreas Persson |
* Copyright (C) 2006-2017 Andreas Persson |
3 |
* |
* |
4 |
* This program is free software; you can redistribute it and/or |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License as |
* modify it under the terms of the GNU General Public License as |
17 |
* 02110-1301 USA. |
* 02110-1301 USA. |
18 |
*/ |
*/ |
19 |
|
|
20 |
|
#include "global.h" |
21 |
#include "regionchooser.h" |
#include "regionchooser.h" |
22 |
|
|
23 |
#include <algorithm> |
#include <algorithm> |
24 |
|
#include <assert.h> |
25 |
|
|
26 |
#include <cairomm/context.h> |
#include <cairomm/context.h> |
27 |
#include <gdkmm/general.h> |
#include <gdkmm/general.h> |
28 |
#include <gdkmm/cursor.h> |
#include <gdkmm/cursor.h> |
29 |
#include <gtkmm/stock.h> |
#include <gtkmm/stock.h> |
30 |
|
#include <gdkmm/pixbuf.h> |
31 |
#include <gtkmm/spinbutton.h> |
#include <gtkmm/spinbutton.h> |
32 |
#include <gtkmm/dialog.h> |
#include <gtkmm/dialog.h> |
33 |
|
|
34 |
#include "global.h" |
#include "Settings.h" |
35 |
|
#include "gfx/builtinpix.h" |
36 |
|
|
37 |
#define REGION_BLOCK_HEIGHT 30 |
#define REGION_BLOCK_HEIGHT 30 |
38 |
#define KEYBOARD_HEIGHT 40 |
#define KEYBOARD_HEIGHT 40 |
39 |
|
|
40 |
|
struct RegionFeatures { |
41 |
|
int sampleRefs; |
42 |
|
int loops; |
43 |
|
|
44 |
|
RegionFeatures() { |
45 |
|
sampleRefs = loops = 0; |
46 |
|
} |
47 |
|
}; |
48 |
|
|
49 |
|
static RegionFeatures regionFeatures(gig::Region* rgn) { |
50 |
|
RegionFeatures f; |
51 |
|
for (int i = 0; i < rgn->DimensionRegions; ++i) { |
52 |
|
gig::DimensionRegion* dr = rgn->pDimensionRegions[i]; |
53 |
|
if (dr->pSample) f.sampleRefs++; |
54 |
|
// the user doesn't care about loop if there is no valid sample reference |
55 |
|
if (dr->pSample && dr->SampleLoops) f.loops++; |
56 |
|
} |
57 |
|
return f; |
58 |
|
} |
59 |
|
|
60 |
void SortedRegions::update(gig::Instrument* instrument) { |
void SortedRegions::update(gig::Instrument* instrument) { |
61 |
// Usually, the regions in a gig file are ordered after their key |
// Usually, the regions in a gig file are ordered after their key |
62 |
// range, but there are files where they are not. The |
// range, but there are files where they are not. The |
78 |
} |
} |
79 |
|
|
80 |
gig::Region* SortedRegions::next() { |
gig::Region* SortedRegions::next() { |
81 |
region_iterator++; |
++region_iterator; |
82 |
return region_iterator == regions.end() ? 0 : *region_iterator; |
return region_iterator == regions.end() ? 0 : *region_iterator; |
83 |
} |
} |
84 |
|
|
86 |
|
|
87 |
RegionChooser::RegionChooser() : |
RegionChooser::RegionChooser() : |
88 |
activeKeyColor("red"), |
activeKeyColor("red"), |
89 |
red("#8070ff"), |
blue("#4796ff"), |
90 |
grey1("grey69"), |
grey1("grey69"), |
91 |
white("white"), |
white("white"), |
92 |
black("black"), |
black("black"), |
93 |
m_VirtKeybModeChoice(_("Virtual Keyboard Mode")), |
m_VirtKeybModeChoice(_("Virtual Keyboard Mode")), |
94 |
currentActiveKey(-1) |
currentActiveKey(-1), |
95 |
|
modifyallregions(false) |
96 |
{ |
{ |
97 |
set_size_request(500, KEYBOARD_HEIGHT + REGION_BLOCK_HEIGHT); |
set_size_request(500, KEYBOARD_HEIGHT + REGION_BLOCK_HEIGHT); |
98 |
|
|
99 |
|
loadBuiltInPix(); |
100 |
|
|
101 |
|
// create gray blue hatched pattern |
102 |
|
{ |
103 |
|
const int width = grayBlueHatchedPattern->get_width(); |
104 |
|
const int height = grayBlueHatchedPattern->get_height(); |
105 |
|
const int stride = grayBlueHatchedPattern->get_rowstride(); |
106 |
|
|
107 |
|
// manually convert from RGBA to ARGB |
108 |
|
this->grayBlueHatchedPatternARGB = grayBlueHatchedPattern->copy(); |
109 |
|
const int pixelSize = stride / width; |
110 |
|
const int totalPixels = width * height; |
111 |
|
assert(pixelSize == 4); |
112 |
|
unsigned char* ptr = this->grayBlueHatchedPatternARGB->get_pixels(); |
113 |
|
for (int iPixel = 0; iPixel < totalPixels; ++iPixel, ptr += pixelSize) { |
114 |
|
const unsigned char r = ptr[0]; |
115 |
|
const unsigned char g = ptr[1]; |
116 |
|
const unsigned char b = ptr[2]; |
117 |
|
const unsigned char a = ptr[3]; |
118 |
|
ptr[0] = b; |
119 |
|
ptr[1] = g; |
120 |
|
ptr[2] = r; |
121 |
|
ptr[3] = a; |
122 |
|
} |
123 |
|
|
124 |
|
Cairo::RefPtr<Cairo::ImageSurface> imageSurface = Cairo::ImageSurface::create( |
125 |
|
this->grayBlueHatchedPatternARGB->get_pixels(), Cairo::FORMAT_ARGB32, width, height, stride |
126 |
|
); |
127 |
|
this->grayBlueHatchedSurfacePattern = Cairo::SurfacePattern::create(imageSurface); |
128 |
|
this->grayBlueHatchedSurfacePattern->set_extend(Cairo::EXTEND_REPEAT); |
129 |
|
} |
130 |
|
|
131 |
instrument = 0; |
instrument = 0; |
132 |
region = 0; |
region = 0; |
133 |
resize.active = false; |
resize.active = false; |
218 |
{ |
{ |
219 |
} |
} |
220 |
|
|
221 |
|
void RegionChooser::setModifyAllRegions(bool b) { |
222 |
|
modifyallregions = b; |
223 |
|
// redraw required parts |
224 |
|
queue_draw(); |
225 |
|
} |
226 |
|
|
227 |
void RegionChooser::invalidate_key(int key) { |
void RegionChooser::invalidate_key(int key) { |
228 |
const int h = KEYBOARD_HEIGHT; |
const int h = KEYBOARD_HEIGHT; |
229 |
const int w = get_width() - 1; |
const int w = get_width() - 1; |
374 |
cr->line_to(x3, h1 - 0.5); |
cr->line_to(x3, h1 - 0.5); |
375 |
cr->stroke(); |
cr->stroke(); |
376 |
|
|
377 |
Gdk::Cairo::set_source_rgba(cr, region == r ? red : white); |
if (region == r) |
378 |
|
Gdk::Cairo::set_source_rgba(cr, blue); |
379 |
|
else if (modifyallregions) |
380 |
|
cr->set_source(grayBlueHatchedSurfacePattern); |
381 |
|
else |
382 |
|
Gdk::Cairo::set_source_rgba(cr, white); |
383 |
|
|
384 |
cr->rectangle(x3 + 1, 1, x2 - x3 - 1, h1 - 2); |
cr->rectangle(x3 + 1, 1, x2 - x3 - 1, h1 - 2); |
385 |
cr->fill(); |
cr->fill(); |
386 |
Gdk::Cairo::set_source_rgba(cr, black); |
Gdk::Cairo::set_source_rgba(cr, black); |
391 |
|
|
392 |
for (gig::Region* r = regions.first() ; r ; r = regions.next()) { |
for (gig::Region* r = regions.first() ; r ; r = regions.next()) { |
393 |
int x = key_to_x(r->KeyRange.low, w); |
int x = key_to_x(r->KeyRange.low, w); |
394 |
|
int x2 = key_to_x(r->KeyRange.high + 1, w); |
395 |
|
|
396 |
|
RegionFeatures features = regionFeatures(r); |
397 |
|
|
398 |
|
const bool bShowLoopSymbol = features.loops > 0; |
399 |
|
const bool bShowSampleRefSymbol = features.sampleRefs < r->DimensionRegions; |
400 |
|
if (bShowLoopSymbol || bShowSampleRefSymbol) { |
401 |
|
const int margin = 2; |
402 |
|
const int wRgn = x2 - x; |
403 |
|
//printf("x=%d x2=%d wRgn=%d\n", x, x2, wRgn); |
404 |
|
|
405 |
|
cr->save(); |
406 |
|
cr->set_line_width(1); |
407 |
|
cr->rectangle(x, 1, wRgn, h1 - 1); |
408 |
|
cr->clip(); |
409 |
|
if (bShowSampleRefSymbol) { |
410 |
|
const int wPic = 8; |
411 |
|
const int hPic = 8; |
412 |
|
Gdk::Cairo::set_source_pixbuf( |
413 |
|
cr, (features.sampleRefs) ? yellowDot : redDot, |
414 |
|
x + (wRgn-wPic)/2.f, |
415 |
|
(bShowLoopSymbol) ? margin : (h1-hPic)/2.f |
416 |
|
); |
417 |
|
cr->paint(); |
418 |
|
} |
419 |
|
if (bShowLoopSymbol) { |
420 |
|
const int wPic = 12; |
421 |
|
const int hPic = 14; |
422 |
|
Gdk::Cairo::set_source_pixbuf( |
423 |
|
cr, (features.loops == r->DimensionRegions) ? blackLoop : grayLoop, |
424 |
|
x + (wRgn-wPic)/2.f, |
425 |
|
(bShowSampleRefSymbol) ? h1 - hPic - margin : (h1-hPic)/2.f |
426 |
|
); |
427 |
|
cr->paint(); |
428 |
|
} |
429 |
|
cr->restore(); |
430 |
|
} |
431 |
|
} |
432 |
|
|
433 |
|
for (gig::Region* r = regions.first() ; r ; r = regions.next()) { |
434 |
|
int x = key_to_x(r->KeyRange.low, w); |
435 |
|
|
436 |
if (x < clip_low) continue; |
if (x < clip_low) continue; |
437 |
if (x >= clip_high) break; |
if (x >= clip_high) break; |
451 |
layout->set_width(get_width() * Pango::SCALE); |
layout->set_width(get_width() * Pango::SCALE); |
452 |
//layout->set_height(get_height() * Pango::SCALE); |
//layout->set_height(get_height() * Pango::SCALE); |
453 |
layout->set_spacing(10); |
layout->set_spacing(10); |
454 |
Gdk::Cairo::set_source_rgba(cr, red); |
Gdk::Cairo::set_source_rgba(cr, blue); |
455 |
// get the text dimensions |
// get the text dimensions |
|
Pango::Rectangle rect = layout->get_logical_extents(); |
|
456 |
int text_width, text_height; |
int text_width, text_height; |
457 |
layout->get_pixel_size(text_width, text_height); |
layout->get_pixel_size(text_width, text_height); |
458 |
cr->move_to(0, (REGION_BLOCK_HEIGHT - text_height) / 2); |
cr->move_to(0, (REGION_BLOCK_HEIGHT - text_height) / 2); |
624 |
void RegionChooser::update_after_move(int pos) |
void RegionChooser::update_after_move(int pos) |
625 |
{ |
{ |
626 |
instrument_struct_to_be_changed_signal.emit(instrument); |
instrument_struct_to_be_changed_signal.emit(instrument); |
627 |
region->SetKeyRange(pos, pos + region->KeyRange.high - |
const int range = region->KeyRange.high - region->KeyRange.low; |
628 |
region->KeyRange.low); |
const int diff = pos - int(region->KeyRange.low); |
629 |
|
region->SetKeyRange(pos, pos + range); |
630 |
|
if (Settings::singleton()->moveRootNoteWithRegionMoved) { |
631 |
|
for (int i = 0; i < 256; ++i) { |
632 |
|
gig::DimensionRegion* dimrgn = region->pDimensionRegions[i]; |
633 |
|
if (!dimrgn || !dimrgn->pSample || !dimrgn->PitchTrack) continue; |
634 |
|
dimrgn->UnityNote += diff; |
635 |
|
} |
636 |
|
} |
637 |
regions.update(instrument); |
regions.update(instrument); |
638 |
instrument_changed.emit(); |
instrument_changed.emit(); |
639 |
instrument_struct_changed_signal.emit(instrument); |
instrument_struct_changed_signal.emit(instrument); |
745 |
dimensionManager.set_region(region); |
dimensionManager.set_region(region); |
746 |
} |
} |
747 |
|
|
748 |
|
void RegionChooser::select_next_region() { |
749 |
|
if (!instrument) return; |
750 |
|
if (!region) { |
751 |
|
for (int i = 0; i < 128; ++i) { |
752 |
|
::gig::Region* rgn = instrument->GetRegion(i); |
753 |
|
if (rgn) { |
754 |
|
set_region(rgn); |
755 |
|
return; |
756 |
|
} |
757 |
|
} |
758 |
|
} else { |
759 |
|
bool currentFound = false; |
760 |
|
for (int i = 0; i < 128; ++i) { |
761 |
|
::gig::Region* rgn = instrument->GetRegion(i); |
762 |
|
if (!rgn) continue; |
763 |
|
if (currentFound) { |
764 |
|
if (rgn != region) { |
765 |
|
set_region(rgn); |
766 |
|
return; |
767 |
|
} |
768 |
|
} else { |
769 |
|
if (rgn == region) currentFound = true; |
770 |
|
} |
771 |
|
} |
772 |
|
} |
773 |
|
} |
774 |
|
|
775 |
|
void RegionChooser::select_prev_region() { |
776 |
|
if (!instrument) return; |
777 |
|
if (!region) { |
778 |
|
for (int i = 0; i < 128; ++i) { |
779 |
|
::gig::Region* rgn = instrument->GetRegion(i); |
780 |
|
if (rgn) { |
781 |
|
set_region(rgn); |
782 |
|
return; |
783 |
|
} |
784 |
|
} |
785 |
|
} else { |
786 |
|
bool currentFound = false; |
787 |
|
for (int i = 127; i >= 0; --i) { |
788 |
|
::gig::Region* rgn = instrument->GetRegion(i); |
789 |
|
if (!rgn) continue; |
790 |
|
if (currentFound) { |
791 |
|
if (rgn != region) { |
792 |
|
set_region(rgn); |
793 |
|
return; |
794 |
|
} |
795 |
|
} else { |
796 |
|
if (rgn == region) currentFound = true; |
797 |
|
} |
798 |
|
} |
799 |
|
} |
800 |
|
} |
801 |
|
|
802 |
void RegionChooser::motion_resize_region(int x, int y) |
void RegionChooser::motion_resize_region(int x, int y) |
803 |
{ |
{ |
804 |
const int w = get_width() - 1; |
const int w = get_width() - 1; |
847 |
|
|
848 |
update_after_resize(); |
update_after_resize(); |
849 |
|
|
850 |
get_window()->invalidate_rect(rect, false); |
//get_window()->invalidate_rect(rect, false); |
851 |
|
get_window()->invalidate(false); // repaint entire region, otherwise it would create visual artifacts |
852 |
} |
} |
853 |
} |
} |
854 |
|
|