Forum Archive

updating shapenode fillcolor on touch_began

nekosen

Hi everybody,

I want to change the fillcolor of a shapenode when touching it. My script works for updating the text but not the shapenode fillcolor. Can you help me ?

Here is the code I have, it's working except for the fillcolor of the shapenode. Sorry the layout of the code is not optimal, I just copy/pasted.

p.s. i am starting from cards tutorial. I love python(ista) !

thanks

from scene import *
import sound
import random
import math
A = Action

class ntt(Node):
    chr_wdth = 60


    def __init__(self, txt):
        #self.color = 'black'

        rect_path = ui.Path.rect(0, 0, 75, 100)
        rect_path.line_width = 1
        # the bg_node is a shape node that uses the path to draw the shape
        self.ntt_shpnd = ShapeNode(rect_path, 'white', 'black')
        self.ntt_lblnd = LabelNode(txt, ('Courier New', 10))

        self.ntt_lblnd.color = "red"
        self.ntt_lblnd.position = (0, 0)
        # add the nodes as children of the card node
        self.add_child(self.ntt_shpnd)
        self.ntt_shpnd.add_child(self.ntt_lblnd)

    def point_inside(self, x, y):
        '''Check if the point x, y is within the bounding box'''
        return self.bbox.contains_point((x, y))

    def update(self):
        pass

class MyScene (Scene):
    def setup(self):
        ntt_1=ntt("hello")
        ntt_1.position= self.size/2
        self.ntts = []
        self.ntts.append(ntt_1)
        self.add_child(ntt_1)

    def did_change_size(self):
        pass

    def update(self):
        pass

    def touch_began(self, touch):
        x, y = touch.location
        if self.ntts[0].point_inside(x, y):
            self.ntts[0].ntt_lblnd.text = "hello back"
            #throw_1
            self.ntts[0].ntt_shpnd = ShapeNode(self.ntts[0].ntt_shpnd.path, "red","black")
            #throw_2
            self.ntts[0].ntt_shpnd.fill_color="red"

    def touch_moved(self, touch):
        pass

    def touch_ended(self, touch):
        pass

if __name__ == '__main__':
    run(MyScene(), show_fps=False)
ccc

```

```
will format it as Python code.

What is an ntt and why should the reader of this code have to guess?
Is it https://en.wikipedia.org/wiki/Nippon_Telegraph_and_Telephone ?

nekosen

Hi ccc,

'ntt' is an abbrevation for 'entity' but that probably won't help you either. An entity is a node on the screen that I want to work with. In that sense it is concrete for me. It is meant to be abstract, it s not a trick of some kind to hide something. Is that okay for you ?

Anyway, the question remains.

<
from scene import *
import sound
import random
import math
A = Action

class ntt(Node):
chr_wdth = 60

def __init__(self, txt):
    #self.color = 'black'

    rect_path = ui.Path.rect(0, 0, 75, 100)
    rect_path.line_width = 1
    # the bg_node is a shape node that uses the path to draw the shape
    self.ntt_shpnd = ShapeNode(rect_path, 'white', 'black')
    self.ntt_lblnd = LabelNode(txt, ('Courier New', 10))

    self.ntt_lblnd.color = "red"
    self.ntt_lblnd.position = (0, 0)
    # add the nodes as children of the card node
    self.add_child(self.ntt_shpnd)
    self.ntt_shpnd.add_child(self.ntt_lblnd)

def point_inside(self, x, y):
    '''Check if the point x, y is within the bounding box'''
    return self.bbox.contains_point((x, y))

def update(self):
    pass

class MyScene (Scene):
def setup(self):
ntt_1=ntt("hello")
ntt_1.position= self.size/2
self.ntts = []
self.ntts.append(ntt_1)
self.add_child(ntt_1)

def did_change_size(self):
    pass

def update(self):
    pass

def touch_began(self, touch):
    x, y = touch.location
    if self.ntts[0].point_inside(x, y):
        self.ntts[0].ntt_lblnd.text = "hello back"
        #throw_1
        self.ntts[0].ntt_shpnd = ShapeNode(self.ntts[0].ntt_shpnd.path, "red","black")
        #throw_2
        self.ntts[0].ntt_shpnd.fill_color="red"

def touch_moved(self, touch):
    pass

def touch_ended(self, touch):
    pass

if name == 'main':
run(MyScene(), show_fps=False)

cvp

@nekosen said

I want to change the fillcolor of a shapenode

Why did you recreate a new shapenode, your fill_color setting is sufficient

            #throw_1
            #self.ntts[0].ntt_shpnd = ShapeNode(self.ntts[0].ntt_shpnd.path, "red","black")
            #throw_2
            self.ntts[0].ntt_shpnd.fill_color="red"
nekosen

Hi cvp,

As you can see I tried before on #throw 1:

self.ntts[0].ntt_shpnd = ShapeNode(self.ntts[0].ntt_shpnd.path, "red","black")

it does not work hence #throw 2

Both trials don't work.

sorry for me being not clear enough.

Thanks

cvp

@nekosen when I comment your throw1, and I tap the node, it becomes red, that is not what you want?

nekosen

Hi cvp,

I deeply apologize. Indeed, I retried as per your suggestion and now it does behave like I want! I must have done something wrong and lost track of what I was doing.

Thanks for helping me to break out of this, I was really stuck!

cvp

@nekosen you can also change the text color by

            self.ntts[0].ntt_lblnd.color="black"
cvp

@nekosen when you want to insert a code, please use button

Insert Code Here

nekosen

Super,

the way is open now.

Okay and now I understand, I have to click on button.

from scene import *
import sound
import random
import math
A = Action

class ntt(Node):
    chr_wdth = 60


    def __init__(self, txt):
        #self.color = 'black'

        rect_path = ui.Path.rect(0, 0, 75, 100)
        rect_path.line_width = 1
        # the bg_node is a shape node that uses the path to draw the shape
        self.ntt_shpnd = ShapeNode(rect_path, 'white', 'black')
        self.ntt_lblnd = LabelNode(txt, ('Courier New', 10))

        self.ntt_lblnd.color = "red"
        self.ntt_lblnd.position = (0, 0)
        # add the nodes as children of the card node
        self.add_child(self.ntt_shpnd)
        self.ntt_shpnd.add_child(self.ntt_lblnd)

    def point_inside(self, x, y):
        '''Check if the point x, y is within the bounding box'''
        return self.bbox.contains_point((x, y))

    def update(self):
        pass

class MyScene (Scene):
    def setup(self):
        ntt_1=ntt("hello")
        ntt_1.position= self.size/2
        self.ntts = []
        self.ntts.append(ntt_1)
        self.add_child(ntt_1)

    def did_change_size(self):
        pass

    def update(self):
        pass

    def touch_began(self, touch):
        x, y = touch.location
        if self.ntts[0].point_inside(x, y):
            self.ntts[0].ntt_lblnd.text = "hello back"
            #throw_1
            self.ntts[0].ntt_shpnd = ShapeNode(self.ntts[0].ntt_shpnd.path, "red","black")
            #throw_2
            self.ntts[0].ntt_shpnd.fill_color="red"

    def touch_moved(self, touch):
        pass

    def touch_ended(self, touch):
        pass

if __name__ == '__main__':
    run(MyScene(), show_fps=False)


Yes it works !

Thanks both of you!

cvp

@nekosen did you see the previous post about changing the labelnode text color?

nekosen

yes, thank you for that as well.

but I didn't see your second post : I was writing while you posted it.

cvp

@nekosen ok, thus happy end 🙂

nekosen

Yes, but you triggered the solution ! Let that be clear. (I have to wait 180seconds before I can answer, because I have no reputation - I am new here)

cvp

@nekosen said

I am new here

Everybody has been new one day. Thus welcome in this forum of this marvelous app.

nekosen

How can I do the in line stuff like you did with my 'I am new here' ?

nekosen

'@nekosen said: ..

and thanks for welcoming

nekosen

I will check the forum instructions.

cvp

@nekosen you select/copy the text you want to reuse, you tap reply, and you type
">" followed by the copied text .

Tap quote on a post to see it with its commands, but the quote puts a second ">" in front of each line

nekosen

@cvp :

you select/copy the text you want to reuse, you tap reply, and you type
">" followed by the copied text .

Tap quote on a post to see it with its commands, but the quote puts a second ">" in front of each line

got it, thanks!

JonB

@nekosen by the way,

Instead of

        x, y = touch.location
        if self.ntts[0].point_inside(x, y):

You can write

        if touch.location in self.ntts[0].bbox:
            # ...

(You can maybe omit the final bbox -- I forget).

nekosen

@JonB

Hi JonB,

Your proposal is better, I can omit the :

def point_inside(self, x, y):
        '''Check if the point x, y is within the bounding box'''
        return self.bbox.contains_point((x, y))

And this is more readable :

if touch.location in ntt.bbox :
'''instead of if ntt.point_inside(x, y):'''

If it could be without bbox like you suggest, it would even be shorter, but I tried, it's not the case.

Thanks!

JonB

In this case it is the same, but note that bbox includes the node and all of its descendants, while frame is the node itself.