aboutsummaryrefslogtreecommitdiff
path: root/src/ui_implementation.odin
blob: c175fb58df8761953060b07cd767e804cc6df3c2 (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
package main

import "../lib/oui"
import "core:strings"
import "core:fmt"
import rl "vendor:raylib"


Theme :: struct {
    background: rl.Color,
    background_bar: rl.Color,
    button: rl.Color,
    button_hover: rl.Color,
    button_click: rl.Color,
    base: rl.Color,
    slider_bar: rl.Color,
    text: rl.Color,
    price_100: rl.Color,
    price_150: rl.Color,
    price_200: rl.Color,
    price_300: rl.Color,
}

theme : Theme = {
    background =        {25 , 27 , 29 , 255,},
    background_bar =    {43 , 43 , 48 , 255,},
    button =            {91 , 91 , 204, 255,},
    button_hover =      {91 , 91 , 204, 255,},
    button_click =      {91 , 91 , 204, 255,},
    base =              {60 , 60 , 60 , 255,},
    slider_bar =        {91 , 91 , 204, 255,},
    text =              {255, 255, 255, 252,},
    price_100 =         {30 , 240, 30 , 255,},
    price_150 =         {240, 200, 30 , 255,},
    price_200 =         {240, 30 , 30 , 255,},
    price_300 =         {240, 30 , 240, 255,},
}

Sizings :: struct {
    date: int,
    call: int,
    wrap: int,
    price: int,
    lunch: int,
    timeline: int,
    inter_timeline: int,
}
sizings : Sizings = {
    date =  110,
    call =  85,
    wrap =  90,
    price = 100,
    lunch = 100,
    timeline = 30,
    inter_timeline = 3,
}

DAY_HEIGHT :: 35 // Only here for legacy UI
TIMELINE_START :: 175 // Only here for legacy UI
TIMELINE_END :: -85 // Only here for legacy UI

Item :: oui.Item
Call :: oui.Call

Data_Element :: enum int {
    Panel,
    Button,
    SliderU8,
    SliderInt,
    Label,
    Text_Input,
    Timeblock,
    Timeline,
    Timeline_Block,
// ...
}

Data_Head :: struct {
    subtype: Data_Element,
}

Data_Panel :: struct {
    using _: Data_Head,
    color: rl.Color,
}

Data_Button :: struct {
    using _: Data_Head,
    text: string,
    selected: bool,
}

Data_SliderInt :: struct {
    using _: Data_Head,
    text: string,
    value: ^int,
    min: int,
    max: int,
}

Data_SliderU8 :: struct {
    using _: Data_Head,
    text: string,
    value: ^u8,
}

Data_Label :: struct {
    using _: Data_Head,
    text: string,
    font: rl.Font,
    font_size: i32,
    alignment: Text_Alignment,
}

Data_Timeline :: struct {
    using _: Data_Head,
    day: ^Workday,
}
Data_Timeline_Block :: struct {
    using _: Data_Head,
    timeblock: ^Timeblock,
}

panel :: proc(color : rl.Color = rl.RED) -> ^Item {
    item := oui.item_make(c0)

    data := oui.alloc_typed(c0, item, Data_Panel)
    data.subtype = .Panel
    data.color = color

    return item
}

panel_line :: proc(parent: ^Item, color : rl.Color, height: int = 40) -> (item: ^Item) {
    item = oui.item_make(c0)
    item.layout_cut_children = .Left
    item.layout_size.y = height

    old := parent.layout_cut_children
    parent.layout_cut_children = .Top
    oui.item_insert(parent, item)
    parent.layout_cut_children = old

    data := oui.alloc_typed(c0, item, Data_Panel)
    data.subtype = .Panel
    data.color = color

    return
}

button :: proc(text: string, width: int, selected := false) -> ^Item {
    item := oui.item_make(c0)
    item.layout_size = {width, 35}
    item.callback = button_callback
    item.id = oui.gen_id(c0, text)

    data := oui.alloc_typed(c0, item, Data_Button)
    data.subtype = .Button
    data.text = text
    data.selected = selected

    return item
}
button_callback :: proc(ctxt: ^oui.Context, item: ^Item, event: Call) -> int {
    data := cast(^Data_Button) item.handle

    #partial switch event {
        case .Cursor_Handle:
            //return int(Cursor_Type.Hand)
    }

    return -1
}

slider_int :: proc(id: string, text: string, width: int, value: ^int, min: int, max: int) -> ^Item {
    item := oui.item_make(c0)
    item.layout_size = {width, 25}
    item.id = oui.gen_id(c0, id)
    item.callback = slider_int_callback
    
    data := oui.alloc_typed(c0, item, Data_SliderInt)
    data.subtype = .SliderInt
    data.text = text
    data.value = value
    data.min = min
    data.max = max
    
    return item
}
slider_int_callback :: proc(ctxt: ^oui.Context, item: ^Item, event: Call) -> int {
    data := cast(^Data_SliderInt) item.handle
    rect := item.bounds
    
    #partial switch event {
        case .Left_Capture:
            cursor_position := clamp(oui.get_cursor(c0).x, rect.l, rect.r)
            
            data.value^ = int(f32(data.min) + (f32(data.max - data.min) * (f32(cursor_position - rect.l) / f32(rect.r - rect.l))))
    }
    
    return -1
}

slider_u8 :: proc(id: string, text: string, width: int, value: ^u8) -> ^Item {
    item := oui.item_make(c0)
    item.layout_size = {width, 25}
    item.id = oui.gen_id(c0, id)
    item.callback = slider_u8_callback
    
    data := oui.alloc_typed(c0, item, Data_SliderU8)
    data.subtype = .SliderU8
    data.text = text
    data.value = value
    
    return item
}
slider_u8_callback :: proc(ctxt: ^oui.Context, item: ^Item, event: Call) -> int {
    data := cast(^Data_SliderU8) item.handle
    rect := item.bounds
    
    #partial switch event {
        case .Left_Capture:
            cursor_position := clamp(oui.get_cursor(c0).x, rect.l, rect.r)
            
            data.value^ = u8(255*(f32(cursor_position - rect.l) / f32(rect.r - rect.l)))
    }
    
    return -1
}
color_sliders :: proc(parent: ^Item, color: ^rl.Color) {
    width :: 167
    oui.item_insert(parent, slider_u8("slider_r", fmt.tprintf("%d", color.r), width, &color.r))
    oui.item_insert(parent, slider_u8("slider_g", fmt.tprintf("%d", color.g), width, &color.g))
    oui.item_insert(parent, slider_u8("slider_b", fmt.tprintf("%d", color.b), width, &color.b))
    oui.item_insert(parent, slider_u8("slider_a", fmt.tprintf("%d", color.a), width, &color.a))
}

timeline :: proc(parent: ^Item, day: ^Workday) -> ^Item {
    item := oui.item_make(c0)
    
    data := oui.alloc_typed(c0, item, Data_Timeline)
    data.subtype = .Timeline
    data.day = day
    
    return item
}
timeline_block :: proc(timeblock: ^Timeblock) -> ^Item {
    item := oui.item_make(c0)
    item.layout = .Absolute
    
    data := oui.alloc_typed(c0, item, Data_Timeline_Block)
    data.subtype = .Timeline_Block
    data.timeblock = timeblock
    
    return item
}

Text_Alignment :: enum int {
    // Techically called justification, but text_alignment is more self-explanatory.
    Left,
    Right,
    Center,
}
label :: proc(text: string, font: rl.Font, width: int = 150, alignment: Text_Alignment = .Left) -> ^Item {
	item := oui.item_make(c0)
    item.layout_size = {width, 25}
	
    data := oui.alloc_typed(c0, item, Data_Label)
    data.subtype = .Label
	data.text = text
    data.font = font
	data.alignment = alignment
	
	return item
}
calculate_text_alignment :: proc(text: cstring, font: rl.Font, alignment: Text_Alignment, rect: oui.RectI, spacing: f32 = 0) -> (output: [2]int) {
    
    measurement := rl.MeasureTextEx(font, text, f32(font.baseSize), spacing)
    
    switch alignment {
        case .Left:
            output.x = rect.l
        case .Right:
            output.x = rect.r - int(measurement.x)
        case .Center:
            output.x = (rect.l+(rect.r-rect.l)/2) - int(measurement.x/2)
    }
    
    output.y = (rect.t+(rect.b-rect.t)/2) - int(measurement.y/2)
    
    return
}
i2f :: proc "contextless" (input: [2]int) -> rl.Vector2 {
    return { f32(input.x), f32(input.y) }
}
f2i :: proc "contextless" (input: [2]f32) -> [2]int {
    return { int(input.x), int(input.y) }
}

// recursive loop
ui_draw_children :: proc(item: ^oui.Item) {
    list := oui.children_list(c0, item)
    for kid in list {
        ui_draw(kid)
    }
}

ui_draw :: proc(item: ^oui.Item) {
    head := cast(^Data_Head) item.handle
    rect := item.bounds
    
    //fmt.println(rect, head, item)

    if head == nil {
        ui_draw_children(item)
        return
    }

    #partial switch head.subtype {
        //case .Panel_Root:
            // ... render any type of item
        case .Button:
            data := cast(^Data_Button) item.handle
            text_spacing := clamp(item.anim.hot - item.anim.active, 0, item.anim.hot)*2
    		rl.DrawRectangle(i32(rect.l), i32(rect.t), i32(rect.r-rect.l), i32(rect.b-rect.t), theme.button)
            
            text := strings.clone_to_cstring(data.text, context.temp_allocator)
            position := calculate_text_alignment(text, font, .Center, rect, text_spacing)
            
            rl.DrawTextEx(font, text, i2f(position), f32(font.baseSize), text_spacing, theme.text);
            
            //fmt.println(item.anim)
            
        case .SliderInt:
            data := cast(^Data_SliderInt) head
            rl.DrawRectangle(i32(rect.l), i32(rect.t), i32(rect.r-rect.l), i32(rect.b-rect.t), theme.base)
            rl.DrawRectangle(i32(rect.l+1), i32(rect.t+1), i32(f32(rect.r-rect.l)*(f32(data.value^)/f32(data.max))-2), i32(rect.b-rect.t-2), theme.slider_bar)
            
            text := strings.clone_to_cstring(data.text, context.temp_allocator)
            position := calculate_text_alignment(text, font, .Center, rect)
            
            rl.DrawTextEx(font, text, i2f(position), f32(font.baseSize), 0.0, theme.text);
            
        case .SliderU8:
            data := cast(^Data_SliderU8) head
            rl.DrawRectangle(i32(rect.l), i32(rect.t), i32(rect.r-rect.l), i32(rect.b-rect.t), theme.base)
            rl.DrawRectangle(i32(rect.l+1), i32(rect.t+1), i32(f32(rect.r-rect.l)*(f32(data.value^)/255)-2), i32(rect.b-rect.t-2), theme.slider_bar)
            
            text := strings.clone_to_cstring(data.text, context.temp_allocator)
            position := calculate_text_alignment(text, font, .Center, rect)
            
            rl.DrawTextEx(font, text, i2f(position), f32(font.baseSize), 0.0, theme.text);
            
        case .Panel:
        	data := cast(^Data_Panel) head
    		rl.DrawRectangle(i32(rect.l), i32(rect.t), i32(rect.r-rect.l), i32(rect.b-rect.t), data.color)
    		ui_draw_children(item)
            
    	case .Label:
            data := cast(^Data_Label) item.handle
            text := strings.clone_to_cstring(data.text, context.temp_allocator)
            position := calculate_text_alignment(text, data.font, data.alignment, rect)

            rl.DrawTextEx(data.font, text, i2f(position), f32(data.font.baseSize), 0.0, theme.text);
        
        case .Timeline:
            rl.DrawRectangle(i32(rect.l),
                             i32(rect.t),
                             i32(rect.r - rect.l),
                             i32(rect.b - rect.t),
                             theme.background_bar)
        case .Timeline_Block:
            data := cast(^Data_Timeline_Block) item.handle
            
            color := theme.price_100
            value := data.timeblock.value
            
            switch {
                case value>2.1:
                    color = theme.price_300
                case value>1.6:
                    color = theme.price_200
                case value>1.1:
                    color = theme.price_150
            }
                
            rl.DrawRectangle(i32(rect.l), i32(rect.t), i32(rect.r-rect.l), i32(rect.b-rect.t), color)
    }
}