- Joined
- Nov 7, 2014
- Messages
- 571
A small struct for debugging/seeing ranges/circles (attack ranges, GroupEnumUnitsInRange, TriggerRegisterUnitInRange, etc.).
JASS:
// SelectionCircle API
//
// creates a selection circle at point (x, y) with the specified radius;
// format of color: RRGGBBAA (e.g: 0xFF0000FF = red)
SelectionCircle.create_xy takes real x, real y, real radius, integer color returns SelectionCircle
// creates a selection circle with the specified radius and continuously updates its position
// to match that of the unit
SelectionCircle.attach_to_unit takes unit u, real radius, integer color returns SelectionCircle
self.destroy takes nothing returns nothing
// changes the radius of the selection circle
self.set_radius takes real radius returns nothing
// changes the color of the selection circle
self.set_color takes integer color returns nothing
// changes the position of the selection circle;
// should only be used for selection circles created with 'create_xy'
self.set_pos takes real x, real y returns nothing
// changes the line style of the selection circle
self.set_dotted takes boolean flag returns nothing
JASS:
library SelectionCircle initializer init
globals
// SelectionCircle(s) attached to units are updated this frequently
private constant real Update_Delay = 1.0 / 32.0
endglobals
globals
private integer R
private integer G
private integer B
private integer A
endglobals
private function get_rgba takes integer i returns nothing
local boolean is_neg = false
if i < 0 then
set is_neg = true
set i = i + 0x80000000
endif
set A = i - i / 0x100 * 0x100
set i = i / 0x100
set B = i - i / 0x100 * 0x100
set i = i / 0x100
set G = i - i / 0x100 * 0x100
set i = i / 0x100
set R = i
if is_neg then
set R = R + 0x80
endif
endfunction
struct SelectionCircle
boolean once = true
string file_name
image img
real radius
integer color
real x
real y
unit target
integer ul_pos = 0
method set_radius takes real radius returns nothing
local SelectionCircle sc = this
set sc.radius = radius
// we can't use the check
// if sc.img != null
// because the first image's handle-id is 0 (gj blizzard...)
// and 0 == null :/
if sc.once then
set sc.once = false
else
call DestroyImage(sc.img)
endif
set sc.img = CreateImage(sc.file_name, /*
image-width, image-height:*/ 2.0*radius, 2.0*radius, 0.0, /*
image-position: */ sc.x, sc.y, 0.0, /*
origin: */ radius, radius, 0.0, /*
image-type: */ 1 /*
*/)
call SetImageRenderAlways(sc.img, true)
call get_rgba(sc.color)
call SetImageColor(sc.img, R, G, B, A)
endmethod
static method create takes real x, real y, real radius, integer color returns SelectionCircle
local SelectionCircle sc = SelectionCircle.allocate()
set sc.file_name = "ReplaceableTextures\\Selection\\SelectionCircleLarge.blp"
set sc.x = x
set sc.y = y
set sc.color = color
call sc.set_radius(radius)
return sc
endmethod
static method create_xy takes real x, real y, real radius, integer color returns SelectionCircle
return create(x, y, radius, color)
endmethod
static SelectionCircle array update_list
static integer update_list_len = 0
static code update_cb
static timer ticker = CreateTimer()
static method attach_to_unit takes unit u, real radius, integer color returns SelectionCircle
local SelectionCircle sc = create_xy(GetUnitX(u), GetUnitY(u), radius, color)
set sc.target = u
set update_list_len = update_list_len + 1
set update_list[update_list_len] = sc
set sc.ul_pos = update_list_len
if update_list_len == 1 then
call TimerStart(ticker, Update_Delay, true, update_cb)
endif
return sc
endmethod
method destroy takes nothing returns nothing
local SelectionCircle sc = this
local SelectionCircle last
call DestroyImage(sc.img)
if sc.ul_pos != 0 then
set sc.target = null
set last = update_list[update_list_len]
set last.ul_pos = sc.ul_pos
set update_list[last.ul_pos] = last
set update_list_len = update_list_len - 1
if update_list_len == 0 then
call PauseTimer(ticker)
endif
endif
call sc.deallocate()
endmethod
static method update takes nothing returns nothing
local integer i
local SelectionCircle sc
set i = 1
loop
exitwhen i > update_list_len
set sc = update_list[i]
set sc.x = GetUnitX(sc.target)
set sc.y = GetUnitY(sc.target)
call SetImagePosition(sc.img, sc.x, sc.y, 0.0)
set i = i + 1
endloop
endmethod
method set_color takes integer color returns nothing
local SelectionCircle sc = this
set sc.color = color
call get_rgba(color)
call SetImageColor(sc.img, R, G, B, A)
endmethod
method set_pos takes real x, real y returns nothing
local SelectionCircle sc = this
set sc.x = x
set sc.y = y
call SetImagePosition(sc.img, x, y, 0.0)
endmethod
method set_dotted takes boolean flag returns nothing
local SelectionCircle sc = this
if flag then
set sc.file_name = "ReplaceableTextures\\Selection\\SelectionCircleLargeDotted.blp"
else
set sc.file_name = "ReplaceableTextures\\Selection\\SelectionCircleLarge.blp"
endif
call sc.set_radius(sc.radius)
endmethod
endstruct
private function init takes nothing returns nothing
set SelectionCircle.update_cb = function SelectionCircle.update
call ExecuteFunc(SCOPE_PRIVATE + "playground")
endfunction
private function playground takes nothing returns nothing
local player red = Player(0)
local player blue = Player(1)
local unit u
local SelectionCircle sc
call SetTimeOfDay(12.0)
set u = CreateUnit(blue, 'hgtw', 0.0, 0.0, 270.0)
set sc = SelectionCircle.create_xy(GetUnitX(u), GetUnitY(u), 800.0, 0xFF0000FF)
call sc.set_dotted(true)
set u = CreateUnit(red, 'Hamg', 0.0, -800.0, 270.0)
call SelectionCircle.attach_to_unit(u, 700.0, 0xF3DB29FF)
set u = CreateUnit(red, 'hmtm', -1200.0, -1200.0, 270.0)
call SelectionCircle.attach_to_unit(u, 1250.0, 0xFF00FFFF)
endfunction
endlibrary
Attachments
Last edited: