mirror of https://github.com/buggins/dlangui.git
styles: background drawable - working; add initial implementation of Button
This commit is contained in:
parent
bfc77c0974
commit
c6c89303d8
|
@ -271,6 +271,7 @@
|
||||||
</Folder>
|
</Folder>
|
||||||
</Folder>
|
</Folder>
|
||||||
<Folder name="widgets">
|
<Folder name="widgets">
|
||||||
|
<File path="src\dlangui\widgets\controls.d" />
|
||||||
<File path="src\dlangui\widgets\styles.d" />
|
<File path="src\dlangui\widgets\styles.d" />
|
||||||
<File path="src\dlangui\widgets\widget.d" />
|
<File path="src\dlangui\widgets\widget.d" />
|
||||||
</Folder>
|
</Folder>
|
||||||
|
|
|
@ -3,6 +3,7 @@ module winmain;
|
||||||
import dlangui.platforms.common.platform;
|
import dlangui.platforms.common.platform;
|
||||||
import dlangui.graphics.images;
|
import dlangui.graphics.images;
|
||||||
import dlangui.widgets.widget;
|
import dlangui.widgets.widget;
|
||||||
|
import dlangui.widgets.controls;
|
||||||
import dlangui.core.logger;
|
import dlangui.core.logger;
|
||||||
import dlangui.graphics.fonts;
|
import dlangui.graphics.fonts;
|
||||||
import std.stdio;
|
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) {
|
extern (C) int UIAppMain(string[] args) {
|
||||||
|
|
||||||
imageCache = new ImageCache();
|
imageCache = new ImageCache();
|
||||||
resourceDir = exePath() ~ "..\\res\\";
|
resourceDir = exePath() ~ "..\\res\\";
|
||||||
|
|
||||||
Log.d("Some debug message");
|
|
||||||
Log.e("Sample error #", 22);
|
|
||||||
|
|
||||||
string[] imageDirs = [
|
string[] imageDirs = [
|
||||||
resourceDir
|
resourceDir
|
||||||
];
|
];
|
||||||
drawableCache.resourcePaths = imageDirs;
|
drawableCache.resourcePaths = imageDirs;
|
||||||
Window window = Platform.instance().createWindow("My Window", null);
|
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.text = "Some strange text string. 1234567890";
|
||||||
myWidget.alignment = Align.Center;
|
myWidget.alignment = Align.Center;
|
||||||
window.mainWidget = myWidget;
|
window.mainWidget = myWidget;
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,8 @@ module dlangui.widgets.styles;
|
||||||
|
|
||||||
import dlangui.core.types;
|
import dlangui.core.types;
|
||||||
import dlangui.graphics.fonts;
|
import dlangui.graphics.fonts;
|
||||||
|
import dlangui.graphics.drawbuf;
|
||||||
|
import dlangui.graphics.images;
|
||||||
|
|
||||||
immutable ubyte ALIGN_UNSPECIFIED = 0;
|
immutable ubyte ALIGN_UNSPECIFIED = 0;
|
||||||
immutable uint COLOR_UNSPECIFIED = 0xFFDEADFF;
|
immutable uint COLOR_UNSPECIFIED = 0xFFDEADFF;
|
||||||
|
@ -33,13 +35,14 @@ class Style {
|
||||||
protected ubyte _stateMask;
|
protected ubyte _stateMask;
|
||||||
protected ubyte _stateValue;
|
protected ubyte _stateValue;
|
||||||
protected ubyte _align = Align.TopLeft;
|
protected ubyte _align = Align.TopLeft;
|
||||||
protected uint _backgroundColor = COLOR_TRANSPARENT;
|
protected ubyte _fontStyle = FONT_STYLE_UNSPECIFIED;
|
||||||
protected uint _textColor = COLOR_UNSPECIFIED;
|
protected FontFamily _fontFamily = FontFamily.Unspecified;
|
||||||
protected ushort _fontSize = FONT_SIZE_UNSPECIFIED;
|
protected ushort _fontSize = FONT_SIZE_UNSPECIFIED;
|
||||||
protected ushort _fontWeight = FONT_WEIGHT_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 string _fontFace;
|
||||||
protected FontFamily _fontFamily = FontFamily.Unspecified;
|
protected string _backgroundImageId;
|
||||||
protected Rect _padding;
|
protected Rect _padding;
|
||||||
protected Rect _margins;
|
protected Rect _margins;
|
||||||
|
|
||||||
|
@ -47,6 +50,7 @@ class Style {
|
||||||
protected Style[] _children;
|
protected Style[] _children;
|
||||||
|
|
||||||
protected FontRef _font;
|
protected FontRef _font;
|
||||||
|
protected DrawableRef _backgroundDrawable;
|
||||||
|
|
||||||
@property const(Theme) theme() const {
|
@property const(Theme) theme() const {
|
||||||
if (_theme !is null)
|
if (_theme !is null)
|
||||||
|
@ -78,6 +82,19 @@ class Style {
|
||||||
return currentTheme;
|
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 {
|
@property ref FontRef font() const {
|
||||||
if (!(cast(Style)this)._font.isNull)
|
if (!(cast(Style)this)._font.isNull)
|
||||||
return (cast(Style)this)._font;
|
return (cast(Style)this)._font;
|
||||||
|
@ -160,6 +177,14 @@ class Style {
|
||||||
return parentStyle.backgroundColor;
|
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)
|
/// get full alignment (both vertical and horizontal)
|
||||||
@property ubyte alignment() const {
|
@property ubyte alignment() const {
|
||||||
if (_align != Align.Unspecified)
|
if (_align != Align.Unspecified)
|
||||||
|
@ -215,6 +240,14 @@ class Style {
|
||||||
|
|
||||||
@property Style backgroundColor(uint color) {
|
@property Style backgroundColor(uint color) {
|
||||||
_backgroundColor = color;
|
_backgroundColor = color;
|
||||||
|
_backgroundImageId = null;
|
||||||
|
_backgroundDrawable.clear();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property Style backgroundImageId(string image) {
|
||||||
|
_backgroundImageId = image;
|
||||||
|
_backgroundDrawable.clear();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,4 +346,5 @@ private __gshared Theme _currentTheme;
|
||||||
|
|
||||||
static this() {
|
static this() {
|
||||||
_currentTheme = new Theme("default");
|
_currentTheme = new Theme("default");
|
||||||
|
Style button = _currentTheme.createSubstyle("BUTTON").backgroundImageId("btn_default_normal").alignment(Align.Center);
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,7 +149,10 @@ class Widget {
|
||||||
Rect rc = _pos;
|
Rect rc = _pos;
|
||||||
applyMargins(rc);
|
applyMargins(rc);
|
||||||
buf.fillRect(_pos, backgroundColor);
|
buf.fillRect(_pos, backgroundColor);
|
||||||
|
DrawableRef bg = style.backgroundDrawable;
|
||||||
|
bg.drawTo(buf, rc);
|
||||||
applyPadding(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.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);
|
buf.fillRect(Rect(rc.left, rc.top + rc.height / 2, rc.right, rc.top + rc.height / 2 + 2), 0xFF80FF);
|
||||||
DrawableRef img = drawableCache.get("exit");
|
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(250, 250, 250+60, 250+150));
|
||||||
img.drawTo(buf, Rect(50, 50, 50+30, 50+30));
|
img.drawTo(buf, Rect(50, 50, 50+30, 50+30));
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
_needDraw = false;
|
_needDraw = false;
|
||||||
}
|
}
|
||||||
/// Applies alignment for content of size sz - set rectangle rc to aligned value of content inside of initial value of rc.
|
/// Applies alignment for content of size sz - set rectangle rc to aligned value of content inside of initial value of rc.
|
||||||
|
|
Loading…
Reference in New Issue