PDA

View Full Version : Overriding ImageControl.onMeasurePointer(...)



ronnke
October 16th, 2017, 15:03
I'm overriding the onMeasurePointer event in order to apply some custom calculations to the pointer label. Specifically, I need to get the pointer length. Previously I have done this by defining a measureVector function to calculate the pointer length and then going from there.

Does the API expose the underlining measureVector function which the base CoreRPG uses to calculate pointer lengths? A custom measureVector function works, but it would better to utilise the built-in version if available.

Thanks.

Moon Wizard
October 16th, 2017, 20:24
The API does not expose a measureVector function. If you override the onMeasurePointer event, the client assumes you are handling all the calculations.

There's probably some older Lua code from v2.x time frame that is already written to handle this. I might be able to find an old copy to pass you, if you need.

Cheers,
JPG

ronnke
October 16th, 2017, 22:03
If you have some code, that would be great.

Would it be possible to call the inherited onMeasurePointer from the overridden onMeasurePointer and get the pointer length that way?

Moon Wizard
October 16th, 2017, 22:46
Wow, apparently it's been included for longer than I remember; since I had to go back to rulesets prior to v2.9.4.

Here's the MeasurePointer code from my d20_JPG ruleset back in 2009:



function onMeasureVector(token, vector)
if hasGrid() then
local diagonals = 0;
local straights = 0;
local gridsize = getGridSize();

for i = 1, #vector do
local gx = math.abs(math.floor(vector[i].x / gridsize));
local gy = math.abs(math.floor(vector[i].y / gridsize));

if gx > gy then
diagonals = diagonals + gy;
straights = straights + gx - gy;
else
diagonals = diagonals + gx;
straights = straights + gy - gx;
end
end

local squares = math.floor(diagonals * 1.5) + straights;
local feet = squares * 5;

return feet .. "\'";
else
return "";
end
end


Cheers,
JPG

Moon Wizard
October 16th, 2017, 22:46
There's no base function or default function to call in the API. If the event function doesn't exist, the client just handles internally.

Cheers,
JPG

ronnke
October 16th, 2017, 23:08
Wow, apparently it's been included for longer than I remember; since I had to go back to rulesets prior to v2.9.4.

Here's the MeasurePointer code from my d20_JPG ruleset back in 2009:

Cheers,
JPG

Many thanks, however, I'm working with hex grids not squares. I have some code I used pre-core, but I was hoping to avoid that as I seem to recall it had rounding issues with hex grids of uneven gridsize. The internal calculation used post core seems to get this right. Is it possible to share the code/method used internally to get the pointer/vector length, then I could migrate this to lua and hopefully arrive at the same calculation?

Moon Wizard
October 16th, 2017, 23:36
That's a little more complicated, since it's all C++ code. I don't think there ever was hex specific Lua code (that I remember).



string MeasureTokenVector(CTokenInstance* pToken)
{
float fDist = floor(MeasureVector(pToken->m_iVector));
stringstream ssDist;
if (fDist > 0)
ssDist << (fDist * s_fBaseUnit) << CImageValue::s_sDistanceSuffix;
return ssDist.str();
}

float MeasureVector(list<POINT>& iVector)
{
if (m_nGridSize <= 0)
return 0;

float fDist = 0;
if ((m_nGridType == IVGT_HEXROW) || (m_nGridType == IVGT_HEXCOLUMN))
{
int qw = (int)(m_nGridSize * tan(3.14159/6) / 2.0f);
int hh = m_nGridSize / 2;

if ((qw > 0) && (hh > 0))
{
for (auto it = iVector.begin(); it != iVector.end(); ++it)
{
POINT& pt = *it;

float fColumn, fRow;
if (m_nGridType == IVGT_HEXCOLUMN)
{
fColumn = (float)pt.x / (float)(qw * 3);
fRow = ((float)pt.y / (float)(hh*2)) - (float)(fColumn * 0.5);
}
else
{
fRow = (float)pt.y / (float)(qw*3);
fColumn = ((float)pt.x / (float)(hh*2)) - (float)(fRow * 0.5);
}

if ((fRow >= 0 && fColumn >= 0) || (fRow < 0 && fColumn < 0))
fDist += fabs(fColumn) + fabs(fRow);
else
fDist += max(fabs(fColumn), fabs(fRow));
}
}
}
// m_nGridType == IVGT_GRID
else
{
float fDiagonals = 0;
float fStraights = 0;

for (auto it = iVector.begin(); it != iVector.end(); ++it)
{
POINT& pt = *it;

float gx = (float)fabs((float)pt.x / (float)m_nGridSize);
float gy = (float)fabs((float)pt.y / (float)m_nGridSize);

fDiagonals += min(gx, gy);
fStraights += fabs(gx - gy);
}

fDist = fStraights + (fDiagonals * s_fDiagMult);
}

return fDist;
}


Cheers,
JPG

ronnke
October 17th, 2017, 00:10
That's a little more complicated, since it's all C++ code. I don't think there ever was hex specific Lua code (that I remember).

Thanks! That's perfect.