omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular
    1. Home
    2. dcl
    3. Best

    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.


    • Profile
    • Following 0
    • Followers 0
    • Topics 3
    • Posts 13
    • Best 3
    • Controversial 0
    • Groups 0

    Best posts made by dcl

    • RE: Gestures for Scene

      @cvp the from objc_util import * is part of the Gestures.py file which was obtained as-is from GitHub and the call is at the top of the file, before any of the code. So unless the files are getting included weird as a part of the Scene setup, I am not sure what might cause that behavior

      @JonB Thanks for the tip! I tried instantiating the gestures object in the __init__() function of the Scene rather than setup() that seems to work. The key being that you MUST call the Scene.__init__() function inside the MyScene.__init__() function, else the script will throw exceptions of missing members.

      The revised code is below:

      from scene import *
      import Gestures
      
      class MyScene (Scene):
          def __init__(self, **kwargs):
              Scene.__init__(self, **kwargs)
              self.GO = Gestures.Gestures()
      
          def setup(self):
              self.ship = SpriteNode('spc:PlayerShip1Orange')
              self.ship.last_scale = self.ship.scale
              self.ship.position = self.size / 2
              self.add_child(self.ship)
              GO.add_pinch(self.view, self.handle_zoom)
      
          def handle_zoom(self, data):
              self.ship.scale = self.ship.last_scale * data.scale
                  if data.state is self.Gest.ENDED:
                      self.ship.last_scale = self.ship.scale
      
      run(MyScene())
      
      posted in Pythonista
      dcl
      dcl
    • Gestures for Scene

      Hello all,

      About a year ago I started looking for a solution to gesture recognition for a Pythonista Scene module. At that time, I was able to find the wonderful Gestures module for UI Views by @mikael but I was unable to use this in the Scene module (game). I was also unable to find anything that fit the need at the time, so I began crafting my own bit of code inspired by Gestures but for the Scene module using only Python code and the scene.touches interface (not hooking back into the objC code that the Gestures module uses).

      Some of the history of this project can be found at this forum post:

      https://forum.omz-software.com/topic/4624/help-gestures-for-scene-pythonista-debugging

      But I am pleased to say that while the code hasn't progressed much since that post, I have now gotten the code broken into individual files (instead of the monolithic code blob posted previously) and put on GitHub here:

      https://github.com/dlazenby/PythonistaSceneGestures

      Because this continues to be developed completely in my free time, and is fairly low on my priority list at the moment, I am providing this on GitHub in hopes that someone with more free time will take an interest and help me bring this project to completion for others to freely use.

      The current status of the project:

      By running the code from the gesture_handler.py file, the user gets a Scene in which user touches can be visualized on-screen individually and data about the "gesture" (grouping and movement of the current touches) can be seen at the top of the screen.

      Gesture data available:
      State (of the recognizer internal state machine)
      Number of Touches present
      Duration of the "gesture" (seconds)
      Translation of the "gesture" (pixels x, pixels y)
      Rotation of the "gesture" (degrees)
      Scale of the "gesture" (multiplier 1.0 = 100%)
      Result of the Recognizer (was the "gesture" recognized, and if so what type)

      There is also logging capability built in. To enable it, uncomment the code block near the top of the gesture_handler.py file. The logger dumps quite a bit of data (at least one message per update loop).

      My intention for the project is to
      (1) Finalize the functionality, making it similar to the Gestures module in that you have an object that, using hook functions placed inside the Scene module's touch_began(), touch_updated(), and touch_ended(), will analyze the touches and when a gesture is recognized will call a user defined callback which was setup on object creation along with the type of gesture to be recognized.
      (2) Possibly move some of the recognizer's functionality onto another thread in order to improve performance (I have very little experience with multi-threaded programs in Python / Pythonista)

      Currently, I am happy with the math which is analyzing the touches, but the schema for recognizing the "gesture" based on that data is not reliable. If someone would like to help troubleshoot what I have or develop a new / better schema for analyzing the data generated about a grouping of touches to recognize it as a gesture, that would be great!

      posted in Pythonista
      dcl
      dcl
    • RE: Superscript Text in ui Button [Solved]

      Thanks to zrzka for a working solution!

      I functionized his answer here:

      def superscript(view, itemName, text, size, offset, startPos, length):
      	#Get the UI object by name from the current UI view
      	target_ui_item = view[itemName]
      	#Create a obj-c Mutable Attributed String object and initialize with text
      	NSMutableAttributedString = ObjCClass('NSMutableAttributedString')
      	attributed_string = NSMutableAttributedString.alloc().initWithString_(ns(text))
      	range = NSRange(startPos, length)
      	#Create an obj-c Font
      	UIFont = ObjCClass('UIFont')
      	#Define a List (?) of attributes
      	attributes = {
      		ns('NSFont'): UIFont.systemFontOfSize(size),
      		ns('NSBaselineOffset'): ns(offset)
      	}
      	#Set the specific attributes of a specific range of the NS Mutable Attributed String
      	attributed_string.setAttributes_range_(attributes, range)
      	#
      	ui_item_objc = ObjCInstance(target_ui_item)
      	UIButton = ObjCClass('UIButton')                
      	for subview in ui_item_objc.subviews():
      		if subview.isKindOfClass(UIButton):
      			subview.setAttributedTitle_forState_(attributed_string, 0)
      

      Into this function, you should pass your current UI.View containing the UI element (in my case a button) which you want the superscripted string.
      The second argument is the name of the UI element, in my case 'x_squared' <- the name of my button
      The third argument is the text string that you want printed, in my case 'x2'
      The fourth argument is the font size of the base character (I used 16)
      The fifth arg is the offset for how high (or low, use negative) to offset the smaller character (I used 8)
      The sixth arg is the string position to start the superscript string, (for me that was char 1)
      The final arg is the length of the superscripted string (also 1 for me)
      So the final call to this function looked like this (see first line below)

      The second line below did a log base 10 string (slightly more complex call than an "x squared" button

      superscript(self, 'x_squared', 'x2', 16, 8, 1, 1)
      superscript(self, 'log_base_ten', 'log10', 16, -10, 3, 2)
      

      Hope this helps anyone who might need this in the future!
      Thanks to everyone who helped me!

      posted in Pythonista
      dcl
      dcl