v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
menu/events.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * This file is part of the public interface to the Sifteo SDK.
4  * Copyright <c> 2012 Sifteo, Inc. All rights reserved.
5  */
6 
7 /*
8  * Stateful events are dispatched by setting MenuEvent.type (and
9  * MenuEvent.neighbor/MenuEvent.item as appropriate) in a transTo$TATE or
10  * state$TATE method and returning immediately.
11  *
12  * All events are immediately caught by the dispatchEvent method in pollEvent,
13  * which copies it into the caller's event structure, and returns control to
14  * the caller to handle the event.
15  *
16  * The default handler for an event, if any, must be explicitly invoked
17  * at some point during the event loop, by calling performDefault().
18  */
19 
20 #pragma once
21 #ifdef NOT_USERSPACE
22 # error This is a userspace-only header, not allowed by the current build.
23 #endif
24 
25 #include <sifteo/menu/types.h>
26 
27 namespace Sifteo {
28 
34 inline void Menu::handleNeighborAdd()
35 {
36  MENU_LOG("Default handler: neighborAdd\n");
37  // TODO: play a sound
38 }
39 
40 inline void Menu::handleNeighborRemove()
41 {
42  MENU_LOG("Default handler: neighborRemove\n");
43  // TODO: play a sound
44 }
45 
46 inline void Menu::handleItemArrive()
47 {
48  MENU_LOG("Default handler: itemArrive\n");
49  // TODO: play a sound
50 }
51 
52 inline void Menu::handleItemDepart()
53 {
54  MENU_LOG("Default handler: itemDepart\n");
55  // TODO: play a sound
56 }
57 
58 inline void Menu::handleItemPress()
59 {
60  MENU_LOG("Default handler: itemPress\n");
61  // animate out icon
62  changeState(MENU_STATE_FINISH);
63 }
64 
65 inline void Menu::handleExit()
66 {
67  MENU_LOG("Default handler: exit\n");
68  // nothing
69 }
70 
71 inline void Menu::handlePrepaint()
72 {
73  if (hasBeenStarted)
74  {
75  System::paint();
76  }
77 }
78 
79 inline void Menu::performDefault()
80 {
81  switch(currentEvent.type) {
82  case MENU_NEIGHBOR_ADD:
83  handleNeighborAdd();
84  break;
85  case MENU_NEIGHBOR_REMOVE:
86  handleNeighborRemove();
87  break;
88  case MENU_ITEM_ARRIVE:
89  handleItemArrive();
90  break;
91  case MENU_ITEM_DEPART:
92  handleItemDepart();
93  break;
94  case MENU_ITEM_PRESS:
95  handleItemPress();
96  break;
97  case MENU_EXIT:
98  handleExit();
99  break;
100  case MENU_PREPAINT:
101  handlePrepaint();
102  break;
103  default:
104  break;
105  }
106 
107  // clear event
108  clearEvent();
109 }
110 
111 inline void Menu::clearEvent()
112 {
113  currentEvent.type = MENU_UNEVENTFUL;
114 }
115 
116 inline bool Menu::dispatchEvent(struct MenuEvent *ev)
117 {
118  if (currentEvent.type != MENU_UNEVENTFUL) {
119  *ev = currentEvent;
120  return true;
121  }
122  return false;
123 }
124 
129 }; // namespace Sifteo