Added support for deciding text drawing method, more work on edit_box

This commit is contained in:
2020-06-16 10:12:21 -05:00
parent 5d7e2028b1
commit d392d8249d
3 changed files with 118 additions and 15 deletions

View File

@@ -5,6 +5,39 @@ module CyberarmEngine
super(*args)
@active_line = 0
@repeatable_keys = [
{
key: Gosu::KB_UP,
down: false,
repeat_delay: 50,
last_repeat: 0,
action: proc {move(:up)}
},
{
key: Gosu::KB_DOWN,
down: false,
repeat_delay: 50,
last_repeat: 0,
action: proc {move(:down)}
}
]
end
def update
super
caret_stay_left_of_last_newline
calculate_active_line
@repeatable_keys.each do |key|
if key[:down]
if Gosu.milliseconds > key[:last_repeat] + key[:repeat_delay]
key[:action].call
key[:last_repeat] = Gosu.milliseconds
end
end
end
end
def draw_caret
@@ -25,11 +58,42 @@ module CyberarmEngine
end
end
def set_position(int)
@text_input.selection_start = @text_input.caret_pos = int
end
def calculate_active_line
sub_text = @text_input.text[0...@text_input.caret_pos]
@active_line = sub_text.lines.size-1
end
def caret_stay_left_of_last_newline
@text_input.text+="\n" unless @text_input.text.end_with?("\n")
eof = @text_input.text.chomp.length
set_position(eof) if @text_input.caret_pos > eof
end
def caret_position_under_mouse(mouse_x, mouse_y)
1.upto(@text.text.length) do |i|
if mouse_x < @text.x - @offset_x + @text.textobject.text_width(@text.text[0...i])
return i - 1;
active_line = ((mouse_y - @text.y) / @text.textobject.height).round
active_line = 0 if @active_line < 0
active_line = @text.text.strip.lines.size if @active_line > @text.text.strip.lines.size
# 1.upto(@text.text.length) do |i|
# if mouse_x < @text.x - @offset_x + @text.width(@text.text[0...i])
# return i - 1
# end
# end
buffer = ""
@text.text.strip.lines.each do |line|
buffer.length.upto(line.length) do |i|
if mouse_x < @text.x - @offset_x + @text.width(@text.text[buffer.length...i])
puts "#{i}"
return i - 1
end
end
buffer += line
end
@text_input.text.length
@@ -42,6 +106,14 @@ module CyberarmEngine
def button_down(id)
super
@repeatable_keys.detect do |key|
if key[:key] == id
key[:down] = true
key[:last_repeat] = Gosu.milliseconds + key[:repeat_delay]
return true
end
end
case id
when Gosu::KB_ENTER, Gosu::KB_RETURN
caret_pos = @text_input.caret_pos
@@ -50,6 +122,33 @@ module CyberarmEngine
end
end
def button_up(id)
super
@repeatable_keys.detect do |key|
if key[:key] == id
key[:down] = false
return true
end
end
end
def move(direction)
pos = @text_input.caret_pos
line = nil
case direction
when :up
return if @active_line == 0
when :down
return if @active_line == @text_input.text.chomp.lines
text = @text_input.text.chomp.lines[0..@active_line].join("\n")
pos = text.length
end
set_position(pos)
end
def drag_update(sender, x, y, button)
@text_input.caret_pos = caret_position_under_mouse(x, y)