Subversion Repositories DIN Is Noise

Rev

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

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


#include "item_list.h"
#include "font.h"
#include <fstream>
using namespace std;

extern fstream dlog;

extern int mousex, mouseyy;

void item_list::add (const string& item) {
  items.push_back (item);
}

void item_list::set_pos (int x, int y) {
  posx = x;
  posy = y;
  extents.left = posx;
  extents.bottom = posy;
  extents.top = extents.bottom;
  int lh = get_line_height ();
  for (int i = 0, j = items.size (); i < j; ++i) {
    extents.right = max (extents.right, posx + get_char_width (items[i]));
    extents.top += lh;
  }
  extents.calc ();
}

void item_list::draw () {
  int lh = get_line_height ();
  int x = extents.left, y = extents.bottom;
  for (int i = 0, j = items.size(); i < j; ++i) {
    if (i == hovered_item) glColor3f (0, 1, 0); else glColor3f (clr.r, clr.g, clr.b);
    draw_string (items[i], x, y);
    y += lh;
  }
}

int item_list::handle_input () {
  int r = button::handle_input ();
  if (hover) {
    int lh = get_line_height ();
    int x = extents.left, yl = extents.bottom, yh = yl + lh;
    for (int i = 0, j = items.size(); i < j; ++i) {
      if (mousex >= x && (mouseyy >= yl && mouseyy <= yh)) {
        hovered_item = i;
        break;
      }
      yl = yh;
      yh += lh;
    }
  } else hovered_item = -1;
  return r;
}