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
    21860
    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.
    • stephen
      stephen last edited by

      @mikael i usually do great at dealing with numbers... after a couple days of this i seem to have made a circle back to where i started... it might be that i dont understand Sigma or i may be using the wrong function all together... i even tried generating a logarithmic table then iterating the values to get drag to stop movment lol. any chance you @cvp @JonB or @ccc know a non built-in way to do this? here is where my testing code as it was when i stopped to post this... sorry the codes messy..

      
      import numpy as np
      import math
      from scene import *
      w, h=get_screen_size()
      
      def Distance(pos1, pos2):
      	x1, y1=pos1
      	x2, y2=pos2
      	dx, dy=x1-x2, y1-y2
      	return math.sqrt(dx*dx + dy*dy)
      
      def Force(pos1, pos2, k):
      	x1, y1=pos1
      	x2, y2=pos2
      	return Distance(pos1, pos2)*k#/1*(1+np.exp(1))**k
      
      def Direction(pos1, pos2):
      	x1, y1=pos1
      	x2, y2=pos2
      	dx=(x1-x2)/Distance(pos1,pos2)
      	dy=(y1-y2)/Distance(pos1,pos2)
      	return Point(dx, dy)
      	
      class main (Scene):
      	def setup(self):
      		self.isDragging = False
      		self.drag_T=0
      		self.i=0
      
      		self.touch_loc=(0, 0)
      		self.ticks=0
      		self.tick_rate=0.01
      		self.origin_pos=Point(w/2, 64)
      		
      		self.subject=SpriteNode(
      			texture=None, color=(0.0, 1.0, 0.0), size=Size(64, 64),
      			position=self.origin_pos, anchor_point=(0.5, 0.5), parent=self)
      		self.sub_data=LabelNode(
      			text=f'(◕_◕)', font=('Menlo', 22), anchor_point=(0.5, 0.5),
      			color=(0.0, 0.0, 0.0, 1.0), parent=self.subject)
      		self.data=LabelNode(
      			text="", font=('Menlo', 14), anchor_point=(0.0, 0.0), color=(1.0, 1.0, 1.0),
      			position=Point(150,512), size=Size(256, 512), parent=self)
      								
      	
      	def update(self):
      		w, h=get_screen_size()
      		self.i+=self.dt
      		self.ticks+=1
      		self.drag_T= (self.drag_T+1) if self.isDragging else 0
      		d_o=Distance(self.subject.position, self.origin_pos)
      		d_s=Distance(self.subject.position, self.touch_loc)
      		d_t=Distance(self.origin_pos, self.touch_loc)
      		max=Distance((w/2, h/3), self.subject.position)
      		target_dir=Direction(self.touch_loc, self.subject.position)
      		f=Force(self.origin_pos, self.touch_loc, self.drag_T)
      		damp=(1/(1+np.exp(-max)))
      		
      		
      		if self.isDragging:
      			w, h=get_screen_size()
      			x, y=self.subject.position
      			xx, yy=self.touch_loc
      			tx, ty=target_dir
      			if max > 1:
      				self.subject.position=(x+tx, y+ty+max)
      			
      		self.DataUpdate(d_t, d_s, d_o, max, f, target_dir)
      			
      		self.i=0
      	def DataUpdate(self, d_t_o, d_s_t, d_s_o, max, f, t_dir):
      		self.data.text= (f'Ticks: {self.ticks}\n'+
      						f'Dist from Max {max}\n'+
      						f'Force/Friction: {f}\n'+
      						f'Drag Time: {self.drag_T}\n'+
      						f'isDragging: {self.isDragging}\n'+
      						f'Target Direction: {t_dir}\n'+
      						f'Subject position: {self.subject.position}\n'+
      						f'Touch location: {self.touch_loc}\n'+
      						f'Origin: {self.origin_pos}\n\n'+
      						f'Distance - Touch→Origin: {d_t_o}\n'+
      						f'Distance - Subject→Origin: {d_s_o}\n'+
      						f'Distance - subject→Touch: {d_s_t}\n')
      		
      	def touch_began(self, touch):
      		self.isDragging = True
      		self.touch_loc=touch.location
      	
      	def touch_moved(self, touch):
      		tx, ty=touch.location
      		sx, sy=self.subject.position
      		self.touch_loc=touch.location
      		
      	def touch_ended(self, touch):
      		self.isDragging = False
      		#self.touch_loc=(0, 0)
      		#self.direction=(0, 0)
      		self.drag_T=0
      		self.subject.position=self.origin_pos
      run(main())
      		
      	
      
      cvp 2 Replies Last reply Reply Quote 1
      • cvp
        cvp @stephen last edited by

        @stephen I'm sincerely sorry but, perhaps/obviously due to my poor English, I don't understand what you want to do with this script.

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

          @stephen first remark, setting tx,ty,sx,sy is useless, isn't it?

              def touch_moved(self, touch):
                  tx, ty=touch.location
                  sx, sy=self.subject.position
                  self.touch_loc=touch.location 
          
          stephen 1 Reply Last reply Reply Quote 1
          • stephen
            stephen @cvp last edited by stephen

            @cvp yes very much so in is what i posted. when i test i use alot of exlusive calls to insure control to eliminate inclusive bug or errors then when i achieve outcome expected i remove exclusive properties one at a time . just a metegation process i developed over the years. i should have mentioned this im sorry for the confusion. lol ☺️ the one your speaking of used to be connected to instance variables that i already removed lol kinda what i meant when i said sorry for the messy code lol

            also my goal was to creat a drag dampener. for example dragging an object from the bottom of the screen thats constrained to half screen size for travel. nd object speed slows as it aproaches max distance. similar to a vehicle comng to a stop at a trafic light..

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

              @stephen ok, understood. I'm really not a specialist of games or scene but I wonder if there are no functions for the physical aspect of the movements.
              Hoping other guys will help you. And sorry, I shouldn't have doubted 🙄

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

                @cvp

                there are animations and i beleive interpolation functions i could use and i, sure they work amazingly 🙃 but i dont learn much using premade. i like to try and understand whats going on even behind the scenes. then once i understand then ill use builtin "shortcuts". i guess i like to make things as dificult as possible lol at least thats what my ex-wife would say 😅😂😅

                thank you for checking it out tho its much appritiated. and just fyi the only diference from games and other types of programs is graphical programing mainly. and resource managment. i use scene module mainly for touch input controls and i like the way it implements the eventloop. i could just use my own loop and objc for touch input. but i am slowly replacing parts of scene with objc code cuz it has been improving performance.

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

                  @stephen I can understand that. Due to my experience of (very) old guy using (very) old tools, I like to do all myself and I never have used Scene. I also prefer do animations my self in update method.
                  Good luck and have fun...

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

                    @stephen, the original request for ”pull to refresh” was covered earlier with one line of ”math”. What are you trying to achieve here? Spring force? Coulomb effect?

                    If you like scene and like to play with forces, you could look at SpriteKit - but that would not be ”rolling your own”.

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

                      @mikael its ok but yes i was trying to acheive a spring like constraint. but ill go ahead a put it to the side for now. thanks for the reply.

                      1 Reply Last reply Reply Quote 0
                      • Drizzel
                        Drizzel last edited by Drizzel

                        I‘m trying to double tap on a TableViewCell without actually selecting it, but it should still be selectable on a single tap. How would I go about doing this?

                        Let’s assume I have this code:

                        import ui
                        import gestures
                        
                        def double_tap(data):
                        	print('double tapped row', data.view.row)
                        def tap(data):
                        	print('tapped', data.view.row)
                        def long_press(data):
                        	print('long pressed', data.view.row)
                        
                        def tableview_cell_for_row(tableview, section, row):
                        	data = tableview.data_source.items[row]
                        	cell = ui.TableViewCell('subtitle')
                        	cell.text_label.text = data
                        	cell.row = row
                        	gestures.doubletap(cell, double_tap)
                        	#gestures.tap(cell, tap, number_of_touches_required = 1)
                        	#gestures.long_press(cell, long_press, minimum_press_duration = .2)
                        	
                        	return cell
                        	
                        class Delegate():
                        	def tableview_did_select(self, tableview, section, row):
                        		print('selected row', row)
                        
                        tv = ui.TableView()
                        tv.delegate = Delegate()
                        tv.data_source = ui.ListDataSource([str(x) for x in range(5)])
                        tv.data_source.delete_enabled = False
                        tv.data_source.tableview_cell_for_row = tableview_cell_for_row
                        tv.frame = (tv.frame[0], tv.frame[1], 400, 400)
                        
                        tv.present(style = 'sheet')
                        
                        stephen mikael 3 Replies Last reply Reply Quote 0
                        • stephen
                          stephen @Drizzel last edited by stephen

                          @Drizzel said:

                          I‘m trying to double tap on a TableViewCell without actually selecting it, but it should still be selectable on a single tap. How would I go about doing this?

                          Let’s assume I have this code:

                          import ui
                          import gestures
                          
                          def double_tap(data):
                          	print('double tapped row', data.view.row)
                          def tap(data):
                          	print('tapped', data.view.row)
                          def long_press(data):
                          	print('long pressed', data.view.row)
                          
                          def tableview_cell_for_row(tableview, section, row):
                          	data = tableview.data_source.items[row]
                          	cell = ui.TableViewCell('subtitle')
                          	cell.text_label.text = data
                          	cell.row = row
                          	gestures.doubletap(cell, double_tap)
                          	#gestures.tap(cell, tap, number_of_touches_required = 1)
                          	#gestures.long_press(cell, long_press, minimum_press_duration = .2)
                          	
                          	return cell
                          	
                          class Delegate():
                          	def tableview_did_select(self, tableview, section, row):
                          		print('selected row', row)
                          
                          tv = ui.TableView()
                          tv.delegate = Delegate()
                          tv.data_source = ui.ListDataSource([str(x) for x in range(5)])
                          tv.data_source.delete_enabled = False
                          tv.data_source.tableview_cell_for_row = tableview_cell_for_row
                          tv.frame = (tv.frame[0], tv.frame[1], 400, 400)
                          
                          tv.present(style = 'sheet')
                          

                          i believe you might wand def tableview_cell_for_row(tableview, section, row): as a method of class Delegate instead of a global function.

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

                            @Drizzel
                            im sure there is a much simpler way but...

                            why not have a state checker give x time to look for a second tap before executing functionality for a single tap? possibly a decorator?

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

                              @Drizzel, below works to separate the two gestures, but we lose the visual feedback for the tap selection, you would need to implement that yourself.

                              Better would be to find the built-in tap and give our doubletap preference over it, but I could not (yet...?) find it on the tableview, cell, content view, or the label.

                              import ui
                              import gestures
                              
                              def double_tap(data):
                                  print('double tapped row', data.view.row)
                                  
                              def tap(data):
                                  print('tapped', data.view.row)
                                  
                              def tableview_cell_for_row(tableview, section, row):
                                  data = tableview.data_source.items[row]
                                  cell = ui.TableViewCell('subtitle')
                                  cell.selectable = False
                                  cell.text_label.text = data
                                  cell.row = row
                                  doubler = gestures.doubletap(cell, double_tap)
                                  tapper = gestures.tap(cell, tap)
                                  doubler.before(tapper)
                                  return cell
                                  
                              class Delegate():
                                  def tableview_did_select(self, tableview, section, row):
                                      print('selected row', row)
                              
                              tv = ui.TableView(allows_selection=False)
                              tv.delegate = Delegate()
                              tv.data_source = ui.ListDataSource([str(x) for x in range(5)])
                              tv.data_source.delete_enabled = False
                              tv.data_source.tableview_cell_for_row = tableview_cell_for_row
                              
                              tv.present('fullscreen')
                              
                              1 Reply Last reply Reply Quote 3
                              • Drizzel
                                Drizzel last edited by

                                @mikael thanks, that works. I just change the cell‘s background color to lightgrey for visual feedback

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

                                  @stephen True, I corrected it :) The code was a bit rushed 🤷‍♂️

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

                                    awesome 😎🤓😊

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

                                      @Drizzel, here’s a version that supports both the doubletap and regular row selection.

                                      import ui
                                      import gestures
                                      
                                      def double_tap(data):
                                          print('double tapped row', data.view.row)
                                          
                                      def tableview_cell_for_row(tableview, section, row):
                                          data = tableview.data_source.items[row]
                                          cell = ui.TableViewCell('subtitle')
                                          cell.text_label.text = data
                                          cell.row = row
                                          doubler = gestures.doubletap(cell, double_tap)
                                          doubler.recognizer.delaysTouchesBegan = True
                                          return cell
                                          
                                      class Delegate():
                                          def tableview_did_select(self, tableview, section, row):
                                              print('selected row', row)
                                      
                                      tv = ui.TableView()
                                      tv.delegate = Delegate()
                                      tv.data_source = ui.ListDataSource([str(x) for x in range(5)])
                                      tv.data_source.delete_enabled = False
                                      tv.data_source.tableview_cell_for_row = tableview_cell_for_row
                                      
                                      tv.present('fullscreen')
                                      
                                      cvp 2 Replies Last reply Reply Quote 0
                                      • Anxietier
                                        Anxietier last edited by

                                        hi, i found another bug, after i use 5 fingers to switch app and get back, multi-touch would be not working

                                        1 Reply Last reply Reply Quote 1
                                        • cvp
                                          cvp @mikael last edited by

                                          @mikael Do you think it would be possible to use you gestures module on an objectiveC object?

                                          I want to add gestures on a SceneView object, but functions like tap require their view parameter as an object having objc_instance property (see UIGestureRecognizerDelegate init).

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

                                            @mikael I solved it with

                                            	def scene_view_tap(self,sender):
                                            		location = self.recognizer.locationInView_(self.scene_view)
                                            		node = self.scene_view.hitTest_options_(location,None)
                                            		print(node)
                                            
                                            .
                                            .
                                            .
                                            		self.gesture_recognizer_target = ui.Button()
                                            		self.gesture_recognizer_target.action = self.scene_view_tap
                                            		UITapGestureRecognizer = ObjCClass('UITapGestureRecognizer')
                                            		self.recognizer = UITapGestureRecognizer.alloc().initWithTarget_action_(self.gesture_recognizer_target, sel('invokeAction:')).autorelease()
                                            		scene_view.addGestureRecognizer_(self.recognizer)
                                            		self.scene_view = scene_view
                                            
                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors