Skip to content

Instantly share code, notes, and snippets.

@encela95dus
Created August 5, 2019 04:12
Show Gist options
  • Save encela95dus/33f06c4f1c439849be7736f897b0c6b0 to your computer and use it in GitHub Desktop.
Save encela95dus/33f06c4f1c439849be7736f897b0c6b0 to your computer and use it in GitHub Desktop.
customslider.py
import ui
class CustomSlider(ui.View):
def __init__(self,*args,**kwargs):
super().__init__(*args, **kwargs)
@property
def value(self):
return self.__value
@value.setter
def value(self, val):
if val < 0:
self.__value = val = 0
elif val > 1.0:
self.__value = val = 1.0
else:
self.__value = val
self.pos = self.width*val
self.set_needs_display()
@property
def action(self):
return self.__action
@action.setter
def action(self, act):
self.__action = act
def draw(self):
ui.set_color((.5,.5,.5,1.0))
self.path = ui.Path()
self.path.move_to(0, self.height/2.0+self.height/8)
self.path.line_to(self.width, self.height/2+self.height/8)
self.path.stroke()
self.path = ui.Path()
ui.set_color((0,0,1,1))
self.path.move_to(0, self.height/2.0+self.height/8)
self.path.line_to(self.pos, self.height/2+self.height/8)
self.path.stroke()
self.path = ui.Path.oval(self.pos, self.height/2.0,
self.height/4.0, self.height/4.0)
ui.set_color((.5,.5,.5,1.0))
self.path.fill()
self.value = self.pos/self.width
if self.action:
self.action(self)
def touch_began(self, touch):
self.pos = touch.location.x
def touch_moved(self, touch):
delta = (touch.location.x - self.pos)
if abs(delta) > 5.0:
self.pos += delta
self.set_needs_display()
def touch_ended(self, touch):
self.set_needs_display()
if __name__ == '__main__':
v = ui.load_view()
v.height = 500
v.width = 500
def update_label(sender):
sender.superview['label1'].text=str(sender.value)
v['view1'].action = update_label
v['view1'].value = .5
v.present('sheet')
[
{
"class" : "View",
"attributes" : {
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {493, 363}}",
"selected" : false,
"nodes" : [
{
"class" : "View",
"attributes" : {
"class" : "View",
"name" : "view1",
"uuid" : "873644B6-2A2E-4C33-B4AF-C8D8DD35D576",
"frame" : "{{153, 53}, {100, 100}}",
"custom_class" : "CustomSlider"
},
"frame" : "{{50, 50}, {400, 100}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Label",
"attributes" : {
"font_size" : 18,
"text" : "0.5",
"font_name" : "<System>",
"name" : "label1",
"class" : "Label",
"alignment" : "center",
"frame" : "{{128, 169}, {150, 32}}",
"uuid" : "69ECF698-7671-42F9-AB70-2E654312FE67"
},
"frame" : "{{50, 200}, {400, 50}}",
"selected" : true,
"nodes" : [
]
}
]
}
]
@cclauss
Copy link

cclauss commented Aug 9, 2019

        if val < 0:
            self.__value = val = 0
        elif val > 1.0:
            self.__value = val = 1.0
        else:
            self.__value = val
# —>
        self.__value = val = max(0, min(val, 1.0))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment