Skip to content

Commit

Permalink
Client|Widgets: Use the mouse click utility in Button and LineEdit wi…
Browse files Browse the repository at this point in the history
…dgets

Also cleaned up the ButtonWidget implementation and allowed
LineEditWidget to receive events even though not in focus (for mouse
events).
  • Loading branch information
skyjake committed May 31, 2013
1 parent e961afc commit 3562e79
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 30 deletions.
77 changes: 62 additions & 15 deletions doomsday/client/src/ui/widgets/buttonwidget.cpp
Expand Up @@ -46,28 +46,46 @@ DENG2_PIMPL(ButtonWidget)
updateBackground();
}

void setState(State st)
{
if(state == st) return;

State const prev = state;
state = st;
animating = true;

switch(st)
{
case Up:
scale.setStyle(prev == Down? Animation::Bounce : Animation::EaseIn);
scale.setValue(1.f, .3f);
frameOpacity.setValue(.15f, .6f);
break;

case Hover:
//scale.setStyle(Animation::EaseIn);
//scale.setValue(1.1f, .15f);
frameOpacity.setValue(.4f, .15f);
break;

case Down:
scale.setValue(.9f);
frameOpacity.setValue(.5f);
break;
}
}

void updateHover(Vector2i const &pos)
{
if(state == Down) return;

if(self.hitTest(pos))
{
if(state == Up)
{
state = Hover;
scale.setStyle(Animation::EaseIn);
scale.setValue(1.1f, .15f);
frameOpacity.setValue(.5f, .15f);
animating = true;
}
if(state == Up) setState(Hover);
}
else if(state == Hover)
{
state = Up;
scale.setStyle(Animation::EaseIn);
scale.setValue(1.f, .3f);
frameOpacity.setValue(.15f, .75f);
animating = true;
setState(Up);
}
}

Expand Down Expand Up @@ -96,9 +114,38 @@ ButtonWidget::ButtonWidget(String const &name) : LabelWidget(name), d(new Instan

bool ButtonWidget::handleEvent(Event const &event)
{
if(event.type() == Event::MousePosition)
if(event.isMouse())
{
d->updateHover(event.as<MouseEvent>().pos());
MouseEvent const &mouse = event.as<MouseEvent>();

if(mouse.type() == Event::MousePosition)
{
d->updateHover(mouse.pos());
}
else if(mouse.type() == Event::MouseButton)
{
switch(handleMouseClick(event))
{
case MouseClickStarted:
d->setState(Instance::Down);
return true;

case MouseClickFinished:
d->setState(Instance::Up);
if(d->action && hitTest(mouse.pos()))
{
d->action->trigger();
}
return true;

case MouseClickAborted:
d->setState(Instance::Up);
return true;

default:
break;
}
}
}
return false;
}
Expand Down
29 changes: 17 additions & 12 deletions doomsday/client/src/ui/widgets/consolecommandwidget.cpp
Expand Up @@ -68,19 +68,20 @@ ConsoleCommandWidget::ConsoleCommandWidget(String const &name)

bool ConsoleCommandWidget::handleEvent(Event const &event)
{
if(!event.isKeyDown()) return false;

KeyEvent const &key = event.as<KeyEvent>();

// Override the handling of the Enter key.
if(key.qtKey() == Qt::Key_Return || key.qtKey() == Qt::Key_Enter)
if(event.isKeyDown())
{
String const entered = d->history.enter();
KeyEvent const &key = event.as<KeyEvent>();

// Execute the command right away.
Con_Execute(CMDS_CONSOLE, entered.toUtf8(), false, false);
// Override the handling of the Enter key.
if(key.qtKey() == Qt::Key_Return || key.qtKey() == Qt::Key_Enter)
{
String const entered = d->history.enter();

return true;
// Execute the command right away.
Con_Execute(CMDS_CONSOLE, entered.toUtf8(), false, false);

return true;
}
}

if(LineEditWidget::handleEvent(event))
Expand All @@ -89,6 +90,10 @@ bool ConsoleCommandWidget::handleEvent(Event const &event)
return true;
}

// Fallback to history navigation.
return d->history.handleControlKey(key.qtKey());
if(event.isKeyDown())
{
// Fallback to history navigation.
return d->history.handleControlKey(event.as<KeyEvent>().qtKey());
}
return false;
}
19 changes: 16 additions & 3 deletions doomsday/client/src/ui/widgets/lineeditwidget.cpp
Expand Up @@ -23,6 +23,7 @@
#include "ui/style.h"

#include <de/KeyEvent>
#include <de/MouseEvent>
#include <de/ScalarRule>
#include <de/Drawable>

Expand Down Expand Up @@ -187,8 +188,6 @@ LineEditWidget::LineEditWidget(String const &name)
AbstractLineEditor(new FontLineWrapping),
d(new Instance(this))
{
setBehavior(HandleEventsOnlyWhenFocused);

// The widget's height is tied to the number of lines.
rule().setInput(Rule::Height, *d->height);
}
Expand Down Expand Up @@ -265,7 +264,20 @@ void LineEditWidget::draw()

bool LineEditWidget::handleEvent(Event const &event)
{
if(event.isKeyDown())
switch(handleMouseClick(event))
{
case MouseClickStarted:
return true;

case MouseClickFinished:
root().setFocus(this);
return true;

default:
break;
}

if(hasFocus() && event.isKeyDown())
{
KeyEvent const &key = event.as<KeyEvent>();

Expand All @@ -283,6 +295,7 @@ bool LineEditWidget::handleEvent(Event const &event)
return true;
}
}

return GuiWidget::handleEvent(event);
}

Expand Down

0 comments on commit 3562e79

Please sign in to comment.