Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Metronome app built in under 10 minutes with Glimmer DSL for SWT(My iPhone metronome broke during drum practice, so I wrote my own with Glimmer DSL for SWT)
Metronome app built in under 10 minutes with Glimmer DSL for SWT [message #1838824] Fri, 05 March 2021 18:40
Andy Maleh is currently offline Andy MalehFriend
Messages: 75
Registered: March 2020
Location: Montreal, Quebec, Canada
Member
While going through drum pad practice yesterday, I noticed that my iPhone metronome app was broken after the latest update as it was ticking up on the second beat, not the first anymore. It was a small thing, but quite annoying, so I deleted the app and wrote my own Metronome app in Glimmer DSL for SWT in under 10 minutes for the initial working 4/4 rhythm version (please don't mind the initial rough code).

# From: https://github.com/AndyObtiva/glimmer-dsl-swt/blob/master/docs/reference/GLIMMER_SAMPLES.md#metronome

require 'glimmer-dsl-swt'

class Metronome
  class Beat
    attr_accessor :on
    
    def off!
      self.on = false
    end
    
    def on!
      self.on = true
    end
  end
  
  class Rhythm
    attr_accessor :beats, :signature_top, :signature_bottom
    
    def initialize(signature_top, signature_bottom)
      @signature_top = signature_top
      @signature_bottom = signature_bottom
      @beats = @signature_top.times.map {Beat.new}
    end
  end

  include Glimmer::UI::CustomShell
      
  attr_reader :beats
  
  before_body {
    @rhythm = Rhythm.new(4, 4)
    @beats = @rhythm.beats
  }
  
  body {
    shell {
      grid_layout 4, true
      text 'Glimmer Metronome'
      minimum_size 200, 200
            
      4.times { |n|
        canvas {
          layout_data {
            width_hint 50
            height_hint 50
          }
          oval(0, 0, 50, 50) {
            background bind(self, "beats[#{n}].on") {|on| on ? :red : :yellow}
          }
        }
      }
      
      on_swt_show {
        @thread ||= Thread.new {
          4.times.cycle { |n|
            sleep(0.25)
            beats.each(&:off!)
            beats[n].on!
          }
        }
      }
      
      on_widget_disposed {
        @thread.kill # safe since no stored data is involved
      }
    }
  }
end

Metronome.launch


https://1.bp.blogspot.com/-B8Fjsr1O114/YELUjOngRAI/AAAAAAAABqQ/2t1c41R-9PcNrW8-aL6N8sTAzI8AcxuYQCLcBGAsYHQ/s320/Screen%2BShot%2B2021-03-05%2Bat%2B8.00.36%2BPM.png

Anyways, I later revised it, adding a beat count option in addition to the BPM (beats per minute), with auto-grow/shrink support for the window shell when adding/subtracting beats.

https://github.com/AndyObtiva/glimmer-dsl-swt/raw/master/images/glimmer-metronome.gif

Learn more at this blog post:
https://andymaleh.blogspot.com/2021/03/glimmer-metronome-hello-canvas.htmlhttps://andymaleh.blogspot.com/2021/03/glimmer-metronome-hello-canvas.html


EclipseCon / EclipseWorld / Agile Conference Speaker
Open-Source Software Author of Glimmer DSL for SWT

[Updated on: Sat, 06 March 2021 02:00]

Report message to a moderator

Previous Topic:Mandelbrot Fractal with Glimmer DSL for SWT
Next Topic:Draw2D Spike in Glimmer DSL for SWT
Goto Forum:
  


Current Time: Mon May 06 15:00:41 GMT 2024

Powered by FUDForum. Page generated in 0.02928 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top