summaryrefslogtreecommitdiff
path: root/ipl/gpacks/drawtree/draw_box.icn
blob: 20b4c1aa131d0b6201f50389001adb679f2fcd23 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
$define Win_Size   1500
$define BG         "white"
$define FG         "black"
$define COLOR_LIST ["yellow", "blue", "green", "red", "orange", "brown", "gray", "purple", "pink"]

# set the default for DrawTree_Box_R
procedure drawtree_box_default(fg, bg)

   local draw_record

   draw_record                 := DrawTree_Box_R()

   draw_record.win_width       := Win_Size
   draw_record.win_height      := Win_Size - 200
   if /fg then draw_record.fg  := FG else draw_record.fg := fg
   if /bg then draw_record.bg  := BG else draw_record.bg := bg
   draw_record.color_list      := ["red", "blue", "green", "orange"]
   draw_record.num_color       := 4
   draw_record.win             := WOpen("canvas=hidden", 
                                  "size=" || draw_record.win_width || "," || draw_record.win_height,
                                  "bg=" || draw_record.bg, "fg=" || draw_record.fg,
                                  "dx=10", "dy=10")

   draw_record.box_size         := 20
   draw_record.draw_box_size    := 16
   
   set_box_shape(draw_record)

   draw_record.menu := ["background", format_box_cb, "total box size", format_box_cb, "visible box size",  format_box_cb, "snapshot", format_box_cb]

   return draw_record

end


procedure set_box_shape(draw_record)

   local  y_num, x_num, x, y

   draw_record.grid_x :=  table()
   draw_record.grid_y :=  table()
   draw_record.grid_x_coor :=  table()
   draw_record.grid_y_coor :=  table()
   
   y_num := 0
   x_num := 0
   every y := 0 to draw_record.win_height by draw_record.box_size do {
      draw_record.grid_y[y_num] := y
      draw_record.grid_y_coor[y] :=  y_num
      y_num +:= 1
   }

   every x := 0 to draw_record.win_width by draw_record.box_size do {
      draw_record.grid_x[x_num] := x
      draw_record.grid_x_coor[x] :=  x_num
      x_num +:= 1
   }

   draw_record.y_num := y_num
   draw_record.x_num := x_num

   draw_record.x_start := table()
 
   return 

end



# draw the tree in a seperated with line between each node
procedure drawtree_box(draw_record, children)

   local id, x, y

   every id := 0 to children.num_children do {
       if children.num_gen[id] == 0 then break

       x := integer(((draw_record.x_num - children.num_gen[id]) / 2) + 1)
       draw_record.x_start[id] := x
   }

   EraseArea(draw_record.win)

   every id := 0 to children.num_children do {
       y := children.all[id].generation
       x := children.all[id].gen_id + draw_record.x_start[y]
       DrawRectangle(draw_record.win, 
                     draw_record.grid_x[x],
                     draw_record.grid_y[y],
                     draw_record.draw_box_size,
                     draw_record.draw_box_size)
   }
   
end


# event handler 
procedure event_handler_box(draw_record, children, event)

   local x, y, gen, id, x_id

   if event == &lpress then {

       x := &x
       y := &y

       while /draw_record.grid_x_coor[x] do {
           x -:= 1
           if x == 0 then return fail
       }     
                                                                       
       while /draw_record.grid_y_coor[y] do {
           y -:= 1
           if y == -1 then return fail
       }     

       y    := draw_record.grid_y_coor[y]
       x    := draw_record.grid_x_coor[x]
       if /draw_record.x_start[y] then return fail
       x_id := x - draw_record.x_start[y] 

       every id := 0 to children.num_children do {      
           if y == children.all[id].generation then
             if x_id == children.all[id].gen_id then {
                 fill_boxes(draw_record, children, id, x, y)                 
                 break;
              }
       }

   }

   else if event == &mpress then {

       y := &y
       while /draw_record.grid_y_coor[y] do {
           y -:= 1
           if y == -1 then return fail
       }     
       y := draw_record.grid_y_coor[y]
       if /draw_record.x_start[y] then return fail       

       every id := 0 to children.num_children do {      
           if y == children.all[id].generation then {
              x := children.all[id].gen_id + draw_record.x_start[y]
              Fg(draw_record.win, COLOR_LIST[children.all[id].gen_id % 
                                             *COLOR_LIST + 1])
              fill_boxes(draw_record, children, id, x, y)                 
           }
       }
   
       Fg(draw_record.win, draw_record.fg)
   }

  else if event == &rpress then 
        drawtree_box(draw_record, children)

end    


procedure fill_boxes(draw_record, children, child, x, y)

   local id

    FillRectangle(draw_record.win, 
                  draw_record.grid_x[x],
                  draw_record.grid_y[y],
                  draw_record.draw_box_size,
                  draw_record.draw_box_size)

    every id := !children.all[child].children_id do {
        y := children.all[id].generation
        x := children.all[id].gen_id + draw_record.x_start[y]
        fill_boxes(draw_record, children, id, x, y)                         
   }

end