v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
menu/events.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * Sifteo SDK
4  *
5  * Copyright <c> 2012 Sifteo, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 /*
27  * Stateful events are dispatched by setting MenuEvent.type (and
28  * MenuEvent.neighbor/MenuEvent.item as appropriate) in a transTo$TATE or
29  * state$TATE method and returning immediately.
30  *
31  * All events are immediately caught by the dispatchEvent method in pollEvent,
32  * which copies it into the caller's event structure, and returns control to
33  * the caller to handle the event.
34  *
35  * The default handler for an event, if any, must be explicitly invoked
36  * at some point during the event loop, by calling performDefault().
37  */
38 
39 #pragma once
40 #ifdef NOT_USERSPACE
41 # error This is a userspace-only header, not allowed by the current build.
42 #endif
43 
44 #include <sifteo/menu/types.h>
45 
46 namespace Sifteo {
47 
53 inline void Menu::handleNeighborAdd()
54 {
55  MENU_LOG("Default handler: neighborAdd\n");
56  // TODO: play a sound
57 }
58 
59 inline void Menu::handleNeighborRemove()
60 {
61  MENU_LOG("Default handler: neighborRemove\n");
62  // TODO: play a sound
63 }
64 
65 inline void Menu::handleItemArrive()
66 {
67  MENU_LOG("Default handler: itemArrive\n");
68  // TODO: play a sound
69 }
70 
71 inline void Menu::handleItemDepart()
72 {
73  MENU_LOG("Default handler: itemDepart\n");
74  // TODO: play a sound
75 }
76 
77 inline void Menu::handleItemPress()
78 {
79  MENU_LOG("Default handler: itemPress\n");
80  // animate out icon
81  changeState(MENU_STATE_FINISH);
82 }
83 
84 inline void Menu::handleExit()
85 {
86  MENU_LOG("Default handler: exit\n");
87  // nothing
88 }
89 
90 inline void Menu::handlePrepaint()
91 {
92  if (hasBeenStarted)
93  {
94  System::paint();
95  }
96 }
97 
98 inline void Menu::performDefault()
99 {
100  switch(currentEvent.type) {
101  case MENU_NEIGHBOR_ADD:
102  handleNeighborAdd();
103  break;
104  case MENU_NEIGHBOR_REMOVE:
105  handleNeighborRemove();
106  break;
107  case MENU_ITEM_ARRIVE:
108  handleItemArrive();
109  break;
110  case MENU_ITEM_DEPART:
111  handleItemDepart();
112  break;
113  case MENU_ITEM_PRESS:
114  handleItemPress();
115  break;
116  case MENU_EXIT:
117  handleExit();
118  break;
119  case MENU_PREPAINT:
120  handlePrepaint();
121  break;
122  default:
123  break;
124  }
125 
126  // clear event
127  clearEvent();
128 }
129 
130 inline void Menu::clearEvent()
131 {
132  currentEvent.type = MENU_UNEVENTFUL;
133 }
134 
135 inline bool Menu::dispatchEvent(struct MenuEvent *ev)
136 {
137  if (currentEvent.type != MENU_UNEVENTFUL) {
138  *ev = currentEvent;
139  return true;
140  }
141  return false;
142 }
143 
148 }; // namespace Sifteo
Definition: array.h:34
static void paint()
Draw the next frame.
Definition: system.h:119