Compare commits

9 Commits

Author SHA1 Message Date
6cf4cd73dd Fixed toggling Element enabled state not visually shown 2021-11-18 15:37:57 -06:00
5e5f8ba7ea Added set_color and set_font to Element- fixes image elements unable to change their color when hovered, fixes hovered text changing its color incorrectly 2021-11-18 13:11:02 -06:00
63a51d9d2f Improved scroll_height calculation to adapt to how Flow's flow 2021-11-18 11:58:11 -06:00
6af384536a Fixed locking up when performing text wrapping due to text width check not being satisfied 2021-11-17 22:30:30 -06:00
c1b25d2045 Fixed centered text with unequal margins/paddings/border thicknesses being offset, fixed text wrapping using parents #width (which includes padding and margins) instead of the correct #content_width (which is only the parents content width; which the text is part of) 2021-11-17 17:23:48 -06:00
a915a25699 Updated required gems, reimplemented text wrapping to use a proper binary search and correct inserting newlines in the wrong spot for certain text lengths 2021-11-16 23:35:49 -06:00
0aa9b59316 Containers that need to be recalculated but will not affect their current size can request to have themselves directly recalculated on the next update (fixes scrolling triggering many unneed recalculations), list_box now excludes the currently selected item from the menu, probably fixed list_box callback being called twice instead of the correct once. 2021-11-15 10:17:49 -06:00
d2bf406d29 Bump version 2021-09-23 14:36:19 -05:00
f82c101728 Fixed ListBox sometimes returning self in callback instead of self.value 2021-09-23 14:35:46 -05:00
7 changed files with 126 additions and 47 deletions

View File

@@ -27,8 +27,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = %w[lib assets] spec.require_paths = %w[lib assets]
spec.add_dependency "clipboard", "~> 1.3.5" spec.add_dependency "clipboard", "~> 1.3"
spec.add_dependency "excon", "~> 0.78.0" spec.add_dependency "excon", "~> 0.88"
spec.add_dependency "gosu", "~> 1.1" spec.add_dependency "gosu", "~> 1.1"
spec.add_dependency "gosu_more_drawables", "~> 0.3" spec.add_dependency "gosu_more_drawables", "~> 0.3"
# spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows # spec.add_dependency "ffi", :platforms => [:mswin, :mingw] # Required by Clipboard on Windows

View File

@@ -4,7 +4,7 @@ module CyberarmEngine
include Event include Event
include Common include Common
attr_accessor :x, :y, :z, :enabled, :tip attr_accessor :x, :y, :z, :tip
attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas attr_reader :parent, :options, :style, :event_handler, :background_canvas, :border_canvas
def initialize(options = {}, block = nil) def initialize(options = {}, block = nil)
@@ -51,6 +51,9 @@ module CyberarmEngine
def stylize def stylize
set_static_position set_static_position
set_color
set_font
set_padding set_padding
set_margin set_margin
@@ -70,6 +73,15 @@ module CyberarmEngine
@y = @style.y if @style.y != 0 @y = @style.y if @style.y != 0
end end
def set_color
@style.color = safe_style_fetch(:color)
@text&.color = @style.color
end
def set_font
@text&.swap_font(safe_style_fetch(:text_size), safe_style_fetch(:font))
end
def set_background def set_background
@style.background = safe_style_fetch(:background) @style.background = safe_style_fetch(:background)
@@ -138,14 +150,8 @@ module CyberarmEngine
old_width = width old_width = width
old_height = height old_height = height
_style = @style.send(event)
@style_event = event @style_event = event
if @text.is_a?(CyberarmEngine::Text)
@text.color = _style&.dig(:color) || @style.default[:color]
@text.swap_font(_style&.dig(:text_size) || @style.default[:text_size], _style&.dig(:font) || @style.default[:font])
end
return if self.is_a?(ToolTip) return if self.is_a?(ToolTip)
if old_width != width || old_height != height if old_width != width || old_height != height
@@ -238,6 +244,14 @@ module CyberarmEngine
:handled :handled
end end
def enabled=(boolean)
@enabled = boolean
recalculate
@enabled
end
def enabled? def enabled?
@enabled @enabled
end end
@@ -370,19 +384,42 @@ module CyberarmEngine
end end
def scroll_width def scroll_width
@children.sum(&:width) + noncontent_width @children.sum(&:outer_width)
end end
def scroll_height def scroll_height
@children.sum(&:height) + noncontent_height if is_a?(CyberarmEngine::Element::Flow)
return 0 if @children.size.zero?
pairs_ = []
sorted_children_ = @children.sort_by(&:y)
a_ = []
y_position_ = sorted_children_.first.y
sorted_children_.each do |child|
unless child.y == y_position_
y_position_ = child.y
pairs_ << a_
a_ = []
end
a_ << child
end
pairs_ << a_ unless pairs_.last == a_
pairs_.sum { |pair| pair.map(&:outer_height).max } + @style.padding_bottom + @style.border_thickness_bottom
else
@children.sum(&:outer_height) + @style.padding_bottom + @style.border_thickness_bottom
end
end end
def max_scroll_width def max_scroll_width
scroll_width - width scroll_width - outer_width
end end
def max_scroll_height def max_scroll_height
scroll_height - height scroll_height - outer_height
end end
def dimensional_size(size, dimension) def dimensional_size(size, dimension)

View File

@@ -194,7 +194,9 @@ module CyberarmEngine
if @scroll_position.y < 0 if @scroll_position.y < 0
@scroll_position.y += @scroll_speed @scroll_position.y += @scroll_speed
@scroll_position.y = 0 if @scroll_position.y > 0 @scroll_position.y = 0 if @scroll_position.y > 0
recalculate # recalculate
root.gui_state.request_recalculate_for(self)
return :handled return :handled
end end
@@ -208,7 +210,9 @@ module CyberarmEngine
if @scroll_position.y.abs < max_scroll_height if @scroll_position.y.abs < max_scroll_height
@scroll_position.y -= @scroll_speed @scroll_position.y -= @scroll_speed
@scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height @scroll_position.y = -max_scroll_height if @scroll_position.y.abs > max_scroll_height
recalculate # recalculate
root.gui_state.request_recalculate_for(self)
return :handled return :handled
end end

View File

@@ -46,10 +46,18 @@ module CyberarmEngine
:handled :handled
end end
def clicked_left_mouse_button(_sender, _x, _y)
# @block&.call(self.value) if @enabled
:handled
end
def show_menu def show_menu
@menu.clear @menu.clear
@items.each do |item| @items.each do |item|
next if item == self.value
btn = Button.new( btn = Button.new(
item, item,
{ {
@@ -61,7 +69,7 @@ module CyberarmEngine
}, },
proc do proc do
self.choose = item self.choose = item
@block&.call(item) @block&.call(self.value)
end end
) )

View File

@@ -47,8 +47,8 @@ module CyberarmEngine
when :left when :left
@text.x = @style.border_thickness_left + @style.padding_left + @x @text.x = @style.border_thickness_left + @style.padding_left + @x
when :center when :center
@text.x = if @text.width <= outer_width @text.x = if @text.width <= width
@x + outer_width / 2 - @text.width / 2 @x + width / 2 - @text.width / 2
else # Act as left aligned else # Act as left aligned
@style.border_thickness_left + @style.padding_left + @x @style.border_thickness_left + @style.padding_left + @x
end end
@@ -61,46 +61,64 @@ module CyberarmEngine
end end
def handle_text_wrapping(max_width) def handle_text_wrapping(max_width)
max_width ||= @parent&.width max_width ||= @parent&.content_width
max_width ||= @x - (window.width + noncontent_width) max_width ||= @x - (window.width + noncontent_width)
wrap_behavior = style.text_wrap wrap_behavior = style.text_wrap
copy = @raw_text.to_s.dup copy = @raw_text.to_s.dup
if line_width(copy[0]) <= max_width && line_width(copy) > max_width && wrap_behavior != :none # Only perform text wrapping: if it is enabled, is possible to wrap, and text is too long to fit on one line
breaks = [] if wrap_behavior != :none && line_width(copy[0]) <= max_width && line_width(copy) > max_width
breaks = [] # list of indexes to insert a line break
line_start = 0 line_start = 0
line_end = copy.length line_end = copy.length
while line_start != copy.length stalled = false
if line_width(copy[line_start...line_end]) > max_width stalled_interations = 0
line_end = ((line_end - line_start) / 2.0) max_stalled_iterations = 10
line_end = 1.0 if line_end <= 1 checked_copy_length = line_width(copy[line_start..line_end])
elsif line_end < copy.length && line_width(copy[line_start...line_end + 1]) < max_width
# To small, grow!
# TODO: find a more efficient way
line_end += 1
else # FOUND IT! # find length of lines
entering_line_end = line_end.floor while line_width(copy[line_start..line_end]) > max_width && stalled_interations < max_stalled_iterations
max_reach = line_end.floor - line_start < 63 ? line_end.floor - line_start : 63 search_start = line_start
reach = 0 search_end = line_end
# Perform a binary search to find length of line
while search_start < search_end
midpoint = ((search_start.to_f + search_end) / 2.0).floor
if line_width(copy[line_start..midpoint]) > max_width
search_end = midpoint
else
search_start = midpoint + 1
end
end
if wrap_behavior == :word_wrap if wrap_behavior == :word_wrap
max_reach.times do |i| word_search_end = search_end
reach = i failed = false
break if copy[line_end.floor - i].to_s.match(/[[:punct:]]| /)
until(copy[word_search_end].to_s.match(/[[:punct:]]| /))
word_search_end -= 1
if word_search_end <= 1 || word_search_end < line_start
failed = true
break
end
end end
# puts "Max width: #{max_width}/#{line_width(@raw_text)} Reach: {#{reach}/#{max_reach}} Line Start: #{line_start}/#{line_end.floor} (#{copy.length}|#{@raw_text.length}) [#{entering_line_end}] '#{copy}' {#{copy[line_start...line_end]}}" line_start = failed ? search_end : word_search_end + 1 # walk in front of punctuation
line_end = line_end.floor - reach + 1 if reach != max_reach # Add +1 to walk in front of punctuation else
line_start = search_end
end end
breaks << line_end.floor breaks << line_start
line_start = line_end.floor
line_end = copy.length
break if entering_line_end == copy.length || reach == max_reach # Prevent locking up due to outer while loop text width < max_width check not being satisfied.
end stalled = checked_copy_length == line_width(copy[line_start..line_end])
checked_copy_length = line_width(copy[line_start..line_end])
stalled_interations += 1 if stalled
stalled_interations = 0 unless stalled
end end
breaks.each_with_index do |pos, index| breaks.each_with_index do |pos, index|

View File

@@ -25,6 +25,7 @@ module CyberarmEngine
@last_mouse_pos = nil @last_mouse_pos = nil
@dragging_element = nil @dragging_element = nil
@pending_recalculate_request = false @pending_recalculate_request = false
@pending_element_recalculate_requests = []
@menu = nil @menu = nil
@min_drag_distance = 0 @min_drag_distance = 0
@@ -56,6 +57,7 @@ module CyberarmEngine
if @tip.value.length.positive? if @tip.value.length.positive?
Gosu.flush Gosu.flush
@tip.draw @tip.draw
end end
@@ -73,8 +75,11 @@ module CyberarmEngine
@root_container.recalculate @root_container.recalculate
@pending_recalculate_request = false @pending_recalculate_request = false
@pending_element_recalculate_requests.clear # GUI has already been recalculated
end end
@pending_element_recalculate_requests.each(&:recalculate)
if @pending_focus_request if @pending_focus_request
@pending_focus_request = false @pending_focus_request = false
@@ -231,6 +236,13 @@ module CyberarmEngine
@pending_recalculate_request = true @pending_recalculate_request = true
end end
def request_recalculate_for(element)
# element is already queued
return if @pending_element_recalculate_requests.detect { |e| e == element }
@pending_element_recalculate_requests << element
end
def request_focus(element) def request_focus(element)
@pending_focus_request = true @pending_focus_request = true
@pending_focus_element = element @pending_focus_element = element

View File

@@ -1,4 +1,4 @@
module CyberarmEngine module CyberarmEngine
NAME = "InDev".freeze NAME = "InDev".freeze
VERSION = "0.19.0".freeze VERSION = "0.19.1".freeze
end end