Skip to content

Commit b0cb09f

Browse files
committed
cleanup
1 parent b1bf783 commit b0cb09f

File tree

7 files changed

+43
-41
lines changed

7 files changed

+43
-41
lines changed

main/canvas.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def draw_canvas
1919

2020
def send_if_runnable(methode, args)
2121
begin
22-
send(methode.to_s, *args)
22+
send(methode, *args)
2323
rescue Exception => e
2424
puts "#{ e } (#{ e.class })!"
2525
end

main/cursor.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ class Cursor
33

44
def initialize(line)
55
@line = line
6-
@x, @y = $start_of_editor_text, $editor_top_margin
6+
@x = $app.start_of_editor_text
7+
@y = $app.editor_top_margin
78
end
89

910
def set_position(x,y) #on mouse down
@@ -13,11 +14,11 @@ def set_position(x,y) #on mouse down
1314
end
1415

1516
def get_position
16-
return @x, @y
17+
[@x, @y]
1718
end
1819

1920
def y_position
20-
return @y
21+
@y
2122
end
2223

2324
def draw_line
@@ -29,7 +30,7 @@ def draw_line
2930
end
3031

3132
def go_to_new_line #after tap enter button
32-
@y += $line_height+$line_space
33+
@y += $app.line_height+$app.line_space
3334
end
3435

3536
private

main/editor.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ def draw_text
1717
@lines.elements.each do |e|
1818
e.each_pair do |key, string_object|
1919
eval("text '#{string_object.content}',
20-
#{$width/2},
20+
#{$app.width/2},
2121
#{string_object.get_position.to_a.last}")
2222
end
2323
end
2424
end
2525

2626
def draw_explanation
2727
return eval("text '#{@description.explanation}',
28-
#{$width-@description.explanation_length-$editor_right_margin},
29-
#{@description.position+$editor_top_margin}")
28+
#{$app.width-@description.explanation_length-$app.editor_right_margin},
29+
#{@description.position+$app.editor_top_margin}")
3030

3131
end
3232
end

main/lines.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Lines
22

33
def initialize
44
@elements = []
5-
@line_height = $line_height
6-
@line_space = $line_space
5+
@line_height = $app.line_height
6+
@line_space = $app.line_space
77
@positiontable = Hash.new {|this_hash,missing_key|
88
found_key = this_hash.keys.find { |this_key|
99
this_key.class == Range && this_key.include?(missing_key) }
@@ -25,7 +25,7 @@ def position(x,y)
2525
#get the linge and extract x, y param
2626
line_range = @positiontable[y].get_position
2727
y = line_range.to_a.last
28-
return $start_of_editor_text, y
28+
[$app.start_of_editor_text, y]
2929
end
3030

3131
def new_or_next_line(cursor)

main/string_object.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def arguments
3232

3333
def update_argument(x_mouse, y_mouse)
3434
update_argument_position
35-
arg = find_argument($pressed[0])
35+
arg = find_argument($app.pressed[0])
3636
if arg
37-
new_arg_value = -$width/2+x_mouse+$editor_left_margin-length((methode+ " ").to_s)-length(@object_list[1][0..arg].join(" ").to_s)+$initial_arg_value
37+
new_arg_value = -$app.width/2+x_mouse+$app.editor_left_margin-length((methode+ " ").to_s)-length(@object_list[1][0..arg].join(" ").to_s)+$app.initial_arg_value
3838
@object_list[1][arg] = new_arg_value
3939
@content = @object_list.first + " " + @object_list[1].join(" ") + "\n" if @object_list.first
4040
end
@@ -67,7 +67,7 @@ def length(s)
6767
end
6868

6969
def update_argument_position
70-
start_position = $width/2+$editor_left_margin+length((methode).to_s)
70+
start_position = $app.width/2+$app.editor_left_margin+length((methode).to_s)
7171
arg_length_new = 0
7272
space = length(" ")
7373
@argument_position = Hash.new {|this_hash,missing_key| #set always a new hash to keep track of growing numbers

main/watch.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def initialize(lines, description, cursor)
66
end
77

88
def check_line(x, y)
9-
if x > ($width/2) + $editor_left_margin
9+
if x > ($app.width/2) + $app.editor_left_margin
1010
if @lines.positiontable(y) == :undefined
1111
@description.position = [-50] # set it outside the view field
1212
else
@@ -19,7 +19,7 @@ def check_line(x, y)
1919
def check_param(x,y)
2020
y_position = @cursor.y_position
2121
string_object = @lines.positiontable(y_position)
22-
if (string_object.position[0]).include?($pressed[1])
22+
if (string_object.position[0]).include?($app.pressed[1])
2323
string_object.update_argument(x,y) unless string_object == :undefined
2424
end
2525
end

simple_live_coding.rb

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
class RubyDraw < Processing::App
44

5-
require_relative 'main/string_object'
6-
require_relative 'main/lines'
7-
require_relative 'main/cursor'
8-
require_relative 'main/parse'
9-
require_relative 'main/description_list'
10-
require_relative 'main/editor'
11-
require_relative 'main/canvas'
12-
require_relative 'main/watch'
5+
require_relative 'main/string_object'
6+
require_relative 'main/lines'
7+
require_relative 'main/cursor'
8+
require_relative 'main/parse'
9+
require_relative 'main/description_list'
10+
require_relative 'main/editor'
11+
require_relative 'main/canvas'
12+
require_relative 'main/watch'
1313

14-
def setup
15-
$width = 700
16-
$height = 600
17-
$editor_left_margin = 10
18-
$editor_right_margin = 15
19-
$editor_top_margin = 15
20-
$start_of_editor_text = $width/2
21-
$line_height = 13
22-
$line_space = 2
14+
attr_reader :editor_left_margin, :editor_right_margin, :editor_top_margin, :start_of_editor_text, :line_height, :line_space, :pressed, :initial_arg_value
2315

16+
def setup
17+
size 700, 600 # access with $app.width
18+
@editor_left_margin = 10
19+
@editor_right_margin = 15
20+
@editor_top_margin = 15
21+
@start_of_editor_text = 350
22+
@line_height = 13
23+
@line_space = 2
24+
@pressed = false #check if mouse pressed
2425

25-
size 700, 600 # needs to be hardcoded
2626
@lines = Lines.new
2727
@cursor = Cursor.new(@lines)
2828
@parser = Parse.new(@lines, @cursor)
@@ -39,23 +39,24 @@ def draw
3939
background 55
4040
@canvas.draw_canvas
4141
fill color 304, 353, 300
42-
rect ($width/2)-10,0,($width/2)+10,height
42+
rect ($app.width/2)-10,0,($app.width/2)+10,height
4343
fill color 104, 153, 0
4444
@editor.draw_content
4545

4646

47-
text "this is a DEMO app", $width/2, $height-40
48-
text "write 'rect 20 20 200 200' and drag the values...", $width/2, $height-20
47+
text "this is a DEMO app", $app.width/2, $app.height-40
48+
text "write 'rect 20 20 200 200' and drag the values...", $app.width/2, $app.height-20
4949

5050
fill color 14, 13, 0
51-
@cursor.draw_line
51+
@cursor.draw_line #we need a line marking the text position
5252
fill color 104, 153, 0
5353

5454
if mouse_pressed?
55+
sleep(0.2) #needed because of unintented param change line selection
5556
loop
5657
@watcher.check_param(mouse_x, mouse_y)
5758
else
58-
$pressed = false
59+
@pressed = false
5960
no_loop
6061
end
6162

@@ -73,8 +74,8 @@ def mouse_moved
7374

7475

7576
def mouse_pressed
76-
$pressed = [mouse_x, mouse_y]
77-
$initial_arg_value = @watcher.get_param(mouse_x, mouse_y) #get the initial value from watcher
77+
@pressed = [mouse_x, mouse_y]
78+
@initial_arg_value = @watcher.get_param(mouse_x, mouse_y) #get the initial value from watcher
7879
@cursor.set_position(mouse_x, mouse_y)
7980
redraw
8081
end

0 commit comments

Comments
 (0)