Files
project_misth/samples/button.rb
T

95 lines
2.9 KiB
Ruby

App.run 720, 480, "Button Test"
main_scene = Scene.new
default_font = Font.new "assets/fonts/charybdis.ttf", 24, pixel_art: true
bg_image1 = Image.new "assets/images/menu_bg.png", alpha: 0.7, pixel_art: true
bg_image2 = Image.new "assets/images/game_over_dead_bg.png", alpha: 1, pixel_art: true
bg_animation = Animation.new [bg_image1, bg_image2], fps: 5
App.language = :en
App.localize(:en, :btn_text, "Click me!")
text = ElementText.new :btn_text, default_font, position: {x: 50, y: 50}, z: 1, click_through: true
rect = ElementRect.new({x: 50, y: 50, w: 100, h: 25}, 0xFF0000FF, z: 0)
image_element = ElementImage.new bg_animation, position: {x: 0, y: 0}, z: -1, click_through: true
rect.on_click do
rect.color = rect.color == 0xFF0000FF ?
0x00FF00FF
:
0xFF0000FF
end
=begin
input handling:
on_click - exists to simplify on elements, not the standard way.
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!
####
also in a scene you can use (with click_though semantics):
main_scene.elements_at(x, y)
=end
main_scene.on_update do |delta_time|
# puts "Delta time: #{delta_time}ms"
# commented out to avoid spamming the console, but you can uncomment to see the delta time being printed every frame
# runs at 60fps, so you should see around 16-17ms being printed
el = main_scene.elements_at(App.mouse_position)
for elem in el
if elem == image_element
puts "Hovering over image element"
elsif elem == rect
puts "Hovering over rect element"
elsif elem == text
puts "Hovering over text element"
end
end
end
main_scene << text
main_scene << rect
main_scene << image_element
# main_scene.delete(text) # Uncomment to test element deletion
App.start main_scene