Example #1
0
class LSystemsView(Screen):

    def __init__(self,**kwargs): 
        #TODO KV!!!!!!
        self.register_event_type('on_edit')
        super(LSystemsView, self).__init__(**kwargs)
        self.name = "view"
        self.main_layout=BoxLayout(orientation='vertical')
        self.buttons_layout = BoxLayout(orientation='horizontal',size_hint= (1, .05))
        self.labels_layout = BoxLayout(orientation='horizontal',size_hint= (1, .05))       
        self.image = LSystemImage()
        self.image.bind(on_update=self.image_update)

        self.width_choice=DropDown()
        self.width_choice.bind(on_select=self.choose_width)
        self.width_choice_button=Button(text="Line width")
        self.width_choice_button.bind(on_release = self.width_choice.open)

        for i in range(1,10):
            btn = ToggleButton(text=str(i), group='width_values',size_hint_y=None,height=sp(30))
            if i == 1: btn.state = 'down'
            btn.bind(on_press = lambda btn: self.width_choice.select(btn.text))
            self.width_choice.add_widget(btn)
        self.fractal_choice = DropDown()
        self.fractal_choice.bind(on_select=self.choose_fractal)
        self.file_choice_button=Button(text = "File")        
        self.choice_button=Button(text = "L-System")
        self.file_choice_button.bind(on_release = self.on_file_choose) #self.file_choice.open)
        self.choice_button.bind(on_release = self.fractal_choice.open)
        self.next_button = Button(text="Next")
        self.ls_name = Label(text = "")
        self.cur_file = Label(text = "")
        self.plus_button = Button(text="+")
        self.minus_button = Button(text="-")
        self.background_button = Button(text="Background")
        self.background_button.bind(on_press=self.choose_background_color)
        self.plus_button.bind(on_press=self.plus)
        self.minus_button.bind(on_press=self.minus)
        self.next_button.bind(on_press=self.next)        
        self.edit_button = Button(text="Edit")
        self.edit_button.bind(on_press=self.edit)
        self.segments = Label(text="")
        self.iterations=Label()
        self.buttons_layout.add_widget(self.file_choice_button)
        self.buttons_layout.add_widget(self.choice_button)
        self.buttons_layout.add_widget(self.next_button)
        self.buttons_layout.add_widget(self.plus_button)
        self.buttons_layout.add_widget(self.minus_button)
        self.buttons_layout.add_widget(self.edit_button)
        self.buttons_layout.add_widget(self.background_button)
        self.buttons_layout.add_widget(self.width_choice_button)
        self.labels_layout.add_widget(Label(text="[b]File name:[/b]",markup=True))
        self.labels_layout.add_widget(self.cur_file)
        self.labels_layout.add_widget(Label(text="[b]L-system name:[/b]",markup=True))
        self.labels_layout.add_widget(self.ls_name)
        self.labels_layout.add_widget(Label(text="[b]Number of iterations:[/b]",markup=True))
        self.labels_layout.add_widget(self.iterations)
        self.labels_layout.add_widget(Label(text="[b]Number of segments:[/b]",markup=True))
        self.labels_layout.add_widget(self.segments)
        self.main_layout.add_widget(self.image)
        self.main_layout.add_widget(self.buttons_layout)
        self.main_layout.add_widget(self.labels_layout)

        self.file_chooser = LFileChooser()
        self.file_chooser.bind(on_choose=self.choose_file)
        self.popup = Popup(title='Load L-Systems', content=self.file_chooser,
                      size_hint=(None, None), size=(500, 500))


        
        self.color_picker=BackgroundColorPicker()
        self.color_picker.bind(on_choose=self.change_background_color)
        self.color_picker_popup = Popup(title = "Pick color",
                                        content = self.color_picker,
                                        size_hint = (None,None),
                                        size = (500,500))
        
        

        self.add_widget(self.main_layout)




    def choose_background_color(self,*args,**kwargs):
        self.color_picker_popup.open()

    def change_background_color(self,instance,color):
        r,g,b,a = color
        r=int(255*r)
        g=int(255*g)
        b=int(255*b)
        self.image.set_background_color((r,g,b))
        self.color_picker_popup.dismiss()

    def on_edit(self,*args,**kwargs):
        pass

    def on_file_choose(self,*args):
        self.popup.open()

    def edit(self,instance,*args):        
        name=self.ls_name.text
        if name: fractal = self.fractals[name][0]
        else: fractal = '',{},0
        self.dispatch("on_edit",(name,fractal))


    def set_fractals(self):
        self.fractal_choice.clear_widgets()
        for k in sorted(self.fractals.keys()):
            btn = Button(text = k,size_hint_y=None, height=44)
            btn.bind(on_release = lambda btn: self.fractal_choice.select(btn.text))
            self.fractal_choice.add_widget(btn)
       
    def choose_file(self,instance,text):
        self.cur_file.text = basename(text)
        self.popup.dismiss()
        self.fractals=l_parser(open(l_file_path(text)))
        self.set_fractals()
        self.choose_fractal(None,sorted(self.fractals.keys())[0])

    def choose_width(self,instance,text):
        self.image.set_width(int(text))


    def choose_fractal(self,instance,text):
        self.ls_name.text = text
        self.image.set_lsystem(self.fractals[text][0])
        self.image.set_iterations(1)
        self.iterations.text = str(self.image.get_iterations())


    def image_update(self,instance,num):
        self.segments.text = str(num)

    def next(self,*args):
        cur_name = self.ls_name.text
        if not cur_name: return
        lst = self.fractals.keys()
        i = lst.index(cur_name)
        i+=1
        if i==len(self.fractals): i=0
        self.choose_fractal(None,lst[i])
     

        
    def plus(self,*args):
        self.image.set_iterations(self.image.get_iterations()+1)
        self.iterations.text = str(self.image.get_iterations())

    def minus(self,*args):
        self.image.set_iterations(self.image.get_iterations()-1)
        if self.iterations.text!="0":
            self.iterations.text = str(self.image.get_iterations())
Example #2
0
class LSystemsEdit(Screen):
    def __init__(self, **kwargs):
        super(LSystemsEdit, self).__init__(**kwargs)
        self.name = "edit"
        self.main_layout = BoxLayout(orientation='horizontal')

        self.edit_layout = BoxLayout(orientation='vertical', size_hint_x=.4)
        #Text boxes
        self.name_input = TextInput(multiline=False,
                                    size_hint_y=None,
                                    height=sp(30))
        self.name_input.bind(text=self.on_text)
        self.angle = TextInput(multiline=False,
                               size_hint_y=None,
                               height=sp(30))
        self.angle.bind(text=self.on_text)
        self.axiom = TextInput(multiline=False,
                               size_hint_y=None,
                               height=sp(30))
        self.axiom.bind(text=self.on_text)
        self.rules = TextInput(multiline=True, size_hint=(1, .4))
        self.rules.bind(text=self.on_text)
        self.rule_chooser = GridLayout(cols=6, size_hint=(1, .2))
        #buttons for changing number of iterations
        self.iterations_buttons = BoxLayout(orientation='horizontal',
                                            size_hint_y=None,
                                            height=sp(30))
        self.minus = Button(text="-")
        self.iter_label = Label(text="1")
        self.plus = Button(text="+")
        self.minus.bind(on_press=self.iter_press)
        self.plus.bind(on_press=self.iter_press)
        self.iterations_buttons.add_widget(self.minus)
        self.iterations_buttons.add_widget(self.iter_label)
        self.iterations_buttons.add_widget(self.plus)

        self.image = LSystemImage()

        self.edit_layout.add_widget(
            Label(text="[b]Name[/b]",
                  markup=True,
                  size_hint_y=None,
                  height=sp(30)))
        self.edit_layout.add_widget(self.name_input)
        self.edit_layout.add_widget(
            Label(text="[b]Angle[/b]",
                  markup=True,
                  size_hint_y=None,
                  height=sp(30)))
        self.edit_layout.add_widget(self.angle)
        self.edit_layout.add_widget(
            Label(text="[b]Axiom[/b]",
                  markup=True,
                  size_hint_y=None,
                  height=sp(30)))
        self.edit_layout.add_widget(self.axiom)
        self.edit_layout.add_widget(
            Label(text="[b]Rules[/b]",
                  markup=True,
                  size_hint_y=None,
                  height=sp(30)))
        self.edit_layout.add_widget(self.rules)
        self.edit_layout.add_widget(
            (Label(text="[b]Choose rule to visualise[/b]",
                   markup=True,
                   size_hint_y=None,
                   height=sp(30))))
        self.edit_layout.add_widget(self.rule_chooser)
        self.edit_layout.add_widget(
            (Label(text="[b]Change number of iterations[/b]",
                   markup=True,
                   size_hint_y=None,
                   height=sp(30))))
        self.edit_layout.add_widget(self.iterations_buttons)
        self.edit_layout.add_widget((Label(text="[b]L-systems syntax[/b]",
                                           markup=True,
                                           size_hint_y=None,
                                           height=sp(30))))
        syntax_label = ScrollableLabel()
        syntax_label.text = syntax
        #        def f(*args,**kwargs): syntax_label.text_size = syntax_label.size
        #        syntax_label.bind(size = f)

        self.edit_layout.add_widget(syntax_label)
        self.main_layout.add_widget(self.edit_layout)
        self.main_layout.add_widget(self.image)

        #self.load_lsystem(('quartet', ('fb', {'a': 'fbfa+hfa+fb-fa', 'h': '-', 'b': 'fb+fa-fb-jfbfa', 'j': '+', 'f': ''}, 90.0)))

        self.add_widget(self.main_layout)

    def iter_press(self, instance, *args):
        if instance.text == "+":
            iteration = int(self.iter_label.text) + 1
        else:
            iteration = int(self.iter_label.text) - 1
            if iteration < 0: iteration = 0
        self.image.set_iterations(iteration)
        self.iter_label.text = str(iteration)

    def set_rule_chooser(self):
        if self.lsystem:
            name, (axiom, rules, angle) = self.lsystem
        else:
            rules = {}
        self.rule_chooser.clear_widgets()
        for name in ["axiom"] + sorted(rules.keys()):
            btn = ToggleButton(text=name,
                               group='rules',
                               size_hint_y=None,
                               height=sp(30))
            if name == 'axiom': btn.state = 'down'
            btn.bind(on_press=self.on_rule_choose)
            self.rule_chooser.add_widget(btn)

    def on_rule_choose(self, instance, *args):
        if self.lsystem:
            name, (axiom, rules, angle) = self.lsystem
            new_axiom = instance.text if instance.text != "axiom" else axiom
            self.image.set_lsystem((new_axiom, rules, angle))

    def on_text(self, instance, *args):
        self.get_lsystem()
        if self.lsystem:
            self.image.set_lsystem(self.lsystem[1])
            #self.image.set_iterations(1)
        self.set_rule_chooser()

    def load_lsystem(self, lsystem):
        name, (axiom, rules, angle) = lsystem
        self.name_input.text = name
        self.axiom.text = axiom
        self.angle.text = str(angle)
        self.rules.text = '\n'.join(
            ["%s=%s" % (k, v) for (k, v) in sorted(rules.items())])

    def get_lsystem(self):
        name = self.name_input.text
        axiom = self.axiom.text.replace(" ", "")
        angle = ''
        try:
            angle = float(self.angle.text)
        except:
            angle = 0
        try:
            rules = dict([
                x.split("=")
                for x in self.rules.text.replace(" ", "").split("\n") if x
            ])
        except:
            rules = {}
        self.lsystem = name, (axiom, rules, angle)
class LSystemsEdit(Screen):

    def __init__(self,**kwargs):
        super(LSystemsEdit, self).__init__(**kwargs)
        self.name="edit"
        self.main_layout=BoxLayout(orientation='horizontal')

        self.edit_layout=BoxLayout(orientation='vertical',size_hint_x=.4)
        #Text boxes
        self.name_input = TextInput(multiline=False,size_hint_y=None,height=sp(30))
        self.name_input.bind(text=self.on_text)
        self.angle = TextInput(multiline=False,size_hint_y=None,height=sp(30))
        self.angle.bind(text=self.on_text)
        self.axiom = TextInput(multiline=False,size_hint_y=None,height=sp(30))
        self.axiom.bind(text=self.on_text)
        self.rules = TextInput(multiline=True,size_hint=(1,.4))
        self.rules.bind(text=self.on_text)
        self.rule_chooser = GridLayout(cols=6,size_hint=(1,.2))
        #buttons for changing number of iterations
        self.iterations_buttons = BoxLayout(orientation = 'horizontal',size_hint_y=None,height=sp(30))
        self.minus = Button(text="-")
        self.iter_label = Label(text = "1")
        self.plus = Button(text = "+")
        self.minus.bind(on_press = self.iter_press)
        self.plus.bind(on_press = self.iter_press)
        self.iterations_buttons.add_widget(self.minus)
        self.iterations_buttons.add_widget(self.iter_label)
        self.iterations_buttons.add_widget(self.plus)        

        self.image = LSystemImage()

        self.edit_layout.add_widget(Label(text="[b]Name[/b]",markup=True,size_hint_y=None,height=sp(30)))
        self.edit_layout.add_widget(self.name_input)
        self.edit_layout.add_widget(Label(text="[b]Angle[/b]",markup=True,size_hint_y=None,height=sp(30)))
        self.edit_layout.add_widget(self.angle)
        self.edit_layout.add_widget(Label(text="[b]Axiom[/b]",markup=True,size_hint_y=None,height=sp(30)))
        self.edit_layout.add_widget(self.axiom)
        self.edit_layout.add_widget(Label(text="[b]Rules[/b]",markup=True,size_hint_y=None,height=sp(30)))
        self.edit_layout.add_widget(self.rules)
        self.edit_layout.add_widget((Label(text="[b]Choose rule to visualise[/b]",markup=True,size_hint_y=None,height=sp(30))))
        self.edit_layout.add_widget(self.rule_chooser)
        self.edit_layout.add_widget((Label(text="[b]Change number of iterations[/b]",markup=True,size_hint_y=None,height=sp(30))))
        self.edit_layout.add_widget(self.iterations_buttons)
        self.edit_layout.add_widget((Label(text="[b]L-systems syntax[/b]",markup=True,size_hint_y=None,height=sp(30))))
        syntax_label=ScrollableLabel()
        syntax_label.text = syntax
#        def f(*args,**kwargs): syntax_label.text_size = syntax_label.size
#        syntax_label.bind(size = f)


        self.edit_layout.add_widget(syntax_label)
        self.main_layout.add_widget(self.edit_layout)
        self.main_layout.add_widget(self.image)

        #self.load_lsystem(('quartet', ('fb', {'a': 'fbfa+hfa+fb-fa', 'h': '-', 'b': 'fb+fa-fb-jfbfa', 'j': '+', 'f': ''}, 90.0)))

        self.add_widget(self.main_layout)



    def iter_press(self,instance,*args):
        if instance.text == "+":
            iteration = int(self.iter_label.text)+1
        else:
            iteration = int(self.iter_label.text)-1
            if iteration<0: iteration = 0
        self.image.set_iterations(iteration)
        self.iter_label.text = str(iteration)


    def set_rule_chooser(self):
        if self.lsystem:
            name,(axiom,rules,angle) = self.lsystem
        else:
            rules = {}
        self.rule_chooser.clear_widgets()
        for name in ["axiom"]+sorted(rules.keys()):
            btn = ToggleButton(text=name, group='rules',size_hint_y=None,height=sp(30))
            if name == 'axiom': btn.state = 'down'
            btn.bind(on_press = self.on_rule_choose)
            self.rule_chooser.add_widget(btn)


    def on_rule_choose(self,instance,*args):
        if self.lsystem:
            name,(axiom,rules,angle) = self.lsystem
            new_axiom = instance.text if instance.text!="axiom" else axiom
            self.image.set_lsystem((new_axiom,rules,angle))

    def on_text(self,instance,*args):
        self.get_lsystem()
        if self.lsystem:
            self.image.set_lsystem(self.lsystem[1])
            #self.image.set_iterations(1)
        self.set_rule_chooser()

    def load_lsystem(self,lsystem):
        name,(axiom,rules,angle) = lsystem
        self.name_input.text = name
        self.axiom.text = axiom
        self.angle.text = str(angle)
        self.rules.text = '\n'.join(["%s=%s"%(k,v) for (k,v) in sorted(rules.items())])

    def get_lsystem(self):
        name = self.name_input.text
        axiom = self.axiom.text.replace(" ","")
        angle=''
        try: angle = float(self.angle.text)
        except: angle = 0
        try: rules = dict([x.split("=") for x in self.rules.text.replace(" ","").split("\n") if x])
        except: rules = {}
        self.lsystem = name,(axiom,rules,angle)
Example #4
0
class LSystemsView(Screen):
    def __init__(self, **kwargs):
        #TODO KV!!!!!!
        self.register_event_type('on_edit')
        super(LSystemsView, self).__init__(**kwargs)
        self.name = "view"
        self.main_layout = BoxLayout(orientation='vertical')
        self.buttons_layout = BoxLayout(orientation='horizontal',
                                        size_hint=(1, .05))
        self.labels_layout = BoxLayout(orientation='horizontal',
                                       size_hint=(1, .05))
        self.image = LSystemImage()
        self.image.bind(on_update=self.image_update)

        self.width_choice = DropDown()
        self.width_choice.bind(on_select=self.choose_width)
        self.width_choice_button = Button(text="Line width")
        self.width_choice_button.bind(on_release=self.width_choice.open)

        for i in range(1, 10):
            btn = ToggleButton(text=str(i),
                               group='width_values',
                               size_hint_y=None,
                               height=sp(30))
            if i == 1: btn.state = 'down'
            btn.bind(on_press=lambda btn: self.width_choice.select(btn.text))
            self.width_choice.add_widget(btn)
        self.fractal_choice = DropDown()
        self.fractal_choice.bind(on_select=self.choose_fractal)
        self.file_choice_button = Button(text="File")
        self.choice_button = Button(text="L-System")
        self.file_choice_button.bind(
            on_release=self.on_file_choose)  #self.file_choice.open)
        self.choice_button.bind(on_release=self.fractal_choice.open)
        self.next_button = Button(text="Next")
        self.ls_name = Label(text="")
        self.cur_file = Label(text="")
        self.plus_button = Button(text="+")
        self.minus_button = Button(text="-")
        self.background_button = Button(text="Background")
        self.background_button.bind(on_press=self.choose_background_color)
        self.plus_button.bind(on_press=self.plus)
        self.minus_button.bind(on_press=self.minus)
        self.next_button.bind(on_press=self.next)
        self.edit_button = Button(text="Edit")
        self.edit_button.bind(on_press=self.edit)
        self.segments = Label(text="")
        self.iterations = Label()
        self.buttons_layout.add_widget(self.file_choice_button)
        self.buttons_layout.add_widget(self.choice_button)
        self.buttons_layout.add_widget(self.next_button)
        self.buttons_layout.add_widget(self.plus_button)
        self.buttons_layout.add_widget(self.minus_button)
        self.buttons_layout.add_widget(self.edit_button)
        self.buttons_layout.add_widget(self.background_button)
        self.buttons_layout.add_widget(self.width_choice_button)
        self.labels_layout.add_widget(
            Label(text="[b]File name:[/b]", markup=True))
        self.labels_layout.add_widget(self.cur_file)
        self.labels_layout.add_widget(
            Label(text="[b]L-system name:[/b]", markup=True))
        self.labels_layout.add_widget(self.ls_name)
        self.labels_layout.add_widget(
            Label(text="[b]Number of iterations:[/b]", markup=True))
        self.labels_layout.add_widget(self.iterations)
        self.labels_layout.add_widget(
            Label(text="[b]Number of segments:[/b]", markup=True))
        self.labels_layout.add_widget(self.segments)
        self.main_layout.add_widget(self.image)
        self.main_layout.add_widget(self.buttons_layout)
        self.main_layout.add_widget(self.labels_layout)

        self.file_chooser = LFileChooser()
        self.file_chooser.bind(on_choose=self.choose_file)
        self.popup = Popup(title='Load L-Systems',
                           content=self.file_chooser,
                           size_hint=(None, None),
                           size=(500, 500))

        self.color_picker = BackgroundColorPicker()
        self.color_picker.bind(on_choose=self.change_background_color)
        self.color_picker_popup = Popup(title="Pick color",
                                        content=self.color_picker,
                                        size_hint=(None, None),
                                        size=(500, 500))

        self.add_widget(self.main_layout)

    def choose_background_color(self, *args, **kwargs):
        self.color_picker_popup.open()

    def change_background_color(self, instance, color):
        r, g, b, a = color
        r = int(255 * r)
        g = int(255 * g)
        b = int(255 * b)
        self.image.set_background_color((r, g, b))
        self.color_picker_popup.dismiss()

    def on_edit(self, *args, **kwargs):
        pass

    def on_file_choose(self, *args):
        self.popup.open()

    def edit(self, instance, *args):
        name = self.ls_name.text
        if name: fractal = self.fractals[name][0]
        else: fractal = '', {}, 0
        self.dispatch("on_edit", (name, fractal))

    def set_fractals(self):
        self.fractal_choice.clear_widgets()
        for k in sorted(self.fractals.keys()):
            btn = Button(text=k, size_hint_y=None, height=44)
            btn.bind(
                on_release=lambda btn: self.fractal_choice.select(btn.text))
            self.fractal_choice.add_widget(btn)

    def choose_file(self, instance, text):
        self.cur_file.text = basename(text)
        self.popup.dismiss()
        self.fractals = l_parser(open(l_file_path(text)))
        self.set_fractals()
        self.choose_fractal(None, sorted(self.fractals.keys())[0])

    def choose_width(self, instance, text):
        self.image.set_width(int(text))

    def choose_fractal(self, instance, text):
        self.ls_name.text = text
        self.image.set_lsystem(self.fractals[text][0])
        self.image.set_iterations(1)
        self.iterations.text = str(self.image.get_iterations())

    def image_update(self, instance, num):
        self.segments.text = str(num)

    def next(self, *args):
        cur_name = self.ls_name.text
        if not cur_name: return
        lst = self.fractals.keys()
        i = lst.index(cur_name)
        i += 1
        if i == len(self.fractals): i = 0
        self.choose_fractal(None, lst[i])

    def plus(self, *args):
        self.image.set_iterations(self.image.get_iterations() + 1)
        self.iterations.text = str(self.image.get_iterations())

    def minus(self, *args):
        self.image.set_iterations(self.image.get_iterations() - 1)
        if self.iterations.text != "0":
            self.iterations.text = str(self.image.get_iterations())