lib/molecules/intuition/utilities.rb in atome-0.5.7.3.6 vs lib/molecules/intuition/utilities.rb in atome-0.5.7.3.9

- old
+ new

@@ -1,7 +1,116 @@ # frozen_string_literal: true +class Atome + + def create_new_button(button_id, position_in_menu, label, code) + essential_keys = [:inactive, :active] + buttons_style = data.select { |key, value| essential_keys.include?(key) } + menu_item = box({ id: button_id }) + menu_item.text({ data: label, id: "#{button_id}_label" }) + menu_item.code({ button_code: code }) + + inactive_style = buttons_style[:inactive] + active_style = buttons_style[:active] + if active_style + active_state_text = active_style[:text] + keys_to_exclude = [:margin, :spacing, :disposition, :text] + active_style = active_style.reject { |key, _| keys_to_exclude.include?(key) } + end + + if inactive_style + inactive_state_text = inactive_style[:text] + margin = inactive_style[:margin] + spacing = inactive_style[:spacing] + disposition = inactive_style[:disposition] + keys_to_exclude = [:margin, :spacing, :disposition, :text] + inactive_style = inactive_style.reject { |key, _| keys_to_exclude.include?(key) } + menu_item.set(inactive_style) + + if disposition == :horizontal + menu_item.left = margin[:left] + (inactive_style[:width] + spacing) * position_in_menu + menu_item.top = margin[:top] + else + menu_item.top = margin[:top] + (inactive_style[:height] + spacing) * position_in_menu + menu_item.left = margin[:left] + end + + menu_item.text.each do |text_f| + grab(text_f).set(inactive_state_text) + end + + end + + menu_item.touch(:down) do + unless @active_item == menu_item.id + menu_item.set(active_style) + menu_item.text.each do |text_f| + grab(text_f).set(active_state_text) + end + fasten.each do |item_id| + unless button_id == item_id + grab(item_id).remove({ all: :shadow }) + grab(item_id).set(inactive_style) + grab("#{item_id}_label").remove({ all: :shadow }) + grab("#{item_id}_label").set(inactive_state_text) + end + end + code.call if code + end + @active_item = menu_item.id + end + end + + def add_button(params) + params.each do |button_id, params| + label = params[:text] + code = params[:code] + # {"new_button"=>{"text"=>"button1", "code"=>#<Proc:0xafe>}} + index = fasten.length + create_new_button(button_id, index, label, code) + end + false + end + + def resize_matrix(params) + + width(params[:width]) + height(params[:height]) + current_matrix = self + real_width = current_matrix.to_px(:width) + real_height = current_matrix.to_px(:height) + spacing = current_matrix.data[:spacing] + matrix_cells = current_matrix.data[:matrix] + + total_spacing_x = spacing * (matrix_cells.collect.length ** (0.5) + 1) + total_spacing_y = spacing * (matrix_cells.collect.length ** (0.5) + 1) + + if real_width > real_height + full_size = real_width + available_width = full_size - total_spacing_x + available_height = full_size - total_spacing_y + else + full_size = real_width + available_width = full_size - total_spacing_x + available_height = full_size - total_spacing_y + end + + box_width = available_width / matrix_cells.collect.length ** (0.5) + box_height = available_height / matrix_cells.collect.length ** (0.5) + + matrix_cells.collect.each_with_index do |box_id, index| + box = grab(box_id) + box.width(box_width) + box.height(box_height) + box.left((box_width + spacing) * (index % matrix_cells.collect.length ** (0.5)) + spacing) + box.top((box_height + spacing) * (index / matrix_cells.collect.length ** (0.5)).floor + spacing) + end + + end + +end + new(molecule: :input) do |params, bloc| params[:height] ||= 15 params[:width] ||= 222 new_id = params.delete(:id) || identity_generator @@ -166,11 +275,11 @@ left: 3, top: 3, blur: 9, invert: true, red: 0, green: 0, blue: 0, alpha: 0.7 }) - range = slider.box({ id: "#{slider.id}_range", top: :auto, bottom: 0,tag: { system: true } }) + range = slider.box({ id: "#{slider.id}_range", top: :auto, bottom: 0, tag: { system: true } }) range.remove(:box_color) if range_found range.apply(slider_shadow.id,) range_found.each do |part, val| range.send(part, val) @@ -286,85 +395,13 @@ red: 0, green: 0, blue: 0, alpha: 0.6 }) slider end -new(molecule: :button) do |params, bloc| - params[:height] ||= 25 - params[:width] ||= 25 - states = params.delete(:states) || 1 - new_id = params.delete(:id) || identity_generator - back_col = params.delete(:back) - back_col ||= :grey - - default_parent = if self.instance_of?(Atome) - id - else - :view - end - attach_to = params[:attach] || default_parent - renderer_found = grab(attach_to).renderers - button = box( - { renderers: renderer_found, id: new_id, type: :shape, color: back_col, - left: 0, top: 0, data: '', attach: attach_to, - smooth: 3, overflow: :hidden,tag: { system: true } - }) - button.remove(:box_color) - button.touch(:down) do - button.tick(:button) - bloc.call((button.tick[:button] - 1) % states) - - end - - params.each do |part_f, val_f| - button.send(part_f, val_f) - end - - button -end - new({ particle: :cells }) -class Atome - def resize_matrix(params) - - width(params[:width]) - height(params[:height]) - current_matrix = self - real_width = current_matrix.to_px(:width) - real_height = current_matrix.to_px(:height) - spacing = current_matrix.data[:spacing] - matrix_cells = current_matrix.data[:matrix] - - total_spacing_x = spacing * (matrix_cells.collect.length ** (0.5) + 1) - total_spacing_y = spacing * (matrix_cells.collect.length ** (0.5) + 1) - - if real_width > real_height - full_size = real_width - available_width = full_size - total_spacing_x - available_height = full_size - total_spacing_y - else - full_size = real_width - available_width = full_size - total_spacing_x - available_height = full_size - total_spacing_y - end - - box_width = available_width / matrix_cells.collect.length ** (0.5) - box_height = available_height / matrix_cells.collect.length ** (0.5) - - matrix_cells.collect.each_with_index do |box_id, index| - box = grab(box_id) - box.width(box_width) - box.height(box_height) - box.left((box_width + spacing) * (index % matrix_cells.collect.length ** (0.5)) + spacing) - box.top((box_height + spacing) * (index / matrix_cells.collect.length ** (0.5)).floor + spacing) - end - - end -end - new(molecule: :matrix) do |params, &bloc| params ||= {} # We test if self is main if so we attach the matrix to the view parent_found = if self == self grab(:view) @@ -440,33 +477,43 @@ end main_app = box({ id: id_f, width: :auto, height: :auto, top: 0, bottom: 0, left: 0, right: 0, apply: :app_color, category: :application }) main_app.remove(:box_color) main_app.instance_variable_set('@pages', {}) - menu = params.delete(:menu) - main_app.box(menu.merge({ id: "#{id_f}_menu" })) if menu - params.each do |part_f, val_f| - main_app.send(part_f, val_f) - end + + buttons({ + id: "#{id_f}_menu", + attach: id_f, + inactive: { text: { color: :gray }, width: 66, height: 12, spacing: 3, disposition: :horizontal, + color: :orange, margin: { left: 33, top: 12 } }, + active: { text: { color: :white, shadow: {} }, color: :blue, shadow: {} }, + }) main_app end new(molecule: :page) do |params, &bloc| + allow_menu = params.delete(:menu) if params[:id] id_f = params.delete(:id) - page_name=params.delete(:name) + page_name = params.delete(:name) @pages[id_f.to_sym] = params else puts "must send an id" end - page_name= page_name || id_f - menu_f= grab("#{@id}_menu") - page_title=menu_f.text({ data: page_name }) - page_title.touch(:down) do + page_name = page_name || id_f + item_code = lambda do show(id_f) end - + unless allow_menu == false + menu_f = grab("#{@id}_menu") + menu_f.add_button({ "#{@id}_menu_item_#{page_name}" => { + text: page_name, + code: item_code + } }) + actor({ "#{@id}_menu_item_#{page_name}" => :buttons }) + menu_f.role([:button]) + end end new(molecule: :show) do |page_id, &bloc| params = @pages[page_id.to_sym] params ||= {} @@ -474,13 +521,12 @@ header = params.delete(:header) left_side_bar = params.delete(:left_side_bar) right_side_bar = params.delete(:right_side_bar) # modules = params.delete(:modules) basic_size = 30 - attached.each do |page_id_found| + fasten.each do |page_id_found| page_found = grab(page_id_found) - # puts "#{page_id_found} : #{page_found}" page_found.delete({ recursive: true }) if page_found && page_found.category.include?(:page) end color({ id: :page_color, red: 0.1, green: 0.1, blue: 0.1 }) # TODO : remove the patch below when possible id_f = if params[:id] @@ -515,11 +561,11 @@ new_left_side_bar = box({ left: 0, right: :auto, depth: -1, width: basic_size, top: 0, bottom: 0, height: :auto, category: :left_side_bar, id: "#{id_f}_left_side_bar" }) new_left_side_bar.remove(:box_color) new_left_side_bar.set(left_side_bar) end - attached.each do |item_id_found| + fasten.each do |item_id_found| item_found = grab(item_id_found) if item_found&.category&.include?(:footer) main_page.height(:auto) main_page.bottom(item_found.height) end @@ -549,6 +595,42 @@ grab("#{id_f}_header").left(basic_size) end end end main_page +end + +new(molecule: :buttons) do |params, &bloc| + role_f = params.delete(:role) + actor_f = params.delete(:actor) + params_saf = deep_copy(params) + context = params.delete(:attach) || :view + id = params.delete(:id) || identity_generator + main = grab(context).box({ id: id }) + main.role(role_f) || main.role(:buttons) + main.actor(actor_f) if actor_f + main.color({ blue: 0.5, red: 1, green: 1, alpha: 0 }) + main.data(params_saf) + default = params.delete(:inactive) || {} + main.data[:default] = default + default_text = default.delete(:text) + main.data[:default_text] = default_text + active = params.delete(:active) || {} + active_text = active.delete(:text) + inactive = {} + active.each_key do |part_f| + inactive[part_f] = default[part_f] + end + inactive_text = {} + active.each_key do |part_f| + inactive_text[part_f] = default_text[part_f] + end + + params.each_with_index do |(item_id, part_f), index| + label = part_f[:text] + code = part_f[:code] + main.create_new_button(item_id, index, label, code) + + end + main + end \ No newline at end of file