styles for states

This commit is contained in:
Vadim Lopatin 2014-03-17 13:33:40 +04:00
parent 08a95002c1
commit 5fd93700a0
5 changed files with 37 additions and 25 deletions

View File

@ -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)));

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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");
}

View File

@ -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);
}