rubygoo 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,53 +1,67 @@
1
- class GosuRenderAdapter
1
+ module Rubygoo
2
+ class GosuRenderAdapter
2
3
 
3
- def initialize(window)
4
- @window = window
5
- end
4
+ def initialize(window)
5
+ @window = window
6
+ end
6
7
 
7
- def draw_box(x1,y1,x2,y2,color)
8
- c = convert_color(color)
9
- @window.draw_quad x1, y1, c, x2, y1, c, x1, y2, c, x2, y2, c
10
- end
8
+ def draw_box_filled(x1,y1,x2,y2,color)
9
+ c = convert_color(color)
10
+ @window.draw_quad x1, y1, c, x2, y1, c, x1, y2, c, x2, y2, c
11
+ end
11
12
 
12
- # fill in a rect with color or full screen if no color
13
- def fill(color,rect=nil)
14
- if rect.nil?
15
- draw_box 0, 0, @window.width, @window.height, color
16
- else
17
- draw_box rect[0], rect[1], rect[2], rect[3], color
13
+ def draw_box(x1,y1,x2,y2,color)
14
+ c = convert_color(color)
15
+ @window.draw_line x1, y1, c, x2, y1, c
16
+ @window.draw_line x2, y1, c, x2, y2, c
17
+ @window.draw_line x2, y2, c, x1, y2, c
18
+ @window.draw_line x1, y2, c, x1, y1, c
18
19
  end
19
- end
20
20
 
21
- # make static for now for migration ease of rendering fonts
22
- def convert_color(goo_color)
23
- Gosu::Color.new goo_color.a,goo_color.r,goo_color.g,goo_color.b
24
- end
21
+ # fill in a rect with color or full screen if no color
22
+ def fill(color,rect=nil)
23
+ if rect.nil?
24
+ draw_box_filled 0, 0, @window.width, @window.height, color
25
+ else
26
+ draw_box_filled rect[0], rect[1], rect[2]+rect[0], rect[3]+rect[1], color
27
+ end
28
+ end
29
+
30
+ # make static for now for migration ease of rendering fonts
31
+ def convert_color(goo_color)
32
+ Gosu::Color.new goo_color.a,goo_color.r,goo_color.g,goo_color.b
33
+ end
25
34
 
26
- def start_drawing(); end
35
+ def start_drawing(); end
27
36
 
28
- def finish_drawing(); end
37
+ def finish_drawing(); end
29
38
 
30
- def draw_image(img, x, y)
31
- # z is unused here
32
- img.draw x, y, 1
33
- end
39
+ def draw_image(img, x, y, color=nil)
40
+ # z is unused here
41
+ if color
42
+ img.draw x, y, 1,1,1,convert_color(color)
43
+ else
44
+ img.draw x, y, 1
45
+ end
46
+ end
34
47
 
35
- def size_text(text, font_file, font_size)
36
- @font_cache ||= {}
37
- @font_cache[font_file] ||= {}
38
- font = @font_cache[font_file][font_size] ||= Font.new(@window, font_file, font_size)
48
+ def size_text(text, font_file, font_size)
49
+ @font_cache ||= {}
50
+ @font_cache[font_file] ||= {}
51
+ font = @font_cache[font_file][font_size] ||= Font.new(@window, font_file, font_size)
39
52
 
40
- return [font.text_width(text),font.height]
41
- end
53
+ return [font.text_width(text),font.height]
54
+ end
42
55
 
43
- def render_text(text, font_file, font_size, color)
44
- @font_cache ||= {}
45
- @font_cache[font_file] ||= {}
46
- font = @font_cache[font_file][font_size] ||= Font.new(@window, font_file, font_size)
56
+ def render_text(text, font_file, font_size, color)
57
+ @font_cache ||= {}
58
+ @font_cache[font_file] ||= {}
59
+ font = @font_cache[font_file][font_size] ||= Font.new(@window, font_file, font_size)
47
60
 
48
- # TODO how do you set the color here?
49
- text_image = Image.from_text(@window, text, font_file, font_size, 2, 9999, :left)
50
- end
61
+ # TODO how do you set the color here?
62
+ text_image = Image.from_text(@window, text, font_file, font_size, 2, font.text_width(text).ceil+text.length*2, :left)
63
+ end
51
64
 
52
65
 
66
+ end
53
67
  end
@@ -1,37 +1,44 @@
1
- class RubygameAppAdapter
1
+ require 'rubygame'
2
+ include Rubygame
3
+ include Mouse
4
+ include Key
2
5
 
3
- def initialize(app)
4
- @app = app
5
- end
6
+ module Rubygoo
6
7
 
7
- def update(time)
8
- @app.update time
9
- end
8
+ class RubygameAppAdapter
10
9
 
11
- def draw(target)
12
- @app.draw target
13
- end
10
+ def initialize(app)
11
+ @app = app
12
+ end
14
13
 
15
- # TODO convert keys?!?
16
- def on_event(event)
17
- case event
18
- when KeyUpEvent
19
- @app.on_event GooEvent.new(:key_released, {
20
- :key => event.key, :mods => event.mods, :string => event.string})
21
- when KeyDownEvent
22
- @app.on_event GooEvent.new(:key_pressed, {
23
- :key => event.key, :mods => event.mods, :string => event.string})
24
- when MouseUpEvent
25
- @app.on_event GooEvent.new(:mouse_up, {
26
- :x => event.pos[0], :y => event.pos[1], :button => event.button})
27
- when MouseDownEvent
28
- @app.on_event GooEvent.new(:mouse_down, {
29
- :x => event.pos[0], :y => event.pos[1], :button => event.button})
30
- when MouseMotionEvent
31
- @app.on_event GooEvent.new(:mouse_motion, {
32
- :x => event.pos[0], :y => event.pos[1]})
14
+ def update(time)
15
+ @app.update time
33
16
  end
34
- end
35
17
 
18
+ def draw(target)
19
+ @app.draw target
20
+ end
21
+
22
+ # TODO convert keys?!?
23
+ def on_event(event)
24
+ case event
25
+ when KeyUpEvent
26
+ @app.on_event GooEvent.new(:key_released, {
27
+ :key => event.key, :mods => event.mods, :string => event.string})
28
+ when KeyDownEvent
29
+ @app.on_event GooEvent.new(:key_pressed, {
30
+ :key => event.key, :mods => event.mods, :string => event.string})
31
+ when MouseUpEvent
32
+ @app.on_event GooEvent.new(:mouse_up, {
33
+ :x => event.pos[0], :y => event.pos[1], :button => event.button})
34
+ when MouseDownEvent
35
+ @app.on_event GooEvent.new(:mouse_down, {
36
+ :x => event.pos[0], :y => event.pos[1], :button => event.button})
37
+ when MouseMotionEvent
38
+ @app.on_event GooEvent.new(:mouse_motion, {
39
+ :x => event.pos[0], :y => event.pos[1]})
40
+ end
41
+ end
42
+ end
36
43
 
37
44
  end
@@ -1,51 +1,53 @@
1
- class RubygameRenderAdapter
1
+ module Rubygoo
2
+ class RubygameRenderAdapter
2
3
 
3
- def initialize(screen)
4
- @screen = screen
5
- TTF.setup
6
- end
4
+ def initialize(screen)
5
+ @screen = screen
6
+ TTF.setup
7
+ end
7
8
 
8
- def draw_box(x1,y1,x2,y2,color)
9
- @screen.draw_box [x1,y1], [x2,y2], convert_color(color)
10
- end
9
+ def draw_box(x1,y1,x2,y2,color)
10
+ @screen.draw_box [x1,y1], [x2,y2], convert_color(color)
11
+ end
11
12
 
12
- # fill in a rect with color or full screen if no color
13
- def fill(color,rect=nil)
14
- if rect.nil?
15
- @screen.fill convert_color(color)
16
- else
17
- @screen.fill convert_color(color), rect
13
+ # fill in a rect with color or full screen if no color
14
+ def fill(color,rect=nil)
15
+ if rect.nil?
16
+ @screen.fill convert_color(color)
17
+ else
18
+ @screen.fill convert_color(color), rect
19
+ end
18
20
  end
19
- end
20
21
 
21
- def convert_color(goo_color)
22
- [goo_color.r,goo_color.g,goo_color.b,goo_color.a]
23
- end
22
+ def convert_color(goo_color)
23
+ [goo_color.r,goo_color.g,goo_color.b,goo_color.a]
24
+ end
24
25
 
25
- def start_drawing(); end
26
+ def start_drawing(); end
26
27
 
27
- def finish_drawing()
28
- @screen.flip
29
- end
28
+ def finish_drawing()
29
+ @screen.flip
30
+ end
30
31
 
31
- def draw_image(img, x, y)
32
- img.blit @screen, [x,y]
33
- end
32
+ def draw_image(img, x, y, color=nil)
33
+ img.blit @screen, [x,y]
34
+ end
34
35
 
35
- def size_text(text, font_file, font_size)
36
- @font_cache ||= {}
37
- @font_cache[font_file] ||= {}
38
- font = @font_cache[font_file][font_size] ||= TTF.new(font_file, font_size)
36
+ def size_text(text, font_file, font_size)
37
+ @font_cache ||= {}
38
+ @font_cache[font_file] ||= {}
39
+ font = @font_cache[font_file][font_size] ||= TTF.new(font_file, font_size)
39
40
 
40
- font.size_text text
41
- end
41
+ font.size_text text
42
+ end
42
43
 
43
- def render_text(text, font_file, font_size, color)
44
- @font_cache ||= {}
45
- @font_cache[font_file] ||= {}
46
- font = @font_cache[font_file][font_size] ||= TTF.new(font_file, font_size)
44
+ def render_text(text, font_file, font_size, color)
45
+ @font_cache ||= {}
46
+ @font_cache[font_file] ||= {}
47
+ font = @font_cache[font_file][font_size] ||= TTF.new(font_file, font_size)
47
48
 
48
- text_image = font.render text, true, convert_color(color)
49
- end
49
+ text_image = font.render text, true, convert_color(color)
50
+ end
50
51
 
52
+ end
51
53
  end
@@ -1,80 +1,93 @@
1
- class App < Container
1
+ module Rubygoo
2
+ class App < Container
2
3
 
3
- DEFAULT_PARAMS = {:theme=>'default',:x=>10,:y=>10,:width=>600,:height=>480,:data_dir=>File.join(File.dirname(__FILE__),"..","..","themes")}
4
- attr_accessor :theme_name, :theme, :data_dir, :theme_dir, :renderer
4
+ DEFAULT_PARAMS = {:theme=>'default',:x=>10,:y=>10,:width=>600,:height=>480,:data_dir=>File.join(File.dirname(__FILE__),"..","..","themes"),:mouse_cursor => true}
5
+ attr_accessor :theme_name, :theme, :data_dir, :theme_dir, :renderer
5
6
 
6
- def initialize(opts={})
7
- merged_opts = DEFAULT_PARAMS.merge opts
8
- @widgets = []
9
- @tabbed_widgets = []
10
- @focussed_widget = 0
11
- theme_name = merged_opts[:theme]
12
- @data_dir = merged_opts[:data_dir]
13
- @theme_name = theme_name
14
- @theme = load_theme theme_name
15
- @renderer = merged_opts[:renderer]
16
- super merged_opts
17
- end
7
+ def initialize(opts={})
8
+ merged_opts = DEFAULT_PARAMS.merge opts
9
+ @widgets = []
10
+ @tabbed_widgets = []
11
+ @focussed_widget = 0
12
+ theme_name = merged_opts[:theme]
13
+ @data_dir = merged_opts[:data_dir]
14
+ @theme_name = theme_name
15
+ @theme = load_theme theme_name
16
+ @renderer = merged_opts[:renderer]
17
+ super merged_opts
18
18
 
19
- def app()
20
- self
21
- end
19
+ # should this go here?
20
+ @mouse_opt = merged_opts[:mouse_cursor]
21
+ if @mouse_opt
22
+ @mouse = MouseCursor.new
23
+ @mouse.parent = self
24
+ @mouse.app = self.app
25
+ @mouse.added
26
+ end
27
+ end
22
28
 
23
- def load_theme(theme_name)
24
- @theme_dir = File.join(@data_dir,theme_name)
25
- @theme = YAML::load_file(File.join(@theme_dir,"config.yml"))
26
- end
29
+ def app()
30
+ self
31
+ end
27
32
 
28
- def draw(screen)
29
- screen.start_drawing
30
- super screen
31
- screen.finish_drawing
32
- end
33
+ def load_theme(theme_name)
34
+ @theme_dir = File.join(@data_dir,theme_name)
35
+ @theme = YAML::load_file(File.join(@theme_dir,"config.yml"))
36
+ end
33
37
 
34
- def add_tabbed_widget(w)
35
- w.focus_priority = @tabbed_widgets.size unless w.focus_priority
36
- @focussed_widget = 1
37
- w.focus if @tabbed_widgets.empty?
38
- @tabbed_widgets << w
39
- @tabbed_widgets.sort_by {|w| w.focus_priority}
40
- end
38
+ def draw(screen)
39
+ @renderer.start_drawing
40
+ super @renderer
41
+ @mouse.draw @renderer if @mouse_opt
42
+ @renderer.finish_drawing
43
+ end
41
44
 
42
- def focus_back()
43
- @tabbed_widgets[@focussed_widget].unfocus
44
- @focussed_widget += 1
45
- @focussed_widget %= @tabbed_widgets.size
46
- @tabbed_widgets[@focussed_widget].focus
47
- end
45
+ def add_tabbed_widget(w)
46
+ w.focus_priority = @tabbed_widgets.size unless w.focus_priority
47
+ @focussed_widget = 1
48
+ w.focus if @tabbed_widgets.empty?
49
+ @tabbed_widgets << w
50
+ @tabbed_widgets.sort_by {|w| w.focus_priority}
51
+ end
48
52
 
49
- def focus_forward()
50
- @tabbed_widgets[@focussed_widget].unfocus
51
- @focussed_widget -= 1
52
- @focussed_widget %= @tabbed_widgets.size
53
- @tabbed_widgets[@focussed_widget].focus
54
- end
53
+ def focus_back()
54
+ @tabbed_widgets[@focussed_widget].unfocus
55
+ @focussed_widget += 1
56
+ @focussed_widget %= @tabbed_widgets.size
57
+ @tabbed_widgets[@focussed_widget].focus
58
+ end
59
+
60
+ def focus_forward()
61
+ @tabbed_widgets[@focussed_widget].unfocus
62
+ @focussed_widget -= 1
63
+ @focussed_widget %= @tabbed_widgets.size
64
+ @tabbed_widgets[@focussed_widget].focus
65
+ end
55
66
 
56
- # this is where all the rubygame to rubygoo event mapping will
57
- # happen
58
- def on_event(event)
59
- case event.event_type
60
- when :key_released
61
- modal_keyboard_call :key_released, event
62
- when :key_pressed
63
- case event.data[:key]
64
- when K_TAB
65
- if event.data[:mods].include? K_LSHIFT or event.data[:mods].include? K_RSHIFT
66
- focus_back
67
+ def on_event(event)
68
+ case event.event_type
69
+ when :key_released
70
+ modal_keyboard_call :key_released, event
71
+ when :key_pressed
72
+ case event.data[:key]
73
+ when K_TAB
74
+ if event.data[:mods].include? K_LSHIFT or event.data[:mods].include? K_RSHIFT
75
+ focus_back
76
+ else
77
+ focus_forward
78
+ end
67
79
  else
68
- focus_forward
80
+ modal_keyboard_call :key_pressed, event
69
81
  end
70
82
  else
71
- modal_keyboard_call :key_pressed, event
83
+ # ALL mouse events go here
84
+ modal_mouse_call event.event_type, event
85
+
86
+ if event.event_type == :mouse_motion
87
+ @mouse.mouse_motion event if @mouse_opt
88
+ end
72
89
  end
73
- else
74
- # ALL mouse events go here
75
- modal_mouse_call event.event_type, event
76
90
  end
77
- end
78
91
 
92
+ end
79
93
  end
80
-
@@ -1,53 +1,55 @@
1
- class Button < Widget
2
- can_fire :pressed
3
- def initialize(text, opts={})
4
- super opts
5
- @text = text
6
- end
7
-
8
- def added()
9
- font = theme_property :font
10
- @font_size = theme_property :font_size
11
- @color = theme_property :color
12
- @bg_color = theme_property :bg_color
13
- @border_color = theme_property :border_color
14
- @focus_color = theme_property :focus_color
15
- @font_file = File.join(@app.theme_dir,font)
16
-
17
- @rendered_text ||= @app.renderer.render_text @text, @font_file, @font_size, @color
18
- @rect = Rect.new [@x-@x_pad,@y-@y_pad,@rendered_text.width+2*@x_pad,@rendered_text.height+2*@y_pad]
19
- end
1
+ module Rubygoo
2
+ class Button < Widget
3
+ can_fire :pressed
4
+ def initialize(text, opts={})
5
+ super opts
6
+ @text = text
7
+ end
20
8
 
21
- # called when there is a mouse click
22
- def mouse_up(event)
23
- fire :pressed, event
24
- end
9
+ def added()
10
+ font = theme_property :font
11
+ @font_size = theme_property :font_size
12
+ @color = theme_property :color
13
+ @bg_color = theme_property :bg_color
14
+ @border_color = theme_property :border_color
15
+ @focus_color = theme_property :focus_color
16
+ @font_file = File.join(@app.theme_dir,font)
17
+
18
+ @rendered_text ||= @app.renderer.render_text @text, @font_file, @font_size, @color
19
+ @rect = Rect.new [@x-@x_pad,@y-@y_pad,@rendered_text.width+2*@x_pad,@rendered_text.height+2*@y_pad]
20
+ end
25
21
 
26
- # called when a key press is sent to us
27
- def key_pressed(event)
28
- case event.data[:key]
29
- when K_SPACE
22
+ # called when there is a mouse click
23
+ def mouse_up(event)
30
24
  fire :pressed, event
31
25
  end
32
- end
33
26
 
34
- def draw(adapter)
35
- if @focussed
36
- adapter.fill @focus_color, @rect
37
- elsif @bg_color
38
- adapter.fill @bg_color, @rect
39
- end
40
- if @border_color
41
- x1 = @rect[0]
42
- y1 = @rect[1]
43
- x2 = @rect[2] + x1
44
- y2 = @rect[3] + y1
45
- adapter.draw_box x1, y1, x2, y2, @border_color
27
+ # called when a key press is sent to us
28
+ def key_pressed(event)
29
+ case event.data[:key]
30
+ when K_SPACE
31
+ fire :pressed, event
32
+ end
46
33
  end
47
34
 
35
+ def draw(adapter)
36
+ if @focussed
37
+ adapter.fill @focus_color, @rect
38
+ elsif @bg_color
39
+ adapter.fill @bg_color, @rect
40
+ end
41
+ if @border_color
42
+ x1 = @rect[0]
43
+ y1 = @rect[1]
44
+ x2 = @rect[2] + x1
45
+ y2 = @rect[3] + y1
46
+ adapter.draw_box x1, y1, x2, y2, @border_color
47
+ end
48
48
 
49
- adapter.draw_image @rendered_text, @x, @y
50
49
 
50
+ adapter.draw_image @rendered_text, @x, @y, @color
51
+
52
+ end
51
53
  end
52
54
  end
53
55