Exemple #1
0
 def __init__(self, **kwargs):
     self._last_reload_key = None
     self._do_animation = kwargs.get('do_animation', True)
     self._do_interaction = kwargs.get('do_interaction', True)
     kwargs.setdefault('font_size', sp(WORD_FONTSIZE))
     super(Word, self).__init__(**kwargs)
     if self.provider is None:
         if 'word' in kwargs:
             self.provider = WordProvider(kwargs['word'])
     self.reload()
Exemple #2
0
 def load(self, data):
     assert(isinstance(data, dict))
     self.provider = WordProvider(data['word'])
     self.provider.load_settings(data['settings'])
     self.reload()
Exemple #3
0
class Word(Button):
    padding = NumericProperty('20dp')
    wordid = StringProperty()
    screen = ObjectProperty()
    word = ObjectProperty()

    antonym = BooleanProperty(False)
    zoom = NumericProperty(0)

    fontzoom = NumericProperty(0)
    direction = NumericProperty(0)

    provider = ObjectProperty()
    wordtype = NumericProperty(0)
    text = StringProperty()

    def __init__(self, **kwargs):
        self._last_reload_key = None
        self._do_animation = kwargs.get('do_animation', True)
        self._do_interaction = kwargs.get('do_interaction', True)
        kwargs.setdefault('font_size', sp(WORD_FONTSIZE))
        super(Word, self).__init__(**kwargs)
        if self.provider is None:
            if 'word' in kwargs:
                self.provider = WordProvider(kwargs['word'])
        self.reload()

    def dump(self):
        provider = self.provider
        return { 'word': self.word, 'settings': provider.dump_settings() }

    def load(self, data):
        assert(isinstance(data, dict))
        self.provider = WordProvider(data['word'])
        self.provider.load_settings(data['settings'])
        self.reload()

    def reload(self, *args):
        provider = self.provider
        if not provider:
            return
        text = provider.get()
        wordtype = provider.itype
        antonym = provider.antonym
        zoom = provider.zoom

        key = provider.dump_settings()
        if self._last_reload_key == key:
            return
        self._last_reload_key = key

        self.text = text
        self.wordtype = wordtype
        self.antonym = antonym
        self.zoom = zoom

        if self._do_animation:
            Animation(
                    fontzoom=self.zoom * sp(5),
                    padding=max(dp(20), dp(20) - self.zoom * dp(10)),
                    direction=(provider.tense - 1) * dp(5),
                    d=0.3, t='out_quart').start(self)
        else:
            self.fontzoom = self.zoom * sp(5)
            self.padding = max(dp(20), dp(20) - self.zoom * dp(10))
            self.direction = (provider.tense - 1) * dp(5)

    def on_touch_down(self, touch):
        self._last_touch = touch
        return super(Word, self).on_touch_down(touch)

    def on_touch_move(self, touch):
        return super(Word, self).on_touch_move(touch)

    def on_press(self, *args):
        if self.__class__ is not Word:
            return
        if not self._do_interaction:
            return
        touch = self._last_touch
        x, y = self.to_window(touch.x, touch.y)
        dw = DroppableWord(word=self.word,
            wordid=self.wordid, screen=self.screen, center=(x, y),
            last_touch=self._last_touch)
        touch.grab(dw)
        self._dw = dw
        self.screen.root_layout.add_widget(dw)

    def on_release(self, *args):
        if self.__class__ is not Word:
            return
        if not self._do_interaction:
            return
        t = self._last_touch
        d = Vector(t.pos).distance(t.opos)
        dt = t.time_update - t.time_start
        # if the user moved the touch more than 20 pixels away of the initial
        # position, or if he touched more than 200ms, abort.
        if d > 20 or dt > 0.2:
            return
        self.screen.open_gesture_selection(self)