Input: Implementing Interaction Techniques as Finite State ...

55
Input: Implementing Interaction Techniques as Finite State Machines

Transcript of Input: Implementing Interaction Techniques as Finite State ...

Page 1: Input: Implementing Interaction Techniques as Finite State ...

Input: Implementing Interaction Techniques

as Finite State Machines

Page 2: Input: Implementing Interaction Techniques as Finite State ...

2

Administration

• HW4a due today • HW5 set today

Page 3: Input: Implementing Interaction Techniques as Finite State ...

3

Interaction Techniques

• A method for carrying out a specific interactive task – Example: enter a number in a

range • Could use … (simulated) slider • (simulated) knob • Type in a number (text edit box)

– Each is a different interaction technique

Page 4: Input: Implementing Interaction Techniques as Finite State ...

4

How do we implement interaction techniques? • Focus of today’s lecture • Important for understanding

existing techniques • Important for designing and

building your own: – Why not just use existing ones?

Page 5: Input: Implementing Interaction Techniques as Finite State ...

5

Suppose we wanted to implement an interaction for specifying a line

• Could just specify two endpoints – click, click – not good: no affordance,no feedback

• Better feedback is to use “rubber banding” – stretch out the line as you drag – at all times, shows where you would

end up if you “let go”

Page 6: Input: Implementing Interaction Techniques as Finite State ...

6

Aside

• Rubber banding provides good feedback

• How would we provide better affordance?

Page 7: Input: Implementing Interaction Techniques as Finite State ...

7

Aside

• Rubber banding provides good feedback

• How would we provide better affordance? – Changing cursor shape is about all

we have to work with

Page 8: Input: Implementing Interaction Techniques as Finite State ...

8

Implementing rubber banding

Accept the press for endpoint p1;

P2 = P1;

Draw line P1-P2;

Repeat

Erase line P1-P2;

P2 = current_position( );

Draw line P1-P2;

Until release event;

Act on line input;

Page 9: Input: Implementing Interaction Techniques as Finite State ...

9

Implementing rubber banding

• Need to get around this loop absolute min of 5 times / sec – 10 times better – more would be better

• Notice we need “undraw” here

Page 10: Input: Implementing Interaction Techniques as Finite State ...

10

2nd Aside: How do we do “undraw” in a frame buffer? • Writes to frame buffer memory

are destructive (old background lost)

Page 11: Input: Implementing Interaction Techniques as Finite State ...

11

2nd Aside: How do we do “undraw” in a frame buffer? • Writes to frame buffer memory

are destructive (old background lost)

• Two major alternatives: – XOR – Completely redraw the image

from some description (e.g., interactor tree)

Page 12: Input: Implementing Interaction Techniques as Finite State ...

12

What’s wrong with this code?

Accept the press for endpoint p1;

P2 = P1;

Draw line P1-P2;

Repeat

Erase line P1-P2;

P2 = current_position( );

Draw line P1-P2;

Until release event;

Act on line input;

Page 13: Input: Implementing Interaction Techniques as Finite State ...

13

Not event driven

• Not in the basic event / redraw cycle form – don’t want to mix event and sampled – in many systems, can’t ignore events

for arbitrary lengths of time

• How do we do this in a normal event / redraw loop?

Page 14: Input: Implementing Interaction Techniques as Finite State ...

14

You don’t get to write control flow in event driven systems • Control is in the hands of the user • Basically have to chop up the

actions in the code above and redistribute them in event driven form – “event driven control flow” – need to maintain “state”

(where you are) between events and start up “in the state” you were in when you left off

• Examples from assignments?

Page 15: Input: Implementing Interaction Techniques as Finite State ...

15

Finite state machine controllers

• One good way to maintain “state” is to use a state machine  Finite State Machine (FSM) – Has a collection of states

the system could be “in” • One current state

– Events cause you to move from current state to other states (or back to same state) • And execute actions as you move

Page 16: Input: Implementing Interaction Techniques as Finite State ...

16

FSM notation

• Circles represent states – arrow for start state

• Begin the interaction in this state

– double circles for “final states” • Typically not really “final”, just denoting end of part of interaction

• Typically means you reset to start state

Page 17: Input: Implementing Interaction Techniques as Finite State ...

17

FSM notation

• Transitions represented as arcs – Labeled with a “symbol”

• for us an event (can vary)

– Also optionally labeled with an action

B A

Mouse_Down / Draw_Line()

Page 18: Input: Implementing Interaction Techniques as Finite State ...

18

FSM Notation

• Means: when you are in state A and you see a mouse down, do the action (call draw_line), and go to state B

B A

Mouse_Down / Draw_Line()

Page 19: Input: Implementing Interaction Techniques as Finite State ...

19

FSM Notation

• Sometimes also put actions on states – same as action on all incoming

transitions

B A

Mouse_Down / Draw_Line()

Draw_Line()

Page 20: Input: Implementing Interaction Techniques as Finite State ...

20

Rubber banding again (cutting up the code) Accept the press for endpoint p1;

A: P2 = P1;

Draw line P1-P2;

Repeat

B: Erase line P1-P2;

P2 = current_position();

Draw line P1-P2;

Until release event;

C: Act on line input;

Page 21: Input: Implementing Interaction Techniques as Finite State ...

21

A: P2 = P1;

Draw line P1-P2;

B: Erase line P1-P2;

P2 = current_position();

Draw line P1-P2;

C: Act on line input;

FSM control for rubber banding

Press / A

Move / B

Release / C

Page 22: Input: Implementing Interaction Techniques as Finite State ...

22

How does this work: demonstration!

5 volunteers: 3 states 1 event actor 1 user

FSM control for rubber banding

Page 23: Input: Implementing Interaction Techniques as Finite State ...

23

Example #2: Button

• For drawing a line, had to represent – Clicking the first point – Moving the cursor – Clicking the second point

• What kinds of things do we need to represent for buttons?

Page 24: Input: Implementing Interaction Techniques as Finite State ...

24

Second example: button

Press inside => highlight Move in/out => change highlight Release inside => act Release outside => do nothing

Page 25: Input: Implementing Interaction Techniques as Finite State ...

25

FSM for a button?

Page 26: Input: Implementing Interaction Techniques as Finite State ...

26

FSM for a button

Press-inside / A

Leave / B Enter / C

Release / D

Release / E

Page 27: Input: Implementing Interaction Techniques as Finite State ...

27

FSM for a button

A: highlight button B: unhighlight button C: highlight button D: <do nothing> E: unhighlight; do button action

Press-inside / A

Leave / B Enter / C

Release / D

Release / E

Page 28: Input: Implementing Interaction Techniques as Finite State ...

28

How does this work: demonstration!

7 volunteers: 5 states 1 event actor 1 user

FSM control for buttons

Page 29: Input: Implementing Interaction Techniques as Finite State ...

29

Now your turn!

• Document window with text in it and a scrollbar on one side

• What’s the FSM for the scrollbar thumb?

• 1 user • 1 event actor • N(?) states

Page 30: Input: Implementing Interaction Techniques as Finite State ...

30

• What’s the FSM for the scrollbar if the user just clicks on the scrollbar?

• 1 user • 1 event actor • N(?) states

Page 31: Input: Implementing Interaction Techniques as Finite State ...

31

In general...

• Machine states represent context of interaction – “where you are” in control flow

• Transitions indicate how to respond to various events – what to do in each context

Page 32: Input: Implementing Interaction Techniques as Finite State ...

32

“Events” in FSMs

• What constitutes an “event” varies – may be just low level events, or – higher level (synthesized) events

• e.g. region-enter, press-inside • Also things you might not think of like time passing

Page 33: Input: Implementing Interaction Techniques as Finite State ...

33

Guards on transitions

• Sometimes also use “guards” – predicate (bool expr) before event –  adds extra conditions required to

fire – typical notation:

expression: event / action • e.g. button.enabled: press-inside / A

Page 34: Input: Implementing Interaction Techniques as Finite State ...

34

FSM are a good way to do control flow in event driven systems

• Can do (formal or informal) analysis or reasoning about UI – are all possible inputs (e.g.

errors) handled from each state? – what are next legal inputs

• can use to enable / disable

Page 35: Input: Implementing Interaction Techniques as Finite State ...

35

Implementing FSMs

state = start_state; for (;;) { raw_evt = wait_for_event(); events = transform_event(raw_evt); for each evt in events { state = fsm_transition(state, evt); } } • Note that this is basically the

normal event loop

Page 36: Input: Implementing Interaction Techniques as Finite State ...

36

Implementing FSMs

fsm_transition(state, evt)

switch (state)

case 0: // case for each state

case 1: // case for next state

return state;

Page 37: Input: Implementing Interaction Techniques as Finite State ...

37

Implementing FSMs

fsm_transition(state, evt)

switch (state)

case 0: // case for each state

switch (evt.kind)

case loc_move: // trans evt

… action … // trans action

state = 42; // trans target

case loc_dn:

...

case 1: // case for next state

switch (evt.kind) …

return state;

Page 38: Input: Implementing Interaction Techniques as Finite State ...

38

Implementing FSMs

fsm_transition(state, evt)

switch (state)

case 0: // case for each state

switch (evt.kind)

case loc_move: // trans evt

… action … // trans action

state = 42; // trans target

case loc_dn:

...

case 1: // case for next state

switch (evt.kind) …

return state;

Page 39: Input: Implementing Interaction Techniques as Finite State ...

39

FSM Issues

• Notation – Graphical notation is nice for

small things, but doesn’t scale (spaghetti)

– Textual notation is not nice • Like all GOTO control flow

• Handles sequencing well, but not independent action – State explosion problems

Page 40: Input: Implementing Interaction Techniques as Finite State ...

40

State explosion problems

• Suppose you had a button

• And you want to add an option to modify its action with ctrl key – Changes label and action

Page 41: Input: Implementing Interaction Techniques as Finite State ...

41

Modified button example

• What does tracking the control key look like?

Page 42: Input: Implementing Interaction Techniques as Finite State ...

42

Modified button example

• Control key

Page 43: Input: Implementing Interaction Techniques as Finite State ...

43

Modified button example

• Control key x Button

x

Page 44: Input: Implementing Interaction Techniques as Finite State ...

44

Modified button example

• Transitions are really independent “Cross-product” machine

x

Page 45: Input: Implementing Interaction Techniques as Finite State ...

45

Cross product machines

• Replicate machine A once for every state in machine B

Page 46: Input: Implementing Interaction Techniques as Finite State ...

46

Cross product machines

• Replicate machine A once for every state in machine B

Page 47: Input: Implementing Interaction Techniques as Finite State ...

47

Cross product machines

• Replicate machine A once for every state in machine B

Page 48: Input: Implementing Interaction Techniques as Finite State ...

48

Cross product machines

• Add transitions from machine B between corresponding states

Page 49: Input: Implementing Interaction Techniques as Finite State ...

49

Cross product machines

• Correct and simplify based on semantics

Page 50: Input: Implementing Interaction Techniques as Finite State ...

50

Cross product machines

• Correct and simplify based on semantics

Page 51: Input: Implementing Interaction Techniques as Finite State ...

51

Now suppose we add another independent action (shift key?)

Page 52: Input: Implementing Interaction Techniques as Finite State ...

52

Now suppose we add another independent action (shift key?) • Same pattern

– But, gets really ugly – Won’t attempt it here

• Quickly get combinatoric explosion – Big drawback of FSM

Page 53: Input: Implementing Interaction Techniques as Finite State ...

53

State machines very useful, but do have limits • State machines don’t handle

independent actions very well • Mostly useful for smaller things

– Great for individual components – Not so great for whole dialogs

• Path of least resistance is rigid sequencing – Ask: is this good for what I am doing?

Page 54: Input: Implementing Interaction Techniques as Finite State ...

54

Questions?

Page 55: Input: Implementing Interaction Techniques as Finite State ...

55

Insert ticket Insert coin

Press cancel / Return ticket

Insert coin

Enough money/give change & ticket

Press cancel / Return coins & ticket

Receipt button/ give receipt

timeout