From 5fd93700a0f02f5533dd4ff5791e7fa9ce189ac4 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Mon, 17 Mar 2014 13:33:40 +0400 Subject: [PATCH] styles for states --- examples/example1/main.d | 2 +- src/dlangui/platforms/windows/winapp.d | 9 +++++++- src/dlangui/widgets/controls.d | 29 +++++++++++++------------- src/dlangui/widgets/styles.d | 6 +++++- src/dlangui/widgets/widget.d | 16 +++++++------- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/examples/example1/main.d b/examples/example1/main.d index fdaf047a..18de0608 100644 --- a/examples/example1/main.d +++ b/examples/example1/main.d @@ -43,7 +43,7 @@ extern (C) int UIAppMain(string[] args) { LinearLayout layout = new LinearLayout(); layout.addChild((new TextWidget()).textColor(0x00802000).text("Text widget 0")); layout.addChild((new TextWidget()).textColor(0x40FF4000).text("Text widget")); - layout.addChild((new Button()).textColor(0x40FF4000).text("Button1")); + layout.addChild((new Button()).text("Button1")); //.textColor(0x40FF4000) layout.addChild((new Button()).textColor(0x000000FF).text("Button2")); layout.addChild((new TextWidget()).textColor(0x40FF4000).text("Text widget")); layout.addChild((new ImageWidget()).drawableId("exit").padding(Rect(5,5,5,5))); diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index 6e1ad690..3283f26b 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -370,6 +370,10 @@ class Win32Window : Window { return res; } + void requestUpdate() { + InvalidateRect(_hwnd, null, FALSE); + } + bool onMouse(uint message, ushort flags, short x, short y) { Log.d("Win32 Mouse Message ", message, " flags=", flags, " x=", x, " y=", y); MouseButton button = MouseButton.None; @@ -422,7 +426,10 @@ class Win32Window : Window { event.lbutton = _lbutton; event.rbutton = _rbutton; event.mbutton = _mbutton; - return dispatchMouseEvent(event); + bool res = dispatchMouseEvent(event); + if (res) + requestUpdate(); + return res; } } diff --git a/src/dlangui/widgets/controls.d b/src/dlangui/widgets/controls.d index 4367b9ec..74440a53 100644 --- a/src/dlangui/widgets/controls.d +++ b/src/dlangui/widgets/controls.d @@ -30,20 +30,6 @@ class TextWidget : Widget { return false; } - override bool onMouseEvent(MouseEvent event) { - if (event.action == MouseAction.ButtonDown && event.button == MouseButton.Left) { - setState(State.Pressed); - Log.d("Button state: ", state); - return true; - } - if (event.action == MouseAction.ButtonUp && event.button == MouseButton.Left) { - resetState(State.Pressed); - Log.d("Button state: ", state); - return true; - } - return false; - } - override void onDraw(DrawBuf buf) { if (visibility != Visibility.Visible) return; @@ -147,4 +133,19 @@ class Button : Widget { applyAlign(rc, sz); font.drawText(buf, rc.left, rc.top, text, textColor); } + + override bool onMouseEvent(MouseEvent event) { + if (event.action == MouseAction.ButtonDown && event.button == MouseButton.Left) { + setState(State.Pressed); + Log.d("Button state: ", state); + return true; + } + if (event.action == MouseAction.ButtonUp && event.button == MouseButton.Left) { + resetState(State.Pressed); + Log.d("Button state: ", state); + return true; + } + return false; + } + } diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index dd455fb1..278a36c6 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -402,7 +402,8 @@ class Style { /// create state substyle for this style Style createState(uint stateMask = 0, uint stateValue = 0) { - Style child = createSubstyle(id); + assert(stateMask != 0); + Style child = createSubstyle(null); child._stateMask = stateMask; child._stateValue = stateValue; child._backgroundColor = COLOR_UNSPECIFIED; @@ -414,6 +415,7 @@ class Style { const(Style) forState(uint state) const { if (state == 0) return this; + Log.d("forState ", state, " styleId=", _id, " substates=", _substates.length); if (id is null && parentStyle !is null && _substates.length == 0) return parentStyle.forState(state); foreach(item; _substates) { @@ -502,4 +504,6 @@ static this() { _currentTheme = new Theme("default"); Style button = _currentTheme.createSubstyle("BUTTON").backgroundImageId("btn_default_normal").alignment(Align.Center); Style text = _currentTheme.createSubstyle("TEXT").margins(Rect(3,3,3,3)).padding(Rect(3,3,3,3)); + button.createState(State.Pressed, State.Pressed).backgroundImageId("btn_default_pressed"); + button.createState(State.Focused, State.Focused).backgroundImageId("btn_default_selected"); } diff --git a/src/dlangui/widgets/widget.d b/src/dlangui/widgets/widget.d index 97d97645..8f8c7c7f 100644 --- a/src/dlangui/widgets/widget.d +++ b/src/dlangui/widgets/widget.d @@ -68,13 +68,13 @@ class Widget { const (Style) stateStyle = normalStyle.forState(stateFlags); if (stateStyle !is normalStyle) return stateStyle; // found style for state in current style - // lookup state style in parent (one level max) - const (Style) parentStyle = normalStyle.parentStyle; - if (parentStyle is normalStyle) - return normalStyle; // no parent - const (Style) parentStateStyle = parentStyle.forState(stateFlags); - if (parentStateStyle !is parentStyle) - return parentStateStyle; // found style for state in parent + //// lookup state style in parent (one level max) + //const (Style) parentStyle = normalStyle.parentStyle; + //if (parentStyle is normalStyle) + // return normalStyle; // no parent + //const (Style) parentStateStyle = parentStyle.forState(stateFlags); + //if (parentStateStyle !is parentStyle) + // return parentStateStyle; // found style for state in parent return normalStyle; // fallback to current style } /// returns style for current widget state @@ -327,7 +327,7 @@ class Widget { return; Rect rc = _pos; applyMargins(rc); - DrawableRef bg = style.backgroundDrawable; + DrawableRef bg = stateStyle.backgroundDrawable; if (!bg.isNull) { bg.drawTo(buf, rc); }