cyberarm_engine 0.12.1 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -8
  3. data/.travis.yml +5 -5
  4. data/Gemfile +6 -6
  5. data/LICENSE.txt +21 -21
  6. data/README.md +43 -43
  7. data/Rakefile +10 -10
  8. data/bin/console +14 -14
  9. data/bin/setup +8 -8
  10. data/cyberarm_engine.gemspec +36 -36
  11. data/lib/cyberarm_engine.rb +47 -46
  12. data/lib/cyberarm_engine/animator.rb +54 -0
  13. data/lib/cyberarm_engine/background.rb +175 -175
  14. data/lib/cyberarm_engine/bounding_box.rb +149 -149
  15. data/lib/cyberarm_engine/common.rb +96 -96
  16. data/lib/cyberarm_engine/engine.rb +101 -101
  17. data/lib/cyberarm_engine/game_object.rb +256 -256
  18. data/lib/cyberarm_engine/game_state.rb +88 -88
  19. data/lib/cyberarm_engine/gosu_ext/circle.rb +8 -8
  20. data/lib/cyberarm_engine/ray.rb +55 -55
  21. data/lib/cyberarm_engine/shader.rb +262 -205
  22. data/lib/cyberarm_engine/text.rb +146 -146
  23. data/lib/cyberarm_engine/timer.rb +22 -22
  24. data/lib/cyberarm_engine/transform.rb +272 -83
  25. data/lib/cyberarm_engine/ui/border_canvas.rb +100 -100
  26. data/lib/cyberarm_engine/ui/dsl.rb +98 -101
  27. data/lib/cyberarm_engine/ui/element.rb +275 -259
  28. data/lib/cyberarm_engine/ui/elements/button.rb +66 -66
  29. data/lib/cyberarm_engine/ui/elements/check_box.rb +58 -58
  30. data/lib/cyberarm_engine/ui/elements/container.rb +176 -162
  31. data/lib/cyberarm_engine/ui/elements/edit_line.rb +171 -102
  32. data/lib/cyberarm_engine/ui/elements/flow.rb +16 -16
  33. data/lib/cyberarm_engine/ui/elements/image.rb +51 -51
  34. data/lib/cyberarm_engine/ui/elements/label.rb +49 -49
  35. data/lib/cyberarm_engine/ui/elements/progress.rb +49 -49
  36. data/lib/cyberarm_engine/ui/elements/stack.rb +12 -12
  37. data/lib/cyberarm_engine/ui/elements/toggle_button.rb +55 -55
  38. data/lib/cyberarm_engine/ui/event.rb +46 -45
  39. data/lib/cyberarm_engine/ui/gui_state.rb +134 -134
  40. data/lib/cyberarm_engine/ui/style.rb +36 -36
  41. data/lib/cyberarm_engine/ui/theme.rb +120 -119
  42. data/lib/cyberarm_engine/vector.rb +202 -198
  43. data/lib/cyberarm_engine/version.rb +4 -4
  44. metadata +6 -5
@@ -1,103 +1,172 @@
1
- module CyberarmEngine
2
- class Element
3
- class EditLine < Button
4
- def initialize(text, options = {}, block = nil)
5
- super(text, options, block)
6
-
7
- @type = default(:type)
8
-
9
- @caret_width = default(:caret_width)
10
- @caret_height= @text.height
11
- @caret_color = default(:caret_color)
12
- @caret_interval = default(:caret_interval)
13
- @caret_last_interval = Gosu.milliseconds
14
- @show_caret = true
15
-
16
- @text_input = Gosu::TextInput.new
17
- @text_input.text = text
18
-
19
- return self
20
- end
21
-
22
- def render
23
- Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
24
- draw_text
25
- Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret
26
- end
27
- end
28
-
29
- def update
30
- if @type == :password
31
- @text.text = default(:password_character) * @text_input.text.length
32
- else
33
- @text.text = @text_input.text
34
- end
35
-
36
- if Gosu.milliseconds >= @caret_last_interval + @caret_interval
37
- @caret_last_interval = Gosu.milliseconds
38
-
39
- @show_caret = !@show_caret
40
- end
41
- end
42
-
43
- def left_mouse_button(sender, x, y)
44
- super
45
- window.text_input = @text_input
46
-
47
- @caret_last_interval = Gosu.milliseconds
48
- @show_caret = true
49
-
50
- return :handled
51
- end
52
-
53
- def enter(sender)
54
- if @focus
55
- @style.background_canvas.background = default(:active, :background)
56
- @text.color = default(:active, :color)
57
- else
58
- @style.background_canvas.background = default(:hover, :background)
59
- @text.color = default(:hover, :color)
60
- end
61
-
62
- return :handled
63
- end
64
-
65
- def leave(sender)
66
- unless @focus
67
- super
68
- end
69
-
70
- return :handled
71
- end
72
-
73
- def blur(sender)
74
- @focus = false
75
- @style.background_canvas.background = default(:background)
76
- @text.color = default(:color)
77
- window.text_input = nil
78
-
79
- return :handled
80
- end
81
-
82
- # TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
83
- def caret_position
84
- if @type == :password
85
- @text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos-1].length)
86
- else
87
- @text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1])
88
- end
89
- end
90
-
91
- def recalculate
92
- super
93
-
94
- @width = dimensional_size(@style.width, :width) || default(:width)
95
- update_background
96
- end
97
-
98
- def value
99
- @text_input.text
100
- end
101
- end
102
- end
1
+ module CyberarmEngine
2
+ class Element
3
+ class EditLine < Button
4
+ def initialize(text, options = {}, block = nil)
5
+ super(text, options, block)
6
+
7
+ @type = default(:type)
8
+
9
+ @caret_width = default(:caret_width)
10
+ @caret_height= @text.height
11
+ @caret_color = default(:caret_color)
12
+ @caret_interval = default(:caret_interval)
13
+ @caret_last_interval = Gosu.milliseconds
14
+ @show_caret = true
15
+
16
+ @text_input = Gosu::TextInput.new
17
+ @text_input.text = text
18
+
19
+ @offset_x = 0
20
+
21
+ return self
22
+ end
23
+
24
+ def render
25
+ Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
26
+ Gosu.translate(-@offset_x, 0) do
27
+ draw_selection
28
+ draw_caret if @focus && @show_caret
29
+ draw_text
30
+ end
31
+ end
32
+ end
33
+
34
+ def draw_caret
35
+ Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z)
36
+ end
37
+
38
+ def draw_selection
39
+ selection_width = caret_position - selection_start_position
40
+
41
+ Gosu.draw_rect(selection_start_position, @text.y, selection_width, @text.height, default(:selection_color), @z)
42
+ end
43
+
44
+ def update
45
+ if @type == :password
46
+ @text.text = default(:password_character) * @text_input.text.length
47
+ else
48
+ @text.text = @text_input.text
49
+ end
50
+
51
+ if Gosu.milliseconds >= @caret_last_interval + @caret_interval
52
+ @caret_last_interval = Gosu.milliseconds
53
+
54
+ @show_caret = !@show_caret
55
+ end
56
+
57
+ keep_caret_visible
58
+ end
59
+
60
+ def move_caret_to_mouse(mouse_x)
61
+ 1.upto(@text.text.length) do |i|
62
+ if mouse_x < @text.x + @text.textobject.text_width(@text.text[0...i])
63
+ @text_input.caret_pos = @text_input.selection_start = i - 1;
64
+ return
65
+ end
66
+ end
67
+
68
+ @text_input.caret_pos = @text_input.selection_start = @text_input.text.length
69
+ end
70
+
71
+ def keep_caret_visible
72
+ caret_pos = (caret_position - @text.x) + @caret_width
73
+
74
+ @last_text ||= "/\\"
75
+ @last_pos ||= -1
76
+
77
+ puts "caret pos: #{caret_pos}, width: #{@width}, offset: #{@offset_x}" if (@last_text != @text.text) || (@last_pos != caret_pos)
78
+
79
+ @last_text = @text.text
80
+ @last_pos = caret_pos
81
+
82
+
83
+ if caret_pos.between?(@offset_x, @width + @offset_x)
84
+ # Do nothing
85
+
86
+ elsif caret_pos < @offset_x
87
+ if caret_pos > @width
88
+ @offset_x = caret_pos + @width
89
+ else
90
+ @offset_x = 0
91
+ end
92
+
93
+ elsif caret_pos > @width
94
+ @offset_x = caret_pos - @width
95
+ puts "triggered"
96
+
97
+ else
98
+ # Reset to Zero
99
+ @offset_x = 0
100
+ end
101
+ end
102
+
103
+ def left_mouse_button(sender, x, y)
104
+ super
105
+ window.text_input = @text_input
106
+
107
+ @caret_last_interval = Gosu.milliseconds
108
+ @show_caret = true
109
+
110
+ move_caret_to_mouse(x)
111
+
112
+ return :handled
113
+ end
114
+
115
+ def enter(sender)
116
+ if @focus
117
+ @style.background_canvas.background = default(:active, :background)
118
+ @text.color = default(:active, :color)
119
+ else
120
+ @style.background_canvas.background = default(:hover, :background)
121
+ @text.color = default(:hover, :color)
122
+ end
123
+
124
+ return :handled
125
+ end
126
+
127
+ def leave(sender)
128
+ unless @focus
129
+ super
130
+ end
131
+
132
+ return :handled
133
+ end
134
+
135
+ def blur(sender)
136
+ @focus = false
137
+ @style.background_canvas.background = default(:background)
138
+ @text.color = default(:color)
139
+ window.text_input = nil
140
+
141
+ return :handled
142
+ end
143
+
144
+ def caret_position
145
+ text_input_position_for(:caret_pos)
146
+ end
147
+
148
+ def selection_start_position
149
+ text_input_position_for(:selection_start)
150
+ end
151
+
152
+ def text_input_position_for(method)
153
+ if @type == :password
154
+ @text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.send(method)].length)
155
+ else
156
+ @text.x + @text.textobject.text_width(@text_input.text[0..@text_input.send(method)])
157
+ end
158
+ end
159
+
160
+ def recalculate
161
+ super
162
+
163
+ @width = dimensional_size(@style.width, :width) || default(:width)
164
+ update_background
165
+ end
166
+
167
+ def value
168
+ @text_input.text
169
+ end
170
+ end
171
+ end
103
172
  end
@@ -1,17 +1,17 @@
1
- module CyberarmEngine
2
- class Element
3
- class Flow < Container
4
- include Common
5
-
6
- def layout
7
- @children.each do |child|
8
- if fits_on_line?(child)
9
- position_on_current_line(child)
10
- else
11
- position_on_next_line(child)
12
- end
13
- end
14
- end
15
- end
16
- end
1
+ module CyberarmEngine
2
+ class Element
3
+ class Flow < Container
4
+ include Common
5
+
6
+ def layout
7
+ @children.each do |child|
8
+ if fits_on_line?(child)
9
+ position_on_current_line(child)
10
+ else
11
+ position_on_next_line(child)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
17
  end
@@ -1,52 +1,52 @@
1
- module CyberarmEngine
2
- class Element
3
- class Image < Element
4
- def initialize(path, options = {}, block = nil)
5
- super(options, block)
6
- @path = path
7
-
8
- @image = Gosu::Image.new(path, retro: @options[:image_retro])
9
- @scale_x, @scale_y = 1, 1
10
- end
11
-
12
- def render
13
- @image.draw(
14
- @style.border_thickness_left + @style.padding_left + @x,
15
- @style.border_thickness_top + @style.padding_top + @y,
16
- @z + 2,
17
- @scale_x, @scale_y) # TODO: Add color support?
18
- end
19
-
20
- def clicked_left_mouse_button(sender, x, y)
21
- @block.call(self) if @block
22
-
23
- return :handled
24
- end
25
-
26
- def recalculate
27
- _width = dimensional_size(@style.width, :width)
28
- _height= dimensional_size(@style.height,:height)
29
-
30
- if _width && _height
31
- @scale_x = _width.to_f / @image.width
32
- @scale_y = _height.to_f / @image.height
33
- elsif _width
34
- @scale_x = _width.to_f / @image.width
35
- @scale_y = @scale_x
36
- elsif _height
37
- @scale_y = _height.to_f / @image.height
38
- @scale_x = @scale_y
39
- else
40
- @scale_x, @scale_y = 1, 1
41
- end
42
-
43
- @width = _width ? _width : @image.width.round * @scale_x
44
- @height= _height ? _height : @image.height.round * @scale_y
45
- end
46
-
47
- def value
48
- @path
49
- end
50
- end
51
- end
1
+ module CyberarmEngine
2
+ class Element
3
+ class Image < Element
4
+ def initialize(path, options = {}, block = nil)
5
+ super(options, block)
6
+ @path = path
7
+
8
+ @image = Gosu::Image.new(path, retro: @options[:image_retro])
9
+ @scale_x, @scale_y = 1, 1
10
+ end
11
+
12
+ def render
13
+ @image.draw(
14
+ @style.border_thickness_left + @style.padding_left + @x,
15
+ @style.border_thickness_top + @style.padding_top + @y,
16
+ @z + 2,
17
+ @scale_x, @scale_y) # TODO: Add color support?
18
+ end
19
+
20
+ def clicked_left_mouse_button(sender, x, y)
21
+ @block.call(self) if @block
22
+
23
+ return :handled
24
+ end
25
+
26
+ def recalculate
27
+ _width = dimensional_size(@style.width, :width)
28
+ _height= dimensional_size(@style.height,:height)
29
+
30
+ if _width && _height
31
+ @scale_x = _width.to_f / @image.width
32
+ @scale_y = _height.to_f / @image.height
33
+ elsif _width
34
+ @scale_x = _width.to_f / @image.width
35
+ @scale_y = @scale_x
36
+ elsif _height
37
+ @scale_y = _height.to_f / @image.height
38
+ @scale_x = @scale_y
39
+ else
40
+ @scale_x, @scale_y = 1, 1
41
+ end
42
+
43
+ @width = _width ? _width : @image.width.round * @scale_x
44
+ @height= _height ? _height : @image.height.round * @scale_y
45
+ end
46
+
47
+ def value
48
+ @path
49
+ end
50
+ end
51
+ end
52
52
  end
@@ -1,50 +1,50 @@
1
- module CyberarmEngine
2
- class Element
3
- class Label < Element
4
- def initialize(text, options = {}, block = nil)
5
- super(options, block)
6
-
7
- @text = Text.new(text, font: @options[:font], z: @z, color: @options[:color], size: @options[:text_size], shadow: @options[:text_shadow])
8
- end
9
-
10
- def render
11
- @text.draw
12
- end
13
-
14
- def clicked_left_mouse_button(sender, x, y)
15
- @block.call(self) if @block
16
-
17
- return :handled
18
- end
19
-
20
- def recalculate
21
- @width, @height = 0, 0
22
-
23
- _width = dimensional_size(@style.width, :width)
24
- _height= dimensional_size(@style.height,:height)
25
-
26
- @width = _width ? _width : @text.width.round
27
- @height= _height ? _height : @text.height.round
28
-
29
- @text.x = @style.border_thickness_left + @style.padding_left + @x
30
- @text.y = @style.border_thickness_top + @style.padding_top + @y
31
- @text.z = @z + 3
32
-
33
- update_background
34
- end
35
-
36
- def value
37
- @text.text
38
- end
39
-
40
- def value=(value)
41
- @text.text = value
42
-
43
- old_width, old_height = width, height
44
- recalculate
45
-
46
- root.gui_state.request_recalculate if old_width != width || old_height != height
47
- end
48
- end
49
- end
1
+ module CyberarmEngine
2
+ class Element
3
+ class Label < Element
4
+ def initialize(text, options = {}, block = nil)
5
+ super(options, block)
6
+
7
+ @text = Text.new(text, font: @options[:font], z: @z, color: @options[:color], size: @options[:text_size], shadow: @options[:text_shadow])
8
+ end
9
+
10
+ def render
11
+ @text.draw
12
+ end
13
+
14
+ def clicked_left_mouse_button(sender, x, y)
15
+ @block.call(self) if @block
16
+
17
+ return :handled
18
+ end
19
+
20
+ def recalculate
21
+ @width, @height = 0, 0
22
+
23
+ _width = dimensional_size(@style.width, :width)
24
+ _height= dimensional_size(@style.height,:height)
25
+
26
+ @width = _width ? _width : @text.width.round
27
+ @height= _height ? _height : @text.height.round
28
+
29
+ @text.x = @style.border_thickness_left + @style.padding_left + @x
30
+ @text.y = @style.border_thickness_top + @style.padding_top + @y
31
+ @text.z = @z + 3
32
+
33
+ update_background
34
+ end
35
+
36
+ def value
37
+ @text.text
38
+ end
39
+
40
+ def value=(value)
41
+ @text.text = value
42
+
43
+ old_width, old_height = width, height
44
+ recalculate
45
+
46
+ root.gui_state.request_recalculate if old_width != width || old_height != height
47
+ end
48
+ end
49
+ end
50
50
  end