Skip to content

Commit 0878001

Browse files
committed
improved editor feeling with line insert and up/down keys
1 parent 09a34b0 commit 0878001

File tree

6 files changed

+78
-44
lines changed

6 files changed

+78
-44
lines changed

main/canvas.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ def initialize(lines)
77

88
def draw_canvas
99
elements = @lines.elements
10-
elements.each do |e|
11-
e.each_pair{|key, line|
12-
send_if_runnable(line.methode, line.arguments) if line.methode
13-
}
10+
elements.each do |str_obj|
11+
send_if_runnable(str_obj.methode, str_obj.arguments) if str_obj.methode
1412
end
1513
end
1614

main/cursor.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def go_to_new_line #after tap enter button
3333
@y += $app.line_height+$app.line_space
3434
end
3535

36+
def go_to_previous_line
37+
@y -= $app.line_height+$app.line_space
38+
end
39+
3640
private
3741
def editor_line
3842
#return true, if the is a line to edit on

main/editor.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ def draw_content
1414
end
1515

1616
def draw_text
17-
@lines.elements.each do |e|
18-
e.each_pair do |key, string_object|
19-
eval("text '#{string_object.content}',
17+
@lines.elements.each do |str_obj|
18+
eval("text '#{str_obj.content}',
2019
#{$app.width/2},
21-
#{string_object.get_position.to_a.last}")
22-
end
20+
#{str_obj.get_position.to_a.last}")
2321
end
2422
end
2523

main/lines.rb

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ def initialize
44
@elements = []
55
@line_height = $app.line_height
66
@line_space = $app.line_space
7-
@positiontable = Hash.new {|this_hash,missing_key|
8-
found_key = this_hash.keys.find { |this_key|
9-
this_key.class == Range && this_key.include?(missing_key) }
10-
found_key ? this_hash[missing_key] = this_hash[found_key] : :undefined
11-
} #source: http://www.developwithpurpose.com/ruby-hash-awesomeness-part-2/
7+
@positiontable = generate_hashtable
128
initialize_positiontable
139
end
1410

@@ -28,13 +24,17 @@ def position(x,y)
2824
[$app.start_of_editor_text, y]
2925
end
3026

31-
def new_or_next_line(cursor)
32-
if last_line_of_editor(cursor.y_position)
33-
update_positiontable_with_new_line
34-
cursor.go_to_new_line
35-
else
27+
def make_new_line(cursor)
28+
update_positiontable_with_new_line(cursor)
3629
cursor.go_to_new_line
37-
end
30+
end
31+
32+
def previous_line(cursor)
33+
cursor.go_to_previous_line unless first_line_of_editor(cursor.y_position)
34+
end
35+
36+
def next_line(cursor)
37+
cursor.go_to_new_line unless last_line_of_editor(cursor.y_position)
3838
end
3939

4040
def elements
@@ -45,39 +45,62 @@ def current_content(y)
4545
@positiontable[y].content
4646
end
4747

48-
def delete_last_key(y)
49-
@positiontable[y].delete
48+
def delete_last_key(cursor)
49+
y = cursor.y_position
50+
if @positiontable[y].content.empty?
51+
cursor.go_to_previous_line unless first_line_of_editor(y)
52+
else
53+
@positiontable[y].delete
54+
end
5055
end
5156

5257

5358
private
54-
5559
def initialize_positiontable
60+
str_obj = StringObject.new("")
61+
@elements << str_obj
5662
begin_of_line = 0
5763
line_range = [begin_of_line..@line_height + @line_space]
58-
string_object = StringObject.new("")
59-
string_object.set_position = line_range
60-
@elements << { line_range[0] => string_object }
61-
@elements.each do |e| #should wok with first
62-
@positiontable.update(e)
63-
end
64+
str_obj.set_position = line_range
65+
@positiontable.update({ line_range[0] => str_obj })
6466
end
6567

6668
def last_line_of_editor(y)
67-
@positiontable[y] == @elements.last.to_a.last.last #ugly
69+
@positiontable[y] == @elements.last
6870
end
6971

70-
def update_positiontable_with_new_line
71-
#create_line_element with position range
72-
begin_of_line = @line_height + @line_space
73-
begin_of_line *= @elements.count if @elements
74-
line_range = [begin_of_line..begin_of_line + @line_height + @line_space]
72+
def first_line_of_editor(y)
73+
@positiontable[y] == @elements.first
74+
end
75+
76+
def update_positiontable_with_new_line(cursor)
77+
@new_positiontable = generate_hashtable #make new one becouse of content generated on mouseover
7578
string_object = StringObject.new("")
76-
string_object.set_position = line_range
77-
@elements << { line_range[0] => string_object }
78-
@elements.each do |e|
79-
@positiontable.update(e)
79+
position = line_number(on_position_of(cursor))
80+
@elements.insert(position, string_object )
81+
@elements.each_with_index do |e , idx|
82+
line_range = find_position_range(idx)
83+
e.set_position = line_range
84+
@new_positiontable.update({ line_range[0] => e })
8085
end
86+
@positiontable = @new_positiontable
87+
end
88+
89+
def generate_hashtable
90+
Hash.new {|this_hash,missing_key|
91+
found_key = this_hash.keys.find { |this_key|
92+
this_key.class == Range && this_key.include?(missing_key) }
93+
found_key ? this_hash[missing_key] = this_hash[found_key] : :undefined
94+
}#source: http://www.developwithpurpose.com/ruby-hash-awesomeness-part-2/
95+
end
96+
97+
def find_position_range(idx)
98+
begin_of_line = (@line_height + @line_space) * idx
99+
line_range = [begin_of_line..begin_of_line + @line_height + @line_space]
100+
return line_range
81101
end
82102

103+
def line_number(str_obj) #begins with 0!!
104+
str_obj.position.first.last/(@line_height + @line_space)
105+
end
83106
end

main/parse.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@ def update_line(key, key_code)
1111
#do nothing because shift was pressed
1212
when 10 #after return ad a new line
1313
#check cursor for new line position
14-
@lines.new_or_next_line(@cursor)
14+
@lines.make_new_line(@cursor)
1515
when 8
16-
@lines.delete_last_key(@cursor.y_position)
16+
@lines.delete_last_key(@cursor)
17+
when 38 #up
18+
@lines.previous_line(@cursor)
19+
when 40 #down
20+
@lines.next_line(@cursor)
21+
22+
when 37 #left
23+
when 39 #right
24+
when 157 #command
25+
when 17 #ctrl
26+
when 18 #alt
27+
1728
else
1829
string_object = @lines.on_position_of(@cursor)
1930
#update line key

simple_live_coding.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def setup
3535
#frameRate 25
3636

3737
#initial drawing for testing:
38-
"rect 20 30 30 40".chars.each{ |c| @parser.update_line(c, 0) }
38+
#"rect 20 30 30 40".chars.each{ |c| @parser.update_line(c, 0) }
3939
end
4040

4141
def draw
@@ -55,7 +55,7 @@ def draw
5555
fill color 104, 153, 0
5656

5757
if mouse_pressed?
58-
sleep(0.2) #needed because of unintented param change line selection
58+
sleep(0.12) #needed because of unintented param change line selection
5959
loop
6060
@watcher.check_param(mouse_x, mouse_y)
6161
else
@@ -85,4 +85,4 @@ def mouse_pressed
8585

8686
end
8787

88-
RubyDraw.new :title => "simple_live_coding"
88+
RubyDraw.new :title => "simple_live_coding"

0 commit comments

Comments
 (0)