custom drawable attributes in theme; scroll bar theming

This commit is contained in:
Vadim Lopatin 2014-03-24 22:11:01 +04:00
parent 502ea57e42
commit 1dfb88defd
6 changed files with 92 additions and 9 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

View File

@ -91,7 +91,7 @@ struct Glyph
class RefCountedObject { class RefCountedObject {
protected int _refCount; protected int _refCount;
@property int refCount() { return _refCount; } @property int refCount() const { return _refCount; }
void addRef() { void addRef() {
_refCount++; _refCount++;
} }
@ -105,8 +105,8 @@ class RefCountedObject {
struct Ref(T) { // if (T is RefCountedObject) struct Ref(T) { // if (T is RefCountedObject)
private T _data; private T _data;
alias get this; alias get this;
@property bool isNull() { return _data is null; } @property bool isNull() const { return _data is null; }
@property int refCount() { return _data !is null ? _data.refCount : 0; } @property int refCount() const { return _data !is null ? _data.refCount : 0; }
this(T data) { this(T data) {
_data = data; _data = data;
if (_data !is null) if (_data !is null)

View File

@ -683,6 +683,13 @@ class Drawable : RefCountedObject {
@property Rect padding() { return Rect(0,0,0,0); } @property Rect padding() { return Rect(0,0,0,0); }
} }
class EmptyDrawable : Drawable {
override void drawTo(DrawBuf buf, Rect rc, int tilex0 = 0, int tiley0 = 0) {
}
@property override int width() { return 0; }
@property override int height() { return 0; }
}
class SolidFillDrawable : Drawable { class SolidFillDrawable : Drawable {
protected uint _color; protected uint _color;
this(uint color) { this(uint color) {

View File

@ -283,9 +283,9 @@ class ScrollBar : WidgetGroup, OnClickHandler {
@property ScrollBar orientation(Orientation value) { @property ScrollBar orientation(Orientation value) {
if (_orientation != value) { if (_orientation != value) {
_orientation = value; _orientation = value;
_btnBack.drawableId = _orientation == Orientation.Vertical ? "scrollbar_btn_up" : "scrollbar_btn_left"; _btnBack.drawableId = style.customDrawableId(_orientation == Orientation.Vertical ? ATTR_SCROLLBAR_BUTTON_UP : ATTR_SCROLLBAR_BUTTON_LEFT);
_btnForward.drawableId = _orientation == Orientation.Vertical ? "scrollbar_btn_down" : "scrollbar_btn_right"; _btnForward.drawableId = style.customDrawableId(_orientation == Orientation.Vertical ? ATTR_SCROLLBAR_BUTTON_DOWN : ATTR_SCROLLBAR_BUTTON_RIGHT);
_indicator.drawableId = _orientation == Orientation.Vertical ? "scrollbar_indicator_vertical" : "scrollbar_indicator_horizontal"; _indicator.drawableId = style.customDrawableId(_orientation == Orientation.Vertical ? ATTR_SCROLLBAR_INDICATOR_VERTICAL : ATTR_SCROLLBAR_INDICATOR_HORIZONTAL);
requestLayout(); requestLayout();
} }
return this; return this;
@ -295,9 +295,9 @@ class ScrollBar : WidgetGroup, OnClickHandler {
super(ID); super(ID);
styleId = "BUTTON"; styleId = "BUTTON";
_orientation = orient; _orientation = orient;
_btnBack = new ImageButton("BACK", _orientation == Orientation.Vertical ? "scrollbar_btn_up" : "scrollbar_btn_left"); _btnBack = new ImageButton("BACK", style.customDrawableId(_orientation == Orientation.Vertical ? ATTR_SCROLLBAR_BUTTON_UP : ATTR_SCROLLBAR_BUTTON_LEFT));
_btnForward = new ImageButton("FORWARD", _orientation == Orientation.Vertical ? "scrollbar_btn_down" : "scrollbar_btn_right"); _btnForward = new ImageButton("FORWARD", style.customDrawableId(_orientation == Orientation.Vertical ? ATTR_SCROLLBAR_BUTTON_DOWN : ATTR_SCROLLBAR_BUTTON_RIGHT));
_indicator = new IndicatorButton(_orientation == Orientation.Vertical ? "scrollbar_indicator_vertical" : "scrollbar_indicator_horizontal"); _indicator = new IndicatorButton(style.customDrawableId(_orientation == Orientation.Vertical ? ATTR_SCROLLBAR_INDICATOR_VERTICAL : ATTR_SCROLLBAR_INDICATOR_HORIZONTAL));
addChild(_btnBack); addChild(_btnBack);
addChild(_btnForward); addChild(_btnForward);
addChild(_indicator); addChild(_indicator);

View File

@ -39,6 +39,31 @@ enum Align : ubyte {
TopLeft = Left | Top, TopLeft = Left | Top,
} }
class DrawableAttribute {
protected string _id;
protected string _drawableId;
protected DrawableRef _drawable;
protected bool _initialized;
this(string id, string drawableId) {
_id = id;
_drawableId = drawableId;
}
@property string id() const { return _id; }
@property string drawableId() const { return _drawableId; }
@property void drawableId(string newDrawable) { _drawableId = newDrawable; clear(); }
@property ref DrawableRef drawable() const {
if (!_drawable.isNull)
return (cast(DrawableAttribute)this)._drawable;
(cast(DrawableAttribute)this)._drawable = drawableCache.get(_id);
(cast(DrawableAttribute)this)._initialized = true;
return (cast(DrawableAttribute)this)._drawable;
}
void clear() {
_drawable.clear();
_initialized = false;
}
}
/// style properties /// style properties
class Style { class Style {
protected string _id; protected string _id;
@ -69,6 +94,8 @@ class Style {
protected Style[] _substates; protected Style[] _substates;
protected Style[] _children; protected Style[] _children;
protected DrawableAttribute[string] _customDrawables;
protected FontRef _font; protected FontRef _font;
protected DrawableRef _backgroundDrawable; protected DrawableRef _backgroundDrawable;
@ -115,6 +142,30 @@ class Style {
return (cast(Style)this)._backgroundDrawable; return (cast(Style)this)._backgroundDrawable;
} }
/// get custom drawable attribute
@property ref DrawableRef customDrawable(string id) {
if (id in _customDrawables)
return _customDrawables[id].drawable;
return parentStyle.customDrawable(id);
}
/// get custom drawable attribute
@property string customDrawableId(string id) const {
if (id in _customDrawables)
return _customDrawables[id].drawableId;
return parentStyle.customDrawableId(id);
}
/// sets custom drawable attribute for style
Style setCustomDrawable(string id, string resourceId) {
if (id in _customDrawables)
_customDrawables[id].drawableId = resourceId;
else
_customDrawables[id] = new DrawableAttribute(id, resourceId);
return this;
}
//=================================================== //===================================================
// font properties // font properties
@ -510,6 +561,19 @@ class Theme : Style {
return _maxHeight; return _maxHeight;
} }
private DrawableRef _emptyDrawable;
@property override ref DrawableRef customDrawable(string id) const {
if (id in _customDrawables)
return _customDrawables[id].drawable;
return (cast(Theme)this)._emptyDrawable;
}
@property override string customDrawableId(string id) const {
if (id in _customDrawables)
return _customDrawables[id].drawableId;
return null;
}
/// create new named style /// create new named style
override Style createSubstyle(string id) { override Style createSubstyle(string id) {
Style style = new Style(this, id); Style style = new Style(this, id);
@ -541,6 +605,12 @@ private __gshared Theme _currentTheme;
_currentTheme = theme; _currentTheme = theme;
} }
immutable ATTR_SCROLLBAR_BUTTON_UP = "scrollbar_button_up";
immutable ATTR_SCROLLBAR_BUTTON_DOWN = "scrollbar_button_down";
immutable ATTR_SCROLLBAR_BUTTON_LEFT = "scrollbar_button_left";
immutable ATTR_SCROLLBAR_BUTTON_RIGHT = "scrollbar_button_right";
immutable ATTR_SCROLLBAR_INDICATOR_VERTICAL = "scrollbar_indicator_vertical";
immutable ATTR_SCROLLBAR_INDICATOR_HORIZONTAL = "scrollbar_indicator_horizontal";
Theme createDefaultTheme() { Theme createDefaultTheme() {
Log.d("Creating default theme"); Log.d("Creating default theme");
@ -551,6 +621,12 @@ Theme createDefaultTheme() {
button.createState(State.Disabled, State.Disabled).backgroundImageId("btn_default_small_normal_disable"); button.createState(State.Disabled, State.Disabled).backgroundImageId("btn_default_small_normal_disable");
button.createState(State.Pressed, State.Pressed).backgroundImageId("btn_default_small_pressed"); button.createState(State.Pressed, State.Pressed).backgroundImageId("btn_default_small_pressed");
button.createState(State.Focused, State.Focused).backgroundImageId("btn_default_small_selected"); button.createState(State.Focused, State.Focused).backgroundImageId("btn_default_small_selected");
res.setCustomDrawable(ATTR_SCROLLBAR_BUTTON_UP, "scrollbar_btn_up");
res.setCustomDrawable(ATTR_SCROLLBAR_BUTTON_DOWN, "scrollbar_btn_down");
res.setCustomDrawable(ATTR_SCROLLBAR_BUTTON_LEFT, "scrollbar_btn_left");
res.setCustomDrawable(ATTR_SCROLLBAR_BUTTON_RIGHT, "scrollbar_btn_right");
res.setCustomDrawable(ATTR_SCROLLBAR_INDICATOR_VERTICAL, "scrollbar_indicator_vertical");
res.setCustomDrawable(ATTR_SCROLLBAR_INDICATOR_HORIZONTAL, "scrollbar_indicator_horizontal");
res.dumpStats(); res.dumpStats();
return res; return res;
} }