imgui-bindings 0.0.1 → 0.0.2.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,334 @@
1
+ require 'ffi'
2
+ require 'raylib'
3
+ require 'opengl'
4
+
5
+ require_relative 'imgui'
6
+
7
+ module ImGui
8
+
9
+ @@g_Time = 0.0 # UInt64
10
+ @@g_BackendPlatformName = FFI::MemoryPointer.from_string("imgui_impl_raylib")
11
+
12
+ # [TODO] Support ClipboardText
13
+ # g_ClipboardTextData
14
+ # ImplRaylib_GetClipboardText
15
+ # ImplRaylib_SetClipboardText
16
+
17
+ # [INTERNAL]
18
+ def self.ImplRaylib_UpdateMousePosAndButtons()
19
+ # Update buttons
20
+ io = ImGuiIO.new(ImGui::GetIO())
21
+
22
+ # Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
23
+ if io[:WantSetMousePos]
24
+ Raylib.SetMousePosition(io[:MousePos][:x].to_i, io[:MousePos][:y].to_i)
25
+ else
26
+ io[:MousePos][:x] = -Float::MAX
27
+ io[:MousePos][:y] = -Float::MAX
28
+ end
29
+
30
+ io[:MouseDown][0] = Raylib.IsMouseButtonDown(Raylib::MOUSE_BUTTON_LEFT)
31
+ io[:MouseDown][1] = Raylib.IsMouseButtonDown(Raylib::MOUSE_BUTTON_MIDDLE)
32
+ io[:MouseDown][2] = Raylib.IsMouseButtonDown(Raylib::MOUSE_BUTTON_RIGHT)
33
+
34
+ mouse_pos = Raylib.GetMousePosition()
35
+ io[:MousePos][:x] = mouse_pos[:x]
36
+ io[:MousePos][:y] = mouse_pos[:y]
37
+ end
38
+
39
+ # [INTERNAL]
40
+ def self.ImplRaylib_UpdateMouseCursor()
41
+ io = ImGuiIO.new(ImGui::GetIO())
42
+ return if (io[:ConfigFlags] & ImGuiConfigFlags_NoMouseCursorChange)
43
+
44
+ if io[:MouseDrawCursor] || ImGui::GetMouseCursor() == ImGuiMouseCursor_None
45
+ Raylib.HideCursor() # Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
46
+ else
47
+ Raylib.ShowCursor() # Show OS mouse cursor
48
+ end
49
+ end
50
+
51
+ #
52
+ # [TODO] Support ImplRaylib_UpdateGamepads
53
+ #
54
+
55
+ def self.ImplRaylib_Shutdown()
56
+ end
57
+
58
+ def self.ImplRaylib_NewFrame()
59
+ io = ImGuiIO.new(ImGui::GetIO())
60
+
61
+ unless io[:Fonts].IsBuilt()
62
+ puts "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame()."
63
+ end
64
+
65
+ # Setup display size (every frame to accommodate for window resizing)
66
+ io[:DisplaySize][:x] = Raylib.GetScreenWidth()
67
+ io[:DisplaySize][:y] = Raylib.GetScreenHeight()
68
+
69
+ # Setup time step
70
+ current_time = Raylib.GetTime()
71
+
72
+ io[:DeltaTime] = @@g_Time > 0 ? (current_time - @@g_Time).to_f : 1.0 / 60.0
73
+ @@g_Time = current_time
74
+
75
+ ImplRaylib_UpdateMousePosAndButtons()
76
+ ImplRaylib_UpdateMouseCursor()
77
+
78
+ wheel_y = Raylib.GetMouseWheelMove()
79
+ io[:MouseWheel] += 1 if wheel_y > 0
80
+ io[:MouseWheel] -= 1 if wheel_y < 0
81
+ io[:MouseWheelH] = 0 # [TODO] Get wheel tilt from Raylib
82
+ io[:MouseWheelH] = 0 # [TODO] Get wheel tilt from Raylib
83
+
84
+ # [TODO] update gamepads
85
+
86
+ # [NOTE] rlgl does not provide glBlendFuncSeparate wrapper. So we manually set states for ImGui here.
87
+ # Ref.: https://github.com/vaiorabbit/ruby-imgui/blob/05e94e6bf1969d3abf12598fef831219dca90247/lib/imgui_impl_opengl3.rb#L227-L234
88
+ # [TODO] Hide raw OpenGL operation
89
+ GL.BlendFuncSeparate(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA, GL::ONE, GL::ONE_MINUS_SRC_ALPHA)
90
+
91
+ end
92
+
93
+ KEY_IDS = [
94
+ # Alphanumeric keys
95
+ Raylib::KEY_APOSTROPHE, # Key: '
96
+ Raylib::KEY_COMMA, # Key: ,
97
+ Raylib::KEY_MINUS, # Key: -
98
+ Raylib::KEY_PERIOD, # Key: .
99
+ Raylib::KEY_SLASH, # Key: /
100
+ Raylib::KEY_ZERO, # Key: 0
101
+ Raylib::KEY_ONE, # Key: 1
102
+ Raylib::KEY_TWO, # Key: 2
103
+ Raylib::KEY_THREE, # Key: 3
104
+ Raylib::KEY_FOUR, # Key: 4
105
+ Raylib::KEY_FIVE, # Key: 5
106
+ Raylib::KEY_SIX, # Key: 6
107
+ Raylib::KEY_SEVEN, # Key: 7
108
+ Raylib::KEY_EIGHT, # Key: 8
109
+ Raylib::KEY_NINE, # Key: 9
110
+ Raylib::KEY_SEMICOLON, # Key: ;
111
+ Raylib::KEY_EQUAL, # Key: =
112
+ Raylib::KEY_A, # Key: A | a
113
+ Raylib::KEY_B, # Key: B | b
114
+ Raylib::KEY_C, # Key: C | c
115
+ Raylib::KEY_D, # Key: D | d
116
+ Raylib::KEY_E, # Key: E | e
117
+ Raylib::KEY_F, # Key: F | f
118
+ Raylib::KEY_G, # Key: G | g
119
+ Raylib::KEY_H, # Key: H | h
120
+ Raylib::KEY_I, # Key: I | i
121
+ Raylib::KEY_J, # Key: J | j
122
+ Raylib::KEY_K, # Key: K | k
123
+ Raylib::KEY_L, # Key: L | l
124
+ Raylib::KEY_M, # Key: M | m
125
+ Raylib::KEY_N, # Key: N | n
126
+ Raylib::KEY_O, # Key: O | o
127
+ Raylib::KEY_P, # Key: P | p
128
+ Raylib::KEY_Q, # Key: Q | q
129
+ Raylib::KEY_R, # Key: R | r
130
+ Raylib::KEY_S, # Key: S | s
131
+ Raylib::KEY_T, # Key: T | t
132
+ Raylib::KEY_U, # Key: U | u
133
+ Raylib::KEY_V, # Key: V | v
134
+ Raylib::KEY_W, # Key: W | w
135
+ Raylib::KEY_X, # Key: X | x
136
+ Raylib::KEY_Y, # Key: Y | y
137
+ Raylib::KEY_Z, # Key: Z | z
138
+ Raylib::KEY_LEFT_BRACKET, # Key: [
139
+ Raylib::KEY_BACKSLASH, # Key: '\'
140
+ Raylib::KEY_RIGHT_BRACKET, # Key: ]
141
+ Raylib::KEY_GRAVE, # Key: `
142
+ # Function keys
143
+ Raylib::KEY_SPACE, # Key: Space
144
+ Raylib::KEY_ESCAPE, # Key: Esc
145
+ Raylib::KEY_ENTER, # Key: Enter
146
+ Raylib::KEY_TAB, # Key: Tab
147
+ Raylib::KEY_BACKSPACE, # Key: Backspace
148
+ Raylib::KEY_INSERT, # Key: Ins
149
+ Raylib::KEY_DELETE, # Key: Del
150
+ Raylib::KEY_RIGHT, # Key: Cursor right
151
+ Raylib::KEY_LEFT, # Key: Cursor left
152
+ Raylib::KEY_DOWN, # Key: Cursor down
153
+ Raylib::KEY_UP, # Key: Cursor up
154
+ Raylib::KEY_PAGE_UP, # Key: Page up
155
+ Raylib::KEY_PAGE_DOWN, # Key: Page down
156
+ Raylib::KEY_HOME, # Key: Home
157
+ Raylib::KEY_END, # Key: End
158
+ Raylib::KEY_CAPS_LOCK, # Key: Caps lock
159
+ Raylib::KEY_SCROLL_LOCK, # Key: Scroll down
160
+ Raylib::KEY_NUM_LOCK, # Key: Num lock
161
+ Raylib::KEY_PRINT_SCREEN, # Key: Print screen
162
+ Raylib::KEY_PAUSE, # Key: Pause
163
+ Raylib::KEY_F1, # Key: F1
164
+ Raylib::KEY_F2, # Key: F2
165
+ Raylib::KEY_F3, # Key: F3
166
+ Raylib::KEY_F4, # Key: F4
167
+ Raylib::KEY_F5, # Key: F5
168
+ Raylib::KEY_F6, # Key: F6
169
+ Raylib::KEY_F7, # Key: F7
170
+ Raylib::KEY_F8, # Key: F8
171
+ Raylib::KEY_F9, # Key: F9
172
+ Raylib::KEY_F10, # Key: F10
173
+ Raylib::KEY_F11, # Key: F11
174
+ Raylib::KEY_F12, # Key: F12
175
+ Raylib::KEY_LEFT_SHIFT, # Key: Shift left
176
+ Raylib::KEY_LEFT_CONTROL, # Key: Control left
177
+ Raylib::KEY_LEFT_ALT, # Key: Alt left
178
+ Raylib::KEY_LEFT_SUPER, # Key: Super left
179
+ Raylib::KEY_RIGHT_SHIFT, # Key: Shift right
180
+ Raylib::KEY_RIGHT_CONTROL, # Key: Control right
181
+ Raylib::KEY_RIGHT_ALT, # Key: Alt right
182
+ Raylib::KEY_RIGHT_SUPER, # Key: Super right
183
+ Raylib::KEY_KB_MENU, # Key: KB menu
184
+ # Keypad keys
185
+ Raylib::KEY_KP_0, # Key: Keypad 0
186
+ Raylib::KEY_KP_1, # Key: Keypad 1
187
+ Raylib::KEY_KP_2, # Key: Keypad 2
188
+ Raylib::KEY_KP_3, # Key: Keypad 3
189
+ Raylib::KEY_KP_4, # Key: Keypad 4
190
+ Raylib::KEY_KP_5, # Key: Keypad 5
191
+ Raylib::KEY_KP_6, # Key: Keypad 6
192
+ Raylib::KEY_KP_7, # Key: Keypad 7
193
+ Raylib::KEY_KP_8, # Key: Keypad 8
194
+ Raylib::KEY_KP_9, # Key: Keypad 9
195
+ Raylib::KEY_KP_DECIMAL, # Key: Keypad .
196
+ Raylib::KEY_KP_DIVIDE, # Key: Keypad /
197
+ Raylib::KEY_KP_MULTIPLY, # Key: Keypad *
198
+ Raylib::KEY_KP_SUBTRACT, # Key: Keypad -
199
+ Raylib::KEY_KP_ADD, # Key: Keypad +
200
+ Raylib::KEY_KP_ENTER, # Key: Keypad Enter
201
+ Raylib::KEY_KP_EQUAL, # Key: Keypad =
202
+ ]
203
+
204
+ def self.ImplRaylib_ProcessKeyboard()
205
+ io = ImGuiIO.new(ImGui::GetIO())
206
+
207
+ io[:KeyShift] = Raylib.IsKeyDown(Raylib::KEY_RIGHT_SHIFT) || Raylib.IsKeyDown(Raylib::KEY_LEFT_SHIFT)
208
+ io[:KeyCtrl] = Raylib.IsKeyDown(Raylib::KEY_RIGHT_CONTROL) || Raylib.IsKeyDown(Raylib::KEY_LEFT_CONTROL)
209
+ io[:KeyAlt] = Raylib.IsKeyDown(Raylib::KEY_RIGHT_ALT) || Raylib.IsKeyDown(Raylib::KEY_LEFT_ALT)
210
+ io[:KeySuper] = Raylib.IsKeyDown(Raylib::KEY_RIGHT_SUPER) || Raylib.IsKeyDown(Raylib::KEY_LEFT_SUPER) # [TODO] io.KeySuper = false on _WIN32
211
+
212
+ KEY_IDS.each { |key| io[:KeysDown][key] = Raylib.IsKeyDown(key) }
213
+
214
+ keyPressed = Raylib.GetKeyPressed()
215
+ io.AddInputCharacter(keyPressed) if keyPressed > 0
216
+
217
+ return true
218
+ end
219
+
220
+ def self.ImplRaylib_Init()
221
+ @@g_Time = 0
222
+
223
+ io = ImGuiIO.new(ImGui::GetIO())
224
+
225
+ io[:BackendPlatformUserData] = nil
226
+ io[:BackendPlatformName] = @@g_BackendPlatformName
227
+ io[:BackendFlags] |= ImGuiBackendFlags_HasMouseCursors # We can honor GetMouseCursor() values (optional)
228
+ io[:BackendFlags] |= ImGuiBackendFlags_HasSetMousePos # We can honor io.WantSetMousePos requests (optional, rarely used)
229
+
230
+ # Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
231
+ io[:KeyMap][ImGuiKey_Tab] = Raylib::KEY_TAB
232
+ io[:KeyMap][ImGuiKey_LeftArrow] = Raylib::KEY_LEFT
233
+ io[:KeyMap][ImGuiKey_RightArrow] = Raylib::KEY_RIGHT
234
+ io[:KeyMap][ImGuiKey_UpArrow] = Raylib::KEY_UP
235
+ io[:KeyMap][ImGuiKey_DownArrow] = Raylib::KEY_DOWN
236
+ io[:KeyMap][ImGuiKey_PageUp] = Raylib::KEY_PAGE_UP
237
+ io[:KeyMap][ImGuiKey_PageDown] = Raylib::KEY_PAGE_DOWN
238
+ io[:KeyMap][ImGuiKey_Home] = Raylib::KEY_HOME
239
+ io[:KeyMap][ImGuiKey_End] = Raylib::KEY_END
240
+ io[:KeyMap][ImGuiKey_Insert] = Raylib::KEY_INSERT
241
+ io[:KeyMap][ImGuiKey_Delete] = Raylib::KEY_DELETE
242
+ io[:KeyMap][ImGuiKey_Backspace] = Raylib::KEY_BACKSPACE
243
+ io[:KeyMap][ImGuiKey_Space] = Raylib::KEY_SPACE
244
+ io[:KeyMap][ImGuiKey_Enter] = Raylib::KEY_ENTER
245
+ io[:KeyMap][ImGuiKey_Escape] = Raylib::KEY_ESCAPE
246
+ io[:KeyMap][ImGuiKey_KeyPadEnter] = Raylib::KEY_KP_ENTER
247
+ io[:KeyMap][ImGuiKey_A] = Raylib::KEY_A
248
+ io[:KeyMap][ImGuiKey_C] = Raylib::KEY_C
249
+ io[:KeyMap][ImGuiKey_V] = Raylib::KEY_V
250
+ io[:KeyMap][ImGuiKey_X] = Raylib::KEY_X
251
+ io[:KeyMap][ImGuiKey_Y] = Raylib::KEY_Y
252
+ io[:KeyMap][ImGuiKey_Z] = Raylib::KEY_Z
253
+
254
+ # [TODO] Support ClipboardText
255
+
256
+ mouse_pos = Raylib.GetMousePosition()
257
+ io[:MousePos][:x] = mouse_pos[:x]
258
+ io[:MousePos][:y] = mouse_pos[:y]
259
+
260
+ return true
261
+ end
262
+
263
+ # [INTERNAL]
264
+ def self.set_vertex(xy, uv, color)
265
+ Raylib.rlColor4ub(color[0], color[1], color[2], color[3])
266
+ Raylib.rlTexCoord2f(uv[0], uv[1])
267
+ Raylib.rlVertex2f(xy[0], xy[1])
268
+ end
269
+
270
+ def self.ImplRaylib_RenderDrawData(draw_data_raw)
271
+ draw_data = ImDrawData.new(draw_data_raw)
272
+ Raylib.rlDisableBackfaceCulling()
273
+
274
+ clip_offset = draw_data[:DisplayPos]
275
+ draw_data[:CmdListsCount].times do |n|
276
+ cmd_list = ImDrawList.new((draw_data[:CmdLists].pointer + FFI.type_size(:pointer) * n).read_pointer)
277
+ vtx_buffer = cmd_list[:VtxBuffer][:Data] # const ImDrawVert*
278
+ idx_buffer = cmd_list[:IdxBuffer][:Data] # const ImDrawIdx*
279
+
280
+ cmd_list[:CmdBuffer][:Size].times do |cmd_i|
281
+ pcmd = ImDrawCmd.new(cmd_list[:CmdBuffer][:Data] + ImDrawCmd.size * cmd_i) # const ImDrawCmd*
282
+ if pcmd[:UserCallback] != nil
283
+ # [TODO] Handle user callback (Ref.: https://github.com/ffi/ffi/wiki/Callbacks )
284
+ else
285
+ rect_min_x = (pcmd[:ClipRect][:x] - clip_offset[:x])
286
+ rect_min_y = (pcmd[:ClipRect][:y] - clip_offset[:y])
287
+ rect_max_x = (pcmd[:ClipRect][:z] - clip_offset[:x])
288
+ rect_max_y = (pcmd[:ClipRect][:w] - clip_offset[:y])
289
+
290
+ rect_w = rect_max_x - rect_min_x
291
+ rect_h = rect_max_y - rect_min_y
292
+
293
+ Raylib.BeginScissorMode(rect_min_x, rect_min_y, rect_w, rect_h)
294
+
295
+ # Render triangles
296
+ indices = idx_buffer + FFI.type_size(:ImDrawIdx) * pcmd[:IdxOffset]
297
+ vertices = vtx_buffer + ImDrawVert.size * pcmd[:VtxOffset]
298
+ 0.step(pcmd[:ElemCount] - 3, 3) do |i|
299
+ Raylib.rlPushMatrix()
300
+ Raylib.rlBegin(Raylib::RL_TRIANGLES)
301
+ Raylib.rlSetTexture(pcmd[:TextureId].read_uint32)
302
+
303
+ index = indices.get_array_of_uint16(i * FFI::type_size(:ImDrawIdx), 3)
304
+
305
+ base_offset = ImDrawVert.size * index[0]
306
+ xy = vertices + (base_offset + ImDrawVert.offset_of(:pos))
307
+ uv = vertices + (base_offset + ImDrawVert.offset_of(:uv))
308
+ color = vertices + (base_offset + ImDrawVert.offset_of(:col))
309
+ set_vertex(xy.read_array_of_float(2), uv.read_array_of_float(2), color.read_array_of_uint8(4))
310
+
311
+ base_offset = ImDrawVert.size * index[2]
312
+ xy = vertices + (base_offset + ImDrawVert.offset_of(:pos))
313
+ uv = vertices + (base_offset + ImDrawVert.offset_of(:uv))
314
+ color = vertices + (base_offset + ImDrawVert.offset_of(:col))
315
+ set_vertex(xy.read_array_of_float(2), uv.read_array_of_float(2), color.read_array_of_uint8(4))
316
+
317
+ base_offset = ImDrawVert.size * index[1]
318
+ xy = vertices + (base_offset + ImDrawVert.offset_of(:pos))
319
+ uv = vertices + (base_offset + ImDrawVert.offset_of(:uv))
320
+ color = vertices + (base_offset + ImDrawVert.offset_of(:col))
321
+ set_vertex(xy.read_array_of_float(2), uv.read_array_of_float(2), color.read_array_of_uint8(4))
322
+
323
+ Raylib.rlSetTexture(0)
324
+ Raylib.rlEnd()
325
+ Raylib.rlPopMatrix()
326
+ end
327
+ Raylib.EndScissorMode()
328
+ end
329
+ end
330
+ end
331
+ Raylib.rlEnableBackfaceCulling()
332
+ end
333
+
334
+ end
metadata CHANGED
@@ -1,16 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imgui-bindings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaiorabbit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-31 00:00:00.000000000 Z
12
- dependencies: []
13
- description: 'Ruby bindings for Dear ImGui.
11
+ date: 2022-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: opengl-bindings2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ description: 'Ruby bindings for Dear ImGui ( https://github.com/ocornut/imgui ).
14
42
 
15
43
  '
16
44
  email:
@@ -30,6 +58,7 @@ files:
30
58
  - lib/imgui_impl_glfw.rb
31
59
  - lib/imgui_impl_opengl2.rb
32
60
  - lib/imgui_impl_opengl3.rb
61
+ - lib/imgui_impl_raylib.rb
33
62
  - lib/imgui_impl_sdl2.rb
34
63
  - lib/imgui_impl_sdlrenderer.rb
35
64
  homepage: https://github.com/vaiorabbit/ruby-imgui
@@ -47,11 +76,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
76
  version: 2.6.0
48
77
  required_rubygems_version: !ruby/object:Gem::Requirement
49
78
  requirements:
50
- - - ">="
79
+ - - ">"
51
80
  - !ruby/object:Gem::Version
52
- version: '0'
81
+ version: 1.3.1
53
82
  requirements: []
54
- rubygems_version: 3.3.3
83
+ rubygems_version: 3.3.4
55
84
  signing_key:
56
85
  specification_version: 4
57
86
  summary: Bindings for Dear ImGui