Files
puzzlez/src/haxe/ru/m/skin/ButtonSVGSkin.hx

69 lines
2.0 KiB
Haxe

package ru.m.skin;
import format.SVG;
import hw.color.Color;
import hw.view.form.ButtonView;
import hw.view.form.ToggleButtonView;
import hw.view.skin.ISkin;
using StringTools;
using hw.color.ColorUtil;
@:style class ButtonSVGSkin implements ISkin<ButtonView> {
@:style(null) public var svg:String;
@:style(0) public var color:Null<Color>;
@:style(false) public var solid:Null<Bool>;
private var svgs:Map<ButtonState, SVG>;
private var needUpdate:Bool;
public function new(?svg:String, ?color:Color, ?solid:Bool) {
this.svg = svg;
this.color = color;
this.solid = solid;
this.needUpdate = true;
}
private inline function buildSVG(color:Color):SVG {
return new SVG(svg.replace("currentColor", '#${color}'));
}
private function update():Void {
if (needUpdate && svg != null) {
var color = color;
if (solid) {
color = color.multiply(1.5);
}
svgs = new Map();
svgs.set(UP, buildSVG(color));
svgs.set(DOWN, buildSVG(color.diff(-24)));
svgs.set(OVER, buildSVG(color.diff(24)));
svgs.set(DISABLED, buildSVG(color.grey()));
needUpdate = false;
}
}
public function draw(view:ButtonView):Void {
update();
var svg = svgs.get(view.state);
var graphics = view.content.graphics;
var color = this.color;
if (Std.is(view, ToggleButtonView)) {
if (!cast(view, ToggleButtonView).on) {
color = color.multiply(0.5);
}
}
graphics.beginFill(0, 0);
graphics.drawRect(0, 0, view.width, view.height);
graphics.beginFill(color);
if (!solid) {
graphics.lineStyle(2, color.multiply(1.5));
}
// ToDo: padding
svg.render(graphics, 0, 0, Std.int(view.width * 0.8), Std.int(view.height * 0.8));
graphics.lineStyle();
graphics.endFill();
}
}