main.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. local imgui = require("imgui")
  2. local Action = require("node.action")
  3. local Condition = require("node.condition")
  4. local function coro_test (action)
  5. for i=0,100 do
  6. action.progress = i/100
  7. coroutine.yield( )
  8. end
  9. end
  10. local function coro_test_longer (action)
  11. for i=0,400 do
  12. action.progress = i/400
  13. coroutine.yield( )
  14. end
  15. end
  16. local array = {
  17. Action(coroutine.create( coro_test )),
  18. Action(coroutine.create( coro_test )),
  19. Action(coroutine.create( coro_test )),
  20. Condition("and", {Action(coroutine.create( coro_test )), Action(coroutine.create( coro_test_longer ))}),
  21. Condition("and", {Action(), Action(coroutine.create(function() coroutine.yield() end))}),
  22. Condition("and", {Action(coroutine.create(function() coroutine.yield() end)), Action(coroutine.create(function() coroutine.yield() end))}),
  23. Condition("or", {Action(coroutine.create( coro_test )), Action(coroutine.create( coro_test_longer ))}),
  24. Condition("or", {Action(), Action(coroutine.create(function() coroutine.yield() end))}),
  25. Condition("or", {Action(coroutine.create(function() coroutine.yield() end)), Action(coroutine.create(function() coroutine.yield() end))})
  26. }
  27. for i,node in ipairs(array) do node.id = i end
  28. local selected_node = nil
  29. function not_in(v,t)
  30. for k,e in ipairs(t) do
  31. if v == e then return false end
  32. end
  33. return true
  34. end
  35. function draw_array()
  36. imgui.Begin("Array")
  37. imgui.BeginChild("", 0,0, true)
  38. imgui.Columns(2)
  39. for i,node in ipairs(array) do
  40. local current_selected_node = node:draw_debug()
  41. if current_selected_node then
  42. selected_node = current_selected_node
  43. end
  44. end
  45. imgui.NextColumn()
  46. if selected_node then
  47. imgui.Text(selected_node.name)
  48. imgui.Text(selected_node.id)
  49. imgui.Text("Is finished "..tostring(selected_node:is_finished()))
  50. end
  51. imgui.Button("OK")
  52. imgui.EndChild()
  53. imgui.End()
  54. end
  55. local iui_state = {
  56. array={cbk = draw_array, show=true}
  57. }
  58. function love.load()
  59. end
  60. function love.update(dt)
  61. imgui.NewFrame()
  62. for _,node in ipairs(array) do
  63. node:update(dt)
  64. if not node:is_finished() then break end
  65. end
  66. end
  67. function love.draw()
  68. if imgui.BeginMainMenuBar() then
  69. if imgui.BeginMenu("Test") then
  70. for k,v in pairs(iui_state) do
  71. if imgui.MenuItem(k, nil, v.show) then
  72. iui_state[k].show = not iui_state[k].show
  73. end
  74. end
  75. imgui.EndMenu()
  76. end
  77. end
  78. imgui.EndMainMenuBar()
  79. for k,v in pairs(iui_state) do
  80. if v.show then v.cbk() end
  81. end
  82. imgui.Render()
  83. end
  84. function love.quit()
  85. imgui.ShutDown()
  86. end
  87. --
  88. -- User inputs
  89. --
  90. function love.textinput(t)
  91. imgui.TextInput(t)
  92. if not imgui.GetWantCaptureKeyboard() then
  93. -- Pass event to the game
  94. end
  95. end
  96. function love.keypressed(key)
  97. imgui.KeyPressed(key)
  98. if not imgui.GetWantCaptureKeyboard() then
  99. -- Pass event to the game
  100. end
  101. end
  102. function love.keyreleased(key)
  103. imgui.KeyReleased(key)
  104. if not imgui.GetWantCaptureKeyboard() then
  105. -- Pass event to the game
  106. end
  107. end
  108. function love.mousemoved(x, y)
  109. imgui.MouseMoved(x, y)
  110. if not imgui.GetWantCaptureMouse() then
  111. -- Pass event to the game
  112. end
  113. end
  114. function love.mousepressed(x, y, button)
  115. imgui.MousePressed(button)
  116. if not imgui.GetWantCaptureMouse() then
  117. -- Pass event to the game
  118. end
  119. end
  120. function love.mousereleased(x, y, button)
  121. imgui.MouseReleased(button)
  122. if not imgui.GetWantCaptureMouse() then
  123. -- Pass event to the game
  124. end
  125. end
  126. function love.wheelmoved(x, y)
  127. imgui.WheelMoved(y)
  128. if not imgui.GetWantCaptureMouse() then
  129. -- Pass event to the game
  130. end
  131. end