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
82
# File 'lib/shenmegui/controls.rb', line 74

def initialize(params={})
  @properties = {}
  update_properties(self.class.default_properties.dup) if self.class.default_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

.default(params) ⇒ Object

注意@default_properties都是未上钩子的属性,因为钩子本身需要控件的self,而此时控件还未实例化



31
32
33
34
# File 'lib/shenmegui/controls.rb', line 31

def self.default(params)
  @default_properties ||= {}
  @default_properties.merge!(params)
end

.default_propertiesObject



36
37
38
# File 'lib/shenmegui/controls.rb', line 36

def self.default_properties
  @default_properties
end

.property(*arr) ⇒ Object

读取时直接从@properties读取,写入时则调用update_properties这个统一的接口



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

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



22
23
24
25
26
27
28
# File 'lib/shenmegui/controls.rb', line 22

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

end

Instance Method Details

#before_syncObject



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

def before_sync
end

#focusObject



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

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

#render(material = {}) ⇒ Object



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

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

#syncObject



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

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

#sync_eventsObject



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

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

#update_properties(data) ⇒ Object



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

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