Public paste
FloatLayout subclass causes Widget instantiation TypeError
By: LogicalDash | Date: Jul 13 2015 14:09 | Format: Python | Expires: never | Size: 11.17 KB | Hits: 831

  1. from kivy.uix.floatlayout import FloatLayout
  2. from kivy.properties import BooleanProperty, StringProperty, NumericProperty, DictProperty, ObjectProperty, ListProperty, ReferenceListProperty
  3. from kivy.lang import Builder
  4.  
  5.  
  6. class Card(FloatLayout):
  7.     """A trading card, similar to the kind you use to play games like
  8.    _Magic: the Gathering_.
  9.  
  10.    Its appearance is determined by several properties, the most
  11.    important being:
  12.  
  13.    * ``headline_text``, a string to be shown at the top of the card;
  14.      may be styled with eg. ``headline_font_name`` or
  15.      ``headline_color``
  16.  
  17.    * ``art_source``, the path to an image to be displayed below the
  18.      headline; may be hidden by setting ``show_art`` to ``False``
  19.  
  20.    * ``midline_text``, similar to ``headline_text`` but appearing
  21.      below the art
  22.  
  23.    * ``text``, shown in a box the same size as the art. Styleable
  24.      like ``headline_text`` and you can customize the box with
  25.      eg. ``foreground_color`` and ``foreground_source``
  26.  
  27.    * ``footer_text``, like ``headline_text`` but at the bottom
  28.  
  29.    :class:`Card` is particularly useful when put in a
  30.    :class:`DeckLayout`, allowing the user to drag cards in between
  31.    any number of piles, into particular positions within a particular
  32.    pile, and so forth.
  33.  
  34.    """
  35.     dragging = BooleanProperty(False)
  36.     deck = NumericProperty()
  37.     idx = NumericProperty()
  38.     ud = DictProperty({})
  39.  
  40.     collide_x = NumericProperty()
  41.     collide_y = NumericProperty()
  42.     collide_pos = ReferenceListProperty(collide_x, collide_y)
  43.  
  44.     foreground = ObjectProperty()
  45.     foreground_source = StringProperty('')
  46.     foreground_color = ListProperty([1, 1, 1, 1])
  47.     foreground_image = ObjectProperty(None, allownone=True)
  48.     foreground_texture = ObjectProperty(None, allownone=True)
  49.  
  50.     background_source = StringProperty('')
  51.     background_color = ListProperty([.7, .7, .7, 1])
  52.     background_image = ObjectProperty(None, allownone=True)
  53.     background_texture = ObjectProperty(None, allownone=True)
  54.  
  55.     outline_color = ListProperty([0, 0, 0, 1])
  56.     content_outline_color = ListProperty([0, 0, 0, 0])
  57.     foreground_outline_color = ListProperty([0, 0, 0, 1])
  58.     art_outline_color = ListProperty([0, 0, 0, 0])
  59.  
  60.     art = ObjectProperty()
  61.     art_source = StringProperty('')
  62.     art_color = ListProperty([1, 1, 1, 1])
  63.     art_image = ObjectProperty(None, allownone=True)
  64.     art_texture = ObjectProperty(None, allownone=True)
  65.     show_art = BooleanProperty(True)
  66.  
  67.     headline = ObjectProperty()
  68.     headline_text = StringProperty('Headline')
  69.     headline_markup = BooleanProperty(True)
  70.     headline_font_name = StringProperty('DroidSans')
  71.     headline_font_size = NumericProperty(18)
  72.     headline_color = ListProperty([0, 0, 0, 1])
  73.  
  74.     midline = ObjectProperty()
  75.     midline_text = StringProperty('')
  76.     midline_markup = BooleanProperty(True)
  77.     midline_font_name = StringProperty('DroidSans')
  78.     midline_font_size = NumericProperty(14)
  79.     midline_color = ListProperty([0, 0, 0, 1])
  80.  
  81.     footer = ObjectProperty()
  82.     footer_text = StringProperty('')
  83.     footer_markup = BooleanProperty(True)
  84.     footer_font_name = StringProperty('DroidSans')
  85.     footer_font_size = NumericProperty(10)
  86.     footer_color = ListProperty([0, 0, 0, 1])
  87.  
  88.     text = StringProperty('')
  89.     text_color = ListProperty([0, 0, 0, 1])
  90.     markup = BooleanProperty(True)
  91.     font_name = StringProperty('DroidSans')
  92.     font_size = NumericProperty(12)
  93.  
  94.     def on_background_source(self, *args):
  95.         """When I get a new ``background_source``, load it as an
  96.        :class:`Image` and store that in ``background_image``.
  97.  
  98.        """
  99.         if self.background_source:
  100.             self.background_image = Image(source=self.background_source)
  101.  
  102.     def on_background_image(self, *args):
  103.         """When I get a new ``background_image``, store its texture in
  104.        ``background_texture``.
  105.  
  106.        """
  107.         if self.background_image is not None:
  108.             self.background_texture = self.background_image.texture
  109.  
  110.     def on_foreground_source(self, *args):
  111.         """When I get a new ``foreground_source``, load it as an
  112.        :class:`Image` and store that in ``foreground_image``.
  113.  
  114.        """
  115.         if self.foreground_source:
  116.             self.foreground_image = Image(source=self.foreground_source)
  117.  
  118.     def on_foreground_image(self, *args):
  119.         """When I get a new ``foreground_image``, store its texture in my
  120.        ``foreground_texture``.
  121.  
  122.        """
  123.         if self.foreground_image is not None:
  124.             self.foreground_texture = self.foreground_image.texture
  125.  
  126.     def on_art_source(self, *args):
  127.         """When I get a new ``art_source``, load it as an :class:`Image` and
  128.        store that in ``art_image``.
  129.  
  130.        """
  131.         if self.art_source:
  132.             self.art_image = Image(source=self.art_source)
  133.  
  134.     def on_art_image(self, *args):
  135.         """When I get a new ``art_image``, store its texture in
  136.        ``art_texture``.
  137.  
  138.        """
  139.         if self.art_image is not None:
  140.             self.art_texture = self.art_image.texture
  141.  
  142.     def on_touch_down(self, touch):
  143.         """If I'm the first card to collide this touch, grab it, store my
  144.        metadata in its userdict, and store the relative coords upon
  145.        me where the collision happened.
  146.  
  147.        """
  148.         if not self.collide_point(*touch.pos):
  149.             return
  150.         if 'card' in touch.ud:
  151.             return
  152.         touch.grab(self)
  153.         self.dragging = True
  154.         touch.ud['card'] = self
  155.         touch.ud['idx'] = self.idx
  156.         touch.ud['deck'] = self.deck
  157.         touch.ud['layout'] = self.parent
  158.         self.collide_x = touch.x - self.x
  159.         self.collide_y = touch.y - self.y
  160.  
  161.     def on_touch_move(self, touch):
  162.         """If I'm being dragged, move so as to be always positioned the same
  163.        relative to the touch.
  164.  
  165.        """
  166.         if not self.dragging:
  167.             touch.ungrab(self)
  168.             return
  169.         self.pos = (
  170.             touch.x - self.collide_x,
  171.             touch.y - self.collide_y
  172.         )
  173.  
  174.     def on_touch_up(self, touch):
  175.         """Stop dragging if needed."""
  176.         if not self.dragging:
  177.             return
  178.         touch.ungrab(self)
  179.         self.dragging = False
  180.  
  181.     def copy(self):
  182.         """Return a new :class:`Card` just like me."""
  183.         d = {}
  184.         for att in (
  185.                 'deck',
  186.                 'idx',
  187.                 'ud',
  188.                 'foreground_source',
  189.                 'foreground_color',
  190.                 'foreground_image',
  191.                 'foreground_texture',
  192.                 'background_source',
  193.                 'background_color',
  194.                 'background_image',
  195.                 'background_texture',
  196.                 'outline_color',
  197.                 'content_outline_color',
  198.                 'foreground_outline_color',
  199.                 'art_outline_color',
  200.                 'art_source',
  201.                 'art_color',
  202.                 'art_image',
  203.                 'art_texture',
  204.                 'show_art',
  205.                 'headline_text',
  206.                 'headline_markup',
  207.                 'headline_font_name',
  208.                 'headline_font_size',
  209.                 'headline_color',
  210.                 'midline_text',
  211.                 'midline_markup',
  212.                 'midline_font_name',
  213.                 'midline_font_size',
  214.                 'midline_color',
  215.                 'footer_text',
  216.                 'footer_markup',
  217.                 'footer_font_name',
  218.                 'footer_font_size',
  219.                 'footer_color',
  220.                 'text',
  221.                 'text_color',
  222.                 'markup',
  223.                 'font_name',
  224.                 'font_size'
  225.         ):
  226.             v = getattr(self, att)
  227.             if v is not None:
  228.                 d[att] = v
  229.         return Card(**d)
  230.  
  231.  
  232. Builder.load_string("""
  233. <Card>:
  234.    headline: headline
  235.    midline: midline
  236.    footer: footer
  237.    art: art
  238.    foreground: foreground
  239.    canvas:
  240.        Color:
  241.            rgba: root.background_color
  242.        Rectangle:
  243.            texture: root.background_texture
  244.            pos: root.pos
  245.            size: root.size
  246.        Color:
  247.            rgba: root.outline_color
  248.        Line:
  249.            points: [self.x, self.y, self.right, self.y, self.right, self.top, self.x, self.top, self.x, self.y]
  250.        Color:
  251.            rgba: [1, 1, 1, 1]
  252.    BoxLayout:
  253.        size_hint: 0.9, 0.9
  254.        pos_hint: {'x': 0.05, 'y': 0.05}
  255.        orientation: 'vertical'
  256.        canvas:
  257.            Color:
  258.                rgba: root.content_outline_color
  259.            Line:
  260.                points: [self.x, self.y, self.right, self.y, self.right, self.top, self.x, self.top, self.x, self.y]
  261.            Color:
  262.                rgba: [1, 1, 1, 1]
  263.        Label:
  264.            id: headline
  265.            text: root.headline_text
  266.            markup: root.headline_markup
  267.            font_name: root.headline_font_name
  268.            font_size: root.headline_font_size
  269.            color: root.headline_color
  270.            size_hint: (None, None)
  271.            size: self.texture_size
  272.        ColorTextureBox:
  273.            id: art
  274.            color: root.art_color
  275.            texture: root.art_texture
  276.            outline_color: root.art_outline_color if root.show_art else [0, 0, 0, 0]
  277.            size_hint: (1, 1) if root.show_art else (None, None)
  278.            size: (0, 0)
  279.        Label:
  280.            id: midline
  281.            text: root.midline_text
  282.            markup: root.midline_markup
  283.            font_name: root.midline_font_name
  284.            font_size: root.midline_font_size
  285.            color: root.midline_color
  286.            size_hint: (None, None)
  287.            size: self.texture_size
  288.        ColorTextureBox:
  289.            id: foreground
  290.            color: root.foreground_color
  291.            outline_color: root.foreground_outline_color
  292.            texture: root.foreground_texture
  293.            Label:
  294.                text: root.text
  295.                color: root.text_color
  296.                markup: root.markup
  297.                font_name: root.font_name
  298.                font_size: root.font_size
  299.                text_size: foreground.size
  300.                size_hint: (None, None)
  301.                size: self.texture_size
  302.                pos: foreground.pos
  303.                valign: 'top'
  304.        Label:
  305.            id: footer
  306.            text: root.footer_text
  307.            markup: root.footer_markup
  308.            font_name: root.footer_font_name
  309.            font_size: root.footer_font_size
  310.            color: root.footer_color
  311.            size_hint: (None, None)
  312.            size: self.texture_size
  313. """)
  314.  
  315.  
  316. 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}
  317.  
  318. Card(**kwargs)  # TypeError: object.__init__() takes no parameters