Fix inputs and add methods

This commit is contained in:
2026-05-07 13:55:02 +01:00
parent a77f75dcf4
commit 0f693a5de0
12 changed files with 500 additions and 93 deletions
+12 -38
View File
@@ -12,7 +12,7 @@ bg_animation = Animation.new [bg_image1, bg_image2], fps: 5
App.language = :en
App.localize(:en, :btn_text, "Click %{name}!")
text = ElementText.new :btn_text, default_font, 0xFFFFFF, {name: "Me"}, position: {x: 50, y: 50}, z: 1, click_through: true, alpha: 1
text = ElementText.new :btn_text, default_font, 0xFFFF00, {name: "Me"}, position: {x: 50, y: 50}, z: 1, click_through: true, alpha: 1
rect = ElementRect.new 100, 25, 0xFF0000, position: {x: 50, y: 50}, z: 0, rotation: 60, scale: { x: 1.5, y: 1 }, alpha: 0.4
@@ -28,40 +28,7 @@ rect.on_click do
text.params!
end
=begin
input handling:
App.map_key(:space, :jump) # maps the space key to a :jump action (multiple keys can be mapped to the same action)
# all events here are in english ones, (so need to find which english key corresponds to the key in the current language of the keyboard)
key can be one of [:a..:z, :0..:9, :space, :enter, :shift, :ctrl, :alt, :tab, :backspace, :escape, :up, :down, :left, :right, :middle]
this can be checked anywhere using:
App.down?(:jump) # returns true if the jump action is currently being pressed
App.pressed?(:jump) # returns true if the jump action was just pressed this frame
App.released?(:jump) # returns true if the jump action was just released this frame
for wheel, you can use:
App.wheel # returns a hash with :x and :y values representing the wheel movement since the last frame (can be used for zooming, scrolling, etc.)
mouse position can be accessed with:
App.mouse_position # returns a hash with :x and :y values representing the current mouse position in the window
For localized input, you can use:
App.start_text_input!
App.text_input # returns the text typed this frame as a string (useful for text input fields, chat, etc.)
# can be multiple characters if the user types fast, so you should append it to a string variable to get the full input
App.stop_text_input!
=end
App.map_key :mouse_left, :press
main_scene.on_update do |delta_time|
# puts "Delta time: #{delta_time}ms"
@@ -69,13 +36,20 @@ main_scene.on_update do |delta_time|
# runs at 60fps, so you should see around 16-17ms being printed
el = main_scene.elements_at(App.mouse_position)
puts App.text_input if App.text_input != ""
next unless App.pressed?(:press)
puts "#{App.pressed?(:press)}, #{App.down?(:press)}, #{App.released?(:press)}"
for elem in el
if elem == image_element
#puts "Hovering over image element"
puts "Down over image element"
elsif elem == rect
#puts "Hovering over rect element"
puts "Down over rect element"
elsif elem == text
#puts "Hovering over text element"
puts "Down over text element"
end
end