card.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014-2016 Tomek Wójcik <tomek@bthlabs.pl>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. """
  23. pie_time.card
  24. =============
  25. This module contains the AbstractCard class.
  26. """
  27. import os
  28. import sys
  29. import pygame
  30. class AbstractCard(object):
  31. """
  32. The abstract card class.
  33. All the custom cards **must** inherit from this class.
  34. **Application binding and settings.**
  35. The application calls the card's :py:meth:`pie_time.AbstractCard.set_app`
  36. and :py:meth:`pie_time.AbstractCard.set_settings` methods during
  37. initialization (before calling the
  38. :py:meth:`pie_time.AbstractCard.initialize` method).
  39. The application reference is stored in ``_app`` attribute.
  40. The settings dictionary is stored in ``_settings`` attribute and defaults
  41. to an empty dictionary.
  42. **Drawing**
  43. All the drawing on the card's surface should be done in the
  44. :py:meth:`pie_time.AbstractCard.tick` method. The method's implementation
  45. should be as fast as possible to avoid throttling the FPS down.
  46. **Resources**
  47. The :py:meth:`pie_time.AbstractCard.path_for_resource` method can be used
  48. to get an absolute path to a resource file. The card's resource folder
  49. should be placed along with the module containing the card's class.
  50. Name of the resource folder can be customized by overriding the
  51. :py:attr:`pie_time.AbstractCard.RESOURCE_FOLDER` attribute.
  52. """
  53. #: Name of the folder containing the resources
  54. RESOURCE_FOLDER = 'resources'
  55. def __init__(self):
  56. self._app = None
  57. self._settings = {}
  58. self._surface = None
  59. def set_app(self, app):
  60. """Binds the card with the *app*."""
  61. self._app = app
  62. def set_settings(self, settings):
  63. """Sets *settings* as the card's settings."""
  64. self._settings = settings
  65. @property
  66. def width(self):
  67. """The card's surface width. Defaults to the app screen's width."""
  68. return self._app.screen_size[0]
  69. @property
  70. def height(self):
  71. """The card's surface height. Defaults to the app screen's height."""
  72. return self._app.screen_size[1]
  73. @property
  74. def surface(self):
  75. """
  76. The cards surface. The surface width and height are defined by the
  77. respective properties of the class.
  78. """
  79. if self._surface is None:
  80. self._surface = pygame.surface.Surface((self.width, self.height))
  81. return self._surface
  82. @property
  83. def background_color(self):
  84. """
  85. The background color. Defaults to
  86. :py:attr:`pie_time.PieTime.BACKGROUND_COLOR`.
  87. """
  88. return self._settings.get(
  89. 'background_color', self._app.BACKGROUND_COLOR
  90. )
  91. def path_for_resource(self, resource, folder=None):
  92. """
  93. Returns an absolute path for *resource*. The optional *folder*
  94. keyword argument allows specifying a subpath.
  95. """
  96. _subpath = ''
  97. if folder:
  98. _subpath = folder
  99. module_path = sys.modules[self.__module__].__file__
  100. return os.path.join(
  101. os.path.abspath(os.path.dirname(module_path)),
  102. self.RESOURCE_FOLDER, _subpath, resource
  103. )
  104. def initialize(self):
  105. """
  106. Initializes the card.
  107. The application calls this method right after creating an instance of
  108. the class.
  109. This method can be used to perform additional initialization on the
  110. card, e.g. loading resources, setting the initial state etc.
  111. The default implementation does nothing.
  112. """
  113. pass
  114. def quit(self):
  115. """
  116. Initializes the card.
  117. This method can be used to perform additional cleanup on the
  118. card, e.g. stop threads, free resources etc.
  119. The default implementation does nothing.
  120. """
  121. def show(self):
  122. """
  123. Shows the card.
  124. The application calls this method each time the card becomes the
  125. current card.
  126. This method can be used to reset initial state, e.g. sprite positions.
  127. The default implementation does nothing.
  128. """
  129. pass
  130. def hide(self):
  131. """
  132. Hides the card.
  133. The application calls this method each time the card resignes the
  134. current card.
  135. This method can be used to e.g. stop threads which aren't supposed to
  136. be running when the card isn't being displayed.
  137. The default implementation does nothing.
  138. """
  139. pass
  140. def tick(self):
  141. """
  142. Ticks the card.
  143. The application calls this method on the current card in every main
  144. loop iteration.
  145. This method should be used to perform drawing and other operations
  146. needed to properly display the card on screen.
  147. Subclasses **must** override this method.
  148. """
  149. raise NotImplementedError('TODO')