LibCombatAlerts

MoveableControl Reference

Table of Contents

  1. Prerequisites
  2. Getting Started: Basic Usage Example
  3. Position Format
  4. Functions
  5. Events
  6. Demo / Sample Code

Prerequisites

Important: Significant improvements were made to MoveableControl in version 0.7.1 (007010), and this document assumes that you are working with version 0.7.1 or newer.

First, make sure LibCombatAlerts is listed as a dependency in your addon's manifest:

## DependsOn: LibCombatAlerts>=7010

The library can be accessed via the LibCombatAlerts global variable, and it is recommended that you localize it for usage; for example:

local LCA = LibCombatAlerts

Getting Started: Basic Usage Example

Assuming you have already created a top-level control named myTopLevelControl (it is also recommended that your top-level control has the following attributes: mouseEnabled="true" movable="true" clampedToScreen="true"), you can enable MoveableControl for your control with the following:

local myPositionHandler = LCA.MoveableControl:New(myTopLevelControl)

Next, you will want to set an initial position for your control, and assuming you are loading a position from your addon's saved variables, the code will look something like this:

myPositionHandler:UpdatePosition(MyAddon.savedVars.pos)

Finally, to save the new position to your addon's saved variables whenever the control is moved:

myPositionHandler:RegisterCallback("MyAddonsNameHere", LCA.EVENT_CONTROL_MOVE_STOP, function(newPos)
   MyAddon.savedVars.pos = newPos
end)

Position Format

The positions that MoveableControl accepts as input and produces as output have the following format:

position = {
   horizontal_anchor = horizontal_offset,
   vertical_anchor = vertical_offset,
}

horizontal_anchor can be any one of the following:

left
center
right

vertical_anchor can be any one of the following:

top
mid
bottom

For example, a position where the control's center is aligned with the screen's center and the control's top edge is 42 units down from the screen's top edge will look like this:

position = {
   center = 0,
   top = 42,
}

Functions

Create and attach a MoveableControl position handler object to a control:

New( control, options )

control is a top-level control.

options is an optional table to specify the color and thickness of the anchor lines (e.g., { color = 0xFF0000FF, size = 2 }); if it is omitted, then the default color and thickness are white and 4, respectively.

Returns: A MoveableControl object

Start or stop gamepad movement:

ToggleGamepadMove( enable, timeout )

enable (boolean) specifies whether you want to start or stop gamepad movement mode.

If there is no movement for the duration specified by timeout (milliseconds), gamepad movement mode will end. If it is omitted, then the default timeout of 3000ms will be used.

The start of gamepad movement mode will trigger the LCA.EVENT_CONTROL_MOVE_START event just like mouse movement, and the end of gamepad movement mode—regardless of whether it was ended by timeout or by an explicit call—will trigger the LCA.EVENT_CONTROL_MOVE_STOP event just like mouse movement.

Set a new position:

UpdatePosition( pos )

pos is a position in the format specified above.

Get the current position:

GetPosition( bypassCache )

If bypassCache is true then the current position is recomputed. Recomputation of the position is necessary only if the control was moved by means outside of MoveableControl's purview; e.g., by an addon calling SetAnchor on the control.

Returns: A position in the format specified above

Get the current left/top position:

GetLeftTopPosition( )

Returns: A position in the format specified above, except that the anchoring ignores whatever the player has set and is always left/top

Toggle position lock:

ToggleLock( locked )

locked (boolean) is the new lock state.

Note that this only affects movement via mouse drag.

Set position snap:

SetSnap( snap )

snap (integer) is the snap size; a setting of either 0 or nil will disable snapping.

Snapping is applied at the conclusion of either mouse or gamepad movement, immediately before the LCA.EVENT_CONTROL_MOVE_STOP event is triggered.

Events

Registration:

RegisterCallback( name, eventCode, callback )

name is a unique identifier for your callback function.

eventCode is one of the event codes listed below.

callback is the function to be called; omit this to unregister a callback.

Start of mouse or gamepad movement:

LCA.EVENT_CONTROL_MOVE_START

Conclusion of mouse or gamepad movement:

LCA.EVENT_CONTROL_MOVE_STOP

The final post-snapping position in the format specified above will be passed to the callback function as the first parameter.

Demo / Sample Code

A simple, fully-functional demo of the usage of MoveableControl is available below:

💾 MoveableControlDemo.zip

Return to LibCombatAlerts Developer Reference