-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_1_Sketchpad_CreatingItems_Canvas.rb
More file actions
120 lines (98 loc) · 2.19 KB
/
12_1_Sketchpad_CreatingItems_Canvas.rb
File metadata and controls
120 lines (98 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# coding: utf-8
# Copyright (C) 2019 Mark D. Blackwell.
require 'tk'
require 'tkextlib/tile'
module ::Sketchpad
module Position
extend self
def set(x, y)
@x, @y = x, y
nil
end
def value
[@x, @y]
end
end
end
module ::Sketchpad
module GraphicalHelper
def f_content
$f_content_private ||= begin
f = ::Tk::Tile::Frame.new root
f.grid sticky: :wnes
end
end
def root
$root_private ||= begin
tell_tk_which_encoding_to_use
::TkRoot.new
end
end
def weights_column_and_row_default_set_up(*args)
first = 0
args.reverse_each do |e|
::TkGrid.columnconfigure e, first, weight: 1
::TkGrid. rowconfigure e, first, weight: 1
end
nil
end
private
def tell_tk_which_encoding_to_use
::Tk::Encoding.encoding = ''.encoding
nil
end
end
end
module ::Sketchpad
module GraphicalObjects
def ca_canvas
@ca_canvas_private ||= begin
c = ::TkCanvas.new f_content
c.grid sticky: :wnes
end
end
end
end
module ::Sketchpad
module Graphical
extend GraphicalHelper
extend GraphicalObjects
extend self
def main
f_content.padding '3 3 3 3'
weights_column_and_row_set_up
event_bindings_set_up
::Tk.mainloop
nil
end
private
def event_bindings_set_up
event_bindings_set_up_canvas
nil
end
def event_bindings_set_up_canvas
ca_canvas.bind '1', lambda_segment_start, '%x %y'
ca_canvas.bind 'B1-Motion', lambda_segment_append, '%x %y'
nil
end
def lambda_segment_append
@lambda_segment_append_private ||= ::Kernel.lambda do |x_end, y_end|
x_start, y_start = Position.value
::TkcLine.new ca_canvas, x_start, y_start, x_end, y_end
Position.set x_end, y_end
nil
end
end
def lambda_segment_start
@lambda_segment_start_private ||= ::Kernel.lambda do |x,y|
Position.set x, y
nil
end
end
def weights_column_and_row_set_up
weights_column_and_row_default_set_up f_content, root
nil
end
end
end
::Sketchpad::Graphical.main