v2 / examples / gg / arcs_and_slices.v
168 lines · 151 sloc · 4.46 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1module main
2
3import gg
4import math
5
6const win_width = 700
7const win_height = 800
8const bg_color = gg.white
9
10// A transparent color is used to aid in verifying that
11// rendering is precise on each of the arc types (e.g. no overlapping or double rendered slices)
12const colour = gg.rgba(100, 100, 0, 100)
13
14enum Selection {
15 segs = 0
16 len
17}
18
19struct App {
20mut:
21 gg &gg.Context = unsafe { nil }
22 mouse struct {
23 mut:
24 x f32
25 y f32
26 }
27
28 sel Selection
29 segs int = 8
30}
31
32fn main() {
33 mut app := &App{}
34 app.gg = gg.new_context(
35 width: win_width
36 height: win_height
37 create_window: true
38 window_title: 'Arcs and Slices'
39 user_data: app
40 bg_color: bg_color
41 frame_fn: on_frame
42 event_fn: on_event
43 )
44 app.gg.run()
45}
46
47fn on_frame(mut app App) {
48 app.gg.begin()
49
50 start := math.tau * app.mouse.y / (win_width * app.gg.scale)
51 end := math.tau * app.mouse.x / (win_width * app.gg.scale)
52
53 segs := if app.sel == .segs { '[${app.segs}]' } else { '${app.segs}' }
54 app.gg.draw_text_def(10, 10, 'Segments: ${segs}')
55 app.gg.draw_text_def(250, 10, 'Drawing Angles (radians)')
56 app.gg.draw_text_def(200, 26, 'Start: ${start}°')
57 app.gg.draw_text_def(350, 26, 'End: ${end}°')
58 mut x, mut y := 0, -80
59
60 y += 150
61 x = 20
62 app.gg.draw_text_def(10, y + 40, 'slice')
63 x += 150
64 app.gg.draw_slice_empty(x, y + 60, 50, start, end, app.segs, colour)
65 app.gg.draw_text_def(x - 50, y + 120, 'r=50 empty')
66 x += 150
67 app.gg.draw_slice_empty(x, y + 60, 0, start, end, app.segs, colour)
68 app.gg.draw_text_def(x - 50, y + 120, 'r=0 empty')
69 x += 150
70 app.gg.draw_slice_filled(x, y + 60, 50, start, end, app.segs, colour)
71 app.gg.draw_text_def(x - 50, y + 120, 'r=50 filled')
72 x += 150
73 app.gg.draw_slice_filled(x, y + 60, 0, start, end, app.segs, colour)
74 app.gg.draw_text_def(x - 50, y + 120, 'r=0 filled')
75
76 y += 150
77 x = 20
78 app.gg.draw_text_def(10, y + 40, 'arc_empty')
79 x += 150
80 app.gg.draw_arc_empty(x, y + 60, 30, 20, start, end, app.segs, colour)
81 app.gg.draw_text_def(x - 50, y + 120, 'r=[30,50]')
82 x += 150
83 app.gg.draw_arc_empty(x, y + 60, -10, 60, start, end, app.segs, colour)
84 app.gg.draw_text_def(x - 50, y + 120, 'r=[-10,50]')
85 x += 150
86 app.gg.draw_arc_empty(x, y + 60, 50, 0, start, end, app.segs, colour)
87 app.gg.draw_text_def(x - 50, y + 120, 'r=[50,50]')
88 x += 150
89 app.gg.draw_arc_empty(x, y + 60, 0, 0, start, end, app.segs, colour)
90 app.gg.draw_text_def(x - 50, y + 120, 'r=[0,0]')
91
92 y += 150
93 x = 20
94 app.gg.draw_text_def(10, y + 40, 'arc_filled')
95 x += 150
96 app.gg.draw_arc_filled(x, y + 60, 30, 20, start, end, app.segs, colour)
97 app.gg.draw_text_def(x - 50, y + 120, 'r=[30,50]')
98 x += 150
99 app.gg.draw_arc_filled(x, y + 60, -10, 60, start, end, app.segs, colour)
100 app.gg.draw_text_def(x - 50, y + 120, 'r=[-10,50]')
101 x += 150
102 app.gg.draw_arc_filled(x, y + 60, 50, 0, start, end, app.segs, colour)
103 app.gg.draw_text_def(x - 50, y + 120, 'r=[50,50]')
104 x += 150
105 app.gg.draw_arc_filled(x, y + 60, 0, 0, start, end, app.segs, colour)
106 app.gg.draw_text_def(x - 50, y + 120, 'r=[0,0]')
107
108 y += 150
109 x = 20
110 app.gg.draw_text_def(10, y + 40, 'arc_line')
111 x += 150
112 app.gg.draw_arc_line(x, y + 60, 50, start, end, app.segs, colour)
113 app.gg.draw_text_def(x - 50, y + 120, 'r=50')
114 x += 150
115 app.gg.draw_arc_line(x, y + 60, 0, start, end, app.segs, colour)
116 app.gg.draw_text_def(x - 50, y + 120, 'r=0')
117
118 y += 150
119 app.gg.draw_text_def(10, y + 20, 'Use arrow keys to increase/decrease number of segments.')
120 app.gg.draw_text_def(10, y + 36,
121 'Use the mouse to adjust the start/end angles, in radians. Mouse position (0,0) is at the top-left of the window.')
122 app.gg.draw_text_def(10, y + 52,
123 'Note: because y=0 is at the top of the screen and not the bottom, angle=0 is at the bottom of an arc, not the top!')
124 app.gg.draw_text_def(10, y + 68,
125 'Compared to a graph, where y=0 is at the bottom, arcs therefore appear y-flipped.')
126
127 app.gg.end()
128}
129
130fn on_event(e &gg.Event, mut app App) {
131 match e.typ {
132 .key_down {
133 match e.key_code {
134 .escape {
135 app.gg.quit()
136 }
137 .up {
138 app.sel = unsafe { Selection(math.max(0, int(app.sel) - 1)) }
139 }
140 .down {
141 app.sel = unsafe { Selection(math.min(int(Selection.len) - 1, int(app.sel) + 1)) }
142 }
143 .left {
144 match app.sel {
145 .segs {
146 app.segs = math.max(1, app.segs / 2)
147 }
148 else {}
149 }
150 }
151 .right {
152 match app.sel {
153 .segs {
154 app.segs = math.min(64, app.segs * 2)
155 }
156 else {}
157 }
158 }
159 else {}
160 }
161 }
162 .mouse_move {
163 app.mouse.x = e.mouse_x
164 app.mouse.y = e.mouse_y
165 }
166 else {}
167 }
168}
169