OSP Code QoL [Presentations] Scrollable combobox button

Users who are viewing this thread

Currently bugged. Can be deleted as I don't want to transform code backwards for compatibility. Will be a part of new WRECK integrated macro operators.

This is a scrollable combobox button. The scrollbar will appear automatically when bottom border reached. WSE combobox has a scrollbar but the text will be scaled the same way as button. Meaning if you make button narrower the text also became narrow. The vanilla X_scale also behaves the same way. In this code this is not an issue.

The module system needs some adjustments before.

1. module_strings.py.
Add this 2 lines to the beginning of strings. It will generate 128 string entities with content that will be equal to registers.
Python:
strings = [
("register_s%d" % i, "{!}{s%d}" % i) for i in range(128)] + [ # Compatibility between string registers and strings entities
("empty_string", " "),

2. module_meshes.py.
Add new meshes. If you use wreck integrated just uncomment them in plugin_presentations.py.
Python:
meshes += [
("ui_btn_drop_u", 0, "button_drop",         0, 0, 0, 0, 0, 0, 1, 1, 1), # (-0.017586,0.236414),(0,0.073),(0,0)
("ui_btn_drop_d", 0, "button_drop_clicked", 0, 0, 0, 0, 0, 0, 1, 1, 1),
("ui_btn_drop_h", 0, "button_drop_hl",      0, 0, 0, 0, 0, 0, 1, 1, 1),
("ui_btn_dropchild_u", 0, "button_drop_child",         0, 0, 0, 0, 0, 0, 1, 1, 1), # (-0.004735,0.221721),(0,0.058312),(0,0)
("ui_btn_dropchild_d", 0, "button_drop_child_clicked", 0, 0, 0, 0, 0, 0, 1, 1, 1),
("ui_btn_dropchild_h", 0, "button_drop_child_hl",      0, 0, 0, 0, 0, 0, 1, 1, 1),
]

Now we add the main code. In this code I use global variables, but you can store them in troop slots or anywhere.
Python:
# <"$custom_combobox_button"> - button_overlay_id
# <"$custom_combobox_button_items_num"> - num items
# <"$custom_combobox_button_str_script"> - script that stores strings to registers starting from s0
# <"$custom_combobox_button_value> - current item (value)

Python:
scripts += [
("store_division_names", [
(try_for_range, ":division", 0, 9),
   (assign, ":str_register", ":division"), # str registers are just a numbers
   (str_store_class_name, ":str_register", ":division"),
(try_end),
]),

# Output: reg1 - overlay ID
("create_combobox_parent", [
(store_script_param, ":x", 1),
(store_script_param, ":y", 2),
(store_script_param, ":scale_x", 3),
(store_script_param, ":scale_y", 4),
(val_div, ":scale_y", 2),
#(ui_create_mesh, ":mesh_combobox_down", "mesh_ui_btn_drop_d", l.x, l.y, l.scale_x, l.scale_y), # Macro operator
(create_mesh_overlay, ":mesh_combobox_down", "mesh_ui_btn_drop_d"),
(position_set_x, pos127, ":x"),
(position_set_y, pos127, ":y"),
(overlay_set_position, ":mesh_combobox_down", pos127),
(position_set_x, pos127, ":scale_x"),
(position_set_y, pos127, ":scale_y"),
(overlay_set_size, ":mesh_combobox_down", pos127),
#(ui_create_image_button, ":button_combobox", "mesh_ui_btn_drop_h", "mesh_ui_btn_drop_d", l.x, l.y, l.scale_x, l.scale_y), # Macro operator
(create_image_button_overlay, ":button_combobox", "mesh_ui_btn_drop_h", "mesh_ui_btn_drop_d"),
(position_set_x, pos127, ":x"),
(position_set_y, pos127, ":y"),
(overlay_set_position, ":button_combobox", pos127),
(position_set_x, pos127, ":scale_x"),
(position_set_y, pos127, ":scale_y"),
(overlay_set_size, ":button_combobox", pos127),
#(ui_create_mesh, ":mesh_combobox_up", "mesh_ui_btn_drop_u", l.x, l.y, l.scale_x, l.scale_y), # Macro operator
(create_mesh_overlay, ":mesh_combobox_up", "mesh_ui_btn_drop_u"),
(position_set_x, pos127, ":x"),
(position_set_y, pos127, ":y"),
(overlay_set_position, ":mesh_combobox_up", pos127),
(position_set_x, pos127, ":scale_x"),
(position_set_y, pos127, ":scale_y"),
(overlay_set_size, ":mesh_combobox_up", pos127),
(assign, reg1, ":button_combobox"),
]),

# Output: reg1 - overlay ID
("create_combobox_child", [
(store_script_param, ":x", 1),
(store_script_param, ":y", 2),
(store_script_param, ":scale_x", 3),
(store_script_param, ":scale_y", 4),
(val_div, ":scale_y", 2),
#(ui_create_image_button, ":button_child_combobox", "mesh_ui_btn_dropchild_h", "mesh_ui_btn_dropchild_d", l.x, l.y, l.scale_x, l.scale_y), # Macro operator
(create_image_button_overlay, ":button_child_combobox", "mesh_ui_btn_dropchild_h", "mesh_ui_btn_dropchild_d"),
(position_set_x, pos127, ":x"),
(position_set_y, pos127, ":y"),
(overlay_set_position, ":button_child_combobox", pos127),
(position_set_x, pos127, ":scale_x"),
(position_set_y, pos127, ":scale_y"),
(overlay_set_size, ":button_child_combobox", pos127),
#(ui_create_mesh, ":mesh_combobox_up", "mesh_ui_btn_dropchild_u", l.x, l.y, l.scale_x, l.scale_y), # Macro operator
(create_mesh_overlay, ":mesh_combobox_up", "mesh_ui_btn_dropchild_u"),
(position_set_x, pos127, ":x"),
(position_set_y, pos127, ":y"),
(overlay_set_position, ":mesh_combobox_up", pos127),
(position_set_x, pos127, ":scale_x"),
(position_set_y, pos127, ":scale_y"),
(overlay_set_size, ":mesh_combobox_up", pos127),
(assign, reg1, ":button_child_combobox"),
]),

# Output: reg1 - overlay ID
("create_combobox", [
(store_script_param, ":x", 1),
(store_script_param, ":y", 2),
(val_sub, ":y", 1),
(store_script_param, ":scale_x", 3),
(store_script_param, ":scale_y", 4),
(store_script_param, "$custom_combobox_button_str_script", 5),
(store_script_param, ":num_items", 6),
(call_script, "$custom_combobox_button_str_script"),
(assign, "$custom_combobox_button_items_num", ":num_items"),
(call_script, "script_create_combobox_parent", ":x", ":y", ":scale_x", ":scale_y"),
(assign, ":button", reg1),
(store_mul, ":item_y", 30, ":scale_y"),
(val_div, ":item_y", 1000),
(store_mul, ":container_y", ":item_y", ":num_items"),
(try_begin),
   (gt, ":container_y", ":y"),
   (store_div, ":num_items_fit", ":y", ":item_y"),
   (store_mul, ":container_y", ":num_items_fit", ":item_y"),
(try_end),
(store_mul, ":container_x", 230, ":scale_x"),
(val_div, ":container_x", 1000),
(store_div, ":label_x", ":container_x", 2),
(val_add, ":label_x", ":x"),
(store_div, ":shift_y", ":item_y", 7),
(store_add, ":label_y", ":y", ":shift_y"),
#(ui_create_label, reg1, s0, ":label_x", ":label_y", tf_center_justify, ":scale_y"),
(create_text_overlay, reg1, s0, tf_center_justify),
(position_set_x, pos127, ":label_x"),
(position_set_y, pos127, ":label_y"),
(overlay_set_position, reg1, pos127),
(position_set_x, pos127, ":scale_y"),
(position_set_y, pos127, ":scale_y"),
(overlay_set_size, reg1, pos127),
(store_mul, ":offset_x", 10, ":scale_x"),
(val_div, ":offset_x", 1000),
(val_sub, ":x", ":offset_x"),
(val_sub, ":y", ":container_y"),
(store_div, ":child_x", ":offset_x", 2),
(assign, ":child_y", 0),
#(ui_create_container, ":container", l.x, l.y, ":container_x", ":container_y"), # Macro operator
(create_text_overlay, ":container", "str_empty_string", tf_scrollable),
(position_set_x, pos127, ":x"),
(position_set_y, pos127, ":y"),
(overlay_set_position, ":container", pos127),
(position_set_x, pos127, ":container_x"),
(position_set_y, pos127, ":container_y"),
(overlay_set_area_size, ":container", pos127),
(assign, "$custom_combobox_container_size_x", ":container_x"),
(assign, "$custom_combobox_container_size_y", ":container_y"),
(set_container_overlay, ":container"),
(store_div, ":half_container_x", ":container_x", 2),
(try_for_range_backwards, ":str_register_no", 0, ":num_items"),
   (call_script, "script_create_combobox_child", ":child_x", ":child_y", ":scale_x", ":scale_y"),
   (store_add, ":label_x", ":child_x", ":half_container_x"),
   (store_add, ":label_y", ":child_y", ":shift_y"),
   #(ui_create_label, reg1, ":str_register_no", ":label_x", ":label_y", tf_center_justify, ":scale_y"),
   (create_text_overlay, reg1, ":str_register_no", tf_center_justify),
   (position_set_x, pos127, ":label_x"),
   (position_set_y, pos127, ":label_y"),
   (overlay_set_position, reg1, pos127),
   (position_set_x, pos127, ":scale_y"),
   (position_set_y, pos127, ":scale_y"),
   (overlay_set_size, reg1, pos127),
   (val_add, ":child_y", ":item_y"),
(try_end),
(set_container_overlay, -1),
(overlay_get_position, pos1, ":container"),
(position_get_y, ":container_y", pos1),
(val_sub, ":container_y", 750),
(position_set_y, pos1, ":container_y"),
(overlay_set_position, ":container", pos1),
(assign, reg1, ":button"),
]),
]

Before you can use script to create combobox you need to store items text to string registers. Example is in 'script_store_division_names'. Starting register should be always s0. The end register is determined by items number.

Now add code to your presentation
Python:
(set_fixed_point_multiplier, 1000),
(call_script, "script_create_combobox", 500, 200, 500, 1000, "script_store_division_names", 9),
(assign, "$custom_combobox_button", reg1),

This will create a combobox at position (500, 200) with size (500, 1000). Total 9 items will be inserted and the script that stores items text is "script_store_division_names".

Python:
(store_trigger_param, ":object", 1),
(store_trigger_param, ":value", 2),
(set_fixed_point_multiplier, 1000),
(store_add, ":mesh_u", "$custom_combobox_button", 1),
(try_begin),
  (eq, ":object", ":mesh_u"),
  (eq, ":value", 0),
  (overlay_set_display, ":mesh_u", 0),
(else_try),
  (eq, ":object", "$custom_combobox_button"),
  (eq, ":value", 1),
  (overlay_set_display, ":mesh_u", 1),
(try_end),
(try_for_range, ":item_no", 0, "$custom_combobox_button_items_num"),
  (val_mul, ":item_no", 3),
  (store_add, ":offset", "$custom_combobox_button", 4),
  (store_add, ":cur_item_button", ":offset", ":item_no"),
  (store_add, ":mesh_child_u", ":cur_item_button", 1),
  (store_add, ":label_child_u", ":cur_item_button", 2),
  (try_begin),
    (eq, ":object", ":label_child_u"),
    (eq, ":value", 0),
    (overlay_set_display, ":mesh_child_u", 0),
  (else_try),
    (eq, ":object", ":label_child_u"),
    (eq, ":value", 1),
    (overlay_set_display, ":mesh_child_u", 1),
  (try_end),
(try_end),

Python:
      (set_fixed_point_multiplier, 1000),
      (mouse_get_position, pos10),

      (store_add, ":container", "$custom_combobox_button", 3),
      (overlay_get_position, pos1, ":container"),
      (position_get_y, ":container_y", pos1),
      (try_begin),
        (key_clicked, key_left_mouse_button),
        (gt, ":container_y", 0),
        (position_get_x, ":mouse_x", pos10),
        (position_get_y, ":mouse_y", pos10),
        (position_get_x, ":container_x", pos1),
        (store_add, ":upper_bound_x", ":container_x", "$custom_combobox_container_size_x"),
        (store_add, ":upper_bound_y", ":container_y", "$custom_combobox_container_size_y"),
        (this_or_next|negate|is_between, ":mouse_x", ":container_x", ":upper_bound_x"),
        (negate|is_between, ":mouse_y", ":container_y", ":upper_bound_y"),
        (val_sub, ":container_y", 750),
        (position_set_y, pos1, ":container_y"),
        (overlay_set_position, ":container", pos1),
        (store_add, ":mesh_parent_u", "$custom_combobox_button", 1),
        (overlay_set_display, "$custom_combobox_button", 1),
        (overlay_set_display, ":mesh_parent_u", 1),
      (try_end),

Add this code before checking object and value of your custom combobox. In this code we replace local variables ":eek:bject" and ":value" of trigger params. That can be used later like a normal combobox check.

Python:
      (set_fixed_point_multiplier, 1000),
      (store_trigger_param, ":object", 1),
      (store_trigger_param, ":value", 2),
      (try_begin),
        (eq, ":object", "$custom_combobox_button"),
        (store_add, ":container", "$custom_combobox_button", 3),
        (overlay_get_position, pos1, ":container"),
        (position_get_y, ":container_y", pos1),
        (val_add, ":container_y", 750),
        (position_set_y, pos1, ":container_y"),
        (overlay_set_position, ":container", pos1),
        (overlay_set_display, "$custom_combobox_button", 0),
      (else_try),
        (try_for_range, ":item_no", 0, "$custom_combobox_button_items_num"),
          (store_mul, ":offset", ":item_no", 3),
          (val_add, ":offset", "$custom_combobox_button"),
          (store_add, ":cur_item_button", ":offset", 4),
          (eq, ":object", ":cur_item_button"),
          (assign, ":object", "$custom_combobox_button"),
          (assign, ":value", ":item_no"),
          (assign, "$custom_combobox_button_value", ":value"),
          (call_script, "$custom_combobox_button_str_script"),
          (store_add, ":label_button", "$custom_combobox_button", 2),
          (store_sub, ":maximum_value", "$custom_combobox_button_items_num", 1),
          (store_sub, ":item_text", ":maximum_value", ":value"),
          (overlay_set_text, ":label_button", ":item_text"),
        (try_end),
        (overlay_set_display, "$custom_combobox_button", 1),
        (store_add, ":container", "$custom_combobox_button", 3),
        (overlay_get_position, pos1, ":container"),
        (position_get_y, ":container_y", pos1),
        (try_begin),
          (gt, ":container_y", 0),
          (val_sub, ":container_y", 750),
          (position_set_y, pos1, ":container_y"),
          (overlay_set_position, ":container", pos1),
        (try_end),
        (store_add, ":mesh_parent_u", "$custom_combobox_button", 1),
        (overlay_set_display, ":mesh_parent_u", 1),
      (try_end),

P.S. I haven't tested this code because I use wreck integrated and this code is reverted back manually. So feedback is appreciated.
 
Last edited:
Back
Top Bottom