v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
menu/types.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 #pragma once
27 #ifdef NOT_USERSPACE
28 # error This is a userspace-only header, not allowed by the current build.
29 #endif
30 
31 #ifdef MENU_LOGS_ENABLED
32 # define MENU_LOG(...) LOG(__VA_ARGS__)
33 #else
34 # define MENU_LOG(...)
35 #endif
36 
37 #include <sifteo/cube.h>
38 #include <sifteo/asset.h>
39 #include <sifteo/video.h>
40 
41 namespace Sifteo {
42 
48 typedef enum {
49  MENU_UNEVENTFUL = 0,
50  MENU_NEIGHBOR_ADD,
51  MENU_NEIGHBOR_REMOVE,
52  MENU_ITEM_ARRIVE,
53  MENU_ITEM_DEPART,
54  MENU_ITEM_PRESS,
55  MENU_EXIT,
56  MENU_PREPAINT
57 } MenuEventType;
58 
59 struct MenuAssets {
60  const PinnedAssetImage *background;
61  const AssetImage *footer;
62  const AssetImage *header;
63  const AssetImage *tips[8];
64  const AssetImage *overflowIcon;
65 };
66 
67 struct MenuItem {
68  const AssetImage *icon;
69  const AssetImage *label;
70 };
71 
72 struct MenuNeighbor {
73  bool operator==(const struct MenuNeighbor& rhs) const
74  {
75  return (masterSide == rhs.masterSide)
76  && (neighbor == rhs.neighbor)
77  && (neighborSide == rhs.neighborSide);
78  }
79 
80  bool operator!=(const struct MenuNeighbor& rhs) const
81  {
82  return !operator==(rhs);
83  }
84 
85  PCubeID neighbor;
86  Side masterSide;
87  Side neighborSide;
88 };
89 
90 struct MenuEvent {
91  MenuEventType type;
92  union {
93  struct MenuNeighbor neighbor;
94  uint8_t item;
95  int8_t direction;
96  };
97 };
98 
108 typedef enum {
109  MENU_STATE_START,
110  MENU_STATE_STATIC,
111  MENU_STATE_TILTING,
112  MENU_STATE_INERTIA,
113  MENU_STATE_FINISH,
114  MENU_STATE_HOP_UP,
115  MENU_STATE_PAN_TARGET
116 } MenuState;
117 
118 
119 class Menu {
120  public:
122  Menu() {}
123 
125  Menu(VideoBuffer&, const MenuAssets*, MenuItem*);
126 
128  void init(VideoBuffer&, const MenuAssets*, MenuItem*);
129 
130  bool pollEvent(struct MenuEvent *);
131  void performDefault();
132  void reset();
133  void replaceIcon(uint8_t item, const AssetImage *icon, const AssetImage *label = 0);
134  bool itemVisible(uint8_t item);
135  void setIconYOffset(uint8_t px);
136  void setPeekTiles(uint8_t numTiles);
137  void anchor(uint8_t item, bool hopUp = false, int8_t panTarget=-1);
138  MenuState getState();
139  bool isTilted();
140  bool isHorizontal();
141  bool isAtEdge();
142  bool isTiltingAtEdge();
143  void setNumTips(uint8_t nt);
144  int getCurrentTip();
145  void drawFooter(bool force = false);
146 
147  VideoBuffer *videoBuffer() const;
148  CubeID cube() const;
149 
150  private:
151  static const float kTimeDilator = 13.1f;
152  static const float kMaxSpeedMultiplier = 2.f;
153  static const float kAccelScalingFactor = -0.25f;
154  static const uint8_t kNumTilesX = 18;
155  static const uint8_t kNumVisibleTilesX = 16;
156  static const uint8_t kNumTilesY = 18;
157  static const uint8_t kNumVisibleTilesY = 16;
158  static const float kAccelThresholdOn = 4.15f;
159  static const float kAccelThresholdOff = 0.85f;
160  static const float kAccelThresholdStep = 9.5f;
161  static const uint8_t kDefaultIconYOffset = 16;
162  static const uint8_t kDefaultPeekTiles = 1;
163  static const float kPanEasingRate = 0.05f;
164  //static const float kPanMaxSpeed = 7.5f; // moved due to weird linker error
165  static const unsigned kPanDelayMilliseconds = 800;
166 
167  // instance-constants
168  uint8_t kHeaderHeight;
169  uint8_t kFooterHeight;
170  int8_t kIconYOffset;
171  uint8_t kIconTileWidth;
172  uint8_t kIconTileHeight;
173  int8_t kEndCapPadding;
174  uint8_t kPeekTiles;
175 
176  // runtime computed constants
177  unsigned kIconPixelWidth() const { return kIconTileWidth * TILE; }
178  unsigned kIconPixelHeight() const { return kIconTileHeight * TILE; }
179  unsigned kItemTileWidth() const { return ((kEndCapPadding + TILE - 1) / TILE) + kIconTileWidth - kPeekTiles; }
180  unsigned kItemPixelWidth() const { return kItemTileWidth() * TILE; }
181  float kOneG() const { return abs(64 * kAccelScalingFactor); }
182 
183  // external parameters and metadata
184  VideoBuffer *vid; // videobuffer and its attached cube
185  const struct MenuAssets *assets; // theme assets of the menu
186  uint8_t numTips; // number of tips in the theme
187  struct MenuItem *items; // items in the strip
188  uint8_t numItems; // number of items in the strip
189  uint8_t startingItem; // centered item in strip on first draw
190  int8_t targetItem; // item to immediately pan to after first draw
191  // event breadcrumb
192  struct MenuEvent currentEvent;
193  // state tracking
194  MenuState currentState;
195  bool stateFinished;
196  Float2 accel; // accelerometer caching
197  // footer drawing
198  int currentTip;
199  SystemTime prevTipTime;
200  // static state: event at beginning of touch only
201  bool prevTouch;
202  // inertial state: where to stop
203  int stopping_position;
204  int panDelay;
205  int tiltDirection;
206  // scrolling states (Inertia and Tilt): physics
207  float position; // current x position
208  int prev_ut; // tile validity tracker
209  float velocity; // current velocity
210  TimeStep frameclock; // framerate timer
211  // finish state: animation iterations
212  int finishIteration;
213  // internal
214  MenuNeighbor neighbors[NUM_SIDES]; // menu neighbours
215  //prevent rendering before everything is set up
216  bool hasBeenStarted;
217 
218  // states.h
219  void changeState(MenuState);
220  void transToStart();
221  void stateStart();
222  void transFromStart();
223  void transToStatic();
224  void stateStatic();
225  void transFromStatic();
226  void transToTilting();
227  void stateTilting();
228  void transFromTilting();
229  void transToInertia();
230  void stateInertia();
231  void transFromInertia();
232  void transToFinish();
233  void stateFinish();
234  void transFromFinish();
235  void transToHopUp();
236  void stateHopUp();
237  void transFromHopUp();
238 
239  void transToPanTarget();
240  void statePanTarget();
241  void transFromPanTarget();
242 
243  // events.h
244  bool dispatchEvent(struct MenuEvent *ev);
245  void clearEvent();
246  void handleNeighborAdd();
247  void handleNeighborRemove();
248  void handleItemArrive();
249  void handleItemDepart();
250  void handleItemPress();
251  void handleExit();
252  void handlePrepaint();
253 
254  // util.h
255  void detectNeighbors();
256  uint8_t computeSelected();
257  void checkForPress();
258  void drawColumn(int);
259  int stoppingPositionFor(int);
260  float velocityMultiplier();
261  float maxVelocity();
262  static float lerp(float min, float max, float u);
263  void updateBG0();
264  bool itemVisibleAtCol(uint8_t item, int column);
265  uint8_t itemAtCol(int column);
266  int computeCurrentTile();
267 };
268 
273 }; // namespace Sifteo
Side
An enumeration which names the four sides of a Sifteo cube.
Definition: cube.h:54
MenuState
Definition: menu/types.h:108
Vector2< float > Float2
Typedef for a 2-vector of floats.
Definition: math.h:647
T abs(const T &value)
For any type, return the absolute value.
Definition: math.h:90
_SYSCubeID PCubeID
Alternate POD type for CubeID storage.
Definition: cube.h:70
Definition: array.h:34
Total number of sides (4)
Definition: cube.h:59