lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.14.0 vs lib/cyberarm_engine/ui/element.rb in cyberarm_engine-0.15.0
- old
+ new
@@ -14,24 +14,24 @@
@block = block
@focus = false
@enabled = true
@visible = true
- @tip = @options[:tip] ? @options[:tip] : ""
+ @tip = @options[:tip] || ""
@style = Style.new(options)
+ @root ||= nil
+ @gui_state ||= nil
+
@x = @style.x
@y = @style.y
@z = @style.z
@width = 0
@height = 0
- @fixed_x = @x if @x != 0
- @fixed_y = @y if @y != 0
-
@style.width = default(:width) || nil
@style.height = default(:height) || nil
@style.background_canvas = Background.new
@style.border_canvas = BorderCanvas.new(element: self)
@@ -40,20 +40,26 @@
default_events
end
def stylize
+ set_static_position
set_border_thickness(@style.border_thickness)
set_padding(@style.padding)
set_margin(@style.margin)
set_background(@style.background)
set_border_color(@style.border_color)
end
+ def set_static_position
+ @x = @style.x if @style.x != 0
+ @y = @style.y if @style.y != 0
+ end
+
def set_background(background)
@style.background = background
@style.background_canvas.background = background
end
@@ -94,11 +100,11 @@
@style.margin_top = default(:margin_top) || @style.margin
@style.margin_bottom = default(:margin_bottom) || @style.margin
end
def default_events
- [:left, :middle, :right].each do |button|
+ %i[left middle right].each do |button|
event(:"#{button}_mouse_button")
event(:"released_#{button}_mouse_button")
event(:"clicked_#{button}_mouse_button")
event(:"holding_#{button}_mouse_button")
end
@@ -127,25 +133,30 @@
@visible = !@visible
root.gui_state.request_recalculate
end
def show
+ bool = visible?
@visible = true
- root.gui_state.request_recalculate
+ root.gui_state.request_recalculate unless bool
end
def hide
+ bool = visible?
@visible = false
- root.gui_state.request_recalculate
+ root.gui_state.request_recalculate if bool
end
def draw
- return unless @visible
+ return unless visible?
@style.background_canvas.draw
@style.border_canvas.draw
- render
+
+ Gosu.clip_to(@x, @y, width, height) do
+ render
+ end
end
def update
end
@@ -153,20 +164,20 @@
end
def button_up(id)
end
- def draggable?(button)
+ def draggable?(_button)
false
end
def render
end
def hit?(x, y)
x.between?(@x, @x + width) &&
- y.between?(@y, @y + height)
+ y.between?(@y, @y + height)
end
def width
if visible?
inner_width + @width
@@ -213,25 +224,24 @@
def inner_height
(@style.border_thickness_top + @style.padding_top) + (@style.padding_bottom + @style.border_thickness_bottom)
end
- private def dimensional_size(size, dimension)
- raise "dimension must be either :width or :height" unless dimension == :width || dimension == :height
+ def dimensional_size(size, dimension)
+ raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)
+
if size && size.is_a?(Numeric)
if size.between?(0.0, 1.0)
- ((@parent.send(:"content_#{dimension}") - self.send(:"noncontent_#{dimension}")) * size).round
+ ((@parent.send(:"content_#{dimension}") - send(:"noncontent_#{dimension}")) * size).round
else
size
end
- else
- nil
end
end
def background=(_background)
- @style.background_canvas.background=(_background)
+ @style.background_canvas.background = (_background)
update_background
end
def update_background
@style.background_canvas.x = @x
@@ -276,14 +286,14 @@
def value
raise "#{self.class}#value was not overridden!"
end
- def value=(value)
+ def value=(_value)
raise "#{self.class}#value= was not overridden!"
end
def to_s
- "#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} value=#{ value.is_a?(String) ? "\"#{value}\"" : value }"
+ "#{self.class} x=#{x} y=#{y} width=#{width} height=#{height} value=#{value.is_a?(String) ? "\"#{value}\"" : value}"
end
end
-end
\ No newline at end of file
+end