#include <lib/std.mi>

Global Group thisGroup;
Global Layer VolumeLayer;
Global Map VolMap;
Global Boolean volClicked;

System.onScriptLoaded() {
  thisGroup = getScriptGroup();
  volClicked = false;
  debugString("onScriptLoaded()", 9);
  if(thisGroup == NULL) return;

  String vLayer = getToken(getParam(), ";", 0);
  String vMap = getToken(getParam(), ";", 1);

  VolumeLayer = thisGroup.getObject(vLayer);
  VolMap = new map;
  VolMap.loadMap(vMap);

  onVolumeChanged(getVolume());
}

System.onVolumeChanged(int vol) {
  volumeLayer.setRegionFromMap(VolMap,vol,1);
}

VolumeLayer.onLeftButtonDown(int x, int y) {
  volClicked = true;
}

VolumeLayer.onLeftButtonUp(int x, int y) {
  volClicked = false;
}

VolumeLayer.onMouseMove(int x, int y) {
  if(volClicked == true) {
    x -= VolumeLayer.getLeft();
    y -= VolumeLayer.getTop();
    if(volMap.inRegion(x,y)) {
      int tvol = VolMap.getValue(x, y);
      setVolume(tvol);
    }
  }
}