miércoles, 25 de julio de 2012

Gtk_Timeout - Pon un timer en tu vida.


 En esta ejemplo hemos añadido al ejemplo de TextView una función que es llamada por el Timer de Gtk. Con ello hemos conseguido crear un texto animado. Cuando pulsamos el boton activamos el Timer.
 Timeout.add(50, mover) --> Llama a mover con velocidad de 50 ticks

  Atención puesto que "mover" tiene que ser una funcion de tipo booleano y retornar true para continuar en funcionamiento.

Nota: Booleano es de tipo lógico y sus valores posibles son true o false

Muy sencillo:


[indent=4]
uses
    Gtk
init

    Gtk.init (ref args)
    var test = new Miventana ()
    test.show_all ()
  
    Gtk.main ()
  
  
class Miventana : Window
    texto1: Gtk.TextView = new TextView
  
    rojo: Gtk.TextTag
    blanco: Gtk.TextTag
    ti1: Gtk.TextIter;     ti2: Gtk.TextIter;    ti3: Gtk.TextIter;    ti4: Gtk.TextIter
  
    acolor : Gdk.Color
  
    boton1: Button= new Button.with_label ("Pulsa este botón")
    contador: int = 0
    frase:string ="La casa esta rota, pero ya la estamos arreglando. La casa esta rota, pero ya la estamos arreglando."
    vbox : VBox = new VBox (true, 5)
      
    init
        Gdk.Color.parse("#1F2AF1", out acolor)
        rojo=texto1.buffer.create_tag("rojo", "foreground-gdk", acolor);
        blanco=texto1.buffer.create_tag("blanco", "foreground", "White");
      
        texto1.editable = false
        texto1.cursor_visible = false
        title = "Ventana de prueba"
        default_height = 360
        default_width = 560
        window_position = WindowPosition.CENTER
        boton1.clicked.connect (pulsado)
      
        destroy.connect(Gtk.main_quit)
        vbox.add (boton1)
        vbox.add(texto1)
        add (vbox)
        texto1.set_size_request(100, 200)
        boton1.set_size_request(100,10)
        texto1.buffer.text = frase
        texto1.buffer.get_iter_at_mark (out ti1, texto1.buffer.get_insert ())
        texto1.buffer.get_iter_at_mark (out ti2, texto1.buffer.get_insert ())
        texto1.buffer.get_iter_at_mark (out ti3, texto1.buffer.get_insert ())
        texto1.buffer.get_iter_at_mark (out ti4, texto1.buffer.get_insert ())
  
        Timeout.add(50, mover)
      
    def pulsado (btn : Button)
        contador=0
      
    def mover():bool
        contador+=1
      
        ti1.set_offset(0)
        ti2.set_offset(contador)
        ti3.set_offset(contador+5)
        ti4.set_offset(longitud(frase))
      
        texto1.buffer.remove_all_tags (ti1,ti4)
        texto1.buffer.apply_tag (blanco,ti1,ti2)
        texto1.buffer.apply_tag (rojo,ti2,ti3)
        texto1.buffer.apply_tag (blanco,ti3,ti4)
        return true



2 comentarios:

  1. I am getting the error:

    timer.gs:63.24-63.31: error: The name `longitud' does not exist in the context of `Miventana.mover'
    ti4.set_offset(longitud(frase))
    ^^^^^^^^

    Any ideas on how to solve it?

    ResponderEliminar
  2. ups. Instead longitud use length function. Thanks.

    ResponderEliminar