Sha256: 171e3f777de2b89ba2a990bcb67397b80a5ddb3e46ce1cd15687dfe51b0a4dca

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# -*- coding: utf-8 -*-


require 'rays/image'
require 'reflex/application'
require 'reflex/window'
require 'spacy/text'


module Spacy


  class Window < Reflex::Window

    def initialize ()
      super
      set :title,  Reflex::Application.instance.name
      set :bounds, 100, 100, 600, 500
      @text = Text.new
      p = painter
      p.background 1
      p.fill 0
      p.stroke nil
      p.font Rays::Font.new "Menlo", 12
    end

    def update (dt)
      @t ||= Time.now
      @c ||= 0
      redraw
      @c += 1
      @fps = @c / (Time.now - @t)
      puts "#{@fps} FPS" if (@c % 100) == 0
    end

    def draw ()
      super
      paint do |p|
        h = p.font.height
        draw_text p, h
        draw_cursor p, h
        draw_fps p
      end
    end

    def close ()
      #Profiler__.print_profile STDOUT
      super
    end

    def key_down (key)
      @text.selection = key.chars
    end

    def key_up (key)
    end

    def points_down (points)
    end

    def points_up (points)
    end

    def points_moved (points)
    end

    private

      def draw_text (p, lineheight)
        y = 0
        @text.each do |line|
          p.text line, 0, y
          y += lineheight
        end
      end

      def draw_cursor (p, lineheight)
        row, col = @text.cursor
        x = p.font.width @text[row][0..col]
        p.rect x, row * lineheight, 4, lineheight
      end

      def draw_fps (p)
        pos = bounds(0)[1].move_by(-64, -20).to_a(2)
        p.text "#{@fps.to_i} FPS", *pos if @fps
      end

  end# Window


end # Spacy

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spacy-0.1.6 lib/spacy/window.rb
spacy-0.1.5 lib/spacy/window.rb
spacy-0.1.4 lib/spacy/window.rb