|
|
MultiStateButton
Demonstration of state transition tables in Heron.
MultiStateButton
class MultiStateButton :
{
attributes {
graphicalState : Int = 0;
boundingBox : Rect;
onClick : Action;
}
operations {
hitTest(coord : Coord) : Bool {
return boundingBox.hitTest(coord);
}
draw() {
switch (graphicalState) {
case (0) {
// draw unfocused button
}
case (1) {
// draw focused button
}
case (2) {
// draw down button
}
default {
throw new Exception("invalid graphical state");
}
}
}
updateGraphicalState(nState : int) {
if (nState != graphicalState) {
graphicalState = nState;
draw();
}
}
}
states
{
unfocusedState(e : UnfocusedEvent) {
entry {
UpdateGraphicalState(0);
}
transitions {
MouseMoveEvent -> focusingState;
}
}
focusingState(e : MouseMoveEvent) {
entry {
if (hitTest(e.coord))
this.send(FocusedEvent);
else
this.send(UnfocusedEvent);
}
transitions {
FocusedEvent -> focusedState;
UnfocusedEvent -> unfocusedState;
}
}
focusedState(e : FocusedEvent) {
entry {
updateGraphicalState(1);
}
transitions {
MouseMoveEvent -> focusingState;
ButtonDownEvent -> downState;
}
}
downAndFocusedState(e : ButtonDownEvent ) {
entry {
updateGraphicalState(2);
}
transitions {
MouseMoveEvent -> movingWhileDownState;
MouseUpEvent -> clickedState;
}
}
movingWhileDownState(e : MouseMoveEvent) {
entry {
if (hitTest(e.coord))
this.send(downAndFocusedEvent);
else
this.send(downAndUnfocusedEvent);
}
transitions {
FocusedEvent -> focusedState;
UnfocusedEvent -> unfocussedState;
}
}
downAndUnfocusedState(e : ButtonDownEvent ) {
entry {
updateGraphicalState(0);
}
transitions {
MouseMoveEvent -> movingWhileDownState;
MouseUpEvent -> unfocusedState;
}
}
clickedState(e : ButtonUpEvent) {
entry {
onClick.invoke(this);
this.send(UnfocusedEvent);
}
transitions {
UnfocusedEvent -> unfocusedState;
}
}
}
}
Sign in to add a comment
