From eb99980d5ed0f4262a4dede2b0a956d23b5e0be6 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 25 Jan 2016 17:30:27 +0300 Subject: [PATCH] fix style with substyles modification - close #150 --- examples/d3d/src/d3d.d | 152 +++++++++++++++++++---------------- src/dlangui/widgets/styles.d | 42 ++++++++++ 2 files changed, 124 insertions(+), 70 deletions(-) diff --git a/examples/d3d/src/d3d.d b/examples/d3d/src/d3d.d index 6f2297ec..9b650f76 100644 --- a/examples/d3d/src/d3d.d +++ b/examples/d3d/src/d3d.d @@ -13,77 +13,89 @@ extern (C) int UIAppMain(string[] args) { // create window Window window = Platform.instance.createWindow("DlangUI example - 3D Application", null, WindowFlag.Resizable, 600, 500); - // create some widget to show in window - //window.mainWidget = (new Button()).text("Hello, world!"d).margins(Rect(20,20,20,20)); - window.mainWidget = parseML(q{ - VerticalLayout { - margins: 10 - padding: 10 - backgroundColor: "#C0E0E070" // semitransparent yellow background - // red bold text with size = 150% of base style size and font face Arial - TextWidget { text: "Hello World example for DlangUI"; textColor: "red"; fontSize: 150%; fontWeight: 800; fontFace: "Arial" } - HorizontalLayout { - layoutWidth: fill - TextWidget { text: "Text 20%"; backgroundColor:"#80FF0000"; layoutWidth: 20% } - VerticalLayout { - layoutWidth: 30% - TextWidget { text: "Text 30%"; backgroundColor:"#80FF00FF" } - TextWidget { text: "Text 30%"; backgroundColor:"#8000FFFF" } - TextWidget { text: "Text 30%"; backgroundColor:"#8000FFFF" } - } - TextWidget { text: "Text 50%"; backgroundColor:"#80FFFF00"; layoutWidth: 50% } - } - // arrange controls as form - table with two columns - TableLayout { - colCount: 2 - TextWidget { text: "param 1" } - EditLine { id: edit1; text: "some text" } - TextWidget { text: "param 2" } - EditLine { id: edit2; text: "some text for param2" } - TextWidget { text: "some radio buttons" } - // arrange some radio buttons vertically - VerticalLayout { - RadioButton { id: rb1; text: "Item 1" } - RadioButton { id: rb2; text: "Item 2" } - RadioButton { id: rb3; text: "Item 3" } - } - TextWidget { text: "and checkboxes" } - // arrange some checkboxes horizontally - HorizontalLayout { - CheckBox { id: cb1; text: "checkbox 1" } - CheckBox { id: cb2; text: "checkbox 2" } - } - } - HorizontalLayout { - Button { id: btnOk; text: "Ok" } - Button { id: btnCancel; text: "Cancel" } - } - CanvasWidget { - id: canvas - minWidth: 500 - minHeight: 300 - } - } - }); + static if (true) { + VerticalLayout layout = new VerticalLayout(); + Button btn = new Button(null, "Button 1"d); + btn.fontSize = 32; + Button btn2 = new Button(null, "Button 2"d); + btn2.fontSize = 32; + layout.addChild(btn); + layout.addChild(btn2); + window.mainWidget = layout; + } else { - auto canvas = window.mainWidget.childById!CanvasWidget("canvas"); - canvas.onDrawListener = delegate(CanvasWidget canvas, DrawBuf buf, Rect rc) { - Log.w("canvas.onDrawListener clipRect=" ~ to!string(buf.clipRect)); - buf.fill(0xFFFFFF); - int x = rc.left; - int y = rc.top; - buf.fillRect(Rect(x+20, y+20, x+150, y+200), 0x80FF80); - buf.fillRect(Rect(x+90, y+80, x+250, y+250), 0x80FF80FF); - canvas.font.drawText(buf, x + 40, y + 50, "fillRect()"d, 0xC080C0); - buf.drawFrame(Rect(x + 400, y + 30, x + 550, y + 150), 0x204060, Rect(2,3,4,5), 0x80704020); - canvas.font.drawText(buf, x + 400, y + 5, "drawFrame()"d, 0x208020); - canvas.font.drawText(buf, x + 300, y + 100, "drawPixel()"d, 0x000080); - for (int i = 0; i < 80; i++) - buf.drawPixel(x+300 + i * 4, y+140 + i * 3 % 100, 0xFF0000 + i * 2); - canvas.font.drawText(buf, x + 200, y + 150, "drawLine()"d, 0x800020); - for (int i = 0; i < 40; i+=3) - buf.drawLine(Point(x+200 + i * 4, y+190), Point(x+150 + i * 7, y+320 + i * 2), 0x008000 + i * 5); - }; + // create some widget to show in window + //window.mainWidget = (new Button()).text("Hello, world!"d).margins(Rect(20,20,20,20)); + window.mainWidget = parseML(q{ + VerticalLayout { + margins: 10 + padding: 10 + backgroundColor: "#C0E0E070" // semitransparent yellow background + // red bold text with size = 150% of base style size and font face Arial + TextWidget { text: "Hello World example for DlangUI"; textColor: "red"; fontSize: 150%; fontWeight: 800; fontFace: "Arial" } + HorizontalLayout { + layoutWidth: fill + TextWidget { text: "Text 20%"; backgroundColor:"#80FF0000"; layoutWidth: 20% } + VerticalLayout { + layoutWidth: 30% + TextWidget { text: "Text 30%"; backgroundColor:"#80FF00FF" } + TextWidget { text: "Text 30%"; backgroundColor:"#8000FFFF" } + TextWidget { text: "Text 30%"; backgroundColor:"#8000FFFF" } + } + TextWidget { text: "Text 50%"; backgroundColor:"#80FFFF00"; layoutWidth: 50% } + } + // arrange controls as form - table with two columns + TableLayout { + colCount: 2 + TextWidget { text: "param 1" } + EditLine { id: edit1; text: "some text" } + TextWidget { text: "param 2" } + EditLine { id: edit2; text: "some text for param2" } + TextWidget { text: "some radio buttons" } + // arrange some radio buttons vertically + VerticalLayout { + RadioButton { id: rb1; text: "Item 1" } + RadioButton { id: rb2; text: "Item 2" } + RadioButton { id: rb3; text: "Item 3" } + } + TextWidget { text: "and checkboxes" } + // arrange some checkboxes horizontally + HorizontalLayout { + CheckBox { id: cb1; text: "checkbox 1" } + CheckBox { id: cb2; text: "checkbox 2" } + } + } + HorizontalLayout { + Button { id: btnOk; text: "Ok"; fontSize: 27px } + Button { id: btnCancel; text: "Cancel"; fontSize: 27px } + } + CanvasWidget { + id: canvas + minWidth: 500 + minHeight: 300 + } + } + }); + + auto canvas = window.mainWidget.childById!CanvasWidget("canvas"); + canvas.onDrawListener = delegate(CanvasWidget canvas, DrawBuf buf, Rect rc) { + Log.w("canvas.onDrawListener clipRect=" ~ to!string(buf.clipRect)); + buf.fill(0xFFFFFF); + int x = rc.left; + int y = rc.top; + buf.fillRect(Rect(x+20, y+20, x+150, y+200), 0x80FF80); + buf.fillRect(Rect(x+90, y+80, x+250, y+250), 0x80FF80FF); + canvas.font.drawText(buf, x + 40, y + 50, "fillRect()"d, 0xC080C0); + buf.drawFrame(Rect(x + 400, y + 30, x + 550, y + 150), 0x204060, Rect(2,3,4,5), 0x80704020); + canvas.font.drawText(buf, x + 400, y + 5, "drawFrame()"d, 0x208020); + canvas.font.drawText(buf, x + 300, y + 100, "drawPixel()"d, 0x000080); + for (int i = 0; i < 80; i++) + buf.drawPixel(x+300 + i * 4, y+140 + i * 3 % 100, 0xFF0000 + i * 2); + canvas.font.drawText(buf, x + 200, y + 150, "drawLine()"d, 0x800020); + for (int i = 0; i < 40; i+=3) + buf.drawLine(Point(x+200 + i * 4, y+190), Point(x+150 + i * 7, y+320 + i * 2), 0x008000 + i * 5); + }; + } Scene3d scene = new Scene3d(); Camera cam = new Camera(); diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index 17975529..d0aefc11 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -806,6 +806,40 @@ public: return child; } + Style clone() { + Style res = new Style(_theme, null); + res._stateMask = _stateMask; + res._stateValue = _stateValue; + res._align = _align; + res._fontStyle = _fontStyle; + res._fontFamily = _fontFamily; + res._fontWeight = _fontWeight; + res._fontSize = _fontSize; + res._backgroundColor = _backgroundColor; + res._textColor = _textColor; + res._textFlags = _textFlags; + res._alpha = _alpha; + res._fontFace = _fontFace; + res._backgroundImageId = _backgroundImageId; + res._padding = _padding; + res._margins = _margins; + res._minWidth = _minWidth; + res._maxWidth = _maxWidth; + res._minHeight = _minHeight; + res._maxHeight = _maxHeight; + res._layoutWidth = _layoutWidth; + res._layoutHeight = _layoutHeight; + res._layoutWeight = _layoutWeight; + res._maxLines = _maxLines; + + res._focusRectColors = _focusRectColors.dup; + + res._customDrawables = _customDrawables.dup; + res._customColors = _customColors.dup; + res._customLength = _customLength.dup; + return res; + } + /// find exact existing state style or create new if no matched styles found Style getOrCreateState(uint stateMask = 0, uint stateValue = 0) { if (stateValue == State.Normal) @@ -878,6 +912,14 @@ class Theme : Style { style._margins.left = SIZE_UNSPECIFIED; // inherit style._textColor = COLOR_UNSPECIFIED; // inherit style._textFlags = TEXT_FLAGS_UNSPECIFIED; // inherit + Style parent = get(id); + if (parent) { + foreach(item; parent._substates) { + Style substate = item.clone(); + substate._parentStyle = style; + style._substates ~= substate; + } + } return style; }