62 lines
1.8 KiB
Haxe
62 lines
1.8 KiB
Haxe
package ru.m.skin;
|
|
|
|
import format.SVG;
|
|
import haxework.color.Color;
|
|
import haxework.view.form.ButtonView;
|
|
import haxework.view.skin.ISkin;
|
|
|
|
using StringTools;
|
|
using haxework.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;
|
|
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();
|
|
}
|
|
}
|