cyberarm_engine 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3fd7c0b09fa9487e4a543e508ac8e0ec56561ac34cb620230a49f7c4244b1b3
4
- data.tar.gz: 6903fa77e7e3bda53f8cadc9d1915ea905330dd2dcbe8ac8e2a83043132f7256
3
+ metadata.gz: 13dd35ba206e4239217450cd996f67797a204b97ff4b4bedf5ab77b8b8fa30cb
4
+ data.tar.gz: d2c4bd2d4f94d0d008fd828d4a8ed7bfea0670bbebaf74dd19f7bda01fd1b0c5
5
5
  SHA512:
6
- metadata.gz: 39089172ed27cb6e49958d740190f97a28311fcadc4c89e7ba09f0efa71966abcf1ed73cdaa3cc3bcecece699bce411cc670da504ed2c1ee6b18bb4c38f3c740
7
- data.tar.gz: 847163bc84e4dad3161e4e8f8c0eae741c421423323692e11f88e089e62763166070608d435b3e2c9d8b49467d45f5fdec815974e750e722e69489c795eea702
6
+ metadata.gz: 76274df828fc73fae6d3ec1c220815e90cc193c49dfc879495205c63e52a0a617e16740987d06deb2c9c39624eae53c9d47d51799b178fdb3bdc2c38995ee3d0
7
+ data.tar.gz: 1e7a9b9532d93adc3256cd1ed1d8a0c92c41b682850afdee2f91e56ebc0459b16ede6c05a6fa29e7b2f7ad8b51d95b51750d383c36ab059003ff102d0abfc79b
@@ -19,16 +19,17 @@ require_relative "cyberarm_engine/ui/event"
19
19
  require_relative "cyberarm_engine/ui/style"
20
20
  require_relative "cyberarm_engine/ui/border_canvas"
21
21
  require_relative "cyberarm_engine/ui/element"
22
- require_relative "cyberarm_engine/ui/label"
23
- require_relative "cyberarm_engine/ui/button"
24
- require_relative "cyberarm_engine/ui/toggle_button"
25
- require_relative "cyberarm_engine/ui/edit_line"
26
- require_relative "cyberarm_engine/ui/image"
27
- require_relative "cyberarm_engine/ui/container"
28
-
29
- require_relative "cyberarm_engine/ui/flow"
30
- require_relative "cyberarm_engine/ui/stack"
31
- require_relative "cyberarm_engine/ui/check_box"
22
+ require_relative "cyberarm_engine/ui/elements/label"
23
+ require_relative "cyberarm_engine/ui/elements/button"
24
+ require_relative "cyberarm_engine/ui/elements/toggle_button"
25
+ require_relative "cyberarm_engine/ui/elements/edit_line"
26
+ require_relative "cyberarm_engine/ui/elements/image"
27
+ require_relative "cyberarm_engine/ui/elements/container"
28
+ require_relative "cyberarm_engine/ui/elements/flow"
29
+ require_relative "cyberarm_engine/ui/elements/stack"
30
+ require_relative "cyberarm_engine/ui/elements/check_box"
31
+ require_relative "cyberarm_engine/ui/elements/progress"
32
+
32
33
  require_relative "cyberarm_engine/ui/dsl"
33
34
 
34
35
  require_relative "cyberarm_engine/game_state"
@@ -158,9 +158,20 @@ module CyberarmEngine
158
158
  @top_right = background[:top_right]
159
159
  @bottom_left = background[:bottom_left]
160
160
  @bottom_right = background[:bottom_right]
161
+ elsif background.is_a?(Range)
162
+ set([background.begin, background.begin, background.end, background.end])
161
163
  else
162
164
  raise ArgumentError, "background '#{background}' of type '#{background.class}' was not able to be processed"
163
165
  end
164
166
  end
165
167
  end
166
- end
168
+ end
169
+
170
+ # Add <=> method to support Range based gradients
171
+ module Gosu
172
+ class Color
173
+ def <=>(other)
174
+ self
175
+ end
176
+ end
177
+ end
@@ -62,7 +62,7 @@ module CyberarmEngine
62
62
  @states << klass
63
63
  else
64
64
  @states << klass.new(options) if child_of?(klass, GameState)
65
- @states << klass.new if child_of?(klass, Container)
65
+ @states << klass.new if child_of?(klass, Element::Container)
66
66
  end
67
67
  end
68
68
 
@@ -3,7 +3,7 @@ module CyberarmEngine
3
3
  def flow(options = {}, &block)
4
4
  options[:parent] = @containers.last
5
5
  options[:theme] = current_theme
6
- _container = Flow.new(options, block)
6
+ _container = Element::Flow.new(options, block)
7
7
  @containers << _container
8
8
  _container.build
9
9
  _container.parent.add(_container)
@@ -15,7 +15,7 @@ module CyberarmEngine
15
15
  def stack(options = {}, &block)
16
16
  options[:parent] = @containers.last
17
17
  options[:theme] = current_theme
18
- _container = Stack.new(options, block)
18
+ _container = Element::Stack.new(options, block)
19
19
  @containers << _container
20
20
  _container.build
21
21
  _container.parent.add(_container)
@@ -27,7 +27,7 @@ module CyberarmEngine
27
27
  def label(text, options = {}, &block)
28
28
  options[:parent] = @containers.last
29
29
  options[:theme] = current_theme
30
- _element = Label.new(text, options, block)
30
+ _element = Element::Label.new(text, options, block)
31
31
  @containers.last.add(_element)
32
32
 
33
33
  return _element
@@ -36,7 +36,7 @@ module CyberarmEngine
36
36
  def button(text, options = {}, &block)
37
37
  options[:parent] = @containers.last
38
38
  options[:theme] = current_theme
39
- _element = Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
39
+ _element = Element::Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
40
40
  @containers.last.add(_element)
41
41
 
42
42
  return _element
@@ -45,7 +45,7 @@ module CyberarmEngine
45
45
  def edit_line(text, options = {}, &block)
46
46
  options[:parent] = @containers.last
47
47
  options[:theme] = current_theme
48
- _element = EditLine.new(text, options, block)
48
+ _element = Element::EditLine.new(text, options, block)
49
49
  @containers.last.add(_element)
50
50
 
51
51
  return _element
@@ -54,7 +54,7 @@ module CyberarmEngine
54
54
  def toggle_button(options = {}, &block)
55
55
  options[:parent] = @containers.last
56
56
  options[:theme] = current_theme
57
- _element = ToggleButton.new(options, block)
57
+ _element = Element::ToggleButton.new(options, block)
58
58
  @containers.last.add(_element)
59
59
 
60
60
  return _element
@@ -63,7 +63,7 @@ module CyberarmEngine
63
63
  def check_box(text, options = {}, &block)
64
64
  options[:parent] = @containers.last
65
65
  options[:theme] = current_theme
66
- _element = CheckBox.new(text, options, block)
66
+ _element = Element::CheckBox.new(text, options, block)
67
67
  @containers.last.add(_element)
68
68
 
69
69
  return _element
@@ -72,7 +72,16 @@ module CyberarmEngine
72
72
  def image(path, options = {}, &block)
73
73
  options[:parent] = @containers.last
74
74
  options[:theme] = current_theme
75
- _element = Image.new(path, options, block)
75
+ _element = Element::Image.new(path, options, block)
76
+ @containers.last.add(_element)
77
+
78
+ return _element
79
+ end
80
+
81
+ def progress(options = {}, &block)
82
+ options[:parent] = @containers.last
83
+ options[:theme] = current_theme
84
+ _element = Element::Progress.new(options, block)
76
85
  @containers.last.add(_element)
77
86
 
78
87
  return _element
@@ -23,11 +23,14 @@ module CyberarmEngine
23
23
  @y = @style.y
24
24
  @z = @style.z
25
25
 
26
+ @width = 0
27
+ @height = 0
28
+
26
29
  @fixed_x = @x if @x != 0
27
30
  @fixed_y = @y if @y != 0
28
31
 
29
- @style.width = default(:width) || $window.width
30
- @style.height = default(:height) || $window.height
32
+ @style.width = default(:width) || nil
33
+ @style.height = default(:height) || nil
31
34
 
32
35
  stylize
33
36
 
@@ -119,17 +122,17 @@ module CyberarmEngine
119
122
 
120
123
  def toggle
121
124
  @visible = !@visible
122
- root.recalculate
125
+ root.gui_state.request_recalculate
123
126
  end
124
127
 
125
128
  def show
126
129
  @visible = true
127
- root.recalculate
130
+ root.gui_state.request_recalculate
128
131
  end
129
132
 
130
133
  def hide
131
134
  @visible = false
132
- root.recalculate
135
+ root.gui_state.request_recalculate
133
136
  end
134
137
 
135
138
  def draw
@@ -159,7 +162,7 @@ module CyberarmEngine
159
162
 
160
163
  def width
161
164
  if visible?
162
- (@style.border_thickness_left + @style.padding_left) + @style.width + (@style.padding_right + @style.border_thickness_right)
165
+ (@style.border_thickness_left + @style.padding_left) + @width + (@style.padding_right + @style.border_thickness_right)
163
166
  else
164
167
  0
165
168
  end
@@ -171,7 +174,7 @@ module CyberarmEngine
171
174
 
172
175
  def height
173
176
  if visible?
174
- (@style.border_thickness_top + @style.padding_top) + @style.height + (@style.padding_bottom + @style.border_thickness_bottom)
177
+ (@style.border_thickness_top + @style.padding_top) + @height + (@style.padding_bottom + @style.border_thickness_bottom)
175
178
  else
176
179
  0
177
180
  end
@@ -181,6 +184,19 @@ module CyberarmEngine
181
184
  @style.margin_top + height + @style.margin_bottom
182
185
  end
183
186
 
187
+ private def dimensional_size(size, dimension)
188
+ raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
189
+ if size && size.is_a?(Numeric)
190
+ if size.between?(0.0, 1.0)
191
+ @parent.send(:"#{dimension}") * size
192
+ else
193
+ size
194
+ end
195
+ else
196
+ nil
197
+ end
198
+ end
199
+
184
200
  def background=(_background)
185
201
  @style.background_canvas.background=(_background)
186
202
  update_background
@@ -214,6 +230,10 @@ module CyberarmEngine
214
230
  @root
215
231
  end
216
232
 
233
+ def is_root?
234
+ @gui_state != nil
235
+ end
236
+
217
237
  def recalculate
218
238
  raise "#{self.class}#recalculate was not overridden!"
219
239
  end
@@ -0,0 +1,55 @@
1
+ module CyberarmEngine
2
+ class Element
3
+ class Button < Label
4
+ def initialize(text, options = {}, block = nil)
5
+ super(text, options, block)
6
+
7
+ @style.background_canvas.background = default(:background)
8
+ end
9
+
10
+ def render
11
+ draw_text
12
+ end
13
+
14
+ def draw_text
15
+ @text.draw
16
+ end
17
+
18
+ def enter(sender)
19
+ @focus = false unless window.button_down?(Gosu::MsLeft)
20
+
21
+ if @focus
22
+ @style.background_canvas.background = default(:active, :background)
23
+ @text.color = default(:active, :color)
24
+ else
25
+ @style.background_canvas.background = default(:hover, :background)
26
+ @text.color = default(:hover, :color)
27
+ end
28
+ end
29
+
30
+ def left_mouse_button(sender, x, y)
31
+ @focus = true
32
+ @style.background_canvas.background = default(:active, :background)
33
+ window.current_state.focus = self
34
+ @text.color = default(:active, :color)
35
+ end
36
+
37
+ def released_left_mouse_button(sender,x, y)
38
+ enter(sender)
39
+ end
40
+
41
+ def clicked_left_mouse_button(sender, x, y)
42
+ @block.call(self) if @block
43
+ end
44
+
45
+ def leave(sender)
46
+ @style.background_canvas.background = default(:background)
47
+ @text.color = default(:color)
48
+ end
49
+
50
+ def blur(sender)
51
+ @focus = false
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,59 @@
1
+ module CyberarmEngine
2
+ class Element
3
+ class CheckBox < Flow
4
+ def initialize(text, options, block = nil)
5
+ super({}, block = nil)
6
+ options[:toggled] = options[:checked]
7
+
8
+ @toggle_button = ToggleButton.new(options)
9
+ @label = Label.new(text, options)
10
+
11
+ define_label_singletons
12
+
13
+ add(@toggle_button)
14
+ add(@label)
15
+ end
16
+
17
+ def text=(text)
18
+ @label.text = text
19
+ recalculate
20
+ end
21
+
22
+ def value
23
+ @toggle_button.value
24
+ end
25
+
26
+ def value=(bool)
27
+ @toggle_button.vlaue = bool
28
+ end
29
+
30
+ def define_label_singletons
31
+ @label.define_singleton_method(:_toggle_button) do |button|
32
+ @_toggle_button = button
33
+ end
34
+
35
+ @label._toggle_button(@toggle_button)
36
+
37
+ @label.define_singleton_method(:holding_left_mouse_button) do |sender, x, y|
38
+ @_toggle_button.left_mouse_button(sender, x, y)
39
+ end
40
+
41
+ @label.define_singleton_method(:released_left_mouse_button) do |sender, x, y|
42
+ @_toggle_button.released_left_mouse_button(sender, x, y)
43
+ end
44
+
45
+ @label.define_singleton_method(:clicked_left_mouse_button) do |sender, x, y|
46
+ @_toggle_button.clicked_left_mouse_button(sender, x, y)
47
+ end
48
+
49
+ @label.define_singleton_method(:enter) do |sender|
50
+ @_toggle_button.enter(sender)
51
+ end
52
+
53
+ @label.define_singleton_method(:leave) do |sender|
54
+ @_toggle_button.leave(sender)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,159 @@
1
+ module CyberarmEngine
2
+ class Element
3
+ class Container < Element
4
+ include Common
5
+
6
+ attr_accessor :stroke_color, :fill_color
7
+ attr_reader :children, :gui_state
8
+ attr_reader :scroll_x, :scroll_y
9
+
10
+ def initialize(options = {}, block = nil)
11
+ @gui_state = options.delete(:gui_state)
12
+ super
13
+
14
+ @scroll_x, @scroll_y = 0, 0
15
+ @scroll_speed = 10
16
+
17
+ @text_color = options[:color]
18
+
19
+ @children = []
20
+ end
21
+
22
+ def build
23
+ @block.call(self) if @block
24
+
25
+ recalculate
26
+ end
27
+
28
+ def add(element)
29
+ @children << element
30
+
31
+ recalculate
32
+ end
33
+
34
+ def render
35
+ Gosu.clip_to(@x, @y, width, height) do
36
+ @children.each(&:draw)
37
+ end
38
+ end
39
+
40
+ def update
41
+ @children.each(&:update)
42
+ end
43
+
44
+ def hit_element?(x, y)
45
+ @children.reverse_each do |child|
46
+ case child
47
+ when Container
48
+ if element = child.hit_element?(x, y)
49
+ return element
50
+ end
51
+ else
52
+ return child if child.hit?(x, y)
53
+ end
54
+ end
55
+
56
+ self if hit?(x, y)
57
+ end
58
+
59
+ def recalculate
60
+ @current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
61
+ return unless visible?
62
+ stylize
63
+
64
+ layout
65
+
66
+ if is_root?
67
+ @width = @style.width = window.width
68
+ @height = @style.height = window.height
69
+ else
70
+ _width = dimensional_size(@style.width, :width)
71
+ _height= dimensional_size(@style.height,:height)
72
+ @width = _width ? _width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
73
+ @height = _height ? _height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
74
+ end
75
+
76
+
77
+ # Move child to parent after positioning
78
+ @children.each do |child|
79
+ child.x += @x
80
+ child.y += @y
81
+
82
+ child.stylize
83
+ child.recalculate
84
+ child.reposition # TODO: Implement top,bottom,left,center, and right positioning
85
+ end
86
+
87
+ update_background
88
+ end
89
+
90
+ def layout
91
+ raise "Not overridden"
92
+ end
93
+
94
+ def max_width
95
+ @max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
96
+ end
97
+
98
+ def fits_on_line?(element) # Flow
99
+ @current_position.x + element.outer_width <= max_width &&
100
+ @current_position.x + element.outer_width <= window.width
101
+ end
102
+
103
+ def position_on_current_line(element) # Flow
104
+ element.x = element.style.margin_left + @current_position.x
105
+ element.y = element.style.margin_top + @current_position.y
106
+
107
+ element.recalculate
108
+
109
+ @current_position.x += element.outer_width
110
+ @current_position.x = @style.margin_left if @current_position.x >= max_width
111
+ end
112
+
113
+ def tallest_neighbor(querier, y_position) # Flow
114
+ response = querier
115
+ @children.each do |child|
116
+ response = child if child.outer_height > response.outer_height
117
+ break if child == querier
118
+ end
119
+
120
+ return response
121
+ end
122
+
123
+ def position_on_next_line(child) # Flow
124
+ @current_position.x = @style.margin_left
125
+ @current_position.y += tallest_neighbor(child, @current_position.y).outer_height
126
+
127
+ child.x = child.style.margin_left + @current_position.x
128
+ child.y = child.style.margin_top + @current_position.y
129
+
130
+ child.recalculate
131
+
132
+ @current_position.x += child.outer_width
133
+ end
134
+
135
+ def move_to_next_line(element) # Stack
136
+ element.x = element.style.margin_left + @current_position.x
137
+ element.y = element.style.margin_top + @current_position.y
138
+
139
+ element.recalculate
140
+
141
+ @current_position.y += element.outer_height
142
+ end
143
+
144
+ # def mouse_wheel_up(sender, x, y)
145
+ # @children.each {|c| c.y -= @scroll_speed}
146
+ # @children.each {|c| c.recalculate}
147
+ # end
148
+
149
+ # def mouse_wheel_down(sender, x, y)
150
+ # @children.each {|c| c.y += @scroll_speed}
151
+ # @children.each {|c| c.recalculate}
152
+ # end
153
+
154
+ def value
155
+ @children.map {|c| c.class}.join(", ")
156
+ end
157
+ end
158
+ end
159
+ end