Files
tankz/src/client/haxe/ru/m/skin/ButtonSVGSkin.hx

46 lines
1.4 KiB
Haxe

package ru.m.skin;
import format.SVG;
import haxework.color.Color;
import haxework.color.ColorUtil;
import haxework.view.ButtonView;
import haxework.view.skin.ISkin;
class ButtonSVGSkin implements ISkin<ButtonView> {
private var svg:String;
private var color:Color;
private var svgs:Map<ButtonState, SVG>;
public function new(svg:String, color:Color) {
this.svg = svg;
this.color = color;
init();
}
private inline function buildSVG(color:Color):SVG {
return new SVG(StringTools.replace(svg, "currentColor", '#${StringTools.hex(color)}'));
}
private function init():Void {
svgs = new Map<ButtonState, SVG>();
svgs.set(ButtonState.UP, buildSVG(color));
svgs.set(ButtonState.DOWN, buildSVG(ColorUtil.diff(color, -24)));
svgs.set(ButtonState.OVER, buildSVG(ColorUtil.diff(color, 24)));
svgs.set(ButtonState.DISABLED, buildSVG(ColorUtil.grey(color)));
}
public function draw(view:ButtonView):Void {
var svg = svgs.get(view.state);
var graphics = view.content.graphics;
graphics.beginFill(0, 0);
graphics.drawRect(0, 0, view.width, view.height);
graphics.beginFill(color);
graphics.lineStyle(2, ColorUtil.multiply(color, 1.5));
svg.render(graphics, 0, 0, Std.int(view.width * 0.8), Std.int(view.height * 0.8));
graphics.lineStyle();
graphics.endFill();
}
}