• rownn

    Hey everyone,

    I have a ui.path in a shapeNode, which is growing somehow every second. Unfortunately, the path is jumping from state to state. I know why it is jumping and I also know for what this behavior is good for, but I‘d like to fix the path in center where it is meant to be. I guess playing around with the anchor_point would fix the problem only in some cases. Of course I could calculate around with the nodes bbox and the path bounds, but I cannot imagine that there is no better solution. I also thought about defining the size of path with a rect() before generating the actual path, but the resulting rectangle would be visible, of course. I hope you know what I’m talking about. Following a snippet which hopefully describes better what my long questions is about.

    from scene import *
    from time import time
    
    
    class MyScene (Scene):
                
        def setup(self):
            self.points = (Point(-100, -100),Point(100, -100),Point(100, 100),Point(-200, 100))
            self.startTime = time()
            self.prev_dt = -1       
            
            ground = Node(parent=self)
            ground.position = (self.size.w/2, self.size.h/2)
            
            self.node = ShapeNode(ui.Path.oval(0,0,10,10), fill_color='clear', stroke_color='red')
            self.node2 = ShapeNode(ui.Path.oval(0,0,20,20), fill_color='clear', stroke_color='yellow')
            
            ground.add_child(self.node)
            ground.add_child(self.node2)
    
        
        def update(self):
            dt = int((time()-self.startTime)%4)
    
            if dt != self.prev_dt:
                self.prev_dt = dt
                
                path = ui.Path.oval(0,0,10,10)
                path.move_to(self.points[0].x, self.points[0].y)
                path.line_to(self.points[1].x, self.points[1].y)
                
                for i in range(dt): path.line_to(self.points[(i+2)%4].x,self.points[(i+2)%4].y)
                
                self.node.path = path
    
    if __name__ == '__main__':
        run(MyScene(), show_fps=False) 
    

    Thx alot forfor every hint :)
    rownn

    posted in Pythonista read more
  • rownn

    For this part of the discussion alone this thread is worthy to exist. :)

    @rownn said: (boolean+1)%2
    The school math way

    @mikael said: boolean = boolean == False
    The developers way

    @cvp said: boolean = not boolean
    The pythonish way

    @stephen said: boolean = !boolean
    The beautiful but !pythonish way

    posted in Pythonista read more
  • rownn

    Hey guys,

    amazing again. I think I will study you snippets over a glass of wine this evening :)

    posted in Pythonista read more
  • rownn

    Hey @mikael,

    thanks alot! It is amazing to see one of my codes in a rewritten most likely better way :) I just flew over it, but there are many very nice looking changes and I‘m excited to get into it deeper soon. Until then I thank you for the time you spent and the insights which I will have.

    PS: Thought it would be clever to avoid the update method. Isnt it performance-consuming?

    posted in Pythonista read more
  • rownn

    Hey @stephen,

    good question. Actually I wanted to shrink the scene, too. But than I realized that it wouldnt effect the appearance I wish. Besides this I wasnt able to get the scene shinking smoothly with the sceneView :( Is there a way to achieve this?

    Thanks for the tip regarding the ui importing, you are right, of course :)
    And thanks for looking though the code

    posted in Pythonista read more
  • rownn

    Hey everyone,

    awkward title of the topic, I know. I struggled with fitting nodes in resizing sceneViews. I have a sceneView, which is resized via animation by pushing a button. The question was how to stick the scene at the top of the view. I have found a solution, but because of the fact I am new to that stuff I‘d like to post the code here with the silent question if there is a better way or if it is a common way.

    from scene import *
    import ui
    
    class MyScene(Scene):
        def __init__(self):
            Scene.__init__(self)
            self.background_color= '#eee'
            self.frm = ShapeNode(parent=self, fill_color='red', stroke_color='clear')
        
        def setup(self):
            self.viewSizeHasChanged()
            
        def viewSizeHasChanged(self):
            self.frm.position = (150,self.view.height/2-(500-self.view.height)/2)
            self.frm.path = ui.Path.rect(0,0,250,450)
    
    class GUI(ui.View):
        def __init__(self):
            self.background_color = '#ddd'
            self.scales = (100, 500)
            self.state = True
            self.separator_H = 10
            
            self.mainView = ui.View(frame=(50,50,300,500), background_color='#fff')
            self.add_subview(self.mainView)
            
            self.sn = SceneView()
            self.sn.frame = self.mainView.bounds
            self.mainView.add_subview(self.sn)
            self.sn.scene = MyScene()
            
            self.btn_Do = ui.Button(name='Do', title='Do', background_color= '#ddd', corner_radius = 12, action = self.btn_tapped)
            self.btn_Do.frame = (175, 275, 50, 50)
            self.add_subview(self.btn_Do)
            
        def btn_tapped(self, sender):
            if sender.name == 'Do':
                self.state = (self.state+1)%2
                self.sn.scene.viewSizeHasChanged()
                self.animate(self.scales[self.state])
                    
        def animate(self, H):
            def animation(): self.mainView.frame = (50,(600-H)/2,300,H)
            ui.animate(animation, duration=1.0)
            
    if __name__ == '__main__':
        GUI().present('fullscreen')
    

    Thx for every hint guys
    rownn

    posted in Pythonista read more
  • rownn

    Great! Thanks:)

    posted in Pythonista read more
  • rownn

    The animation part of the code seems to be not needed.
    So...

    def keyboard_frame_will_change(self, frame):
        self.y = self.scene.size.h-self.textView_H-frame[3]
        #self.animate(frame)
            
    #def animate(self, frame):
        #def animation():
            #self.y = self.scene.size.h-self.textView_H-frame[3]
        #ui.animate(animation, duration=0.25)
         ```

    posted in Pythonista read more
  • rownn

    Hi everyone,

    my first idea was to detect if the TextView is focused or not, but sometimes I work with an external keyboard, so JonBs hint is perfect. It works perfectly fine for me. Hope there won‘t be any unexpected issues. Here the reduced code:

    from scene import *
    import ui
    
    class MyScene (Scene):
        def setup(self):
            v = V(scene=self)
            self.view.add_subview(v)
            
            background = Node(parent=self)
            background.position = (self.size.w/2,self.size.h/2)#(self.size.w/2, self.size.h/2)
            background.add_child(ShapeNode(ui.Path.rect(0,0,self.size.w,self.size.h), fill_color='#dddddd', stroke_color='clear'))
            
            self.add_child(background)
            
    class V(ui.View):
        def __init__(self, *args, **kwargs):
            self.scene = kwargs["scene"]
            self.textView_H = 200
            self.frame=(0, self.scene.size.h-self.textView_H, self.scene.size.w, self.scene.size.h)
            
            self.tv=ui.TextView(frame=(10, 0, self.scene.size.w-20, self.textView_H), font=('Courier', 17.0), background_color='#333333', text_color = '#ffffff')
            self.tv.text = 'Heyhey.'
            self.tv.delegate=self
            self.add_subview(self.tv)
        
        def keyboard_frame_will_change(self, frame):
            self.animate(frame)
            
        def animate(self, frame):
            def animation():
                self.y = self.scene.size.h-self.textView_H-frame[3]
            ui.animate(animation, duration=0.25)
        
            
    if __name__ == '__main__':
        run(MyScene(), PORTRAIT, show_fps=True)
    

    Thank you guys. Amazing fast replying forum!

    posted in Pythonista read more
  • rownn

    Hi everyone,

    I work on a scene with ui.TextView and I‘d like to know how I could detect if the virtual keyboard is open/visible or not. Depending on the visibility of the keyboard I‘d like to do some layout changes. Does somebody have a hint for me?

    Greetings :)
    rownn

    posted in Pythonista read more
  • rownn

    Hi guys,

    I feel a bit ashamed about my topic, because everything I needed to know is well descripted in the documentation. :/ But as I said I‘m not that familiar with pythonista yet, but I have learned alot in the last few days. Thanks again!

    AND: pythonista rocks!

    posted in Pythonista read more
  • rownn

    Hey,
    textView seems to be exectly what I looked for, amazing, thank you guys. @stephen thx for sharing the snippet. Took a look in the textView before, but I guess I will need a lot more snippets to feel a bit familiar with pythonista.

    :) rownn

    posted in Pythonista read more
  • rownn

    Hey guys,

    I’m somehow overwhelmed. Thanks for all your replies and hints. I have to go through your tips first and I will be back soon.

    Thanks again
    rownn

    posted in Pythonista read more
  • rownn

    Hi everybody,

    I’m not sure if the title of the topic is clear. I’d like to make it possible to write and edit a text while a game is running. So I tried to use an UI text field and I saw that it is very close to what I’m looking for, but the behavior differs to usual text editing in other applications. The text field seems to be just one line without line breaks, I cannot see the curser position and selecting parts of input is possible but not visible. So is there a way to get an text editor field into games? I don’t want it to be somehow fancy. I want it just as usual.

    Hope someone of you can give me a hint, happy eastern to those who are going to celebrate it and greetings to the other

    :) rownn

    posted in Pythonista read more
  • rownn

    Ok, I think I got it. After I recognized that it is possible to add UI elements to a CustomView via long pressing and SubViews I solved the stuff this way.

    Pythonista makes fun! Great and nice app!!!

    ——————————————————————————————————-
    ——————————————————————————————————
    *.py

    from scene import *
    import ui
    
    class MyScene (Scene):
        
        width = 900
        height = 500
        activeNode = None
        handles = ()
        menu_collapse = True
        
        def setup(self):
            ground = Node(parent=self)
            ground.position = (self.size.w/2, self.size.h/2)
            
            self.ellipse = ShapeNode(ui.Path.oval(0,0, self.width, self.height), fill_color='clear', stroke_color='#b50000')
            self.handleWidth = ShapeNode(ui.Path.oval(0,0, 10, 10), fill_color='#b50000', stroke_color='clear')
            self.handleHeight = ShapeNode(ui.Path.oval(0,0, 10, 10), fill_color='#b50000', stroke_color='clear')
            
            menuShape = ui.Path.rect(0,0,32,2)
            menuShape.append_path(ui.Path.rect(0,-8,32,2))
            menuShape.append_path(ui.Path.rect(0,-16,32,2))
            self.menu = ShapeNode(menuShape, fill_color='white', stroke_color='clear')
            self.menu.position = (24,self.size.y-20)
            
            ground.add_child(self.ellipse)
            ground.add_child(self.handleWidth)
            ground.add_child(self.handleHeight)
            self.add_child(self.menu)
            self.add_child(ground)
            
            self.handles = (self.handleWidth, self.handleHeight, self.menu)
            
            self.ui01 = self.view.superview.subviews[1]
            self.ui01.x = -400
            
            self.sliderW = self.ui01['slider-01']
            self.sliderW.action = self.sliderW_changed
            self.sliderH = self.ui01['slider-02']
            self.sliderH.action = self.sliderH_changed
            
            self.updateStuff()
            
            
        def updateStuff(self):
            self.ellipse.path = ui.Path.oval(0,0, self.width, self.height)
            self.handleWidth.position = (self.width/2,0)
            self.handleHeight.position = (0, self.height/2)
            self.sliderW.value = round(self.width/1000,2)
            self.sliderH.value = round(self.height/1000,2)
            self.ui01['label-01'].text = str(self.width)
            self.ui01['label-02'].text = str(self.height)       
            
            
        def checkTouchAndSetActiveHandle(self, touch):
            l = touch.location
            detectSize  = 20
            
            for node in self.handles:
                p = node.point_to_scene((0,0)) #.position
                if (p.x-detectSize < l.x < p.x+detectSize) and (p.y-detectSize < l.y < p.y+detectSize): 
                    self.activeNode = node  
                    
            
        def touch_began(self, touch):
            self.checkTouchAndSetActiveHandle(touch)
            if self.activeNode == self.menu:
                if self.menu_collapse == True: 
                    self.menu.position = (424, self.size.y-20)
                    self.ui01.x = 0
                    self.menu_collapse = False
                    
                else: 
                    self.menu.position = (24, self.size.y-20)
                    self.ui01.x = -400
                    self.menu_collapse = True
                
            elif self.activeNode == self.handleWidth:
                self.handleWidth.path = ui.Path.oval(0,0, 50, 50)
            
            elif self.activeNode == self.handleHeight:
                self.handleHeight.path = ui.Path.oval(0,0, 50, 50)  
                
                        
        def touch_moved(self, touch):
            x = touch.location.x
            y = touch.location.y
            
            if self.activeNode == self.handleWidth:
                self.width = max(0,self.handleWidth.parent.point_from_scene((x,y)).x)*2
                self.updateStuff()
                
            elif self.activeNode == self.handleHeight:
                self.height = max(0,self.handleHeight.parent.point_from_scene((x,y)).y)*2
                self.updateStuff()
            
            
        def touch_ended(self, touch):
            self.activeNode = None
            self.handleWidth.path = ui.Path.oval(0,0, 10, 10)
            self.handleHeight.path = ui.Path.oval(0,0, 10, 10)
            
            
        def sliderW_changed(self, sender):
            value = round(sender.superview['slider-01'].value*1000,2)
            sender.superview['label-01'].text = str(value)
            self.width = value
            self.updateStuff()
            
    
        def sliderH_changed(self, sender):
            value = round(sender.superview['slider-02'].value*1000,2)
            sender.superview['label-02'].text = str(value)
            self.height = value
            self.updateStuff()
            
            
    v = ui.load_view('test-03_SceneWithUI.pyui')
    v['sceneview'].scene = MyScene()
    v.present('fullscreen') 
    

    ——————————————————————————————————-
    ——————————————————————————————————
    *.pyui file

    [
      {
        "nodes" : [
          {
            "nodes" : [
    
            ],
            "frame" : "{{0, 0}, {1024, 768}}",
            "class" : "View",
            "attributes" : {
              "flex" : "WHRB",
              "alpha" : 1,
              "custom_class" : "SceneView",
              "frame" : "{{150, -10}, {100, 100}}",
              "tint_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
              "class" : "View",
              "uuid" : "94D2E098-8578-4C00-89C7-CFB241F4AB69",
              "background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
              "name" : "sceneview"
            },
            "selected" : false
          },
          {
            "nodes" : [
              {
                "nodes" : [
    
                ],
                "frame" : "{{0, 0}, {400, 768}}",
                "class" : "View",
                "attributes" : {
                  "alpha" : 0.20000000000000001,
                  "flex" : "WH",
                  "frame" : "{{150, 334}, {100, 100}}",
                  "class" : "View",
                  "background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
                  "uuid" : "863D3F2F-3606-48E0-907A-83F59724AB15",
                  "name" : "view1"
                },
                "selected" : false
              },
              {
                "nodes" : [
    
                ],
                "frame" : "{{110, 30}, {193, 34}}",
                "class" : "Slider",
                "attributes" : {
                  "flex" : "",
                  "continuous" : true,
                  "frame" : "{{79, 194}, {200, 34}}",
                  "uuid" : "668C42E2-A62F-4A49-9460-E3150C231B61",
                  "class" : "Slider",
                  "value" : 0.5,
                  "name" : "slider-01"
                },
                "selected" : false
              },
              {
                "nodes" : [
    
                ],
                "frame" : "{{310, 30}, {70, 34}}",
                "class" : "Label",
                "attributes" : {
                  "font_size" : 18,
                  "text_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
                  "frame" : "{{104, 195}, {150, 32}}",
                  "uuid" : "BED64DBB-5A87-491A-BE29-FEE43749DC87",
                  "class" : "Label",
                  "alignment" : "left",
                  "text" : "0.5",
                  "name" : "label-01",
                  "font_name" : "<System>"
                },
                "selected" : false
              },
              {
                "nodes" : [
    
                ],
                "frame" : "{{25, 30}, {75, 34}}",
                "class" : "Label",
                "attributes" : {
                  "alpha" : 1,
                  "font_size" : 18,
                  "text_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
                  "frame" : "{{125, 64}, {150, 32}}",
                  "uuid" : "4796A6EA-2337-4F70-8E9E-09B097F7B271",
                  "class" : "Label",
                  "alignment" : "left",
                  "text" : "Width",
                  "name" : "label1",
                  "font_name" : "<System>"
                },
                "selected" : false
              },
              {
                "nodes" : [
    
                ],
                "frame" : "{{25, 80}, {75, 34}}",
                "class" : "Label",
                "attributes" : {
                  "alpha" : 1,
                  "font_size" : 18,
                  "text_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
                  "frame" : "{{125, 64}, {150, 32}}",
                  "uuid" : "AD165DAE-C1EE-42EB-8035-5DCE2BFFFDF2",
                  "class" : "Label",
                  "alignment" : "left",
                  "text" : "Height",
                  "name" : "label2",
                  "font_name" : "<System>"
                },
                "selected" : false
              },
              {
                "nodes" : [
    
                ],
                "frame" : "{{110, 80}, {192, 34}}",
                "class" : "Slider",
                "attributes" : {
                  "flex" : "",
                  "continuous" : true,
                  "frame" : "{{100, 63}, {200, 34}}",
                  "uuid" : "F61A2731-DA26-4E24-9648-45B15D58D991",
                  "class" : "Slider",
                  "value" : 0.5,
                  "name" : "slider-02"
                },
                "selected" : false
              },
              {
                "nodes" : [
    
                ],
                "frame" : "{{310, 80}, {70, 34}}",
                "class" : "Label",
                "attributes" : {
                  "font_size" : 18,
                  "text_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
                  "frame" : "{{125, 64}, {150, 32}}",
                  "uuid" : "A30D7411-95D9-4E3D-8AAF-EC1264C70D86",
                  "class" : "Label",
                  "alignment" : "left",
                  "text" : "0.5",
                  "name" : "label-02",
                  "font_name" : "<System>"
                },
                "selected" : true
              }
            ],
            "frame" : "{{0, 0}, {400, 768}}",
            "class" : "View",
            "attributes" : {
              "flex" : "H",
              "alpha" : 1,
              "frame" : "{{462, 334}, {100, 100}}",
              "tint_color" : "RGBA(1.000000,0.347826,0.021739,1.000000)",
              "uuid" : "B1E5A256-9CE8-4B7E-962E-FD577172D67D",
              "class" : "View",
              "name" : "menu-01"
            },
            "selected" : true
          }
        ],
        "frame" : "{{0, 0}, {1024, 768}}",
        "class" : "View",
        "attributes" : {
          "flex" : "",
          "custom_class" : "",
          "enabled" : true,
          "tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
          "border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
          "background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
          "name" : ""
        },
        "selected" : false
      }
    ]
    

    posted in Pythonista read more
  • rownn

    Hey,

    another question, which still has to do something with combining scenes and UI ... so I keep asking here. It seems that the scene is part of the UI. What would be the best way to separate them? I mean, how could I achieve a hiding/unhiding UI? Or transforming the position of the UI. Or working with several UIs in one scene at all?

    sorry for asking that much at once
    rownn

    posted in Pythonista read more
  • rownn

    @brumm Perfect! Works great!

    posted in Pythonista read more
  • rownn

    Ok, think I got it, but I‘m not sure if it is the best way.

    from scene import *
    import ui
    
    class MyScene (Scene):
        def setup(self):
    
            print (self.view.superview['slider1'].value)
    
            pass
            # Define your scene like usual...
    
    v = ui.load_view('example.pyui')
    v['sceneview'].scene = MyScene()
    v.present('fullscreen') ```

    posted in Pythonista read more
  • rownn

    Hey everyone,

    thanks for these snippets. They are very useful!
    But I wonder how to get the value of a slider, for example a slider named ‚slider1‘, into the MyScene class.

    Thanks for every hint
    Rownn

    posted in Pythonista read more
  • rownn

    Really? It is that easy? Many thx!!!

    posted in Pythonista read more
Internal error.

Oops! Looks like something went wrong!