Skip to content

Instantly share code, notes, and snippets.

@meshula
Created September 10, 2020 06:43
Show Gist options
  • Select an option

  • Save meshula/6d8531af44800bdbdccf2ba5621726e3 to your computer and use it in GitHub Desktop.

Select an option

Save meshula/6d8531af44800bdbdccf2ba5621726e3 to your computer and use it in GitHub Desktop.
im4ui.pxyl
let im4_items = []
let im4_order = 0
let im4_found_index = 0
let im4_search_label = ""
let im4_cursor_x = 16
let im4_selection = 0
let im4_iteration = 0
let im4_current_font = ∅
const im4_button_kind = 0
const im4_dropdown_kind = 1
const im4_checkbox_kind = 2
let im4_unselected_color = #8
let im4_selection_color = #FF0
let im4_selection_outline_color = #440
// im4__* indicates internal API
def im4__clear_item(i):
i.a_order = 999
return iterate.CONTINUE
def im4__compare(i):
if equivalent(i.label, im4_search_label):
i.a_order = im4_order
im4_order += 1
im4_found_index = 0
return iterate.BREAK
return iterate.CONTINUE
def im4__draw(i):
if i.a_order == 999:
return
let color = #8
let box_color = #4
if im4_iteration == im4_selection:
color = im4_selection_color
box_color = im4_selection_outline_color
else:
color = im4_unselected_color
box_color = im4_selection_outline_color
let pos = xy(im4_cursor_x, 24)
let sz = xy(0, 0)
if i.kind == im4_checkbox_kind:
if i.option:
sz = draw_text({font: im4_current_font, text: "[X]", pos: pos, color: color, outline: box_color, x_align: "left", z: 999})
else:
sz = draw_text({font: im4_current_font, text: "[ ]", pos: pos, color: color, outline: box_color, x_align: "left", z: 999})
if sz == ∅:
debug_print("WHAT CHECK")
return
pos.x += sz.x + 2
let tsz = draw_text({font: im4_current_font, text: i.label, pos: pos, color: color, outline: box_color, x_align: "left", z: 999})
// ocasionally draw_text returns ∅, haven't worked out why yet
if tsz == ∅:
debug_print("WHAT ", i.label)
return
sz.x += tsz.x
sz.y += tsz.y
if i.kind == im4_dropdown_kind:
pos.x += sz.x + 2
tsz = draw_text({font: im4_current_font, text: i.options[i.option], pos: pos, color: color, outline: box_color, x_align: "left", z: 999})
sz.x += tsz.x
sz.y += tsz.y
im4_cursor_x += sz.x + 8
im4_iteration += 1
//---- public interface
def im4_begin():
im4_order = 0
im4_cursor_x = 16
iterate(im4_items, im4__clear_item)
def im4_font(font):
im4_current_font = font
def im4_button(label, action):
im4_found_index = 999
im4_search_label = label
iterate(im4_items, im4__compare)
if im4_found_index == 999:
let b = make_entity({
a_order: im4_order, // a_order because sort keys off alphanumerically lowest member
action:action,
kind:im4_button_kind,
label:label })
push(im4_items, b)
im4_order += 1
def im4_checkbox(label, checked, action):
im4_found_index = 999
im4_search_label = label
iterate(im4_items, im4__compare)
if im4_found_index == 999:
let b = make_entity({
a_order: im4_order, // a_order because sort keys off alphanumerically lowest member
action:action,
kind:im4_checkbox_kind,
option: checked,
label:label })
push(im4_items, b)
im4_order += 1
def im4_dropdown(label, options, action):
im4_found_index = 999
im4_search_label = label
iterate(im4_items, im4__compare)
if im4_found_index == 999:
let b = make_entity({
a_order: im4_order, // a_order because sort keys off alphanumerically lowest member
action:action,
kind:im4_dropdown_kind,
option:0,
options:options,
label:label })
push(im4_items, b)
im4_order += 1
def im4_end():
sort(im4_items, "a_order", false)
def im4_set_inputs(left, right, action):
if left:
im4_selection -= 1
debug_print("left", im4_selection)
if right:
im4_selection += 1
debug_print("right", im4_selection)
if im4_selection ≥ im4_order:
im4_selection = im4_order - 1
if im4_selection < 0:
im4_selection = 0
if action:
debug_print("action", im4_selection)
if im4_items[im4_selection].kind == im4_dropdown_kind:
im4_items[im4_selection].option += 1
if im4_items[im4_selection].option == size(im4_items[im4_selection].options):
im4_items[im4_selection].option = 0
im4_items[im4_selection].action(im4_items[im4_selection].option)
else:
if im4_items[im4_selection].kind == im4_checkbox_kind:
im4_items[im4_selection].option = 1 - im4_items[im4_selection].option
im4_items[im4_selection].action(im4_items[im4_selection].option)
else:
im4_items[im4_selection].action()
def im4_render():
im4_iteration = 0
iterate(im4_items, im4__draw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment