cyberarm_engine 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
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
+ end
47
+
48
+ def enter(sender)
49
+ if @focus
50
+ @style.background_canvas.background = default(:active, :background)
51
+ @text.color = default(:active, :color)
52
+ else
53
+ @style.background_canvas.background = default(:hover, :background)
54
+ @text.color = default(:hover, :color)
55
+ end
56
+ end
57
+
58
+ def leave(sender)
59
+ unless @focus
60
+ super
61
+ end
62
+ end
63
+
64
+ def blur(sender)
65
+ @focus = false
66
+ @style.background_canvas.background = default(:background)
67
+ @text.color = default(:color)
68
+ window.text_input = nil
69
+ end
70
+
71
+ # TODO: Fix caret rendering in wrong position unless caret_pos is at end of text
72
+ def caret_position
73
+ if @type == :password
74
+ @text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos-1].length)
75
+ else
76
+ @text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1])
77
+ end
78
+ end
79
+
80
+ def recalculate
81
+ super
82
+
83
+ @width = dimensional_size(@style.width, :width) || default(:width)
84
+ update_background
85
+ end
86
+
87
+ def value
88
+ @text_input.text
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +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
17
+ end
@@ -0,0 +1,50 @@
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
+ end
23
+
24
+ def recalculate
25
+ _width = dimensional_size(@style.width, :width)
26
+ _height= dimensional_size(@style.height,:height)
27
+
28
+ if _width && _height
29
+ @scale_x = _width.to_f / @image.width
30
+ @scale_y = _height.to_f / @image.height
31
+ elsif _width
32
+ @scale_x = _width.to_f / @image.width
33
+ @scale_y = @scale_x
34
+ elsif _height
35
+ @scale_y = _height.to_f / @image.height
36
+ @scale_x = @scale_y
37
+ else
38
+ @scale_x, @scale_y = 1, 1
39
+ end
40
+
41
+ @width = _width ? _width : @image.width.round * @scale_x
42
+ @height= _height ? _height : @image.height.round * @scale_y
43
+ end
44
+
45
+ def value
46
+ @path
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,45 @@
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
+ end
17
+
18
+ def recalculate
19
+ _width = dimensional_size(@style.width, :width)
20
+ _height= dimensional_size(@style.height,:height)
21
+ @width = _width ? _width : @text.width.round
22
+ @height= _height ? _height : @text.height.round
23
+
24
+ @text.x = @style.border_thickness_left + @style.padding_left + @x
25
+ @text.y = @style.border_thickness_top + @style.padding_top + @y
26
+ @text.z = @z + 3
27
+
28
+ update_background
29
+ end
30
+
31
+ def value
32
+ @text.text
33
+ end
34
+
35
+ def value=(value)
36
+ @text.text = value
37
+
38
+ old_width, old_height = width, height
39
+ recalculate
40
+
41
+ root.gui_state.request_recalculate if old_width != width || old_height != height
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,50 @@
1
+ module CyberarmEngine
2
+ class Element
3
+ class Progress < Element
4
+ def initialize(options = {}, block = nil)
5
+ super(options, block)
6
+
7
+ @fraction_background = Background.new(background: @style.fraction_background)
8
+ self.value = options[:fraction] ? options[:fraction] : 0.0
9
+ end
10
+
11
+ def render
12
+ @fraction_background.draw
13
+ end
14
+
15
+ def recalculate
16
+ _width = dimensional_size(@style.width, :width)
17
+ _height= dimensional_size(@style.height,:height)
18
+ @width = _width
19
+ @height= _height
20
+
21
+ update_background
22
+ end
23
+
24
+ def update_background
25
+ super
26
+
27
+ @fraction_background.x = @style.border_thickness_left + @style.padding_left + @x
28
+ @fraction_background.y = @style.border_thickness_top + @style.padding_top + @y
29
+ @fraction_background.z = @z
30
+ @fraction_background.width = @width * @fraction
31
+ @fraction_background.height = @height
32
+
33
+ @fraction_background.background = @style.fraction_background
34
+ end
35
+
36
+ def value
37
+ @fraction
38
+ end
39
+
40
+ def value=(decimal)
41
+ raise "value must be number" unless decimal.is_a?(Numeric)
42
+
43
+ @fraction = decimal.clamp(0.0, 1.0)
44
+ update_background
45
+
46
+ return @fraction
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module CyberarmEngine
2
+ class Element
3
+ class Stack < Container
4
+ include Common
5
+
6
+ def layout
7
+ @children.each do |child|
8
+ move_to_next_line(child)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,54 @@
1
+ module CyberarmEngine
2
+ class Element
3
+ class ToggleButton < Button
4
+ attr_reader :toggled
5
+
6
+ def initialize(options, block = nil)
7
+ super(options[:checkmark], options, block)
8
+ @toggled = options[:toggled] || false
9
+ if @toggled
10
+ @text.text = @options[:checkmark]
11
+ else
12
+ @text.text = ""
13
+ end
14
+
15
+ return self
16
+ end
17
+
18
+ def toggled=(boolean)
19
+ @toggled = !boolean
20
+ toggle
21
+ end
22
+
23
+ def clicked_left_mouse_button(sender, x, y)
24
+ toggle
25
+
26
+ @block.call(self) if @block
27
+ end
28
+
29
+ def toggle
30
+ if @toggled
31
+ @toggled = false
32
+ @text.text = ""
33
+ else
34
+ @toggled = true
35
+ @text.text = @options[:checkmark]
36
+ end
37
+ end
38
+
39
+ def recalculate
40
+ super
41
+
42
+ _width = dimensional_size(@style.width, :width)
43
+ _height= dimensional_size(@style.height,:height)
44
+ @width = _width ? _width : @text.textobject.text_width(@options[:checkmark])
45
+ @height = _height ? _height : @text.height
46
+ update_background
47
+ end
48
+
49
+ def value
50
+ @toggled
51
+ end
52
+ end
53
+ end
54
+ end
@@ -10,15 +10,18 @@ module CyberarmEngine
10
10
 
11
11
  @down_keys = {}
12
12
 
13
- @root_container = Stack.new
13
+ @root_container = Element::Stack.new(gui_state: self)
14
14
  @game_objects << @root_container
15
15
  @containers = [@root_container]
16
16
 
17
+ @active_width = window.width
18
+ @active_height = window.height
19
+
17
20
  @focus = nil
18
21
  @mouse_over = nil
19
22
  @mouse_down_on = {}
20
23
  @mouse_down_position = {}
21
-
24
+ @pending_recalculate_request = false
22
25
 
23
26
  setup
24
27
  end
@@ -35,6 +38,11 @@ module CyberarmEngine
35
38
  end
36
39
 
37
40
  def update
41
+ if @pending_recalculate_request
42
+ @root_container.recalculate
43
+ @pending_recalculate_request = false
44
+ end
45
+
38
46
  super
39
47
 
40
48
  new_mouse_over = @root_container.hit_element?(window.mouse_x, window.mouse_y)
@@ -49,6 +57,11 @@ module CyberarmEngine
49
57
  redirect_holding_mouse_button(:left) if @mouse_over && Gosu.button_down?(Gosu::MsLeft)
50
58
  redirect_holding_mouse_button(:middle) if @mouse_over && Gosu.button_down?(Gosu::MsMiddle)
51
59
  redirect_holding_mouse_button(:right) if @mouse_over && Gosu.button_down?(Gosu::MsRight)
60
+
61
+ request_recalculate if @active_width != window.width || @active_height != window.height
62
+
63
+ @active_width = window.width
64
+ @active_height = window.height
52
65
  end
53
66
 
54
67
  def button_down(id)
@@ -115,5 +128,10 @@ module CyberarmEngine
115
128
  def redirect_mouse_wheel(button)
116
129
  @mouse_over.publish(:"mouse_wheel_#{button}", window.mouse_x, window.mouse_y) if @mouse_over
117
130
  end
131
+
132
+ # Schedule a full GUI recalculation on next update
133
+ def request_recalculate
134
+ @pending_recalculate_request = true
135
+ end
118
136
  end
119
137
  end
@@ -26,7 +26,23 @@ module CyberarmEngine
26
26
  end
27
27
  end
28
28
 
29
- hash.merge(options)
29
+ deep_merge(hash, options)
30
+ end
31
+
32
+ # Derived from Rails Hash#deep_merge!
33
+ # Enables passing partial themes through Element options without issue
34
+ def deep_merge(original, intergrate, &block)
35
+ hash = original.merge(intergrate) do |key, this_val, other_val|
36
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
37
+ deep_merge(this_val, other_val, &block)
38
+ elsif block_given?
39
+ block.call(key, this_val, other_val)
40
+ else
41
+ other_val
42
+ end
43
+ end
44
+
45
+ return hash
30
46
  end
31
47
 
32
48
  THEME = {
@@ -74,7 +90,7 @@ module CyberarmEngine
74
90
  caret_interval: 500,
75
91
  },
76
92
 
77
- Image: {
93
+ Image: { # < Element
78
94
  retro: false
79
95
  },
80
96
 
@@ -88,7 +104,16 @@ module CyberarmEngine
88
104
 
89
105
  ToggleButton: { # < Button
90
106
  checkmark: "√"
107
+ },
108
+
109
+ Progress: { # < Element
110
+ width: 250,
111
+ height: 36,
112
+ background: 0xff111111,
113
+ fraction_background: [0xffc75e61, 0xffe26623],
114
+ border_thickness: 4,
115
+ border_color: [0xffd59674, 0xffff8746]
91
116
  }
92
117
  }.freeze
93
118
  end
94
- end
119
+ end
@@ -1,4 +1,4 @@
1
1
  module CyberarmEngine
2
2
  NAME = "InDev"
3
- VERSION = "0.8.1"
3
+ VERSION = "0.9.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyberarm_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyberarm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-21 00:00:00.000000000 Z
11
+ date: 2019-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -93,21 +93,22 @@ files:
93
93
  - lib/cyberarm_engine/objects/text.rb
94
94
  - lib/cyberarm_engine/objects/timer.rb
95
95
  - lib/cyberarm_engine/ui/border_canvas.rb
96
- - lib/cyberarm_engine/ui/button.rb
97
- - lib/cyberarm_engine/ui/check_box.rb
98
- - lib/cyberarm_engine/ui/container.rb
99
96
  - lib/cyberarm_engine/ui/dsl.rb
100
- - lib/cyberarm_engine/ui/edit_line.rb
101
97
  - lib/cyberarm_engine/ui/element.rb
98
+ - lib/cyberarm_engine/ui/elements/button.rb
99
+ - lib/cyberarm_engine/ui/elements/check_box.rb
100
+ - lib/cyberarm_engine/ui/elements/container.rb
101
+ - lib/cyberarm_engine/ui/elements/edit_line.rb
102
+ - lib/cyberarm_engine/ui/elements/flow.rb
103
+ - lib/cyberarm_engine/ui/elements/image.rb
104
+ - lib/cyberarm_engine/ui/elements/label.rb
105
+ - lib/cyberarm_engine/ui/elements/progress.rb
106
+ - lib/cyberarm_engine/ui/elements/stack.rb
107
+ - lib/cyberarm_engine/ui/elements/toggle_button.rb
102
108
  - lib/cyberarm_engine/ui/event.rb
103
- - lib/cyberarm_engine/ui/flow.rb
104
109
  - lib/cyberarm_engine/ui/gui_state.rb
105
- - lib/cyberarm_engine/ui/image.rb
106
- - lib/cyberarm_engine/ui/label.rb
107
- - lib/cyberarm_engine/ui/stack.rb
108
110
  - lib/cyberarm_engine/ui/style.rb
109
111
  - lib/cyberarm_engine/ui/theme.rb
110
- - lib/cyberarm_engine/ui/toggle_button.rb
111
112
  - lib/cyberarm_engine/version.rb
112
113
  homepage: https://github.com/cyberarm/cyberarm_engine
113
114
  licenses:
@@ -129,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  - !ruby/object:Gem::Version
130
131
  version: '0'
131
132
  requirements: []
132
- rubygems_version: 3.0.4
133
+ rubygems_version: 3.0.3
133
134
  signing_key:
134
135
  specification_version: 4
135
136
  summary: Make games quickly and easily with gosu