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, #h, #mouse_over, #opts, #parent, #relative, #visible, #w, #x, #x_pad, #y, #y_pad

Instance Method Summary collapse

Methods inherited from Container

#_draw, #_key_pressed, #_key_released, #_mouse_down, #_mouse_drag, #_mouse_motion, #_mouse_up, #_update, #add, #added, #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_color, #hide, #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?

Constructor Details

#initialize(opts = {}) ⇒ 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
# File 'lib/rubygoo/app.rb', line 12

def initialize(opts={})
  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]
  super merged_opts

  # 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
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



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

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



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

def add_tab_group(tg)
  @tab_groups << tg
end

#add_tabbed_widget(w) ⇒ Object



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

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

#appObject



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

def app()
  self
end

#draw(screen) ⇒ Object



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

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

#focus(w) ⇒ Object



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

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

#focus_backObject



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

def focus_back()
  @tab_groups.last.previous
end

#focus_forwardObject



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

def focus_forward()
  @tab_groups.last.next
end

#load_theme(theme_name) ⇒ Object



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

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



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

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



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

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



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rubygoo/app.rb', line 72

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



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

def pop_tab_group()
  @tab_groups.pop
end

#remove_modal(widget) ⇒ Object



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

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

#update(time) ⇒ Object



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

def update(time)
  _update(time)
end