forked from britzl/extension-imgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.script
589 lines (496 loc) · 16.7 KB
/
example.script
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
local JEDI = { "Luke", "Obi-Wan", "Yoda" }
local SITH = { "Maul", "Vader", "Palpatine" }
local function set_style()
--[[local style = {}
style.ChildBorderSize = 1
style.PopupRounding = 0
style.PopupBorderSize = 1
style.FramePadding = vmath.vector3(4, 3, 0)
style.FrameRounding = 0
style.FrameBorderSize = 0
style.ItemSpacing = vmath.vector3(8, 4, 0)
style.ItemInnerSpacing = vmath.vector3(4, 4, 0)
style.CellPadding = vmath.vector3(4, 2, 0)
style.TouchExtraPadding = vmath.vector3(0, 0, 0)
style.IndentSpacing = 21
style.Alpha = 1
style.ScrollbarSize = 14
style.ScrollbarRounding = 9
style.GrabMinSize = 10
style.GrabRounding = 0
style.LogSliderDeadzone = 4
style.TabRounding = 4
style.TabBorderSize = 0
style.TabMinWidthForCloseButton = 0
style.ColorButtonPosition = 1
style.ButtonTextAlign = vmath.vector3(0.5, 0.5, 0)
style.SelectableTextAlign = vmath.vector3(0, 0, 0)
style.DisplayWindowPadding = vmath.vector3(19, 19, 0)
style.DisplaySafeAreaPadding = vmath.vector3(3, 3, 0)
style.MouseCursorScale = 1
style.CircleSegmentMaxError = 1.6
style.ColumnsMinSpacing = 6
style.AntiAliasedLines = true
style.AntiAliasedLinesUseTex = true
style.AntiAliasedFill = true
style.CurveTessellationTol = 1.25
style.WindowPadding = vmath.vector3(8, 8, 0)
style.WindowRounding = 0
style.WindowBorderSize = 1
style.WindowMinSize = vmath.vector3(32, 32, 0)
style.WindowTitleAlign = vmath.vector3(0, 0.5, 0)
style.WindowMenuButtonPosition = 0
style.ChildRounding = 0
imgui.set_style(style)--]]
imgui.set_style_color(imgui.ImGuiCol_Text, 0.90, 0.90, 0.90, 0.90)
imgui.set_style_color(imgui.ImGuiCol_TextDisabled, 0.60, 0.60, 0.60, 1.00)
imgui.set_style_color(imgui.ImGuiCol_WindowBg, 0.09, 0.09, 0.15, 1.00)
imgui.set_style_color(imgui.ImGuiCol_PopupBg, 0.05, 0.05, 0.10, 0.85)
imgui.set_style_color(imgui.ImGuiCol_Border, 0.70, 0.70, 0.70, 0.65)
imgui.set_style_color(imgui.ImGuiCol_BorderShadow, 0.00, 0.00, 0.00, 0.00)
imgui.set_style_color(imgui.ImGuiCol_FrameBg, 0.00, 0.00, 0.01, 1.00)
imgui.set_style_color(imgui.ImGuiCol_FrameBgHovered, 0.90, 0.80, 0.80, 0.40)
imgui.set_style_color(imgui.ImGuiCol_FrameBgActive, 0.90, 0.65, 0.65, 0.45)
imgui.set_style_color(imgui.ImGuiCol_TitleBg, 0.00, 0.00, 0.00, 0.83)
imgui.set_style_color(imgui.ImGuiCol_TitleBgCollapsed, 0.40, 0.40, 0.80, 0.20)
imgui.set_style_color(imgui.ImGuiCol_TitleBgActive, 0.00, 0.00, 0.00, 0.87)
imgui.set_style_color(imgui.ImGuiCol_MenuBarBg, 0.01, 0.01, 0.02, 0.80)
imgui.set_style_color(imgui.ImGuiCol_ScrollbarBg, 0.20, 0.25, 0.30, 0.60)
imgui.set_style_color(imgui.ImGuiCol_ScrollbarGrab, 0.55, 0.53, 0.55, 0.51)
imgui.set_style_color(imgui.ImGuiCol_ScrollbarGrabHovered, 0.56, 0.56, 0.56, 1.00)
imgui.set_style_color(imgui.ImGuiCol_ScrollbarGrabActive, 0.56, 0.56, 0.56, 0.91)
imgui.set_style_color(imgui.ImGuiCol_CheckMark, 0.90, 0.90, 0.90, 0.83)
imgui.set_style_color(imgui.ImGuiCol_SliderGrab, 0.70, 0.70, 0.70, 0.62)
imgui.set_style_color(imgui.ImGuiCol_SliderGrabActive, 0.30, 0.30, 0.30, 0.84)
imgui.set_style_color(imgui.ImGuiCol_Button, 0.48, 0.72, 0.89, 0.49)
imgui.set_style_color(imgui.ImGuiCol_ButtonHovered, 0.50, 0.69, 0.99, 0.68)
imgui.set_style_color(imgui.ImGuiCol_ButtonActive, 0.80, 0.50, 0.50, 1.00)
imgui.set_style_color(imgui.ImGuiCol_Header, 0.30, 0.69, 1.00, 0.53)
imgui.set_style_color(imgui.ImGuiCol_HeaderHovered, 0.44, 0.61, 0.86, 1.00)
imgui.set_style_color(imgui.ImGuiCol_HeaderActive, 0.38, 0.62, 0.83, 1.00)
imgui.set_style_color(imgui.ImGuiCol_ResizeGrip, 1.00, 1.00, 1.00, 0.85)
imgui.set_style_color(imgui.ImGuiCol_ResizeGripHovered, 1.00, 1.00, 1.00, 0.60)
imgui.set_style_color(imgui.ImGuiCol_ResizeGripActive, 1.00, 1.00, 1.00, 0.90)
imgui.set_style_color(imgui.ImGuiCol_PlotLines, 1.00, 1.00, 1.00, 1.00)
imgui.set_style_color(imgui.ImGuiCol_PlotLinesHovered, 0.90, 0.70, 0.00, 1.00)
imgui.set_style_color(imgui.ImGuiCol_PlotHistogram, 0.90, 0.70, 0.00, 1.00)
imgui.set_style_color(imgui.ImGuiCol_PlotHistogramHovered, 1.00, 0.60, 0.00, 1.00)
imgui.set_style_color(imgui.ImGuiCol_TextSelectedBg, 0.00, 0.00, 1.00, 0.35)
end
local function make_data(self)
self.values_line = {}
self.values_hist = {}
for i=1, 60 do
local data = math.random(1, 30)
self.values_hist[data] = (self.values_hist[data] or 0) + 1
table.insert(self.values_line, data)
end
end
function init(self)
self.counter = 0
self.radio = 1
self.show_demo_window = false
self.show_close_window = false
self.show_live_data = false
self.values_line = {}
self.values_hist = {}
make_data(self)
imgui.set_ini_filename()
set_style()
self.style = imgui.get_style()
local scale = 2
imgui.scale_all_sizes(scale)
local fontsize = 12.0 * scale
local fontsizebase = 12.0 * scale
self.fonts = {}
local regular_data, error = sys.load_resource("/example/bundle/fonts/Montserrat-Regular.ttf")
self.fonts["Regular"] = imgui.font_add_ttf_data(regular_data, #regular_data, fontsize, fontsizebase)
local bold_data, error = sys.load_resource("/example/bundle/fonts/Montserrat-Bold.ttf")
self.fonts["Bold"] = imgui.font_add_ttf_data(bold_data, #bold_data, fontsize, fontsizebase)
local italic_data, error = sys.load_resource("/example/bundle/fonts/Montserrat-Italic.ttf")
self.fonts["Italic"] = imgui.font_add_ttf_data(italic_data, #italic_data, fontsize, fontsizebase)
local bolditalic_data, error = sys.load_resource("/example/bundle/fonts/Montserrat-BoldItalic.ttf")
self.fonts["BoldItalic"] = imgui.font_add_ttf_data(bolditalic_data, #bolditalic_data, fontsize, fontsizebase)
-- Resource based Image
local img1_data, error = sys.load_resource("/example/bundle/images/image1.png")
self.image1 = imgui.image_load_data("image1", img1_data, #img1_data)
-- File based Image can be used on desktop platforms
-- self.image1 = imgui.image_load("example/images/image1.png")
local img2_data, error = sys.load_resource("/example/bundle/images/image2.png")
self.image2 = imgui.image_load_data("image2", img2_data, #img2_data)
end
local function close_window(self)
local flags = imgui.WINDOWFLAGS_NOMOVE
local full, open = imgui.begin_window("Close window", true, flags)
if full then
imgui.text("You can close this window, but cannot move")
end
imgui.end_window()
if not open then
self.show_close_window = false
end
end
local function update_tab1(self)
imgui.text("This is some useful text.")
local changed, checked = imgui.checkbox("Demo window", self.show_demo_window)
if changed then
self.show_demo_window = checked
end
imgui.separator()
if imgui.radio_button("Option 1", self.radio == 1) then
self.radio = 1
end
if imgui.radio_button("Option 2", self.radio == 2) then
self.radio = 2
end
if imgui.radio_button("Option 3", self.radio == 3) then
self.radio = 3
end
imgui.separator()
if imgui.button("Button") then
self.counter = self.counter + 1
end
imgui.same_line()
imgui.text(("counter = %d"):format(self.counter))
imgui.separator()
local pos = go.get_position("object")
local changed, x, y, z = imgui.input_float3("pos", pos.x, pos.y, pos.z)
if changed then
pos.x = x
pos.y = y
pos.z = z
go.set_position(pos, "object")
end
local changed, x, y, z = imgui.input_int4("posi", pos.x, pos.y, pos.z, 0)
if changed then
pos.x = x
pos.y = y
pos.z = z
go.set_position(pos, "object")
end
local changed, int = imgui.input_int("test int", self.int or 0)
if changed then
self.int = int
end
if imgui.button("open window with close button") then
self.show_close_window = true;
end
if imgui.begin_table("table_example", 3) then
imgui.table_setup_scroll_freeze(1, 1)
imgui.table_setup_column("FIRST NAME")
imgui.table_setup_column("LAST NAME")
imgui.table_setup_column("AGE")
imgui.table_headers_row()
imgui.table_next_row()
imgui.table_next_column()
imgui.text("Bilbo")
imgui.table_next_column()
imgui.text("Baggins")
imgui.table_next_column()
imgui.text("129")
imgui.table_next_row()
imgui.table_next_column()
imgui.text("Frodo")
imgui.table_next_column()
imgui.text("Baggins")
imgui.table_next_column()
imgui.text("51")
imgui.end_table()
end
imgui.separator()
-- Manual wrapping
imgui.text("Manual wrapping:")
local button_count = 20
local button_size = 60
local window_pos_x, window_pos_y = imgui.get_window_pos()
local region_x, region_y = imgui.get_window_content_region_max()
local window_visible_x = window_pos_x + region_x
local spacing_x = self.style.ItemSpacing.x
for n = 1, button_count do
imgui.push_id(tostring(n))
imgui.button("Box", button_size, button_size)
local last_button_x, last_button_y = imgui.get_item_rect_max()
local next_button_x = last_button_x + spacing_x + button_size
if (n < button_count and next_button_x < window_visible_x) then
imgui.same_line()
end
imgui.pop_id()
end
end
local function update_tab2(self)
local selected = imgui.tree_node("root")
if selected then
imgui.bullet()
self.foo_selected = imgui.selectable("foo", self.foo_selected)
imgui.bullet()
self.bar_selected = imgui.selectable("bar", self.bar_selected)
imgui.tree_pop()
end
-- this is the old way of creating combo boxes
-- it still exists in the ImGUI API since it's a quick way ti create a combo box
local changed, jedi = imgui.combo("Jedi##array", self.selected_jedi or 1, JEDI)
if changed then
self.selected_jedi = jedi
end
-- new way of creating combo boxes with a begin and end function
if imgui.begin_combo("Jedi##selectable", "Select a Jedi") then
for i=1,#JEDI do
if imgui.selectable(JEDI[i], i == (self.selected_jedi or 1)) then
self.selected_jedi = i
end
end
imgui.end_combo()
end
imgui.separator()
if imgui.button("Push me to focus on test double") then
imgui.set_keyboard_focus_here(0)
end
local testdbl = self.testdbl or 0
local changed, value = imgui.input_double("test double", testdbl, 0.0001, 0.01, 5)
if changed then
self.testdbl = value
end
local progress = self.progress or 0
local changed, p = imgui.input_float("progress float", progress, 0.01, 1.0, 3)
if changed then
self.progress = p
progress = p
end
imgui.separator()
imgui.draw_progress(progress, -1, 0.0)
imgui.separator()
local changed, p = imgui.slider_float("slider float", self.slider_float or 0, 0.0, 100.0)
if changed then
self.slider_float = p
end
imgui.push_id("first")
if imgui.button("Same name") then
pprint("first same button")
end
imgui.pop_id()
imgui.push_id("second")
if imgui.button("Same name") then
pprint("second same button")
end
imgui.pop_id()
end
local function update_tab3(self)
local changed, checked = imgui.checkbox("Live Data", self.show_live_data)
if changed then
self.show_live_data = checked
end
if(self.show_live_data == true) then
make_data(self)
end
imgui.separator()
imgui.text_colored(" Data Plot ", 1, 0, 0, 1 )
imgui.plot_lines( "Data", 0, 150, 40, self.values_line )
imgui.separator()
imgui.text_colored(" Data Histogram ", 0, 1, 0, 1 )
imgui.plot_histogram( "Histogram", 0, 150, 40, self.values_hist )
imgui.separator()
end
-- Example images and font styles
local function update_tab4(self)
imgui.font_push(self.fonts["Regular"])
imgui.text_colored("Example Text Regular", 1, 1, 1, 1 )
imgui.font_pop()
imgui.separator()
imgui.font_push(self.fonts["Bold"])
imgui.text_colored("Example Text Bold", 1, 1, 1, 1 )
imgui.font_pop()
imgui.separator()
imgui.font_push(self.fonts["Italic"])
imgui.text_colored("Example Text Italic", 1, 1, 1, 1 )
imgui.font_pop()
imgui.separator()
imgui.font_push(self.fonts["BoldItalic"])
imgui.text_colored("Example Text Bold Italic", 1, 1, 1, 1 )
imgui.font_pop()
imgui.separator()
imgui.font_push(self.fonts["Regular"])
imgui.text_colored("image stretched", 1, 1, 1, 1 )
imgui.image_add(self.image1, 150,20)
imgui.text_colored("image original aspect", 1, 1, 1, 1 )
imgui.image_add(self.image2, 150,150)
imgui.font_pop()
end
local function update_tab5(self)
if imgui.tree_node("Jedi vs Sith") then
imgui.text("Right click for some info")
for i=1,#JEDI do
imgui.tree_node(JEDI[i], imgui.TREENODE_LEAF + imgui.TREENODE_NO_TREE_PUSH_ON_OPEN)
if imgui.begin_dragdrop_source(imgui.DROPFLAGS_SOURCEALLOWNULLID) then
imgui.set_dragdrop_payload("jedi", JEDI[i])
imgui.end_dragdrop_source()
end
if imgui.begin_popup_context_item("jedipop" .. i) then
imgui.text("This is a Jedi")
imgui.end_popup()
end
end
for i=1,#SITH do
imgui.tree_node(SITH[i], imgui.TREENODE_LEAF + imgui.TREENODE_NO_TREE_PUSH_ON_OPEN)
if imgui.begin_dragdrop_source(imgui.DROPFLAGS_SOURCEALLOWNULLID) then
imgui.set_dragdrop_payload("sith", SITH[i])
imgui.end_dragdrop_source()
end
if imgui.begin_popup_context_item("sithpop" .. i) then
imgui.text("This is a Sith")
imgui.end_popup()
end
end
imgui.tree_pop()
end
imgui.separator()
imgui.text("Drop Jedi and Sith here")
if imgui.begin_dragdrop_target() then
if imgui.accept_dragdrop_payload("jedi") then
self.counter = self.counter + 1
elseif imgui.accept_dragdrop_payload("sith") then
self.counter = self.counter - 1
end
imgui.end_dragdrop_target()
end
imgui.text("Counter " .. self.counter)
end
-- Loosely based on "Widgets Width" part of imgui_demo
local function update_tab6(self)
if not self.tab6_f then self.tab6_f = 0 end
imgui.text("SetNextItemWidth(200)")
imgui.same_line()
imgui.set_next_item_width(200)
local changed, f = imgui.input_float("float##1", self.tab6_f)
if changed then
self.tab6_f = f
end
imgui.text("SetNextItemWidth(GetWindowWidth() * 0.5f)")
imgui.same_line()
local w,h = imgui.get_window_size()
imgui.set_next_item_width(w * 0.5)
local changed, f = imgui.input_float("float##2", self.tab6_f)
if changed then
self.tab6_f = f
end
imgui.text("SetNextItemWidth(GetContentRegionAvail().x * 0.5f)")
imgui.same_line()
local aw, ah = imgui.get_content_region_avail()
imgui.set_next_item_width(aw * 0.5)
local changed, f = imgui.input_float("float##3", self.tab6_f)
if changed then
self.tab6_f = f
end
imgui.text("SetNextItemWidth(-200)")
imgui.same_line()
imgui.set_next_item_width(-200)
local changed, f = imgui.input_float("float##4", self.tab6_f)
if changed then
self.tab6_f = f
end
end
local function main_menu_bar(self)
if imgui.begin_main_menu_bar() then
if imgui.begin_menu("File") then
if imgui.menu_item("Quit", "Ctrl+Q") then
sys.exit(0)
end
imgui.end_menu()
end
if imgui.begin_menu("View") then
local clicked = imgui.menu_item("Show Demo Window", nil, self.show_demo_window)
if clicked then
self.show_demo_window = not self.show_demo_window
end
imgui.end_menu()
end
if imgui.begin_menu("About") then
imgui.menu_item("Defold v" .. sys.get_engine_info().version, nil, nil, false)
imgui.end_menu()
end
imgui.end_main_menu_bar()
end
end
local function nested_menu(self)
if imgui.begin_menu("Nested Menu") then
imgui.menu_item("Nested Item")
nested_menu(self)
imgui.end_menu()
end
end
local function menu_bar(self)
if imgui.begin_menu_bar() then
if imgui.begin_menu("Menu") then
if imgui.menu_item("Menu Item") then
print("Clicked 'Menu Item'")
end
-- Shortcuts are only visual, ImGui doesn't currently use them
imgui.menu_item("Has Shortcut", "Ctrl+A")
local changed, active = imgui.menu_item("Toggleable Item", nil, self.toggle_item_active)
if changed then
self.toggle_item_active = active
print("'Toggleable Item' is " .. (self.toggle_item_active and "on" or "off"))
end
imgui.menu_item("Disabled Item", nil, nil, false)
if imgui.begin_menu("Nested Menu") then
imgui.menu_item("Item")
nested_menu(self)
imgui.end_menu()
end
if imgui.menu_item("Click Me!") then
sys.exit(0)
end
imgui.end_menu()
end
if imgui.begin_menu("Other Menu") then
if imgui.menu_item("Other Menu Item") then
print("Clicked 'Other Menu Item'")
end
imgui.end_menu()
end
imgui.end_menu_bar()
end
end
function update(self, dt)
main_menu_bar(self)
if self.show_demo_window then
imgui.demo()
end
if self.show_close_window then
close_window(self)
end
imgui.begin_window("Hello, world!", nil, imgui.WINDOWFLAGS_MENUBAR)
menu_bar(self)
imgui.begin_tab_bar("tabs")
local tab1_open = imgui.begin_tab_item("Tab1")
if tab1_open then
update_tab1(self)
imgui.end_tab_item()
end
local tab2_open = imgui.begin_tab_item("Tab2")
if tab2_open then
update_tab2(self)
imgui.end_tab_item()
end
local tab3_open = imgui.begin_tab_item("Tab3")
if tab3_open then
update_tab3(self)
imgui.end_tab_item()
end
local tab4_open = imgui.begin_tab_item("Tab4")
if tab4_open then
update_tab4(self)
imgui.end_tab_item()
end
local tab5_open = imgui.begin_tab_item("Tab5")
if tab5_open then
update_tab5(self)
imgui.end_tab_item()
end
local tab6_open = imgui.begin_tab_item("Tab6")
if tab6_open then
update_tab6(self)
imgui.end_tab_item()
end
imgui.end_tab_bar()
imgui.end_window()
end