# Nathan Hinton # This is the UI for the game and will contain all of the sub elements required. # There will be three main areas that we will be in charge of. First, the # bottom bar has the selector of item types. There will be a menu in the top # right as well as any store things that we need/ class UI # Setup for the UI # @param width [Integer] Width of screen in pixels # @param height [Integer] Height of the screen in pixels def initialize(parent, width, height) @parent = parent @width = width @height = height @tool_selector = [] @toolbar_width = width / 2 @toolbar_height = height / 9 @tool_info = [ ['extractor', 96], ['belt', 32], ['adder', 64], ['delete', 32], ] @tool_selector_image = Gosu.render(@toolbar_width, @toolbar_height) { Gosu.draw_rect( 0, 0, @toolbar_width, @toolbar_height, Gosu::Color.new(0xC0, 0xFF, 0xFF, 0xFF), 0) # Loop through the tools: @tool_info.each_with_index do |path, idx| Gosu::Image.new("assets/images/#{path[0]}.png").draw( idx * (@toolbar_height + 2), 2, 0, (@toolbar_height / path[1].to_f), (@toolbar_height / path[1].to_f), ) end } @x = (@width / 2) - (@toolbar_width / 2) @y = @height - @toolbar_height @mouse_down_last_update = false end def update(mouse_x, mouse_y) managed = false # Only check if we are clicking if Gosu.button_down?(Gosu::MS_LEFT) if !@mouse_down_last_update puts 'mouse clicked' @mouse_down_last_update = true # Check if we are in the right y area: if mouse_y > @y # Check if we are within the right zone mouse_x_inset = mouse_x - @x if 0 < mouse_x_inset && mouse_x_inset < @toolbar_width managed = true item_index = mouse_x_inset / (@toolbar_height + 2) if item_index < @tool_info.length @parent.current_tool = @tool_info[item_index][0] puts "Selected item!" end end end end else @mouse_down_last_update = false end return managed end def draw() @tool_selector_image.draw(@x, @y) end end