v1.0.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 } MenuState;
97 
98 
99 class Menu {
100  public:
102  Menu() {}
103 
105  Menu(VideoBuffer&, const MenuAssets*, MenuItem*);
106 
108  void init(VideoBuffer&, const MenuAssets*, MenuItem*);
109 
110  bool pollEvent(struct MenuEvent *);
111  void performDefault();
112  void reset();
113  void replaceIcon(uint8_t item, const AssetImage *icon, const AssetImage *label = 0);
114  bool itemVisible(uint8_t item);
115  void setIconYOffset(uint8_t px);
116  void setPeekTiles(uint8_t numTiles);
117  void anchor(uint8_t item, bool hopUp = false);
118  MenuState getState();
119 
120  VideoBuffer *videoBuffer() const;
121  CubeID cube() const;
122 
123  private:
124  static const float kTimeDilator = 13.1f;
125  static const float kMaxSpeedMultiplier = 2.f;
126  static const float kAccelScalingFactor = -0.25f;
127  static const uint8_t kNumTilesX = 18;
128  static const uint8_t kNumVisibleTilesX = 16;
129  static const uint8_t kNumTilesY = 18;
130  static const uint8_t kNumVisibleTilesY = 16;
131  static const float kAccelThresholdOn = 4.15f;
132  static const float kAccelThresholdOff = 0.85f;
133  static const float kAccelThresholdStep = 9.5f;
134  static const uint8_t kDefaultIconYOffset = 16;
135  static const uint8_t kDefaultPeekTiles = 1;
136 
137  // instance-constants
138  uint8_t kHeaderHeight;
139  uint8_t kFooterHeight;
140  int8_t kIconYOffset;
141  uint8_t kIconTileWidth;
142  uint8_t kIconTileHeight;
143  int8_t kEndCapPadding;
144  uint8_t kPeekTiles;
145 
146  // runtime computed constants
147  unsigned kIconPixelWidth() const { return kIconTileWidth * TILE; }
148  unsigned kIconPixelHeight() const { return kIconTileHeight * TILE; }
149  unsigned kItemTileWidth() const { return ((kEndCapPadding + TILE - 1) / TILE) + kIconTileWidth - kPeekTiles; }
150  unsigned kItemPixelWidth() const { return kItemTileWidth() * TILE; }
151  float kOneG() const { return abs(64 * kAccelScalingFactor); }
152 
153  // external parameters and metadata
154  VideoBuffer *vid; // videobuffer and its attached cube
155  const struct MenuAssets *assets; // theme assets of the menu
156  uint8_t numTips; // number of tips in the theme
157  struct MenuItem *items; // items in the strip
158  uint8_t numItems; // number of items in the strip
159  uint8_t startingItem; // centered item in strip on first draw
160  // event breadcrumb
161  struct MenuEvent currentEvent;
162  // state tracking
163  MenuState currentState;
164  bool stateFinished;
165  Float2 accel; // accelerometer caching
166  // footer drawing
167  int currentTip;
168  SystemTime prevTipTime;
169  // static state: event at beginning of touch only
170  bool prevTouch;
171  // inertial state: where to stop
172  int stopping_position;
173  int tiltDirection;
174  // scrolling states (Inertia and Tilt): physics
175  float position; // current x position
176  int prev_ut; // tile validity tracker
177  float velocity; // current velocity
178  TimeStep frameclock; // framerate timer
179  // finish state: animation iterations
180  int finishIteration;
181  // internal
182  MenuNeighbor neighbors[NUM_SIDES]; // menu neighbours
183  //prevent rendering before everything is set up
184  bool hasBeenStarted;
185 
186  // states.h
187  void changeState(MenuState);
188  void transToStart();
189  void stateStart();
190  void transFromStart();
191  void transToStatic();
192  void stateStatic();
193  void transFromStatic();
194  void transToTilting();
195  void stateTilting();
196  void transFromTilting();
197  void transToInertia();
198  void stateInertia();
199  void transFromInertia();
200  void transToFinish();
201  void stateFinish();
202  void transFromFinish();
203  void transToHopUp();
204  void stateHopUp();
205  void transFromHopUp();
206 
207  // events.h
208  bool dispatchEvent(struct MenuEvent *ev);
209  void clearEvent();
210  void handleNeighborAdd();
211  void handleNeighborRemove();
212  void handleItemArrive();
213  void handleItemDepart();
214  void handleItemPress();
215  void handleExit();
216  void handlePrepaint();
217 
218  // util.h
219  void detectNeighbors();
220  uint8_t computeSelected();
221  void checkForPress();
222  void drawColumn(int);
223  void drawFooter(bool force = false);
224  int stoppingPositionFor(int);
225  float velocityMultiplier();
226  float maxVelocity();
227  static float lerp(float min, float max, float u);
228  void updateBG0();
229  bool itemVisibleAtCol(uint8_t item, int column);
230  uint8_t itemAtCol(int column);
231  int computeCurrentTile();
232 };
233 
238 }; // namespace Sifteo