File: ui.cc

package info (click to toggle)
din 5.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,152 kB
  • ctags: 2,490
  • sloc: cpp: 9,369; sh: 6,563; ansic: 2,977; tcl: 1,770; makefile: 285
file content (609 lines) | stat: -rw-r--r-- 15,190 bytes parent folder | download | duplicates (3)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * This file is part of din.
 *
 * din is copyright (c) 2006 - 2012 S Jagannathan <jag@dinisnoise.org>
 * For more information, please visit http://dinisnoise.org
 *
 * din 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.
 *
 * din 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 din.  If not, see <http://www.gnu.org/licenses/>.
 *
*/
#include "ui_list.h"
#include "din.h"
#include "morse_code.h"
#include "console.h"
#include "curve_editor.h"
#include "tcl_interp.h"
#include "command.h"
#include "chrono.h"
#include "font_editor.h"
#include "key_consts.h"
#include "keyboard_keyboard.h"
#include "fft.h"
#include "viewwin.h"
#include "authors_note.h"

using namespace std;

extern console cons;
extern cmdlist cmdlst;
extern din din0;
extern morse_code mc;
extern curve_editor delayed;
extern string dotdin;
extern tcl_interp* p_interp;
extern font fnt;
extern curve_editor waved, comed, octed, veled, dmod_ed;

extern authors_note anote;

extern din din0;

extern keyboard_keyboard keybd2;
extern fft fft0;

extern ui_list uis;

extern audio_out aout;

extern int quit_now;

extern viewport view;

extern float ATTACK_TIME;
extern float DECAY_TIME;
extern float PITCH_BEND;
extern float NOTE_VOLUME;

ui_list::ui_list () :
fed (fnt, "font.ed"),
d_parameters ("parameters"),
lfs_attack_time (0, 50, "attack_time", 8, 70, 15, atv),
lfs_decay_time (0, 30, "decay_time", 8, 70, 15, dkv),
lfs_pitch_bend (0, 10, "pitch_bend", 8, 70, 15, pbv),
lfs_note_volume (0, 20, "note_volume", 8, 70, 15, nvv),
d_min_max ("min/max"),
mml_min ("min"), mml_max ("max"),
mmf_min (8), mmf_max(8),
lmm_attack (lfs_attack_time),
lmm_decay (lfs_decay_time),
lmm_pitch_bend (lfs_pitch_bend),
lmm_note_volume (lfs_note_volume) {

  current = 0;
  prev = 0;

  crved = 0;

  ui* scrs [] = {
    &din0,
    &din0.waved,
    &keybd2.waved,
    &din0.moded,
    &din0.gated,
    &delayed,
    &octed,
    &din0.droneed,
    &comed,
    &mc.ed,
    &dmod_ed,
    &fed,
    &keybd2.attacked,
    &keybd2.decayed,
    &keybd2.veled,
    0
  };

  for (ui** p = scrs; *p != 0; ++p) {
    uis.push_back (*p);
  }

  esct = -1;

  setup_widgets ();

}

void ui_list::set_current (ui* u, int ised) {

  if (current) {
    current->leave ();
    prev = current;
  }

  current = u;
  cons << console::yellow << "@ " << current->name << eol;
  cons.reset_tips ();
  current->enter ();

  if (ised) {
    crved = dynamic_cast<curve_editor*>(u);
    if (crved->sine_enabled) fft0.go (crved->curveinfo[0].curve);
  } else {
    crved = 0;
    uis[0] = current;
  }


}

bool ui_list::handle_input () {

  current->calc_win_mouse ();

  din0.bg (); // bg for handling phrasor on microtonal-keyboard
  keybd2.bg (); // bg for removing decayed notes on keyboard-keyboard
  cons.bg (); // bg for tips display

  for (vector<ui*>::size_type i = 1, j = uis.size(); i < j; ++i) uis[i]->bg (); // bg for editors

  // fade fx for voice, gater & delay
  //
  eval_fade (fdr_voice, w_voice);
  flash_gater (); // flashes in sync with gater value
  eval_fade (fdr_delay, w_delay);


  if (widget::focus == 0) { // find a widget that has focus
    for (vector<widget*>::size_type i = 0, j = widgets.size (); i < j; ++i) {
      widget* wi = widgets[i];
      if (wi->is_screen (current) && wi->visible () && wi->enabled ()) {
        if (wi->handle_input ()) {
          return true;
        }
      }
    }
  } else { // handle input of widget with focus
    widget* wf = widget::focus;
    if (wf->handle_input ()) return true;
  }

  current->handle_input ();

  if (keypressed (k_enter)) { // calc tap bpm
    extern nano_chrono ui_clk;
    extern double TAP_BPM;
    static double last_tapt = ui_clk.secs;
    double dt = ui_clk.secs - last_tapt;
    if (dt) TAP_BPM = 60.0 / dt;
    extern tcl_interp interpreter;
    if (keydown (k_lctrl)) cons ("set resetbeat 1");
    Tcl_UpdateLinkedVar (interpreter.interp, "tapbpm");
    last_tapt = ui_clk.secs;
  }

  extern string INSTRUMENT;

  if (keypressed (k_1)) { // switch instrument
    if (crved == 0) {
      if (current == &din0) {
        INSTRUMENT = "keyboard_keyboard";
        set_current (&keybd2, 0);
        cons ("setup-editors");
      } else {
        INSTRUMENT = "microtonal_keyboard";
        set_current (&din0, 0);
        cons ("setup-editors");
      }
    } else set_current (uis[0], 0);
  } else {

    for (int i = 0; i < MAX_EDITORS; ++i) {
      if (keypressed (key[i])) {
        if (ed[i]) set_current (ed[i]);
        break;
      }
    }
  }

  extern nano_chrono ui_clk;
  if (keypressed (k_esc)) {
    if (cons.command_mode || (current != uis[0])) {
      cons.command_mode = 0;
      set_current (uis[0], 0);
      esct = -1;
    } else {
      esc:
      if (esct == -1) {
        cons << console::red << "Press ESC again to quit" << eol;
        esct = ui_clk.secs;
      } else {
        static const float ESC_TIMEOUT = 1; // in seconds
        float dt = ui_clk.secs - esct;
        if (dt > ESC_TIMEOUT) {
          esct = -1;
          goto esc;
        } else {
          din0.save_scale ();
          din0.dinfo.save ();
          if (din0.delete_all_drones ()) {
            w_voice.turn_off ();
            w_delay.turn_off ();
            quit_now = 1;
            cons << console::green << "Goodbye!" << eol;
          }
        }
      }
    }
  }

  return true;
}

bool ui_list::set_editor (const string& name, int screen) {
  if (screen > 1) {
    ui* ei = 0;
    for (int i = 0, j = uis.size(); i < j; ++i) {
      if (uis[i]->name == name) {
        ei = uis[i];
        break;
      }
    }
    if (ei) {
      ed [screen - 2] = ei;
      return true;
    } else {
      cmdlst.result = "bad editor name";
      return false;
    }
  } else {
    cmdlst.result = "bad screen number (valid: 2 to 8)";
    return false;
  }
}

void ui_list::draw () {

  glClear (GL_COLOR_BUFFER_BIT);

  current->draw ();

  glMatrixMode (GL_PROJECTION);
  glPushMatrix ();
  glLoadIdentity ();
  glOrtho (0, view.xmax, 0, view.ymax, -1, 1);

  glMatrixMode (GL_MODELVIEW);
  glPushMatrix ();
  glLoadIdentity ();

  for (vector<widget*>::size_type i = 0, j = widgets.size (); i < j; ++i) {
    widget* wi = widgets[i];
    if (wi->is_screen (current) && wi->visible()) {
      wi->draw ();
    }
  }

  glPopMatrix ();
  glMatrixMode (GL_PROJECTION);
  glPopMatrix ();
  glMatrixMode (GL_MODELVIEW);

}

void ui_list::setup_widgets () {

  w_voice.add_screen (&din0);
  w_voice.set_listener (&vlis);

  w_gater.add_screen (&din0);
  w_gater.set_listener (&glis);

  w_delay.add_screen (&din0);
  w_delay.add_screen (&keybd2);
  w_delay.set_listener (&dlis);

  w_compress.add_screen (&din0);
  w_compress.add_screen (&keybd2);
  w_compress.set_listener (&clis);
  w_compress.color_blend (0);

  widgets.push_back (&w_voice);
  widgets.push_back (&w_gater);
  widgets.push_back (&w_delay);
  widgets.push_back (&w_compress);

  widget* chd [] = {&mml_min, &mml_max, &mmf_min, &mmf_max};
  for (int i = 0; i < 4; ++i) {
    widget& w = *chd[i];
    w.add_screen (&keybd2);
    d_min_max.add_child (&w);
  }

  d_min_max.add_screen (&keybd2);
  d_min_max.set_moveable (1);

  widgets.push_back (&d_min_max);
  for (int i = 0; i < 4; ++i) widgets.push_back (chd[i]);

  d_min_max.hide ();

  widget* pw1[5] = {&d_min_max, &mml_min, &mml_max, &mmf_min, &mmf_max};
  widget_load<widget> ("d_min_max", pw1, 5);
  mmf_min.set_text ("");
  mmf_max.set_text ("");

  lfs_attack_time.set_color (0.45, 0.85, 1);
  lfs_decay_time.set_color (1, 0.65, 0.45);
  lfs_pitch_bend.set_color (0.7, 1, 0.4);
  lfs_note_volume.set_color (1, 0.7, 1);

  lfs_attack_time.add_screen (&keybd2);
  lfs_decay_time.add_screen (&keybd2);
  lfs_pitch_bend.add_screen (&keybd2);
  lfs_note_volume.add_screen (&keybd2);

  label_field_slider<float>* lim[3] = {&lfs_attack_time, &lfs_decay_time, &lfs_note_volume};
  ifstream flim ((dotdin + "d_parameter_limits").c_str(), ios::in);
  float lo, hi;
  string name;
  for (int i = 0, j = 3; i < j; ++i) {
    flim >> name >> lo >> hi;
    label_field_slider<float>* lfs = lim[i];
    lfs->set_name (name);
    lfs->set_limits (lo, hi);
  }

  int ilo, ihi;
  flim >> name >> ilo >> ihi;
  lfs_pitch_bend.set_name (name);
  lfs_pitch_bend.set_limits (ilo, ihi);

  d_parameters.add_screen (&keybd2);
  d_parameters.add_child (&d_min_max);
  d_parameters.add_child (&lfs_attack_time);
  d_parameters.add_child (&lfs_decay_time);
  d_parameters.add_child (&lfs_pitch_bend);
  d_parameters.add_child (&lfs_note_volume);

  widget* pw2[5] = {&d_parameters, &lfs_attack_time, &lfs_decay_time, &lfs_pitch_bend, &lfs_note_volume};
  widget_load<widget> ("d_parameters", pw2, 5);

  d_parameters.set_moveable (1);

  widgets.push_back (&d_parameters);
  widgets.push_back (&lfs_attack_time);
  widgets.push_back (&lfs_decay_time);
  widgets.push_back (&lfs_pitch_bend);
  widgets.push_back (&lfs_note_volume);

  d_parameters.set_name ("parameters");
  lfs_attack_time.set_name ("attack_time");
  lfs_decay_time.set_name ("decay_time");
  lfs_pitch_bend.set_name ("pitch_bend");
  lfs_note_volume.set_name ("note_volume");

  cons.add_screen (0); // 0 for all
  widgets.push_back (&cons);

  w_voice.set_state (din0.dinfo.voice);
  w_gater.set_state (din0.dinfo.gater);
  w_delay.set_state (din0.dinfo.delay);
  w_compress.set_state (din0.dinfo.compress);

  anote.setup_widgets (widgets);

}

void ui_list::update_widgets () {

  w_voice.set_label ("Voice");
  w_gater.set_label ("Gater");
  w_delay.set_label ("Delay");
  w_compress.set_label ("Compressor");

  d_min_max.set_text ("min/max");
  mml_min.set_text ("min");
  mml_max.set_text("max");
  mmf_min.set_text (mmf_min.get_text());
  mmf_max.set_text (mmf_max.get_text());

  button* wd [] = {&w_voice, &w_gater, &w_delay, &w_compress};
  int n = 4, dx = 50, w = 0;
  for (int i = 0; i < n; ++i) w += wd[i]->get_extents ().width;
  w += n * dx;

  int lh = get_line_height ();
  int x = (view.xmax - w) / 2, y = lh + 2;
  for (int i = 0; i < n; ++i) {
    button& b = *wd[i];
    const box<int>& e = b.get_extents ();
    b.set_extents (x, y, x + e.width, y + e.height);
    b.set_pos (x, y);
    x = e.right + dx;
  }

  lfs_attack_time.set_val (ATTACK_TIME);
  lfs_decay_time.set_val (DECAY_TIME);
  lfs_pitch_bend.set_val (PITCH_BEND);
  lfs_note_volume.set_val (NOTE_VOLUME);

  d_parameters.set_text ("parameters");
  lfs_attack_time.update ();
  lfs_decay_time.update ();
  lfs_pitch_bend.update ();
  lfs_note_volume.update ();

  anote.update_widgets ();

}

void ui_list::flash_gater () {
  static color clr [2] = {checkbutton::off_color, checkbutton::on_color};
  float dg = aout.gatr [aout.samples_per_channel - 1];
  if (fdr_gater.eval ()) {
    w_gater.enable (0);
    w_gater.blend_on_off_color (fdr_gater.alpha);
    color& c = const_cast<color&> (w_gater.get_color ());
    c *= dg;
  } else {
    w_gater.enable (1);
    int gi = din0.dinfo.gater;
    w_gater.set_color (dg * clr [gi].r, dg * clr [gi].g, dg * clr[gi].b);
  }
}

float ui_list::eval_fade (fader& fdr, checkbutton& cb) {
  if (fdr.eval ()) {
    cb.enable (0);
    cb.blend_on_off_color (fdr.alpha);
  } else cb.enable (1);

  return fdr.amount;
}

widget* ui_list::get_widget (const string& name) {
  for (size_vw i = 0, j = widgets.size (); i < j; ++i) {
    widget* wi = widgets[i];
    if (wi->get_name() == name) return wi;
  }
  return 0;
}

void setup_fade (fader& fdr, int& target, int what) {
  if (fdr.on == 0) {
    target = what;
    if (what) fdr.set (0, 1); else fdr.set (1, 0);
  }
}

void voice__listener::changed (checkbutton& cb) {
  int what = cb.is_on ();
  setup_fade (uis.fdr_voice, din0.dinfo.voice, what);
}

void gater__listener::changed (checkbutton& cb) {
  int what = cb.is_on ();
  setup_fade (uis.fdr_gater, din0.dinfo.gater, what);
}

void delay__listener::changed (checkbutton& cb) {
  int what = cb.is_on ();
  setup_fade (uis.fdr_delay, din0.dinfo.delay, what);
}

void compress__listener::changed (checkbutton& cb) {
  din0.dinfo.compress = cb.is_on ();
}

ui_list::~ui_list () {

  widget* pw1[5] = {&d_parameters, &lfs_attack_time, &lfs_decay_time, &lfs_pitch_bend, &lfs_note_volume};
  widget_save<widget> ("d_parameters", pw1, 5);

  widget* pw2[5] = {&d_min_max, &mml_min, &mml_max, &mmf_min, &mmf_max};
  widget_save<widget> ("d_min_max", pw2, 5);

  label_field_slider<float>* lim[3] = {&lfs_attack_time, &lfs_decay_time, &lfs_note_volume};
  ofstream flim ((dotdin + "d_parameter_limits").c_str(), ios::out);
  float lo, hi;
  for (int i = 0, j = 3; i < j; ++i) {
    label_field_slider<float>* lfs = lim[i];
    lfs->get_limits (lo, hi);
    flim << lfs->get_name () << ' ' << lo << ' ' << hi << endl;
  }

  int ilo, ihi;
  lfs_pitch_bend.get_limits (ilo, ihi);
  flim << lfs_pitch_bend.get_name () << ' ' << ilo << ' ' << ihi << endl;

}

void attack_val::operator() (const float& f) {

  ATTACK_TIME = f;

}

void decay_val::operator() (const float& f) {
  DECAY_TIME = f;
}

void pitch_bend_val::operator() (const int& i) {
  PITCH_BEND = i;
}

void note_volume_val::operator() (const float& f) {
  NOTE_VOLUME = f;
  keybd2.calc_visual_params ();
}

template <typename T> void widget_load (const string& fname, T** pw, int n) {
  ifstream f ((dotdin + fname).c_str(), ios::in);
  string ignore; int x, y;
  for (int i = 0; i < n; ++i) {
    f >> ignore >> x >> y;
    pw[i]->set_pos (x, y);
  }
}

template <typename T> void widget_save (const string& fname, T** pw, int n) {
  ofstream f ((dotdin + fname).c_str(), ios::out);
  for (int i = 0; i < n; ++i) pw[i]->save (f);
}

template <typename T> min_max_clicked<T>::min_max_clicked (label_field_slider<T>& _lfs) : lfs (_lfs) {
  lfs.set_button_listener (this);
}

template <typename T> void min_max_clicked<T>::clicked (button& b) {

  // toggle
  if (uis.d_min_max.visible ()) {
    if (&b == ui_list::mm_last) uis.d_min_max.hide ();
  } else {
    uis.d_min_max.show ();
  }

  // label min/max
  uis.d_min_max.set_text (lfs.get_text ());

  // setup color
  widget* w [5] = {&uis.d_min_max, &uis.mml_min, &uis.mml_max, &uis.mmf_min, &uis.mmf_max};
  const color& clr = lfs.get_color ();
  for (int i = 0; i < 5; ++i) w[i]->set_color (clr);

  // load min/max fields
  T low, high;
  lfs.get_limits (low, high);

  uis.mmf_min.set_text (low);
  uis.mmf_max.set_text (high);

  // setup listeners
  uis.mmf_min.set_listener (this);
  uis.mmf_max.set_listener (this);

  ui_list::mm_last = &b;


}

template <typename T> void min_max_clicked<T>::changed (field& f) {

  stringstream ss;
  ss << f.get_text ();
  T t; ss >> t;
  T l, h; lfs.get_limits (l, h);

  if (&f == &uis.mmf_min) {
    lfs.set_limits (t, h);
  } else {
    lfs.set_limits (l, t);
  }

  lfs.get_limits (l, h);
  uis.mmf_min.set_text (l);
  uis.mmf_max.set_text (h);

}