//-----------------------------------------------------------------------------
// volumebyregion.m
//
// Example of an Animated Volume bar using Regions
// (Improved, no longer requires HiddenVolume)
//
// written by FrisbeeMonkey
//-----------------------------------------------------------------------------

#include </lib/std.mi>

Function setVolumeAnim(int volValue);
Function updateVolume(int x, int y);

Global Layer Volume;
Global Map VolumeMap;
Global Boolean VolumeChanging;

System.onScriptLoaded() {
	Group VolGrp = getScriptGroup();

	Volume = VolGrp.findObject("Volume");

	VolumeMap = new Map;
	VolumeMap.loadMap("shogo.vol.map");

	setVolumeAnim(System.getVolume());
}

System.onVolumeChanged(int newVol) {
	setVolumeAnim(newVol);
	int p = (newVol * 100) / 255;
}

setVolumeAnim(int Value) {
	Region r = new Region;
	r.loadFromMap(VolumeMap, Value, 1);
	Volume.setRegion(r);
	delete r;
}

Volume.onLeftButtonDown(int x, int y) {
	VolumeChanging = 1;
	updateVolume(x, y);
}
Volume.onLeftButtonUp(int x, int y) {
	if (VolumeChanging) {
		VolumeChanging = 0;
		updateVolume(x, y);
	}
}
Volume.onMouseMove(int x, int y) {
	if (VolumeChanging) {
		updateVolume(x, y);
	}
}

updateVolume(int x, int y) {
	int newValue = VolumeMap.getValue(x - Volume.getLeft(), y - Volume.getTop());
	System.setVolume(newValue);
}

System.onScriptUnloading() {
	delete VolumeMap;
}