aboutsummaryrefslogtreecommitdiff
path: root/src/main.odin
blob: 698f044c35f5c3bbd57c976ad17c22e2a32cbfbb (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import "../lib/oui"

import "core:fmt"
import "core:math"
import "core:slice"
import "core:runtime"
import "core:strings"
import rl "vendor:raylib"

UBUNTU_MONO := #load("../res/UbuntuMono-Regular.ttf")

font : rl.Font
big_font : rl.Font
small_font : rl.Font

FRACT_MIN : f32 = 0.0
FRACT_MAX : f32 = 1.0

c0 : ^oui.Context

main :: proc() {

	// TODO: Replace the dynamic array of Workday-pointers with
	//       an actual array of Workdays, plus a length variable
	//       for cache reasons, and to simplify the process of
	//       adding new Workdays to the array.

	dayrate : f64 = 3500
	workdays : [dynamic]Workday
	//resize(&workdays, 3)
	the_big_sum : f64 = 0

	{ // No need to keep this array in memory forever
		c, _ := importICS("res/test.ics")

		for thing, i in c {
			fmt.println("\n\nAdding workday nr", i, "\n", toString(thing), "\n")
			append(&workdays, new_workday({00, 00, 1, 1, 1850},
										  thing.start,
										  thing.end,
										  thing.end))
			for each_block in workdays[i].blocks {
				the_big_sum += f64(each_block.value) * (dayrate/7.5) * f64(hourcount(each_block))
			}
		}
	}


	//if true do return

	/*workday0: = new_workday({10, 22, 3, 5, 2023},
	                        {00, 08, 4, 5, 2023},
	                        {00, 22, 4, 5, 2023},
	                        {30, 21, 4, 5, 2023})
	workdays[0] = &workday0

	workday1: = new_workday(workday0.wrap,
	                        {00, 08, 5, 5, 2023},
	                        {30, 15, 5, 5, 2023},
	                        {30, 16, 5, 5, 2023})
	workdays[1] = &workday1

	workday2: = new_workday(workday1.wrap,
	                        {00, 12, 6, 5, 2023},
	                        {15, 17, 6, 5, 2023},
	                        {00, 17, 6, 5, 2023})
	workdays[2] = &workday2
	*/

	slice.sort_by(workdays[:], lessWorkday)

	//call_text: cstring = "00:00"
	call_text: = make([]byte, 6)
	defer delete(call_text)
	call_text[len(call_text)-1] = 0
	//wrap_text: cstring = "00:00"
	wrap_text: = make([]byte, 6)
	defer delete(wrap_text)
	wrap_text[len(wrap_text)-1] = 0
	
	date_text: = make([]byte, len(toString(workdays[0].call))+1 )
	defer delete(date_text)
	wrap_text[len(wrap_text)-1] = 0


	total_sum: cstring = f64_to_cstring(the_big_sum)
	defer delete(total_sum)

	inc_soc: cstring = f64_to_cstring(the_big_sum*1.26)
	defer delete(inc_soc)

	text_height: f32


	using rl
	width: i32 = 700
	height: i32 = 400

	InitWindow(width, height, "satscalc")
	defer CloseWindow()

	SetTargetFPS(60)
	SetWindowState({.WINDOW_RESIZABLE})
	SetWindowMinSize(width, height)

	// Loading fonts - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	small_font = LoadFontFromMemory(".ttf", raw_data(UBUNTU_MONO), i32(len(UBUNTU_MONO)), 14, nil, 0)
	font = LoadFontFromMemory(".ttf", raw_data(UBUNTU_MONO), i32(len(UBUNTU_MONO)),       18, nil, 0)
	big_font = LoadFontFromMemory(".ttf", raw_data(UBUNTU_MONO), i32(len(UBUNTU_MONO)),   24, nil, 0)
	defer UnloadFont(big_font)
	defer UnloadFont(font)
	defer UnloadFont(small_font)
	
	// oui stuff
	
	c0 = new(oui.Context)
	defer free(c0)
	oui.context_init(c0, 2048, 2048 * 8)
	defer oui.context_destroy(c0)
	
	// Setting up the timelines

	for day in &workdays {
		beginning: Moment = {0, 0, day.call.day, day.call.month, day.call.year}
		
		fmt.println("\nNew day!")
		
		for each_block, i in day.blocks {
			if i == day.total_timeblocks do break
			day.fractions[i].start = daycount(diff(beginning, each_block.start))
			day.fractions[i].end = daycount(diff(beginning, each_block.end))
			fmt.println(day.fractions[i])
		}
	}

	for !WindowShouldClose() { // MAIN LOOP ---- MAIN LOOP ---- MAIN LOOP ---- MAIN LOOP

		free_all(context.temp_allocator)

		if IsWindowResized() {
			height = GetScreenHeight()
			width = GetScreenWidth()
			fmt.println("Resized to:", width, 'x', height)
		}
		
		
		// TODO: Find a good way to calculate the size and location
		//       of all the timeblocks in every day.
		//       
		//       I know I want them to scale with the window, and
		//       that the width of them should be normalized.
		//       If you add a day that lasts until 4 AM the morning
		//       after, all the other days should scale down to keep
		//       the vertical alignment of time accurate
		//       
		// TODO: Hovering over a timeblock should make it pulse softly
		//       clicking will put a white border around the timeblock,
		//       and display information about the block in the
		//       bottom left of the screen.
			
		mousePosition: = rl.GetMousePosition()
		oui.set_cursor(c0, int(mousePosition.x), int(mousePosition.y))
		if rl.IsMouseButtonPressed(rl.MouseButton(0))  do oui.set_button(c0, .Left, true)
		if rl.IsMouseButtonReleased(rl.MouseButton(0)) do oui.set_button(c0, .Left, false)
		
		// DRAW
		// ------------------------------------------
		BeginDrawing()
			
			ClearBackground(rl.RED)
			
when true {
			// hotloop
			oui.begin_layout(c0)
				
				
				master_container := panel()
					master_container.id = oui.push_id(c0, "big_mr_boss_man") // Make ID for master thing just because.
					                                                         // Does not need to be freed because master.
					master_container.layout = .Absolute
					master_container.sort_children = true
					master_container.layout_size = {int(GetScreenWidth()), int(GetScreenHeight())}
				
				{
				top_bar := panel_line(master_container, theme.background_bar, 30)
					top_bar.id = oui.push_id(c0, "top_bar") // Make ID for anything that will have children.
					defer oui.pop_id(c0)                    // They must be pop'ed before the next equal-in-hiarchy item
					top_bar.layout_margin = 10
					
					oui.item_insert(top_bar, label("Date", font, sizings.date, .Center))
					oui.item_insert(top_bar, label("Calltimes", font, sizings.call, .Center))
					
					top_bar.layout_cut_children = .Right
					oui.item_insert(top_bar, label("Price", font, sizings.price, .Center))
					oui.item_insert(top_bar, label("Wrap", font, sizings.wrap, .Center))
					
					top_bar.layout_cut_children = .Fill
					oui.item_insert(top_bar, label("Timeline", font, 0, .Center))
				}
				
				{
				bottom_bar := panel(theme.background_bar)
					bottom_bar.id = oui.push_id(c0, "bottom_bar")
					defer oui.pop_id(c0)
				    bottom_bar.layout_cut_children = .Left
					master_container.layout_cut_children = .Bottom
					bottom_bar.z_index = 10 // Makes this render over/after the middle section
					bottom_bar.layout_size.y = 50
					bottom_bar.layout_margin = 10 // Spacing from edges
					bottom_bar.layout_cut_gap = 5 // Spacing between children
					oui.item_insert(master_container, bottom_bar)
						
					price_reason := label("Reason for price of highlighted timeblock", font, 300,)
						bottom_bar.layout_cut_children = .Left
						oui.item_insert(bottom_bar, price_reason)
					
					totals := panel(theme.background_bar)
						totals.layout_cut_children = .Top
						totals.layout_size.x = 50
						bottom_bar.layout_cut_children = .Right
						oui.item_insert(bottom_bar, totals)
					
						pre_sos_price := label("120 000 kr", small_font, 0, .Right)
							pre_sos_price.layout_size.y = 11
							oui.item_insert(totals, pre_sos_price)
						
						post_sos_price := label("160 000 kr", big_font, 300, .Right)
							post_sos_price.layout_size.y = 27
							oui.item_insert(totals, post_sos_price)
				}
				
				{
				middle_section := panel(theme.background)
					middle_section.id = oui.push_id(c0, "middle_section")
					defer oui.pop_id(c0)
					middle_section.layout_margin = 10 // Spacing from edges
					middle_section.layout_cut_gap = sizings.inter_timeline // Spacing between children
					master_container.layout_cut_children = .Fill
					oui.item_insert(master_container, middle_section)
					
					
					// - - - - WORKDAYS - - - -
					
					FRACT_MAX = workdays[0].fractions[0].start
					FRACT_MIN = FRACT_MAX // TODO: Optimize this. It doesn't need to re-calculated every frame
					
					for day, i in &workdays {
						
						for fract, i in day.fractions {
							if fract.start < FRACT_MIN do FRACT_MIN = fract.start
							if fract.end   > FRACT_MAX do FRACT_MAX = fract.end
							if i+1 == day.total_timeblocks do break
						}
						
						line := panel_line(middle_section, theme.background)
						line.layout_cut_children = .Left
						line.layout_cut_gap = 0
						line.layout_margin = 0
						line.layout_size.y = sizings.timeline
						oui.item_insert(line, label(dayprint(day.call), font, sizings.date, .Center))
						oui.item_insert(line, label(clockprint(day.call), font, sizings.call, .Center))
						
						line.layout_cut_children = .Right
						oui.item_insert(line, label("3500 kr", font, sizings.price, .Center))
						oui.item_insert(line, label(clockprint(day.wrap), font, sizings.wrap, .Center))
						
						line.layout_cut_children = .Fill
						timeline(line, &day)
					}
					new_workday := button("+", 100)
					middle_section.layout_cut_children = .Top
					oui.item_insert(middle_section, new_workday)
					if oui.is_clicked(c0, new_workday) do fmt.println("NEW WORKDAY!")
					
					
					
					// - - - - SIZINGS EDITOR - - - -
					{
						oui.item_insert(middle_section, label("Sizings Editor", big_font, 100, .Center))
						
						// To loop over the members of a struct you need to do this goofy shit:
						info := runtime.type_info_base(type_info_of(Sizings))
						st := info.variant.(runtime.Type_Info_Struct)
						root := uintptr(&sizings)
						for offset, i in st.offsets {
							
							line := panel_line(middle_section, theme.background, 25)
								line.id = oui.push_id(c0, fmt.tprintf("sizings_line_%d", i))
								defer oui.pop_id(c0)
								
								oui.item_insert(line, label(st.names[i], font, ))
								
								// To then access the member of the struct you're looping over
								// you need to do this shit:
								//               v----------------------v
								current_value := cast(^int) (root+offset)
								
								oui.item_insert(line, slider_int(fmt.tprintf("sizings-%d", i), fmt.tprintf("%d", current_value^), 300, current_value, 0, 200))
						}
						output_button := button("output sizings", 100)
						middle_section.layout_cut_children = .Top
						oui.item_insert(middle_section, output_button)
						if oui.is_clicked(c0, output_button) do fmt.printf("%#v", sizings)
					}
					
					
					// - - - - THEME EDITOR - - - -
					{
						oui.item_insert(middle_section, label("Theme Editor", big_font, 100, .Center))
						
						// To loop over the members of a struct you need to do this goofy shit:
						info := runtime.type_info_base(type_info_of(Theme))
						st := info.variant.(runtime.Type_Info_Struct)
						root := uintptr(&theme)
						for offset, i in st.offsets {
							
							line := panel_line(middle_section, theme.background, 25)
								line.layout_cut_gap = 10
								line.id = oui.push_id(c0, fmt.tprintf("line_%d", i))
								defer oui.pop_id(c0)
								
								oui.item_insert(line, label(st.names[i], font, ))
								
								// To then access the member of the struct you're looping over
								// you need to do this shit:
								//                  v------------------------v
								color_sliders(line, cast(^Color) (root+offset))
						}
						output_theme_button := button("output theme", 100)
						middle_section.layout_cut_children = .Top
						oui.item_insert(middle_section, output_theme_button)
						if oui.is_clicked(c0, output_theme_button) do fmt.printf("%#v", theme)
					}
					
				}
				
				
			oui.end_layout(c0)
				
				ui_draw(master_container)
				
			oui.process(c0)
			
} else {
			DrawTextEx(font, "Date", {20, 8}, f32(font.baseSize), 0, RAYWHITE);
			DrawTextEx(font, "Calltime", {105, 8}, f32(font.baseSize), 0, RAYWHITE);
			DrawTextEx(font, "Wraptime", {f32(width)-83, 8}, f32(font.baseSize), 0, RAYWHITE);
			
			for day, i in workdays {
				
				// TODO: The fractions held inside Workday-s needs to come in pairs,
				//       just like moments come as pairs in timeblocks.
				//       Because that will let us store both the beginning and ending
				//       of timeblocks, which is a simple way to take care of reading
				//       out-of-bounds and will make it possible to render lunch breaks
				//       
				//      (At least, given how lunch breaks are currently implemented,
				//       as holes in the workday)

				DrawRectangle(10, DAY_HEIGHT*i32(i+1)-4, width-20, DAY_HEIGHT-1, theme.background_bar)
				for block, j in day.blocks {
					if j == day.total_timeblocks do break
					block_color: = theme.price_100
					switch {
						case block.value > 2.1:
							block_color = theme.price_300
						case block.value > 1.6:
							block_color = theme.price_200
						case block.value > 1.1:
							block_color = theme.price_150
					}
					
					DrawRectangle(TIMELINE_START+i32(math.round(day.fractions[j].start*f32(width+TIMELINE_END-TIMELINE_START))),
					              DAY_HEIGHT*i32(i+1)-4,
					              i32(math.round(f32(width+TIMELINE_END-TIMELINE_START)*(day.fractions[j].end-day.fractions[j].start)+0.5)),
					              DAY_HEIGHT-1,
					              block_color)
				}
				
				copy(call_text, clockprint(day.call))
				copy(wrap_text, clockprint(day.wrap))
				copy(date_text, toString(day.call))
				
				text_height := math.round(f32(i+1)*DAY_HEIGHT+(DAY_HEIGHT-f32(font.baseSize))*0.25)
				
				DrawTextEx(font, cstring(&date_text[0]), {20, text_height}, f32(font.baseSize), 0, RAYWHITE);
				DrawTextEx(font, cstring(&wrap_text[0]), {f32(width)-70, text_height}, f32(font.baseSize), 0, RAYWHITE);
				
				if i == len(workdays)-1 {
					DrawTextEx(big_font, "+", {20, DAY_HEIGHT*f32(i+2)}, f32(big_font.baseSize), 0, RAYWHITE)
				}
			}
			
			DrawRectangle(0, height-50, width+10, 60, theme.background_bar)
			
			DrawTextEx(small_font,	total_sum,	{f32(width)-120, f32(height)-43}, f32(small_font.baseSize), 0, RAYWHITE);
			DrawTextEx(big_font,	inc_soc,	{f32(width)-120, f32(height)-29}, f32(big_font.baseSize), 0, RAYWHITE);
}
		EndDrawing()
	}
}