diff --git a/dlanguilib.visualdproj b/dlanguilib.visualdproj index b4492ab3..657bf967 100644 --- a/dlanguilib.visualdproj +++ b/dlanguilib.visualdproj @@ -271,6 +271,7 @@ </Folder> </Folder> <Folder name="widgets"> + <File path="src\dlangui\widgets\controls.d" /> <File path="src\dlangui\widgets\styles.d" /> <File path="src\dlangui\widgets\widget.d" /> </Folder> diff --git a/examples/example1/winmain.d b/examples/example1/winmain.d index 69a85ede..e356e5f9 100644 --- a/examples/example1/winmain.d +++ b/examples/example1/winmain.d @@ -3,6 +3,7 @@ module winmain; import dlangui.platforms.common.platform; import dlangui.graphics.images; import dlangui.widgets.widget; +import dlangui.widgets.controls; import dlangui.core.logger; import dlangui.graphics.fonts; import std.stdio; @@ -24,43 +25,17 @@ version(Windows) { } -class TestWidget : Widget { - public override void onDraw(DrawBuf buf) { - super.onDraw(buf); - FontRef font1; - FontRef font2; - Log.d("Testing opAssign"); - font1 = font2; - Log.d("Testing copy constructor"); - FontRef font3 = font2; - Log.d("On draw: getting font"); - FontRef font = FontManager.instance.getFont(32, 400, false, FontFamily.SansSerif, "Arial"); - Log.d("Got font, drawing text"); - font.drawText(buf, _pos.left + 5, _pos.top + 5, "Text"d, 0x0000FF); - Log.d("Text is drawn successfully"); - DrawBufRef img = imageCache.get(resourceDir ~ "exit.png"); - if (!img.isNull) { - Log.d("loaded image ", img.width, "x", img.height); - buf.drawImage(200, 200, img); - buf.drawImage(250, 250, img); - } - } -} - extern (C) int UIAppMain(string[] args) { imageCache = new ImageCache(); resourceDir = exePath() ~ "..\\res\\"; - Log.d("Some debug message"); - Log.e("Sample error #", 22); - string[] imageDirs = [ resourceDir ]; drawableCache.resourcePaths = imageDirs; Window window = Platform.instance().createWindow("My Window", null); - Widget myWidget = (new TextWidget()).textColor(0x40FF4000); + Widget myWidget = (new Button()).textColor(0x40FF4000); myWidget.text = "Some strange text string. 1234567890"; myWidget.alignment = Align.Center; window.mainWidget = myWidget; diff --git a/src/dlangui/widgets/controls.d b/src/dlangui/widgets/controls.d new file mode 100644 index 00000000..29c51f30 --- /dev/null +++ b/src/dlangui/widgets/controls.d @@ -0,0 +1,31 @@ +module dlangui.widgets.controls; + +import dlangui.widgets.widget; + +class Button : Widget { + protected dstring _text; + override @property dstring text() { return _text; } + override @property void text(dstring s) { _text = s; } + this() { + styleId = "BUTTON"; + } + override void measure(int width, int height) { + _measuredWidth = _measuredHeight = 0; + } + override void layout(Rect rc) { + _pos = rc; + _needLayout = false; + } + override void onDraw(DrawBuf buf) { + super.onDraw(buf); + Rect rc = _pos; + applyMargins(rc); + buf.fillRect(_pos, backgroundColor); + applyPadding(rc); + ClipRectSaver(buf, rc); + FontRef font = font(); + Point sz = font.textSize(text); + applyAlign(rc, sz); + font.drawText(buf, rc.left, rc.top, text, textColor); + } +} \ No newline at end of file diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index c54e22e5..36f3097a 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -2,6 +2,8 @@ module dlangui.widgets.styles; import dlangui.core.types; import dlangui.graphics.fonts; +import dlangui.graphics.drawbuf; +import dlangui.graphics.images; immutable ubyte ALIGN_UNSPECIFIED = 0; immutable uint COLOR_UNSPECIFIED = 0xFFDEADFF; @@ -33,13 +35,14 @@ class Style { protected ubyte _stateMask; protected ubyte _stateValue; protected ubyte _align = Align.TopLeft; - protected uint _backgroundColor = COLOR_TRANSPARENT; - protected uint _textColor = COLOR_UNSPECIFIED; + protected ubyte _fontStyle = FONT_STYLE_UNSPECIFIED; + protected FontFamily _fontFamily = FontFamily.Unspecified; protected ushort _fontSize = FONT_SIZE_UNSPECIFIED; protected ushort _fontWeight = FONT_WEIGHT_UNSPECIFIED; - protected ubyte _fontStyle = FONT_STYLE_UNSPECIFIED; + protected uint _backgroundColor = COLOR_TRANSPARENT; + protected uint _textColor = COLOR_UNSPECIFIED; protected string _fontFace; - protected FontFamily _fontFamily = FontFamily.Unspecified; + protected string _backgroundImageId; protected Rect _padding; protected Rect _margins; @@ -47,6 +50,7 @@ class Style { protected Style[] _children; protected FontRef _font; + protected DrawableRef _backgroundDrawable; @property const(Theme) theme() const { if (_theme !is null) @@ -78,6 +82,19 @@ class Style { return currentTheme; } + @property ref DrawableRef backgroundDrawable() const { + if (!(cast(Style)this)._backgroundDrawable.isNull) + return (cast(Style)this)._backgroundDrawable; + string image = backgroundImageId; + if (image !is null) { + (cast(Style)this)._backgroundDrawable = drawableCache.get(image); + } else { + uint color = backgroundColor; + (cast(Style)this)._backgroundDrawable = new SolidFillDrawable(color); + } + return (cast(Style)this)._backgroundDrawable; + } + @property ref FontRef font() const { if (!(cast(Style)this)._font.isNull) return (cast(Style)this)._font; @@ -160,6 +177,14 @@ class Style { return parentStyle.backgroundColor; } + /// font size + @property string backgroundImageId() const { + if (_backgroundImageId !is null) + return _backgroundImageId; + else + return parentStyle.backgroundImageId; + } + /// get full alignment (both vertical and horizontal) @property ubyte alignment() const { if (_align != Align.Unspecified) @@ -215,6 +240,14 @@ class Style { @property Style backgroundColor(uint color) { _backgroundColor = color; + _backgroundImageId = null; + _backgroundDrawable.clear(); + return this; + } + + @property Style backgroundImageId(string image) { + _backgroundImageId = image; + _backgroundDrawable.clear(); return this; } @@ -313,4 +346,5 @@ private __gshared Theme _currentTheme; static this() { _currentTheme = new Theme("default"); + Style button = _currentTheme.createSubstyle("BUTTON").backgroundImageId("btn_default_normal").alignment(Align.Center); } diff --git a/src/dlangui/widgets/widget.d b/src/dlangui/widgets/widget.d index ae761afb..25330215 100644 --- a/src/dlangui/widgets/widget.d +++ b/src/dlangui/widgets/widget.d @@ -149,7 +149,10 @@ class Widget { Rect rc = _pos; applyMargins(rc); buf.fillRect(_pos, backgroundColor); + DrawableRef bg = style.backgroundDrawable; + bg.drawTo(buf, rc); applyPadding(rc); + /* buf.fillRect(Rect(rc.left + rc.width / 2, rc.top, rc.left + rc.width / 2 + 2, rc.bottom), 0xFF8000); buf.fillRect(Rect(rc.left, rc.top + rc.height / 2, rc.right, rc.top + rc.height / 2 + 2), 0xFF80FF); DrawableRef img = drawableCache.get("exit"); @@ -161,6 +164,7 @@ class Widget { img.drawTo(buf, Rect(250, 250, 250+60, 250+150)); img.drawTo(buf, Rect(50, 50, 50+30, 50+30)); } + */ _needDraw = false; } /// Applies alignment for content of size sz - set rectangle rc to aligned value of content inside of initial value of rc.