// ----
// ---- file   : ModGridAudio2RPNButton.tks
// ---- author : Bastian Spiegel <bs@tkscript.de>
// ---- legal  : (c) 2021-2023 by Bastian Spiegel.
// ----          Distributed under terms of the GNU LESSER GENERAL PUBLIC LICENSE (LGPL). See
// ----          http://www.gnu.org/licenses/licenses.html#LGPL or COPYING for further information.
// ----
// ---- info   : This is part of the "syntracker" midi/audio sequencer.
// ----
// ---- created: 07Dec2021
// ---- changed: 27Dec2021, 01Nov2023
// ----
// ----
// ----

module MModGridAudio2RPNButton;

use namespace ui;
use namespace st2;


// <class.png>
class ModGridAudio2RPNButton : ModGridButton, ActionConsumer {
   protected ModAudio2RPN *mod;

   protected FloatParam *fp_midich;
   protected FloatParam *fp_rpn;
   protected FloatParam *fp_rise;
   protected FloatParam *fp_fall;
   protected FloatParam *fp_curve;


   // <ui_init.png>
   public method initAudio2RPNButton(ModAudio2RPN _mod) {

      initModGridButton();

      setLayout(SuperBorderLayout);
      setEnableKbdFocusOnMouse(false);
      setLookAndFeel(LookAndFeel.LIGHT);

      mod <= _mod;

      fp_rpn <= FloatParam.New();
      fp_rpn.setVisibleTextLength(2);
      fp_rpn.setStep(1.0);
      fp_rpn.setMinMaxValues(1.0, 32.0);
      fp_rpn.setResetValue(1.0f);
      fp_rpn.setValue(mod.rpn + 1);
      fp_rpn.setPrecision(0);
      fp_rpn.setMousePrecision(0);
      fp_rpn.setAlignment(Layout.CENTERY);
      fp_rpn.setToolTipCaption("MIDI Ctl Nr\n\n (note) 1..32 => RPN #100..#131 (\"remote_seq\" output device)");
      if(UI.IsLoDPI())
         fp_rpn.setPadBottom(1);
      addLayer(deref fp_rpn, Layout.LEFT);

      fp_midich <= FloatParam.New();
      fp_midich.setVisibleTextLength(2);
      fp_midich.setStep(1.0);
      fp_midich.setMinMaxValues(1.0, 16.0);
      fp_midich.setResetValue(1.0f);
      fp_midich.setValue(mod.midi_ch + 1);
      fp_midich.setPrecision(0);
      fp_midich.setMousePrecision(0);
      fp_midich.setAlignment(Layout.CENTERY);
      fp_midich.setToolTipCaption("MIDI Channel (\"remote_seq\" output device)");
      fp_midich.setPadLeft(2);
      if(UI.IsLoDPI())
         fp_midich.setPadBottom(1);
      addLayer(deref fp_midich, Layout.LEFT);

      fp_rise <= FloatParam.New();
      fp_rise.setVisibleTextLength(2);
      fp_rise.setStep(1.0);
      fp_rise.setMinMaxValues(0.0, 100.0);
      fp_rise.setResetValue(50.0f);
      fp_rise.setValue(mod.slew_rise * 100.0);
      fp_rise.setPrecision(3);
      fp_rise.setMousePrecision(0);
      fp_rise.setAlignment(Layout.CENTERY);
      fp_rise.setToolTipCaption("Rise Slew Amount");
      if(UI.IsLoDPI())
         fp_rise.setPadBottom(1);
      addLayer(deref fp_rise, Layout.RIGHT);

      fp_fall <= FloatParam.New();
      fp_fall.setVisibleTextLength(2);
      fp_fall.setStep(1.0);
      fp_fall.setMinMaxValues(0.0, 100.0);
      fp_fall.setResetValue(80.0f);
      fp_fall.setValue(mod.slew_fall * 100.0);
      fp_fall.setPrecision(3);
      fp_fall.setMousePrecision(0);
      fp_fall.setAlignment(Layout.CENTERY);
      fp_fall.setToolTipCaption("Fall Slew Amount");
      if(UI.IsLoDPI())
         fp_fall.setPadBottom(1);
      addLayer(deref fp_fall, Layout.RIGHT);

      fp_curve <= FloatParam.New();
      fp_curve.setVisibleTextLength(2);
      fp_curve.setStep(0.1);
      fp_curve.setMinMaxValues(-9.0, 9.0);
      fp_curve.setResetValue(0.0f);
      fp_curve.setValue(mod.curve * 9.0);
      fp_curve.setPrecision(4);
      fp_curve.setMousePrecision(1);
      fp_curve.setAlignment(Layout.CENTERY);
      fp_curve.setPadRight(2.0f);
      fp_curve.setToolTipCaption("Log..Lin..Exp Curve");
      if(UI.IsLoDPI())
         fp_curve.setPadBottom(1);
      addLayer(deref fp_curve, Layout.RIGHT);
   }

   // <method_set.png>
   public virtual setEnableHold(boolean _bHold) {
      // prevent Button HOLD_CLICK action (=> edit modmatrix)
   }

   // <ui_render.png>
   public virtual onDraw() {
      UIRenderer.DrawDefaultControlBackground(0, 0, getSizeX(), getSizeY());
      drawGridBorder();
      UIRenderer.DrawDefaultRaisedBorderRight(0, 0, getSizeX(), getSizeY());
   }

   // <ui_consume.png>
   public virtual consumeAction(Action _ac) : boolean {
      String acName <= _ac.getActionName();
      ActionProvider ap <= _ac.getActionProvider();

      switch(@(ap))
      {
         case @(fp_midich):
            mod.setMidiCh(fp_midich.getFloatValue() - 1);
            Global.Print("MIDI# is "+fp_midich.getFloatValue());
            current_project.markAsModified();
            return true;

         case @(fp_rpn):
            mod.setRPN(fp_rpn.getFloatValue() - 1);
            Global.Print("Ctl Nr is "+fp_rpn.getFloatValue()+" (=> RPN "+(100 + fp_rpn.getFloatValue()- 1)+")");
            current_project.markAsModified();
            return true;

         case @(fp_rise):
            mod.setSlewRise(fp_rise.getValue() / 100.0);
            mod.addToUIParamHistory(ModAudio2RPN.PARAM_SLEW_RISE);
            Global.Print("Rise slew rate is "+fp_rise.getValue());
            current_project.markAsModified();
            return true;

         case @(fp_fall):
            mod.setSlewFall(fp_fall.getValue() / 100.0);
            mod.addToUIParamHistory(ModAudio2RPN.PARAM_SLEW_FALL);
            Global.Print("Fall slew rate is "+fp_fall.getValue());
            current_project.markAsModified();
            return true;

         case @(fp_curve):
            mod.setCurve(fp_curve.getValue() / 9.0);
            mod.addToUIParamHistory(ModAudio2RPN.PARAM_CURVE);
            Global.Print("Curve is "+fp_curve.getValue());
            current_project.markAsModified();
            return true;
      }

      return ModGridButton::consumeAction(_ac);
   }

}
