Module: OBF::Picto4me

Defined in:
lib/obf/picto4me.rb

Class Method Summary collapse

Class Method Details

.to_external(zip_path) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/obf/picto4me.rb', line 2

def self.to_external(zip_path)
  boards = []
  images = []
  sounds = []
  OBF::Utils.load_zip(zip_path) do |zipper|
    json = JSON.parse(zipper.read('*.js'))
    locale = json['locale']
    json['sheets'].each_with_index do |sheet, idx|
      board = OBF::Utils.obf_shell
      board['id'] = idx.to_s
      board['locale'] = locale
      board['name'] = sheet['title']['text']
      board['ext_picto4me_title'] = sheet['title']
      board['ext_picto4me_cellsize'] = sheet['cellsize']
      board['ext_picto4me_pictoOverrule'] = sheet['pictoOverrule']
      board['ext_picto4me_showPictoTitles'] = sheet['showPictoTitles']
      board['ext_picto4me_pictoBorder'] = sheet['pictoBorder']
      grid = []
      sheet['rows'].times do 
        grid << [nil] * sheet['columns']
      end
      board['grid'] = {
        'rows' => sheet['rows'],
        'columns' => sheet['columns'],
        'order' => grid
      }
      sheet['pictos'].each_with_index do |picto, jdx|
        next unless picto
        button = {}
        button['id'] = board['id'] + ":" + picto['id']
        button['label'] = picto['title']['text']
        button['vocalization'] = picto['description']['text']
        button['border_color'] = picto['borderColor'] unless picto['borderColor'] == 'transparent'
        button['background_color'] = picto['bgColor'] unless picto['bgColor'] == 'transparent'
        button['ext_picto4me_lang'] = picto['lang']
        button['ext_picto4me_description'] = picto['description']
        button['ext_picto4me_title'] = picto['title']
        button['ext_picto4me_overlay'] = picto['overlay']
        button['ext_picto4me_source'] = picto['source']
        button['ext_picto4me_key'] = picto['key']
        button['ext_picto4me_categories'] = picto['categories']
        button['ext_picto4me_size'] = picto['size']
        
        if picto['imageurl']
          image = {}
          image['id'] = 'img:' + button['id']
          
          attrs = zipper.read_as_data(picto['imageurl'][1..-1])
          raise "didn't work" unless attrs['data']
          image['data'] = attrs['data']
          image['width'] = attrs['width']
          image['height'] = attrs['height']
          image['content_type'] = attrs['content_type']

          images << image
          button['image_id'] = image['id']
        end
        if picto['soundurl']
          sound = {}
          sound['id'] = 'snd:' + button['id']
          
          attrs = zipper.read_as_data(picto['soundurl'][1..-1])
          raise "didn't work" unless attrs['data']
          sound['data'] = attrs['data']
          sound['content_type'] = attrs['content_type']

          sounds << sound
          button['sound_id'] = sound['id']
        end
        if picto['link'] && json['sheets'][picto['link'].to_i]
          button['load_board'] = {'id' => picto['link']}
        end
        board['buttons'] << button
        row = (jdx / sheet['columns']).floor.to_i
        col = jdx % sheet['columns']
        board['grid']['order'][row][col] = button['id']
      end
      boards << board
    end
  end
  images.uniq!
  sounds.uniq!
  if boards.length == 1
    board = boards[0]
    board['images'] = images
    board['sounds'] = sounds
    return board
  else
    return {
      'boards' => boards,
      'images' => images,
      'sounds' => sounds
    }
  end
end