
By: LogicalDash | Date: Jul 13 2015 14:07 | Format: Python | Expires: never | Size: 8.33 KB | Hits: 804
- from kivy.uix.floatlayout import FloatLayout
- from kivy.properties import BooleanProperty, StringProperty, NumericProperty, DictProperty, ObjectProperty, ListProperty, ReferenceListProperty
- class Card(FloatLayout):
- """A trading card, similar to the kind you use to play games like
- _Magic: the Gathering_.
- Its appearance is determined by several properties, the most
- important being:
- * ``headline_text``, a string to be shown at the top of the card;
- may be styled with eg. ``headline_font_name`` or
- ``headline_color``
- * ``art_source``, the path to an image to be displayed below the
- headline; may be hidden by setting ``show_art`` to ``False``
- * ``midline_text``, similar to ``headline_text`` but appearing
- below the art
- * ``text``, shown in a box the same size as the art. Styleable
- like ``headline_text`` and you can customize the box with
- eg. ``foreground_color`` and ``foreground_source``
- * ``footer_text``, like ``headline_text`` but at the bottom
- :class:`Card` is particularly useful when put in a
- :class:`DeckLayout`, allowing the user to drag cards in between
- any number of piles, into particular positions within a particular
- pile, and so forth.
- """
- dragging = BooleanProperty(False)
- deck = NumericProperty()
- idx = NumericProperty()
- ud = DictProperty({})
- collide_x = NumericProperty()
- collide_y = NumericProperty()
- collide_pos = ReferenceListProperty(collide_x, collide_y)
- foreground = ObjectProperty()
- foreground_source = StringProperty('')
- foreground_color = ListProperty([1, 1, 1, 1])
- foreground_image = ObjectProperty(None, allownone=True)
- foreground_texture = ObjectProperty(None, allownone=True)
- background_source = StringProperty('')
- background_color = ListProperty([.7, .7, .7, 1])
- background_image = ObjectProperty(None, allownone=True)
- background_texture = ObjectProperty(None, allownone=True)
- outline_color = ListProperty([0, 0, 0, 1])
- content_outline_color = ListProperty([0, 0, 0, 0])
- foreground_outline_color = ListProperty([0, 0, 0, 1])
- art_outline_color = ListProperty([0, 0, 0, 0])
- art = ObjectProperty()
- art_source = StringProperty('')
- art_color = ListProperty([1, 1, 1, 1])
- art_image = ObjectProperty(None, allownone=True)
- art_texture = ObjectProperty(None, allownone=True)
- show_art = BooleanProperty(True)
- headline = ObjectProperty()
- headline_text = StringProperty('Headline')
- headline_markup = BooleanProperty(True)
- headline_font_name = StringProperty('DroidSans')
- headline_font_size = NumericProperty(18)
- headline_color = ListProperty([0, 0, 0, 1])
- midline = ObjectProperty()
- midline_text = StringProperty('')
- midline_markup = BooleanProperty(True)
- midline_font_name = StringProperty('DroidSans')
- midline_font_size = NumericProperty(14)
- midline_color = ListProperty([0, 0, 0, 1])
- footer = ObjectProperty()
- footer_text = StringProperty('')
- footer_markup = BooleanProperty(True)
- footer_font_name = StringProperty('DroidSans')
- footer_font_size = NumericProperty(10)
- footer_color = ListProperty([0, 0, 0, 1])
- text = StringProperty('')
- text_color = ListProperty([0, 0, 0, 1])
- markup = BooleanProperty(True)
- font_name = StringProperty('DroidSans')
- font_size = NumericProperty(12)
- def on_background_source(self, *args):
- """When I get a new ``background_source``, load it as an
- :class:`Image` and store that in ``background_image``.
- """
- if self.background_source:
- self.background_image = Image(source=self.background_source)
- def on_background_image(self, *args):
- """When I get a new ``background_image``, store its texture in
- ``background_texture``.
- """
- if self.background_image is not None:
- self.background_texture = self.background_image.texture
- def on_foreground_source(self, *args):
- """When I get a new ``foreground_source``, load it as an
- :class:`Image` and store that in ``foreground_image``.
- """
- if self.foreground_source:
- self.foreground_image = Image(source=self.foreground_source)
- def on_foreground_image(self, *args):
- """When I get a new ``foreground_image``, store its texture in my
- ``foreground_texture``.
- """
- if self.foreground_image is not None:
- self.foreground_texture = self.foreground_image.texture
- def on_art_source(self, *args):
- """When I get a new ``art_source``, load it as an :class:`Image` and
- store that in ``art_image``.
- """
- if self.art_source:
- self.art_image = Image(source=self.art_source)
- def on_art_image(self, *args):
- """When I get a new ``art_image``, store its texture in
- ``art_texture``.
- """
- if self.art_image is not None:
- self.art_texture = self.art_image.texture
- def on_touch_down(self, touch):
- """If I'm the first card to collide this touch, grab it, store my
- metadata in its userdict, and store the relative coords upon
- me where the collision happened.
- """
- if not self.collide_point(*touch.pos):
- return
- if 'card' in touch.ud:
- return
- touch.grab(self)
- self.dragging = True
- touch.ud['card'] = self
- touch.ud['idx'] = self.idx
- touch.ud['deck'] = self.deck
- touch.ud['layout'] = self.parent
- self.collide_x = touch.x - self.x
- self.collide_y = touch.y - self.y
- def on_touch_move(self, touch):
- """If I'm being dragged, move so as to be always positioned the same
- relative to the touch.
- """
- if not self.dragging:
- touch.ungrab(self)
- return
- self.pos = (
- touch.x - self.collide_x,
- touch.y - self.collide_y
- )
- def on_touch_up(self, touch):
- """Stop dragging if needed."""
- if not self.dragging:
- return
- touch.ungrab(self)
- self.dragging = False
- def copy(self):
- """Return a new :class:`Card` just like me."""
- d = {}
- for att in (
- 'deck',
- 'idx',
- 'ud',
- 'foreground_source',
- 'foreground_color',
- 'foreground_image',
- 'foreground_texture',
- 'background_source',
- 'background_color',
- 'background_image',
- 'background_texture',
- 'outline_color',
- 'content_outline_color',
- 'foreground_outline_color',
- 'art_outline_color',
- 'art_source',
- 'art_color',
- 'art_image',
- 'art_texture',
- 'show_art',
- 'headline_text',
- 'headline_markup',
- 'headline_font_name',
- 'headline_font_size',
- 'headline_color',
- 'midline_text',
- 'midline_markup',
- 'midline_font_name',
- 'midline_font_size',
- 'midline_color',
- 'footer_text',
- 'footer_markup',
- 'footer_font_name',
- 'footer_font_size',
- 'footer_color',
- 'text',
- 'text_color',
- 'markup',
- 'font_name',
- 'font_size'
- ):
- v = getattr(self, att)
- if v is not None:
- d[att] = v
- return Card(**d)
- kwargs = {'size': (1, 1), 'headline_text': 'always', 'show_art': False, 'text': "def always(engine, character, thing):\n assert(thing['shrub_places'])\n return True", 'midline_text': 'Trigger', 'ud': {'type': 'trigger', 'funcname': 'always'}, 'show_footer': False}
- Card(**kwargs) # TypeError: object.__init__() takes no parameters
Latest pastes
2 hours ago
1 days ago
1 days ago
2 days ago
3 days ago