omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello β€” I'm @olemoritz on Twitter.


    Pure Python gestures

    Pythonista
    8
    65
    21733
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Anxietier
      Anxietier last edited by

      thx for share ur code, it's really useful.
      i was try to overload view class, so i can zoom imageview and drag it, like photo album, here is my problem:
      when i use 2 fingers to drag imageview then pinch it, it's ok,
      but when i pinch it at first, the imageview's left-top corner will be fixed for a while, as i pinch more scale, the imageview would jump under my fingers, then when i drag and pinch, the experience would be ok
      i dont know am i describe it clearly, here is my demo code

      import gestures
      import ui
      
      
      class MyView(ui.View):
      	def __init__(self):
      		self.width, self.height = ui.get_screen_size()
      		self.background_color = 'white'
      		
      		self.set_ui()
      		self.set_label_action()
      		
      		self.label_init_center = self.label.center
      		self.label_init_width = self.label.width
      		self.label_init_height =  self.label.height
      		
      	def set_ui(self):
      		label = ui.Label()
      		self.label = label
      		label.text = 'Demo'
      		label.border_color = 'black'
      		label.border_width = 1
      		label.width, label.height = 400, 400
      		label.center = self.width/2, self.height/2
      		self.add_subview(label)
      		
      	def set_label_action(self):
      		pincher = gestures.pinch(self.label, self.pinch_handler)
      		panner = gestures.pan(self.label, self.pan_handler, minimum_number_of_touches=2)
      		panner.together_with(pincher)
      		
      	def pan_handler(self, data): 
      		self.label.center = self.label_init_center + data.translation
      		if self.label.text == '2 fingers are pinching':
      			self.label.text += 'and panning'
      		if self.label.text == 'Demo':
      			self.label.text = '2 fingers are panning'
      		if data.state == gestures.ENDED:
      			self.label.text = 'Demo'
      			self.label_init_center = self.label.center
      
      	def pinch_handler(self, data): 
      		self.label.width, self.label.height = self.label_init_width * data.scale, self.label_init_height * data.scale
      		if self.label.text == '2 fingers are panning':
      			self.label.text += ' and pinching'
      		if self.label.text == 'Demo':
      			self.label.text = '2 fingers are pinching'
      		if data.state == gestures.ENDED:
      			self.label.text = 'Demo'
      			self.label_init_width = self.label.width
      			self.label_init_height = self.label.height
      
      		
      if __name__ == "__main__":
      	mView = MyView()
      	mView.present('fullscreen', hide_title_bar = True)```
      stephen mikael 2 Replies Last reply Reply Quote 0
      • stephen
        stephen @Anxietier last edited by

        @Anxietier hey ive never dealt with gestures before but i imagin giving each movment a delegate to register separatly then get the diference of the 'onEvent' call (sorry just made that one up lol) would this posibly help? might need separate Gesture objectsbinstead of calling all from one view. not sure how that would work

        Anxietier 1 Reply Last reply Reply Quote 0
        • Anxietier
          Anxietier @stephen last edited by

          @stephen im not sure understand whats ur meaning (not good at eng..) :
          make one label, when i swipe it up\down\left\right, or drag it, or zoom it, etc.. it can do diff things
          did u mean that? if so, then yes
          btw, if u wanna swipe 4 directions with one handle, u can write like this:
          gestures.RIGHT|gestures.LEFT|gestures.UP|gestures.DOWN

          stephen 1 Reply Last reply Reply Quote 0
          • mikael
            mikael @Anxietier last edited by

            @Anxietier, can you subclass ZoomPanView instead?

            If you run pygestures.py and swipe from the right, you get the demo picture that you can pan and zoom, and that seems to work ok.

            The code for it is at the end of the file.

            Anxietier 1 Reply Last reply Reply Quote 1
            • stephen
              stephen @Anxietier last edited by

              @Anxietier okay i was in a bit of a rush before and didnt visualize fully.

              I have not looked at @mikael 's module yet but givin who authored it im sure ite outstsnding. that said my first post is no longer valid.. πŸ˜…πŸ˜‰



              as for your original issue i went to photos and played with the pinch/pan for a bit and i noticed that if i pinched from corners ↴

              good

              everythingbworked fine. BUT if one finger wasnt close enough to corner ↴

              bad

              the gester would act as though it had constraints.

              if i understand your problem then possibly these are similarbin effect and that should mean increasing the "touch" target rect size would help prevent this?

              Note: inused photos App givin u wanted to Mock functionality β˜ΊοΈπŸ€“

              Anxietier 1 Reply Last reply Reply Quote 0
              • Anxietier
                Anxietier @mikael last edited by

                @mikael hi, i tested zoomPanDemo, and it works fine, thanks for advice.
                btw, im trying to make an effect that when i drag down PanView, it can make me feel damping, as my finger move away, it can get back to origin pos, feel like looking website, drag down for refresh
                could you provide any idea for that? thank you

                stephen 1 Reply Last reply Reply Quote 0
                • Anxietier
                  Anxietier @stephen last edited by

                  @stephen thx for reply
                  ive tryed mikael's zoomPanDemo, add it to my main View, and it works fine, so the issue is on mine :(
                  i would subclass ZoomPanView, and try it :D

                  1 Reply Last reply Reply Quote 0
                  • stephen
                    stephen @Anxietier last edited by

                    @Anxietier no problem. i looked over the moduleand it looks like all uwould need to do is run a ui.animate transgorm back yto orignal psition.if touch doesnt store the original long enough (it should) you can also store this in a var. should be straight forward butd have to run through this module a bit beore i can give an example.

                    Anxietier 1 Reply Last reply Reply Quote 0
                    • Anxietier
                      Anxietier @stephen last edited by

                      @stephen
                      yeah, i made a demo, i subclass ui.view, add ui,imageview to show image, and i def a method to fix imageview_size to view_size, include view_center, when i drag down imageview and leave my finger, it can move back to origin pos,
                      but, i need an experience, i'd try to describe it:
                      '' when i drag it down shortly, imageview would move down follow my finger,
                      when my finger drag long way, like from top screen to bottom, the imagview wouldnt follow my finger, feel like dragging a strong
                      spring ''
                      i try to find a math function to let imageview and fingers' trave non-linear, but i failed, so ... u know :P

                      stephen mikael 2 Replies Last reply Reply Quote 0
                      • stephen
                        stephen @Anxietier last edited by

                        @Anxietier πŸ€“ ah math now i can help u. give me a few min to write up a friction/drag method for you

                        1 Reply Last reply Reply Quote 0
                        • mikael
                          mikael @Anxietier last edited by

                          @Anxietier, please see below for something you can adjust to your needs.

                          I recommend and use scripter, because it makes the animations easy and includes the ease functions.

                          import ui
                          import scripter
                          
                          v = ui.View(background_color='black')
                          
                          class Pulled(ui.View):
                              
                              straight_pull = 100
                              slowing_pull = 100
                              
                              def touch_began(self, t):
                                  self.start_y = ui.convert_point(t.location, from_view=self).y
                                  
                              def touch_moved(self, t):
                                  delta_y = (
                                      ui.convert_point(t.location, from_view=self).y - 
                                      ui.convert_point(t.prev_location, from_view=self).y)
                                  if self.y < self.straight_pull:
                                      self.y += delta_y
                                      return
                                  if self.y < (self.straight_pull + self.slowing_pull):
                                      diff = self.y-self.straight_pull
                                      diff_fraction = diff/self.slowing_pull
                                      effective = 1 - min(1, diff_fraction) ** 2
                                      self.y += delta_y * effective
                                  
                              def touch_ended(self, t):
                                  scripter.y(self, 0, ease_func=scripter.ease_out)
                          
                          v.add_subview(Pulled(
                              frame=v.bounds,
                              flex='WH',
                              background_color='green'))
                          
                          v.present('fullscreen')
                          
                          Anxietier 1 Reply Last reply Reply Quote 0
                          • Anxietier
                            Anxietier @mikael last edited by

                            @mikael
                            thank you, when i ran ur code, it post me an error:
                            scripter.y(self, 0, ease_func=scripter.ease_out)
                            module 'scripter' has no attribute 'y'

                            then i check scripter.py, i only found the vector_class has a method called y(...)
                            im confused~

                            Anxietier 1 Reply Last reply Reply Quote 0
                            • Anxietier
                              Anxietier @Anxietier last edited by

                              @Anxietier @mikael
                              oh, never mind, i restarted app, now its wording fine :D

                              mikael 2 Replies Last reply Reply Quote 0
                              • mikael
                                mikael @Anxietier last edited by

                                @Anxietier, and a version with a revealed label and a refresh trigger:

                                import ui
                                import scripter
                                
                                v = ui.View(background_color='black')
                                
                                class Pulled(ui.View):
                                    
                                    straight_pull = 30
                                    slowing_pull = 80
                                    
                                    def touch_began(self, t):
                                        self.start_y = ui.convert_point(t.location, from_view=self).y
                                        
                                    def touch_moved(self, t):
                                        delta_y = (
                                            ui.convert_point(t.location, from_view=self).y - 
                                            ui.convert_point(t.prev_location, from_view=self).y)
                                        if self.y < self.straight_pull:
                                            self.y += delta_y
                                            return
                                        if self.y < (self.straight_pull + self.slowing_pull):
                                            diff = self.y-self.straight_pull
                                            diff_fraction = diff/self.slowing_pull
                                            try:
                                                self.reveal_func(diff_fraction)
                                            except AttributeError: pass
                                            effective = 1 - min(1, diff_fraction) ** 2
                                            self.y += delta_y * effective
                                            
                                        
                                    def touch_ended(self, t):
                                        scripter.y(self, 0, ease_func=scripter.ease_out)
                                        try:
                                            self.reveal_func(0)
                                        except AttributeError: pass
                                        if self.y > self.straight_pull:
                                            self.trigger_func()
                                
                                label = ui.Label(text='Release to refresh',
                                    text_color='white',
                                    flex='LBR',
                                    alpha=0,
                                )
                                label.size_to_fit()
                                label.center = v.bounds.center()
                                label.y = 8
                                v.add_subview(label)
                                
                                def reveal(fraction):
                                    label.alpha = fraction
                                    
                                def trigger():
                                    scripter.hide(label)
                                    print('refresh')
                                
                                v.add_subview(Pulled(
                                    reveal_func=reveal,
                                    trigger_func=trigger,
                                    frame=v.bounds,
                                    flex='WH',
                                    background_color='green'))
                                
                                v.present('fullscreen')
                                
                                Anxietier 1 Reply Last reply Reply Quote 0
                                • mikael
                                  mikael @Anxietier last edited by

                                  @Anxietier, oh, you can throw away the touch_began method, it’s from an earlier version.

                                  Anxietier 1 Reply Last reply Reply Quote 0
                                  • Anxietier
                                    Anxietier @mikael last edited by

                                    @mikael thank you
                                    and if i want to bind it with gestures, what should i do
                                    i mean is there any conflict between gestures and touch
                                    what i want to make is, i can use twin fingers to zoom and move picture(imageview or subclass zoompanview, later is better i guess), and when i move picture, move space and scale would be limited, as i leave my fingers, picture's edge should be forced move to superview's edge

                                    1 Reply Last reply Reply Quote 0
                                    • Anxietier
                                      Anxietier @mikael last edited by

                                      @mikael ah, dont bother
                                      i didnt read doc carefully(perhaps wrong word :P), now i can bind them together, thanks for ur help, ur codes are really amazing and helpful :D

                                      mikael stephen 2 Replies Last reply Reply Quote 0
                                      • mikael
                                        mikael @Anxietier last edited by

                                        @Anxietier, little optimization of the ”feel” and code simplification.

                                        import ui
                                        import scripter
                                        
                                        v = ui.View(background_color='black')
                                        
                                        class Pulled(ui.View):
                                            
                                            trigger_distance = 30
                                            pull_distance = 80
                                            
                                            def touch_moved(self, t):
                                                delta_y = (
                                                    ui.convert_point(t.location, from_view=self).y - 
                                                    ui.convert_point(t.prev_location, from_view=self).y)
                                                diff_fraction = self.y/self.pull_distance
                                                try:
                                                    self.reveal_func(diff_fraction)
                                                except AttributeError: pass
                                                self.y += delta_y * (1 - min(0.9, diff_fraction) ** 2)
                                                self.y = max(0, self.y)
                                                
                                            def touch_ended(self, t):
                                                scripter.y(self, 0, ease_func=scripter.ease_out)
                                                try:
                                                    self.reveal_func(0)
                                                except AttributeError: pass
                                                if self.y > self.trigger_distance:
                                                    self.trigger_func()
                                        
                                        label = ui.Label(text='Release to refresh',
                                            text_color='white',
                                            flex='LBR',
                                            alpha=0,
                                        )
                                        label.size_to_fit()
                                        label.center = v.bounds.center()
                                        label.y = 8
                                        v.add_subview(label)
                                        
                                        def reveal(fraction):
                                            label.alpha = fraction
                                            
                                        def trigger():
                                            scripter.hide(label)
                                            print('refresh')
                                        
                                        v.add_subview(Pulled(
                                            reveal_func=reveal,
                                            trigger_func=trigger,
                                            frame=v.bounds,
                                            flex='WH',
                                            background_color='green'))
                                        
                                        v.present('fullscreen')
                                        
                                        Anxietier 1 Reply Last reply Reply Quote 1
                                        • stephen
                                          stephen @Anxietier last edited by stephen

                                          !!! Removed by poster to not confuse future readers with incorrect Code Examples.. πŸ€“πŸ˜‰

                                          Anxietier 1 Reply Last reply Reply Quote 0
                                          • Anxietier
                                            Anxietier @mikael last edited by

                                            @mikael
                                            thanks for help, i make it
                                            but there's another problem showed up:
                                            i make a main view, then i subclass zoomPanView and add to mainView , after that i add imageView as zoomPanView's subVIew, like what zoomPanDemo did, when i overwrite on_pan method and test it, data.began and data.changed can give me right value, but data.ended not, i dont know why

                                            Anxietier 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors