mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-17 05:22:35 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4642056576 | |||
| 3d2402b7f7 | |||
| f4a783b371 | |||
| d1dec5791b | |||
| 8c5c5e1b7b | |||
| f9324448ee | |||
| 34d53ae1cb | |||
| 74d1ddd16b | |||
| da69f057a0 | |||
| 4f4770db0e | |||
| b6730204e8 | |||
| d8dc621c62 | |||
| 739ae86966 | |||
| 3be16c644f | |||
| 805077d232 | |||
| db1a0683e2 |
@@ -160,5 +160,9 @@ module CyberarmEngine
|
|||||||
def to_s
|
def to_s
|
||||||
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
{x: @x, y: @y, z: @z, weight: @weight}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -6,7 +6,7 @@ module CyberarmEngine
|
|||||||
attr_reader :text, :textobject
|
attr_reader :text, :textobject
|
||||||
|
|
||||||
def initialize(text, options={})
|
def initialize(text, options={})
|
||||||
@text = text || ""
|
@text = text.to_s || ""
|
||||||
@options = options
|
@options = options
|
||||||
@size = options[:size] || 18
|
@size = options[:size] || 18
|
||||||
@font = options[:font] || "sans-serif"#Gosu.default_font_name
|
@font = options[:font] || "sans-serif"#Gosu.default_font_name
|
||||||
@@ -72,7 +72,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def height
|
def height
|
||||||
(@text.lines.count) * textobject.height
|
@text.lines.count > 0 ? (@text.lines.count) * textobject.height : @textobject.height
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
|
|||||||
@@ -66,31 +66,31 @@ module CyberarmEngine
|
|||||||
@top.z = @element.z
|
@top.z = @element.z
|
||||||
|
|
||||||
@top.width = @element.width
|
@top.width = @element.width
|
||||||
@top.height = @element.border_thickness_top
|
@top.height = @element.style.border_thickness_top
|
||||||
|
|
||||||
# RIGHT
|
# RIGHT
|
||||||
@right.x = @element.x + @element.width
|
@right.x = @element.x + @element.width
|
||||||
@right.y = @element.y + @element.border_thickness_top
|
@right.y = @element.y + @element.style.border_thickness_top
|
||||||
@right.z = @element.z
|
@right.z = @element.z
|
||||||
|
|
||||||
@right.width = -@element.border_thickness_right
|
@right.width = -@element.style.border_thickness_right
|
||||||
@right.height = @element.height - @element.border_thickness_top
|
@right.height = @element.height - @element.style.border_thickness_top
|
||||||
|
|
||||||
# BOTTOM
|
# BOTTOM
|
||||||
@bottom.x = @element.x
|
@bottom.x = @element.x
|
||||||
@bottom.y = @element.y + @element.height
|
@bottom.y = @element.y + @element.height
|
||||||
@bottom.z = @element.z
|
@bottom.z = @element.z
|
||||||
|
|
||||||
@bottom.width = @element.width - @element.border_thickness_right
|
@bottom.width = @element.width - @element.style.border_thickness_right
|
||||||
@bottom.height = -@element.border_thickness_bottom
|
@bottom.height = -@element.style.border_thickness_bottom
|
||||||
|
|
||||||
# LEFT
|
# LEFT
|
||||||
@left.x = @element.x
|
@left.x = @element.x
|
||||||
@left.y = @element.y
|
@left.y = @element.y
|
||||||
@left.z = @element.z
|
@left.z = @element.z
|
||||||
|
|
||||||
@left.width = @element.border_thickness_left
|
@left.width = @element.style.border_thickness_left
|
||||||
@left.height = @element.height - @element.border_thickness_bottom
|
@left.height = @element.height - @element.style.border_thickness_bottom
|
||||||
|
|
||||||
@top.update
|
@top.update
|
||||||
@right.update
|
@right.update
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ module CyberarmEngine
|
|||||||
def initialize(text, options = {}, block = nil)
|
def initialize(text, options = {}, block = nil)
|
||||||
super(text, options, block)
|
super(text, options, block)
|
||||||
|
|
||||||
@background_canvas.background = default(:background)
|
@style.background_canvas.background = default(:background)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
@@ -18,17 +18,17 @@ module CyberarmEngine
|
|||||||
@focus = false unless window.button_down?(Gosu::MsLeft)
|
@focus = false unless window.button_down?(Gosu::MsLeft)
|
||||||
|
|
||||||
if @focus
|
if @focus
|
||||||
@background_canvas.background = default(:active, :background)
|
@style.background_canvas.background = default(:active, :background)
|
||||||
@text.color = default(:active, :color)
|
@text.color = default(:active, :color)
|
||||||
else
|
else
|
||||||
@background_canvas.background = default(:hover, :background)
|
@style.background_canvas.background = default(:hover, :background)
|
||||||
@text.color = default(:hover, :color)
|
@text.color = default(:hover, :color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def left_mouse_button(sender, x, y)
|
def left_mouse_button(sender, x, y)
|
||||||
@focus = true
|
@focus = true
|
||||||
@background_canvas.background = default(:active, :background)
|
@style.background_canvas.background = default(:active, :background)
|
||||||
window.current_state.focus = self
|
window.current_state.focus = self
|
||||||
@text.color = default(:active, :color)
|
@text.color = default(:active, :color)
|
||||||
end
|
end
|
||||||
@@ -42,7 +42,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def leave(sender)
|
def leave(sender)
|
||||||
@background_canvas.background = default(:background)
|
@style.background_canvas.background = default(:background)
|
||||||
@text.color = default(:color)
|
@text.color = default(:color)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ module CyberarmEngine
|
|||||||
add(@toggle_button)
|
add(@toggle_button)
|
||||||
add(@label)
|
add(@label)
|
||||||
|
|
||||||
@width = @toggle_button.width + @label.width
|
@style.width = @toggle_button.width + @label.width
|
||||||
@height = @toggle_button.height + @label.height
|
@style.height = @toggle_button.height + @label.height
|
||||||
end
|
end
|
||||||
|
|
||||||
def text=(text)
|
def text=(text)
|
||||||
@@ -21,6 +21,14 @@ module CyberarmEngine
|
|||||||
recalculate
|
recalculate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def value
|
||||||
|
@toggle_button.value
|
||||||
|
end
|
||||||
|
|
||||||
|
def value=(bool)
|
||||||
|
@toggle_button.vlaue = bool
|
||||||
|
end
|
||||||
|
|
||||||
def define_label_singletons
|
def define_label_singletons
|
||||||
@label.define_singleton_method(:_toggle_button) do |button|
|
@label.define_singleton_method(:_toggle_button) do |button|
|
||||||
@_toggle_button = button
|
@_toggle_button = button
|
||||||
|
|||||||
@@ -15,12 +15,9 @@ module CyberarmEngine
|
|||||||
@text_color = options[:color]
|
@text_color = options[:color]
|
||||||
|
|
||||||
@children = []
|
@children = []
|
||||||
|
|
||||||
@theme = {}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def build
|
def build
|
||||||
@theme.merge(@parent.theme) if @parent
|
|
||||||
@block.call(self) if @block
|
@block.call(self) if @block
|
||||||
|
|
||||||
recalculate
|
recalculate
|
||||||
@@ -40,14 +37,6 @@ module CyberarmEngine
|
|||||||
@children.each(&:update)
|
@children.each(&:update)
|
||||||
end
|
end
|
||||||
|
|
||||||
def theme
|
|
||||||
@theme
|
|
||||||
end
|
|
||||||
|
|
||||||
def color(color)
|
|
||||||
@theme[:color] = color
|
|
||||||
end
|
|
||||||
|
|
||||||
def hit_element?(x, y)
|
def hit_element?(x, y)
|
||||||
@children.reverse_each do |child|
|
@children.reverse_each do |child|
|
||||||
case child
|
case child
|
||||||
@@ -64,20 +53,23 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
@current_position = Vector.new(@margin_left, @margin_top)
|
@current_position = Vector.new(@style.margin_left + @style.padding_left, @style.margin_top + @style.padding_top)
|
||||||
|
return unless visible?
|
||||||
|
stylize
|
||||||
|
|
||||||
layout
|
layout
|
||||||
|
|
||||||
@width = @max_width ? @max_width : (@children.map {|c| c.x + c.width + c.margin_right }.max || 0).round
|
@style.width = @max_width ? @max_width : (@children.map {|c| c.x + c.outer_width }.max || 0).round
|
||||||
@height = @max_height ? @max_height : (@children.map {|c| c.y + c.height + c.margin_bottom}.max || 0).round
|
@style.height = @max_height ? @max_height : (@children.map {|c| c.y + c.outer_height}.max || 0).round
|
||||||
|
|
||||||
# Move child to parent after positioning
|
# Move child to parent after positioning
|
||||||
@children.each do |child|
|
@children.each do |child|
|
||||||
child.x += @x
|
child.x += @x
|
||||||
child.y += @y
|
child.y += @y
|
||||||
|
|
||||||
# Fix child being displaced
|
child.stylize
|
||||||
child.recalculate
|
child.recalculate
|
||||||
|
child.reposition # TODO: Implement top,bottom,left,center, and right positioning
|
||||||
end
|
end
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
@@ -88,7 +80,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def max_width
|
def max_width
|
||||||
@max_width ? @max_width : window.width - (@parent ? @parent.margin_right + @margin_right : @margin_right)
|
@max_width ? @max_width : window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fits_on_line?(element) # Flow
|
def fits_on_line?(element) # Flow
|
||||||
@@ -97,13 +89,13 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def position_on_current_line(element) # Flow
|
def position_on_current_line(element) # Flow
|
||||||
element.x = element.margin_left + @current_position.x
|
element.x = element.style.margin_left + @current_position.x
|
||||||
element.y = element.margin_top + @current_position.y
|
element.y = element.style.margin_top + @current_position.y
|
||||||
|
|
||||||
element.recalculate
|
element.recalculate
|
||||||
|
|
||||||
@current_position.x += element.outer_width
|
@current_position.x += element.outer_width
|
||||||
@current_position.x = @margin_left if @current_position.x >= max_width
|
@current_position.x = @style.margin_left if @current_position.x >= max_width
|
||||||
end
|
end
|
||||||
|
|
||||||
def tallest_neighbor(querier, y_position) # Flow
|
def tallest_neighbor(querier, y_position) # Flow
|
||||||
@@ -117,11 +109,11 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def position_on_next_line(child) # Flow
|
def position_on_next_line(child) # Flow
|
||||||
@current_position.x = @margin_left
|
@current_position.x = @style.margin_left
|
||||||
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height
|
@current_position.y += tallest_neighbor(child, @current_position.y).outer_height
|
||||||
|
|
||||||
child.x = child.margin_left + @current_position.x
|
child.x = child.style.margin_left + @current_position.x
|
||||||
child.y = child.margin_top + @current_position.y
|
child.y = child.style.margin_top + @current_position.y
|
||||||
|
|
||||||
child.recalculate
|
child.recalculate
|
||||||
|
|
||||||
@@ -129,8 +121,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def move_to_next_line(element) # Stack
|
def move_to_next_line(element) # Stack
|
||||||
element.x = element.margin_left + @current_position.x
|
element.x = element.style.margin_left + @current_position.x
|
||||||
element.y = element.margin_top + @current_position.y
|
element.y = element.style.margin_top + @current_position.y
|
||||||
|
|
||||||
element.recalculate
|
element.recalculate
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ module CyberarmEngine
|
|||||||
module DSL
|
module DSL
|
||||||
def flow(options = {}, &block)
|
def flow(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
_container = Flow.new(options, block)
|
_container = Flow.new(options, block)
|
||||||
@containers << _container
|
@containers << _container
|
||||||
_container.build
|
_container.build
|
||||||
options[:parent].add(_container)
|
_container.parent.add(_container)
|
||||||
@containers.pop
|
@containers.pop
|
||||||
|
|
||||||
return _container
|
return _container
|
||||||
@@ -13,10 +14,11 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def stack(options = {}, &block)
|
def stack(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
_container = Stack.new(options, block)
|
_container = Stack.new(options, block)
|
||||||
@containers << _container
|
@containers << _container
|
||||||
_container.build
|
_container.build
|
||||||
options[:parent].add(_container)
|
_container.parent.add(_container)
|
||||||
@containers.pop
|
@containers.pop
|
||||||
|
|
||||||
return _container
|
return _container
|
||||||
@@ -24,6 +26,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def label(text, options = {}, &block)
|
def label(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
_element = Label.new(text, options, block)
|
_element = Label.new(text, options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
@@ -32,6 +35,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def button(text, options = {}, &block)
|
def button(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
_element = Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
|
_element = Button.new(text, options, block) { if block.is_a?(Proc); block.call; end }
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
@@ -40,6 +44,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def edit_line(text, options = {}, &block)
|
def edit_line(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
_element = EditLine.new(text, options, block)
|
_element = EditLine.new(text, options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
@@ -48,6 +53,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def toggle_button(options = {}, &block)
|
def toggle_button(options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
_element = ToggleButton.new(options, block)
|
_element = ToggleButton.new(options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
@@ -56,6 +62,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def check_box(text, options = {}, &block)
|
def check_box(text, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
_element = CheckBox.new(text, options, block)
|
_element = CheckBox.new(text, options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
@@ -64,6 +71,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def image(path, options = {}, &block)
|
def image(path, options = {}, &block)
|
||||||
options[:parent] = @containers.last
|
options[:parent] = @containers.last
|
||||||
|
options[:theme] = current_theme
|
||||||
_element = Image.new(path, options, block)
|
_element = Image.new(path, options, block)
|
||||||
@containers.last.add(_element)
|
@containers.last.add(_element)
|
||||||
|
|
||||||
@@ -71,12 +79,15 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def background(color = Gosu::Color::NONE)
|
def background(color = Gosu::Color::NONE)
|
||||||
@containers.last.background = color
|
@containers.last.style.background = color
|
||||||
end
|
end
|
||||||
|
|
||||||
# Foreground color, e.g. Text
|
def theme(theme)
|
||||||
def color(color)
|
@containers.last.options[:theme] = theme
|
||||||
@containers.last.color(color)
|
end
|
||||||
|
|
||||||
|
def current_theme
|
||||||
|
@containers.last.options[:theme]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -5,7 +5,6 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@type = default(:type)
|
@type = default(:type)
|
||||||
|
|
||||||
|
|
||||||
@caret_width = default(:caret_width)
|
@caret_width = default(:caret_width)
|
||||||
@caret_height= @text.height
|
@caret_height= @text.height
|
||||||
@caret_color = default(:caret_color)
|
@caret_color = default(:caret_color)
|
||||||
@@ -20,7 +19,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
Gosu.clip_to(@text.x, @text.y, @width, @text.height) do
|
Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
|
||||||
draw_text
|
draw_text
|
||||||
Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret
|
Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret
|
||||||
end
|
end
|
||||||
@@ -47,20 +46,23 @@ module CyberarmEngine
|
|||||||
|
|
||||||
def enter(sender)
|
def enter(sender)
|
||||||
if @focus
|
if @focus
|
||||||
@background_canvas.background = default(:active, :background)
|
@style.background_canvas.background = default(:active, :background)
|
||||||
@text.color = default(:active, :color)
|
@text.color = default(:active, :color)
|
||||||
else
|
else
|
||||||
@background_canvas.background = default(:hover, :background)
|
@style.background_canvas.background = default(:hover, :background)
|
||||||
@text.color = default(:hover, :color)
|
@text.color = default(:hover, :color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def leave(sender)
|
def leave(sender)
|
||||||
|
unless @focus
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def blur(sender)
|
def blur(sender)
|
||||||
@focus = false
|
@focus = false
|
||||||
@background_canvas.background = default(:background)
|
@style.background_canvas.background = default(:background)
|
||||||
@text.color = default(:color)
|
@text.color = default(:color)
|
||||||
window.text_input = nil
|
window.text_input = nil
|
||||||
end
|
end
|
||||||
@@ -77,7 +79,7 @@ module CyberarmEngine
|
|||||||
def recalculate
|
def recalculate
|
||||||
super
|
super
|
||||||
|
|
||||||
@width = default(:width)
|
@style.width = default(:width)
|
||||||
update_background
|
update_background
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -5,99 +5,90 @@ module CyberarmEngine
|
|||||||
include Common
|
include Common
|
||||||
|
|
||||||
attr_accessor :x, :y, :z, :enabled
|
attr_accessor :x, :y, :z, :enabled
|
||||||
attr_reader :width, :height, :parent, :options, :event_handler, :background_canvas, :border_canvas
|
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
|
||||||
|
|
||||||
attr_reader :border_thickness, :border_thickness_left, :border_thickness_right, :border_thickness_top, :border_thickness_bottom
|
|
||||||
attr_reader :border_color, :border_color_left, :border_color_right, :border_color_top, :border_color_bottom
|
|
||||||
|
|
||||||
attr_reader :padding, :padding_left, :padding_right, :padding_top, :padding_bottom
|
|
||||||
attr_reader :margin, :margin_left, :margin_right, :margin_top, :margin_bottom
|
|
||||||
|
|
||||||
def initialize(options = {}, block = nil)
|
def initialize(options = {}, block = nil)
|
||||||
@parent = options[:parent] # parent Container (i.e. flow/stack)
|
@parent = options.delete(:parent) # parent Container (i.e. flow/stack)
|
||||||
options = theme_defaults.merge(options)
|
options = theme_defaults(options)
|
||||||
@options = options
|
@options = options
|
||||||
@block = block
|
@block = block
|
||||||
|
|
||||||
@style = Style.new(options)
|
|
||||||
@focus = false
|
@focus = false
|
||||||
@background_canvas = Background.new
|
@enabled = true
|
||||||
@border_canvas = BorderCanvas.new(element: self)
|
@visible = true
|
||||||
|
|
||||||
@x = default(:x)
|
@style = Style.new(options)
|
||||||
@y = default(:y)
|
|
||||||
@z = default(:z)
|
@x = @style.x
|
||||||
|
@y = @style.y
|
||||||
|
@z = @style.z
|
||||||
|
|
||||||
@fixed_x = @x if @x != 0
|
@fixed_x = @x if @x != 0
|
||||||
@fixed_y = @y if @y != 0
|
@fixed_y = @y if @y != 0
|
||||||
|
|
||||||
@width = default(:width) || $window.width
|
@style.width = default(:width) || $window.width
|
||||||
@height = default(:height) || $window.height
|
@style.height = default(:height) || $window.height
|
||||||
|
|
||||||
set_border_thickness(default(:border_thickness))
|
stylize
|
||||||
|
|
||||||
set_padding(default(:padding))
|
|
||||||
|
|
||||||
set_margin(default(:margin))
|
|
||||||
|
|
||||||
set_background(default(:background))
|
|
||||||
set_border_color(default(:border_color))
|
|
||||||
|
|
||||||
raise "#{self.class} 'x' must be a number" unless @x.is_a?(Numeric)
|
|
||||||
raise "#{self.class} 'y' must be a number" unless @y.is_a?(Numeric)
|
|
||||||
raise "#{self.class} 'z' must be a number" unless @z.is_a?(Numeric)
|
|
||||||
raise "#{self.class} 'width' must be a number" unless @width.is_a?(Numeric) || @width.nil?
|
|
||||||
raise "#{self.class} 'height' must be a number" unless @height.is_a?(Numeric) || @height.nil?
|
|
||||||
raise "#{self.class} 'options' must be a Hash" unless @options.is_a?(Hash)
|
|
||||||
|
|
||||||
# raise "#{self.class} 'padding' must be a number" unless @padding.is_a?(Numeric)
|
|
||||||
|
|
||||||
@enabled = true
|
|
||||||
|
|
||||||
default_events
|
default_events
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def stylize
|
||||||
|
set_border_thickness(@style.border_thickness)
|
||||||
|
|
||||||
|
set_padding(@style.padding)
|
||||||
|
|
||||||
|
set_margin(@style.margin)
|
||||||
|
|
||||||
|
@style.background_canvas = Background.new
|
||||||
|
@style.border_canvas = BorderCanvas.new(element: self)
|
||||||
|
|
||||||
|
set_background(@style.background)
|
||||||
|
set_border_color(@style.border_color)
|
||||||
|
end
|
||||||
|
|
||||||
def set_background(background)
|
def set_background(background)
|
||||||
@background = background
|
@style.background = background
|
||||||
@background_canvas.background = background
|
@style.background_canvas.background = background
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_border_thickness(border_thickness)
|
def set_border_thickness(border_thickness)
|
||||||
@border_thickness = border_thickness
|
@style.border_thickness = border_thickness
|
||||||
|
|
||||||
@border_thickness_left = default(:border_thickness_left) || @border_thickness
|
@style.border_thickness_left = default(:border_thickness_left) || @style.border_thickness
|
||||||
@border_thickness_right = default(:border_thickness_right) || @border_thickness
|
@style.border_thickness_right = default(:border_thickness_right) || @style.border_thickness
|
||||||
@border_thickness_top = default(:border_thickness_top) || @border_thickness
|
@style.border_thickness_top = default(:border_thickness_top) || @style.border_thickness
|
||||||
@border_thickness_bottom = default(:border_thickness_bottom) || @border_thickness
|
@style.border_thickness_bottom = default(:border_thickness_bottom) || @style.border_thickness
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_border_color(color)
|
def set_border_color(color)
|
||||||
@border_color = color
|
@style.border_color = color
|
||||||
|
|
||||||
@border_color_left = default(:border_color_left) || @border_color
|
@style.border_color_left = default(:border_color_left) || @style.border_color
|
||||||
@border_color_right = default(:border_color_right) || @border_color
|
@style.border_color_right = default(:border_color_right) || @style.border_color
|
||||||
@border_color_top = default(:border_color_top) || @border_color
|
@style.border_color_top = default(:border_color_top) || @style.border_color
|
||||||
@border_color_bottom = default(:border_color_bottom) || @border_color
|
@style.border_color_bottom = default(:border_color_bottom) || @style.border_color
|
||||||
|
|
||||||
@border_canvas.color = color
|
@style.border_canvas.color = color
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_padding(padding)
|
def set_padding(padding)
|
||||||
@padding = padding
|
@style.padding = padding
|
||||||
|
|
||||||
@padding_left = default(:padding_left) || @padding
|
@style.padding_left = default(:padding_left) || @style.padding
|
||||||
@padding_right = default(:padding_right) || @padding
|
@style.padding_right = default(:padding_right) || @style.padding
|
||||||
@padding_top = default(:padding_top) || @padding
|
@style.padding_top = default(:padding_top) || @style.padding
|
||||||
@padding_bottom = default(:padding_bottom) || @padding
|
@style.padding_bottom = default(:padding_bottom) || @style.padding
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_margin(margin)
|
def set_margin(margin)
|
||||||
@margin = margin
|
@style.margin = margin
|
||||||
|
|
||||||
@margin_left = default(:margin_left) || @margin
|
@style.margin_left = default(:margin_left) || @style.margin
|
||||||
@margin_right = default(:margin_right) || @margin
|
@style.margin_right = default(:margin_right) || @style.margin
|
||||||
@margin_top = default(:margin_top) || @margin
|
@style.margin_top = default(:margin_top) || @style.margin
|
||||||
@margin_bottom = default(:margin_bottom) || @margin
|
@style.margin_bottom = default(:margin_bottom) || @style.margin
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_events
|
def default_events
|
||||||
@@ -122,9 +113,30 @@ module CyberarmEngine
|
|||||||
@enabled
|
@enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def visible?
|
||||||
|
@visible
|
||||||
|
end
|
||||||
|
|
||||||
|
def toggle
|
||||||
|
@visible = !@visible
|
||||||
|
root.recalculate
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
@visible = true
|
||||||
|
root.recalculate
|
||||||
|
end
|
||||||
|
|
||||||
|
def hide
|
||||||
|
@visible = false
|
||||||
|
root.recalculate
|
||||||
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
@background_canvas.draw
|
return unless @visible
|
||||||
@border_canvas.draw
|
|
||||||
|
@style.background_canvas.draw
|
||||||
|
@style.border_canvas.draw
|
||||||
render
|
render
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -146,44 +158,44 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def width
|
def width
|
||||||
(@border_thickness_left + @padding_left) + @width + (@padding_right + @border_thickness_right)
|
if visible?
|
||||||
|
(@style.border_thickness_left + @style.padding_left) + @style.width + (@style.padding_right + @style.border_thickness_right)
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def outer_width
|
def outer_width
|
||||||
@margin_left + width + @margin_right
|
@style.margin_left + width + @style.margin_right
|
||||||
end
|
end
|
||||||
|
|
||||||
def height
|
def height
|
||||||
(@border_thickness_top + @padding_top) + @height + (@padding_bottom + @border_thickness_bottom)
|
if visible?
|
||||||
|
(@style.border_thickness_top + @style.padding_top) + @style.height + (@style.padding_bottom + @style.border_thickness_bottom)
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def outer_height
|
def outer_height
|
||||||
@margin_top + height + @margin_bottom
|
@style.margin_top + height + @style.margin_bottom
|
||||||
end
|
|
||||||
|
|
||||||
def style(hash)
|
|
||||||
if hash
|
|
||||||
@style.set(hash)
|
|
||||||
else
|
|
||||||
@style.hash
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def background=(_background)
|
def background=(_background)
|
||||||
@background_canvas.background=(_background)
|
@style.background_canvas.background=(_background)
|
||||||
update_background
|
update_background
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_background
|
def update_background
|
||||||
@background_canvas.x = @x
|
@style.background_canvas.x = @x
|
||||||
@background_canvas.y = @y
|
@style.background_canvas.y = @y
|
||||||
@background_canvas.z = @z
|
@style.background_canvas.z = @z
|
||||||
@background_canvas.width = width
|
@style.background_canvas.width = width
|
||||||
@background_canvas.height = height
|
@style.background_canvas.height = height
|
||||||
|
|
||||||
@background_canvas.update
|
@style.background_canvas.update
|
||||||
|
|
||||||
@border_canvas.update
|
@style.border_canvas.update
|
||||||
end
|
end
|
||||||
|
|
||||||
def root
|
def root
|
||||||
@@ -206,6 +218,9 @@ module CyberarmEngine
|
|||||||
raise "#{self.class}#recalculate was not overridden!"
|
raise "#{self.class}#recalculate was not overridden!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reposition
|
||||||
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
raise "#{self.class}#value was not overridden!"
|
raise "#{self.class}#value was not overridden!"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -23,7 +23,11 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
@image.draw(@border_thickness_left + @padding_left + @x, @border_thickness_top + @padding_top + @y, @z + 2, @scale_x, @scale_y) # TODO: Add color support?
|
@image.draw(
|
||||||
|
@style.border_thickness_left + @style.padding_left + @x,
|
||||||
|
@style.border_thickness_top + @style.padding_top + @y,
|
||||||
|
@z + 2,
|
||||||
|
@scale_x, @scale_y) # TODO: Add color support?
|
||||||
end
|
end
|
||||||
|
|
||||||
def clicked_left_mouse_button(sender, x, y)
|
def clicked_left_mouse_button(sender, x, y)
|
||||||
@@ -31,8 +35,8 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
@width = @image.width * @scale_x
|
@style.width = @image.width * @scale_x
|
||||||
@height = @image.height * @scale_y
|
@style.height = @image.height * @scale_y
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
|
|||||||
@@ -17,11 +17,11 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
@width = @text.width.round
|
@style.width = @text.width.round
|
||||||
@height= @text.height.round
|
@style.height= @text.height.round
|
||||||
|
|
||||||
@text.x = @border_thickness_left + @padding_left + @x
|
@text.x = @style.border_thickness_left + @style.padding_left + @x
|
||||||
@text.y = @border_thickness_top + @padding_top + @y
|
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
||||||
@text.z = @z + 3
|
@text.z = @z + 3
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
|
|||||||
@@ -1,15 +1,37 @@
|
|||||||
|
module Gosu
|
||||||
|
class Color
|
||||||
|
def _dump(level)
|
||||||
|
[
|
||||||
|
"%02X" % self.alpha,
|
||||||
|
"%02X" % self.red,
|
||||||
|
"%02X" % self.green,
|
||||||
|
"%02X" % self.blue
|
||||||
|
].join
|
||||||
|
end
|
||||||
|
|
||||||
|
def self._load(hex)
|
||||||
|
argb(hex.to_i(16))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Style
|
class Style
|
||||||
def initialize(hash)
|
def initialize(hash = {})
|
||||||
@hash = hash
|
@hash = Marshal.load(Marshal.dump(hash))
|
||||||
end
|
end
|
||||||
|
|
||||||
def hash
|
def method_missing(method, *args, &block)
|
||||||
@hash
|
if method.to_s.end_with?("=")
|
||||||
end
|
raise "Did not expect more than 1 argument" if args.size > 1
|
||||||
|
return @hash[method.to_s.sub("=", "").to_sym] = args.first
|
||||||
|
|
||||||
def set(hash)
|
elsif args.size == 0
|
||||||
@hash.merge!(hash)
|
return @hash[method]
|
||||||
|
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Did not expect arguments"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -9,21 +9,24 @@ module CyberarmEngine
|
|||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
def theme_defaults
|
def theme_defaults(options)
|
||||||
raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element)
|
raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element)
|
||||||
|
_theme = THEME
|
||||||
|
_theme = _theme.merge(options[:theme]) if options[:theme]
|
||||||
|
_theme.delete(:theme) if options[:theme]
|
||||||
|
|
||||||
hash = {}
|
hash = {}
|
||||||
class_names = self.class.ancestors
|
class_names = self.class.ancestors
|
||||||
class_names = class_names[0..class_names.index(CyberarmEngine::Element)].map! {|c| c.to_s.split("::").last.to_sym}.reverse!
|
class_names = class_names[0..class_names.index(CyberarmEngine::Element)].map! {|c| c.to_s.split("::").last.to_sym}.reverse!
|
||||||
|
|
||||||
class_names.each do |klass|
|
class_names.each do |klass|
|
||||||
next unless data = THEME.dig(klass)
|
next unless data = _theme.dig(klass)
|
||||||
data.each do |key, value|
|
data.each do |key, value|
|
||||||
hash.merge!(data)
|
hash.merge!(data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
hash
|
hash.merge(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
THEME = {
|
THEME = {
|
||||||
@@ -86,6 +89,6 @@ module CyberarmEngine
|
|||||||
ToggleButton: { # < Button
|
ToggleButton: { # < Button
|
||||||
checkmark: "√"
|
checkmark: "√"
|
||||||
}
|
}
|
||||||
}
|
}.freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -38,7 +38,7 @@ module CyberarmEngine
|
|||||||
def recalculate
|
def recalculate
|
||||||
super
|
super
|
||||||
|
|
||||||
@width = @text.textobject.text_width(@options[:checkmark])
|
@style.width = @text.textobject.text_width(@options[:checkmark])
|
||||||
update_background
|
update_background
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
NAME = "InDev"
|
NAME = "InDev"
|
||||||
VERSION = "0.4.0"
|
VERSION = "0.8.1"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user