Class: ShenmeGUI::Control::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/shenmegui/controls.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



74
75
76
77
78
79
80
81
# File 'lib/shenmegui/controls.rb', line 74

def initialize(params={})
  @properties = {}
  update_properties(params)
  @id = ShenmeGUI.elements.size
  ShenmeGUI.elements << self
  @children = []
  @events = {}
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



6
7
8
# File 'lib/shenmegui/controls.rb', line 6

def children
  @children
end

#eventsObject

Returns the value of attribute events.



6
7
8
# File 'lib/shenmegui/controls.rb', line 6

def events
  @events
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/shenmegui/controls.rb', line 6

def id
  @id
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/shenmegui/controls.rb', line 6

def parent
  @parent
end

#propertiesObject

Returns the value of attribute properties.



6
7
8
# File 'lib/shenmegui/controls.rb', line 6

def properties
  @properties
end

Class Method Details

.property(*arr) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shenmegui/controls.rb', line 21

def self.property(*arr)
  arr.each do |x|
    define_method(x) do
      @properties[x]
    end

    define_method("#{x}=") do |v|
      update_properties({x => v})
      sync
    end
  end
end

.shortcut(prop) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/shenmegui/controls.rb', line 34

def self.shortcut(prop)
  define_method(:initialize) do |x=nil, params={}|
    params.merge!({prop => x})
    super(params)
  end

end

Instance Method Details

#add_hook(obj) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/shenmegui/controls.rb', line 8

def add_hook(obj)
  case obj
    when String
      HookedString.new(obj, self)
    when Array
      HookedArray.new(obj, self)
    when Hash
      HookedHash.new(obj, self)
    else
      obj
  end
end

#focusObject



58
59
60
61
# File 'lib/shenmegui/controls.rb', line 58

def focus
  msg = "focus:#{@id}"
  ShenmeGUI.socket.send(msg)
end

#render(material = {}) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/shenmegui/controls.rb', line 83

def render(material = {})
  gem_path = $LOADED_FEATURES.grep(/.*\/lib\/shenmegui/)[0].match(/(.*)\/lib/)[1]
  template_path = gem_path + "/templates"
  type = self.class.name.match(/(?:.*::)(.+)/)[1]
  template = ::ERB.new File.read("#{template_path}/#{type}.erb")
  content = children.collect{|x| x.render}.join("\n")
  template.result(binding)
end

#syncObject



51
52
53
54
55
56
# File 'lib/shenmegui/controls.rb', line 51

def sync
  data = @properties
  validate @properties
  msg = "sync:#{@id}->#{data.to_json}"
  ShenmeGUI.socket.send(msg)
end

#sync_eventsObject



68
69
70
71
72
# File 'lib/shenmegui/controls.rb', line 68

def sync_events
  data = @events.keys
  msg = "add_event:#{@id}->#{data.to_json}"
  ShenmeGUI.socket.send(msg)
end

#update_properties(data) ⇒ Object



63
64
65
66
# File 'lib/shenmegui/controls.rb', line 63

def update_properties(data)
  data = Hash[data.keys.collect(&:to_sym).zip(data.values.collect{|x| add_hook(x)})]
  @properties.update(data)
end

#validate(params) ⇒ Object



92
93
# File 'lib/shenmegui/controls.rb', line 92

def validate(params)
end