def _replace_node(self, new_node): """ Move a new node into display, slow animation. """ if not self.is_running: if debug_mode: print("Not running") if self._current_node: self._current_node.kill() self._current_node = None return if self._current_node: self._current_node.do( MoveTo(self._point_offscreen_left, 0.5 * self._move_time) + CallFunc(self._current_node.kill)) self._current_node = UpdatableNode(new_node, self._point_offscreen_right) self._current_node.do( Delay(0.5 * self._move_time) + MoveTo(self._point_onscreen, 0.5 * self._move_time)) self.add(self._current_node)
def _replace_node(self, new_node): """ Move a new node into display, slow animation. """ if not self.is_running: if debug_mode: print("Not running") if self._current_node: self._current_node.kill() self._current_node = None return if self._current_node: self._current_node.do(MoveTo(self._point_offscreen_left, 0.5 * self._move_time) + CallFunc(self._current_node.kill)) self._current_node = UpdatableNode(new_node, self._point_offscreen_right) self._current_node.do(Delay(0.5 * self._move_time) + MoveTo(self._point_onscreen, 0.5 * self._move_time)) self.add(self._current_node)
class CenterLayer(cocos.layer.base_layers.Layer): """ Center part of the screen, handling display of the main content. Current content: - Display the drink or mix currently centered on the bottom ticker; - Display instructions on demand; Added nodes are expected to anchor at the bottom-center. Their sizes should respect the passed content sizes. Parameters ---------- top_margin : int Margin on top of the screen. bottom_margin : int Margin at the bottom of the screen. left_margin : int Margin at the left side of the screen. right_margin : int Margin at the right side of the screen. move_time : float Time in seconds to move an object in and out of the screen. """ def __init__(self, top_margin=100, bottom_margin=150, left_margin=50, right_margin=50, move_time=1.0): """ Initialize the drink layer. """ super(CenterLayer, self).__init__() self._top_margin = top_margin self._bottom_margin = bottom_margin self._left_margin = left_margin self._right_margin = right_margin self._screen_width, self._screen_height = cocos.director.director.get_window_size() self._content_width = self._screen_width - self._left_margin - self._right_margin self._content_height = self._screen_height - self._top_margin - self._bottom_margin self._point_offscreen_right = (self._screen_width + (self._content_width / 2), self._bottom_margin) self._point_offscreen_left = (0 - (self._content_width / 2), self._bottom_margin) self._point_onscreen = (self._screen_width / 2, self._bottom_margin) self._move_time = move_time # Current node on display self._current_node = None # Contains details to construct a new node. None when nothing scheduled. self._next_node = None # Has a clear request been issued? self._clear = False def schedule_next_node(self, node_constructor, in_place=False): """ Schedule a node for display. The node will be instantiated in the render thread. No queueing! Multiple calls without a render pass in between, will result in only the last request being honored. Parameters ---------- node_constructor Constructor for the next node. in_place True if node should be replaced on current node location. False if it should be brought in as a new node. """ self._next_node = (node_constructor, in_place) def show_drink(self, drink, in_place=False): """ Change the drink currently being shown. """ if isinstance(drink, Mix): node_type = MixDisplayConstructor else: node_type = DrinkHistoryDisplayConstructor node_constructor = node_type(drink, self._content_width, self._content_height) self.schedule_next_node(node_constructor, in_place=in_place) def show_explanation(self): """ Show an explanation of the game. """ self.schedule_next_node(ExplanationConstructor(self._content_height)) def show_trends(self, drinks): """ Show trends in prices. """ self.schedule_next_node(TrendDisplayConstructor(self._content_width, self._content_height, drinks)) def clear(self): """ Clear the display, removes the current content, even if it is locked """ self._clear = True def draw(self, *args, **kwargs): """ Called by Cocos2D. Performs actions on next render loop. """ if self._clear: self._replace_node(None) self._clear = False if self._next_node: node_constructor, in_place = self._next_node if node_constructor.ready: self._next_node = None if in_place: if debug_mode: print('Doing in place update') self._update_node(node_constructor.get_node()) else: if debug_mode: print('Doing full update') self._replace_node(node_constructor.get_node()) elif debug_mode: print('Node still constructing') def _replace_node(self, new_node): """ Move a new node into display, slow animation. """ if not self.is_running: if debug_mode: print("Not running") if self._current_node: self._current_node.kill() self._current_node = None return if self._current_node: self._current_node.do(MoveTo(self._point_offscreen_left, 0.5 * self._move_time) + CallFunc(self._current_node.kill)) self._current_node = UpdatableNode(new_node, self._point_offscreen_right) self._current_node.do(Delay(0.5 * self._move_time) + MoveTo(self._point_onscreen, 0.5 * self._move_time)) self.add(self._current_node) def _update_node(self, new_contents): """ Replace the contents of the current displayed node. """ if self._current_node: self._current_node.update(new_contents)
class CenterLayer(cocos.layer.base_layers.Layer): """ Center part of the screen, handling display of the main content. Current content: - Display the drink or mix currently centered on the bottom ticker; - Display instructions on demand; Added nodes are expected to anchor at the bottom-center. Their sizes should respect the passed content sizes. Parameters ---------- top_margin : int Margin on top of the screen. bottom_margin : int Margin at the bottom of the screen. left_margin : int Margin at the left side of the screen. right_margin : int Margin at the right side of the screen. move_time : float Time in seconds to move an object in and out of the screen. """ def __init__(self, top_margin=100, bottom_margin=150, left_margin=50, right_margin=50, move_time=1.0): """ Initialize the drink layer. """ super(CenterLayer, self).__init__() self._top_margin = top_margin self._bottom_margin = bottom_margin self._left_margin = left_margin self._right_margin = right_margin self._screen_width, self._screen_height = cocos.director.director.get_window_size( ) self._content_width = self._screen_width - self._left_margin - self._right_margin self._content_height = self._screen_height - self._top_margin - self._bottom_margin self._point_offscreen_right = (self._screen_width + (self._content_width / 2), self._bottom_margin) self._point_offscreen_left = (0 - (self._content_width / 2), self._bottom_margin) self._point_onscreen = (self._screen_width / 2, self._bottom_margin) self._move_time = move_time # Current node on display self._current_node = None # Contains details to construct a new node. None when nothing scheduled. self._next_node = None # Has a clear request been issued? self._clear = False def schedule_next_node(self, node_constructor, in_place=False): """ Schedule a node for display. The node will be instantiated in the render thread. No queueing! Multiple calls without a render pass in between, will result in only the last request being honored. Parameters ---------- node_constructor Constructor for the next node. in_place True if node should be replaced on current node location. False if it should be brought in as a new node. """ self._next_node = (node_constructor, in_place) def show_drink(self, drink, in_place=False): """ Change the drink currently being shown. """ if isinstance(drink, Mix): node_type = MixDisplayConstructor else: node_type = DrinkHistoryDisplayConstructor node_constructor = node_type(drink, self._content_width, self._content_height) self.schedule_next_node(node_constructor, in_place=in_place) def show_explanation(self): """ Show an explanation of the game. """ self.schedule_next_node(ExplanationConstructor(self._content_height)) def show_trends(self, drinks): """ Show trends in prices. """ self.schedule_next_node( TrendDisplayConstructor(self._content_width, self._content_height, drinks)) def clear(self): """ Clear the display, removes the current content, even if it is locked """ self._clear = True def draw(self, *args, **kwargs): """ Called by Cocos2D. Performs actions on next render loop. """ if self._clear: self._replace_node(None) self._clear = False if self._next_node: node_constructor, in_place = self._next_node if node_constructor.ready: self._next_node = None if in_place: if debug_mode: print('Doing in place update') self._update_node(node_constructor.get_node()) else: if debug_mode: print('Doing full update') self._replace_node(node_constructor.get_node()) elif debug_mode: print('Node still constructing') def _replace_node(self, new_node): """ Move a new node into display, slow animation. """ if not self.is_running: if debug_mode: print("Not running") if self._current_node: self._current_node.kill() self._current_node = None return if self._current_node: self._current_node.do( MoveTo(self._point_offscreen_left, 0.5 * self._move_time) + CallFunc(self._current_node.kill)) self._current_node = UpdatableNode(new_node, self._point_offscreen_right) self._current_node.do( Delay(0.5 * self._move_time) + MoveTo(self._point_onscreen, 0.5 * self._move_time)) self.add(self._current_node) def _update_node(self, new_contents): """ Replace the contents of the current displayed node. """ if self._current_node: self._current_node.update(new_contents)