Class: Rubygoo::App

Inherits:
Container show all
Extended by:
Publisher
Defined in:
lib/rubygoo/app.rb

Constant Summary collapse

DEFAULT_PARAMS =
{:theme=>'default',:x=>10,:y=>10,:data_dir=>File.join(File.dirname(__FILE__),"..","..","themes"),:mouse_cursor => true}

Instance Attribute Summary collapse

Attributes inherited from Container

#bg_color, #queued_widgets, #rect, #widgets

Attributes inherited from Widget

#container, #enabled, #focus_priority, #focussed, #goo_id, #h, #mouse_over, #opts, #padding_left, #padding_top, #parent, #relative, #visible, #w, #x, #y

Instance Method Summary collapse

Methods inherited from Container

#_draw, #_key_pressed, #_key_released, #_mouse_down, #_mouse_drag, #_mouse_dragging, #_mouse_motion, #_mouse_up, #_update, #add, #added, #clear, #get, #remove, #resize, #tab_to?

Methods inherited from Widget

#_draw, #_focus, #_key_pressed, #_key_released, #_mouse_down, #_mouse_drag, #_mouse_dragging, #_mouse_motion, #_mouse_up, #_unfocus, #_update, #added, #contains?, #disable, #enable, #enabled?, #focussed?, #get, #get_color, goo_prop, #hide, inherited, #key_pressed, #key_released, #modal?, #mouse_down, #mouse_drag, #mouse_dragging, #mouse_enter, #mouse_exit, #mouse_motion, #mouse_over?, #mouse_up, #removed, #show, #tab_to?, #theme_property, #unfocus, #update_rect, #visible?

Methods included from Inflector

#camelize, #classify, #constantize, #dasherize, #demodulize, #foreign_key, #humanize, #inflections, #ordinalize, #pluralize, #singularize, #tableize, #titleize, #underscore

Constructor Details

#initialize(opts = {}, &block) ⇒ App

Returns a new instance of App.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubygoo/app.rb', line 12

def initialize(opts={}, &block)
  merged_opts = DEFAULT_PARAMS.merge opts
  @widgets = []
  @tab_groups = []
  @tab_groups << TabGroup.new

  theme_name = merged_opts[:theme]
  @data_dir = merged_opts[:data_dir]
  @theme_name = theme_name
  @theme = load_theme theme_name
  @renderer = merged_opts[:renderer]

  # should this go here?
  @mouse_cursor = merged_opts[:mouse_cursor]
  if @mouse_cursor
    @mouse = MouseCursor.new
    @mouse.parent = self
    @mouse.app = self.app
    @mouse.added
  end
  @mouse_dragging = false

  super merged_opts, &block
end

Instance Attribute Details

#data_dirObject

Returns the value of attribute data_dir.



10
11
12
# File 'lib/rubygoo/app.rb', line 10

def data_dir
  @data_dir
end

#mouseObject

Returns the value of attribute mouse.



10
11
12
# File 'lib/rubygoo/app.rb', line 10

def mouse
  @mouse
end

#rendererObject

Returns the value of attribute renderer.



10
11
12
# File 'lib/rubygoo/app.rb', line 10

def renderer
  @renderer
end

#tab_groupsObject

Returns the value of attribute tab_groups.



10
11
12
# File 'lib/rubygoo/app.rb', line 10

def tab_groups
  @tab_groups
end

#themeObject

Returns the value of attribute theme.



10
11
12
# File 'lib/rubygoo/app.rb', line 10

def theme
  @theme
end

#theme_dirObject

Returns the value of attribute theme_dir.



10
11
12
# File 'lib/rubygoo/app.rb', line 10

def theme_dir
  @theme_dir
end

#theme_nameObject

Returns the value of attribute theme_name.



10
11
12
# File 'lib/rubygoo/app.rb', line 10

def theme_name
  @theme_name
end

Instance Method Details

#add_modal(widget) ⇒ Object

redirects all events to this widget



122
123
124
125
126
127
128
# File 'lib/rubygoo/app.rb', line 122

def add_modal(widget)
  @modal_widgets << widget
  tg = TabGroup.new
  tg.add(widget)
  self.app.add_tab_group tg
  add widget
end

#add_tab_group(tg) ⇒ Object



61
62
63
# File 'lib/rubygoo/app.rb', line 61

def add_tab_group(tg)
  @tab_groups << tg
end

#add_tabbed_widget(w) ⇒ Object



57
58
59
# File 'lib/rubygoo/app.rb', line 57

def add_tabbed_widget(w)
  @tab_groups[0].add w
end

#appObject



37
38
39
# File 'lib/rubygoo/app.rb', line 37

def app()
  self
end

#draw(screen) ⇒ Object



46
47
48
49
50
51
# File 'lib/rubygoo/app.rb', line 46

def draw(screen)
  @renderer.start_drawing
  _draw @renderer
  @mouse._draw @renderer if @mouse
  @renderer.finish_drawing
end

#focus(w) ⇒ Object



53
54
55
# File 'lib/rubygoo/app.rb', line 53

def focus(w)
  @tab_groups.last.focus w
end

#focus_backObject



65
66
67
# File 'lib/rubygoo/app.rb', line 65

def focus_back()
  @tab_groups.last.previous
end

#focus_forwardObject



69
70
71
# File 'lib/rubygoo/app.rb', line 69

def focus_forward()
  @tab_groups.last.next
end

#load_theme(theme_name) ⇒ Object



41
42
43
44
# File 'lib/rubygoo/app.rb', line 41

def load_theme(theme_name)
  @theme_dir = File.join(@data_dir,theme_name)
  @theme = YAML::load_file(File.join(@theme_dir,"config.yml"))
end

distribute our keyboard events to our modals first



151
152
153
154
155
156
157
158
159
# File 'lib/rubygoo/app.rb', line 151

def modal_keyboard_call(meth, event)
  if @modal_widgets.empty?
    @widgets.select{|w|w.enabled? and w.focussed?}.each do |w|
      w.send meth, event 
    end
  else
    @modal_widgets.last.send meth, event
  end
end

distribute our mouse events to our modals first



140
141
142
143
144
145
146
147
148
# File 'lib/rubygoo/app.rb', line 140

def modal_mouse_call(meth, event)
  if @modal_widgets.empty?
    @widgets.select{|w|w.enabled? and (w.contains? event.data[:x],event.data[:y] or meth == :_mouse_motion) }.each do |w|
      w.send meth, event 
    end
  else
    @modal_widgets.last.send meth, event
  end
end

#on_event(event) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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/rubygoo/app.rb', line 73

def on_event(event)
  fire :evented, event
  case event.event_type
  when :key_released
    modal_keyboard_call :_key_released, event
  when :key_pressed
    case event.data[:key]
    when K_TAB
      if event.data[:mods].include? K_LSHIFT or event.data[:mods].include? K_RSHIFT
        focus_back
      else
        focus_forward
      end
    else
      modal_keyboard_call :_key_pressed, event
    end
  when :mouse_down
    modal_mouse_call :_mouse_down, event
    # TODO: I know this is simplistic and doesn't account for which button
    # is being pressed/released
    @mouse_start_x = event.data[:x]
    @mouse_start_y = event.data[:y]
  when :mouse_up
    x = event.data[:x]
    y = event.data[:y]
    if @mouse_start_x == x and @mouse_start_y == y
      modal_mouse_call :_mouse_up, event
    else
      event.data[:start_x] = @mouse_start_x
      event.data[:start_y] = @mouse_start_y
      modal_mouse_call :_mouse_drag, event
    end
    @mouse_start_x = nil
    @mouse_start_y = nil
  when :mouse_motion
    x = event.data[:x]
    y = event.data[:y]
    if @mouse_start_x
      event.data[:start_x] = @mouse_start_x
      event.data[:start_y] = @mouse_start_y
      modal_mouse_call :_mouse_dragging, event
    else
      modal_mouse_call :_mouse_motion, event
    end
    @mouse._mouse_motion event if @mouse
  end
end

#pop_tab_groupObject



135
136
137
# File 'lib/rubygoo/app.rb', line 135

def pop_tab_group()
  @tab_groups.pop
end

#remove_modal(widget) ⇒ Object



130
131
132
133
# File 'lib/rubygoo/app.rb', line 130

def remove_modal(widget)
  self.app.pop_tab_group
  remove widget
end

#update(time) ⇒ Object



161
162
163
# File 'lib/rubygoo/app.rb', line 161

def update(time)
  _update(time)
end