Fixed timeline drawing problem at extreme range.
[epichord.git] / src / timeline.cpp
blob58164d5f15c83f8420b1500b1fbf825568203a19
1 /*
2 Epichord - a midi sequencer
3 Copyright (C) 2008 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
23 #include <stdio.h>
24 #include <unistd.h>
26 #include <fltk/Group.h>
27 #include <fltk/Widget.h>
28 #include <fltk/events.h>
32 #include "ui.h"
33 extern UI* ui;
35 #include "backend.h"
37 #include "config.h"
39 #include "uihelper.h"
41 extern struct conf config;
43 using namespace fltk;
45 Timeline::Timeline(int x, int y, int w, int h, const char* label = 0) : fltk::Widget(x, y, w, h, label) {
46 hand = fltk::SharedImage::get(ROOT_DATA_DIR"gfx/hand.gif");
47 if(!hand){
48 printf("gfx files not found, installed correctly?\n");
50 scale = 1;
51 label_scale = 1;
52 zoom = 30;
53 ticks_offset = 0;
54 edit_flag = 0;
56 px_last = 0;
59 int Timeline::handle(int event){
61 int tick;
63 switch(event){
64 case fltk::FOCUS:
65 return 1;
66 case fltk::PUSH:
67 take_focus();
68 if(event_button() == 1){//set left limit
69 //pixel -> tick -> quantize -> global tick
70 tick = quantize(xpix2tick(event_x())*scale/4);
71 //printf("%d %d %d\n",tick,loop_start,loop_end);
72 if(tick < get_loop_end()){
73 set_loop_start(tick);
76 else if(event_button() == 2){//set song position
77 tick = quantize(xpix2tick(event_x())*scale/4);
78 //printf("%d %d %d\n",tick,loop_start,loop_end);
79 //pointer_x = tick2xpix(tick);
80 ui->song_timeline->update(tick);
81 ui->pattern_timeline->update(tick);
82 all_notes_off();
83 reset_backend(tick);
84 ui->song_timeline->redraw();
85 ui->pattern_timeline->redraw();
87 else if(event_button() == 3){//set right limit
88 tick = quantize(xpix2tick(event_x())*scale/4);
89 //printf("%d %d %d\n",tick,loop_start,loop_end);
90 if(tick > get_loop_start()){
91 set_loop_end(tick);
94 redraw();
95 return 1;
97 return 0;
100 void Timeline::draw(){
102 fltk::setfont(fltk::HELVETICA,12);
103 fltk::setcolor(fltk::WHITE);
104 fltk::fillrect(0,0,w(),h());
106 fltk::push_clip(0,0,w(),h());
107 fltk::setcolor(fltk::BLACK);
108 fltk::drawline(0,h()-1,w()-1,h()-1);
110 if(edit_flag){//draw pattern timeline
112 int M = config.beats_per_measure;
114 int I=0;
115 for(int i=0; I<w(); i++){
116 I = i*zoom - scroll;
117 if(I>=0){
118 fltk::fillrect(I,h()/2,1,h()/2);
122 fltk::setcolor(fltk::BLACK);
124 char buf[64];
125 int j = 0;
127 I=0;
128 for(int i=0; I<w(); i++){
129 I = i*zoom*4 - scroll;
130 if(I>=0){
131 fltk::fillrect(I,0,1,h()/2);
132 sprintf(buf,"%u",j*label_scale);
133 fltk::drawtext(buf,I+3,h()-3);
135 j++;
139 else{//draw song timeline
141 int M = config.beats_per_measure;
142 int P = config.measures_per_phrase;
143 int I=0;
144 for(int i=0; I<w(); i++){
145 I = i*zoom*M/4 - scroll;
146 if(I>=0){
147 fltk::fillrect(I,h()/2,1,h()/2);
151 fltk::setcolor(fltk::BLACK);
153 char buf[64];
154 int j = 0;
156 int p = P>0 ? P : 4;
157 I=0;
158 for(int i=0; I<w(); i++){
159 I = i*zoom*4*p*M/4/4 - scroll;
160 if(I>=0){
161 fltk::fillrect(I,0,1,h()/2);
162 sprintf(buf,"%u",j*label_scale*p/4);
163 fltk::drawtext(buf,I+3,h()-3);
165 j++;
170 int X = tick2xpix(get_loop_start()*4/scale);
171 if(X >= -10){
172 fltk::setcolor(fltk::color(0,0,255));
173 fillrect(X+0,0,1,h()-1);
174 fltk::setcolor(fltk::color(85,85,255));
175 fillrect(X+1,0,1,h()-1);
176 fltk::setcolor(fltk::color(170,170,255));
177 fillrect(X+2,0,1,h()-1);
180 X = tick2xpix(get_loop_end()*4/scale);
181 if(X >= -10){
182 fltk::setcolor(fltk::color(128,0,0));
183 fillrect(X+0,0,1,h()-1);
184 fltk::setcolor(fltk::color(170,42,42));
185 fillrect(X-1,0,1,h()-1);
186 fltk::setcolor(fltk::color(255,170,170));
187 fillrect(X-2,0,1,h()-1);
190 if(hand && pointer_x*4/scale-scroll-10 >= -20){
191 hand->draw(pointer_x*4/scale-scroll-10,h()-19);
194 fltk::pop_clip();
199 void Timeline::update(int ticks){
200 //printf("%d\n",ticks);
201 pointer_x = (ticks/16 - ticks_offset/16) * zoom / 8;
202 if(pointer_x != px_last && ticks != get_loop_end()){
203 px_last = pointer_x;
204 redraw();
208 int Timeline::tick2xpix(int tick){
209 //return (tick - ticks_offset) * zoom / 128 - scroll;
210 return tick * zoom / 128 - scroll - ticks_offset*zoom/32;
213 int Timeline::xpix2tick(int xpix){
214 //return (xpix+scroll+ticks_offset*32/zoom)*128 / zoom;
215 return (xpix+scroll) * 128 / zoom + ticks_offset*120/30;
218 int Timeline::quantize(int tick){
219 return tick/128 * 128;