Subversion Repositories DIN Is Noise

Rev

Rev 385 | Rev 964 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
* button.cc
* DIN Is Noise is copyright (c) 2006-2018 Jagannathan Sampath
* For more information, please visit http://dinisnoise.org/
*/


#include "button.h"
#include "font.h"
#include "chrono.h"
#include "input.h"

using std::string;

button::button () {
  lsnr = 0;
  start_time = 0;
  click = not_clicked;
  click_repeat = 0;
  first_repeat_time = 1./4;
  subsequent_repeat_time = 1./16;
}

void button::set_label (const string& l) {
  label = l;
  set_name (label);
  int width = (int) get_char_width (label);
  const box<int>& e = extents;
  set_extents (e.left, e.bottom, e.left + width, e.bottom + get_max_char_height (label));
  set_pos (e.left, e.bottom);
}

void button::draw () {
  widget::draw ();
  draw_string (label, posx, posy);
}

int button::handle_input () {
  widget::handle_input ();
  if (lmb) {
    if (hover) {
      widget::focus = this;
      if (click == not_clicked) {
        if (click_repeat) {
          start_time = ui_clk ();
          repeat_time = first_repeat_time;
        }
        click = clicked_indeed;
        call_listener ();
        return click;
      }
      if (click_repeat) {
        double now = ui_clk ();
        double dt = now - start_time;
        if (dt >= repeat_time) {
          call_listener ();
          repeat_time = subsequent_repeat_time;
          start_time = now;
        }
      }
    }
  } else {
    repeat_time = first_repeat_time;
    click = not_clicked;
    if (widget::focus == this) widget::focus = 0;
  }
  return click;
}