Class: DroidProj::Android::Drawable

Inherits:
Object
  • Object
show all
Defined in:
lib/droidproj/drawable.rb

Constant Summary collapse

FINAL_FILE_PREFIX =
"x_"
SIZES =
[:hdpi, :ldpi, :mdpi, :xhdpi]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDrawable

Returns a new instance of Drawable.



15
16
17
18
# File 'lib/droidproj/drawable.rb', line 15

def initialize
  @active_state = {}
  @states = {}
end

Instance Attribute Details

#active_stateObject

The current state being set for this drawable



10
11
12
# File 'lib/droidproj/drawable.rb', line 10

def active_state
  @active_state
end

#nameObject

The final name for this drawable, used in your code



8
9
10
# File 'lib/droidproj/drawable.rb', line 8

def name
  @name
end

#statesObject

Hash of STATE_HASH =>



6
7
8
# File 'lib/droidproj/drawable.rb', line 6

def states
  @states
end

Instance Method Details

#sorted_statesObject

Internal: Sort self.states by how long each state is

(because Android wants the XML file to be sorted by specificity)

Returns an Array relating to self.states as [:key, :value], sorted by :key length



126
127
128
129
130
131
132
133
134
135
# File 'lib/droidproj/drawable.rb', line 126

def sorted_states
  keys = self.states.keys
  keys.sort_by! {|x|
    x.to_s.length
  }.reverse!
  sorted_hash = keys.map {|key|
    [key, self.states[key]]
  }
  sorted_hash
end

#state(active_state, &block) ⇒ Object

Public: The DSL for creating drawables corresponding to a certain state

active_state - The Hash of options to set the active_state &block - A block to be execute with the new active_state

Examples

drawable.state(enabled: true, focused: false) do
  # In this block, all drawables are set with the above state
  hdpi 'image.png'
end

Returns the Drawable instance being used



33
34
35
36
37
38
# File 'lib/droidproj/drawable.rb', line 33

def state(active_state, &block)
  self.active_state = Hash[active_state.map {|k, v| [k.to_sym, v] }]
  block.call
  self.active_state = {}
  self
end

#to_size_bucketsObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/droidproj/drawable.rb', line 81

def to_size_buckets
  size_buckets = {}
  self.states.each do |state, drawable_states|
    drawable_states.each do |drawable_state|
      size_buckets[drawable_state.size] ||= []
      size_buckets[drawable_state.size] << drawable_state
    end
  end
  size_buckets
end

#xml_stringObject

Public: The StateList XML representation of this drawable, used by Android

Returns the String representation



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/droidproj/drawable.rb', line 95

def xml_string
  str =
%Q{<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
}

  self.sorted_states.each do |state, drawable_states|
    drawable_states.each do |drawable_state|
      if !str.include?(drawable_state.xml_string)
        str <<
%Q{    #{drawable_state.xml_string}
}
      end
    end
  end

  default_drawable = "<item android:drawable=\"@drawable/#{FINAL_FILE_PREFIX}#{self.name}\" />\n"
  if !str.include?(default_drawable)
    str <<
%Q{    #{default_drawable}}
  end

      str <<
%Q{</selector>}
end