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