add Color abstract

This commit is contained in:
2019-03-11 17:02:34 +03:00
parent 8c0991adb0
commit 62ddbb683e
16 changed files with 196 additions and 153 deletions

View File

@@ -2,8 +2,8 @@
<project> <project>
<meta title="Demo" package="haxework.demo" version="1.0.0" company="MegaLoMania"/> <meta title="Demo" package="haxework.demo" version="1.0.0" company="MegaLoMania"/>
<app main="demo.Demo" path="target" file="demo"/> <app main="demo.Demo" path="target" file="demo"/>
<!--<icon path="/opt/repo/games/tankz/src/client/resources/image/tank/pd-0.png"/>--> <icon path="src/haxe-logo.png"/>
<source path="../src/main"/> <source path="../src/main"/>
<source path="src"/> <source path="src"/>
<assets path="src" rename="image" include="*.png"/> <assets path="src" rename="image" include="*.png"/>
@@ -15,7 +15,7 @@
<haxelib name="yaml" version="1.3.0"/> <haxelib name="yaml" version="1.3.0"/>
<haxeflag name="--macro" value="haxework.parser.Parser.auto()"/> <haxeflag name="--macro" value="haxework.parser.Parser.auto()"/>
<haxeflag name="--macro" value="CompilationOption.set('build','2019-02-28 14:16:02')"/> <haxeflag name="--macro" value="CompilationOption.set('build','xxx')"/>
<window fps="30"/> <window fps="30"/>
<window width="1024" height="768" unless="html5"/> <window width="1024" height="768" unless="html5"/>

View File

@@ -1,5 +1,5 @@
--- ---
skin: $r:skin:background skinId: background
geometry.size.stretch: true geometry.size.stretch: true
views: views:
- $type: haxework.gui.ImageView - $type: haxework.gui.ImageView
@@ -15,25 +15,25 @@ views:
views: views:
- id: list_form - id: list_form
$type: haxework.gui.ToggleButtonView $type: haxework.gui.ToggleButtonView
skin: $r:skin:tab skinId: tab
geometry.padding: [25, 8] geometry.padding: [25, 8]
text: List text: List
+onPress: "$code:switcher.change('list_form')" +onPress: "$code:switcher.change('list_form')"
- id: tail_form - id: tail_form
$type: haxework.gui.ToggleButtonView $type: haxework.gui.ToggleButtonView
skin: $r:skin:tab skinId: tab
geometry.padding: [25, 8] geometry.padding: [25, 8]
text: Tail text: Tail
+onPress: "$code:switcher.change('tail_form')" +onPress: "$code:switcher.change('tail_form')"
- id: data_form - id: data_form
$type: haxework.gui.ToggleButtonView $type: haxework.gui.ToggleButtonView
skin: $r:skin:tab skinId: tab
geometry.padding: [25, 8] geometry.padding: [25, 8]
text: Data text: Data
+onPress: "$code:switcher.change('data_form')" +onPress: "$code:switcher.change('data_form')"
- id: switcher - id: switcher
$type: haxework.gui.frame.FrameSwitcher $type: haxework.gui.frame.FrameSwitcher
skin: $r:skin:panel skinId: panel
animateFactory: { $class: haxework.animate.SlideAnimate } animateFactory: { $class: haxework.animate.SlideAnimate }
+onSwitch: $this:onFrameSwitch +onSwitch: $this:onFrameSwitch
geometry.size.stretch: true geometry.size.stretch: true
@@ -41,15 +41,15 @@ views:
views: views:
- id: list_form - id: list_form
$type: demo.form.ListForm $type: demo.form.ListForm
skin: $r:skin:background skinId: background
geometry.size.stretch: true geometry.size.stretch: true
- id: tail_form - id: tail_form
$type: demo.form.TailForm $type: demo.form.TailForm
skin: $r:skin:background skinId: background
geometry.size.stretch: true geometry.size.stretch: true
- id: data_form - id: data_form
$type: demo.form.DataForm $type: demo.form.DataForm
skin: $r:skin:background skinId: background
geometry.size.stretch: true geometry.size.stretch: true
- $type: haxework.gui.HGroupView - $type: haxework.gui.HGroupView
geometry.size.percent.width: 100 geometry.size.percent.width: 100
@@ -58,7 +58,7 @@ views:
views: views:
- $type: haxework.gui.ButtonView - $type: haxework.gui.ButtonView
geometry.padding: [25, 8] geometry.padding: [25, 8]
skin: $r:skin:button skinId: button
text: Color text: Color
+onPress: "$code:choiceColor()" +onPress: "$code:choiceColor()"
# separator # separator
@@ -66,14 +66,14 @@ views:
geometry.size.stretch: true geometry.size.stretch: true
- $type: haxework.gui.ButtonView - $type: haxework.gui.ButtonView
geometry.padding: [25, 8] geometry.padding: [25, 8]
skin: $r:skin:button skinId: button
text: OK text: OK
- $type: haxework.gui.ButtonView - $type: haxework.gui.ButtonView
geometry.padding: [25, 8] geometry.padding: [25, 8]
skin: $r:skin:button skinId: button
text: Apply text: Apply
- $type: haxework.gui.ButtonView - $type: haxework.gui.ButtonView
geometry.padding: [25, 8] geometry.padding: [25, 8]
skin: $r:skin:button skinId: button
text: Cancel text: Cancel
+onPress: "$code:flash.system.System.exit(0)" +onPress: "$code:flash.system.System.exit(0)"

View File

@@ -1,9 +1,7 @@
package demo; package demo;
import haxework.gui.utils.ColorUtils; import haxework.color.ColorUtil;
import haxework.gui.list.ScrollBarSkin;
import haxework.resources.IResources; import haxework.resources.IResources;
import haxework.gui.skin.ISkin;
import haxework.gui.skin.Skin; import haxework.gui.skin.Skin;
import haxework.gui.skin.TextSkin; import haxework.gui.skin.TextSkin;
@@ -12,39 +10,23 @@ class Theme {
@:provide static var resources:IResources; @:provide static var resources:IResources;
public static function setColor(color:Int, textColor:Int = 0xffffff):Void { public static function setColor(color:Int, textColor:Int = 0xffffff):Void {
var text:ISkin<Dynamic> = new TextSkin(textColor, 16, "Courirer"); var text = new TextSkin(textColor, 16, "Courirer");
var background = Skin.color(0x00000); var background = Skin.color(0x000000);
var button:SkinSet = [ var border = Skin.border(ColorUtil.multiply(color, 1.5), 1, 2);
Skin.buttonColor(color),
text,
];
var tab:SkinSet = [
Skin.tabColor(color),
text,
];
var view:SkinSet = [
Skin.color(color),
Skin.border(textColor),
text,
];
var scroll:SkinSet = [
ScrollBarSkin.vertical(color, ColorUtils.diff(color, 128)),
];
var border = Skin.border(ColorUtils.multiply(color, 1.5), 1, 2);
resources.skin.put("text0", [ resources.skin.put("text0", [
Skin.color(ColorUtils.diff(color, 128)), Skin.color(ColorUtil.diff(color, 128)),
new TextSkin(ColorUtils.diff(color, -128), 16, "Courirer"), new TextSkin(ColorUtil.diff(color, -128), 16, "Courirer"),
]); ]);
resources.skin.put("text1", [ resources.skin.put("text1", [
Skin.color(ColorUtils.diff(color, 64)), Skin.color(ColorUtil.diff(color, 64)),
new TextSkin(ColorUtils.diff(color, -128), 16, "Courirer"), new TextSkin(ColorUtil.diff(color, -128), 16, "Courirer"),
]); ]);
resources.skin.put("text", resources.skin.get("text0")); resources.skin.put("text", resources.skin.get("text0"));
resources.skin.put("background", [background]); resources.skin.put("background", [background]);
resources.skin.put("button", button); resources.skin.put("button", [Skin.buttonColor(color), text]);
resources.skin.put("tab", tab); resources.skin.put("tab", [Skin.tabColor(color), text]);
resources.skin.put("view", view); resources.skin.put("view", [Skin.color(color), border, text]);
resources.skin.put("scroll", scroll); resources.skin.put("scroll", [Skin.scrollVertical(color, ColorUtil.diff(color, 128))]);
resources.skin.put("border", [border]); resources.skin.put("border", [border]);
resources.skin.put("panel", [background, border]); resources.skin.put("panel", [background, border]);
} }

View File

@@ -1,5 +1,4 @@
--- ---
skin: $r:skin:background
views: views:
- $type: haxework.gui.ScrollView - $type: haxework.gui.ScrollView
geometry.size.stretch: true geometry.size.stretch: true
@@ -13,4 +12,4 @@ views:
data: $r:any:data data: $r:any:data
scroll: scroll:
$type: haxework.gui.list.VScrollBarView $type: haxework.gui.list.VScrollBarView
skin: $r:skin:scroll skinId: scroll

View File

@@ -7,7 +7,5 @@ views:
geometry.size.stretch: true geometry.size.stretch: true
scroll: scroll:
$type: haxework.gui.list.VScrollBarView $type: haxework.gui.list.VScrollBarView
skin: $r:skin:scroll skinId: scroll
geometry.size.height: 100%
geometry.size.width: 10
data: $r:any:data50 data: $r:any:data50

View File

@@ -13,4 +13,4 @@ views:
data: $r:any:data data: $r:any:data
scroll: scroll:
$type: haxework.gui.list.VScrollBarView $type: haxework.gui.list.VScrollBarView
skin: $r:skin:scroll skinId: scroll

View File

@@ -0,0 +1,53 @@
package haxework.color;
abstract Color(Int) {
public var alpha(get, never):Int;
public var red(get, never):Int;
public var green(get, never):Int;
public var blue(get, never):Int;
public var zero(get, never):Bool;
public inline function new(value:Int) {
this = value;
}
private inline function get_alpha():Int {
return (this >> 24) & 255;
}
private inline function get_red():Int {
return (this >> 16) & 255;
}
private inline function get_green():Int {
return (this >> 8) & 255;
}
private inline function get_blue():Int {
return this & 255;
}
private inline function get_zero():Bool {
return green == 0 && red == 0 && blue == 0;
}
@:from static public inline function fromArray(value:Array<Int>):Color {
return new Color((value[0] << 24) + (value[1] << 16) + (value[2] << 8) + value[3]);
}
@:from static public inline function fromInt(value:Int):Color {
return new Color(value);
}
@:to public inline function toInt():Int {
return this;
}
@:from static public inline function fromString(value:String):Color {
return new Color(Std.parseInt('0x${value.split('#').pop()}'));
}
@:to public inline function toString():String {
return StringTools.hex(this);
}
}

View File

@@ -0,0 +1,26 @@
package haxework.color;
class ColorUtil {
public static function floor(colorPart:Float):Int {
return Std.int(Math.max(0, Math.min(255, colorPart)));
}
public static function multiply(color:Color, m:Float):Color {
return [
color.alpha,
floor(color.red * m),
floor(color.green * m),
floor(color.blue * m),
];
}
public static function diff(color:Color, d:Int):Color {
return [
color.alpha,
floor(color.red + d),
floor(color.green + d),
floor(color.blue + d),
];
}
}

View File

@@ -1,9 +1,9 @@
package haxework.gui; package haxework.gui;
import haxework.gui.core.Geometry.Position;
import flash.display.DisplayObject; import flash.display.DisplayObject;
import flash.display.Sprite; import flash.display.Sprite;
import flash.events.MouseEvent; import flash.events.MouseEvent;
import haxework.gui.core.Geometry.Position;
import haxework.gui.list.ScrollBarView; import haxework.gui.list.ScrollBarView;
import haxework.gui.skin.Skin; import haxework.gui.skin.Skin;
import haxework.signal.Signal; import haxework.signal.Signal;
@@ -28,7 +28,7 @@ class ScrollView extends HGroupView {
private function onMouseWheelEvent(event:MouseEvent):Void { private function onMouseWheelEvent(event:MouseEvent):Void {
#if flash event.preventDefault(); #end #if flash event.preventDefault(); #end
position -= event.delta / 50; position -= event.delta * scroll.ratio / 3;
} }
private function set_view(value:IView<Dynamic>):IView<Dynamic> { private function set_view(value:IView<Dynamic>):IView<Dynamic> {

View File

@@ -1,54 +0,0 @@
package haxework.gui.list;
import haxework.gui.skin.ISkin;
class HScrollBarSkin implements ISkin<ScrollBarView> {
public var foreColor(default, default):Int;
public var backColor(default, default):Int;
public function new(foreColor:Int = 0, backColor:Int = 0) {
this.foreColor = foreColor;
this.backColor = backColor;
}
public function draw(view:ScrollBarView):Void {
if (view.ratio < 1) {
view.content.graphics.beginFill(backColor);
view.content.graphics.drawRect(0, 0, view.width, view.height);
view.content.graphics.beginFill(foreColor);
view.content.graphics.drawRect(view.width * view.position, 0, view.width * view.ratio, view.height);
view.content.graphics.endFill();
}
}
}
class VScrollBarSkin implements ISkin<ScrollBarView> {
public var foreColor(default, default):Int;
public var backColor(default, default):Int;
public function new(foreColor:Int = 0, backColor:Int = 0) {
this.foreColor = foreColor;
this.backColor = backColor;
}
public function draw(view:ScrollBarView):Void {
if (view.ratio < 1) {
view.content.graphics.beginFill(backColor);
view.content.graphics.drawRect(0, 0, view.width, view.height);
view.content.graphics.beginFill(foreColor);
view.content.graphics.drawRect(0, view.height * view.position, view.width, view.height * view.ratio);
view.content.graphics.endFill();
}
}
}
class ScrollBarSkin {
public static function horizontal(foreColor:Int = 0xffffff, backColor:Int = 0x707070) {
return new HScrollBarSkin(foreColor, backColor);
}
public static function vertical(foreColor:Int = 0xffffff, backColor:Int = 0x707070) {
return new VScrollBarSkin(foreColor, backColor);
}
}

View File

@@ -1,7 +1,7 @@
package haxework.gui.skin; package haxework.gui.skin;
import flash.display.Graphics; import flash.display.Graphics;
import haxework.gui.utils.ColorUtils; import haxework.color.ColorUtil;
import haxework.gui.ButtonView.ButtonState; import haxework.gui.ButtonView.ButtonState;
class ButtonColorSkin implements ISkin<ButtonView> { class ButtonColorSkin implements ISkin<ButtonView> {
@@ -19,9 +19,9 @@ class ButtonColorSkin implements ISkin<ButtonView> {
private function set_color(value:Int):Int { private function set_color(value:Int):Int {
colors = new Map<ButtonState, Int>(); colors = new Map<ButtonState, Int>();
colors.set(ButtonState.UP, value); colors.set(ButtonState.UP, value);
colors.set(ButtonState.DOWN, ColorUtils.diff(value, -32)); colors.set(ButtonState.DOWN, ColorUtil.diff(value, -32));
colors.set(ButtonState.OVER, ColorUtils.diff(value, 32)); colors.set(ButtonState.OVER, ColorUtil.diff(value, 32));
//disable = ColorUtils.multiply(value, 0.6); //disable = ColorUtil.multiply(value, 0.6);
return value; return value;
} }
@@ -29,12 +29,12 @@ class ButtonColorSkin implements ISkin<ButtonView> {
var color:Int = selectColor(view); var color:Int = selectColor(view);
if (Std.is(view, ToggleButtonView)) { if (Std.is(view, ToggleButtonView)) {
if (!cast(view, ToggleButtonView).on) { if (!cast(view, ToggleButtonView).on) {
color = ColorUtils.multiply(color, 0.5); color = ColorUtil.multiply(color, 0.5);
} }
} }
var graphics:Graphics = view.content.graphics; var graphics:Graphics = view.content.graphics;
graphics.beginFill(color, alpha); graphics.beginFill(color, alpha);
graphics.lineStyle(2, ColorUtils.multiply(color, 1.5)); graphics.lineStyle(2, ColorUtil.multiply(color, 1.5));
graphics.drawRoundRect(1, 1, view.width - 2, view.height - 2, 5, 5); graphics.drawRoundRect(1, 1, view.width - 2, view.height - 2, 5, 5);
} }

View File

@@ -0,0 +1,29 @@
package haxework.gui.skin;
import haxework.color.ColorUtil;
import haxework.gui.list.ScrollBarView;
class HScrollBarSkin implements ISkin<ScrollBarView> {
public var foreColor(default, default):Int;
public var backColor(default, default):Int;
public function new(foreColor:Int = 0, backColor:Int = 0) {
this.foreColor = foreColor;
this.backColor = backColor;
}
public function draw(view:ScrollBarView):Void {
if (view.ratio < 1) {
var graphics = view.content.graphics;
var size = Math.max(view.width * view.ratio, 15);
var position = Math.min(view.width * view.position, view.width - size);
graphics.beginFill(backColor);
graphics.lineStyle(2, ColorUtil.multiply(foreColor, 1.5));
graphics.drawRect(0, 0, view.width, view.height);
graphics.beginFill(foreColor);
graphics.lineStyle();
graphics.drawRect(position, 0, size, view.height);
graphics.endFill();
}
}
}

View File

@@ -4,19 +4,19 @@ import flash.display.BitmapData;
class Skin { class Skin {
public static function size(width:Float, height:Float): ISkin<Dynamic> { public static function size(width:Float, height:Float):ISkin<Dynamic> {
return new SizeSkin(width, height); return new SizeSkin(width, height);
} }
public static function bitmap(image:BitmapData): ISkin<SpriteView> { public static function bitmap(image:BitmapData):ISkin<SpriteView> {
return new BitmapSkin(image); return new BitmapSkin(image);
} }
public static function color(color: Int, alpha: Float = 1.0): ISkin<SpriteView> { public static function color(color:Int, alpha:Float = 1.0):ISkin<SpriteView> {
return new ColorSkin(color, alpha); return new ColorSkin(color, alpha);
} }
public static function border(color: Int, alpha: Float = 1.0, tickness: Float = 1.0): ISkin<SpriteView> { public static function border(color:Int, alpha:Float = 1.0, tickness:Float = 1.0):ISkin<SpriteView> {
return new BorderSkin(color, alpha, tickness); return new BorderSkin(color, alpha, tickness);
} }
@@ -24,11 +24,23 @@ class Skin {
return new TextSkin(fontColor, fontSize, fontFamily); return new TextSkin(fontColor, fontSize, fontFamily);
} }
public static function buttonColor(color: Int, alpha: Float = 1.0): ISkin<ButtonView> { public static function buttonColor(color:Int, alpha:Float = 1.0):ISkin<ButtonView> {
return new ButtonColorSkin(color, alpha); return new ButtonColorSkin(color, alpha);
} }
public static function tabColor(color: Int, alpha: Float = 1.0): ISkin<ButtonView> { public static function buttonBitmap(image:BitmapData):ISkin<ButtonView> {
return new ButtonBitmapSkin(image);
}
public static function tabColor(color:Int, alpha:Float = 1.0):ISkin<ButtonView> {
return new TabColorSkin(color, alpha); return new TabColorSkin(color, alpha);
} }
public static function scrollHorizontal(foreColor:Int, backColor:Int) {
return new HScrollBarSkin(foreColor, backColor);
}
public static function scrollVertical(foreColor:Int, backColor:Int) {
return new VScrollBarSkin(foreColor, backColor);
}
} }

View File

@@ -1,7 +1,7 @@
package haxework.gui.skin; package haxework.gui.skin;
import flash.display.Graphics; import flash.display.Graphics;
import haxework.gui.utils.ColorUtils; import haxework.color.ColorUtil;
class TabColorSkin extends ButtonColorSkin { class TabColorSkin extends ButtonColorSkin {
@@ -9,12 +9,12 @@ class TabColorSkin extends ButtonColorSkin {
var color:Int = selectColor(view); var color:Int = selectColor(view);
if (Std.is(view, ToggleButtonView)) { if (Std.is(view, ToggleButtonView)) {
if (!cast(view, ToggleButtonView).on) { if (!cast(view, ToggleButtonView).on) {
color = ColorUtils.multiply(color, 0.5); color = ColorUtil.multiply(color, 0.5);
} }
} }
var graphics:Graphics = view.content.graphics; var graphics:Graphics = view.content.graphics;
graphics.beginFill(color, alpha); graphics.beginFill(color, alpha);
graphics.lineStyle(2, ColorUtils.multiply(color, 1.5)); graphics.lineStyle(2, ColorUtil.multiply(color, 1.5));
graphics.drawRoundRectComplex(1, 1, view.width - 2, view.height - 2, 5, 5, 0, 0); graphics.drawRoundRectComplex(1, 1, view.width - 2, view.height - 2, 5, 5, 0, 0);
} }
} }

View File

@@ -0,0 +1,29 @@
package haxework.gui.skin;
import haxework.color.ColorUtil;
import haxework.gui.list.ScrollBarView;
class VScrollBarSkin implements ISkin<ScrollBarView> {
public var foreColor(default, default):Int;
public var backColor(default, default):Int;
public function new(foreColor:Int = 0, backColor:Int = 0) {
this.foreColor = foreColor;
this.backColor = backColor;
}
public function draw(view:ScrollBarView):Void {
if (view.ratio < 1) {
var graphics = view.content.graphics;
var size = Math.max(view.height * view.ratio, 15);
var position = Math.min(view.height * view.position, view.height - size);
graphics.beginFill(backColor);
graphics.lineStyle(2, ColorUtil.multiply(foreColor, 1.5));
graphics.drawRect(0, 0, view.width, view.height);
graphics.beginFill(foreColor);
graphics.lineStyle();
graphics.drawRect(0, position, view.width, size);
graphics.endFill();
}
}
}

View File

@@ -1,31 +0,0 @@
package haxework.gui.utils;
class ColorUtils {
public static function multiply(color:Int, m:Float):Int {
var rgb:Array<Int> = color2rgb(color);
var red:Int = cast Math.min(255, Math.round(rgb[0] * m));
var green:Int = cast Math.min(255, Math.round(rgb[1] * m));
var blue:Int = cast Math.min(255, Math.round(rgb[2] * m));
return rgb2color(red, green, blue);
}
public static function diff(color:Int, d:Int):Int {
var rgb:Array<Int> = color2rgb(color);
var red:Int = cast Math.max(0, Math.min(255, rgb[0] + d));
var green:Int = cast Math.max(0, Math.min(255, rgb[1] + d));
var blue:Int = cast Math.max(0, Math.min(255, rgb[2] + d));
return rgb2color(red, green, blue);
}
public static function rgb2color(red:Int, green:Int, blue:Int):Int {
return (red << 16) + (green << 8) + blue;
}
public static function color2rgb(color:Int):Array<Int> {
var red:Int = ((color & 0xFF0000) >>> 16);
var green:Int = ((color & 0x00FF00) >> 8);
var blue:Int = (color & 0x0000FF);
return [red, green, blue];
}
}